Class: RelationToJSON::HasOneReflection
Instance Attribute Summary
#name, #reflection, #required_columns
Instance Method Summary
collapse
#foreign_key, #initialize, #primary_key
Instance Method Details
#association_relation(transposed) ⇒ Object
21
22
23
24
25
|
# File 'lib/relation_to_json/has_one_reflection.rb', line 21
def association_relation(transposed)
query = { foreign_key => transposed[primary_key] }
query[polymorphic_association_key] = foreign_class if polymorphic?
klass.where(**query)
end
|
#foreign_class ⇒ Object
36
37
38
|
# File 'lib/relation_to_json/has_one_reflection.rb', line 36
def foreign_class
reflection.active_record.name
end
|
#pluck_association_columns(transposed) ⇒ Object
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
# File 'lib/relation_to_json/has_one_reflection.rb', line 5
def pluck_association_columns(transposed)
return recurse_json_with_schema(transposed) if nested_relations?
required_columns = Set[primary_key, *@required_columns]
plucked_attributes = association_relation(transposed)
.pluck(*required_columns)
.map { |plucked| required_columns.zip(Array.wrap(plucked)).to_h }
foreign_key_indexed_plucked_values = plucked_attributes
.to_h { |attributes| [attributes[foreign_key], attributes] }
transposed.fetch(primary_key, []).map do |record_foreign_key|
foreign_key_indexed_plucked_values[record_foreign_key]
end
end
|
#polymorphic? ⇒ Boolean
27
28
29
|
# File 'lib/relation_to_json/has_one_reflection.rb', line 27
def polymorphic?
reflection.inverse_of.polymorphic?
end
|
#polymorphic_association_key ⇒ Object
31
32
33
34
|
# File 'lib/relation_to_json/has_one_reflection.rb', line 31
def polymorphic_association_key
reflection.type
end
|