auto_select2 0.2.0 → 0.2.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1959785b4c590ab8a899b2ae4d04ae92877bff2d
4
- data.tar.gz: ec11ae6b9ab6bea151a90327d444e9a1188283ea
3
+ metadata.gz: e3d6624af9d5f131eafc59d8ecfde5a5e9a9d558
4
+ data.tar.gz: d821780986f28b65b2de8c77ed95e47f937f14b8
5
5
  SHA512:
6
- metadata.gz: c51d13d9a225ccbff420e2aefacbafeb2ed86db076af426a701f75f5d4bd309c2744d556f356e38ceae2c3c1eda1f03f57a7180daa7cdee349dcdabfa3fa22c2
7
- data.tar.gz: a9de852f51f7573b7ae71d7c20ec17a7007cd49f0820659153cb674b3c1b2a8aefe6299a8f37829fe216da68d1d5e6d295d80ddbe2c481e7d9fcf46c9c64046a
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
- column = options[:column].present? ? options[:column] : 'name'
22
- conditions = default_search_conditions(term, options[:basic_conditions], column)
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(column)
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
- columns = [columns] unless columns.is_a?(Array)
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
- title_method = options[:title_method]
67
- id_column = options[:id_column] || searched_class.primary_key
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, title_method, id)
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, title_method, ids[0]) if item.present?
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, title_method, id)
87
- if item.respond_to?(:to_select2) && title_method.blank?
88
- item.to_select2
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
- title_method ||= :name
91
- { text: item.public_send(title_method), id: id }
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
- { text: default_value.public_send(options[:default_text_column]),
19
- id: default_value.public_send(options[:default_id_column]) }
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(default_arel, options[:item_ids], id_column: options[:default_id_column])
28
+ get_init_values(
29
+ default_arel,
30
+ options[:item_ids],
31
+ options
32
+ )
25
33
  end
26
34
  end
27
35
  end
@@ -1,3 +1,3 @@
1
1
  module AutoSelect2
2
- VERSION = '0.2.0'
2
+ VERSION = '0.2.1'
3
3
  end
@@ -1,9 +1,40 @@
1
1
  jQuery ($) ->
2
- format = (item)->
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
- result = '<span class="'+item.class_name+'">'+item.text+'</span>'
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: format
17
- formatResult: format
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.0
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-01 00:00:00.000000000 Z
12
+ date: 2014-12-03 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: railties