Class: Torque::PostgreSQL::Associations::Builder::BelongsToMany
- Inherits:
-
ActiveRecord::Associations::Builder::CollectionAssociation
- Object
- ActiveRecord::Associations::Builder::CollectionAssociation
- Torque::PostgreSQL::Associations::Builder::BelongsToMany
- Defined in:
- lib/torque/postgresql/associations/builder/belongs_to_many.rb
Class Method Summary collapse
- .add_change_callbacks(model, reflection) ⇒ Object
- .add_default_callbacks(model, reflection) ⇒ Object
- .add_destroy_callbacks(model, reflection) ⇒ Object
- .add_touch_callbacks(model, reflection) ⇒ Object
- .define_callbacks(model, reflection) ⇒ Object
- .define_readers(mixin, name) ⇒ Object
- .define_validations(model, reflection) ⇒ Object
- .define_writers(mixin, name) ⇒ Object
- .macro ⇒ Object
-
.touch_record(o, changes, foreign_key, name, touch, touch_method) ⇒ Object
:nodoc:.
- .valid_dependent_options ⇒ Object
- .valid_options(options) ⇒ Object
Class Method Details
.add_change_callbacks(model, reflection) ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/torque/postgresql/associations/builder/belongs_to_many.rb', line 98 def self.add_change_callbacks(model, reflection) foreign_key = reflection.foreign_key name = reflection.name model.before_save ->(record) do before, after = record.changes[foreign_key] record.association(name).trigger(:before, before, after) if before && after end model.after_save ->(record) do before, after = record.previous_changes[foreign_key] record.association(name).trigger(:after, before, after) if before && after end end |
.add_default_callbacks(model, reflection) ⇒ Object
43 44 45 46 47 |
# File 'lib/torque/postgresql/associations/builder/belongs_to_many.rb', line 43 def self.add_default_callbacks(model, reflection) model.before_validation ->(o) do o.association(reflection.name).default(&reflection.[:default]) end end |
.add_destroy_callbacks(model, reflection) ⇒ Object
113 114 115 |
# File 'lib/torque/postgresql/associations/builder/belongs_to_many.rb', line 113 def self.add_destroy_callbacks(model, reflection) model.after_destroy lambda { |o| o.association(reflection.name).handle_dependency } end |
.add_touch_callbacks(model, reflection) ⇒ Object
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 49 def self.add_touch_callbacks(model, reflection) foreign_key = reflection.foreign_key n = reflection.name touch = reflection.[: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 model.after_create callback.call(:saved_changes), if: :saved_changes? model.after_update callback.call(:saved_changes), if: :saved_changes? model.after_destroy callback.call(:changes_to_save) model.after_touch callback.call(:changes_to_save) end |
.define_callbacks(model, reflection) ⇒ Object
20 21 22 23 24 25 |
# File 'lib/torque/postgresql/associations/builder/belongs_to_many.rb', line 20 def self.define_callbacks(model, reflection) super add_touch_callbacks(model, reflection) if reflection.[:touch] add_default_callbacks(model, reflection) if reflection.[:default] add_change_callbacks(model, reflection) end |
.define_readers(mixin, name) ⇒ Object
27 28 29 30 31 32 33 |
# File 'lib/torque/postgresql/associations/builder/belongs_to_many.rb', line 27 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
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/torque/postgresql/associations/builder/belongs_to_many.rb', line 117 def self.define_validations(model, reflection) if reflection..key?(:required) reflection.[:optional] = !reflection..delete(:required) end if reflection.[:optional].nil? required = model.belongs_to_many_required_by_default else required = !reflection.[:optional] end super if required model.validates_presence_of reflection.name, message: :required end end |
.define_writers(mixin, name) ⇒ Object
35 36 37 38 39 40 41 |
# File 'lib/torque/postgresql/associations/builder/belongs_to_many.rb', line 35 def self.define_writers(mixin, name) mixin.class_eval <<-CODE, __FILE__, __LINE__ + 1 def #{name}=(value) association(:#{name}).writer(value) end CODE end |
.macro ⇒ Object
8 9 10 |
# File 'lib/torque/postgresql/associations/builder/belongs_to_many.rb', line 8 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_options ⇒ Object
16 17 18 |
# File 'lib/torque/postgresql/associations/builder/belongs_to_many.rb', line 16 def self. [:restrict_with_error, :restrict_with_exception] end |
.valid_options(options) ⇒ Object
12 13 14 |
# File 'lib/torque/postgresql/associations/builder/belongs_to_many.rb', line 12 def self.() super + [:touch, :optional, :default, :dependent, :primary_key, :required] end |