Class: Arbre::Form::Base

Inherits:
HTML::Tag
  • Object
show all
Defined in:
lib/arbre/form/base.rb

Direct Known Subclasses

Component

Constant Summary collapse

InvalidMethodError =
Class.new(ArgumentError)
ALLOWED_METHODS =
%i(get post patch put delete)
FORM_METHODS =
%i(get post)

Instance Method Summary collapse

Instance Method Details

#build(content = nil, *args, action: '/', method: :post, multipart: false, enforce_utf8: true, remote: false, authenticity_token: default_auth_token(action, method), token_name: default_token_name, **attributes) ⇒ Object

Raises:



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/arbre/form/base.rb', line 9

def build(
  content = nil,
  *args,
  action: '/',
  method: :post,
  multipart: false,
  enforce_utf8: true,
  remote: false,
  authenticity_token: default_auth_token(action, method),
  token_name: default_token_name,
  **attributes
)
  raise InvalidMethodError, "`#{method}` is not a valid HTTP method" unless ALLOWED_METHODS.include?(method)

  set_attribute :action, action
  set_attribute :enctype, 'multipart/form-data' if multipart
  form_method = FORM_METHODS.include?(method) ? method : :post
  set_attribute :method, form_method.to_s.upcase
  set_attribute :'data-remote', true if remote

  super content, *args, attributes

  within(self) do
    input(type: :hidden, name: 'utf8', value: '') if enforce_utf8
    input(type: :hidden, name: '_method', value: method.to_s.upcase) if method != form_method
    input(type: :hidden, name: token_name, value: authenticity_token) if authenticity_token
  end
end

#tag_nameObject



38
39
40
# File 'lib/arbre/form/base.rb', line 38

def tag_name
  'form'
end