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

Defined in:
lib/sequel_polymorphic/sequel_polymorphic.rb

Instance Method Summary collapse

Instance Method Details

#many_to_many(*args, &block) ⇒ Object

Creates a many-to-many relationship. NB: 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



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

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 # => tag
    collection_singular_id = :"#{collection_singular}_id"
    through_klass          = constantize(singularize(through.to_s).capitalize) # => Tagging

    associate(:many_to_many, collection_name,
      :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 }
    )

  else
    associate(:many_to_many, *args, &block)
  end
end

#many_to_one(*args, &block) ⇒ Object Also known as: belongs_to

Example: Comment.many_to_one :commentable, polymorphic: true



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
# File 'lib/sequel_polymorphic/sequel_polymorphic.rb', line 15

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

  if options[:polymorphic]
    model = self.to_s.downcase          # comment
    plural_model = pluralize(model)     # comments

    associate(:many_to_one, able,
      :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
        klass = constantize(send(:"#{able}_type"))
        klass.where(klass.primary_key => send(:"#{able}_id"))
      end),
      :eager_loader => (proc do |eo|
        id_map = {}
        eo[:rows].each do |model|
          model.associations[able] = nil
          ((id_map[model.send(:"#{able}_type")] ||= {})[model.send(:"#{able}_id")] ||= []) << model
        end
        id_map.each do |klass_name, id_map|
          klass = constantize(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)
    )

  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. NB: Removing/clearing nullifies the *able fields in the related objects Example: Post.one_to_many :comments, as: :commentable



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/sequel_polymorphic/sequel_polymorphic.rb', line 61

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"

    associate(:one_to_many, collection_name,
      :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) }
    )

  else
    associate(:one_to_many, *args, &block)
  end
end