Module: LabelledForm

Defined in:
lib/labelled_form.rb,
lib/labelled_form/version.rb

Defined Under Namespace

Classes: Rails

Constant Summary collapse

VERSION =
"0.3.4"

Instance Method Summary collapse

Instance Method Details

#check_box(method, options = {}, checked_value = "1", unchecked_value = "0") ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/labelled_form.rb', line 123

def check_box(method, options = {}, checked_value = "1", unchecked_value = "0")
  options = options.dup
  label_text = options.delete(:label)
  label_class = options.delete(:label_class)
  super.tap do |out|
    if label_text
      label_text = checked_value == "1" ? nil : checked_value if label_text === true
      out << " ".html_safe
      label_options = options[:multiple] ? { value: checked_value } : {}
      out << label(method, label_text, label_options.merge(class: label_class))
    end
  end
end

#collection_select(method, collection, value_method, text_method, options = {}, html_options = {}) ⇒ Object



91
92
93
94
95
96
# File 'lib/labelled_form.rb', line 91

def collection_select(method, collection, value_method, text_method, options = {}, html_options = {})
  label_options = (options.keys & %i[wrap_in_dfn label_class label_with_colon label]).any? ? options : html_options
  with_label(method, label_options) do
    super
  end
end

#date_field(method, options = {}) ⇒ Object



42
43
44
45
46
# File 'lib/labelled_form.rb', line 42

def date_field method, options = {}
  with_label(method, options) do
    super
  end
end

#email_field(method, options = {}) ⇒ Object



54
55
56
57
58
# File 'lib/labelled_form.rb', line 54

def email_field method, options = {}
  with_label(method, options) do
    super
  end
end

#file_field(method, options = {}) ⇒ Object



78
79
80
81
82
# File 'lib/labelled_form.rb', line 78

def file_field method, options = {}
  with_label(method, options) do
    super
  end
end

#multiple_check_box(method, value, options = {}) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/labelled_form.rb', line 137

def multiple_check_box method, value, options={}
  options = options.dup
  label_text = options.delete(:label)
  label_class = options.delete(:label_class)
  out = "".html_safe

  if !multiple_check_box_fields.include?(method)
    multiple_check_box_fields << method
    out << hidden_field(method, value: "", multiple: true)
  end

  out << check_box(method, { multiple: true }.reverse_merge(options), value, nil)

  if label_text
    label_text = value == "1" ? nil : value if label_text === true
    out << " ".html_safe
    out << label(method, label_text, value: value, class: label_class)
  end

  out
end

#number_field(method, options = {}) ⇒ Object



98
99
100
101
102
# File 'lib/labelled_form.rb', line 98

def number_field method, options = {}
  with_label(method, options) do
    super
  end
end

#password_field(method, options = {}) ⇒ Object



66
67
68
69
70
# File 'lib/labelled_form.rb', line 66

def password_field method, options = {}
  with_label(method, options) do
    super
  end
end

#phone_field(method, options = {}) ⇒ Object



48
49
50
51
52
# File 'lib/labelled_form.rb', line 48

def phone_field method, options = {}
  with_label(method, options) do
    super
  end
end

#radio_button(method, tag_value, options = {}) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/labelled_form.rb', line 110

def radio_button(method, tag_value, options = {})
  options = options.dup
  label_text = options.delete(:label)
  label_class = options.delete(:label_class)
  super.tap do |out|
    if label_text
      label_text = tag_value if label_text === true
      out << " ".html_safe
      out << label(method, label_text, value: tag_value, class: label_class)
    end
  end
end

#range_field(method, options = {}) ⇒ Object



104
105
106
107
108
# File 'lib/labelled_form.rb', line 104

def range_field method, options = {}
  with_label(method, options) do
    super
  end
end

#select(method, choices = nil, options = {}, html_options = {}, &block) ⇒ Object



84
85
86
87
88
89
# File 'lib/labelled_form.rb', line 84

def select(method, choices = nil, options = {}, html_options = {}, &block)
  label_options = (options.keys & %i[wrap_in_dfn label_class label_with_colon label]).any? ? options : html_options
  with_label(method, label_options) do
    super
  end
end

#text_area(method, options = {}) ⇒ Object



36
37
38
39
40
# File 'lib/labelled_form.rb', line 36

def text_area method, options = {}
  with_label(method, options) do
    super
  end
end

#text_field(method, options = {}) ⇒ Object



72
73
74
75
76
# File 'lib/labelled_form.rb', line 72

def text_field method, options = {}
  with_label(method, options) do
    super
  end
end

#url_field(method, options = {}) ⇒ Object



60
61
62
63
64
# File 'lib/labelled_form.rb', line 60

def url_field method, options = {}
  with_label(method, options) do
    super
  end
end

#with_label(method, options) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/labelled_form.rb', line 10

def with_label method, options
  wrap_in_dfn = options.delete(:wrap_in_dfn)
  label_class = options.delete(:label_class)
  label_with_colon = options.delete(:label_with_colon)
  label_text = options.delete(:label) || label_class.present? || label_with_colon

  label_text = method.to_s.humanize if label_text === true
  label_text&.sub!(/_id$/,"")
  label_text << ":" if label_with_colon && !label_text.ends_with?(":")

  field_html = yield

  if label_text
    if wrap_in_dfn
      @template.tag.dfn do
        @template.tag.dt(label(method, label_text, class: label_class)) +
          @template.tag.dd(field_html)
      end
    else
      label(method, label_text, class: label_class) + " ".html_safe + field_html
    end
  else
    field_html
  end
end