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
- #disable_all ⇒ Object
- #disable_not_accessible_for(roles) ⇒ Object
- #disabled_attribute?(attribute_name) ⇒ Boolean
- #geo_input(prefix, js_options = {}, &block) ⇒ Object
- #input(attribute_name, options = {}, &block) ⇒ Object
- #input_field(attribute_name, options = {}) ⇒ Object
- #link_to_add_assoc(assoc, options = {}) ⇒ Object
- #link_to_remove_assoc ⇒ Object
- #locale_tabs(options = {}, &block) ⇒ Object
-
#nested? ⇒ Boolean
ugly check for nested form.
- #render_dsl_node(node, options = {}) ⇒ Object
- #save_buttons ⇒ Object
Instance Method Details
#disable_all ⇒ Object
69 70 71 72 73 |
# File 'lib/ab_admin/views/form_builder.rb', line 69 def disable_all @disable_all = true @defaults ||= {} @defaults[:disabled] = true end |
#disable_not_accessible_for(roles) ⇒ Object
75 76 77 78 |
# File 'lib/ab_admin/views/form_builder.rb', line 75 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
80 81 82 83 84 85 86 |
# File 'lib/ab_admin/views/form_builder.rb', line 80 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
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/ab_admin/views/form_builder.rb', line 117 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 #{'hidden' if [: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 60 61 62 |
# File 'lib/ab_admin/views/form_builder.rb', line 18 def input(attribute_name, = {}, &block) [:collection] = [:collection].call if [:collection].is_a?(Proc) is_select = [:as].nil? || [:as] == :select is_large_collection = [:collection] && [:collection].to_a.length > 30 if [:fancy] || (is_select && is_large_collection) [:input_html] ||= {} [:input_html][:class] = "#{[:input_html][:class]} fancy_select" end if [:append] return super(attribute_name, .merge(wrapper: 'append')) do input_field(attribute_name, .merge([:input_html] || {})) + %(<span class="add-on #{[:append_class]}">#{[:append]}</span>).html_safe end end if [:locale] [:label] ||= object.class.han(attribute_name, locale: [:locale]) attribute_name = "#{attribute_name}_#{[:locale]}" end [:disabled] = disabled_attribute?(attribute_name) unless .has_key?(:disabled) case [:as] 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 unless [:disabled] [:js_options] ||= {} [:js_options][:disabled] = [:disabled] return template.input_set(title) { data_fields.to_s.html_safe + geo_input(prefix, [:js_options]) } when :token [:label] = object.class.han(attribute_name.to_s.sub(/^token_|_id$/, '')) unless .key?(:label) when :association, :tree_select unless [:reflection] [:collection] ||= (attribute_name) if [:as] == :tree_select return association(attribute_name, .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, , &block) end |
#input_field(attribute_name, options = {}) ⇒ Object
64 65 66 67 |
# File 'lib/ab_admin/views/form_builder.rb', line 64 def input_field(attribute_name, = {}) [:disabled] = disabled_attribute?(attribute_name) unless .has_key?(:disabled) super(attribute_name, ) end |
#link_to_add_assoc(assoc, options = {}) ⇒ Object
92 93 94 95 96 97 |
# File 'lib/ab_admin/views/form_builder.rb', line 92 def link_to_add_assoc(assoc, ={}) return if @disable_all model = @object.class.reflect_on_association(assoc).klass title = [@template.icon('plus', true), I18n.t('admin.add'), [:title] || model.model_name.human].join(' ').html_safe link_to_add title, assoc, class: "btn btn-primary #{[:class]}" end |
#link_to_remove_assoc ⇒ Object
99 100 101 102 |
# File 'lib/ab_admin/views/form_builder.rb', line 99 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
104 105 106 107 108 109 110 111 |
# File 'lib/ab_admin/views/form_builder.rb', line 104 def locale_tabs(={}, &block) locale_html = {} [:locales] ||= Globalize.available_locales [:locales].each do |l| locale_html[l] = template.capture { block.call(l) } end template.render 'admin/shared/locale_tabs', locale_html: locale_html, locales: [:locales] end |
#nested? ⇒ Boolean
ugly check for nested form
137 138 139 |
# File 'lib/ab_admin/views/form_builder.rb', line 137 def nested? object_name.include?('_attributes][') end |
#render_dsl_node(node, options = {}) ⇒ Object
88 89 90 |
# File 'lib/ab_admin/views/form_builder.rb', line 88 def render_dsl_node(node, ={}) input node.name, node..merge(), &node.block end |
#save_buttons ⇒ Object
113 114 115 |
# File 'lib/ab_admin/views/form_builder.rb', line 113 def template.render 'admin/shared/save_buttons' end |