Module: Phlexible::Rails::ButtonToConcerns

Extended by:
ActiveSupport::Concern
Included in:
ButtonTo
Defined in:
lib/phlexible/rails/button_to_concerns.rb

Constant Summary collapse

BUTTON_TAG_METHOD_VERBS =
%w[patch put delete].freeze
DEFAULT_OPTIONS =
{ method: 'post', form_class: 'button_to', params: {} }.freeze

Instance Method Summary collapse

Instance Method Details

#initialize(url, options = nil) ⇒ Object



31
32
33
34
# File 'lib/phlexible/rails/button_to_concerns.rb', line 31

def initialize(url, options = nil)
  @url = url
  @options = options
end

#view_template(&block) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/phlexible/rails/button_to_concerns.rb', line 36

def view_template(&block)
  action = url_for(@url)
  @options = DEFAULT_OPTIONS.merge((@options || {}).symbolize_keys)

  method = (@options.delete(:method).presence || method_for_options(@options)).to_s
  form_method = method == 'get' ? 'get' : 'post'

  form action: action, method: form_method, **form_attributes do
    method_tag method
    form_method == 'post' && token_input(action, method.empty? ? 'post' : method)
    param_inputs

    block_given? ? button(**button_attrs, &block) : button(**button_attrs) { @name }
  end
end