Module: Hstore::ActiveRecord::Extensions

Defined in:
lib/activerecord2-hstore/hstore.rb

Instance Method Summary collapse

Instance Method Details

#create_getter_and_setter(column) ⇒ Object

Creates and overrides ActiveRecord’s getter and setter methods for the hstore column. The getter returns a hash. The setter accepts and converts either a hash or a valid hstore string.



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/activerecord2-hstore/hstore.rb', line 25

def create_getter_and_setter(column)
  define_method column.to_sym do
    read_attribute(column.to_sym).to_s.from_hstore
  end

  define_method "#{column}=".to_sym do |value|
    value = {}.to_hstore if value.nil?
    value ||= value.is_a?(String) ? value : value.to_hstore
    write_attribute(column.to_sym, value)
  end
end

#create_hstore_key_availability_scopes(column) ⇒ Object

Creates named scopes for the hstore column allowing you to determine if the column contains a given key or set of keys.



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/activerecord2-hstore/hstore.rb', line 39

def create_hstore_key_availability_scopes(column)
  named_scope "#{column}_has_key".to_sym, Proc.new{ |key|
    { :conditions => ["#{column} ? :key", {:key => key}] }
  }
  named_scope "#{column}_has_all_keys".to_sym, Proc.new{ |keys|
    { :conditions => ["#{column} ?& ARRAY[:keys]", {:keys => Array(keys)}] }
  }
  named_scope "#{column}_has_any_key".to_sym, Proc.new{ |keys|
    { :conditions => ["#{column} ?| ARRAY[:keys]", {:keys => Array(keys)}] }
  }
end

#create_hstore_key_search_scopes(column, key) ⇒ Object

Creates a slew of searchlogic-like named scopes to query for a key on a hstore column



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/activerecord2-hstore/hstore.rb', line 52

def create_hstore_key_search_scopes(column, key)
  named_scope "#{column}_#{key}_eq".to_sym, Proc.new{ |value|
    { :conditions => ["#{column} -> '#{key}' = ?", value.to_s] }
  }
  named_scope "#{column}_#{key}_neq".to_sym, Proc.new{ |value|
    { :conditions => ["#{column} -> '#{key}' != ?", value.to_s] }
  }
  named_scope "#{column}_#{key}_eq_any".to_sym, Proc.new{ |value|
    { :conditions => ["#{column} -> '#{key}' IN(?)", value.map{|v| v.to_s} ] }
  }
  named_scope "#{column}_#{key}_neq_any".to_sym, Proc.new{ |value|
    { :conditions => ["#{column} -> '#{key}' NOT IN(?)", value.map{|v| v.to_s} ] }
  }
  named_scope "#{column}_#{key}_like".to_sym, Proc.new{ |value|
    { :conditions => ["#{column} -> '#{key}' ILIKE(?)", "%#{value.to_s}%"] }
  }
  named_scope "#{column}_#{key}_begins_with".to_sym, Proc.new{ |value|
    { :conditions => ["#{column} -> '#{key}' ILIKE(?)", "#{value.to_s}%"] }
  }
  named_scope "#{column}_#{key}_ends_with".to_sym, Proc.new{ |value|
    { :conditions => ["#{column} -> '#{key}' ILIKE(?)", "%#{value.to_s}"] }
  }
end

#hstore_column(column, keys = []) ⇒ Object

Creates a series of methods and named scopes for a hstore column. This is the primary method you should use to invoke this gem in a model.

USAGE IN A MODEL: class Foo < ActiveRecord::Base

hstore_column :some_column, [:array_of_possible_keys, :you_may_want, :to_query_on]

end



16
17
18
19
20
# File 'lib/activerecord2-hstore/hstore.rb', line 16

def hstore_column(column, keys=[])
  create_getter_and_setter(column)
  create_hstore_key_availability_scopes(column)
  Array(keys).each{ |key| create_hstore_key_search_scopes(column, key) }
end