Class: Superform::Field

Inherits:
Node
  • Object
show all
Defined in:
lib/superform/field.rb

Overview

A Field represents the data associated with a form element. This class provides methods for accessing and modifying the field’s value. HTML concerns are all delegated to the DOM object.

Direct Known Subclasses

Rails::Field

Defined Under Namespace

Classes: Kit

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, value: nil) {|_self| ... } ⇒ Field

Returns a new instance of Field.

Yields:

  • (_self)

Yield Parameters:



8
9
10
11
12
13
14
# File 'lib/superform/field.rb', line 8

def initialize(key, parent:, object: nil, value: nil)
  super key, parent: parent
  @object = object
  @value = value
  @dom = Superform::DOM.new(field: self)
  yield self if block_given?
end

Instance Attribute Details

#domObject (readonly)

Returns the value of attribute dom.



6
7
8
# File 'lib/superform/field.rb', line 6

def dom
  @dom
end

Class Method Details

.inherited(subclass) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/superform/field.rb', line 83

def self.inherited(subclass)
  super
  # Create a new Kit class for each Field subclass with true isolation
  # Copy methods from parent Field classes at creation time, not through inheritance
  subclass.const_set(:Kit, Class.new(Field::Kit))
  
  # Copy all existing methods from the inheritance chain
  field_class = self
  while field_class != Field
    copy_field_methods_to_kit(field_class, subclass::Kit)
    field_class = field_class.superclass
  end
end

.method_added(method_name) ⇒ Object



97
98
99
100
101
102
103
104
105
# File 'lib/superform/field.rb', line 97

def self.method_added(method_name)
  super
  # Skip if this is the base Field class or if we don't have a Kit class yet
  return if self == Field
  return unless const_defined?(:Kit, false)
  
  # Only add method to THIS class's Kit, not subclasses (isolation)
  add_method_to_kit(method_name, self::Kit)
end

Instance Method Details

#assign(value) ⇒ Object Also known as: value=



25
26
27
28
29
30
31
# File 'lib/superform/field.rb', line 25

def assign(value)
  if @object and @object.respond_to? "#{@key}="
    @object.send "#{@key}=", value
  else
    @value = value
  end
end

#collectionObject

Wraps a field that’s an array of values with a bunch of fields that are indexed with the array’s index.



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

def collection(&)
  @collection ||= FieldCollection.new(field: self, &)
end

#fieldObject

Make the name more obvious for extending or writing docs.



41
42
43
# File 'lib/superform/field.rb', line 41

def field
  self
end

#kit(form) ⇒ Object



107
108
109
# File 'lib/superform/field.rb', line 107

def kit(form)
  self.class::Kit.new(field: self, form: form)
end

#valueObject Also known as: serialize



16
17
18
19
20
21
22
# File 'lib/superform/field.rb', line 16

def value
  if @object and @object.respond_to? @key
    @object.send @key
  else
    @value
  end
end