Module: ActiveRecordInheritPreloadAssocPrepend
- Defined in:
- lib/active_record_inherit_assoc.rb
Instance Method Summary collapse
- #associate_records_to_owner(owner, records) ⇒ Object
- #associated_records_by_owner ⇒ Object
- #filter_associated_records_with_inherit!(owner, associated_records, inherit) ⇒ Object
- #scope ⇒ Object
Instance Method Details
#associate_records_to_owner(owner, records) ⇒ Object
61 62 63 64 65 66 67 |
# File 'lib/active_record_inherit_assoc.rb', line 61 def associate_records_to_owner(owner, records) if inherit = reflection.[:inherit] records = Array(records) filter_associated_records_with_inherit!(owner, records, inherit) end super end |
#associated_records_by_owner ⇒ Object
52 53 54 55 56 57 58 59 |
# File 'lib/active_record_inherit_assoc.rb', line 52 def associated_records_by_owner(*) super.tap do |result| next unless inherit = reflection.[:inherit] result.each do |owner, associated_records| filter_associated_records_with_inherit!(owner, associated_records, inherit) end end end |
#filter_associated_records_with_inherit!(owner, associated_records, inherit) ⇒ Object
83 84 85 86 87 88 89 |
# File 'lib/active_record_inherit_assoc.rb', line 83 def filter_associated_records_with_inherit!(owner, associated_records, inherit) associated_records.select! do |record| Array(inherit).all? do |association| record.send(association) == owner.send(association) end end end |
#scope ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/active_record_inherit_assoc.rb', line 70 def scope prescope = super if inherit = reflection.[:inherit] Array(inherit).each do |inherit_assoc| owner_values = owners.map(&inherit_assoc).compact.uniq prescope = prescope.where(inherit_assoc => owner_values) end end prescope end |