Sha256: c0363d70003e9b5552dfcbdb70083e09fa5e29170c9711ce109fdf182ba7aea8
Contents?: true
Size: 1.74 KB
Versions: 2
Compression:
Stored size: 1.74 KB
Contents
require 'active_support/hash_with_indifferent_access' require 'livelist/rails/filter_criterion' module Livelist module Rails class FilterCriteria < HashWithIndifferentAccess alias :criteria :values alias :find_criteria :[] attr_reader :slug, :metadata_properties def initialize(options) @filter = options[:filter] @slug = options[:slug] @metadata_properties = options[:metadata_properties] || {} initialize_criteria(options[:reference_criteria]) end 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 def set_criteria(reference_criteria) clear reference_criteria.each { |reference| create_criterion(reference) } end 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 def create_criterion(reference) filter_criterion = FilterCriterion.new(:filter => @filter, :criteria => self, :reference => reference) self[filter_criterion.slug] = filter_criterion end def slugs criteria.map(&:slug) end def counts=(counts_hash) criteria.each do |criterion| criterion.count = counts_hash[criterion.slug.to_s] || 0 end end def as_json(params) criteria.map do |criterion| criterion.as_json(params) end end end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
livelist-rails-0.0.18 | lib/livelist/rails/filter_criteria.rb |
livelist-rails-0.0.17 | lib/livelist/rails/filter_criteria.rb |