Sha256: 30f7e732caf3b0d068a83ca71fef3c2757abc3311ce351cf24cbf6c0d7b42967

Contents?: true

Size: 1.62 KB

Versions: 20

Compression:

Stored size: 1.62 KB

Contents

# frozen_string_literal: true

module Alchemy
  module Forms
    class Builder < SimpleForm::FormBuilder
      # Renders a simple_form input, but uses input alchemy_wrapper
      #
      def input(attribute_name, options = {}, &block)
        options[:wrapper] = :alchemy

        if object.respond_to?(:attribute_fixed?) && object.attribute_fixed?(attribute_name)
          options[:disabled] = true
          options[:input_html] = options.fetch(:input_html, {}).merge(
            "data-alchemy-tooltip" => Alchemy.t(:attribute_fixed, attribute: attribute_name)
          )
        end

        super
      end

      # Renders a simple_form input that displays a datepicker
      #
      def datepicker(attribute_name, options = {})
        options[:wrapper] = :alchemy

        type = options[:as] || :date
        value = options.fetch(:input_html, {}).delete(:value)
        date = value || object.send(attribute_name.to_sym).presence
        date = Time.zone.parse(date) if date.is_a?(String)

        input_options = {
          type: :text,
          class: type,
          data: {datepicker_type: type},
          value: date&.iso8601
        }.merge(options[:input_html] || {})

        input attribute_name, as: :string, input_html: input_options
      end

      # Renders a button tag wrapped in a div with 'submit' class.
      #
      def submit(label, options = {})
        options = {
          wrapper_html: {class: "submit"}
        }.update(options)
        template.content_tag("div", options.delete(:wrapper_html)) do
          template.content_tag("button", label, options.delete(:input_html))
        end
      end
    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
alchemy_cms-7.0.16 lib/alchemy/forms/builder.rb
alchemy_cms-7.0.15 lib/alchemy/forms/builder.rb
alchemy_cms-7.0.14 lib/alchemy/forms/builder.rb
alchemy_cms-7.0.13 lib/alchemy/forms/builder.rb
alchemy_cms-7.0.12 lib/alchemy/forms/builder.rb
alchemy_cms-7.0.11 lib/alchemy/forms/builder.rb
alchemy_cms-7.0.10 lib/alchemy/forms/builder.rb
alchemy_cms-7.0.9 lib/alchemy/forms/builder.rb
alchemy_cms-7.0.8 lib/alchemy/forms/builder.rb
alchemy_cms-7.0.7 lib/alchemy/forms/builder.rb
alchemy_cms-7.0.6 lib/alchemy/forms/builder.rb
alchemy_cms-7.0.5 lib/alchemy/forms/builder.rb
alchemy_cms-7.0.4 lib/alchemy/forms/builder.rb
alchemy_cms-7.0.3 lib/alchemy/forms/builder.rb
alchemy_cms-7.0.2 lib/alchemy/forms/builder.rb
alchemy_cms-7.0.1 lib/alchemy/forms/builder.rb
alchemy_cms-7.0.0 lib/alchemy/forms/builder.rb
alchemy_cms-7.0.0.pre.rc1 lib/alchemy/forms/builder.rb
alchemy_cms-7.0.0.pre.c lib/alchemy/forms/builder.rb
alchemy_cms-7.0.0.pre.b lib/alchemy/forms/builder.rb