20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
# File 'lib/associate_jsonb/associations/builder/belongs_to.rb', line 20
def add_association_accessor_methods(mixin, reflection)
foreign_key = reflection.foreign_key.to_s
key = (reflection.jsonb_store_key || foreign_key).to_s
store = reflection.jsonb_store_attr
mixin.instance_eval <<~CODE, __FILE__, __LINE__ + 1
if attribute_names.include?(foreign_key)
raise AssociateJsonb::Associations::
ConflictingAssociation,
"Association with foreign key :#{foreign_key} already "\
"exists on #{reflection.active_record.name}"
end
CODE
opts = {}
foreign_type = :integer
sql_type = "numeric"
begin
primary_key = reflection.active_record_primary_key.to_s
primary_column = reflection.klass.columns.find {|col| col.name == primary_key }
if primary_column
foreign_type = primary_column.type
sql_data = primary_column.sql_type_metadata.as_json
sql_type = sql_data["sql_type"]
%i[ limit precision scale ].each do |k|
opts[k] = sql_data[k.to_s] if sql_data[k.to_s]
end
end
rescue
opts = { limit: 8 }
foreign_type = :integer
end
mixin.instance_eval <<~CODE, __FILE__, __LINE__ + 1
store_column_attribute(:#{store}, :#{foreign_key}, foreign_type, sql_type: sql_type, key: "#{key}", **opts)
CODE
end
|