Module: Railsmith::BaseService::AssociationDsl::ClassMethods

Defined in:
lib/railsmith/base_service/association_dsl.rb

Overview

Class-level DSL macros for declaring associations on a service.

Instance Method Summary collapse

Instance Method Details

#association_registryObject

Returns the AssociationRegistry for this class.



90
91
92
# File 'lib/railsmith/base_service/association_dsl.rb', line 90

def association_registry
  @association_registry ||= AssociationRegistry.new
end

#belongs_to(name, service:, foreign_key: nil, optional: false) ⇒ Object

Declare a belongs_to association.

Parameters:

  • name (Symbol)

    association key

  • service (Class)

    service class for the parent record

  • foreign_key (Symbol) (defaults to: nil)

    explicit FK; inferred as “#name_id” when omitted

  • optional (Boolean) (defaults to: false)

    skip presence validation (default: false)



78
79
80
81
82
83
84
85
86
87
# File 'lib/railsmith/base_service/association_dsl.rb', line 78

def belongs_to(name, service:, foreign_key: nil, optional: false)
  association_registry.register(
    AssociationDefinition.new(
      name, :belongs_to,
      service: service,
      foreign_key: foreign_key,
      optional: optional
    )
  )
end

#has_many(name, service:, foreign_key: nil, dependent: :ignore, validate: true, async: false) ⇒ Object

Declare a has_many association.

Parameters:

  • name (Symbol)

    association key (matches nested param key)

  • service (Class)

    service class for the associated records

  • foreign_key (Symbol) (defaults to: nil)

    explicit FK; inferred from parent model when omitted

  • dependent (Symbol) (defaults to: :ignore)

    :destroy, :nullify, :restrict, or :ignore (default)

  • validate (Boolean) (defaults to: true)

    validate nested records (default: true)

  • async (Boolean) (defaults to: false)

    when true, nested writes are enqueued as background jobs instead of running inline inside the parent’s transaction (default: false). Not compatible with dependent: :destroy/:nullify/:restrict.



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/railsmith/base_service/association_dsl.rb', line 36

def has_many(name, service:, foreign_key: nil, dependent: :ignore, validate: true, async: false)
  association_registry.register(
    AssociationDefinition.new(
      name, :has_many,
      service: service,
      foreign_key: foreign_key,
      dependent: dependent,
      validate: validate,
      async: async
    )
  )
end

#has_one(name, service:, foreign_key: nil, dependent: :ignore, validate: true, async: false) ⇒ Object

Declare a has_one association.

Parameters:

  • name (Symbol)

    association key

  • service (Class)

    service class for the associated record

  • foreign_key (Symbol) (defaults to: nil)

    explicit FK; inferred from parent model when omitted

  • dependent (Symbol) (defaults to: :ignore)

    :destroy, :nullify, :restrict, or :ignore (default)

  • validate (Boolean) (defaults to: true)

    validate nested records (default: true)

  • async (Boolean) (defaults to: false)

    when true, the nested write is enqueued as a background job instead of running inline. Not compatible with dependent: :destroy/:nullify/:restrict.



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/railsmith/base_service/association_dsl.rb', line 59

def has_one(name, service:, foreign_key: nil, dependent: :ignore, validate: true, async: false)
  association_registry.register(
    AssociationDefinition.new(
      name, :has_one,
      service: service,
      foreign_key: foreign_key,
      dependent: dependent,
      validate: validate,
      async: async
    )
  )
end

#inherited(subclass) ⇒ Object



94
95
96
97
# File 'lib/railsmith/base_service/association_dsl.rb', line 94

def inherited(subclass)
  super
  subclass.instance_variable_set(:@association_registry, association_registry.dup)
end