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



7
8
9
10
11
# File 'app/helpers/pages_core/labelled_form_builder.rb', line 7

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



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

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



20
21
22
23
24
# File 'app/helpers/pages_core/labelled_form_builder.rb', line 20

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



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

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



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

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



42
43
44
45
46
# File 'app/helpers/pages_core/labelled_form_builder.rb', line 42

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



48
49
50
51
52
# File 'app/helpers/pages_core/labelled_form_builder.rb', line 48

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



54
55
56
57
58
# File 'app/helpers/pages_core/labelled_form_builder.rb', line 54

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



60
61
62
63
64
# File 'app/helpers/pages_core/labelled_form_builder.rb', line 60

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



66
67
68
69
70
# File 'app/helpers/pages_core/labelled_form_builder.rb', line 66

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



72
73
74
75
76
# File 'app/helpers/pages_core/labelled_form_builder.rb', line 72

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



78
79
80
81
82
# File 'app/helpers/pages_core/labelled_form_builder.rb', line 78

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



84
85
86
87
88
# File 'app/helpers/pages_core/labelled_form_builder.rb', line 84

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



90
91
92
93
94
# File 'app/helpers/pages_core/labelled_form_builder.rb', line 90

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