Class: ROM::Model::Form
- Inherits:
-
Object
- Object
- ROM::Model::Form
- 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.
Defined Under Namespace
Modules: ClassInterface
Instance Attribute Summary collapse
-
#model ⇒ Object
(also: #to_model)
readonly
Return model instance representing an ActiveModel object that will be persisted or updated.
-
#params ⇒ Object
readonly
Return raw params received from the request.
-
#result ⇒ Object
readonly
Return the result of commit!.
Attributes included from ClassInterface
#injectible_commands, #input_block, #self_commands, #validations_block, #validator
Instance Method Summary collapse
-
#attributes ⇒ Model::Attributes
Sanitize and coerce input params.
-
#commit! ⇒ Object
abstract
A specialized form object must implement this method.
-
#errors ⇒ ActiveModel::Errors
Return errors.
-
#initialize(params = {}, options = {}) ⇒ Form
constructor
private
A new instance of Form.
-
#save(*args) ⇒ self
Save a form by calling commit! and memoizing result.
-
#success? ⇒ TrueClass, FalseClass
Return whether commit was successful.
-
#validate! ⇒ Object
Trigger validation and store errors (if any).
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 = {}, = {}) @params = params @model = self.class.model.new(params.merge(.slice(*self.class.key))) @result = nil @errors = ActiveModel::Errors.new([]) .each { |key, value| instance_variable_set("@#{key}", value) } end |
Instance Attribute Details
#model ⇒ Object (readonly) Also known as: to_model
Return model instance representing an ActiveModel object that will be persisted or updated
66 67 68 |
# File 'lib/rom/rails/model/form.rb', line 66 def model @model end |
#params ⇒ Object (readonly)
Return raw params received from the request
58 59 60 |
# File 'lib/rom/rails/model/form.rb', line 58 def params @params end |
#result ⇒ Object (readonly)
Return the result of commit!
73 74 75 |
# File 'lib/rom/rails/model/form.rb', line 73 def result @result end |
Instance Method Details
#attributes ⇒ Model::Attributes
Sanitize and coerce input params
This can also set default values
136 137 138 |
# File 'lib/rom/rails/model/form.rb', line 136 def attributes self.class.attributes[params] end |
#commit! ⇒ Object
A specialized form object must implement this method
96 97 98 |
# File 'lib/rom/rails/model/form.rb', line 96 def commit! raise NotImplementedError, "#{self.class}#commit! must be implemented" end |
#errors ⇒ ActiveModel::Errors
Return 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
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
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 |