Class: Sunrise::ModelFilter

Inherits:
Object
  • Object
show all
Defined in:
lib/sunrise/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/sunrise/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



57
58
59
60
61
62
63
64
65
66
# File 'lib/sunrise/model_filter.rb', line 57

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/sunrise/model_filter.rb', line 6

def attributes
  @attributes
end

#columnsObject

Returns the value of attribute columns.



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

def columns
  @columns
end

#conditionsObject

Returns the value of attribute conditions.



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

def conditions
  @conditions
end

#klassObject

Returns the value of attribute klass.



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

def klass
  @klass
end

#order_byObject

Returns the value of attribute order_by.



8
9
10
# File 'lib/sunrise/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/sunrise/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/sunrise/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/sunrise/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



41
42
43
44
45
# File 'lib/sunrise/model_filter.rb', line 41

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

#respond_to?(method_name) ⇒ Boolean

Returns:

  • (Boolean)


68
69
70
71
# File 'lib/sunrise/model_filter.rb', line 68

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

#scopedObject



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

def scoped
  query = @klass.scoped
  query = query.order(order_by) unless order_by.blank?
  query = query.where(conditions) if conditions && !conditions.empty? 
  query
end

#update_attributes(params = {}) ⇒ Object



47
48
49
50
51
52
53
54
55
# File 'lib/sunrise/model_filter.rb', line 47

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