Class: Shaf::Formable::Form

Inherits:
Object
  • Object
show all
Extended by:
ImmutableAttr
Defined in:
lib/shaf/formable/form.rb

Defined Under Namespace

Classes: FormHasNoResourceError

Constant Summary collapse

DEFAULT_TYPE =
'application/json'.freeze
DEFAULT_SUBMIT =
'save'.freeze

Constants included from ImmutableAttr

ImmutableAttr::NON_DUPABLE_CLASSES

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ImmutableAttr

dup, immutable_accessor, immutable_reader, immutable_writer

Constructor Details

#initialize(params = {}) ⇒ Form

Returns a new instance of Form.



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/shaf/formable/form.rb', line 19

def initialize(params = {})
  @title = params[:title]
  @action = params[:action]
  @name = params[:name]
  @method = params[:method]
  @type = params[:type] || DEFAULT_TYPE
  @submit = params[:submit] || DEFAULT_SUBMIT
  @fields = (params[:fields] || {}).map do |name, args|
    Field.new(name, args)
  end
end

Instance Attribute Details

#actionObject



48
49
50
# File 'lib/shaf/formable/form.rb', line 48

def action
  @action&.to_sym
end

#nameObject



31
32
33
# File 'lib/shaf/formable/form.rb', line 31

def name
  (@name || name_from(action))&.to_sym
end

#resourceObject

Returns the value of attribute resource.



14
15
16
# File 'lib/shaf/formable/form.rb', line 14

def resource
  @resource
end

Instance Method Details

#[](name) ⇒ Object



79
80
81
# File 'lib/shaf/formable/form.rb', line 79

def [](name)
  fields.find { |field| field.name == name }
end

#add_field(name, opts) ⇒ Object



52
53
54
# File 'lib/shaf/formable/form.rb', line 52

def add_field(name, opts)
  @fields << Field.new(name, opts)
end

#cloneObject



62
63
64
# File 'lib/shaf/formable/form.rb', line 62

def clone
  dup.tap { |obj| obj.freeze if frozen? }
end

#dupObject



56
57
58
59
60
# File 'lib/shaf/formable/form.rb', line 56

def dup
  super.tap do |obj|
    obj.instance_variable_set(:@fields, @fields.map(&:dup))
  end
end

#fields=(fields) ⇒ Object



44
45
46
# File 'lib/shaf/formable/form.rb', line 44

def fields=(fields)
  @fields = fields.map { |name, args| Field.new(name, args) }
end

#fill!(from: nil) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/shaf/formable/form.rb', line 66

def fill!(from: nil)
  resrc = from || resource
  raise FormHasNoResourceError, <<~MSG unless resrc
    Trying to fill form with values from resource, but form '#{name}' has no resource!
  MSG

  fields.each do |field|
    accessor_name = field.accessor_name
    next unless resrc.respond_to? accessor_name
    field.value = resrc.send(accessor_name)
  end
end

#methodObject



39
40
41
42
# File 'lib/shaf/formable/form.rb', line 39

def method
  _method = @method || http_method_from(action)
  _method.to_s.upcase if _method
end

#method=(http_method) ⇒ Object



35
36
37
# File 'lib/shaf/formable/form.rb', line 35

def method=(http_method)
  @method = http_method.to_s.upcase
end

#to_htmlObject



83
84
85
86
87
88
89
90
91
# File 'lib/shaf/formable/form.rb', line 83

def to_html
  form_element do
    [
      hidden_method_element,
      fields.map(&:to_html).join("\n"),
      submit_element
    ].compact.join("\n")
  end
end