Class: Searchgasm::Conditions::Base
- Inherits:
-
Object
- Object
- Searchgasm::Conditions::Base
- Includes:
- Protection, Shared::Utilities, Shared::VirtualClasses
- Defined in:
- lib/searchgasm/conditions/base.rb,
lib/searchgasm.rb
Overview
Conditions
Represents a collection of conditions and performs various tasks on that collection. For information on each condition see Searchgasm::Condition. Each condition has its own file and class and the source for each condition is pretty self explanatory.
Class Attribute Summary collapse
-
.added_associations ⇒ Object
Returns the value of attribute added_associations.
-
.added_column_conditions ⇒ Object
Returns the value of attribute added_column_conditions.
-
.added_klass_conditions ⇒ Object
Returns the value of attribute added_klass_conditions.
Instance Attribute Summary collapse
-
#any ⇒ Object
:nodoc:.
-
#relationship_name ⇒ Object
Returns the value of attribute relationship_name.
Class Method Summary collapse
-
.association_names ⇒ Object
A list of all associations created, used for caching and performance.
-
.condition_names ⇒ Object
A list of all conditions available, users for caching and performance.
-
.conditions ⇒ Object
A list of available condition type classes.
-
.needed?(model_class, conditions) ⇒ Boolean
:nodoc:.
-
.register_condition(condition_class) ⇒ Object
Registers a condition as an available condition for a column or a class.
Instance Method Summary collapse
-
#any? ⇒ Boolean
Convenience method for determining if we should join the conditions with “AND” or “OR”.
-
#auto_joins ⇒ Object
A list of joins to use when searching, includes relationships.
-
#conditions ⇒ Object
All of the active conditions (conditions that have been set).
-
#conditions=(value) ⇒ Object
Allows you to set the conditions via a hash.
-
#initialize(init_conditions = {}) ⇒ Base
constructor
A new instance of Base.
- #inspect ⇒ Object
-
#sanitize ⇒ Object
Sanitizes the conditions down into conditions that ActiveRecord::Base.find can understand.
Methods included from Shared::VirtualClasses
Methods included from Protection
#conditions_with_protection=, included, #protect=, #protect?
Constructor Details
#initialize(init_conditions = {}) ⇒ Base
Returns a new instance of Base.
81 82 83 84 85 86 |
# File 'lib/searchgasm/conditions/base.rb', line 81 def initialize(init_conditions = {}) add_klass_conditions! add_column_conditions! add_associations! self.conditions = init_conditions end |
Class Attribute Details
.added_associations ⇒ Object
Returns the value of attribute added_associations.
14 15 16 |
# File 'lib/searchgasm/conditions/base.rb', line 14 def added_associations @added_associations end |
.added_column_conditions ⇒ Object
Returns the value of attribute added_column_conditions.
14 15 16 |
# File 'lib/searchgasm/conditions/base.rb', line 14 def added_column_conditions @added_column_conditions end |
.added_klass_conditions ⇒ Object
Returns the value of attribute added_klass_conditions.
14 15 16 |
# File 'lib/searchgasm/conditions/base.rb', line 14 def added_klass_conditions @added_klass_conditions end |
Instance Attribute Details
#any ⇒ Object
:nodoc:
99 100 101 |
# File 'lib/searchgasm/conditions/base.rb', line 99 def any @any end |
#relationship_name ⇒ Object
Returns the value of attribute relationship_name.
11 12 13 |
# File 'lib/searchgasm/conditions/base.rb', line 11 def relationship_name @relationship_name end |
Class Method Details
.association_names ⇒ Object
A list of all associations created, used for caching and performance
57 58 59 |
# File 'lib/searchgasm/conditions/base.rb', line 57 def association_names @association_names ||= [] end |
.condition_names ⇒ Object
A list of all conditions available, users for caching and performance
62 63 64 |
# File 'lib/searchgasm/conditions/base.rb', line 62 def condition_names @condition_names ||= [] end |
.conditions ⇒ Object
A list of available condition type classes
52 53 54 |
# File 'lib/searchgasm/conditions/base.rb', line 52 def conditions @@conditions ||= [] end |
.needed?(model_class, conditions) ⇒ Boolean
:nodoc:
66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/searchgasm/conditions/base.rb', line 66 def needed?(model_class, conditions) # :nodoc: return false if conditions.blank? if conditions.is_a?(Hash) return true if conditions[:any] column_names = model_class.column_names conditions.stringify_keys.keys.each do |condition| return true unless column_names.include?(condition) end end false end |
.register_condition(condition_class) ⇒ Object
Registers a condition as an available condition for a column or a class.
Example
config/initializers/searchgasm.rb
# Actual function for MySQL databases only
class SoundsLike < Searchgasm::Condition::Base
class << self
# I pass you the column, you tell me what you want the method to be called.
# If you don't want to add this condition for that column, return nil
# It defaults to "#{column.name}_sounds_like" (using the class name). So if thats what you want you don't even need to do this.
def name_for_column(column)
super
end
# Only do this if you want aliases for your condition
def aliases_for_column(column)
["#{column.name}_sounds", "#{column.name}_similar_to"]
end
end
# You can return an array or a string. NOT a hash, because all of these conditions
# need to eventually get merged together. The array or string can be anything you would put in
# the :conditions option for ActiveRecord::Base.find(). Also, for a list of methods / variables you can use check out earchgasm::Condition::Base
def to_conditions(value)
["#{quoted_table_name}.#{quoted_column_name} SOUNDS LIKE ?", value]
end
end
Searchgasm::Seearch::Conditions.register_condition(SoundsLikeCondition)
46 47 48 49 |
# File 'lib/searchgasm/conditions/base.rb', line 46 def register_condition(condition_class) raise(ArgumentError, "You can only register conditions that extend Searchgasm::Condition::Base") unless condition_class.ancestors.include?(Searchgasm::Condition::Base) conditions << condition_class unless conditions.include?(condition_class) end |
Instance Method Details
#any? ⇒ Boolean
Convenience method for determining if we should join the conditions with “AND” or “OR”.
104 105 106 |
# File 'lib/searchgasm/conditions/base.rb', line 104 def any? @any == true || @any == "true" || @any == "1" || @any == "yes" end |
#auto_joins ⇒ Object
A list of joins to use when searching, includes relationships
109 110 111 112 113 114 115 116 117 |
# File 'lib/searchgasm/conditions/base.rb', line 109 def auto_joins j = [] associations.each do |association| next if association.conditions.blank? association_joins = association.auto_joins j << (association_joins.blank? ? association.relationship_name.to_sym : {association.relationship_name.to_sym => association_joins}) end j.blank? ? nil : (j.size == 1 ? j.first : j) end |
#conditions ⇒ Object
All of the active conditions (conditions that have been set)
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/searchgasm/conditions/base.rb', line 145 def conditions return @conditions if @conditions return if objects.blank? conditions_hash = {} objects.each do |object| if object.class < Searchgasm::Conditions::Base relationship_conditions = object.conditions next if relationship_conditions.blank? conditions_hash[object.relationship_name.to_sym] = relationship_conditions else next unless object.explicitly_set_value? conditions_hash[object.name.to_sym] = object.value end end conditions_hash end |
#conditions=(value) ⇒ Object
Allows you to set the conditions via a hash.
130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/searchgasm/conditions/base.rb', line 130 def conditions=(value) case value when Hash assert_valid_conditions(value) remove_conditions_from_protected_assignement(value).each do |condition, condition_value| next if condition_value.blank? # ignore blanks on mass assignments send("#{condition}=", condition_value) end else reset_objects! @conditions = value end end |
#inspect ⇒ Object
119 120 121 |
# File 'lib/searchgasm/conditions/base.rb', line 119 def inspect "#<#{klass}Conditions#{conditions.blank? ? "" : " #{conditions.inspect}"}>" end |
#sanitize ⇒ Object
Sanitizes the conditions down into conditions that ActiveRecord::Base.find can understand.
124 125 126 127 |
# File 'lib/searchgasm/conditions/base.rb', line 124 def sanitize return @conditions if @conditions merge_conditions(*(objects.collect { |object| object.sanitize } << {:any => any})) end |