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

#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

#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:



222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/rom/rails/model/form/class_interface.rb', line 222

def build(input = {}, options = {})
  commands =
    if mappings
      command_registry.each_with_object({}) { |(relation, registry), h|
        mapper = mappings[relation]

        h[relation] =
          if mapper
            registry.as(mapper)
          else
            registry
          end
      }
    else
      command_registry
    end
  new(input, options.merge(commands))
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)


121
122
123
124
125
# File 'lib/rom/rails/model/form/class_interface.rb', line 121

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



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

def inherited(klass)
  klass.inject_commands_for(*injectible_commands) if injectible_commands
  klass.commands(*self_commands) if self_commands
  input_blocks.each{|block| klass.input(readers: false, &block) }
  validation_blocks.each{|block| klass.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


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

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)


147
148
149
150
151
152
153
# File 'lib/rom/rails/model/form/class_interface.rb', line 147

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>)


96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/rom/rails/model/form/class_interface.rb', line 96

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)


180
181
182
183
# File 'lib/rom/rails/model/form/class_interface.rb', line 180

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