Module: ActiveRecordInheritPreloadAssocPrepend

Defined in:
lib/active_record_inherit_assoc.rb

Instance Method Summary collapse

Instance Method Details

#associate_records_to_owner(owner, records) ⇒ Object



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

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



45
46
47
48
49
50
51
52
# File 'lib/active_record_inherit_assoc.rb', line 45

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

#build_scopeObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/active_record_inherit_assoc.rb', line 63

def build_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

#filter_associated_records_with_inherit!(owner, associated_records, inherit) ⇒ Object



79
80
81
82
83
84
85
86
# File 'lib/active_record_inherit_assoc.rb', line 79

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