Class: Searchgasm::Condition::Base
- Inherits:
-
Object
- Object
- Searchgasm::Condition::Base
- Includes:
- Shared::Utilities
- Defined in:
- lib/searchgasm/condition/base.rb
Overview
Conditions condition
The base class for creating a condition. Your custom conditions should extend this class. See Searchgasm::Conditions::Base.register_condition on how to write your own condition.
Direct Known Subclasses
BeginsWith, Blank, EndsWith, Equals, GreaterThan, GreaterThanOrEqualTo, Keywords, LessThan, LessThanOrEqualTo, Like, Nil, NotBeginWith, NotBlank, NotEndWith, NotEqual, NotHaveKeywords, NotLike, NotNil, Tree
Instance Attribute Summary collapse
-
#column ⇒ Object
Returns the value of attribute column.
-
#column_for_type_cast ⇒ Object
Returns the value of attribute column_for_type_cast.
-
#column_sql ⇒ Object
Substitutes string vars with table and column name.
-
#column_sql_format ⇒ Object
Returns the value of attribute column_sql_format.
-
#klass ⇒ Object
Returns the value of attribute klass.
-
#table_name ⇒ Object
Returns the value of attribute table_name.
Class Method Summary collapse
-
.condition_names_for_column ⇒ Object
Same as condition_name_for_model, but for a model’s column obj.
-
.condition_names_for_model ⇒ Object
Determines what to call the condition for the model.
-
.condition_type_name ⇒ Object
Name of the condition type inferred from the class name.
- .handle_array_value? ⇒ Boolean
-
.ignore_meaningless_value? ⇒ Boolean
:nodoc:.
- .join_arrays_with_or? ⇒ Boolean
Instance Method Summary collapse
-
#explicitly_set_value=(value) ⇒ Object
Allows nils to be meaninful values.
-
#explicitly_set_value? ⇒ Boolean
Need this if someone wants to actually use nil in a meaningful way.
-
#initialize(klass, options = {}) ⇒ Base
constructor
A new instance of Base.
- #options ⇒ Object
-
#sanitize(alt_value = nil) ⇒ Object
You should refrain from overwriting this method, it performs various tasks before callign your to_conditions method, allowing you to keep to_conditions simple.
-
#value ⇒ Object
The value for the condition.
-
#value=(v) ⇒ Object
Sets the value for the condition.
-
#value_is_meaningless? ⇒ Boolean
:nodoc:.
Constructor Details
#initialize(klass, options = {}) ⇒ Base
Returns a new instance of Base.
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/searchgasm/condition/base.rb', line 46 def initialize(klass, = {}) self.klass = klass self.table_name = [:table_name] || klass.table_name if [:column] self.column = [:column].class < ::ActiveRecord::ConnectionAdapters::Column ? [:column] : klass.columns_hash[[:column].to_s] if [:column_for_type_cast] self.column_for_type_cast = [:column_for_type_cast] else type = (!self.class.value_type.blank? && self.class.value_type.to_s) || (![:column_type].blank? && [:column_type].to_s) || column.sql_type self.column_for_type_cast = column.class.new(column.name, column.default.to_s, type, column.null) end self.column_sql_format = [:column_sql_format] || "{table}.{column}" end end |
Instance Attribute Details
#column ⇒ Object
Returns the value of attribute column.
10 11 12 |
# File 'lib/searchgasm/condition/base.rb', line 10 def column @column end |
#column_for_type_cast ⇒ Object
Returns the value of attribute column_for_type_cast.
10 11 12 |
# File 'lib/searchgasm/condition/base.rb', line 10 def column_for_type_cast @column_for_type_cast end |
#column_sql ⇒ Object
Substitutes string vars with table and column name. Allows us to switch the column and table on the fly and have the condition update appropriately. The table name could be variable depending on the condition. Take STI and more than one child model is used in the condition, the first gets the parent table name, the rest get aliases.
66 67 68 |
# File 'lib/searchgasm/condition/base.rb', line 66 def column_sql @column_sql end |
#column_sql_format ⇒ Object
Returns the value of attribute column_sql_format.
10 11 12 |
# File 'lib/searchgasm/condition/base.rb', line 10 def column_sql_format @column_sql_format end |
#klass ⇒ Object
Returns the value of attribute klass.
10 11 12 |
# File 'lib/searchgasm/condition/base.rb', line 10 def klass @klass end |
#table_name ⇒ Object
Returns the value of attribute table_name.
10 11 12 |
# File 'lib/searchgasm/condition/base.rb', line 10 def table_name @table_name end |
Class Method Details
.condition_names_for_column ⇒ Object
Same as condition_name_for_model, but for a model’s column obj
41 42 43 |
# File 'lib/searchgasm/condition/base.rb', line 41 def condition_names_for_column [condition_type_name] end |
.condition_names_for_model ⇒ Object
Determines what to call the condition for the model
Searchgasm tries to create conditions on each model. Before it does this it passes the model to this method to see what to call the condition. If the condition type doesnt want to create a condition on a model it will just return nil and Searchgasm will skip over it.
36 37 38 |
# File 'lib/searchgasm/condition/base.rb', line 36 def condition_names_for_model [] end |
.condition_type_name ⇒ Object
Name of the condition type inferred from the class name
16 17 18 |
# File 'lib/searchgasm/condition/base.rb', line 16 def condition_type_name name.split("::").last.underscore end |
.handle_array_value? ⇒ Boolean
20 21 22 |
# File 'lib/searchgasm/condition/base.rb', line 20 def handle_array_value? handle_array_value == true end |
.ignore_meaningless_value? ⇒ Boolean
:nodoc:
24 25 26 |
# File 'lib/searchgasm/condition/base.rb', line 24 def ignore_meaningless_value? # :nodoc: ignore_meaningless_value == true end |
.join_arrays_with_or? ⇒ Boolean
28 29 30 |
# File 'lib/searchgasm/condition/base.rb', line 28 def join_arrays_with_or? join_arrays_with_or == true end |
Instance Method Details
#explicitly_set_value=(value) ⇒ Object
Allows nils to be meaninful values
71 72 73 |
# File 'lib/searchgasm/condition/base.rb', line 71 def explicitly_set_value=(value) @explicitly_set_value = value end |
#explicitly_set_value? ⇒ Boolean
Need this if someone wants to actually use nil in a meaningful way
76 77 78 |
# File 'lib/searchgasm/condition/base.rb', line 76 def explicitly_set_value? @explicitly_set_value == true end |
#options ⇒ Object
80 81 82 |
# File 'lib/searchgasm/condition/base.rb', line 80 def {:table_name => table_name, :column => column, :column_for_type_cast => column_for_type_cast, :column_sql_format => column_sql_format} end |
#sanitize(alt_value = nil) ⇒ Object
You should refrain from overwriting this method, it performs various tasks before callign your to_conditions method, allowing you to keep to_conditions simple.
85 86 87 88 89 90 91 92 93 94 |
# File 'lib/searchgasm/condition/base.rb', line 85 def sanitize(alt_value = nil) # :nodoc: return if value_is_meaningless? v = alt_value || value if v.is_a?(Array) && !self.class.handle_array_value? merge_conditions(*v.collect { |i| sanitize(i) } << {:any => self.class.join_arrays_with_or?}) else v = v.utc if column && v.respond_to?(:utc) && [:time, :timestamp, :datetime].include?(column.type) && klass.time_zone_aware_attributes && !klass.skip_time_zone_conversion_for_attributes.include?(column.name.to_sym) to_conditions(v) end end |
#value ⇒ Object
The value for the condition
97 98 99 |
# File 'lib/searchgasm/condition/base.rb', line 97 def value @casted_value ||= type_cast_value(@value) end |
#value=(v) ⇒ Object
Sets the value for the condition
102 103 104 105 106 |
# File 'lib/searchgasm/condition/base.rb', line 102 def value=(v) self.explicitly_set_value = true @casted_value = nil @value = v end |
#value_is_meaningless? ⇒ Boolean
:nodoc:
108 109 110 |
# File 'lib/searchgasm/condition/base.rb', line 108 def value_is_meaningless? # :nodoc: meaningless?(@value) end |