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 <<~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.join_primary_key.to_s primary_column = reflection.klass.columns_hash[primary_key] if primary_column foreign_type = primary_column.type sql_data = primary_column..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 |
#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 |