Module: ROM::Model::Form::ClassInterface

Included in:
ROM::Model::Form
Defined in:
lib/rom/rails/model/form/class_interface.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#attributesClass (readonly)

Return param handler class

This class is used to process input params coming from a request and it’s being created using ‘input` API

Examples:


class MyForm < ROM::Model::Form
  input do
    attribute :name, String
  end
end

MyForm.attributes # => MyForm::Attributes

# process input params
attributes = MyForm.attributes[name: 'Jane']

Returns:

  • (Class)


26
27
28
# File 'lib/rom/rails/model/form/class_interface.rb', line 26

def attributes
  @attributes
end

#injectible_commandsHash (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

A list of relation names for which commands should be injected from the rom env automatically.

This is used only when a given form re-uses existing commands

Returns:

  • (Hash)


71
72
73
# File 'lib/rom/rails/model/form/class_interface.rb', line 71

def injectible_commands
  @injectible_commands
end

#input_blockProc (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

input block stored to be used in inherited hook

Returns:

  • (Proc)


78
79
80
# File 'lib/rom/rails/model/form/class_interface.rb', line 78

def input_block
  @input_block
end

#modelClass (readonly)

Return model class

Returns:

  • (Class)


54
55
56
# File 'lib/rom/rails/model/form/class_interface.rb', line 54

def model
  @model
end

#self_commandsHash (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

relation => command name mapping used to generate commands automatically

Returns:

  • (Hash)


61
62
63
# File 'lib/rom/rails/model/form/class_interface.rb', line 61

def self_commands
  @self_commands
end

#validations_blockProc (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

validation block stored to be used in inherited hook

Returns:

  • (Proc)


85
86
87
# File 'lib/rom/rails/model/form/class_interface.rb', line 85

def validations_block
  @validations_block
end

#validatorClass (readonly)

Return attributes validator

Examples:

class MyForm < ROM::Model::Form
  input do
    attribute :name, String
  end

  validations do
    validates :name, presence: true
  end
end

attributes = MyForm.attributes[name: nil]
MyForm::Validator.call(attributes) # raises validation error

Returns:

  • (Class)


47
48
49
# File 'lib/rom/rails/model/form/class_interface.rb', line 47

def validator
  @validator
end

Instance Method Details

#build(input = {}, options = {}) ⇒ Model::Form

Build a form object using input params and options

Examples:

class MyForm < ROM::Model::Form
  input do
    set_model_name 'User'

    attribute :name, String
    attribute :age, Integer
  end
end

# form for a new object
form = MyForm.build(name: 'Jane')

# form for a persisted object
form = MyForm.build({ name: 'Jane' }, id: 1)

Returns:



236
237
238
# File 'lib/rom/rails/model/form/class_interface.rb', line 236

def build(input = {}, options = {})
  new(input, options.merge(command_registry))
end

#commands(names) ⇒ self

Specify what commands should be generated for a form object

Examples:

class MyForm < ROM::Model::Form
  commands users: :create
end

Parameters:

  • relation (Hash)

    > command name map

Returns:

  • (self)


135
136
137
138
139
# File 'lib/rom/rails/model/form/class_interface.rb', line 135

def commands(names)
  names.each { |relation, _action| attr_reader(relation) }
  @self_commands = names
  self
end

#inherited(klass) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Copy input attributes, validator and model to the descendant



90
91
92
93
94
95
96
# File 'lib/rom/rails/model/form/class_interface.rb', line 90

def inherited(klass)
  klass.inject_commands_for(*injectible_commands) if injectible_commands
  klass.commands(*self_commands) if self_commands
  klass.input(readers: false, &input_block) if input_block
  klass.validations(&validations_block) if validations_block
  super
end

#inject_commands_for(*names) ⇒ Object

Inject specific commands from the rom env

This can be used when the env has re-usable commands

Examples:

class MyForm < ROM::Model::Form
  inject_commands_for :users
end


209
210
211
212
213
# File 'lib/rom/rails/model/form/class_interface.rb', line 209

def inject_commands_for(*names)
  @injectible_commands = names
  names.each { |name| attr_reader(name) }
  self
end

#input(options = {}, &block) ⇒ self

Specify input params handler class

This uses Virtus DSL

Examples:

class MyForm < ROM::Model::Form
  input do
    set_model_name 'User'

    attribute :name, String
    attribute :age, Integer
  end
end

MyForm.build(name: 'Jane', age: 21).attributes
# => #<MyForm::Attributes:0x007f821f863d48 @name="Jane", @age=21>

Returns:

  • (self)


161
162
163
164
165
166
167
# File 'lib/rom/rails/model/form/class_interface.rb', line 161

def input(options = {}, &block)
  readers = options.fetch(:readers) { true }
  define_attributes!(block)
  define_attribute_readers! if readers
  define_model!
  self
end

#key(*keys) ⇒ Array<Symbol>

Set key for the model that is handled by a form object

This defaults to [:id]

Examples:

class MyForm < ROM::Model::Form
  key [:user_id]
end

Returns:

  • (Array<Symbol>)


110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/rom/rails/model/form/class_interface.rb', line 110

def key(*keys)
  if keys.any? && !@key
    @key = keys
    attr_reader(*keys)
  elsif !@key
    @key = [:id]
    attr_reader :id
  elsif keys.any?
    @key = keys
  end
  @key
end

#validations(&block) ⇒ self

Specify attribute validator class

This uses ActiveModel::Validations DSL

Examples:

class MyForm < ROM::Model::Form
  input do
    set_model_name 'User'

    attribute :name, String
    attribute :age, Integer
  end

  validations do
    validates :name, :age, presence: true
  end
end

form = MyForm.build(name: 'Jane', age: nil)
# => #<MyForm::Attributes:0x007f821f863d48 @name="Jane", @age=21>
form.validate! # raises

Returns:

  • (self)


194
195
196
197
# File 'lib/rom/rails/model/form/class_interface.rb', line 194

def validations(&block)
  define_validator!(block)
  self
end