Class: Livelist::Rails::Filter
- Inherits:
-
Object
- Object
- Livelist::Rails::Filter
- Defined in:
- lib/livelist/rails/filter.rb
Constant Summary collapse
- DEFAULT_FILTER_OPTIONS =
{ :reference_criteria => nil }
Instance Attribute Summary collapse
-
#attribute ⇒ Object
Returns the value of attribute attribute.
-
#criteria ⇒ Object
Returns the value of attribute criteria.
-
#criterion_label ⇒ Object
Returns the value of attribute criterion_label.
-
#join ⇒ Object
Returns the value of attribute join.
-
#key_name ⇒ Object
Returns the value of attribute key_name.
-
#model_class ⇒ Object
Returns the value of attribute model_class.
-
#model_name ⇒ Object
Returns the value of attribute model_name.
-
#name ⇒ Object
Returns the value of attribute name.
-
#slug ⇒ Object
Returns the value of attribute slug.
-
#type ⇒ Object
Returns the value of attribute type.
Instance Method Summary collapse
- #as_json(params) ⇒ Object
- #counts(query, params) ⇒ Object
- #counts_relation(query, filter, params) ⇒ Object
- #default_key_name ⇒ Object
- #exclude_filter_relation?(matching_filter, params) ⇒ Boolean
- #filter_class ⇒ Object
- #group_by ⇒ Object
-
#initialize(options = {}) ⇒ Filter
constructor
slug should always be a symbol.
- #initialize_type ⇒ Object
- #prepare_options(options) ⇒ Object
- #relation(query, params, exclude_params_relation) ⇒ Object
- #set_criteria_counts(query, params) ⇒ Object
- #table_name ⇒ Object
- #where(slug_params) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Filter
slug should always be a symbol
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/livelist/rails/filter.rb', line 23 def initialize( = {}) raise ArgumentError, 'slug option required' unless [:slug] raise ArgumentError, 'model_name option required' unless [:model_name] @filter_collection = [:filter_collection] @slug = [:slug].to_sym @name = [:name] || @slug.to_s.capitalize @model_name = [:model_name] @type = [:type] || initialize_type @key_name = [:key_name] || default_key_name @attribute = [:attribute] || default_key_name @criterion_label = [:criterion_label] @criteria = FilterCriteria.new( :filter => self, :reference_criteria => [:reference_criteria], :metadata_properties => [:option_metadata_properties], :slug => @key_name ) end |
Instance Attribute Details
#attribute ⇒ Object
Returns the value of attribute attribute.
11 12 13 |
# File 'lib/livelist/rails/filter.rb', line 11 def attribute @attribute end |
#criteria ⇒ Object
Returns the value of attribute criteria.
11 12 13 |
# File 'lib/livelist/rails/filter.rb', line 11 def criteria @criteria end |
#criterion_label ⇒ Object
Returns the value of attribute criterion_label.
11 12 13 |
# File 'lib/livelist/rails/filter.rb', line 11 def criterion_label @criterion_label end |
#join ⇒ Object
Returns the value of attribute join.
11 12 13 |
# File 'lib/livelist/rails/filter.rb', line 11 def join @join end |
#key_name ⇒ Object
Returns the value of attribute key_name.
11 12 13 |
# File 'lib/livelist/rails/filter.rb', line 11 def key_name @key_name end |
#model_class ⇒ Object
Returns the value of attribute model_class.
11 12 13 |
# File 'lib/livelist/rails/filter.rb', line 11 def model_class @model_class end |
#model_name ⇒ Object
Returns the value of attribute model_name.
11 12 13 |
# File 'lib/livelist/rails/filter.rb', line 11 def model_name @model_name end |
#name ⇒ Object
Returns the value of attribute name.
11 12 13 |
# File 'lib/livelist/rails/filter.rb', line 11 def name @name end |
#slug ⇒ Object
Returns the value of attribute slug.
11 12 13 |
# File 'lib/livelist/rails/filter.rb', line 11 def slug @slug end |
#type ⇒ Object
Returns the value of attribute type.
11 12 13 |
# File 'lib/livelist/rails/filter.rb', line 11 def type @type end |
Instance Method Details
#as_json(params) ⇒ Object
110 111 112 113 114 115 116 |
# File 'lib/livelist/rails/filter.rb', line 110 def as_json(params) { :filter_slug => @slug, :name => @name, :options => @criteria.as_json(params) } end |
#counts(query, params) ⇒ Object
70 71 72 73 74 75 |
# File 'lib/livelist/rails/filter.rb', line 70 def counts(query, params) @filter_collection.filters.each do |filter| query = counts_relation(query, filter, params) end query.except(:order).group(group_by).count.stringify_keys end |
#counts_relation(query, filter, params) ⇒ Object
64 65 66 67 68 |
# File 'lib/livelist/rails/filter.rb', line 64 def counts_relation(query, filter, params) exclude_params_relation = exclude_filter_relation?(filter, params[@slug]) counts_scope = filter.relation(query, params[filter.slug], exclude_params_relation) query.merge(counts_scope) end |
#default_key_name ⇒ Object
84 85 86 87 88 89 |
# File 'lib/livelist/rails/filter.rb', line 84 def default_key_name case @type when :association then @attribute || :id when :attribute then @slug end end |
#exclude_filter_relation?(matching_filter, params) ⇒ Boolean
56 57 58 |
# File 'lib/livelist/rails/filter.rb', line 56 def exclude_filter_relation?(matching_filter, params) params.nil? || (self == matching_filter) end |
#filter_class ⇒ Object
102 103 104 |
# File 'lib/livelist/rails/filter.rb', line 102 def filter_class slug.to_s.classify.constantize end |
#group_by ⇒ Object
49 50 51 52 53 54 |
# File 'lib/livelist/rails/filter.rb', line 49 def group_by case @type when :attribute then "#{model_name.tableize}.#{@slug}" when :association then "#{@slug}.id" end end |
#initialize_type ⇒ Object
118 119 120 121 122 123 124 125 126 |
# File 'lib/livelist/rails/filter.rb', line 118 def initialize_type if model_class.column_names.include?(@slug.to_s) :attribute elsif model_class.reflect_on_all_associations.map(&:name).include?(@slug) :association else nil end end |
#prepare_options(options) ⇒ Object
43 44 45 46 47 |
# File 'lib/livelist/rails/filter.rb', line 43 def () ||= {} .reverse_merge!(DEFAULT_FILTER_OPTIONS) @criteria.set_criteria([:reference_criteria]) if [:reference_criteria] end |
#relation(query, params, exclude_params_relation) ⇒ Object
77 78 79 80 81 82 |
# File 'lib/livelist/rails/filter.rb', line 77 def relation(query, params, exclude_params_relation) query = query.includes(@slug) if @type == :association query = query.where(where(@criteria.slugs)) query = query.where(where(params)) unless exclude_params_relation query end |
#set_criteria_counts(query, params) ⇒ Object
60 61 62 |
# File 'lib/livelist/rails/filter.rb', line 60 def set_criteria_counts(query, params) @criteria.counts = counts(query, params) end |
#table_name ⇒ Object
91 92 93 94 95 96 |
# File 'lib/livelist/rails/filter.rb', line 91 def table_name case @type when :association then @slug when :attribute then model_name.tableize end end |
#where(slug_params) ⇒ Object
106 107 108 |
# File 'lib/livelist/rails/filter.rb', line 106 def where(slug_params) { table_name => { @attribute => slug_params } } end |