Class: Headmin::Filter::AssociationCount

Inherits:
Base
  • Object
show all
Defined in:
app/models/headmin/filter/association_count.rb

Constant Summary collapse

OPERATORS =
%w[eq not_eq gt gteq lt lteq]

Constants inherited from Base

Base::OPERATORS_CONVERT_TO, Base::QUERY_OPERATOR_CONVERT_TO, Base::QUERY_VALUE_CONVERT_TO

Instance Attribute Summary

Attributes inherited from Base

#attribute, #instructions, #raw_value

Instance Method Summary collapse

Methods inherited from Base

#conditionals, #display_value, #initialize, #is_i?, #operators, #string, #values

Constructor Details

This class inherits a constructor from Headmin::Filter::Base

Instance Method Details

#cast_value(value) ⇒ Object



6
7
8
# File 'app/models/headmin/filter/association_count.rb', line 6

def cast_value(value)
  is_i?(value) ? value.to_i : 0
end

#query(collection) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'app/models/headmin/filter/association_count.rb', line 10

def query(collection)
  return collection unless @instructions.any?

  # Store the collections' class for later use
  @parent_class = collection.is_a?(Class) ? collection : collection.klass

  # Join table and group on primary key if necessary
  collection = collection.left_joins(reflection.name).group(primary_key)

  # Build query and execute
  query = nil
  @instructions.each do |instruction|
    query = build_query(query, collection, instruction)
  end
  collection.having(query)
end