-
Notifications
You must be signed in to change notification settings - Fork 196
/
monologue_admin_form_builder.rb
65 lines (57 loc) · 2.21 KB
/
monologue_admin_form_builder.rb
1
2
3
4
5
6
7
8
9
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
class MonologueAdminFormBuilder < ActionView::Helpers::FormBuilder
delegate :content_tag, :tag, to: :@template
%w[text_field text_area password_field collection_select select file_field].each do |method_name|
define_method(method_name) do |name, *args|
content_tag :div, class: "field" do
field_label(name, *args) + super(name, *args)
end
end
end
def check_box(name, *args)
options = args.extract_options!
required = object.class.validators_on(name).any? { |v| v.kind_of? ActiveModel::Validations::PresenceValidator }
content_tag :label, class: (required ? "required checkbox inline" : "checkbox inline") do
super + (options[:label] || name)
end
end
def collection_check_boxes(attribute, records, record_id, record_name)
content_tag :div, class: "field" do
@template.hidden_field_tag("#{object_name}[#{attribute}][]") +
records.map do |record|
element_id = "#{object_name}_#{attribute}_#{record.send(record_id)}"
checkbox = @template.check_box_tag("#{object_name}[#{attribute}][]", record.send(record_id), object.send(attribute).include?(record.send(record_id)), id: element_id)
checkbox + " " + @template.label_tag(element_id, record.send(record_name))
end.join(tag(:br)).html_safe
end
end
def submit(*args)
content_tag :div, class: "form-actions" do
super
end
end
def error_messages
if object.errors.full_messages.any?
content_tag(:div, class: "alert alert-error error_messages") do
content_tag(:strong, "Invalid Fields") +
content_tag(:ul) do
object.errors.messages.map do |msg|
content_tag(:li, msg[1][0])
end.join.html_safe
end
end
end
end
private
def field_label(name, *args)
options = args.extract_options!
required = object.class.validators_on(name).any? { |v| v.kind_of? ActiveModel::Validations::PresenceValidator }
if options[:id]
label(name, options[:label], class: ("required" if required), for: options[:id])
else
label(name, options[:label], class: ("required" if required))
end
end
def objectify_options(options)
super.except(:label)
end
end