Class: AbAdmin::Views::FormBuilder

Inherits:
SimpleForm::FormBuilder
  • Object
show all
Includes:
ActionView::Helpers::JavaScriptHelper, ActionView::Helpers::TagHelper, NestedForm::BuilderMixin
Defined in:
lib/ab_admin/views/form_builder.rb

Instance Method Summary collapse

Instance Method Details

#geo_input(prefix, js_options = {}, &block) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/ab_admin/views/form_builder.rb', line 83

def geo_input(prefix, js_options={}, &block)
  input_name = "#{prefix}_geo_ac"
  js = %Q[$("##{prefix}_geo_input").geoInput(#{js_options.to_json})]
  <<-HTML.html_safe
  <script src="//maps.googleapis.com/maps/api/js?sensor=false&libraries=places&language=#{I18n.locale}" type="text/javascript"></script>
  <div class="geo_input" id="#{prefix}_geo_input">
    <div class="control-group">
      <label class="control-label" for="#{input_name}">#{I18n.t('admin.geo_autocomplete')}</label>
      <div class="controls">
        <input type="text" name="#{input_name}" id="#{input_name}" class="geo_ac string">
      </div>
    </div>
    #{template.capture(&block) if block_given?}
    <div class="controls"><div id="#{prefix}_map" class="admin_map thumbnail"></div></div>
  </div>
  #{template.init_js(js)}
  HTML
end

#input(attribute_name, options = {}, &block) ⇒ Object



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
# File 'lib/ab_admin/views/form_builder.rb', line 18

def input(attribute_name, options = {}, &block)
  options[:collection] = options[:collection].call if options[:collection].is_a?(Proc)
  
  is_select = options[:as].nil? || options[:as] == :select
  is_large_collection = options[:collection] && options[:collection].to_a.length > 30
  if options[:fancy] || (is_select && is_large_collection)
    options[:input_html] ||= {}
    options[:input_html][:class] = "#{options[:input_html][:class]} fancy_select"
  end

  if options[:append]
    return super(attribute_name, options.merge(wrapper: 'append')) do
      input_field(attribute_name, options.merge(options[:input_html] || {})) + %(<span class="add-on #{options[:append_class]}">#{options[:append]}</span>).html_safe
    end
  end

  case options[:as]
    when :map
      title = options[:title] || I18n.t("admin.#{attribute_name}", default: object.class.han(attribute_name))
      prefix = options[:prefix] || object.class.model_name.singular
      data_fields = [:lat, :lon, :zoom].map { |attr| hidden_field(attr) }.join.html_safe
      return template.input_set(title) { data_fields + geo_input(prefix, options[:js_options]) }
    when :token
      options[:label] = object.class.han(attribute_name.to_s.sub(/^token_|_id$/, '')) unless options.key?(:label)
    when :association, :tree_select
      unless options[:reflection]
        options[:collection] ||= fetch_nested_options(attribute_name) if options[:as] == :tree_select
        return association(attribute_name, options.merge(as: :select))
      end
    when :checkbox_tree
      reflection = object.class.reflect_on_association(attribute_name)
      return template.render 'admin/shared/inputs/checkbox_tree', attribute_name: attribute_name, reflection: reflection, f: self
  end

  attribute_name = "#{attribute_name}_#{options[:locale]}" if options[:locale]

  super(attribute_name, options, &block)
end


61
62
63
64
65
# File 'lib/ab_admin/views/form_builder.rb', line 61

def link_to_add_assoc(assoc, options={})
  model = @object.class.reflect_on_association(assoc).klass
  title = [@template.icon('plus', true), I18n.t('admin.add'), options[:title] || model.model_name.human].join(' ').html_safe
  link_to_add title, assoc, class: "btn btn-primary #{options[:class]}"
end


67
68
69
# File 'lib/ab_admin/views/form_builder.rb', line 67

def link_to_remove_assoc
  link_to_remove @template.icon('trash', true) + I18n.t('admin.delete'), class: 'btn btn-danger btn-mini pull-right'
end

#locale_tabs(&block) ⇒ Object



71
72
73
74
75
76
77
# File 'lib/ab_admin/views/form_builder.rb', line 71

def locale_tabs(&block)
  loc_html = {}
  Globalize.available_locales.each do |l|
    loc_html[l] = template.capture { block.call(l) }
  end
  template.render 'admin/shared/locale_tabs', loc_html: loc_html
end

#nested?Boolean

ugly check for nested form

Returns:

  • (Boolean)


103
104
105
# File 'lib/ab_admin/views/form_builder.rb', line 103

def nested?
  object_name.include?('_attributes][')
end

#render_dsl_node(node, options = {}) ⇒ Object



57
58
59
# File 'lib/ab_admin/views/form_builder.rb', line 57

def render_dsl_node(node, options={})
  input node.name, node.options.merge(options), &node.block
end

#save_buttonsObject



79
80
81
# File 'lib/ab_admin/views/form_builder.rb', line 79

def save_buttons
  template.render 'admin/shared/save_buttons'
end