Class: SimpleMaster::Master::Association::BelongsToPolymorphicAssociation

Inherits:
SimpleMaster::Master::Association show all
Defined in:
lib/simple_master/master/association/belongs_to_polymorphic_association.rb

Instance Attribute Summary

Attributes inherited from SimpleMaster::Master::Association

#defined_at, #name, #options

Instance Method Summary collapse

Methods inherited from SimpleMaster::Master::Association

#initialize

Constructor Details

This class inherits a constructor from SimpleMaster::Master::Association

Instance Method Details

#foreign_keyObject



7
8
9
# File 'lib/simple_master/master/association/belongs_to_polymorphic_association.rb', line 7

def foreign_key
  @foreign_key ||= (options[:foreign_key] || ActiveSupport::Inflector.foreign_key(name)).to_sym
end

#foreign_typeObject



11
12
13
# File 'lib/simple_master/master/association/belongs_to_polymorphic_association.rb', line 11

def foreign_type
  @foreign_type ||= options[:foreign_type] || :"#{name}_type"
end

#foreign_type_classObject



15
16
17
# File 'lib/simple_master/master/association/belongs_to_polymorphic_association.rb', line 15

def foreign_type_class
  @foreign_type_class ||= :"#{foreign_type}_class"
end

#init(master_class) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/simple_master/master/association/belongs_to_polymorphic_association.rb', line 28

def init(master_class)
  unless master_class.method_defined?(foreign_type_class)
    fail "[#{master_class}] Please specify `polymorphic_type: true` on polymorphic type column <#{name}>."
  end

  master_class.simple_master_module.class_eval <<-RUBY, __FILE__, __LINE__ + 1
    def #{name}
      return nil if #{foreign_type_class}.nil?

      if #{foreign_type_class} < SimpleMaster::Master
        #{foreign_type_class}.find_by_id(#{foreign_key})
      else
        belongs_to_store[:#{name}] ||= #{foreign_type_class}.simple_master_connection.find_by(id: #{foreign_key})
      end
    end
  RUBY
end

#init_for_test(master_class) ⇒ Object



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
# File 'lib/simple_master/master/association/belongs_to_polymorphic_association.rb', line 46

def init_for_test(master_class)
  master_class.simple_master_module.class_eval <<-RUBY, __FILE__, __LINE__ + 1
    def #{name}
      return nil if #{foreign_type_class}.nil?

      if #{foreign_type_class} < SimpleMaster::Master
        belongs_to_store[:#{name}] || #{foreign_type_class}.find_by_id(#{foreign_key})
      else
        belongs_to_store[:#{name}] ||= #{foreign_type_class}.simple_master_connection.find_by(id: #{foreign_key})
      end
    end

    def #{name}=(value)
      @_association_#{name}_source = self.#{foreign_key} = value&.id
      @_association_#{name}_class_source = self.#{foreign_type_class} = value&.class
      belongs_to_store[:#{name}] = value
    end

    def _#{name}_target_save?
      # Skip saving the association if the key changed after assignment
      return false if @_association_#{name}_source != #{foreign_key}
      return false if @_association_#{name}_class_source != #{foreign_type_class}

      target = belongs_to_store[:#{name}]
      return false if target.nil?

      true
    end
  RUBY
end

#is_active_record?Boolean

Returns:

  • (Boolean)


19
20
21
22
# File 'lib/simple_master/master/association/belongs_to_polymorphic_association.rb', line 19

def is_active_record?
  # target_class will be dynamically loaded.
  false
end

#target_classObject



24
25
26
# File 'lib/simple_master/master/association/belongs_to_polymorphic_association.rb', line 24

def target_class
  fail
end