Class: ROM::Model::Form

Inherits:
Object
  • Object
show all
Extended by:
ClassMacros, ClassInterface
Defined in:
lib/rom/rails/model/form.rb,
lib/rom/rails/model/form/class_interface.rb

Overview

Abstract form class

Form objects in ROM are your top-level interface to persist data in the database. They combine many features that you know from ActiveRecord:

* params processing with sanitization and coercion
* attribute validations
* persisting data in the database

The major difference is that a ROM form object separates those responsibilities - a ROM form class has its own Attributes, Validator and ROM commands that are accessible within its instance.

Examples:

class UserForm < ROM::Model::Form
  commands users: :create

  input do
    set_model_name 'User'

    attribute :name, String
  end

  validations do
    validates :name, presence: true
  end
end

class CreateUserForm < UserForm
  attributes.timestamps :created_at

  def commit!
    users.try { users.create.call(attributes) }
  end
end

# then in your controller
CreateUserForm.build(params[:user]).save

Defined Under Namespace

Modules: ClassInterface

Instance Attribute Summary collapse

Attributes included from ClassInterface

#injectible_commands, #input_block, #self_commands, #validations_block, #validator

Instance Method Summary collapse

Methods included from ClassInterface

build, commands, inherited, inject_commands_for, input, key, validations

Constructor Details

#initialize(params = {}, options = {}) ⇒ Form

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.

Returns a new instance of Form.



83
84
85
86
87
88
89
# File 'lib/rom/rails/model/form.rb', line 83

def initialize(params = {}, options = {})
  @params = params
  @model  = self.class.model.new(params.merge(options.slice(*self.class.key)))
  @result = nil
  @errors =  ActiveModel::Errors.new([])
  options.each { |key, value| instance_variable_set("@#{key}", value) }
end

Instance Attribute Details

#modelObject (readonly) Also known as: to_model

Return model instance representing an ActiveModel object that will be persisted or updated

Returns:

  • (Object)


66
67
68
# File 'lib/rom/rails/model/form.rb', line 66

def model
  @model
end

#paramsObject (readonly)

Return raw params received from the request

Returns:

  • (Object)


58
59
60
# File 'lib/rom/rails/model/form.rb', line 58

def params
  @params
end

#resultObject (readonly)

Return the result of commit!

Returns:

  • (Object)


73
74
75
# File 'lib/rom/rails/model/form.rb', line 73

def result
  @result
end

Instance Method Details

#attributesModel::Attributes

Sanitize and coerce input params

This can also set default values

Returns:



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

def attributes
  self.class.attributes[params]
end

#commit!Object

This method is abstract.

A specialized form object must implement this method

Raises:

  • (NotImplementedError)


96
97
98
# File 'lib/rom/rails/model/form.rb', line 96

def commit!
  raise NotImplementedError, "#{self.class}#commit! must be implemented"
end

#errorsActiveModel::Errors

Return errors

Returns:

  • (ActiveModel::Errors)


145
146
147
# File 'lib/rom/rails/model/form.rb', line 145

def errors
  (result && result.error) || @errors
end

#save(*args) ⇒ self

Save a form by calling commit! and memoizing result

Returns:

  • (self)


105
106
107
108
# File 'lib/rom/rails/model/form.rb', line 105

def save(*args)
  @result = commit!(*args)
  self
end

#success?TrueClass, FalseClass

Return whether commit was successful

Returns:

  • (TrueClass, FalseClass)


115
116
117
# File 'lib/rom/rails/model/form.rb', line 115

def success?
  errors.nil? || !errors.any?
end

#validate!Object

Trigger validation and store errors (if any)



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

def validate!
  validator = self.class::Validator.new(attributes)
  validator.validate

  @errors = validator.errors
end