Module: DataMapper::Associations::ManyToMany
- Extended by:
- DataMapper::Assertions
- Defined in:
- lib/dm-core/associations/many_to_many.rb
Defined Under Namespace
Classes: Proxy
Class Method Summary collapse
-
.setup(name, model, options = {}) ⇒ Object
private
Setup many to many relationship between two models -.
Methods included from DataMapper::Assertions
Class Method Details
.setup(name, model, options = {}) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Setup many to many relationship between two models -
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/dm-core/associations/many_to_many.rb', line 10 def self.setup(name, model, = {}) assert_kind_of 'name', name, Symbol assert_kind_of 'model', model, Model assert_kind_of 'options', , Hash repository_name = model.repository.name model.class_eval <<-EOS, __FILE__, __LINE__ def #{name}(query = {}) #{name}_association.all(query) end def #{name}=(children) #{name}_association.replace(children) end private def #{name}_association @#{name}_association ||= begin unless relationship = model.relationships(#{repository_name.inspect})[#{name.inspect}] raise ArgumentError, "Relationship #{name.inspect} does not exist in \#{model}" end association = Proxy.new(relationship, self) parent_associations << association association end end EOS opts = .dup opts.delete(:through) opts[:child_model] ||= opts.delete(:class_name) || Extlib::Inflection.classify(name) opts[:parent_model] = model opts[:repository_name] = repository_name opts[:remote_relationship_name] ||= opts.delete(:remote_name) || Extlib::Inflection.tableize(opts[:child_model]) opts[:parent_key] = opts[:parent_key] opts[:child_key] = opts[:child_key] opts[:mutable] = true names = [ opts[:child_model], opts[:parent_model].name ].sort model_name = names.join.gsub("::", "") storage_name = Extlib::Inflection.tableize(Extlib::Inflection.pluralize(names[0]) + names[1]) opts[:near_relationship_name] = Extlib::Inflection.tableize(model_name).to_sym model.has(model.n, opts[:near_relationship_name]) relationship = model.relationships(repository_name)[name] = RelationshipChain.new(opts) unless Object.const_defined?(model_name) model = DataMapper::Model.new(storage_name) model.class_eval <<-EOS, __FILE__, __LINE__ def self.name; #{model_name.inspect} end def self.default_repository_name; #{repository_name.inspect} end def self.many_to_many; true end EOS names.each do |n| model.belongs_to(Extlib::Inflection.underscore(n).gsub('/', '_').to_sym) end Object.const_set(model_name, model) end relationship end |