Class: Freeberry::ModelFilter

Inherits:
Object
  • Object
show all
Defined in:
lib/freeberry/model_filter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, options = {}) ⇒ ModelFilter

Returns a new instance of ModelFilter.



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/freeberry/model_filter.rb', line 12

def initialize(klass, options={})
  options.symbolize_keys!
  
  @klass = klass
  
  @columns = options[:columns]
  @columns ||= klass.respond_to?(:column_names) ? @klass.column_names : []
  
  @attributes = options[:attributes] || {}
  @raw_values = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



50
51
52
53
54
55
56
57
58
59
# File 'lib/freeberry/model_filter.rb', line 50

def method_missing(method, *args, &block)
  match_data = method.to_s.match(/^(.+)_before_type_cast$/)
  method_name = (match_data ? match_data[1] : method).to_sym
  
  if @attributes.include?(method_name)
    @raw_values[method_name]
  else
    super
  end
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



6
7
8
# File 'lib/freeberry/model_filter.rb', line 6

def attributes
  @attributes
end

#columnsObject

Returns the value of attribute columns.



6
7
8
# File 'lib/freeberry/model_filter.rb', line 6

def columns
  @columns
end

#conditionsObject

Returns the value of attribute conditions.



10
11
12
# File 'lib/freeberry/model_filter.rb', line 10

def conditions
  @conditions
end

#klassObject

Returns the value of attribute klass.



4
5
6
# File 'lib/freeberry/model_filter.rb', line 4

def klass
  @klass
end

#order_byObject

Returns the value of attribute order_by.



8
9
10
# File 'lib/freeberry/model_filter.rb', line 8

def order_by
  @order_by
end

#order_columnObject

Returns the value of attribute order_column.



8
9
10
# File 'lib/freeberry/model_filter.rb', line 8

def order_column
  @order_column
end

#order_typeObject

Returns the value of attribute order_type.



8
9
10
# File 'lib/freeberry/model_filter.rb', line 8

def order_type
  @order_type
end

Instance Method Details

#filter(options = {}) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/freeberry/model_filter.rb', line 24

def filter(options = {})
  order = options.delete(:order)
  where = options.delete(:conditions)
  
  {
    :order => (self.order_by || order || "id DESC"),
    :conditions => (self.conditions || where)
  }.merge(options)
end

#next_order_typeObject



34
35
36
37
38
# File 'lib/freeberry/model_filter.rb', line 34

def next_order_type
  return if @order_type.blank?
  
  @order_type == 'desc' ? 'asc' : 'desc'
end

#respond_to?(method_name) ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
64
# File 'lib/freeberry/model_filter.rb', line 61

def respond_to?(method_name)
  return true if @attributes.include?(method_name.to_sym)
  super
end

#update_attributes(params = {}) ⇒ Object



40
41
42
43
44
45
46
47
48
# File 'lib/freeberry/model_filter.rb', line 40

def update_attributes(params = {})
  return if params.nil? || params.keys.empty?
  
  options = params.symbolize_keys
  
  @raw_values = extract_values(options)
  @conditions = extract_conditions(options)
  @order_by = extract_order(options)
end