Class: Superform::Namespace

Inherits:
Node
  • Object
show all
Includes:
Enumerable
Defined in:
lib/superform/namespace.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

Attributes inherited from Node

#key, #parent

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, parent:, object: nil, form: Superform::Form.new) {|_self| ... } ⇒ Namespace

Returns a new instance of Namespace.

Yields:

  • (_self)

Yield Parameters:



14
15
16
17
18
19
20
# File 'lib/superform/namespace.rb', line 14

def initialize(key, parent:, object: nil, form: Superform::Form.new)
  super(key, parent:)
  @object = object
  @form = form
  @children = Hash.new
  yield self if block_given?
end

Instance Attribute Details

#formObject (readonly)

Returns the value of attribute form.



12
13
14
# File 'lib/superform/namespace.rb', line 12

def form
  @form
end

#objectObject (readonly)

Returns the value of attribute object.



12
13
14
# File 'lib/superform/namespace.rb', line 12

def object
  @object
end

Class Method Details

.rootObject

Creates a root Namespace, which is essentially a form.



101
102
103
# File 'lib/superform/namespace.rb', line 101

def self.root(*, **, &)
  new(*, parent: nil, **, &)
end

Instance Method Details

#assign(hash) ⇒ Object

Assigns a hash to the current namespace and children namespace.



93
94
95
96
97
98
# File 'lib/superform/namespace.rb', line 93

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:

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.



72
73
74
# File 'lib/superform/namespace.rb', line 72

def collection(key, &)
  @children[key] ||= build_collection(key, &)
end

#eachObject

Iterates through the children of the current namespace, which could be Namespace or Field objects.



88
89
90
# File 'lib/superform/namespace.rb', line 88

def each(&)
  @children.values.each(&)
end

#FieldObject



53
54
55
# File 'lib/superform/namespace.rb', line 53

def Field(...)
  field(...).kit(@form)
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:

Superform :user, object: User.new do |form|
  form.field :email
  form.field :name
end


49
50
51
# File 'lib/superform/namespace.rb', line 49

def field(key, &)
  @children[key] ||= build_field(key, &)
end

#namespace(key) ⇒ 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:

Superform :user, object: User.new do |form|
  form.namespace :permission do |permission|
    form.field :role
  end
end


36
37
38
# File 'lib/superform/namespace.rb', line 36

def namespace(key, &)
  @children[key] ||= build_namespace(key, &)
end

#serializeObject

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.



80
81
82
83
84
# File 'lib/superform/namespace.rb', line 80

def serialize
  each_with_object Hash.new do |child, hash|
    hash[child.key] = child.serialize
  end
end