Class: Torque::PostgreSQL::Associations::Builder::BelongsToMany

Inherits:
ActiveRecord::Associations::Builder::CollectionAssociation
  • Object
show all
Defined in:
lib/torque/postgresql/associations/builder/belongs_to_many.rb

Class Method Summary collapse

Class Method Details

.add_default_callbacks(model, reflection) ⇒ Object



40
41
42
43
44
# File 'lib/torque/postgresql/associations/builder/belongs_to_many.rb', line 40

def self.add_default_callbacks(model, reflection)
  model.before_validation ->(o) do
    o.association(reflection.name).default(&reflection.options[:default])
  end
end

.add_touch_callbacks(model, reflection) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/torque/postgresql/associations/builder/belongs_to_many.rb', line 46

def self.add_touch_callbacks(model, reflection)
  foreign_key = reflection.foreign_key
  n           = reflection.name
  touch       = reflection.options[:touch]

  callback = ->(changes_method) do
    ->(record) do
      BelongsToMany.touch_record(record, record.send(changes_method), foreign_key,
        n, touch, belongs_to_touch_method)
    end
  end

  unless reflection.counter_cache_column
    model.after_create callback.call(:saved_changes), if: :saved_changes?
    model.after_destroy callback.call(:changes_to_save)
  end

  model.after_update callback.call(:saved_changes), if: :saved_changes?
  model.after_touch callback.call(:changes_to_save)
end

.define_callbacks(model, reflection) ⇒ Object



18
19
20
21
22
# File 'lib/torque/postgresql/associations/builder/belongs_to_many.rb', line 18

def self.define_callbacks(model, reflection)
  super
  add_touch_callbacks(model, reflection)   if reflection.options[:touch]
  add_default_callbacks(model, reflection) if reflection.options[:default]
end

.define_readers(mixin, name) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/torque/postgresql/associations/builder/belongs_to_many.rb', line 24

def self.define_readers(mixin, name)
  mixin.class_eval <<-CODE, __FILE__, __LINE__ + 1
    def #{name}
      association(:#{name}).reader
    end
  CODE
end

.define_validations(model, reflection) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/torque/postgresql/associations/builder/belongs_to_many.rb', line 98

def self.define_validations(model, reflection)
  if reflection.options.key?(:required)
    reflection.options[:optional] = !reflection.options.delete(:required)
  end

  if reflection.options[:optional].nil?
    required = model.belongs_to_many_required_by_default
  else
    required = !reflection.options[:optional]
  end

  super

  if required
    model.validates_presence_of reflection.name, message: :required
  end
end

.define_writers(mixin, name) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/torque/postgresql/associations/builder/belongs_to_many.rb', line 32

def self.define_writers(mixin, name)
  mixin.class_eval <<-CODE, __FILE__, __LINE__ + 1
    def #{name}=(value)
      association(:#{name}).writer(value)
    end
  CODE
end

.macroObject



6
7
8
# File 'lib/torque/postgresql/associations/builder/belongs_to_many.rb', line 6

def self.macro
  :belongs_to_many
end

.touch_record(o, changes, foreign_key, name, touch, touch_method) ⇒ Object

:nodoc:



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/torque/postgresql/associations/builder/belongs_to_many.rb', line 67

def self.touch_record(o, changes, foreign_key, name, touch, touch_method) # :nodoc:
  old_foreign_ids = changes[foreign_key] && changes[foreign_key].first

  if old_foreign_ids.present?
    association = o.association(name)
    reflection = association.reflection
    klass = association.klass

    primary_key = reflection.association_primary_key(klass)
    old_records = klass.find_by(primary_key => old_foreign_ids)

    old_records&.map do |old_record|
      if touch != true
        old_record.send(touch_method, touch)
      else
        old_record.send(touch_method)
      end
    end
  end

  o.send(name)&.map do |record|
    if record && record.persisted?
      if touch != true
        record.send(touch_method, touch)
      else
        record.send(touch_method)
      end
    end
  end
end

.valid_dependent_optionsObject



14
15
16
# File 'lib/torque/postgresql/associations/builder/belongs_to_many.rb', line 14

def self.valid_dependent_options
  [:restrict_with_error, :restrict_with_exception]
end

.valid_options(options) ⇒ Object



10
11
12
# File 'lib/torque/postgresql/associations/builder/belongs_to_many.rb', line 10

def self.valid_options(options)
  super + [:touch, :optional, :default, :dependent, :primary_key, :required]
end