Skip to content

Commit 9717ff5

Browse files
committed
feat: select2
1 parent a4a3668 commit 9717ff5

23 files changed

+162
-88
lines changed

Gemfile

-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ gem 'uglifier', '>= 1.3.0'
3131

3232
# JS plugin
3333
gem 'jquery-rails'
34-
gem 'select2-rails', '~> 3'
3534
gem 'turbolinks', '~> 5'
3635

3736
# background jobs

Gemfile.lock

+1-4
Original file line numberDiff line numberDiff line change
@@ -376,8 +376,6 @@ GEM
376376
sdoc (0.4.2)
377377
json (~> 1.7, >= 1.7.7)
378378
rdoc (~> 4.0)
379-
select2-rails (3.5.10)
380-
thor (~> 0.14)
381379
settingslogic (2.0.9)
382380
sidekiq (5.0.4)
383381
concurrent-ruby (~> 1.0)
@@ -516,7 +514,6 @@ DEPENDENCIES
516514
rubocop
517515
sass-rails (~> 5.0)
518516
sdoc (~> 0.4.0)
519-
select2-rails (~> 3)
520517
settingslogic
521518
sidekiq
522519
sidekiq-limit_fetch
@@ -539,4 +536,4 @@ DEPENDENCIES
539536
xray-rails
540537

541538
BUNDLED WITH
542-
1.15.4
539+
1.16.0
+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#= require jquery
22
#= require jquery_ujs
3+
#= require_tree ./admin
34
#= require_self
45

Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
#= require select2
2-
31
init_select2 = (dom) ->
4-
$(dom).find("select[data-select]").each ->
2+
$(dom).find("select.js-select2").each ->
53
$(@).select2({ "width": $(@).data("width") || "80%", "allowClear": true })
6-
$(dom).find("[data-select=tags]").each ->
7-
opts = { "width": $(@).data("width") || "80%" }
8-
opts.tokenSeparators = [",", " "]
9-
tags = eval($(@).data("tags"))
10-
opts.tags = tags
11-
$(@).val(tags.join(","))
12-
$(@).select2(opts)
4+
$(dom).find("select.js-select2-tags").each ->
5+
opts =
6+
tokenSeparators: [',', ' ']
7+
tags: true
8+
with: '80%'
9+
console.log(opts)
10+
$(@).attr('multiple', true).select2(opts)
1311

1412
$ ->
1513
init_select2("body")

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

