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

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



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/ab_admin/views/form_builder.rb', line 75

def attach_file_field(attribute_name, options = {})
  value = options.delete(:value) if options.key?(:value)
  value ||= object.fileupload_asset(attribute_name)
  asset_klass = object.reflections[attribute_name].try(:klass)

  element_guid = object.fileupload_guid
  element_id = template.dom_id(object, [attribute_name, element_guid].join('_'))
  max_size = options[:file_max_size] || asset_klass.try(:max_size)
  script_options = (options.delete(:script) || {}).stringify_keys

  params = {
      :method => attribute_name,
      :assetable_id => object.new_record? ? nil : object.id,
      :assetable_type => object.class.name,
      :guid => element_guid
  }.merge(script_options.delete(:params) || {})

  script_options['action'] ||= '/sunrise/fileupload?' + Rack::Utils.build_query(params)
  if !script_options['allowedExtensions'] && asset_klass
    script_options['allowedExtensions'] = asset_klass.ext_list
  end
  if options[:template_id]
    script_options['template_id'] = options[:template_id]
  end
  if options[:file]
    script_options['allowedExtensions'] ||= %w(pdf doc docx xls xlsx ppt pptx zip rar csv jpg jpeg gif png)
    script_options['template_id'] = '#fileupload_ftmpl'
    options[:asset_render_template] = 'file'
  end
  if options[:video]
    script_options['allowedExtensions'] ||= %w(mp4 flv)
    script_options['template_id'] = '#fileupload_vtmpl'
    options[:asset_render_template] = 'video_file'
  end
  script_options['allowedExtensions'] ||= %w(jpg jpeg png gif)
  script_options['multiple'] ||= object.fileupload_multiple?(attribute_name)
  script_options['element'] ||= element_id
  script_options['sizeLimit'] = max_size.megabytes.to_i

  label ||= if object && object.class.respond_to?(:human_attribute_name)
              object.class.human_attribute_name(attribute_name)
            end

  locals = {
      :element_id => element_id,
      :file_title => (options[:file_title] || "JPEG, GIF, PNG or TIFF"),
      :file_max_size => max_size,
      :label => (label || attribute_name.to_s.humanize),
      :object => object,
      :attribute_name => attribute_name,
      :assets => [value].flatten.delete_if { |v| v.nil? || v.new_record? },
      :script_options => script_options.inspect.gsub('=>', ':'),
      :multiple => script_options['multiple'],
      :asset_klass => params[:klass],
      :asset_render_template => (options[:asset_render_template] || 'asset'),
      :container_data => {klass: params[:assetable_type], assoc: params[:method], multiple: script_options['multiple']}
  }

  if options[:file1]
    container_tmpl = 'fcontainer'
  elsif options[:container]
    container_tmpl = options[:container]
  else
    container_tmpl = 'container'
  end

  if options[:description]
    opts = [attribute_name, object.class.name, object.id, object.fileupload_guid].map { |i| i.to_s.inspect }.join(', ')
    template.concat javascript_tag("$(function(){new AssetDescription(#{opts})})")
  end

  template.render(:partial => "admin/fileupload/#{container_tmpl}", :locals => locals)
end

#geo_input(prefix, &block) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/ab_admin/views/form_builder.rb', line 62

def geo_input(prefix, &block)
  template. :div, :class => 'geo_input' do
    ''.tap do |out|
      out << template.javascript_include_tag("//maps.googleapis.com/maps/api/js?sensor=false&libraries=places&language=#{I18n.locale}")
      out << template.label_tag(:geo_autocomplete, I18n.t('admin.geo_autocomplete'))
      out << template.text_field_tag("#{prefix}_geo_autocomplete")
      out.concat(capture(&block)) if block_given?
      out << template.(:div, '', :class => 'admin_map', :id => "#{prefix}_map")
      out << template.init_js("initGeoInput(#{prefix.inspect})")
    end.html_safe
  end
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
# File 'lib/ab_admin/views/form_builder.rb', line 18

def input(attribute_name, options = {}, &block)
  if options[:fancy]
    options[:input_html] ||= {}
    options[:input_html][:class] = "#{options[:input_html][:class]} fancy_select"
  end

  case options[:as]
    when :uploader
      title = options[:title] || I18n.t("admin.#{attribute_name}", :default => object.class.han(attribute_name))
      return template.input_set(title) { attach_file_field(attribute_name, options = {}) }
    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) }
  end

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

  super(attribute_name, options, &block)
end


40
41
42
43
44
# File 'lib/ab_admin/views/form_builder.rb', line 40

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


46
47
48
# File 'lib/ab_admin/views/form_builder.rb', line 46

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



50
51
52
53
54
55
56
# File 'lib/ab_admin/views/form_builder.rb', line 50

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

#save_buttonsObject



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

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