Skip to content

Commit

Permalink
Merge 6cd6243 into d67f905
Browse files Browse the repository at this point in the history
  • Loading branch information
TobiasBales committed Mar 19, 2015
2 parents d67f905 + 6cd6243 commit f514315
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/forty_facets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ module FortyFacets
require "forty_facets/filter/range_filter_definition"
require "forty_facets/filter/text_filter_definition"
require "forty_facets/filter/facet_filter_definition"
require "forty_facets/filter/scope_facet_filter_definition"
require "forty_facets/facet_search"
4 changes: 4 additions & 0 deletions lib/forty_facets/facet_search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ def facet(path, opts = {})
definitions << FacetFilterDefinition.new(self, path, opts)
end

def scope_facet(scopes, opts = {})
definitions << ScopeFacetFilterDefinition.new(self, scopes, opts)
end

def orders(name_and_order_options)
@order_definitions = name_and_order_options.to_a.inject([]) {|ods, no| ods << OrderDefinition.new(no.first, no.last)}
end
Expand Down
45 changes: 45 additions & 0 deletions lib/forty_facets/filter/scope_facet_filter_definition.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
module FortyFacets
class ScopeFacetFilterDefinition < FilterDefinition

attr_reader(:scopes)

def initialize search, scopes, opts
@search = search
@scopes = Array(scopes).flatten.map(&:to_sym)
@path = opts[:path]
@path ||= @scopes.join('-')
@options = opts
end

def request_param
path
end

def build_filter(search_instance, param_value)
ScopeFacetFilter.new(self, search_instance, param_value)
end

class ScopeFacetFilter < Filter
def values
@values ||= Array.wrap(value).sort.uniq
end

def build_scope
return Proc.new { |base| base } if empty?

return Proc.new do |base|
# intersection of values and definition scopes
scopes = values.map(&:to_sym) & definition.scopes
scopes.inject(base) do |sum, scope|
sum.send(scope)
end
end
end

def facet
[]
end
end

end
end
5 changes: 4 additions & 1 deletion test/fixtures.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ class Movie < ActiveRecord::Base
has_and_belongs_to_many :genres
has_and_belongs_to_many :actors
has_and_belongs_to_many :writers

scope :classics, -> { where('year <= ?', 1980) }
scope :non_classics, -> { where('year > ?', 1980) }
end

LOREM = %w{Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren}
Expand Down Expand Up @@ -133,7 +136,7 @@ class Movie < ActiveRecord::Base

rand = Random.new
LOREM.each_with_index do |title, index|
m = Movie.create!(title: title, studio: studios[index % studios.length], price: rand.rand(20.0), year: (index%3 + 2010) )
m = Movie.create!(title: title, studio: studios[index % studios.length], price: rand.rand(20.0), year: (index + 1975) )
3.times do
actor = actors[rand(actors.length)]
unless m.actors.include? actor
Expand Down
21 changes: 21 additions & 0 deletions test/smoke_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class MovieSearch < FortyFacets::FacetSearch
facet [:studio, :country], name: 'Country'
facet [:studio, :status], name: 'Studio status'
facet [:studio, :producers], name: 'Producers'
scope_facet [:classics, :non_classics], name: 'Classics', path: 'classics'
scope_facet [:classics, :non_classics], name: 'Classics without Path'
text [:studio, :description], name: 'Studio Description'
end

Expand All @@ -33,6 +35,25 @@ def test_it_finds_all_movies
assert_equal Movie.all.size, search.result.size
end

def test_scope_filter
search = MovieSearch.new({'search' => { 'classics' => 'classics'}})
assert_equal 6, search.result.size
search = MovieSearch.new({'search' => { 'classics' => 'non_classics'}})
assert_equal 34, search.result.size
search = MovieSearch.new({'search' => { 'classics' => ['non_classics', 'classics']}})
assert_equal 0, search.result.size
end

def test_scope_filter_without_path
search = MovieSearch.new({'search' => { 'classics-non_classics' => 'classics'}})
assert_equal 6, search.result.size
search = MovieSearch.new({'search' => { 'classics-non_classics' => 'non_classics'}})
assert_equal 34, search.result.size
search = MovieSearch.new({'search' => { 'classics-non_classics' => ['non_classics', 'classics']}})
assert_equal 0, search.result.size

end

def test_text_filter
search = MovieSearch.new({'search' => { 'title' => 'ipsum' }})
assert_equal 1, search.result.size
Expand Down

0 comments on commit f514315

Please sign in to comment.