Class: RailsAdmin::FormBuilder

Inherits:
ActionView::Helpers::FormBuilder
  • Object
show all
Includes:
NestedForm::BuilderMixin
Defined in:
app/helpers/rails_admin/form_builder.rb

Instance Method Summary collapse

Instance Method Details

#dom_id(field) ⇒ Object



93
94
95
96
97
98
99
100
# File 'app/helpers/rails_admin/form_builder.rb', line 93

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



102
103
104
# File 'app/helpers/rails_admin/form_builder.rb', line 102

def dom_name field
  (@dom_name ||= {})[field.name] ||= "#{@object_name}#{options[:index] && "[#{options[:index]}]"}[#{field.method_name}]#{field.is_a?(Config::Fields::Types::HasManyAssociation) ? '[]' : ''}"
end

#errors_for(field) ⇒ Object



59
60
61
# File 'app/helpers/rails_admin/form_builder.rb', line 59

def errors_for field
  field.errors.present? ? @template.(:span, "#{field.label} #{field.errors.first}", :class => 'help-inline') : ''.html_safe
end

#field_for(field) ⇒ Object



67
68
69
70
71
72
73
# File 'app/helpers/rails_admin/form_builder.rb', line 67

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



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/helpers/rails_admin/form_builder.rb', line 29

def field_wrapper_for field, nested_in
  if field.is_a?(RailsAdmin::Config::Fields::Types::Hidden)
    input_for(field)
  else
    # do not show nested field if the target is the origin
    unless field.inverse_of.presence && field.inverse_of == nested_in
      @template.(:div, :class => "clearfix field #{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 => (field.nested_form ? 'nester input' : '')) +
        (field.nested_form ? nested_inputs_for(field) : input_for(field))
      end
    end
  end
end

#fieldset_for(fieldset, nested_in) ⇒ Object



20
21
22
23
24
25
26
27
# File 'app/helpers/rails_admin/form_builder.rb', line 20

def fieldset_for fieldset, nested_in
  if (fields = fieldset.with(:form => self, :object => @object, :view => @template).visible_fields).length > 0
    @template. :fieldset do
      @template.(:legend, fieldset.label.html_safe + (fieldset.help.present? ? @template.(:small, fieldset.help) : ''), :style => "#{fieldset.label == I18n.translate("admin.form.basic_info") ? 'display:none' : ''}") +
      fields.map{ |field| field_wrapper_for(field, nested_in) }.join.html_safe
    end
  end
end

#generate(options = {}) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
# File 'app/helpers/rails_admin/form_builder.rb', line 7

def generate(options = {})
  options.reverse_merge!({
    :action => @template.controller.params[:action],
    :model_config => @template.instance_variable_get(:@model_config),
    :nested_in => false
  })
  groups = options[:model_config].send(options[:nested_in] ? :nested : options[:action]).with(:form => self, :object => @object, :view => @template).visible_groups
  groups.map do |fieldset|
    fieldset_for fieldset, options[:nested_in]
  end.join.html_safe +
  (options[:nested_in] ? '' : @template.render(:partial => 'submit_buttons'))
end

#help_for(field) ⇒ Object



63
64
65
# File 'app/helpers/rails_admin/form_builder.rb', line 63

def help_for field
  field.help.present? ? @template.(:span, field.help, :class => 'help-block') : ''.html_safe
end

#input_for(field) ⇒ Object



51
52
53
54
55
56
57
# File 'app/helpers/rails_admin/form_builder.rb', line 51

def input_for field
  @template.(:div, :class => 'input') do
    field_for(field) +
    errors_for(field) +
    help_for(field)
  end
end

#javascript_for(field, options = {}, &block) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'app/helpers/rails_admin/form_builder.rb', line 75

def javascript_for(field, options = {}, &block)
  %{
    <script type="text/javascript">
      jQuery(function($) {
        if(!$("#{jquery_namespace(field)}").parents(".fields_blueprint").length) {
          if(#{options[:modal] == false ? '!$("#modal").length' : 'true'}) {
            #{@template.capture(&block)}
          }
        }
      });
    </script>
  }.html_safe
end

#jquery_namespace(field) ⇒ Object



89
90
91
# File 'app/helpers/rails_admin/form_builder.rb', line 89

def jquery_namespace field
  "#{(@template.controller.params[:modal] ? '#modal ' : '')}##{dom_id(field)}_field"
end

#nested_inputs_for(field) ⇒ Object



43
44
45
46
47
48
49
# File 'app/helpers/rails_admin/form_builder.rb', line 43

def nested_inputs_for field
  @template.(:div, :class => 'input') do
    errors_for(field) +
    help_for(field)
  end + '<div class="row"></div>'.html_safe +
  field_for(field)
end