+8
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,11 @@ form.form-horizontal .checkbox > label {
2828
.table {
2929
margin-bottom: 5px;
3030
}
31+
32+
.checkbox label, .radio label {
33+
padding-left: 0px;
34+
}
35+
36+
.form-horizontal .checkbox, .form-horizontal .checkbox-inline, .form-horizontal .radio, .form-horizontal .radio-inline {
37+
padding-top: 4px;
38+
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
/*
2-
*= require jquery-ui/datepicker
3-
*= require jquery-ui/slider
42
*= require_self
53
*/

app/assets/stylesheets/datetimepicker.css.scss

-5
This file was deleted.

app/assets/stylesheets/unicorn.css.scss

-7
This file was deleted.

app/controllers/admin/base_controller.rb

+5
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ def index
1313
set_meta(title: "#{ENV['APP_NAME']} Admin")
1414
end
1515

16+
def example
17+
@admin_page_title = 'Template Examples'
18+
set_meta(title: @admin_page_title)
19+
end
20+
1621
private
1722

1823
def authenticate_admin_user!

app/controllers/admin/categories_controller.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def category
7676
end
7777

7878
def category_params
79-
params.fetch(:category, {}).permit(:name, :tag_list, :sort)
79+
params.fetch(:category, {}).permit([:name, { tag_list: [] }, :sort])
8080
end
8181

8282
end

app/helpers/admin_helper.rb

+40-37
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,19 @@ def collection_for_delete_state
2828
[['Deleted', :only_deleted], ['All', :with_deleted]]
2929
end
3030

31-
def collection_for_tags
32-
Tag.all.map(&:name)
33-
end
34-
35-
def render_admin_sorting_buttons(instance, column: :sort)
31+
def render_admin_sorting_buttons(instance, column: :sort, html_attrs: nil)
3632
scope = instance.class.to_s.split('::').last.underscore
3733
html = [:first, :up, :down, :last, :remove].map do |action|
3834
if action == :remove && instance.try(column).nil?
3935
''
4036
else
41-
link_to action.to_s.camelize, send("admin_#{scope}_path", instance, "#{scope}[#{column}]" => action, redirect_to: url_for), method: :put, class: 'btn btn-mini'
37+
html_opts = {
38+
method: :put,
39+
40+
}.merge(html_attrs || {})
41+
link_to action.to_s.camelize, send("admin_#{scope}_path", instance, "#{scope}[#{column}]" => action, redirect_to: url_for), html_opts
4242
end
43-
end.join(' ')
43+
end.select(&:present?).join(' | ')
4444
raw html
4545
end
4646

@@ -51,41 +51,44 @@ def menu_link(link)
5151

5252
def admin_stylesheet_links
5353
[
54-
'https://colorlib.com/polygon/vendors/bootstrap/dist/css/bootstrap.min.css',
55-
'https://colorlib.com/polygon/vendors/datatables.net-bs/css/dataTables.bootstrap.min.css',
56-
'https://colorlib.com/polygon/vendors/datatables.net-buttons-bs/css/buttons.bootstrap.min.css',
57-
'https://colorlib.com/polygon/vendors/datatables.net-fixedheader-bs/css/fixedHeader.bootstrap.min.css',
58-
'https://colorlib.com/polygon/vendors/datatables.net-responsive-bs/css/responsive.bootstrap.min.css',
59-
'https://colorlib.com/polygon/vendors/datatables.net-scroller-bs/css/scroller.bootstrap.min.css',
60-
'https://colorlib.com/polygon/vendors/font-awesome/css/font-awesome.min.css',
61-
'https://colorlib.com/polygon/vendors/nprogress/nprogress.css',
62-
'https://colorlib.com/polygon/vendors/bootstrap-daterangepicker/daterangepicker.css',
63-
'https://colorlib.com/polygon/vendors/switchery/dist/switchery.min.css',
64-
'https://colorlib.com/polygon/vendors/iCheck/skins/flat/green.css',
65-
'https://colorlib.com/polygon/build/css/custom.min.css'
54+
'//colorlib.com/polygon/vendors/bootstrap/dist/css/bootstrap.min.css',
55+
'//colorlib.com/polygon/vendors/datatables.net-bs/css/dataTables.bootstrap.min.css',
56+
'//colorlib.com/polygon/vendors/datatables.net-buttons-bs/css/buttons.bootstrap.min.css',
57+
'//colorlib.com/polygon/vendors/datatables.net-fixedheader-bs/css/fixedHeader.bootstrap.min.css',
58+
'//colorlib.com/polygon/vendors/datatables.net-responsive-bs/css/responsive.bootstrap.min.css',
59+
'//colorlib.com/polygon/vendors/datatables.net-scroller-bs/css/scroller.bootstrap.min.css',
60+
'//colorlib.com/polygon/vendors/font-awesome/css/font-awesome.min.css',
61+
'//colorlib.com/polygon/vendors/nprogress/nprogress.css',
62+
'//colorlib.com/polygon/vendors/bootstrap-daterangepicker/daterangepicker.css',
63+
'//colorlib.com/polygon/vendors/switchery/dist/switchery.min.css',
64+
'//colorlib.com/polygon/vendors/iCheck/skins/flat/green.css',
65+
'//colorlib.com/polygon/vendors/select2/dist/css/select2.min.css',
66+
'//colorlib.com/polygon/build/css/custom.min.css'
6667
]
6768
end
6869

6970
def admin_javascript_links
7071
[
71-
'https://colorlib.com/polygon/vendors/bootstrap/dist/js/bootstrap.min.js',
72-
'https://colorlib.com/polygon/vendors/fastclick/lib/fastclick.js',
73-
'https://colorlib.com/polygon/vendors/nprogress/nprogress.js',
74-
'https://colorlib.com/polygon/vendors/Chart.js/dist/Chart.min.js',
75-
'https://colorlib.com/polygon/vendors/jquery-sparkline/dist/jquery.sparkline.min.js',
76-
'https://colorlib.com/polygon/vendors/Flot/jquery.flot.js',
77-
'https://colorlib.com/polygon/vendors/Flot/jquery.flot.pie.js',
78-
'https://colorlib.com/polygon/vendors/Flot/jquery.flot.time.js',
79-
'https://colorlib.com/polygon/vendors/Flot/jquery.flot.stack.js',
80-
'https://colorlib.com/polygon/vendors/Flot/jquery.flot.resize.js',
81-
'https://colorlib.com/polygon/vendors/flot.orderbars/js/jquery.flot.orderBars.js',
82-
'https://colorlib.com/polygon/vendors/flot-spline/js/jquery.flot.spline.min.js',
83-
'https://colorlib.com/polygon/vendors/DateJS/build/date.js',
84-
'https://colorlib.com/polygon/vendors/iCheck/icheck.min.js',
85-
'https://colorlib.com/polygon/vendors/moment/min/moment.min.js',
86-
'https://colorlib.com/polygon/vendors/bootstrap-daterangepicker/daterangepicker.js',
87-
'https://colorlib.com/polygon/vendors/switchery/dist/switchery.min.js',
88-
'https://colorlib.com/polygon/build/js/custom.min.js'
72+
'//colorlib.com/polygon/vendors/bootstrap/dist/js/bootstrap.min.js',
73+
'//colorlib.com/polygon/vendors/fastclick/lib/fastclick.js',
74+
'//colorlib.com/polygon/vendors/nprogress/nprogress.js',
75+
'//colorlib.com/polygon/vendors/Chart.js/dist/Chart.bundle.min.js',
76+
'//colorlib.com/polygon/vendors/Chart.js/dist/Chart.min.js',
77+
'//colorlib.com/polygon/vendors/jquery-sparkline/dist/jquery.sparkline.min.js',
78+
'//colorlib.com/polygon/vendors/Flot/jquery.flot.js',
79+
'//colorlib.com/polygon/vendors/Flot/jquery.flot.pie.js',
80+
'//colorlib.com/polygon/vendors/Flot/jquery.flot.time.js',
81+
'//colorlib.com/polygon/vendors/Flot/jquery.flot.stack.js',
82+
'//colorlib.com/polygon/vendors/Flot/jquery.flot.resize.js',
83+
'//colorlib.com/polygon/vendors/flot.orderbars/js/jquery.flot.orderBars.js',
84+
'//colorlib.com/polygon/vendors/flot-spline/js/jquery.flot.spline.min.js',
85+
'//colorlib.com/polygon/vendors/DateJS/build/date.js',
86+
'//colorlib.com/polygon/vendors/iCheck/icheck.min.js',
87+
'//colorlib.com/polygon/vendors/moment/min/moment.min.js',
88+
'//colorlib.com/polygon/vendors/bootstrap-daterangepicker/daterangepicker.js',
89+
'//colorlib.com/polygon/vendors/switchery/dist/switchery.min.js',
90+
'//colorlib.com/polygon/vendors/select2/dist/js/select2.full.min.js',
91+
'//colorlib.com/polygon/build/js/custom.min.js'
8992

9093
]
9194
end

app/inputs/debug_sql_input.rb

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class DebugSqlInput < SimpleForm::Inputs::StringInput
2+
3+
def input(*args)
4+
return '' if Rails.env.production?
5+
super(*args)
6+
end
7+
8+
private
9+
10+
def input_html_options(*args)
11+
(super(*args) || {}).merge(name: '', value: object.result.to_sql)
12+
end
13+
14+
def attribute_name
15+
:created_at_eq
16+
end
17+
18+
def label_text(*args)
19+
'Debug SQL'
20+
end
21+
end

app/inputs/is_deleted_input.rb

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class IsDeletedInput < SimpleForm::Inputs::CollectionRadioButtonsInput
2+
# TODO: i18n
3+
def input(wrapper_options = nil)
4+
wrapper_options = (wrapper_options || {}).merge(
5+
include_blank: false,
6+
wrapper: :admin_radio
7+
)
8+
@input_type = 'radio_buttons'
9+
super(wrapper_options)
10+
end
11+
12+
private
13+
14+
def collection
15+
[['Not Deleted', ''], ['Deleted', :only_deleted], ['All', :with_deleted]]
16+
end
17+
18+
def label_text(*args)
19+
'Deleted'
20+
end
21+
end

app/inputs/tags_input.rb

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class TagsInput < SimpleForm::Inputs::CollectionSelectInput
2+
3+
private
4+
5+
def input_html_options(*args)
6+
(super(*args) || {}).merge(class: 'js-select2-tags', multiple: 'multiple')
7+
end
8+
9+
def collection
10+
@collection ||= Tag.all.map { |tag| [tag.name, tag.name] }
11+
end
12+
13+
def multiple?
14+
true
15+
end
16+
17+
def skip_include_blank?
18+
false
19+
end
20+
end

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

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
= admin_search_form_for @q, nil do |f|
2-
= f.input :name_cont, label: 'name contains'
3-
= f.input :delete_state, label: 'delete state', as: :select, collection: collection_for_delete_state, include_blank: 'Without deleted'
4-
= f.input :tagged, label: 'tagged with', as: :select, collection: collection_for_tags, input_html: { 'data-select' => true, multiple: true }
5-
- if Rails.env.development?
6-
= f.input :created_at_eq, as: :string, input_html: { name: '', value: @q.result.to_sql }, label: 'debug sql'
7-
.form-actions
8-
= f.submit_cancel 'Search'
2+
= f.input :name_cont, label: 'Name contains'
3+
= f.input :delete_state, as: :is_deleted
4+
= f.input :tagged, label: 'Tagged with', as: :tags
5+
= f.input :sql, as: :debug_sql
6+
= f.submit_cancel 'Search'
+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
= f.input :name
2-
= f.input :tag_list, label: 'Tags', as: :string, input_html: { 'data-select' => 'tags', 'data-tags' => collection_for_tags.to_json }
2+
= f.input :tag_list, label: 'Tags', as: :tags
33
= f.submit_cancel cancel: admin_categories_path

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@
2323
= render_admin_sorting_buttons(category)
2424
td
2525
- if category.deleted?
26-
= link_to 'restore', restore_admin_category_path(category), method: :post, 'data-confirm' => 'Restore?'
26+
= link_to 'Restore', restore_admin_category_path(category), method: :post, 'data-confirm' => 'Restore?'
2727
- else
28-
= link_to 'show', admin_category_path(category)
28+
= link_to 'Show', admin_category_path(category)
2929
| &nbsp; | &nbsp;
30-
= link_to 'edit', edit_admin_category_path(category)
30+
= link_to 'Edit', edit_admin_category_path(category)
3131
| &nbsp; | &nbsp;
32-
= link_to 'delete', admin_category_path(category), method: :delete, 'data-confirm' => 'Delete?'
32+
= link_to 'Delete', admin_category_path(category), method: :delete, 'data-confirm' => 'Delete?'

app/views/admin/categories/show.html.slim

+9
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@
77
tr
88
th Name
99
td= @category.name
10+
tr
11+
th Tags
12+
td
13+
- @category.tag_list.each do |tag_name|
14+
- class_name = cycle('default', 'primary', 'success', 'info', 'warning', 'danger')
15+
= link_to admin_categories_path(q: { tagged: [tag_name] }) do
16+
span.label class="label-#{class_name}"
17+
= tag_name
18+
| &nbsp;
1019
tr
1120
th Sort
1221
td= @category.sort
+4-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
= admin_search_form_for @q, nil do |f|
2-
= f.input :has_avatar, as: :switch, label: 'has avatar'
3-
= f.input :name_cont, label: 'name contains'
4-
= f.input :email_cont, label: 'email contains'
5-
- if Rails.env.development?
6-
= f.input :created_at_eq, as: :string, input_html: { name: '', value: @q.result.to_sql }, label: 'debug sql'
2+
= f.input :has_avatar, as: :switch, label: 'Has avatar'
3+
= f.input :name_cont, label: 'Name contains'
4+
= f.input :email_cont, label: 'Email contains'
5+
= f.input :sql, as: :debug_sql
76
= f.submit_cancel 'Search'

config/admin_setting.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@ defaults: &defaults
5050
items:
5151
-
5252
icon: 'bar-chart-o'
53-
label: Charts
54-
link: '/charts'
53+
label: Examples
54+
link: admin_examples
5555
tag:
56-
name: 'Coming Soon'
56+
name: 'Hot!'
5757
style: 'success'
5858

5959
development:

config/initializers/simple_form_admin.rb

+9-1
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,17 @@
3232
end
3333
end
3434

35+
config.wrappers :admin_radio, tag: :div, class: 'form-group', error_class: 'parsley-error' do |b|
36+
b.use :label, class: 'control-label col-md-3 col-sm-3 col-xs-12'
37+
b.wrapper tag: :div, class: 'col-md-9 col-sm-9 col-xs-12' do |ba|
38+
ba.use :input, class: 'flat', wrap_with: { tag: 'div', class: 'radio' }
39+
end
40+
end
41+
3542
config.wrapper_mappings = {
3643
boolean: :admin_boolean,
37-
switch: :admin_boolean_switch
44+
switch: :admin_boolean_switch,
45+
is_deleted: :admin_radio
3846
}
3947
end
4048

config/routes.rb

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
namespace :admin do
1616
root to: 'base#index', as: :root
17+
get '/examples', to: 'base#examples', as: :examples
1718
resources :users
1819
resources :categories do
1920
member do

0 commit comments

Comments
 (0)