Skip to content

Commit

Permalink
added selected scope to attribute filter definitoin
Browse files Browse the repository at this point in the history
  • Loading branch information
Bastian Knerr committed Jun 2, 2014
1 parent 7a58cac commit 9376b5c
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 6 deletions.
4 changes: 4 additions & 0 deletions lib/forty_facets/filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ def name
filter_definition.options[:name] || filter_definition.model_field
end

def values
@values ||= Array.wrap(value).sort.uniq
end

def empty?
value.nil? || value == '' || value == []
end
Expand Down
11 changes: 10 additions & 1 deletion lib/forty_facets/filter/attribute_filter_definition.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
module FortyFacets
class AttributeFilterDefinition < FilterDefinition
class AttributeFilter < Filter
def selected
entity = search_instance.class.root_class
column = entity.columns_hash[filter_definition.model_field.to_s]
values.map{|v| column.type_cast(v)}
end

def build_scope
return Proc.new { |base| base } if empty?
Proc.new { |base| base.where(filter_definition.model_field => value) }
Expand All @@ -9,7 +15,10 @@ def build_scope
def facet
my_column = filter_definition.model_field
counts = without.result.reorder('').select("#{my_column} AS facet_value, count(#{my_column}) as occurrences").group(my_column)
counts.map{|c| FacetValue.new(c.facet_value, c.occurrences, false)}
counts.map do |c|
is_selected = selected.include?(c.facet_value)
FacetValue.new(c.facet_value, c.occurrences, is_selected)
end
end

def remove(value)
Expand Down
4 changes: 0 additions & 4 deletions lib/forty_facets/filter/belongs_to_filter_definition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ def klass
association.klass
end

def values
@values ||= Array.wrap(value).sort.uniq
end

def selected
@selected ||= klass.find(values)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/forty_facets/filter_definition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module FortyFacets
FilterDefinition = Struct.new(:search, :model_field, :options) do

FacetValue = Struct.new(:entity, :count, :selected)

def request_param
model_field
end
Expand Down
15 changes: 15 additions & 0 deletions test/smoke_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,21 @@ def test_year_add_remove_filter
assert_equal Movie.where(year: 2011).count, search.result.count
end

def test_selected_year_filter
search = MovieSearch.new()

search = search.filter(:year).add(2010)
assert_equal [2010], search.filter(:year).selected

search = search.filter(:year).add(2011)
assert_equal [2010, 2011], search.filter(:year).selected

facet = search.filter(:year).facet
assert facet.find{|fv| fv.entity == 2010}.selected
assert facet.find{|fv| fv.entity == 2011}.selected
assert !facet.find{|fv| fv.entity == 2012}.selected
end

def test_belongs_to_filter
blank_search = MovieSearch.new
first_facet_value = blank_search.filter(:studio).facet.first
Expand Down

0 comments on commit 9376b5c

Please sign in to comment.