auto_select2 0.1.1 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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 +16 -0
- data/auto_select2.gemspec +1 -1
- data/config/routes.rb +4 -0
- data/lib/auto_select2.rb +1 -0
- data/lib/auto_select2/version.rb +1 -1
- data/lib/select2_search_adapter/base.rb +97 -0
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a68cb3144bf87121bda7928fbd8a21924253eafb
|
4
|
+
data.tar.gz: ed7470fa356f8003f5bd3f259ee4bb73da03f908
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ea72345387f6253f7ab2fe1c3b25d30ef0a0587d2358cf292747b5f71a64e188a6e5c12bb3cccce15f887efb7085d28d79fc106cd106c6a3faccb18db012ca73
|
7
|
+
data.tar.gz: 427c82d917c6accd35c66fa5fc908eb66060870ecabfd8d87f405be0eaa697095bb8e8ecc340437b88155a3b58c5c8fbafb2c3ce67f8f809895df0d5831c82f5
|
@@ -0,0 +1,16 @@
|
|
1
|
+
class Select2AutocompletesController < ApplicationController
|
2
|
+
def search
|
3
|
+
begin
|
4
|
+
adapter = "::Select2SearchAdapter::#{params[:class_name].camelize}SearchAdapter".constantize
|
5
|
+
rescue NameError
|
6
|
+
render json: {error: "not found search adapter for '#{params[:class_name]}'"}.to_json,
|
7
|
+
status: 500
|
8
|
+
return
|
9
|
+
end
|
10
|
+
|
11
|
+
term = params.delete(:term)
|
12
|
+
page = params.delete(:page)
|
13
|
+
search_method = params.delete(:search_method)
|
14
|
+
render json: adapter.search_from_autocomplete(term, page, search_method, params).to_json
|
15
|
+
end
|
16
|
+
end
|
data/auto_select2.gemspec
CHANGED
@@ -14,7 +14,7 @@ Gem::Specification.new do |spec|
|
|
14
14
|
static, ajax and multi-ajax. Moreover this gem is foundation for other gems.
|
15
15
|
For example for AutoSelect2Tab.
|
16
16
|
DESC
|
17
|
-
spec.homepage = "
|
17
|
+
spec.homepage = "https://github.com/Loriowar/auto_select2"
|
18
18
|
spec.license = "MIT"
|
19
19
|
|
20
20
|
spec.files = `git ls-files`.split($/)
|
data/config/routes.rb
ADDED
data/lib/auto_select2.rb
CHANGED
data/lib/auto_select2/version.rb
CHANGED
@@ -0,0 +1,97 @@
|
|
1
|
+
module Select2SearchAdapter
|
2
|
+
class Base
|
3
|
+
class << self
|
4
|
+
# Amount rows per ajax-request
|
5
|
+
def limit
|
6
|
+
25 # TODO: move to settings/config
|
7
|
+
end
|
8
|
+
|
9
|
+
def search_from_autocomplete(term, page, search_method, options)
|
10
|
+
if search_method.nil?
|
11
|
+
search_default(term, page, options)
|
12
|
+
else
|
13
|
+
self.send("search_#{search_method}", term, page, options)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def default_finder(searched_class, term, options)
|
20
|
+
column = options[:column].present? ? options[:column] : 'name'
|
21
|
+
conditions = default_search_conditions(term, options[:basic_conditions], column)
|
22
|
+
if term.nil?
|
23
|
+
[ searched_class.where(options[:basic_conditions]) ]
|
24
|
+
else
|
25
|
+
skip_count = 0
|
26
|
+
unless options.nil? || options[:page].nil?
|
27
|
+
page = options[:page].to_i > 0 ? options[:page].to_i : 1
|
28
|
+
skip_count = limit * ( page - 1 )
|
29
|
+
end
|
30
|
+
query = searched_class.where( conditions ).limit( limit ).offset(skip_count).order(column)
|
31
|
+
query = query.select(options[:select]) if options[:select].present?
|
32
|
+
options[:uniq] ? query.uniq : query
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def default_count(searched_class, term, options = {})
|
37
|
+
conditions = default_search_conditions(term, options[:basic_conditions], options[:column] || 'name')
|
38
|
+
query = searched_class.where(conditions)
|
39
|
+
query = query.select(options[:select]) if options[:select].present?
|
40
|
+
query = options[:uniq] ? query.uniq : query
|
41
|
+
query.count
|
42
|
+
end
|
43
|
+
|
44
|
+
def default_search_conditions(term, basic_conditions, columns)
|
45
|
+
term_filter = ''
|
46
|
+
conditions = []
|
47
|
+
columns = [columns] unless columns.is_a?(Array)
|
48
|
+
unless term.nil?
|
49
|
+
words = term.split(' ')
|
50
|
+
words.each_with_index do |word, index|
|
51
|
+
term_filter += ' AND ' if index > 0
|
52
|
+
columns.each_with_index do |column, idx|
|
53
|
+
term_filter += ' OR ' if idx > 0
|
54
|
+
term_filter += "#{column} LIKE ?"
|
55
|
+
conditions << "%#{word}%"
|
56
|
+
end
|
57
|
+
end
|
58
|
+
term_filter = term_filter.empty? ? nil : "(#{term_filter})"
|
59
|
+
basic_conditions_part = basic_conditions.present? ? "(#{basic_conditions }) " : nil
|
60
|
+
conditions.unshift([term_filter, basic_conditions_part].compact.join(' AND '))
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def get_init_values(searched_class, ids, title_method=nil)
|
65
|
+
ids = ids.split(',')
|
66
|
+
if ids.size > 1
|
67
|
+
result = []
|
68
|
+
ids.each do |id|
|
69
|
+
item = searched_class.find_by_id(id)
|
70
|
+
if item.present?
|
71
|
+
result << get_select2_hash(item, title_method, id)
|
72
|
+
end
|
73
|
+
end
|
74
|
+
result
|
75
|
+
elsif ids.size == 1
|
76
|
+
item = searched_class.find_by_id(ids[0])
|
77
|
+
if item.present?
|
78
|
+
get_select2_hash(item, title_method, ids[0])
|
79
|
+
else
|
80
|
+
nil
|
81
|
+
end
|
82
|
+
else
|
83
|
+
nil
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
def get_select2_hash(item, title_method, id)
|
88
|
+
if item.respond_to?(:to_select2) && title_method.nil?
|
89
|
+
item.to_select2
|
90
|
+
else
|
91
|
+
title_method ||= :name
|
92
|
+
{ text: item.send(title_method), id: id }
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
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.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dmitriy Lisichkin
|
@@ -112,15 +112,18 @@ files:
|
|
112
112
|
- LICENSE.txt
|
113
113
|
- README.md
|
114
114
|
- Rakefile
|
115
|
+
- app/controllers/select2_autocompletes_controller.rb
|
115
116
|
- auto_select2.gemspec
|
117
|
+
- config/routes.rb
|
116
118
|
- lib/auto_select2.rb
|
117
119
|
- lib/auto_select2/helpers.rb
|
118
120
|
- lib/auto_select2/helpers/select2_helpers.rb
|
119
121
|
- lib/auto_select2/version.rb
|
122
|
+
- lib/select2_search_adapter/base.rb
|
120
123
|
- vendor/assets/javascripts/auto_select2/ajax_select2.js.coffee
|
121
124
|
- vendor/assets/javascripts/auto_select2/multi_ajax_select2_value_parser.js.coffee
|
122
125
|
- vendor/assets/javascripts/auto_select2/static_select2.js.coffee
|
123
|
-
homepage:
|
126
|
+
homepage: https://github.com/Loriowar/auto_select2
|
124
127
|
licenses:
|
125
128
|
- MIT
|
126
129
|
metadata: {}
|