Module: BelongsToHstore::Association::ClassMethods

Defined in:
lib/belongs_to_hstore/association.rb

Instance Method Summary collapse

Instance Method Details

#belongs_to_hstore(store_attribute, name, options = {}) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/belongs_to_hstore/association.rb', line 7

def belongs_to_hstore(store_attribute, name, options={})
  @belongs_to_hstore_attributes ||= Set.new
  key = options[:foreign_key] || "#{name}_id"
  key_type = key.gsub(/_id$/, '_type')
  store_accessor store_attribute, key.to_s
  @belongs_to_hstore_attributes.add(key.to_s)

  if options[:polymorphic]
    store_accessor store_attribute, key_type
    @belongs_to_hstore_attributes.add(key_type)
  end
  belongs_to name, options

  define_singleton_method("where_#{store_attribute}") do |options|
    where_hstore(store_attribute, options)
  end
end

#belongs_to_hstore_attributesObject



25
26
27
28
# File 'lib/belongs_to_hstore/association.rb', line 25

def belongs_to_hstore_attributes
  attrs = @belongs_to_hstore_attributes || Set.new
  superclass.respond_to?(:belongs_to_hstore_attributes) ? superclass.belongs_to_hstore_attributes + attrs : attrs
end