Module: AssociateJsonb::Associations::Builder::BelongsTo
- Defined in:
- lib/associate_jsonb/associations/builder/belongs_to.rb
Overview
:nodoc:
Instance Method Summary collapse
- #add_association_accessor_methods(mixin, reflection) ⇒ Object
- #define_accessors(mixin, reflection) ⇒ Object
-
#valid_options(options) ⇒ Object
Maybe convert to builder callbacks here in next iteration def define_callbacks(model, reflection) super add_after_jsonb_initialize_callbacks(model, reflection) if reflection.jsonb_store? add_after_foreign_store_initialize_callbacks(model, reflection) if reflection.foreign_store? end.
Instance Method Details
#add_association_accessor_methods(mixin, reflection) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/associate_jsonb/associations/builder/belongs_to.rb', line 43 def add_association_accessor_methods(mixin, reflection) foreign_key = reflection.join_foreign_key.to_s key = (reflection.jsonb_store_key || foreign_key).to_s store = reflection.jsonb_store_attr mixin.instance_eval " if attribute_names.include?(foreign_key)\n raise AssociateJsonb::Associations::\n ConflictingAssociation,\n \"Association with foreign key :\#{foreign_key} already \"\\\n \"exists on \#{reflection.active_record.name}\"\n end\n CODE\n\n opts = {}\n foreign_type = :integer\n sql_type = \"numeric\"\n begin\n primary_key = reflection.join_primary_key.to_s\n primary_column = reflection.klass.columns_hash[primary_key]\n\n if primary_column\n foreign_type = primary_column.type\n sql_data = primary_column.sql_type_metadata.as_json\n sql_type = sql_data[\"sql_type\"]\n %i[ limit precision scale ].each do |k|\n opts[k] = sql_data[k.to_s] if sql_data[k.to_s]\n end\n end\n rescue\n opts = { limit: 8 }\n foreign_type = :integer\n end\n\n mixin.instance_eval <<~CODE, __FILE__, __LINE__ + 1\n store_column_attribute(:\#{store}, :\#{foreign_key}, foreign_type, sql_type: sql_type, key: \"\#{key}\", **opts)\n CODE\nend\n", __FILE__, __LINE__ + 1 |
#define_accessors(mixin, reflection) ⇒ Object
35 36 37 38 39 40 41 |
# File 'lib/associate_jsonb/associations/builder/belongs_to.rb', line 35 def define_accessors(mixin, reflection) if reflection..key?(:store) add_association_accessor_methods(mixin, reflection) end super end |
#valid_options(options) ⇒ Object
Maybe convert to builder callbacks here in next iteration def define_callbacks(model, reflection)
super
add_after_jsonb_initialize_callbacks(model, reflection) if reflection.jsonb_store?
add_after_foreign_store_initialize_callbacks(model, reflection) if reflection.foreign_store?
end
def add_after_jsonb_initialize_callbacks(model, reflection)
model.after_initialize lambda {|record|
association = association(reflection.name)
p model, reflection.join_primary_key, reflection.join_foreign_key, reflection.
p record.attributes
# record.attributes._write_attribute()
}
end
def add_after_foreign_store_initialize_callbacks(model, reflection)
model.after_initialize lambda {|record|
association = association(reflection.name)
# record.attributes._write_attribute()
}
end
31 32 33 |
# File 'lib/associate_jsonb/associations/builder/belongs_to.rb', line 31 def () super + i[ store store_key ] end |