Class: Dbee::Dsl::AssociationBuilder
- Inherits:
-
Object
- Object
- Dbee::Dsl::AssociationBuilder
- Defined in:
- lib/dbee/dsl/association_builder.rb
Overview
This is really syntactic sugar built on top of Association. It can handle two main use-cases when declaring associations:
-
parent: create an association to a parent model (foreign key is on immediate table)
-
child: create an association to a child model (foreign key is on child table)
Instance Attribute Summary collapse
-
#inflector ⇒ Object
readonly
Returns the value of attribute inflector.
Instance Method Summary collapse
- #child_association(on_class_name, name, opts = {}) ⇒ Object
-
#initialize(inflector) ⇒ AssociationBuilder
constructor
A new instance of AssociationBuilder.
- #parent_association(on_class_name, name, opts = {}) ⇒ Object
Constructor Details
#initialize(inflector) ⇒ AssociationBuilder
Returns a new instance of AssociationBuilder.
19 20 21 22 23 24 25 |
# File 'lib/dbee/dsl/association_builder.rb', line 19 def initialize(inflector) raise ArgumentError, 'inflector is required' unless inflector @inflector = inflector freeze end |
Instance Attribute Details
#inflector ⇒ Object (readonly)
Returns the value of attribute inflector.
17 18 19 |
# File 'lib/dbee/dsl/association_builder.rb', line 17 def inflector @inflector end |
Instance Method Details
#child_association(on_class_name, name, opts = {}) ⇒ Object
36 37 38 39 40 41 42 43 |
# File 'lib/dbee/dsl/association_builder.rb', line 36 def child_association(on_class_name, name, opts = {}) reference_constraint = { name: opts[:foreign_key] || inflector.foreign_key(on_class_name), parent: opts[:primary_key] || :id } association(on_class_name, name, opts, reference_constraint) end |
#parent_association(on_class_name, name, opts = {}) ⇒ Object
27 28 29 30 31 32 33 34 |
# File 'lib/dbee/dsl/association_builder.rb', line 27 def parent_association(on_class_name, name, opts = {}) reference_constraint = { name: opts[:foreign_key] || :id, parent: opts[:primary_key] || inflector.foreign_key(name) } association(on_class_name, name, opts, reference_constraint) end |