Class: AbAdmin::Views::FormBuilder
- Inherits:
-
SimpleForm::FormBuilder
- Object
- SimpleForm::FormBuilder
- AbAdmin::Views::FormBuilder
- Includes:
- ActionView::Helpers::JavaScriptHelper, ActionView::Helpers::TagHelper, NestedForm::BuilderMixin
- Defined in:
- lib/ab_admin/views/form_builder.rb
Instance Method Summary collapse
- #attach_file_field(attribute_name, options = {}) ⇒ Object
- #geo_input(prefix, js_options = {}, &block) ⇒ Object
- #input(attribute_name, options = {}, &block) ⇒ Object
- #link_to_add_assoc(assoc, options = {}) ⇒ Object
- #link_to_remove_assoc ⇒ Object
- #locale_tabs(&block) ⇒ Object
- #save_buttons ⇒ Object
Instance Method Details
#attach_file_field(attribute_name, options = {}) ⇒ Object
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 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/ab_admin/views/form_builder.rb', line 98 def attach_file_field(attribute_name, = {}) value = .delete(:value) if .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 = [:file_max_size] || asset_klass.try(:max_size) = (.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(.delete(:params) || {}) ['action'] ||= '/sunrise/fileupload?' + Rack::Utils.build_query(params) if !['allowedExtensions'] && asset_klass ['allowedExtensions'] = asset_klass.ext_list end if [:template_id] ['template_id'] = [:template_id] end if [:file] ['allowedExtensions'] ||= %w(pdf doc docx xls xlsx ppt pptx zip rar csv jpg jpeg gif png) ['template_id'] = '#fileupload_ftmpl' [:asset_render_template] = 'file' elsif [:video] ['allowedExtensions'] ||= %w(mp4 flv) ['template_id'] = '#fileupload_vtmpl' [:asset_render_template] = 'video_file' end ['allowedExtensions'] ||= %w(jpg jpeg png gif) ['multiple'] ||= object.fileupload_multiple?(attribute_name) ['element'] ||= element_id ['sizeLimit'] = max_size.megabytes.to_i locals = { element_id: element_id, error: error(attribute_name), file_title: ([:file_title] || ['allowedExtensions'].join(', ')), file_max_size: max_size, assets: [value].flatten.delete_if { |v| v.nil? || v.new_record? }, multiple: ['multiple'], asset_render_template: ([:asset_render_template] || 'asset'), container_data: { klass: params[:assetable_type], asset: asset_klass.to_s, assoc: params[:method], multiple: ['multiple'], crop: [:crop] } } locals[:css_class] = ['fileupload', "#{locals[:asset_render_template]}_asset_type"] locals[:css_class] << (['multiple'] ? 'many_assets' : 'one_asset') locals[:css_class] << 'error' if locals[:error] js_opts = [locals[:element_id], template.sort_admin_assets_path(klass: asset_klass), locals[:multiple]].map(&:inspect).join(', ') locals[:js] = <<-JAVASCRIPT var upl = new qq.FileUploaderInput(#{.to_json}); upl._setupDragDrop(); new AdminAssets(#{js_opts}); JAVASCRIPT if [: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] || 'container'}", locals: locals) end |
#geo_input(prefix, js_options = {}, &block) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/ab_admin/views/form_builder.rb', line 79 def geo_input(prefix, ={}, &block) input_name = "#{prefix}_geo_ac" js = %Q[$("##{prefix}_geo_input").geoInput(#{.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> #{template.init_js(js)} </div> HTML end |
#input(attribute_name, options = {}, &block) ⇒ Object
17 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 17 def input(attribute_name, = {}, &block) unless .key?(:fancy) if (!.key?(:as) || [:as] == :select) && [:collection].respond_to?(:size) && [:collection].size > 10 [:fancy] = true end end if [:fancy] [:input_html] ||= {} [:input_html][:class] = "#{[:input_html][:class]} fancy_select" end case [:as] when :uploader title = [:title] || I18n.t("admin.#{attribute_name}", default: object.class.han(attribute_name)) return template.input_set(title) { attach_file_field(attribute_name, ) } when :map title = [:title] || I18n.t("admin.#{attribute_name}", default: object.class.han(attribute_name)) prefix = [: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, [:js_options]) } when :token [:label] ||= object.class.han(attribute_name.to_s.sub(/^token_|_id$/, '')) when :association, :tree_select unless [:reflection] if [:as] == :tree_select [:collection] ||= begin reflection = object.class.reflect_on_association(attribute_name) records = reflection.klass.all(reflection..slice(:conditions, :order)) reflection.klass.nested_opts_with_parent(records, object) end end return association(attribute_name, .merge(as: :select)) end end attribute_name = "#{attribute_name}_#{[:locale]}" if [:locale] super(attribute_name, , &block) end |
#link_to_add_assoc(assoc, options = {}) ⇒ Object
57 58 59 60 61 |
# File 'lib/ab_admin/views/form_builder.rb', line 57 def link_to_add_assoc(assoc, ={}) 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 #{[:class]}" end |
#link_to_remove_assoc ⇒ Object
63 64 65 |
# File 'lib/ab_admin/views/form_builder.rb', line 63 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
67 68 69 70 71 72 73 |
# File 'lib/ab_admin/views/form_builder.rb', line 67 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_buttons ⇒ Object
75 76 77 |
# File 'lib/ab_admin/views/form_builder.rb', line 75 def template.render 'admin/shared/save_buttons' end |