Module: Searchlogic::NamedScopes::Conditions
- Defined in:
- lib/searchlogic/named_scopes/conditions.rb
Overview
Handles dynamically creating named scopes for columns.
Constant Summary collapse
- COMPARISON_CONDITIONS =
{ :equals => [:is, :eq], :does_not_equal => [:not_equal_to, :is_not, :not, :ne], :less_than => [:lt, :before], :less_than_or_equal_to => [:lte], :greater_than => [:gt, :after], :greater_than_or_equal_to => [:gte], }
- WILDCARD_CONDITIONS =
{ :like => [:contains, :includes], :begins_with => [:bw], :ends_with => [:ew], }
- BOOLEAN_CONDITIONS =
{ :null => [:nil], :empty => [] }
- CONDITIONS =
{}
- PRIMARY_CONDITIONS =
CONDITIONS.keys
- ALIAS_CONDITIONS =
CONDITIONS.values.flatten
Instance Method Summary collapse
-
#alias_condition?(name) ⇒ Boolean
Is the name of the method a valid condition that can be dynamically created, AND is it an alias condition.
-
#condition?(name) ⇒ Boolean
Is the name of the method a valid condition that can be dynamically created?.
-
#named_scope_arity(name) ⇒ Object
The arity for a named scope’s proc is important, because we use the arity to determine if the condition should be ignored when calling the search method.
-
#named_scope_options(name) ⇒ Object
Retrieves the options passed when creating the respective named scope.
-
#primary_condition(alias_condition) ⇒ Object
Returns the primary condition for the given alias.
-
#primary_condition?(name) ⇒ Boolean
Is the name of the method a valid condition that can be dynamically created, AND is it a primary condition (not an alias).
-
#primary_condition_name(name) ⇒ Object
Returns the primary name for any condition on a column.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object (private)
114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/searchlogic/named_scopes/conditions.rb', line 114 def method_missing(name, *args, &block) if details = primary_condition_details(name) create_primary_condition(details[:column], details[:condition]) send(name, *args) elsif details = alias_condition_details(name) create_alias_condition(details[:column], details[:condition], args) send(name, *args) else super end end |
Instance Method Details
#alias_condition?(name) ⇒ Boolean
Is the name of the method a valid condition that can be dynamically created, AND is it an alias condition. “gt” not “greater_than”.
109 110 111 |
# File 'lib/searchlogic/named_scopes/conditions.rb', line 109 def alias_condition?(name) !alias_condition_details(name).nil? end |
#condition?(name) ⇒ Boolean
Is the name of the method a valid condition that can be dynamically created?
97 98 99 |
# File 'lib/searchlogic/named_scopes/conditions.rb', line 97 def condition?(name) primary_condition?(name) || alias_condition?(name) end |
#named_scope_arity(name) ⇒ Object
The arity for a named scope’s proc is important, because we use the arity to determine if the condition should be ignored when calling the search method. If the condition is false and the arity is 0, then we skip it all together. Ex:
User.named_scope :age_is_4, :conditions => {:age => 4}
User.search(:age_is_4 => false) == User.all
User.search(:age_is_4 => true) == User.all(:conditions => {:age => 4})
We also use it when trying to “copy” the underlying named scope for association conditions.
68 69 70 71 |
# File 'lib/searchlogic/named_scopes/conditions.rb', line 68 def named_scope_arity(name) = (name) .respond_to?(:arity) ? .arity : nil end |
#named_scope_options(name) ⇒ Object
Retrieves the options passed when creating the respective named scope. Ex:
named_scope :whatever, :conditions => {:column => value}
This method will return:
:conditions => {:column => value}
ActiveRecord hides this internally, so we have to try and pull it out with this method.
48 49 50 51 52 53 54 55 56 |
# File 'lib/searchlogic/named_scopes/conditions.rb', line 48 def (name) key = primary_condition_name(name) if key eval("options", scopes[key]) else nil end end |
#primary_condition(alias_condition) ⇒ Object
Returns the primary condition for the given alias. Ex:
primary_condition(:gt) => :greater_than
76 77 78 |
# File 'lib/searchlogic/named_scopes/conditions.rb', line 76 def primary_condition(alias_condition) CONDITIONS.find { |k, v| k == alias_condition.to_sym || v.include?(alias_condition.to_sym) }.first end |
#primary_condition?(name) ⇒ Boolean
Is the name of the method a valid condition that can be dynamically created, AND is it a primary condition (not an alias). “greater_than” not “gt”.
103 104 105 |
# File 'lib/searchlogic/named_scopes/conditions.rb', line 103 def primary_condition?(name) !primary_condition_details(name).nil? end |
#primary_condition_name(name) ⇒ Object
Returns the primary name for any condition on a column. You can pass it a primary condition, alias condition, etc, and it will return the proper primary condition name. This helps simply logic throughout Searchlogic. Ex:
primary_condition_name(:id_gt) => :id_greater_than
primary_condition_name(:id_greater_than) => :id_greater_than
86 87 88 89 90 91 92 93 94 |
# File 'lib/searchlogic/named_scopes/conditions.rb', line 86 def primary_condition_name(name) if primary_condition?(name) name.to_sym elsif details = alias_condition_details(name) "#{details[:column]}_#{primary_condition(details[:condition])}".to_sym else nil end end |