Class: Livelist::Rails::Filter

Inherits:
Object
  • Object
show all
Defined in:
lib/livelist/rails/filter.rb

Constant Summary collapse

DEFAULT_FILTER_OPTIONS =
{ :reference_criteria => nil }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Filter

slug should always be a symbol

Raises:

  • (ArgumentError)


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(options = {})
  raise ArgumentError, 'slug option required' unless options[:slug]
  raise ArgumentError, 'model_name option required' unless options[:model_name]

  @filter_collection = options[:filter_collection]
  @slug              = options[:slug].to_sym
  @name              = options[:name] || @slug.to_s.capitalize
  @model_name        = options[:model_name]
  @type              = options[:type] || initialize_type
  @key_name          = options[:key_name] || default_key_name
  @attribute         = options[:attribute] || default_key_name
  @criterion_label   = options[:criterion_label]
  @criteria          = FilterCriteria.new(
                         :filter => self,
                         :reference_criteria => options[:reference_criteria],
                         :metadata_properties => options[:option_metadata_properties],
                         :slug => @key_name
                       )
end

Instance Attribute Details

#attributeObject

Returns the value of attribute attribute.



11
12
13
# File 'lib/livelist/rails/filter.rb', line 11

def attribute
  @attribute
end

#criteriaObject

Returns the value of attribute criteria.



11
12
13
# File 'lib/livelist/rails/filter.rb', line 11

def criteria
  @criteria
end

#criterion_labelObject

Returns the value of attribute criterion_label.



11
12
13
# File 'lib/livelist/rails/filter.rb', line 11

def criterion_label
  @criterion_label
end

#joinObject

Returns the value of attribute join.



11
12
13
# File 'lib/livelist/rails/filter.rb', line 11

def join
  @join
end

#key_nameObject

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_classObject

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_nameObject

Returns the value of attribute model_name.



11
12
13
# File 'lib/livelist/rails/filter.rb', line 11

def model_name
  @model_name
end

#nameObject

Returns the value of attribute name.



11
12
13
# File 'lib/livelist/rails/filter.rb', line 11

def name
  @name
end

#slugObject

Returns the value of attribute slug.



11
12
13
# File 'lib/livelist/rails/filter.rb', line 11

def slug
  @slug
end

#typeObject

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_nameObject



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

Returns:

  • (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_classObject



102
103
104
# File 'lib/livelist/rails/filter.rb', line 102

def filter_class
  slug.to_s.classify.constantize
end

#group_byObject



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_typeObject



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 prepare_options(options)
  options ||= {}
  options.reverse_merge!(DEFAULT_FILTER_OPTIONS)
  @criteria.set_criteria(options[:reference_criteria]) if options[: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_nameObject



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