Class: Admin::FormBuilder
- Inherits:
-
ActionView::Helpers::FormBuilder
- Object
- ActionView::Helpers::FormBuilder
- Admin::FormBuilder
- Defined in:
- app/builders/admin/form_builder.rb
Instance Method Summary collapse
- #admin_collection_check_boxes(method, options = {}) ⇒ Object
- #admin_collection_radio_buttons(method, options = {}) ⇒ Object
- #admin_collection_select(method, options = {}) ⇒ Object
- #admin_collection_select2(method, options = {}) ⇒ Object
- #admin_date_select(method, options = {}) ⇒ Object
- #admin_datetime_select(method, options = {}) ⇒ Object
- #admin_display_field(method, options = {}) ⇒ Object
- #admin_file_field(method, options = {}) ⇒ Object
- #admin_generic_field(type, method, options = {}) ⇒ Object
- #admin_image_field(method, options = {}) ⇒ Object
- #admin_nested_fields(method, options = {}) ⇒ Object
- #admin_number_field(method, options = {}) ⇒ Object
- #admin_password_field(method, options = {}) ⇒ Object
- #admin_render_field(method, options = {}) ⇒ Object
- #admin_text_area(method, options = {}) ⇒ Object
- #admin_text_field(method, options = {}) ⇒ Object
- #build_fields_for(decorator, fields) ⇒ Object
- #build_form_buttons ⇒ Object
- #build_form_for(decorator, *attribute_name_list) ⇒ Object
- #field_wrapper(method, options = {}, &block) ⇒ Object
- #input_dom_id(field_name) ⇒ Object
- #label_tag(method, options = {}) ⇒ Object
- #reset_tag ⇒ Object
- #submit_tag ⇒ Object
Instance Method Details
#admin_collection_check_boxes(method, options = {}) ⇒ Object
125 126 127 128 129 130 131 132 133 |
# File 'app/builders/admin/form_builder.rb', line 125 def admin_collection_check_boxes(method, = {}) collection, value_method = .delete(:collection), .delete(:value_method) text_method, = .delete(:text_method), .delete(:html_options) || {} field_wrapper(method, ) do @template.collection_check_boxes(@object_name, method, eval(collection), value_method, text_method, (), @default_options.merge()) do |b| @template.content_tag(:span) { [b.check_box, b.text].join(' ').html_safe } end end end |
#admin_collection_radio_buttons(method, options = {}) ⇒ Object
182 183 184 185 186 187 188 189 190 |
# File 'app/builders/admin/form_builder.rb', line 182 def (method, = {}) collection, value_method = .delete(:collection), .delete(:value_method) text_method, = .delete(:text_method), .delete(:html_options) || {} field_wrapper(method, ) do @template.(@object_name, method, eval(collection), value_method, text_method, (), @default_options.merge()) do |b| @template.content_tag(:span, style: 'height: 34px;display: inline-block;') { [b., b.text].join(' ').html_safe } end end end |
#admin_collection_select(method, options = {}) ⇒ Object
150 151 152 153 154 155 156 157 158 159 160 |
# File 'app/builders/admin/form_builder.rb', line 150 def admin_collection_select(method, = {}) collection, value_method = .delete(:collection), .delete(:value_method) text_method, = .delete(:text_method), .delete(:html_options) || {} is_array = .delete(:is_array) .merge!(prompt: true) .merge!(.extract!(:label_class, :required, :block)) .merge!(name: "#{@object_name}[#{method}][]") if is_array field_wrapper(method, ) do @template.collection_select(@object_name, method, eval(collection), value_method, text_method, (), @default_options.merge()) end end |
#admin_collection_select2(method, options = {}) ⇒ Object
135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'app/builders/admin/form_builder.rb', line 135 def admin_collection_select2(method, = {}) collection, value_method = .delete(:collection), .delete(:value_method) text_method, = .delete(:text_method), .delete(:html_options) || {} is_array = .delete(:is_array) = .delete(:select2_options) .merge!(prompt: true) .merge!(.extract!(:label_class, :required, :block)) .merge!(name: "#{@object_name}[#{method}][]") if is_array sel_scripts = field_wrapper(method, ) do @template.collection_select(@object_name, method, eval(collection), value_method, text_method, (), @default_options.merge()) end select2_scripts = @template.content_tag(:script) { "$('##{input_dom_id(method)}').select2(#{.to_json});".html_safe } sel_scripts.concat(select2_scripts) end |
#admin_date_select(method, options = {}) ⇒ Object
168 169 170 171 172 173 |
# File 'app/builders/admin/form_builder.rb', line 168 def admin_date_select(method, = {}) field_wrapper(method, ) do [:class] = [[:class], 'datepicker'].flatten @template.text_field(@object_name, method, ()) end end |
#admin_datetime_select(method, options = {}) ⇒ Object
175 176 177 178 179 180 |
# File 'app/builders/admin/form_builder.rb', line 175 def admin_datetime_select(method, = {}) field_wrapper(method, ) do [:class] = [[:class], 'datetimepicker'].flatten @template.text_field(@object_name, method, ()) end end |
#admin_display_field(method, options = {}) ⇒ Object
72 73 74 75 76 |
# File 'app/builders/admin/form_builder.rb', line 72 def admin_display_field(method, = {}) field_wrapper(method, ) do @template.content_tag(:span, record.try(method)) end end |
#admin_file_field(method, options = {}) ⇒ Object
102 103 104 105 106 107 108 109 110 111 |
# File 'app/builders/admin/form_builder.rb', line 102 def admin_file_field(method, = {}) file_url = record.try(method) file_url = file_url.class.superclass == CarrierWave::Uploader::Base ? file_url.try('url') : file_url field_wrapper(method, ) do content = [@template.file_field(@object_name, method, ())] content << @template.link_to(file_url, target: '_blank') {"点击查看"} content.join.html_safe end end |
#admin_generic_field(type, method, options = {}) ⇒ Object
96 97 98 99 100 |
# File 'app/builders/admin/form_builder.rb', line 96 def admin_generic_field(type, method, = {}) field_wrapper(method, ) do send type, @object_name, method, () end end |
#admin_image_field(method, options = {}) ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 |
# File 'app/builders/admin/form_builder.rb', line 113 def admin_image_field(method, = {}) field_wrapper(method, ) do img_url = record.try(method) img_url = img_url.class.superclass == CarrierWave::Uploader::Base ? img_url.try('url') : img_url content = [@template.link_to(img_url, target: '_blank') do decorate.respond_to?("human_#{method}") ? decorate.human(method) : @template.image_tag(img_url) end] content << @template.file_field(@object_name, method, ()) content.join.html_safe end end |
#admin_nested_fields(method, options = {}) ⇒ Object
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 63 64 |
# File 'app/builders/admin/form_builder.rb', line 32 def admin_nested_fields(method, = {}) field_wrapper(method, ) do fields_content = [] fields_content << @template.content_tag(:div, id: "#{method}_nested_form") do nested_content = [] nested_content << self.fields_for(method.to_sym) do |ff| @template.render("#{method.to_s.singularize}_fields", f: ff) end nested_content << @template.content_tag(:div, class: 'col-sm-12') do btns_content = [@template.link_to_add_association('添加', self, method.to_sym, class: 'btn btn-sm btn-success'), " "] btns_content << @template.(:input, class: "sort-btn btn btn-sm btn-success", type: "button", id: "#{method}-sort-btn") { "排序" } btns_content.join.html_safe end nested_content.join.html_safe end fields_content << @template.render("admin/application/partials/sort_script", sort_btn_id: "##{method}-sort-btn", container_id: "##{method}_nested_form", resource_parts: method.pluralize) fields_content << @template.content_tag(:script) do %( $(document).on('turbolinks:load', function () { Cocoon(); }); ).html_safe end fields_content.join.html_safe end end |
#admin_number_field(method, options = {}) ⇒ Object
90 91 92 93 94 |
# File 'app/builders/admin/form_builder.rb', line 90 def admin_number_field(method, = {}) field_wrapper(method, ) do @template.number_field(@object_name, method, ()) end end |
#admin_password_field(method, options = {}) ⇒ Object
84 85 86 87 88 |
# File 'app/builders/admin/form_builder.rb', line 84 def admin_password_field(method, = {}) field_wrapper(method, ) do @template.password_field(@object_name, method, ()) end end |
#admin_render_field(method, options = {}) ⇒ Object
66 67 68 69 70 |
# File 'app/builders/admin/form_builder.rb', line 66 def admin_render_field(method, = {}) field_wrapper(method, ) do @template.render("#{method.to_s.singularize}", f: self) end end |
#admin_text_area(method, options = {}) ⇒ Object
162 163 164 165 166 |
# File 'app/builders/admin/form_builder.rb', line 162 def admin_text_area(method, = {}) field_wrapper(method, ) do @template.text_area(@object_name, method, ()) end end |
#admin_text_field(method, options = {}) ⇒ Object
78 79 80 81 82 |
# File 'app/builders/admin/form_builder.rb', line 78 def admin_text_field(method, = {}) field_wrapper(method, ) do @template.text_field(@object_name, method, ()) end end |
#build_fields_for(decorator, fields) ⇒ Object
217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
# File 'app/builders/admin/form_builder.rb', line 217 def build_fields_for(decorator, fields) @template.content_tag(:ul, class: 'row list-unstyled') do fields.each do |field_name| = decorator.try(:attribute_type_for, field_name, true) || {type: 'text_field', required: false} type = .delete(:type) if self.respond_to?('admin_'.concat(type)) concat(send('admin_'.concat(type), field_name, )) else concat admin_generic_field(type, field_name, ) end end end end |
#build_form_buttons ⇒ Object
232 233 234 235 236 |
# File 'app/builders/admin/form_builder.rb', line 232 def lead = @template.content_tag(:h3, nil, class: 'lead clearfix') = @template.content_tag(:div, [submit_tag, ' ', reset_tag].join.html_safe, class: 'col-sm-12') lead.concat(@template.content_tag(:div, , class: 'row')) end |
#build_form_for(decorator, *attribute_name_list) ⇒ Object
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
# File 'app/builders/admin/form_builder.rb', line 192 def build_form_for(decorator, *attribute_name_list) fields_hash = [] attribute_name_list.each do |attribute_name| fields_hash = decorator.try(attribute_name) break if !fields_hash.empty? end if fields_hash.empty? fields_hash = decorator.try(:new_attributes) end profiles = fields_hash.fetch(:profiles, []) form_fields = if profiles.present? profiles.collect { |profile| fields = fields_hash.fetch(profile.to_sym, {}) content = [] content << @template.content_tag(:h3, I18n.t("profiles.#{profile}", default: profile), class: 'lead clearfix') content << build_fields_for(decorator, fields) content.join.html_safe }.join.html_safe else build_fields_for(decorator, fields_hash) end [form_fields, ].join.html_safe end |
#field_wrapper(method, options = {}, &block) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'app/builders/admin/form_builder.rb', line 16 def field_wrapper(method, = {}, &block) = .extract!(:label_class, :required, :block) is_block = [:block] show_label = .delete(:show_label) [:class] ||= 'form-control' .merge!(id: input_dom_id(method)) @template.content_tag(:li, class: is_block ? 'col-sm-12' : 'col-sm-6') do @template.content_tag(:div, class: 'form-group') do content = (show_label.nil? || show_label) ? [label_tag(method, )] : [@template.content_tag(:div, class: 'col-sm-2'){}] content << @template.content_tag(:div, yield(block), class: is_block ? 'col-sm-10' : 'col-sm-8') content.join.html_safe end end end |
#input_dom_id(field_name) ⇒ Object
12 13 14 |
# File 'app/builders/admin/form_builder.rb', line 12 def input_dom_id(field_name) '_form_id_'.concat(field_name.to_s) end |
#label_tag(method, options = {}) ⇒ Object
5 6 7 8 9 10 |
# File 'app/builders/admin/form_builder.rb', line 5 def label_tag(method, = {}) label_class = Array([:label_class]) || [] label_class << ['control-label', [:block] ? 'col-sm-2' : 'col-sm-4'] if label_class.blank? label_class << 'required' if [:required] @template.content_tag(:label, model.human_attribute_name(method), class: label_class.flatten) end |
#reset_tag ⇒ Object
244 245 246 247 248 |
# File 'app/builders/admin/form_builder.rb', line 244 def reset_tag tag_text = @template.content_tag(:i, nil, class: 'glyphicon glyphicon-repeat') tag_text.concat(@template.content_tag(:span, '重置')) @template.content_tag(:button, tag_text, class: 'btn btn-icon btn-warning', type: 'reset') end |
#submit_tag ⇒ Object
238 239 240 241 242 |
# File 'app/builders/admin/form_builder.rb', line 238 def submit_tag tag_text = @template.content_tag(:i, nil, class: 'glyphicon glyphicon-check') tag_text.concat(@template.content_tag(:span, '提交')) @template.content_tag(:button, tag_text, class: 'btn btn-icon btn-info', type: 'submit') end |