impress/app/models/item/search/fields/set_field.rb
Matchu 6e09b8bc10 globalized search first draft
Confirmed features:
    * Output (retrieval, sorting, etc.)
    * Name (positive and negative, but new behavior)
    * Flags (positive and negative)

Planned features:
    * users:owns, user:wants

Known issues:
    * Sets are broken
        * Don't render properly
        * Shouldn't actually be done as joined sets, anyway, since
          we actually want (set1_zone1 OR set1_zone2) AND
          (set2_zone1 OR set2_zone2), which will require breaking
          it into multiple terms queries.
    * Name has regressed: ignores phrases, doesn't require *all*
      words. While we're breaking sets into multiple queries,
      maybe we'll do something similar for name. In fact, we
      really kinda have to if we're gonna keep sorting by name,
      since "straw hat" returns all hats. Eww.
2013-01-24 18:24:35 -06:00

44 lines
1.1 KiB
Ruby

class Item
module Search
module Fields
class SetField < Field
def initialize(*args)
super(*args)
@values = {true => Set.new, false => Set.new}
end
def <<(filter)
if filter.value.respond_to?(:each)
filter.value.each do |value|
add_value(value, filter.positive?)
end
else
add_value(filter.value, filter.positive?)
end
end
def to_flex_params
{
key => nil_if_empty(@values[true]),
:"negative_#{key}" => nil_if_empty(@values[false])
}
end
private
def add_value(value, is_positive)
if @values[!is_positive].include?(value)
raise Item::Search::Contradiction,
"positive #{key} and negative #{key} both contain #{value}"
end
@values[is_positive] << value
end
def nil_if_empty(set)
set unless set.empty?
end
end
end
end
end