Class: Dsfr::FormBuilder

Inherits:
ActionView::Helpers::FormBuilder
  • Object
show all
Includes:
ActionView::Helpers::OutputSafetyHelper
Defined in:
lib/dsfr-form_builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object_name, object, template, options) ⇒ FormBuilder

Returns a new instance of FormBuilder.



10
11
12
13
# File 'lib/dsfr-form_builder.rb', line 10

def initialize(object_name, object, template, options)
  super
  self.display_required_tags = options.fetch(:display_required_tags, true)
end

Instance Attribute Details

#display_required_tagsObject

Returns the value of attribute display_required_tags.



8
9
10
# File 'lib/dsfr-form_builder.rb', line 8

def display_required_tags
  @display_required_tags
end

Instance Method Details

#dsfr_email_field(attribute, opts = {}) ⇒ Object



19
20
21
# File 'lib/dsfr-form_builder.rb', line 19

def dsfr_email_field(attribute, opts = {})
  dsfr_input_field(attribute, :email_field, opts)
end

#dsfr_error_message(attr) ⇒ Object



64
65
66
67
68
69
70
71
72
# File 'lib/dsfr-form_builder.rb', line 64

def dsfr_error_message(attr)
  return if @object.errors[attr].none?

  @template.(:p, class: "fr-messages-group") do
    safe_join(@object.errors.full_messages_for(attr).map do |msg|
      @template.(:span, msg, class: "fr-message fr-message--error")
    end)
  end
end

#dsfr_input_field(attribute, input_kind, opts = {}) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/dsfr-form_builder.rb', line 37

def dsfr_input_field(attribute, input_kind, opts = {})
  dsfr_input_group(attribute, opts) do
    @template.safe_join(
      [
        dsfr_label_with_hint(attribute, opts),
        public_send(input_kind, attribute, class: "fr-input", **opts.except(:class)),
        dsfr_error_message(attribute),
      ]
    )
  end
end

#dsfr_input_group(attribute, opts, &block) ⇒ Object



31
32
33
34
35
# File 'lib/dsfr-form_builder.rb', line 31

def dsfr_input_group(attribute, opts, &block)
  @template.(:div, class: input_group_classes(attribute, opts), data: opts[:data]) do
    yield(block)
  end
end

#dsfr_label_with_hint(attribute, opts = {}) ⇒ Object



49
50
51
52
53
54
55
56
57
58
# File 'lib/dsfr-form_builder.rb', line 49

def dsfr_label_with_hint(attribute, opts = {})
  label_class = "fr-label #{opts[:class]}"
  label(attribute, class: label_class) do
    label_and_tags = [label_value(attribute, opts)]
    label_and_tags.push(required_tag) if opts[:required] && display_required_tags
    label_and_tags.push(hint_tag(opts[:hint])) if opts[:hint]

    @template.safe_join(label_and_tags)
  end
end

#dsfr_phone_field(attribute, opts = {}) ⇒ Object



27
28
29
# File 'lib/dsfr-form_builder.rb', line 27

def dsfr_phone_field(attribute, opts = {})
  dsfr_input_field(attribute, :phone_field, opts)
end

#dsfr_text_field(attribute, opts = {}) ⇒ Object



15
16
17
# File 'lib/dsfr-form_builder.rb', line 15

def dsfr_text_field(attribute, opts = {})
  dsfr_input_field(attribute, :text_field, opts)
end

#dsfr_url_field(attribute, opts = {}) ⇒ Object



23
24
25
# File 'lib/dsfr-form_builder.rb', line 23

def dsfr_url_field(attribute, opts = {})
  dsfr_input_field(attribute, :url_field, opts)
end

#hint_tag(text) ⇒ Object



74
75
76
77
78
79
80
81
82
# File 'lib/dsfr-form_builder.rb', line 74

def hint_tag(text)
  return "" unless text

  @template.(:span, class: "fr-hint-text") do
    text
  end

  @template.(:span, text, class: "fr-hint-text")
end

#input_group_classes(attribute, opts) ⇒ Object



88
89
90
91
92
93
94
95
96
# File 'lib/dsfr-form_builder.rb', line 88

def input_group_classes(attribute, opts)
  join_classes(
    [
      "fr-input-group",
      @object.errors[attribute].any? ? "fr-input-group--error" : nil,
      opts[:class],
    ]
  )
end

#join_classes(arr) ⇒ Object



84
85
86
# File 'lib/dsfr-form_builder.rb', line 84

def join_classes(arr)
  arr.compact.join(" ")
end

#label_value(attribute, opts) ⇒ Object



98
99
100
101
102
# File 'lib/dsfr-form_builder.rb', line 98

def label_value(attribute, opts)
  return opts[:label] if opts[:label]

  (@object.try(:object) || @object).class.human_attribute_name(attribute)
end

#required_tagObject



60
61
62
# File 'lib/dsfr-form_builder.rb', line 60

def required_tag
  @template.(:span, "*", class: "fr-text-error")
end