Sha256: 8f1705e3bb441b78e3af835855264a29f8f6a43c4458d97a55d499d71e1c1c53

Contents?: true

Size: 870 Bytes

Versions: 1

Compression:

Stored size: 870 Bytes

Contents

# frozen_string_literal: true

module SmartButtons::PatchButton
  def patch_button(**options)
    simple_form_for options[:record], url: options[:url], method: :patch, html: options[:form_options] do |f|
      options[:attributes].each do |attr|
        concat build_input attr, options, f
      end
      concat(button_tag(type: :submit, **options[:button_options]) do
        yield if block_given?
      end)
    end
  end

  private

  def build_url(options)
    {
      controller: options[:controller],
      action: options[:action],
      id: options[:record].id
    }.merge(options[:parameters])
  end

  def build_input(attr, options, form)
    form.input(
      attr[0],
      input_html: {
        value: attr[1],
        id: "#{options[:model_name]}_#{attr[0]}",
        name: "#{options[:model_name]}[#{attr[0]}]"
      },
      as: :hidden
    )
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
smart_buttons-1.0.2 lib/smart_buttons/patch_button.rb