Module: Sequel::Plugins::Polymorphic::ClassMethods
- Defined in:
- lib/sequel/plugins/polymorphic.rb
Instance Method Summary collapse
-
#many_to_many(*args, &block) ⇒ Object
Creates a many-to-many relationship.
-
#many_to_one(*args, &block) ⇒ Object
(also: #belongs_to)
Creates a many-to-one relationship.
-
#one_to_many(*args, &block) ⇒ Object
(also: #has_many)
Creates a one-to-many relationship.
-
#one_to_one(*args, &block) ⇒ Object
(also: #has_one)
Creates a one-to-one relationship.
Instance Method Details
#many_to_many(*args, &block) ⇒ Object
Creates a many-to-many relationship. Note: Removing/clearing the collection deletes the instances in the through relationship (as opposed to nullifying the *able fields as in the one-to-many). Example: Post.many_to_many :tags, :through => :taggings, :as => :taggable
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/sequel/plugins/polymorphic.rb', line 99 def many_to_many(*args, &block) collection_name, = *args ||= {} if through = ([:through] || [:join_table]) and able = [:as] able_id = :"#{able}_id" able_type = :"#{able}_type" collection_singular = singularize(collection_name.to_s).to_sym collection_singular_id = :"#{collection_singular}_id" through_klass = constantize(singularize(camelize(through.to_s))) = { :left_key => able_id, :join_table => through, :conditions => {able_type => self.to_s}, :adder => proc { |many_of_instance| through_klass.create(collection_singular_id => many_of_instance.pk, able_id => pk, able_type => self.class.to_s) }, :remover => proc { |many_of_instance| through_klass.where(collection_singular_id => many_of_instance.pk, able_id => pk, able_type => self.class.to_s).delete }, :clearer => proc { through_klass.where(able_id => pk, able_type => self.class.to_s).delete } }.merge() associate(:many_to_many, collection_name, , &block) else associate(:many_to_many, *args, &block) end end |
#many_to_one(*args, &block) ⇒ Object Also known as: belongs_to
Creates a many-to-one relationship. Example: Comment.many_to_one :commentable, :polymorphic => true
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 |
# File 'lib/sequel/plugins/polymorphic.rb', line 17 def many_to_one(*args, &block) able, = *args ||= {} if [:polymorphic] model = underscore(self.to_s) plural_model = pluralize(model) = { :reciprocal => plural_model.to_sym, :reciprocal_type => :many_to_one, :setter => (proc do |able_instance| self[:"#{able}_id"] = (able_instance.pk if able_instance) self[:"#{able}_type"] = (able_instance.class.name if able_instance) end), :dataset => (proc do able_type = send(:"#{able}_type") able_id = send(:"#{able}_id") return if able_type.nil? || able_id.nil? klass = self.class.send(:constantize, able_type) klass.where(klass.primary_key => able_id) end), :eager_loader => (proc do |eo| id_map = {} eo[:rows].each do |model| model_able_type = model.send(:"#{able}_type") model_able_id = model.send(:"#{able}_id") model.associations[able] = nil ((id_map[model_able_type] ||= {})[model_able_id] ||= []) << model if !model_able_type.nil? && !model_able_id.nil? end id_map.each do |klass_name, id_map| klass = constantize(camelize(klass_name)) klass.where(klass.primary_key=>id_map.keys).all do || id_map[.pk].each do |model| model.associations[able] = end end end end) }.merge() associate(:many_to_one, able, , &block) else associate(:many_to_one, *args, &block) end end |
#one_to_many(*args, &block) ⇒ Object Also known as: has_many
Creates a one-to-many relationship. Note: Removing/clearing nullifies the *able fields in the related objects. Example: Post.one_to_many :awesome_comments, :as => :commentable
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/sequel/plugins/polymorphic.rb', line 69 def one_to_many(*args, &block) collection_name, = *args ||= {} if able = [:as] able_id = :"#{able}_id" able_type = :"#{able}_type" many_dataset_name = :"#{collection_name}_dataset" = { :key => able_id, :reciprocal => able, :reciprocal_type => :one_to_many, :conditions => {able_type => self.to_s}, :adder => proc { |many_of_instance| many_of_instance.update(able_id => pk, able_type => self.class.to_s) }, :remover => proc { |many_of_instance| many_of_instance.update(able_id => nil, able_type => nil) }, :clearer => proc { send(many_dataset_name).update(able_id => nil, able_type => nil) } }.merge() associate(:one_to_many, collection_name, , &block) else associate(:one_to_many, *args, &block) end end |
#one_to_one(*args, &block) ⇒ Object Also known as: has_one
Creates a one-to-one relationship. Note: Removing/clearing nullifies the *able fields in the related objects. Example: Article.one_to_one :post, as: :postable
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/sequel/plugins/polymorphic.rb', line 129 def one_to_one(*args, &block) collection_name, = *args ||= {} if able = [:as] able_id = :"#{able}_id" able_type = :"#{able}_type" many_dataset_name = :"#{collection_name}_dataset" = { :key => able_id, :reciprocal => able, :reciprocal_type => :one_to_one, :conditions => {able_type => self.to_s}, :setter => (proc do |able_instance| self[:"#{able}_id"] = (able_instance.pk if able_instance) self[:"#{able}_type"] = (able_instance.class.name if able_instance) end) }.merge() associate(:one_to_one, collection_name, , &block) else associate(:one_to_one, *args, &block) end end |