Class: Alchemy::Forms::Builder

Inherits:
SimpleForm::FormBuilder
  • Object
show all
Defined in:
lib/alchemy/forms/builder.rb

Instance Method Summary collapse

Instance Method Details

#datepicker(attribute_name, options = {}) ⇒ Object

Renders a simple_form input that displays a datepicker



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/alchemy/forms/builder.rb', line 24

def datepicker(attribute_name, options = {})
  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,
    value: date&.iso8601
  }.merge(options[:input_html] || {})

  date_field = input attribute_name, as: :string, input_html: input_options
  template.("alchemy-datepicker", date_field, type: type)
end

#input(attribute_name, options = {}, &block) ⇒ Object

Renders a simple_form input, but uses input alchemy_wrapper



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/alchemy/forms/builder.rb', line 8

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(
      is: "alchemy-tooltip",
      "data-tooltip": Alchemy.t(:attribute_fixed, attribute: attribute_name)
    )
  end

  super
end

#submit(label, options = {}) ⇒ Object

Renders a button tag wrapped in a div with ‘submit’ class.



42
43
44
45
46
47
48
49
# File 'lib/alchemy/forms/builder.rb', line 42

def submit(label, options = {})
  options = {
    wrapper_html: {class: "submit"}
  }.update(options)
  template.("div", options.delete(:wrapper_html)) do
    template.("button", label, options.delete(:input_html))
  end
end