Class: Superform::Namespace
- Includes:
- Enumerable
- Defined in:
- lib/superform.rb
Overview
A Namespace maps and object to values, but doesn’t actually have a value itself. For example, a ‘User` object or ActiveRecord model could be passed into the `:user` namespace. To access the values on a Namespace, the `field` can be called for single values.
Additionally, to access namespaces within a namespace, such as if a ‘User has_many :addresses` in ActiveRecord, the `namespace` method can be called which will return another Namespace object and set the current Namespace as the parent.
Instance Attribute Summary collapse
-
#object ⇒ Object
readonly
Returns the value of attribute object.
Attributes inherited from Node
Class Method Summary collapse
-
.root ⇒ Object
Creates a root Namespace, which is essentially a form.
Instance Method Summary collapse
-
#assign(hash) ⇒ Object
Assigns a hash to the current namespace and children namespace.
-
#collection(key) ⇒ Object
Wraps an array of objects in Namespace classes.
-
#each ⇒ Object
Iterates through the children of the current namespace, which could be ‘Namespace` or `Field` objects.
-
#field(key) ⇒ Object
Maps the ‘Object#proprety` and `Object#property=` to a field in a web form that can be read and set by the form.
-
#initialize(key, parent:, object: nil, field_class: Field) {|_self| ... } ⇒ Namespace
constructor
A new instance of Namespace.
-
#namespace(key, &block) ⇒ Object
Creates a ‘Namespace` child instance with the parent set to the current instance, adds to the `@children` Hash to ensure duplicate child namespaces aren’t created, then calls the method on the ‘@object` to get the child object to pass into that namespace.
-
#serialize ⇒ Object
Creates a Hash of Hashes and Arrays that represent the fields and collections of the Superform.
Constructor Details
#initialize(key, parent:, object: nil, field_class: Field) {|_self| ... } ⇒ Namespace
Returns a new instance of Namespace.
85 86 87 88 89 90 91 |
# File 'lib/superform.rb', line 85 def initialize(key, parent:, object: nil, field_class: Field) super(key, parent: parent) @object = object @field_class = field_class @children = Hash.new yield self if block_given? end |
Instance Attribute Details
#object ⇒ Object (readonly)
Returns the value of attribute object.
83 84 85 |
# File 'lib/superform.rb', line 83 def object @object end |
Class Method Details
.root ⇒ Object
Creates a root Namespace, which is essentially a form.
171 172 173 |
# File 'lib/superform.rb', line 171 def self.root(*, **, &) new(*, parent: nil, **, &) end |
Instance Method Details
#assign(hash) ⇒ Object
Assigns a hash to the current namespace and children namespace.
163 164 165 166 167 168 |
# File 'lib/superform.rb', line 163 def assign(hash) each do |child| child.assign hash[child.key] end self end |
#collection(key) ⇒ Object
Wraps an array of objects in Namespace classes. For example, if ‘User#addresses` returns an enumerable or array of `Address` classes:
“‘ruby Superform :user, object: User.new do |form|
form.field :email
form.field :name
form.collection :addresses do |address|
address.field(:street)
address.field(:state)
address.field(:zip)
end
end “‘ The object within the block is a `Namespace` object that maps each object within the enumerable to another `Namespace` or `Field`.
142 143 144 |
# File 'lib/superform.rb', line 142 def collection(key, &) create_child(key, NamespaceCollection, &) end |
#each ⇒ Object
Iterates through the children of the current namespace, which could be ‘Namespace` or `Field` objects.
158 159 160 |
# File 'lib/superform.rb', line 158 def each(&) @children.values.each(&) end |
#field(key) ⇒ Object
Maps the ‘Object#proprety` and `Object#property=` to a field in a web form that can be read and set by the form. For example, a User form might look like this:
“‘ruby Superform :user, object: User.new do |form|
form.field :email
form.field :name
end “‘
120 121 122 123 124 |
# File 'lib/superform.rb', line 120 def field(key) create_child(key, @field_class, object: object).tap do |field| yield field if block_given? end end |
#namespace(key, &block) ⇒ Object
Creates a ‘Namespace` child instance with the parent set to the current instance, adds to the `@children` Hash to ensure duplicate child namespaces aren’t created, then calls the method on the ‘@object` to get the child object to pass into that namespace.
For example, if a ‘User#permission` returns a `Permission` object, we could map that to a form like this:
“‘ruby Superform :user, object: User.new do |form|
form.namespace :permission do ||
form.field :role
end
end “‘
107 108 109 |
# File 'lib/superform.rb', line 107 def namespace(key, &block) create_child(key, self.class, object: object_for(key: key), &block) end |
#serialize ⇒ Object
Creates a Hash of Hashes and Arrays that represent the fields and collections of the Superform. This can be used to safely update ActiveRecord objects without the need for Strong Parameters. You will want to make sure that all the fields displayed in the form are ones that you’re OK updating from the generated hash.
150 151 152 153 154 |
# File 'lib/superform.rb', line 150 def serialize each_with_object Hash.new do |child, hash| hash[child.key] = child.serialize end end |