Skip to content

Commit 5d0abf5

Browse files
committed
feat: form wtapper
1 parent e997e02 commit 5d0abf5

13 files changed

+102
-162
lines changed

app/assets/stylesheets/admin/gentelella-custom.scss

+4
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,7 @@
1010
.page-title .title_right {
1111
text-align: right;
1212
}
13+
14+
form.form-horizontal .checkbox > label {
15+
padding-left: 0px;
16+
}
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class AdminFormBuilder < SimpleForm::FormBuilder
2+
def submit_cancel(*args, &block)
3+
out = ActiveSupport::SafeBuffer.new
4+
out << template.content_tag(:div, class: 'ln_solid') do
5+
end
6+
out << template.content_tag(:div, class: 'form-group') do
7+
template.content_tag :div, class: 'col-md-9 col-sm-9 col-xs-12 col-md-offset-3' do
8+
options = args.extract_options!
9+
options[:class] = [options[:class], 'btn btn-primary'].compact
10+
args << options
11+
if cancel = options.delete(:cancel)
12+
cancel_text = I18n.t('simple_form.buttons.cancel', default: 'Cancel')
13+
link = template.link_to(cancel_text, cancel, class: 'btn btn-default')
14+
submit(*args, &block) + '&nbsp;&nbsp;'.html_safe + link
15+
else
16+
submit(*args, &block)
17+
end
18+
end
19+
end
20+
out
21+
end
22+
end

app/helpers/admin_helper.rb

+17
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ def admin_stylesheet_links
5555
'https://colorlib.com/polygon/vendors/font-awesome/css/font-awesome.min.css',
5656
'https://colorlib.com/polygon/vendors/nprogress/nprogress.css',
5757
'https://colorlib.com/polygon/vendors/bootstrap-daterangepicker/daterangepicker.css',
58+
'https://colorlib.com/polygon/vendors/switchery/dist/switchery.min.css',
59+
'https://colorlib.com/polygon/vendors/iCheck/skins/flat/green.css',
5860
'https://colorlib.com/polygon/build/css/custom.min.css'
5961
]
6062
end
@@ -74,9 +76,24 @@ def admin_javascript_links
7476
'https://colorlib.com/polygon/vendors/flot.orderbars/js/jquery.flot.orderBars.js',
7577
'https://colorlib.com/polygon/vendors/flot-spline/js/jquery.flot.spline.min.js',
7678
'https://colorlib.com/polygon/vendors/DateJS/build/date.js',
79+
'https://colorlib.com/polygon/vendors/iCheck/icheck.min.js',
7780
'https://colorlib.com/polygon/vendors/moment/min/moment.min.js',
7881
'https://colorlib.com/polygon/vendors/bootstrap-daterangepicker/daterangepicker.js',
82+
'https://colorlib.com/polygon/vendors/switchery/dist/switchery.min.js',
7983
'https://colorlib.com/polygon/build/js/custom.min.js'
84+
8085
]
8186
end
87+
88+
def admin_search_form_for(obj, options, &block)
89+
options ||= {}
90+
options.deep_merge!(builder: AdminFormBuilder, html: { class: 'form-horizontal' }, wrapper: :admin, defaults: { required: false })
91+
search_form_for(obj, options, &block)
92+
end
93+
94+
def admin_form_for(obj, options, &block)
95+
options ||= {}
96+
options.deep_merge!(builder: AdminFormBuilder, html: { class: 'form-horizontal' }, wrapper: :admin, defaults: { required: false })
97+
simple_form_for(obj, options, &block)
98+
end
8299
end

app/inputs/pic_preview_input.rb

+6-3
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@ def input(_wrapper_options = nil)
44
# :version is a custom attribute from :input_html hash, so you can pick custom sizes
55
version = input_html_options.delete(:version)
66
out = ActiveSupport::SafeBuffer.new
7+
input_html_classes.push('form-control')
78
out << @builder.file_field(attribute_name, input_html_options)
89
if object.send("#{attribute_name}?")
9-
remove_chk = @builder.check_box("remove_#{attribute_name}") # carrierwave: remove_xxx
10+
remove_chk = @builder.check_box("remove_#{attribute_name}", class: 'flat') # carrierwave: remove_xxx
1011
out << '<br />'.html_safe
11-
out << "<label>#{remove_chk} remove</label>".html_safe # TRANSLATION: I18n here
12+
remove_text = I18n.t('simple_form.buttons.remove', default: 'Remove')
13+
out << "<label>#{remove_chk} #{remove_text}</label>".html_safe
1214
out << '<br />'.html_safe
13-
out << template.image_tag(object.send(attribute_name).tap { |o| break o.send(version) if version }.send('url'))
15+
img_path = object.send(attribute_name).tap { |o| break o.send(version) if version }.send('url')
16+
out << template.image_tag(img_path)
1417
end
1518
out
1619
end

