Module: ROM::Model::Form::ClassInterface
- Included in:
- ROM::Model::Form
- Defined in:
- lib/rom/rails/model/form/class_interface.rb
Instance Attribute Summary collapse
-
#attributes ⇒ Class
readonly
Return param handler class.
-
#injectible_commands ⇒ Hash
readonly
private
A list of relation names for which commands should be injected from the rom env automatically.
-
#model ⇒ Class
readonly
Return model class.
-
#self_commands ⇒ Hash
readonly
private
relation => command name mapping used to generate commands automatically.
-
#validator ⇒ Class
readonly
Return attributes validator.
Instance Method Summary collapse
-
#build(input = {}, options = {}) ⇒ Model::Form
Build a form object using input params and options.
-
#commands(names) ⇒ self
Specify what commands should be generated for a form object.
-
#inherited(klass) ⇒ Object
private
Copy input attributes, validator and model to the descendant.
-
#inject_commands_for(*names) ⇒ Object
Inject specific commands from the rom env.
-
#input(options = {}, &block) ⇒ self
Specify input params handler class.
-
#key(*keys) ⇒ Array<Symbol>
Set key for the model that is handled by a form object.
-
#validations(&block) ⇒ self
Specify attribute validator class.
Instance Attribute Details
#attributes ⇒ Class (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
26 27 28 |
# File 'lib/rom/rails/model/form/class_interface.rb', line 26 def attributes @attributes end |
#injectible_commands ⇒ Hash (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
71 72 73 |
# File 'lib/rom/rails/model/form/class_interface.rb', line 71 def injectible_commands @injectible_commands end |
#model ⇒ Class (readonly)
Return model class
54 55 56 |
# File 'lib/rom/rails/model/form/class_interface.rb', line 54 def model @model end |
#self_commands ⇒ Hash (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
61 62 63 |
# File 'lib/rom/rails/model/form/class_interface.rb', line 61 def self_commands @self_commands end |
#validator ⇒ Class (readonly)
Return attributes validator
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
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 = {}, = {}) 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, .merge(commands)) end |
#commands(names) ⇒ self
Specify what commands should be generated for a form object
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
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
147 148 149 150 151 152 153 |
# File 'lib/rom/rails/model/form/class_interface.rb', line 147 def input( = {}, &block) readers = .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]
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
180 181 182 183 |
# File 'lib/rom/rails/model/form/class_interface.rb', line 180 def validations(&block) define_validator!(block) self end |