Class: Livelist::Rails::FilterCriteria
- Inherits:
-
HashWithIndifferentAccess
- Object
- HashWithIndifferentAccess
- Livelist::Rails::FilterCriteria
- Defined in:
- lib/livelist/rails/filter_criteria.rb
Instance Attribute Summary collapse
-
#metadata_properties ⇒ Object
readonly
Returns the value of attribute metadata_properties.
-
#slug ⇒ Object
readonly
Returns the value of attribute slug.
Instance Method Summary collapse
- #as_json(params) ⇒ Object
- #counts=(counts_hash) ⇒ Object
- #create_criterion(reference) ⇒ Object
- #default_reference_criteria ⇒ Object
-
#initialize(options) ⇒ FilterCriteria
constructor
A new instance of FilterCriteria.
- #initialize_criteria(reference_criteria) ⇒ Object
- #set_criteria(reference_criteria) ⇒ Object
- #slugs ⇒ Object
Constructor Details
#initialize(options) ⇒ FilterCriteria
Returns a new instance of FilterCriteria.
12 13 14 15 16 17 18 |
# File 'lib/livelist/rails/filter_criteria.rb', line 12 def initialize() @filter = [:filter] @slug = [:slug] @metadata_properties = [:metadata_properties] || {} initialize_criteria([:reference_criteria]) end |
Instance Attribute Details
#metadata_properties ⇒ Object (readonly)
Returns the value of attribute metadata_properties.
10 11 12 |
# File 'lib/livelist/rails/filter_criteria.rb', line 10 def @metadata_properties end |
#slug ⇒ Object (readonly)
Returns the value of attribute slug.
10 11 12 |
# File 'lib/livelist/rails/filter_criteria.rb', line 10 def slug @slug end |
Instance Method Details
#as_json(params) ⇒ Object
53 54 55 56 57 |
# File 'lib/livelist/rails/filter_criteria.rb', line 53 def as_json(params) criteria.map do |criterion| criterion.as_json(params) end end |
#counts=(counts_hash) ⇒ Object
47 48 49 50 51 |
# File 'lib/livelist/rails/filter_criteria.rb', line 47 def counts=(counts_hash) criteria.each do |criterion| criterion.count = counts_hash[criterion.slug.to_s] || 0 end end |
#create_criterion(reference) ⇒ Object
38 39 40 41 |
# File 'lib/livelist/rails/filter_criteria.rb', line 38 def create_criterion(reference) filter_criterion = FilterCriterion.new(:filter => @filter, :criteria => self, :reference => reference) self[filter_criterion.slug] = filter_criterion end |
#default_reference_criteria ⇒ Object
31 32 33 34 35 36 |
# File 'lib/livelist/rails/filter_criteria.rb', line 31 def default_reference_criteria case @filter.type when :attribute then @filter.model_class.select("distinct #{@filter.slug}") when :association then @filter.filter_class.scoped end end |
#initialize_criteria(reference_criteria) ⇒ Object
20 21 22 23 24 |
# File 'lib/livelist/rails/filter_criteria.rb', line 20 def initialize_criteria(reference_criteria) reference_criteria ||= default_reference_criteria reference_criteria = reference_criteria.call if reference_criteria.respond_to?(:call) reference_criteria.each { |reference| create_criterion(reference) } end |
#set_criteria(reference_criteria) ⇒ Object
26 27 28 29 |
# File 'lib/livelist/rails/filter_criteria.rb', line 26 def set_criteria(reference_criteria) clear reference_criteria.each { |reference| create_criterion(reference) } end |
#slugs ⇒ Object
43 44 45 |
# File 'lib/livelist/rails/filter_criteria.rb', line 43 def slugs criteria.map(&:slug) end |