Skip to content
Mike Nelson edited this page Jan 30, 2020 · 9 revisions

WIP PAGE

Inputs provided to an Op are accessible in a handful of ways. Let's take a look at an example Op:

class ContactInfoUpdateOp < Subroutine::Op
  include ::Subroutine::AssociationFields
  
  association :user
  string :email
  string :phone

  def perform
    # this is a very unnecessary explicit assignment for the sake of example
    user.email = email if field_provided?(:email)
    user.phone = phone if field_provided?(:phone)
    user.save! if user.changed?
  end
end

class UserInfoUpdateOp < ::Subroutine::Op
  include ::Subroutine::AssociationFields
  
  association :user

  with_options group: :user do
    string :first_name
    string :last_name
    date :dob
  end

  inputs_from ContactInfoUpdateOp, group :contact
  
  def perform
    user.update(user_params)
    ContactInfoUpdateOp.submit!(contact_params.merge(user: user))
  end
end

params

Using params is the most common way to access Op inputs. It retrieves the inputs passed to the op. It does not include the default values or input not defined by field declarations.

op = UserInfoUpdateOp.new
op.params # => { }

Clone this wiki locally