Class: Macros::Model::Build

Inherits:
Base
  • Object
show all
Defined in:
lib/macros/model/build.rb

Instance Method Summary collapse

Methods inherited from Base

register

Constructor Details

#initialize(from:, respond_to: nil) ⇒ Macros::Model::Build

Returns step macro instance.

Examples:

Macros::Model::Build(from: :scope)

model should respond to :last_sign_in_at

Macros::Model::Build(from: :scope, respond_to: :last_sign_in_at)

Parameters:

  • from (Hash)

    required attribute, key in the context to build model class from

  • condition (Hash)

    attribute the model should respond to as the condition to be set.



14
15
16
17
# File 'lib/macros/model/build.rb', line 14

def initialize(from:, respond_to: nil)
  @from = from
  @respond_to = respond_to
end

Instance Method Details

#call(ctx) ⇒ Object

Sets the model in the context

Parameters:

  • ctx (Trailblazer::Skill)

    tbl context hash



21
22
23
24
25
26
27
28
29
# File 'lib/macros/model/build.rb', line 21

def call(ctx, **)
  scope = ctx[@from]
  return false unless scope

  klass = scope.to_s.classify.constantize
  return false if @respond_to && !klass.new.respond_to?(@respond_to)

  ctx[:model] = klass.new
end