Class: RailsAdmin::FormBuilder
- Inherits:
-
ActionView::Helpers::FormBuilder
- Object
- ActionView::Helpers::FormBuilder
- RailsAdmin::FormBuilder
- Includes:
- NestedForm::BuilderMixin
- Defined in:
- app/helpers/rails_admin/form_builder.rb
Instance Method Summary collapse
- #dom_id(field) ⇒ Object
- #dom_name(field) ⇒ Object
- #errors_for(field) ⇒ Object
- #field_for(field) ⇒ Object
- #field_wrapper_for(field, nested_in) ⇒ Object
- #fieldset_for(fieldset, nested_in) ⇒ Object
- #generate(options = {}) ⇒ Object
- #help_for(field) ⇒ Object
- #input_for(field) ⇒ Object
- #javascript_for(field, options = {}, &block) ⇒ Object
- #jquery_namespace(field) ⇒ Object
- #object_infos ⇒ Object
Instance Method Details
#dom_id(field) ⇒ Object
100 101 102 103 104 105 106 107 |
# File 'app/helpers/rails_admin/form_builder.rb', line 100 def dom_id field (@dom_id ||= {})[field.name] ||= [ @object_name.to_s.gsub(/\]\[|[^-a-zA-Z0-9:.]/, "_").sub(/_$/, ""), [:index], field.method_name ].reject(&:blank?).join('_') end |
#dom_name(field) ⇒ Object
109 110 111 |
# File 'app/helpers/rails_admin/form_builder.rb', line 109 def dom_name field (@dom_name ||= {})[field.name] ||= %{#{@object_name}#{[:index] && "[#{[:index]}]"}[#{field.method_name}]#{field.is_a?(Config::Fields::Association) && field.multiple? ? '[]' : ''}} end |
#errors_for(field) ⇒ Object
61 62 63 |
# File 'app/helpers/rails_admin/form_builder.rb', line 61 def errors_for field field.errors.present? ? @template.content_tag(:span, "#{field.label} #{field.errors.to_sentence}", :class => 'help-inline') : ''.html_safe end |
#field_for(field) ⇒ Object
69 70 71 72 73 74 75 |
# File 'app/helpers/rails_admin/form_builder.rb', line 69 def field_for field if field.read_only? field.pretty_value.to_s.html_safe else field.render end end |
#field_wrapper_for(field, nested_in) ⇒ Object
43 44 45 46 47 48 49 50 51 |
# File 'app/helpers/rails_admin/form_builder.rb', line 43 def field_wrapper_for field, nested_in # do not show nested field if the target is the origin unless field.inverse_of.presence && field.inverse_of == nested_in @template.content_tag(:div, :class => "control-group #{field.type_css_class} #{field.css_class} #{'error' if field.errors.present?}", :id => "#{dom_id(field)}_field") do label(field.method_name, field.label, :class => 'control-label') + (field.nested_form ? field_for(field) : input_for(field)) end end end |
#fieldset_for(fieldset, nested_in) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 |
# File 'app/helpers/rails_admin/form_builder.rb', line 31 def fieldset_for fieldset, nested_in if (fields = fieldset.with(:form => self, :object => @object, :view => @template).visible_fields).length > 0 @template.content_tag :fieldset do contents = [] contents << @template.content_tag(:legend, %{<i class="icon-chevron-#{(fieldset.active? ? 'down' : 'right')}"></i> #{fieldset.label}}.html_safe, :style => "#{fieldset.name == :default ? 'display:none' : ''}") contents << @template.content_tag(:p, fieldset.help) if fieldset.help.present? contents << fields.map{ |field| field_wrapper_for(field, nested_in) }.join contents.join.html_safe end end end |
#generate(options = {}) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'app/helpers/rails_admin/form_builder.rb', line 7 def generate( = {}) .reverse_merge!({ :action => @template.controller.params[:action], :model_config => @template.instance_variable_get(:@model_config), :nested_in => false }) if [:nested_in] action = :nested elsif @template.request.format == 'text/javascript' action = :modal else action = [:action] end groups = [:model_config].send(action).with(:form => self, :object => @object, :view => @template).visible_groups object_infos + groups.map do |fieldset| fieldset_for fieldset, [:nested_in] end.join.html_safe + ([:nested_in] ? '' : @template.render(:partial => 'submit_buttons')) end |
#help_for(field) ⇒ Object
65 66 67 |
# File 'app/helpers/rails_admin/form_builder.rb', line 65 def help_for field field.help.present? ? @template.content_tag(:p, field.help, :class => 'help-block') : ''.html_safe end |
#input_for(field) ⇒ Object
53 54 55 56 57 58 59 |
# File 'app/helpers/rails_admin/form_builder.rb', line 53 def input_for field @template.content_tag(:div, :class => 'controls') do field_for(field) + errors_for(field) + help_for(field) end end |
#javascript_for(field, options = {}, &block) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 |
# File 'app/helpers/rails_admin/form_builder.rb', line 84 def javascript_for(field, = {}, &block) %{<script type="text/javascript"> jQuery(function($) { if(!$("#{jquery_namespace(field)}").parents(".fields_blueprint").length) { if(#{[:modal] == false ? '!$("#modal").length' : 'true'}) { #{@template.capture(&block)} } } }); </script>}.html_safe end |
#jquery_namespace(field) ⇒ Object
96 97 98 |
# File 'app/helpers/rails_admin/form_builder.rb', line 96 def jquery_namespace field %{#{(@template.controller.params[:modal] ? '#modal ' : '')}##{dom_id(field)}_field} end |
#object_infos ⇒ Object
77 78 79 80 81 82 |
# File 'app/helpers/rails_admin/form_builder.rb', line 77 def object_infos model_config = RailsAdmin.config(object) model_label = model_config.label object_label = (object.new_record? ? I18n.t('admin.form.new_model', :name => model_label) : object.send(model_config.object_label_method).presence || "#{model_config.label} ##{object.id}") %{<span style="display:none" class="object-infos" data-model-label="#{model_label}" data-object-label="#{object_label}"></span>}.html_safe end |