Class: RailsAdminNext::FormBuilder
- Inherits:
-
ActionView::Helpers::FormBuilder
- Object
- ActionView::Helpers::FormBuilder
- RailsAdminNext::FormBuilder
show all
- Includes:
- ApplicationHelper
- Defined in:
- app/helpers/rails_admin_next/form_builder.rb
Instance Method Summary
collapse
#action, #actions, #authorized?, #breadcrumb, #bulk_menu, #current_action, #current_action?, #edit_user_link, #flash_alert_class, #image_tag, #logout_method, #logout_path, #main_navigation, #menu_for, #navigation, #rails_admin_icon, #root_navigation, #static_navigation, #wording_for
Instance Method Details
#dom_id(field) ⇒ Object
113
114
115
116
117
118
119
120
|
# File 'app/helpers/rails_admin_next/form_builder.rb', line 113
def dom_id(field)
(@dom_id ||= {})[field.name] ||=
[
@object_name.to_s.gsub(/\]\[|[^-a-zA-Z0-9:.]/, "_").sub(/_$/, ""),
options[:index],
field.method_name
].reject(&:blank?).join("_")
end
|
#dom_name(field) ⇒ Object
122
123
124
|
# File 'app/helpers/rails_admin_next/form_builder.rb', line 122
def dom_name(field)
(@dom_name ||= {})[field.name] ||= %(#{@object_name}#{options[:index] && "[#{options[:index]}]"}[#{field.method_name}]#{"[]" if field.is_a?(Config::Fields::Association) && field.multiple?})
end
|
#errors_for(field) ⇒ Object
67
68
69
|
# File 'app/helpers/rails_admin_next/form_builder.rb', line 67
def errors_for(field)
field.errors.present? ? @template.content_tag(:span, field.errors.to_sentence, class: "help-inline text-danger") : "".html_safe
end
|
#field_for(field) ⇒ Object
75
76
77
|
# File 'app/helpers/rails_admin_next/form_builder.rb', line 75
def field_for(field)
field.read_only? ? @template.content_tag(:div, field.pretty_value, class: "form-control-static") : field.render
end
|
#field_wrapper_for(field, nested_in) ⇒ Object
43
44
45
46
47
48
49
50
51
52
53
54
55
|
# File 'app/helpers/rails_admin_next/form_builder.rb', line 43
def field_wrapper_for(field, nested_in)
return if nested_field_association?(field, nested_in)
@template.content_tag(:div, class: "control-group row mb-3 #{field.type_css_class} #{field.css_class} #{"error" if field.errors.present?}", id: "#{dom_id(field)}_field") do
if field.label
label(field.method_name, field.label, class: "col-sm-2 col-form-label text-md-end") +
(field.nested_form ? field_for(field) : input_for(field))
else
field.nested_form ? field_for(field) : input_for(field)
end
end
end
|
#fieldset_for(fieldset, nested_in) ⇒ Object
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
# File 'app/helpers/rails_admin_next/form_builder.rb', line 23
def fieldset_for(fieldset, nested_in)
fields = fieldset.with(
form: self,
object: @object,
view: @template,
controller: @template.controller
).visible_fields
return if fields.empty?
@template.content_tag :fieldset do
contents = []
chevron = RailsAdminNext::Icons.svg(:collapse, class: ("collapsed" unless fieldset.active?)).html_safe
legend = @template.safe_join([chevron, " #{fieldset.label}"])
contents << @template.content_tag(:legend, legend, style: (fieldset.name == :default) ? "display:none" : "")
contents << @template.content_tag(:p, fieldset.help) if fieldset.help.present?
contents << fields.collect { |field| field_wrapper_for(field, nested_in) }.join
contents.join.html_safe
end
end
|
#generate(options = {}) ⇒ Object
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
# File 'app/helpers/rails_admin_next/form_builder.rb', line 7
def generate(options = {})
without_field_error_proc_added_div do
options.reverse_merge!(
action: @template.controller.params[:action],
model_config: @template.instance_variable_get(:@model_config),
nested_in: false
)
object_infos +
visible_groups(options[:model_config], generator_action(options[:action], options[:nested_in])).collect do |fieldset|
fieldset_for fieldset, options[:nested_in]
end.join.html_safe +
(options[:nested_in] ? "" : @template.render(partial: "rails_admin_next/main/submit_buttons"))
end
end
|
#help_for(field) ⇒ Object
71
72
73
|
# File 'app/helpers/rails_admin_next/form_builder.rb', line 71
def help_for(field)
field.help.present? ? @template.content_tag(:div, field.help, class: "form-text") : "".html_safe
end
|
#hidden_field(method, options = {}) ⇒ Object
126
127
128
129
130
131
132
|
# File 'app/helpers/rails_admin_next/form_builder.rb', line 126
def hidden_field(method, options = {})
if method == :id && object.id.is_a?(Array)
super(method, {value: RailsAdminNext.config.composite_keys_serializer.serialize(object.id)})
else
super
end
end
|
57
58
59
60
61
62
63
64
65
|
# File 'app/helpers/rails_admin_next/form_builder.rb', line 57
def input_for(field)
css = "col-sm-10 controls"
css += " has-error" if field.errors.present?
@template.content_tag(:div, class: css) do
field_for(field) +
errors_for(field) +
help_for(field)
end
end
|
#nested_field_set(nested_form, field, allow_destroy:) ⇒ Object
Wraps one nested-attributes subform (rendered by native fields_for) for
the nested-form Stimulus controller: a .fields container, an optional
_destroy hidden field + remove button (gated on allow_destroy, always
present in the blank ), then the recursively generated fields.
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
# File 'app/helpers/rails_admin_next/form_builder.rb', line 83
def nested_field_set(nested_form, field, allow_destroy:)
@template.content_tag(:div, class: "fields tab-pane active", data: {"nested-form-target": "subform"}) do
contents = []
contents << nested_form.hidden_field(:id) if nested_form.object.persisted?
if allow_destroy
contents << nested_form.hidden_field(:_destroy, data: {"nested-form-target": "destroy"})
contents << @template.content_tag(:button, RailsAdminNext::Icons.svg(:trash).html_safe, type: "button", class: "btn btn-danger remove_nested_fields", data: {action: "nested-form#remove"})
end
contents << nested_form.generate(action: :nested, model_config: field.associated_model_config, nested_in: field)
contents.join.html_safe
end
end
|
#object_infos ⇒ Object
100
101
102
103
104
105
106
107
108
109
110
111
|
# File 'app/helpers/rails_admin_next/form_builder.rb', line 100
def object_infos
model_config = RailsAdminNext.config(object)
model_label = model_config.label
object_label =
if object.new_record?
I18n.t("admin.form.new_model", name: model_label)
else
object.send(model_config.object_label_method).presence || "#{model_config.label} ##{object.id}"
end
%(<span hidden class="object-infos" data-model-label="#{CGI.escapeHTML(model_label.to_s)}" data-object-label="#{CGI.escapeHTML(object_label.to_s)}"></span>).html_safe
end
|