Class: SimplyCouch::Model::HasAndBelongsToMany::Property

Inherits:
AssociationProperty show all
Defined in:
lib/simply_couch/model/has_and_belongs_to_many.rb

Instance Attribute Summary

Attributes inherited from AssociationProperty

#name, #options

Instance Method Summary collapse

Methods inherited from AssociationProperty

#association?, #build, #dirty?, #serialize, #supports_dirty?

Constructor Details

#initialize(owner_clazz, name, options = {}) ⇒ Property

Returns a new instance of Property.



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/simply_couch/model/has_and_belongs_to_many.rb', line 187

def initialize(owner_clazz, name, options = {})
  options = {
    :storing_keys => false,
    :class_name => owner_clazz.find_association_class_name(name),
    :foreign_key => nil,
  }.update(options)

  # there is only one pair of foreign_keys and it usualy the name of the class not storing the keys
  if options[:foreign_key].blank?
    if options[:storing_keys]
      options[:foreign_key] = options[:class_name].singularize.underscore.sub(/.*\//, '').foreign_key.pluralize.to_sym
    else
      options[:foreign_key] = owner_clazz.name.singularize.underscore.sub(/.*\//, '').foreign_key.pluralize.to_sym
    end
  end
  options[:class_storing_keys] = options[:storing_keys] ? owner_clazz.name : options[:class_name]
  @name, @options = name, options

  options.assert_valid_keys(:class_name, :foreign_key, :storing_keys, :class_storing_keys)

  owner_clazz.class_eval do
    _define_cache_accessors(name, options)
    define_has_and_belongs_to_many_property(options[:foreign_key]) if options[:storing_keys]
    define_has_and_belongs_to_many_views(name, options)
    define_has_and_belongs_to_many_getter(name, options)
    define_has_and_belongs_to_many_setter_add(name, options)
    define_has_and_belongs_to_many_setter_remove(name, options)
    define_has_and_belongs_to_many_setter_remove_all(name, options)
    define_has_and_belongs_to_many_count(name, options)
    define_has_and_belongs_to_many_after_destroy_cleanup(name, options)
  end
end