Module: Sequel::Plugins::Polymorphic::ClassMethods

Defined in:
lib/sequel/plugins/polymorphic.rb

Instance Method Summary collapse

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



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/sequel/plugins/polymorphic.rb', line 119

def many_to_many(*args, &block)
  collection_name, options = *args
  options ||= {}

  if through = (options[:through] || options[:join_table]) and able = options[: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)))

    association_options = {
      :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(options)
    associate(:many_to_many, collection_name, association_options, &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



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
78
79
80
81
# File 'lib/sequel/plugins/polymorphic.rb', line 37

def many_to_one(*args, &block)
  able, options = *args
  options ||= {}

  if options[:polymorphic]
    model = underscore(self.to_s)
    plural_model = pluralize(model)

    association_options = {
      :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 |related_obj|
            id_map[related_obj.pk].each do |model|
              model.associations[able] = related_obj
            end
          end
        end
      end)
    }.merge(options)
    associate(:many_to_one, able, association_options, &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



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/sequel/plugins/polymorphic.rb', line 89

def one_to_many(*args, &block)
  collection_name, options = *args
  options ||= {}

  if able = options[:as]
    able_id           = :"#{able}_id"
    able_type         = :"#{able}_type"
    many_dataset_name = :"#{collection_name}_dataset"

    association_options = {
      :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(options)
    associate(:one_to_many, collection_name, association_options, &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



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/sequel/plugins/polymorphic.rb', line 149

def one_to_one(*args, &block)
  collection_name, options = *args
  options ||= {}

  if able = options[:as]
    able_id           = :"#{able}_id"
    able_type         = :"#{able}_type"
    many_dataset_name = :"#{collection_name}_dataset"

    association_options = {
      :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(options)
    associate(:one_to_one, collection_name, association_options, &block)
  else
    associate(:one_to_one, *args, &block)
  end
end