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

#disable_allObject



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

def disable_all
  @disable_all = true
  @defaults ||= {}
  @defaults[:disabled] = true
end

#disable_not_accessible_for(roles) ⇒ Object



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

def disable_not_accessible_for(roles)
  ActiveSupport::Deprecation.warn('#disable_not_accessible_for is deprecated without replacement')
  @disable_not_accessible_for = roles
end

#disabled_attribute?(attribute_name) ⇒ Boolean

Returns:

  • (Boolean)


77
78
79
80
81
82
83
# File 'lib/ab_admin/views/form_builder.rb', line 77

def disabled_attribute?(attribute_name)
  @disable_all
  # return true if @disable_all
  # return unless @disable_not_accessible_for
  # @accessible_attributes ||= object.class.attr_accessible.values_at(*@disable_not_accessible_for).map(&:to_a).flatten
  # !@accessible_attributes.include?(attribute_name.to_s)
end

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



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/ab_admin/views/form_builder.rb', line 114

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 #{'hidden' if js_options[:disabled]}">
      <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
56
57
58
59
# 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

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

  options[:disabled] = disabled_attribute?(attribute_name) unless options.has_key?(:disabled)

  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 unless options[:disabled]
      options[:js_options] ||= {}
      options[:js_options][:disabled] = options[:disabled]
      return template.input_set(title) { data_fields.to_s.html_safe + 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

  super(attribute_name, options, &block)
end

#input_field(attribute_name, options = {}) ⇒ Object



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

def input_field(attribute_name, options = {})
  options[:disabled] = disabled_attribute?(attribute_name) unless options.has_key?(:disabled)
  super(attribute_name, options)
end


89
90
91
92
93
94
# File 'lib/ab_admin/views/form_builder.rb', line 89

def link_to_add_assoc(assoc, options={})
  return if @disable_all
  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


96
97
98
99
# File 'lib/ab_admin/views/form_builder.rb', line 96

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

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



101
102
103
104
105
106
107
108
# File 'lib/ab_admin/views/form_builder.rb', line 101

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

#nested?Boolean

ugly check for nested form

Returns:

  • (Boolean)


134
135
136
# File 'lib/ab_admin/views/form_builder.rb', line 134

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

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



85
86
87
# File 'lib/ab_admin/views/form_builder.rb', line 85

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

#save_buttonsObject



110
111
112
# File 'lib/ab_admin/views/form_builder.rb', line 110

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