Module: ActiveRecordInheritPreloadAssocPrepend

Defined in:
lib/active_record_inherit_assoc.rb

Instance Method Summary collapse

Instance Method Details

#associate_records_to_owner(owner, records) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/active_record_inherit_assoc.rb', line 62

def associate_records_to_owner(owner, records)
  if inherit = reflection.options[:inherit]
    records = Array(records)
    filter_associated_records_with_inherit!(owner, records, inherit)
  end
  super
end

#associated_records_by_ownerObject



53
54
55
56
57
58
59
60
# File 'lib/active_record_inherit_assoc.rb', line 53

def associated_records_by_owner(*)
  super.tap do |result|
    next unless inherit = reflection.options[: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



87
88
89
90
91
92
93
94
# File 'lib/active_record_inherit_assoc.rb', line 87

def filter_associated_records_with_inherit!(owner, associated_records, inherit)
  associated_records.select! do |record|
    Array(inherit).all? do |association|
      record_value = record.send(association)
      record_value == owner.send(association) || reflection.options[:inherit_allowed_list]&.include?(record_value)
    end
  end
end

#scopeObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/active_record_inherit_assoc.rb', line 71

def scope
  prescope = super

  if inherit = reflection.options[:inherit]
    Array(inherit).each do |inherit_assoc|
      owner_values = owners.map(&inherit_assoc)
      owner_values.compact!
      owner_values.uniq!
      owner_values.concat(reflection.options[:inherit_allowed_list]) if reflection.options[:inherit_allowed_list]
      prescope = prescope.where(inherit_assoc => owner_values)
    end
  end

  prescope
end