app/inputs/switch_input.rb

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class SwitchInput < SimpleForm::Inputs::BooleanInput
2+
def input(wrapper_options = nil)
3+
super(wrapper_options)
4+
end
5+
end

app/views/admin/base/index.html.slim

-1
Original file line numberDiff line numberDiff line change
@@ -1 +0,0 @@
1-
input data-datetimepicker="true"
+3-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
= search_form_for @q, :builder => SimpleForm::FormBuilder, html: { class: "form-horizontal" }, wrapper: :admin, defaults: { required: false } do |f|
2-
= f.input :has_avatar, as: :boolean, label: "has avatar"
1+
= admin_search_form_for @q, nil do |f|
2+
= f.input :has_avatar, as: :switch, label: "has avatar"
33
= f.input :name_cont, label: "name contains"
44
= f.input :email_cont, label: "email contains"
55
- if Rails.env.development?
66
= f.input :created_at_eq, as: :string, input_html: { name: "", value: @q.result.to_sql }, label: "debug sql"
7-
.form-actions
8-
= f.submit "Search", class: "btn btn-primary"
7+
= f.submit_cancel 'Search'

app/views/admin/users/_form.html.slim

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,4 @@
33
= f.input :password, as: :password if f.object.new_record?
44
= f.input :admin, as: :boolean
55
= f.input :avatar, as: :pic_preview, input_html: { version: :thumb }
6-
.form-actions
7-
= f.submit class: "btn btn-primary"
6+
= f.submit_cancel cancel: admin_users_path

app/views/admin/users/edit.html.slim

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
= render partial: "btns", object: @user, as: :user
33

44
= admin_widget_box "Edit User" do
5-
= simple_form_for @user, as: :user, wrapper: :admin do |f|
5+
= admin_form_for @user, as: :user, wrapper: :admin do |f|
66
= render partial: "form", object: f, as: :f

app/views/admin/users/index.html.slim

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
- append_page_button('export to CSV', url_for(request.params.merge(format: :csv)), class: 'btn-success')
1+
- append_page_button('Export', url_for(request.params.merge(format: :csv)))
22

33
.row
44
= admin_widget_box "Filtering" do

app/views/admin/users/new.html.slim

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
.row
2-
= admin_widget_box "New User" do
3-
= simple_form_for @user, as: :user, wrapper: :admin do |f|
4-
= render partial: "form", object: f, as: :f
1+
= admin_widget_box "New User" do
2+
= admin_form_for @user, as: :user, wrapper: :admin do |f|
3+
= render partial: "form", object: f, as: :f
+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Use this setup block to configure all options available in SimpleForm.
2+
SimpleForm.setup do |config|
3+
config.error_notification_class = 'alert alert-danger'
4+
config.button_class = 'btn btn-default'
5+
config.boolean_label_class = nil
6+
7+
config.wrappers :admin, tag: :div, class: 'form-group' do |b|
8+
b.use :placeholder
9+
b.use :label, class: 'control-label col-md-3 col-sm-3 col-xs-12'
10+
b.wrapper tag: :div, class: 'col-md-9 col-sm-9 col-xs-12' do |ba|
11+
ba.use :input, class: 'form-control', wrap_with: nil
12+
# ba.use :hint, wrap_with: { tag: 'span', class: 'help-block' }
13+
ba.use :error, wrap_with: { tag: 'span', class: 'help-inline' }
14+
end
15+
end
16+
17+
config.wrappers :admin_boolean, tag: :div, class: 'form-group' do |b|
18+
b.use :label, class: 'control-label col-md-3 col-sm-3 col-xs-12'
19+
b.wrapper tag: :div, class: 'col-md-9 col-sm-9 col-xs-12' do |ba|
20+
ba.wrapper tag: :div, class: 'checkbox' do |bba|
21+
bba.use :input, class: 'flat'
22+
end
23+
end
24+
end
25+
26+
config.wrappers :admin_boolean_switch, tag: :div, class: 'form-group' do |b|
27+
b.use :label, class: 'control-label col-md-3 col-sm-3 col-xs-12'
28+
b.wrapper tag: :div, class: 'col-md-9 col-sm-9 col-xs-12' do |ba|
29+
ba.wrapper tag: :div, class: 'checkbox' do |bba|
30+
bba.use :input, class: 'js-switch'
31+
end
32+
end
33+
end
34+
35+
config.wrapper_mappings = {
36+
boolean: :admin_boolean,
37+
switch: :admin_boolean_switch
38+
}
39+
end

config/initializers/simple_form_bootstrap.rb

-146
This file was deleted.

0 commit comments

Comments
 (0)