Module: Polymorpheus::Interface::BelongsToPolymorphic

Defined in:
lib/polymorpheus/interface/belongs_to_polymorphic.rb

Instance Method Summary collapse

Instance Method Details

#belongs_to_polymorphic(*association_names, options) ⇒ Object



4
5
6
7
8
9
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
# File 'lib/polymorpheus/interface/belongs_to_polymorphic.rb', line 4

def belongs_to_polymorphic(*association_names, options)
  polymorphic_api = options.delete(:as)
  builder = Polymorpheus::InterfaceBuilder.new(polymorphic_api,
                                               association_names,
                                               options)

  # The POLYMORPHEUS_ASSOCIATIONS constant is useful for two reasons:
  #
  # 1. It is useful for other classes to be able to ask this class
  #    about its polymorphic relationship.
  #
  # 2. It prevents a class from defining multiple polymorphic
  #    relationships. Doing so would be a bad idea from a design
  #    standpoint, and we don't want to allow for (and support)
  #    that added complexity.
  #
  const_set('POLYMORPHEUS_ASSOCIATIONS', builder.association_names)

  # Set belongs_to associations
  builder.associations.each do |association|
    belongs_to association.name.to_sym, **association.options
  end

  # Exposed interface for introspection
  define_method 'polymorpheus' do
    builder.exposed_interface(self)
  end

  # Getter method
  define_method polymorphic_api do
    builder.get_associated_object(self)
  end

  # Setter method
  define_method "#{polymorphic_api}=" do |object_to_associate|
    builder.set_associated_object(self, object_to_associate)
  end
end