Class: Searchgasm::Conditions::Base
- Inherits:
-
Object
- Object
- Searchgasm::Conditions::Base
- Includes:
- Protection, Utilities
- 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
-
#relationship_name ⇒ Object
Returns the value of attribute relationship_name.
-
#scope ⇒ Object
Returns the value of attribute scope.
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.
-
.create_virtual_class(model_class) ⇒ Object
Creates virtual classes for the class passed to it.
-
.klass ⇒ Object
The class / model we are searching.
-
.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
-
#conditions ⇒ Object
All of the active conditions (conditions that have been set).
-
#conditions=(conditions) ⇒ Object
Allows you to set the conditions via a hash.
-
#includes ⇒ Object
A list of includes to use when searching, includes relationships.
-
#initialize(init_conditions = {}) ⇒ Base
constructor
A new instance of Base.
- #klass ⇒ Object
-
#sanitize ⇒ Object
Sanitizes the conditions down into conditions that ActiveRecord::Base.find can understand.
Methods included from Protection
#conditions_with_protection=, included, #protect?
Constructor Details
#initialize(init_conditions = {}) ⇒ Base
Returns a new instance of Base.
98 99 100 101 102 103 |
# File 'lib/searchgasm/conditions/base.rb', line 98 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.
13 14 15 |
# File 'lib/searchgasm/conditions/base.rb', line 13 def added_associations @added_associations end |
.added_column_conditions ⇒ Object
Returns the value of attribute added_column_conditions.
13 14 15 |
# File 'lib/searchgasm/conditions/base.rb', line 13 def added_column_conditions @added_column_conditions end |
.added_klass_conditions ⇒ Object
Returns the value of attribute added_klass_conditions.
13 14 15 |
# File 'lib/searchgasm/conditions/base.rb', line 13 def added_klass_conditions @added_klass_conditions end |
Instance Attribute Details
#relationship_name ⇒ Object
Returns the value of attribute relationship_name.
10 11 12 |
# File 'lib/searchgasm/conditions/base.rb', line 10 def relationship_name @relationship_name end |
#scope ⇒ Object
Returns the value of attribute scope.
10 11 12 |
# File 'lib/searchgasm/conditions/base.rb', line 10 def scope @scope end |
Class Method Details
.association_names ⇒ Object
A list of all associations created, used for caching and performance
56 57 58 |
# File 'lib/searchgasm/conditions/base.rb', line 56 def association_names @association_names ||= [] end |
.condition_names ⇒ Object
A list of all conditions available, users for caching and performance
61 62 63 |
# File 'lib/searchgasm/conditions/base.rb', line 61 def condition_names @condition_names ||= [] end |
.conditions ⇒ Object
A list of available condition type classes
51 52 53 |
# File 'lib/searchgasm/conditions/base.rb', line 51 def conditions @@conditions ||= [] end |
.create_virtual_class(model_class) ⇒ Object
Creates virtual classes for the class passed to it. This is a neccesity for keeping dynamically created method names specific to models. It provides caching and helps a lot with performance.
77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/searchgasm/conditions/base.rb', line 77 def create_virtual_class(model_class) class_search_name = "::#{model_class.name}Conditions" begin class_search_name.constantize rescue NameError eval <<-end_eval class #{class_search_name} < ::Searchgasm::Conditions::Base; end; end_eval class_search_name.constantize end end |
.klass ⇒ Object
The class / model we are searching
92 93 94 95 |
# File 'lib/searchgasm/conditions/base.rb', line 92 def klass # Can't cache this because thin and mongrel don't play nice with caching constants name.split("::").last.gsub(/Conditions$/, "").constantize end |
.needed?(model_class, conditions) ⇒ Boolean
:nodoc:
65 66 67 68 69 70 71 72 73 |
# File 'lib/searchgasm/conditions/base.rb', line 65 def needed?(model_class, conditions) # :nodoc: if conditions.is_a?(Hash) conditions.stringify_keys.keys.each do |condition| return true unless model_class.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". 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()
def to_conditions(value)
["#{quoted_table_name}.#{quoted_column_name} SOUNDS LIKE ?", value]
end
end
Searchgasm::Seearch::Conditions.register_condition(SoundsLikeCondition)
45 46 47 48 |
# File 'lib/searchgasm/conditions/base.rb', line 45 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
#conditions ⇒ Object
All of the active conditions (conditions that have been set)
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/searchgasm/conditions/base.rb', line 150 def conditions conditions_hash = {} objects.each do |object| case object when self.class 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=(conditions) ⇒ Object
Allows you to set the conditions via a hash. If you do not pass a hash it will set scope instead, so that you can continue to add conditions and ultimately merge it all together at the end.
139 140 141 142 143 144 145 146 147 |
# File 'lib/searchgasm/conditions/base.rb', line 139 def conditions=(conditions) case conditions when Hash assert_valid_conditions(conditions) remove_conditions_from_protected_assignement(conditions).each { |condition, value| send("#{condition}=", value) } else self.scope = conditions end end |
#includes ⇒ Object
A list of includes to use when searching, includes relationships
117 118 119 120 121 122 123 124 |
# File 'lib/searchgasm/conditions/base.rb', line 117 def includes i = [] associations.each do |association| association_includes = association.includes i << (association_includes.blank? ? association.relationship_name.to_sym : {association.relationship_name.to_sym => association_includes}) end i.blank? ? nil : (i.size == 1 ? i.first : i) end |
#klass ⇒ Object
126 127 128 |
# File 'lib/searchgasm/conditions/base.rb', line 126 def klass self.class.klass end |
#sanitize ⇒ Object
Sanitizes the conditions down into conditions that ActiveRecord::Base.find can understand.
131 132 133 134 135 |
# File 'lib/searchgasm/conditions/base.rb', line 131 def sanitize conditions = merge_conditions(*objects.collect { |object| object.sanitize }) return scope if conditions.blank? merge_conditions(conditions, scope) end |