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_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_default_callbacks(model, reflection) ⇒ Object
42 43 44 45 46 |
# File 'lib/torque/postgresql/associations/builder/belongs_to_many.rb', line 42 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
97 98 99 |
# File 'lib/torque/postgresql/associations/builder/belongs_to_many.rb', line 97 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
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/torque/postgresql/associations/builder/belongs_to_many.rb', line 48 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 |
# 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] end |
.define_readers(mixin, name) ⇒ Object
26 27 28 29 30 31 32 |
# File 'lib/torque/postgresql/associations/builder/belongs_to_many.rb', line 26 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
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/torque/postgresql/associations/builder/belongs_to_many.rb', line 101 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
34 35 36 37 38 39 40 |
# File 'lib/torque/postgresql/associations/builder/belongs_to_many.rb', line 34 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:
66 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 |
# File 'lib/torque/postgresql/associations/builder/belongs_to_many.rb', line 66 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 |