Class: Railsmith::BaseService::AssociationDefinition
- Inherits:
-
Object
- Object
- Railsmith::BaseService::AssociationDefinition
- Defined in:
- lib/railsmith/base_service/association_definition.rb
Overview
Value object representing a single declared association on a service.
Stores the association name, kind (:has_many, :has_one, :belongs_to), the associated service class, and options governing cascading behaviour.
Constant Summary collapse
- ASYNC_INCOMPATIBLE_DEPENDENT =
Dependent modes that imply cascading service-layer behavior on parent destruction. Async nested writes cannot honor these guarantees because the job runs after the parent transaction has committed, so combining them is rejected up-front.
%i[destroy nullify restrict].freeze
Instance Attribute Summary collapse
-
#async ⇒ Object
readonly
Returns the value of attribute async.
-
#dependent ⇒ Object
readonly
Returns the value of attribute dependent.
-
#foreign_key ⇒ Object
readonly
Returns the value of attribute foreign_key.
-
#kind ⇒ Object
readonly
Returns the value of attribute kind.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#optional ⇒ Object
readonly
Returns the value of attribute optional.
-
#service_class ⇒ Object
readonly
Returns the value of attribute service_class.
-
#validate ⇒ Object
readonly
Returns the value of attribute validate.
Instance Method Summary collapse
-
#async? ⇒ Boolean
Returns true when this association should be written in a background job rather than inline inside the parent’s transaction.
-
#inferred_foreign_key(parent_model_class = nil) ⇒ Object
Returns the FK column name (Symbol) for this association.
-
#initialize(name, kind, service:, **options) ⇒ AssociationDefinition
constructor
A new instance of AssociationDefinition.
Constructor Details
#initialize(name, kind, service:, **options) ⇒ AssociationDefinition
Returns a new instance of AssociationDefinition.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/railsmith/base_service/association_definition.rb', line 22 def initialize(name, kind, service:, **) @name = name.to_sym @kind = kind.to_sym @service_class = service @foreign_key = [:foreign_key]&.to_sym @dependent = (.fetch(:dependent, :ignore) || :ignore).to_sym @optional = .fetch(:optional, false) @validate = .fetch(:validate, true) @async = .fetch(:async, false) ? true : false validate_async_dependent_compatibility! freeze end |
Instance Attribute Details
#async ⇒ Object (readonly)
Returns the value of attribute async.
16 17 18 |
# File 'lib/railsmith/base_service/association_definition.rb', line 16 def async @async end |
#dependent ⇒ Object (readonly)
Returns the value of attribute dependent.
16 17 18 |
# File 'lib/railsmith/base_service/association_definition.rb', line 16 def dependent @dependent end |
#foreign_key ⇒ Object (readonly)
Returns the value of attribute foreign_key.
16 17 18 |
# File 'lib/railsmith/base_service/association_definition.rb', line 16 def foreign_key @foreign_key end |
#kind ⇒ Object (readonly)
Returns the value of attribute kind.
16 17 18 |
# File 'lib/railsmith/base_service/association_definition.rb', line 16 def kind @kind end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
16 17 18 |
# File 'lib/railsmith/base_service/association_definition.rb', line 16 def name @name end |
#optional ⇒ Object (readonly)
Returns the value of attribute optional.
16 17 18 |
# File 'lib/railsmith/base_service/association_definition.rb', line 16 def optional @optional end |
#service_class ⇒ Object (readonly)
Returns the value of attribute service_class.
16 17 18 |
# File 'lib/railsmith/base_service/association_definition.rb', line 16 def service_class @service_class end |
#validate ⇒ Object (readonly)
Returns the value of attribute validate.
16 17 18 |
# File 'lib/railsmith/base_service/association_definition.rb', line 16 def validate @validate end |
Instance Method Details
#async? ⇒ Boolean
Returns true when this association should be written in a background job rather than inline inside the parent’s transaction.
39 40 41 |
# File 'lib/railsmith/base_service/association_definition.rb', line 39 def async? @async end |
#inferred_foreign_key(parent_model_class = nil) ⇒ Object
Returns the FK column name (Symbol) for this association. Falls back to auto-inference from the parent model class when no explicit foreign_key was given.
has_many / has_one: FK lives on the child → parent_model_id (e.g. order_id) belongs_to: FK lives on this record → association_name_id (e.g. customer_id)
51 52 53 54 55 56 57 58 59 60 |
# File 'lib/railsmith/base_service/association_definition.rb', line 51 def inferred_foreign_key(parent_model_class = nil) return @foreign_key if @foreign_key case kind when :has_many, :has_one :"#{underscore_model_name(parent_model_class)}_id" when :belongs_to :"#{name}_id" end end |