Module: ActiveRecord::Filter::ClassMethods

Defined in:
lib/active_record/filter.rb

Instance Method Summary collapse

Instance Method Details

#filterize(attributes = modal_attributes_with_options, options = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/active_record/filter.rb', line 6

def filterize(attributes = modal_attributes_with_options, options = {})
  if attributes.is_a?(Hash)
    options = attributes
    attributes = accessible_attributes
  end

  attributes = Array(attributes)

  class_eval(<<-eoruby, __FILE__, __LINE__ + 1)
    def self.filter(params)
      Filters::Filter.new(self, #{attributes}, #{options}).call(params)
    end
  eoruby
end

#orderize(*attributes) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/active_record/filter.rb', line 21

def orderize(*attributes)
  options = attributes.extract_options!

  if options.any?
    include_table = options.fetch(:on)
    table_name = include_table.to_s.classify.constantize.table_name
    attributes = "#{table_name}.#{attributes.first}" if include_table.present?
  else
    attributes = [:name] if attributes.empty?

    attributes.map! do |attribute|
      # Converte symbols para Arel::Nodes::Ordering
      attribute = arel_table[attribute].asc if attribute.is_a?(Symbol)

      if attribute.is_a?(Arel::Attributes::Attribute)
        attribute = attribute.asc
      end

      if attribute.class < Arel::Nodes::Ordering
        # Converte Arel::Nodes::Ordering para string com prefixo
        # da tabela e quoted
        arel_table.engine.connection.visitor.accept attribute
      else
        attribute
      end
    end

    attributes = attributes.join(', ')
  end

  class_eval(<<-eoruby, __FILE__, __LINE__ + 1)
    def self.ordered
      query = order('#{attributes}')

      #{"query = query.joins(:#{include_table})" if include_table.present?}

      query
    end
  eoruby
end