Class: Searchgasm::Condition::Base
- Inherits:
-
Object
- Object
- Searchgasm::Condition::Base
- Includes:
- 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, Contains, DoesNotEqual, EndsWith, Equals, GreaterThan, GreaterThanOrEqualTo, Keywords, LessThan, LessThanOrEqualTo, Tree
Instance Attribute Summary collapse
-
#column ⇒ Object
Returns the value of attribute column.
-
#klass ⇒ Object
Returns the value of attribute klass.
-
#value ⇒ Object
The value for the condition.
Class Method Summary collapse
-
.aliases_for_column(column) ⇒ Object
Alias methods for the column condition.
-
.aliases_for_klass(klass) ⇒ Object
Alias methods for the klass condition.
-
.comparable_column?(column) ⇒ Boolean
A utility method for using in name_for_column.
-
.condition_name ⇒ Object
Name of the condition inferred from the class name.
-
.name_for_column(column) ⇒ Object
I pass you a column you tell me what to call the condition.
-
.name_for_klass(klass) ⇒ Object
Sane as name_for_column but for the class as a whole.
-
.string_column?(column) ⇒ Boolean
A utility method for using in name_for_column.
Instance Method Summary collapse
-
#condition_name ⇒ Object
A convenience method for the name of this condition.
-
#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.
-
#ignore_blanks? ⇒ Boolean
In most cases a blank value should be ignored, except for conditions like equals.
-
#initialize(klass, column = nil) ⇒ Base
constructor
A new instance of Base.
-
#name ⇒ Object
A convenience method for the name of the method for that specific column or klass.
-
#quote_column_name(column_name) ⇒ Object
Quotes a column name properly for sql.
-
#quote_table_name(table_name) ⇒ Object
Quotes a table name properly for sql.
-
#quoted_column_name ⇒ Object
A convenience method for using when writing your sql in to_conditions.
-
#quoted_table_name ⇒ Object
A convenience method for using when writing your sql in to_conditions.
-
#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.
Constructor Details
#initialize(klass, column = nil) ⇒ Base
Returns a new instance of Base.
52 53 54 55 |
# File 'lib/searchgasm/condition/base.rb', line 52 def initialize(klass, column = nil) self.klass = klass self.column = column.is_a?(String) ? klass.columns_hash[column] : column 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 |
#klass ⇒ Object
Returns the value of attribute klass.
10 11 12 |
# File 'lib/searchgasm/condition/base.rb', line 10 def klass @klass end |
#value ⇒ Object
The value for the condition
115 116 117 |
# File 'lib/searchgasm/condition/base.rb', line 115 def value @value end |
Class Method Details
.aliases_for_column(column) ⇒ Object
Alias methods for the column condition.
26 27 28 |
# File 'lib/searchgasm/condition/base.rb', line 26 def aliases_for_column(column) [] end |
.aliases_for_klass(klass) ⇒ Object
Alias methods for the klass condition
37 38 39 |
# File 'lib/searchgasm/condition/base.rb', line 37 def aliases_for_klass(klass) [] end |
.comparable_column?(column) ⇒ Boolean
A utility method for using in name_for_column. For example you wouldn’t want a string column to use the greater thann condition, but you would for an integer column.
47 48 49 |
# File 'lib/searchgasm/condition/base.rb', line 47 def comparable_column?(column) [:integer, :float, :decimal, :datetime, :timestamp, :time, :date].include?(column.type) end |
.condition_name ⇒ Object
Name of the condition inferred from the class name
15 16 17 |
# File 'lib/searchgasm/condition/base.rb', line 15 def condition_name name.split("::").last.gsub(/Condition$/, "").underscore end |
.name_for_column(column) ⇒ Object
I pass you a column you tell me what to call the condition. If you don’t want to use this condition for the column just return nil
21 22 23 |
# File 'lib/searchgasm/condition/base.rb', line 21 def name_for_column(column) "#{column.name}_#{condition_name}" end |
.name_for_klass(klass) ⇒ Object
Sane as name_for_column but for the class as a whole. For example the tree methods apply to the class as a whole and not specific columns. Any condition that applies to columns should probably return nil here.
32 33 34 |
# File 'lib/searchgasm/condition/base.rb', line 32 def name_for_klass(klass) nil end |
.string_column?(column) ⇒ Boolean
A utility method for using in name_for_column. For example the keywords condition only applied to string columns, the great than condition doesnt.
42 43 44 |
# File 'lib/searchgasm/condition/base.rb', line 42 def string_column?(column) [:string, :text].include?(column.type) end |
Instance Method Details
#condition_name ⇒ Object
A convenience method for the name of this condition
78 79 80 |
# File 'lib/searchgasm/condition/base.rb', line 78 def condition_name self.class.condition_name end |
#explicitly_set_value=(value) ⇒ Object
Allows nils to be meaninful values
58 59 60 |
# File 'lib/searchgasm/condition/base.rb', line 58 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
63 64 65 |
# File 'lib/searchgasm/condition/base.rb', line 63 def explicitly_set_value? @explicitly_set_value == true end |
#ignore_blanks? ⇒ Boolean
In most cases a blank value should be ignored, except for conditions like equals. A blank value is meaningful there, but a blank value for they keyswords condition is not.
68 69 70 |
# File 'lib/searchgasm/condition/base.rb', line 68 def ignore_blanks? true end |
#name ⇒ Object
A convenience method for the name of the method for that specific column or klass
73 74 75 |
# File 'lib/searchgasm/condition/base.rb', line 73 def name column ? self.class.name_for_column(column) : self.class.name_for_klass(klass) end |
#quote_column_name(column_name) ⇒ Object
Quotes a column name properly for sql.
83 84 85 |
# File 'lib/searchgasm/condition/base.rb', line 83 def quote_column_name(column_name) klass.connection.quote_column_name(column_name) end |
#quote_table_name(table_name) ⇒ Object
Quotes a table name properly for sql
93 94 95 |
# File 'lib/searchgasm/condition/base.rb', line 93 def quote_table_name(table_name) klass.connection.quote_table_name(table_name) end |
#quoted_column_name ⇒ Object
A convenience method for using when writing your sql in to_conditions. This is the proper way to use a column name in a query for most databases
88 89 90 |
# File 'lib/searchgasm/condition/base.rb', line 88 def quoted_column_name quote_column_name(column.name) end |
#quoted_table_name ⇒ Object
A convenience method for using when writing your sql in to_conditions. This is the proper way to use a table name in a query for most databases
98 99 100 |
# File 'lib/searchgasm/condition/base.rb', line 98 def quoted_table_name quote_table_name(klass.table_name) 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.
103 104 105 106 107 108 109 110 111 112 |
# File 'lib/searchgasm/condition/base.rb', line 103 def sanitize(alt_value = nil) # :nodoc: return unless explicitly_set_value? v = alt_value || value if v.is_a?(Array) && !["equals", "does_not_equal"].include?(condition_name) merge_conditions(*v.collect { |i| sanitize(i) }) else v = v.utc if column && [: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 |