Class: ScopedSearch::Base
- Inherits:
-
Object
- Object
- ScopedSearch::Base
- Extended by:
- ActiveModel::Naming
- Defined in:
- lib/scoped_search.rb
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
-
#attributes_merged ⇒ Object
readonly
Returns the value of attribute attributes_merged.
-
#model_class ⇒ Object
readonly
Returns the value of attribute model_class.
Instance Method Summary collapse
- #build_relation ⇒ Object
-
#initialize(klass, options) ⇒ Base
constructor
A new instance of Base.
- #method_missing(method_name, *args) ⇒ Object
- #to_key ⇒ Object
Constructor Details
#initialize(klass, options) ⇒ Base
Returns a new instance of Base.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/scoped_search.rb', line 11 def initialize(klass, ) @model_class = klass @attributes = @attributes_merged = @attributes.reverse_merge(klass.scopes.keys.inject({}) { |m,o| m[o] = nil; m; }) @attributes_merged.each do |attribute, value| class_eval <<-RUBY attr_accessor :#{attribute}_multi_params def #{attribute} @attributes[:#{attribute}] end def #{attribute}=(val) @attributes[:#{attribute}] = val end RUBY end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args) ⇒ Object
51 52 53 |
# File 'lib/scoped_search.rb', line 51 def method_missing(method_name, *args) build_relation.send(method_name, *args) end |
Instance Attribute Details
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
9 10 11 |
# File 'lib/scoped_search.rb', line 9 def attributes @attributes end |
#attributes_merged ⇒ Object (readonly)
Returns the value of attribute attributes_merged.
9 10 11 |
# File 'lib/scoped_search.rb', line 9 def attributes_merged @attributes_merged end |
#model_class ⇒ Object (readonly)
Returns the value of attribute model_class.
9 10 11 |
# File 'lib/scoped_search.rb', line 9 def model_class @model_class end |
Instance Method Details
#build_relation ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/scoped_search.rb', line 31 def build_relation return model_class if attributes.empty? attributes.reject { |k,v| v.blank? || v.to_s == "false" }.inject(model_class) do |s, k| if model_class.scopes.keys.include?(k.first.to_sym) if k.second.is_a?(Array) && multi_params?(k.first) s.send(*k.flatten) elsif k.second.to_s == "true" s.send(k.first) else s.send(k.first, k.second) end else s end end end |
#to_key ⇒ Object
49 |
# File 'lib/scoped_search.rb', line 49 def to_key; nil; end |