Class: Torque::PostgreSQL::Associations::BelongsToManyAssociation
- Inherits:
-
ActiveRecord::Associations::CollectionAssociation
- Object
- ActiveRecord::Associations::CollectionAssociation
- Torque::PostgreSQL::Associations::BelongsToManyAssociation
- Includes:
- ActiveRecord::Associations::ForeignAssociation
- Defined in:
- lib/torque/postgresql/associations/belongs_to_many_association.rb
Instance Method Summary collapse
- #empty? ⇒ Boolean
- #handle_dependency ⇒ Object
- #ids_reader ⇒ Object
- #ids_writer(new_ids) ⇒ Object
- #include?(record) ⇒ Boolean
- #insert_record(record) ⇒ Object
Instance Method Details
#empty? ⇒ Boolean
48 49 50 |
# File 'lib/torque/postgresql/associations/belongs_to_many_association.rb', line 48 def empty? size.zero? end |
#handle_dependency ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/torque/postgresql/associations/belongs_to_many_association.rb', line 9 def handle_dependency case [:dependent] when :restrict_with_exception raise ::ActiveRecord::DeleteRestrictionError.new(reflection.name) unless empty? when :restrict_with_error unless empty? record = owner.class.human_attribute_name(reflection.name).downcase owner.errors.add(:base, :'restrict_dependent_destroy.has_many', record: record) throw(:abort) end when :destroy # No point in executing the counter update since we're going to destroy the parent anyway load_target.each { |t| t.destroyed_by_association = reflection } destroy_all else delete_all end end |
#ids_reader ⇒ Object
30 31 32 |
# File 'lib/torque/postgresql/associations/belongs_to_many_association.rb', line 30 def ids_reader owner[reflection.active_record_primary_key] end |
#ids_writer(new_ids) ⇒ Object
34 35 36 37 38 |
# File 'lib/torque/postgresql/associations/belongs_to_many_association.rb', line 34 def ids_writer(new_ids) column = reflection.active_record_primary_key owner.update_column(column, owner[column] = new_ids.presence) @association_scope = nil end |
#include?(record) ⇒ Boolean
52 53 54 55 |
# File 'lib/torque/postgresql/associations/belongs_to_many_association.rb', line 52 def include?(record) list = owner[reflection.active_record_primary_key] ids_reader && ids_reader.include?(record[klass_fk]) end |
#insert_record(record) ⇒ Object
40 41 42 43 44 45 46 |
# File 'lib/torque/postgresql/associations/belongs_to_many_association.rb', line 40 def insert_record(record, *) super attribute = (ids_reader || owner[reflection.active_record_primary_key] = []) attribute.push(record[klass_fk]) record end |