Class: Admin::FormBuilder

Inherits:
ActionView::Helpers::FormBuilder
  • Object
show all
Defined in:
app/builders/admin/form_builder.rb

Instance Method Summary collapse

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, options = {})
  collection, value_method = options.delete(:collection), options.delete(:value_method)
  text_method, html_options = options.delete(:text_method), options.delete(:html_options) || {}
  field_wrapper(method, options) do
    @template.collection_check_boxes(@object_name, method, eval(collection), value_method, text_method, objectify_options(options), @default_options.merge(html_options)) do |b|
      @template.(: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 admin_collection_radio_buttons(method, options = {})
  collection, value_method = options.delete(:collection), options.delete(:value_method)
  text_method, html_options = options.delete(:text_method), options.delete(:html_options) || {}
  field_wrapper(method, options) do
    @template.collection_radio_buttons(@object_name, method, eval(collection), value_method, text_method, objectify_options(options), @default_options.merge(html_options)) do |b|
      @template.(:span, style: 'height: 34px;display: inline-block;') { [b.radio_button, 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, options = {})
  collection, value_method = options.delete(:collection), options.delete(:value_method)
  text_method, html_options = options.delete(:text_method), options.delete(:html_options) || {}
  is_array = options.delete(:is_array)
  options.merge!(prompt: true)
  html_options.merge!(options.extract!(:label_class, :required, :block))
  html_options.merge!(name: "#{@object_name}[#{method}][]") if is_array
  field_wrapper(method, html_options) do
    @template.collection_select(@object_name, method, eval(collection), value_method, text_method, objectify_options(options), @default_options.merge(html_options))
  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, options = {})
  collection, value_method = options.delete(:collection), options.delete(:value_method)
  text_method, html_options = options.delete(:text_method), options.delete(:html_options) || {}
  is_array = options.delete(:is_array)
  select2_options = options.delete(:select2_options)
  options.merge!(prompt: true)
  html_options.merge!(options.extract!(:label_class, :required, :block))
  html_options.merge!(name: "#{@object_name}[#{method}][]") if is_array
  sel_scripts = field_wrapper(method, html_options) do
    @template.collection_select(@object_name, method, eval(collection), value_method, text_method, objectify_options(options), @default_options.merge(html_options))
  end
  select2_scripts = @template.(:script) { "$('##{input_dom_id(method)}').select2(#{select2_options.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, options = {})
  field_wrapper(method, options) do
    options[:class] = [options[:class], 'datepicker'].flatten
    @template.text_field(@object_name, method, objectify_options(options))
  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, options = {})
  field_wrapper(method, options) do
    options[:class] = [options[:class], 'datetimepicker'].flatten
    @template.text_field(@object_name, method, objectify_options(options))
  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, options = {})
  field_wrapper(method, options) do
    @template.(: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, options = {})
  file_url = record.try(method)
  file_url = file_url.class.superclass == CarrierWave::Uploader::Base ? file_url.try('url') : file_url

  field_wrapper(method, options) do
    content = [@template.file_field(@object_name, method, objectify_options(options))]
    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, options = {})
  field_wrapper(method, options) do
    send type, @object_name, method, objectify_options(options)
  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, options = {})
  field_wrapper(method, options) 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, objectify_options(options))
    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, options = {})
  field_wrapper(method, options) do
    fields_content = []
    fields_content << @template.(: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.(:div, class: 'col-sm-12') do
        btns_content = [@template.link_to_add_association('添加', self, method.to_sym, class: 'btn btn-sm btn-success'), "&nbsp;"]
        btns_content << @template.button_tag(: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.(: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, options = {})
  field_wrapper(method, options) do
    @template.number_field(@object_name, method, objectify_options(options))
  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, options = {})
  field_wrapper(method, options) do
    @template.password_field(@object_name, method, objectify_options(options))
  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, options = {})
  field_wrapper(method, options) 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, options = {})
  field_wrapper(method, options) do
    @template.text_area(@object_name, method, objectify_options(options))
  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, options = {})
  field_wrapper(method, options) do
    @template.text_field(@object_name, method, objectify_options(options))
  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.(:ul, class: 'row list-unstyled') do
    fields.each do |field_name|
      options = decorator.try(:attribute_type_for, field_name, true) || {type: 'text_field', required: false}
      type = options.delete(:type)

      if self.respond_to?('admin_'.concat(type))
        concat(send('admin_'.concat(type), field_name, options))
      else
        concat admin_generic_field(type, field_name, options)
      end
    end
  end
end

#build_form_buttonsObject



232
233
234
235
236
# File 'app/builders/admin/form_builder.rb', line 232

def build_form_buttons
  lead = @template.(:h3, nil, class: 'lead clearfix')
  tags = @template.(:div, [submit_tag, ' ', reset_tag].join.html_safe, class: 'col-sm-12')
  lead.concat(@template.(:div, tags, 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.(: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, build_form_buttons].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, options = {}, &block)
  label_options = options.extract!(:label_class, :required, :block)
  is_block = label_options[:block]
  show_label = options.delete(:show_label)
  options[:class] ||= 'form-control'
  options.merge!(id: input_dom_id(method))

  @template.(:li, class: is_block ? 'col-sm-12' : 'col-sm-6') do
    @template.(:div, class: 'form-group') do
      content = (show_label.nil? || show_label) ? [label_tag(method, label_options)] : [@template.(:div, class: 'col-sm-2'){}]
      content << @template.(: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, options = {})
  label_class = Array(options[:label_class]) || []
  label_class << ['control-label', options[:block] ? 'col-sm-2' : 'col-sm-4'] if label_class.blank?
  label_class << 'required' if options[:required]
  @template.(:label, model.human_attribute_name(method), class: label_class.flatten)
end

#reset_tagObject



244
245
246
247
248
# File 'app/builders/admin/form_builder.rb', line 244

def reset_tag
  tag_text = @template.(:i, nil, class: 'glyphicon glyphicon-repeat')
  tag_text.concat(@template.(:span, '重置'))
  @template.(:button, tag_text, class: 'btn btn-icon btn-warning', type: 'reset')
end

#submit_tagObject



238
239
240
241
242
# File 'app/builders/admin/form_builder.rb', line 238

def submit_tag
  tag_text = @template.(:i, nil, class: 'glyphicon glyphicon-check')
  tag_text.concat(@template.(:span, '提交'))
  @template.(:button, tag_text, class: 'btn btn-icon btn-info', type: 'submit')
end