Sha256: ddd5cbd4fbfbcae0f41501f3100edf966636e32391222fabcc3f70dd3402ff55

Contents?: true

Size: 1.64 KB

Versions: 6

Compression:

Stored size: 1.64 KB

Contents

require "view_component/form"

module Plutonium
  module Helpers
    module FormHelper
      include ActionView::Helpers::FormHelper

      def resource_form_for(record, **options, &block)
        options[:url] ||= resource_url_for(record, action: record.new_record? ? :create : :update)
        options = build_form_options(options)

        with_resource_form_field_error_proc do
          form_for(record, options, &block)
        end
      end

      def resource_form_with(**options, &block)
        options = build_form_options(options)
        with_resource_form_field_error_proc do
          form_with(**options, &block)
        end
      end

      def token_tag(...)
        # needed to workaround https://github.com/tailwindlabs/tailwindcss/issues/3350
        super(...).sub(" />", " hidden />").html_safe
      end

      private

      def build_form_options(options)
        options[:builder] ||= PlutoniumUi::FormBuilder
        options[:wrapper] ||= :default_resource_form
        options[:html] ||= {}
        options[:html][:novalidate] = false unless options[:html].key?(:novalidate)
        options
      end

      def with_resource_form_field_error_proc
        # borrowed from https://github.com/heartcombo/simple_form/blob/main/lib/simple_form/action_view_extensions/form_helper.rb#L40C1-L50C10
        # this does not look threadsafe
        default_field_error_proc = ::ActionView::Base.field_error_proc
        begin
          ::ActionView::Base.field_error_proc = proc { |html_tag, instance| html_tag }
          yield
        ensure
          ::ActionView::Base.field_error_proc = default_field_error_proc
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
plutonium-0.14.1 lib/plutonium/helpers/form_helper.rb
plutonium-0.14.0 lib/plutonium/helpers/form_helper.rb
plutonium-0.13.3 lib/plutonium/helpers/form_helper.rb
plutonium-0.13.2 lib/plutonium/helpers/form_helper.rb
plutonium-0.13.1 lib/plutonium/helpers/form_helper.rb
plutonium-0.13.0 lib/plutonium/helpers/form_helper.rb