Module: PagesCore::LabelledFormBuilder

Included in:
FormBuilder
Defined in:
app/helpers/pages_core/labelled_form_builder.rb

Instance Method Summary collapse

Instance Method Details

#field_with_label(attr, str, label = nil, class_name = nil) ⇒ Object



5
6
7
8
9
# File 'app/helpers/pages_core/labelled_form_builder.rb', line 5

def field_with_label(attr, str, label = nil, class_name = nil)
  classes = ["field", class_name]
  classes << "field-with-errors" if object.errors[attr].any?
  tag.div(label_for(attr, label) + str, class: classes.compact.join(" "))
end

#label_and_errors(attribute, label_text) ⇒ Object



11
12
13
14
15
16
# File 'app/helpers/pages_core/labelled_form_builder.rb', line 11

def label_and_errors(attribute, label_text)
  return label_text unless object.errors[attribute].any?

  error = tag.span(object.errors[attribute].first, class: "error")
  safe_join([label_text, error], " ")
end

#label_for(attribute, label_text = nil) ⇒ Object



18
19
20
21
22
# File 'app/helpers/pages_core/labelled_form_builder.rb', line 18

def label_for(attribute, label_text = nil)
  label_text ||= object.class.human_attribute_name(attribute)
  tag.label(label_and_errors(attribute, label_text),
            for: [object.class.to_s.underscore, attribute].join("_"))
end

#labelled_check_box(attr, label = nil, options = {}, checked = "1", unchecked = "0") ⇒ Object



24
25
26
27
28
29
30
# File 'app/helpers/pages_core/labelled_form_builder.rb', line 24

def labelled_check_box(
  attr, label = nil, options = {}, checked = "1", unchecked = "0"
)
  labelled_field(attr, label, options) do |opts|
    check_box(attr, opts, checked, unchecked)
  end
end

#labelled_country_select(attr, label = nil, opts = {}, html_opts = {}) ⇒ Object



32
33
34
35
36
37
38
# File 'app/helpers/pages_core/labelled_form_builder.rb', line 32

def labelled_country_select(
  attr, label = nil, opts = {}, html_opts = {}
)
  labelled_field(attr, label, opts) do |options|
    country_select(attr, options, html_opts)
  end
end

#labelled_date_select(attribute, label_text = nil, options = {}) ⇒ Object



40
41
42
43
44
# File 'app/helpers/pages_core/labelled_form_builder.rb', line 40

def labelled_date_select(attribute, label_text = nil, options = {})
  labelled_field(attribute, label_text, options) do |opts|
    date_select(attribute, opts)
  end
end

#labelled_datetime_select(attribute, label_text = nil, options = {}) ⇒ Object



46
47
48
49
50
# File 'app/helpers/pages_core/labelled_form_builder.rb', line 46

def labelled_datetime_select(attribute, label_text = nil, options = {})
  labelled_field(attribute, label_text, options) do |opts|
    datetime_select(attribute, opts)
  end
end

#labelled_file_field(attribute, label_text = nil, options = {}) ⇒ Object



52
53
54
55
56
# File 'app/helpers/pages_core/labelled_form_builder.rb', line 52

def labelled_file_field(attribute, label_text = nil, options = {})
  labelled_field(attribute, label_text, options) do |opts|
    file_field(attribute, opts)
  end
end

#labelled_image_file_field(attribute, label_text = nil, options = {}) ⇒ Object



58
59
60
61
62
# File 'app/helpers/pages_core/labelled_form_builder.rb', line 58

def labelled_image_file_field(attribute, label_text = nil, options = {})
  labelled_field(attribute, label_text, options) do |opts|
    image_file_field(attribute, opts)
  end
end

#labelled_password_field(attribute, label_text = nil, options = {}) ⇒ Object



64
65
66
67
68
# File 'app/helpers/pages_core/labelled_form_builder.rb', line 64

def labelled_password_field(attribute, label_text = nil, options = {})
  labelled_field(attribute, label_text, options, "text-field") do |opts|
    password_field(attribute, opts)
  end
end

#labelled_select(attribute, choices, label_text = nil, options = {}) ⇒ Object



70
71
72
73
74
# File 'app/helpers/pages_core/labelled_form_builder.rb', line 70

def labelled_select(attribute, choices, label_text = nil, options = {})
  labelled_field(attribute, label_text, options) do |opts|
    select(attribute, choices, opts)
  end
end

#labelled_text_area(attribute, label_text = nil, options = {}) ⇒ Object



76
77
78
79
80
# File 'app/helpers/pages_core/labelled_form_builder.rb', line 76

def labelled_text_area(attribute, label_text = nil, options = {})
  labelled_field(attribute, label_text, options, "text-area") do |opts|
    text_area(attribute, opts)
  end
end

#labelled_text_field(attribute, label_text = nil, options = {}) ⇒ Object



82
83
84
85
86
# File 'app/helpers/pages_core/labelled_form_builder.rb', line 82

def labelled_text_field(attribute, label_text = nil, options = {})
  labelled_field(attribute, label_text, options, "text-field") do |opts|
    text_field(attribute, opts)
  end
end

#labelled_time_select(attribute, label_text = nil, options = {}) ⇒ Object



88
89
90
91
92
# File 'app/helpers/pages_core/labelled_form_builder.rb', line 88

def labelled_time_select(attribute, label_text = nil, options = {})
  labelled_field(attribute, label_text, options) do |opts|
    time_select(attribute, opts)
  end
end