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/error_proxy.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 Classes: ErrorProxy
Instance Attribute Summary collapse
-
#errors ⇒ ErrorProxy
readonly
Return any errors with the form.
-
#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, #self_commands, #validator
Instance Method Summary collapse
-
#attributes ⇒ Model::Attributes
Sanitize and coerce input params.
-
#commit! ⇒ Object
abstract
A specialized form object must implement this method.
-
#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.
91 92 93 94 95 96 97 |
# File 'lib/rom/rails/model/form.rb', line 91 def initialize(params = {}, = {}) @params = params @model = self.class.model.new(params.merge(.slice(*self.class.key))) @result = nil @errors = ErrorProxy.new .each { |key, value| instance_variable_set("@#{key}", value) } end |
Instance Attribute Details
#errors ⇒ ErrorProxy (readonly)
Return any errors with the form
81 82 83 |
# File 'lib/rom/rails/model/form.rb', line 81 def errors @errors end |
#model ⇒ Object (readonly) Also known as: to_model
Return model instance representing an ActiveModel object that will be persisted or updated
67 68 69 |
# File 'lib/rom/rails/model/form.rb', line 67 def model @model end |
#params ⇒ Object (readonly)
Return raw params received from the request
59 60 61 |
# File 'lib/rom/rails/model/form.rb', line 59 def params @params end |
#result ⇒ Object (readonly)
Return the result of commit!
74 75 76 |
# File 'lib/rom/rails/model/form.rb', line 74 def result @result end |
Instance Method Details
#attributes ⇒ Model::Attributes
Sanitize and coerce input params
This can also set default values
149 150 151 |
# File 'lib/rom/rails/model/form.rb', line 149 def attributes self.class.attributes[params] end |
#commit! ⇒ Object
A specialized form object must implement this method
104 105 106 |
# File 'lib/rom/rails/model/form.rb', line 104 def commit! raise NotImplementedError, "#{self.class}#commit! must be implemented" end |
#save(*args) ⇒ self
Save a form by calling commit! and memoizing result
113 114 115 116 117 118 119 120 |
# File 'lib/rom/rails/model/form.rb', line 113 def save(*args) @errors.clear @result = commit!(*args) @errors.set @result.error if result.respond_to? :error self end |
#success? ⇒ TrueClass, FalseClass
Return whether commit was successful
127 128 129 |
# File 'lib/rom/rails/model/form.rb', line 127 def success? errors.success? end |
#validate! ⇒ Object
Trigger validation and store errors (if any)
134 135 136 137 138 139 140 |
# File 'lib/rom/rails/model/form.rb', line 134 def validate! @errors.clear validator = self.class::Validator.new(attributes) validator.validate @errors.set validator.errors end |