Class: Sunrise::ModelFilter
- Inherits:
-
Object
- Object
- Sunrise::ModelFilter
- Defined in:
- lib/sunrise/model_filter.rb
Instance Attribute Summary collapse
-
#attributes ⇒ Object
Returns the value of attribute attributes.
-
#columns ⇒ Object
Returns the value of attribute columns.
-
#conditions ⇒ Object
Returns the value of attribute conditions.
-
#klass ⇒ Object
Returns the value of attribute klass.
-
#order_by ⇒ Object
Returns the value of attribute order_by.
-
#order_column ⇒ Object
Returns the value of attribute order_column.
-
#order_type ⇒ Object
Returns the value of attribute order_type.
Instance Method Summary collapse
- #filter(options = {}) ⇒ Object
-
#initialize(klass, options = {}) ⇒ ModelFilter
constructor
A new instance of ModelFilter.
- #method_missing(method, *args, &block) ⇒ Object
- #next_order_type ⇒ Object
- #respond_to?(method_name) ⇒ Boolean
- #scoped ⇒ Object
- #update_attributes(params = {}) ⇒ Object
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, ={}) .symbolize_keys! @klass = klass @columns = [:columns] @columns ||= klass.respond_to?(:column_names) ? @klass.column_names : [] @attributes = [: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
#attributes ⇒ Object
Returns the value of attribute attributes.
6 7 8 |
# File 'lib/sunrise/model_filter.rb', line 6 def attributes @attributes end |
#columns ⇒ Object
Returns the value of attribute columns.
6 7 8 |
# File 'lib/sunrise/model_filter.rb', line 6 def columns @columns end |
#conditions ⇒ Object
Returns the value of attribute conditions.
10 11 12 |
# File 'lib/sunrise/model_filter.rb', line 10 def conditions @conditions end |
#klass ⇒ Object
Returns the value of attribute klass.
4 5 6 |
# File 'lib/sunrise/model_filter.rb', line 4 def klass @klass end |
#order_by ⇒ Object
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_column ⇒ Object
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_type ⇒ Object
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( = {}) order = .delete(:order) where = .delete(:conditions) { :order => (self.order_by || order || "id DESC"), :conditions => (self.conditions || where) }.merge() end |
#next_order_type ⇒ Object
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
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 |
#scoped ⇒ Object
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? = params.symbolize_keys @raw_values = extract_values() @conditions = extract_conditions() @order_by = extract_order() end |