auto_select2 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of auto_select2 might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/app/controllers/select2_autocompletes_controller.rb +1 -3
- data/lib/auto_select2/select2_search_adapter/base.rb +25 -13
- data/lib/auto_select2/select2_search_adapter/default.rb +11 -3
- data/lib/auto_select2/version.rb +1 -1
- data/vendor/assets/javascripts/auto_select2/ajax_select2.js.coffee +40 -4
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e3d6624af9d5f131eafc59d8ecfde5a5e9a9d558
|
4
|
+
data.tar.gz: d821780986f28b65b2de8c77ed95e47f937f14b8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 36ff5e67ac0dafd10e88dcad6015badfa87a634266c357ba2e2cba4aac2ae965200bebb3f5f9533071999c26c0fd0b55ca80e6861df1d0e514cc46e41e9eb79a
|
7
|
+
data.tar.gz: f5250c998ddde7236c7c0047fcb3c71d81d2c4b5252c215639d81feebc831e2b0058709a5052e9786dca1fdd623993994766f41e85fe311c2b58f9768dd1ff7b
|
@@ -3,9 +3,7 @@ class Select2AutocompletesController < ApplicationController
|
|
3
3
|
begin
|
4
4
|
if params[:class_name].present?
|
5
5
|
adapter = "::#{params[:class_name].camelize}SearchAdapter".constantize
|
6
|
-
elsif params[:default_class_name].present?
|
7
|
-
params[:default_text_column].present? &&
|
8
|
-
params[:default_id_column].present?
|
6
|
+
elsif params[:default_class_name].present?
|
9
7
|
adapter = ::AutoSelect2::Select2SearchAdapter::Default
|
10
8
|
else
|
11
9
|
render json: {error: "not enough search parameters'"}.to_json,
|
@@ -18,8 +18,8 @@ module AutoSelect2
|
|
18
18
|
private
|
19
19
|
|
20
20
|
def default_finder(searched_class, term, options)
|
21
|
-
|
22
|
-
conditions = default_search_conditions(term, options[:basic_conditions],
|
21
|
+
columns = options[:column].present? ? options[:column] : 'name'
|
22
|
+
conditions = default_search_conditions(term, options[:basic_conditions], columns)
|
23
23
|
if term.nil?
|
24
24
|
[ searched_class.where(options[:basic_conditions]) ]
|
25
25
|
else
|
@@ -28,7 +28,7 @@ module AutoSelect2
|
|
28
28
|
page = options[:page].to_i > 0 ? options[:page].to_i : 1
|
29
29
|
skip_count = limit * ( page - 1 )
|
30
30
|
end
|
31
|
-
query = searched_class.where( conditions ).limit( limit ).offset(skip_count).order(
|
31
|
+
query = searched_class.where( conditions ).limit( limit ).offset(skip_count).order(columns)
|
32
32
|
query = query.select(options[:select]) if options[:select].present?
|
33
33
|
options[:uniq] ? query.uniq : query
|
34
34
|
end
|
@@ -45,7 +45,9 @@ module AutoSelect2
|
|
45
45
|
def default_search_conditions(term, basic_conditions, columns)
|
46
46
|
term_filter = ''
|
47
47
|
conditions = []
|
48
|
-
|
48
|
+
unless columns.is_a?(Array)
|
49
|
+
columns = columns.split(/[\s,]+/)
|
50
|
+
end
|
49
51
|
unless term.nil?
|
50
52
|
words = term.split(' ')
|
51
53
|
words.each_with_index do |word, index|
|
@@ -63,32 +65,42 @@ module AutoSelect2
|
|
63
65
|
end
|
64
66
|
|
65
67
|
def get_init_values(searched_class, ids, options = {})
|
66
|
-
|
67
|
-
|
68
|
+
hash_method = options[:hash_method]
|
69
|
+
text_columns = options[:default_text_column]
|
70
|
+
id_column = options[:default_id_column] || searched_class.primary_key
|
68
71
|
ids = ids.split(',')
|
69
72
|
if ids.size > 1
|
70
73
|
result = []
|
71
74
|
ids.each do |id|
|
72
75
|
item = searched_class.where(id_column => id).first
|
73
76
|
if item.present?
|
74
|
-
result << get_select2_hash(item,
|
77
|
+
result << get_select2_hash(item, hash_method, id_column, text_columns)
|
75
78
|
end
|
76
79
|
end
|
77
80
|
result
|
78
81
|
elsif ids.size == 1
|
79
82
|
item = searched_class.where(id_column => ids[0]).first
|
80
|
-
get_select2_hash(item,
|
83
|
+
get_select2_hash(item, hash_method, id_column, text_columns) if item.present?
|
81
84
|
else
|
82
85
|
nil
|
83
86
|
end
|
84
87
|
end
|
85
88
|
|
86
|
-
def get_select2_hash(item,
|
87
|
-
if
|
88
|
-
item.
|
89
|
+
def get_select2_hash(item, hash_method, id_column, text_columns)
|
90
|
+
if hash_method.present? && item.respond_to?(hash_method)
|
91
|
+
item.public_send(hash_method)
|
89
92
|
else
|
90
|
-
|
91
|
-
|
93
|
+
if item.respond_to?(:to_select2)
|
94
|
+
item.to_select2
|
95
|
+
else
|
96
|
+
label_method = text_columns.split(/[\s,]+/).first
|
97
|
+
label_method ||= :name
|
98
|
+
if item.respond_to?(label_method)
|
99
|
+
{ text: item.public_send(label_method), id: item.public_send(id_column) }
|
100
|
+
else
|
101
|
+
return { error: 'not found label method' }
|
102
|
+
end
|
103
|
+
end
|
92
104
|
end
|
93
105
|
end
|
94
106
|
end
|
@@ -15,13 +15,21 @@ module AutoSelect2
|
|
15
15
|
default_count = default_count(default_arel, term, column: options[:default_text_column])
|
16
16
|
{
|
17
17
|
items: default_values.map do |default_value|
|
18
|
-
|
19
|
-
|
18
|
+
get_select2_hash(
|
19
|
+
default_value,
|
20
|
+
options[:hash_method],
|
21
|
+
options[:default_id_column],
|
22
|
+
options[:default_text_column]
|
23
|
+
)
|
20
24
|
end,
|
21
25
|
total: default_count
|
22
26
|
}
|
23
27
|
else
|
24
|
-
get_init_values(
|
28
|
+
get_init_values(
|
29
|
+
default_arel,
|
30
|
+
options[:item_ids],
|
31
|
+
options
|
32
|
+
)
|
25
33
|
end
|
26
34
|
end
|
27
35
|
end
|
data/lib/auto_select2/version.rb
CHANGED
@@ -1,9 +1,40 @@
|
|
1
1
|
jQuery ($) ->
|
2
|
-
|
2
|
+
itemResultCssClass = (item)->
|
3
|
+
cssClass = ''
|
4
|
+
if item.class_name != undefined
|
5
|
+
cssClass = item.class_name
|
6
|
+
cssClass
|
7
|
+
|
8
|
+
itemSelectionCssClass = (item)->
|
9
|
+
cssClass = ''
|
10
|
+
if item != null && item.selection_class_name != undefined
|
11
|
+
cssClass = item.selection_class_name
|
12
|
+
else if item != null
|
13
|
+
cssClass = itemResultCssClass(item)
|
14
|
+
cssClass
|
15
|
+
|
16
|
+
formatResult = (item)->
|
3
17
|
result = item.text
|
4
18
|
if item.class_name != undefined
|
5
|
-
|
19
|
+
classes = item.class_name.split(' ')
|
20
|
+
classes = classes.map (origClass)->
|
21
|
+
'in-' + origClass
|
22
|
+
result = '<span class="'+classes.join(' ')+'">'+item.text+'</span>'
|
6
23
|
result
|
24
|
+
|
25
|
+
formatSelection = (item)->
|
26
|
+
result = item.text
|
27
|
+
classes = item.selection_class_name
|
28
|
+
if classes == undefined
|
29
|
+
classes = item.class_name
|
30
|
+
if classes != undefined
|
31
|
+
classes = classes.split(' ')
|
32
|
+
# INFO: Turn on this if formatSelectionCssClass enabled
|
33
|
+
# classes = classes.map (origClass)->
|
34
|
+
# 'in-' + origClass
|
35
|
+
result = '<span class="'+classes.join(' ')+'">'+item.text+'</span>'
|
36
|
+
result
|
37
|
+
|
7
38
|
window.initAutoAjaxSelect2 = ->
|
8
39
|
$inputs = $('input.auto-ajax-select2').not('.select2-offscreen')
|
9
40
|
$inputs.each ->
|
@@ -13,8 +44,11 @@ jQuery ($) ->
|
|
13
44
|
s2DefaultOptions = {
|
14
45
|
allowClear: true
|
15
46
|
multiple: false
|
16
|
-
formatSelection:
|
17
|
-
formatResult:
|
47
|
+
formatSelection: formatSelection
|
48
|
+
formatResult: formatResult
|
49
|
+
# INFO: Not documented feature of select2 library, worked very well, but not clearing classes after item removing
|
50
|
+
# formatSelectionCssClass: itemSelectionCssClass
|
51
|
+
formatResultCssClass: itemResultCssClass
|
18
52
|
ajax: {
|
19
53
|
url: path,
|
20
54
|
dataType: 'json',
|
@@ -44,11 +78,13 @@ jQuery ($) ->
|
|
44
78
|
id = $element.val()
|
45
79
|
initText = $element.data('init-text')
|
46
80
|
initClassName = $element.data('init-class-name')
|
81
|
+
initSelectionClassName = $element.data('init-selection-class-name')
|
47
82
|
if (id != '')
|
48
83
|
if initText != undefined
|
49
84
|
params = { id: id, text: initText }
|
50
85
|
if initClassName != undefined
|
51
86
|
params.class_name = initClassName
|
87
|
+
params.selection_class_name = initSelectionClassName
|
52
88
|
callback(params)
|
53
89
|
else
|
54
90
|
$.ajax(path, {
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: auto_select2
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dmitriy Lisichkin
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2014-12-
|
12
|
+
date: 2014-12-03 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: railties
|