Class: Searchlogic::Condition::Base

Inherits:
Object
  • Object
show all
Includes:
Shared::Utilities
Defined in:
lib/searchlogic/condition/base.rb

Overview

Conditions condition

The base class for creating a condition. Your custom conditions should extend this class. See Searchlogic::Conditions::Base.register_condition on how to write your own condition.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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/searchlogic/condition/base.rb', line 46

def initialize(klass, options = {})
  self.klass = klass
  self.table_name = options[:table_name] || klass.table_name
  
  if options[:column]
    self.column = options[:column].class < ::ActiveRecord::ConnectionAdapters::Column ? options[:column] : klass.columns_hash[options[:column].to_s]
    
    if options[:column_for_type_cast]
      self.column_for_type_cast = options[:column_for_type_cast]
    else
      type = (!self.class.value_type.blank? && self.class.value_type.to_s) || (!options[:column_type].blank? && options[: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 = options[:column_sql_format] || "{table}.{column}"
  end
end

Instance Attribute Details

#columnObject

Returns the value of attribute column.



10
11
12
# File 'lib/searchlogic/condition/base.rb', line 10

def column
  @column
end

#column_for_type_castObject

Returns the value of attribute column_for_type_cast.



10
11
12
# File 'lib/searchlogic/condition/base.rb', line 10

def column_for_type_cast
  @column_for_type_cast
end

#column_sqlObject

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/searchlogic/condition/base.rb', line 66

def column_sql
  @column_sql
end

#column_sql_formatObject

Returns the value of attribute column_sql_format.



10
11
12
# File 'lib/searchlogic/condition/base.rb', line 10

def column_sql_format
  @column_sql_format
end

#klassObject

Returns the value of attribute klass.



10
11
12
# File 'lib/searchlogic/condition/base.rb', line 10

def klass
  @klass
end

#table_nameObject

Returns the value of attribute table_name.



10
11
12
# File 'lib/searchlogic/condition/base.rb', line 10

def table_name
  @table_name
end

Class Method Details

.condition_names_for_columnObject

Same as condition_name_for_model, but for a model’s column obj



41
42
43
# File 'lib/searchlogic/condition/base.rb', line 41

def condition_names_for_column
  [condition_type_name]
end

.condition_names_for_modelObject

Determines what to call the condition for the model

Searchlogic 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 Searchlogic will skip over it.



36
37
38
# File 'lib/searchlogic/condition/base.rb', line 36

def condition_names_for_model
  []
end

.condition_type_nameObject

Name of the condition type inferred from the class name



16
17
18
# File 'lib/searchlogic/condition/base.rb', line 16

def condition_type_name
  name.split("::").last.underscore
end

.handle_array_value?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/searchlogic/condition/base.rb', line 20

def handle_array_value?
  handle_array_value == true
end

.ignore_meaningless_value?Boolean

:nodoc:

Returns:

  • (Boolean)


24
25
26
# File 'lib/searchlogic/condition/base.rb', line 24

def ignore_meaningless_value? # :nodoc:
  ignore_meaningless_value == true
end

.join_arrays_with_or?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/searchlogic/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/searchlogic/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

Returns:

  • (Boolean)


76
77
78
# File 'lib/searchlogic/condition/base.rb', line 76

def explicitly_set_value?
  @explicitly_set_value == true
end

#optionsObject



80
81
82
# File 'lib/searchlogic/condition/base.rb', line 80

def options
  {: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/searchlogic/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

#valueObject

The value for the condition



97
98
99
# File 'lib/searchlogic/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/searchlogic/condition/base.rb', line 102

def value=(v)
  self.explicitly_set_value = true
  @casted_value = nil
  @value = v
end

#value_is_meaningless?Boolean

:nodoc:

Returns:

  • (Boolean)


108
109
110
# File 'lib/searchlogic/condition/base.rb', line 108

def value_is_meaningless? # :nodoc:
  meaningless?(@value)
end