activeadmin-ajax_filter 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 63324cc53660dfec34a886466480f787eb108d04
4
- data.tar.gz: 1abc7df986952ceadf6e36ef10d8bc62dde92744
3
+ metadata.gz: 3388e55ea6dd77ff8bd2a7431a5b4bb4f0c402bc
4
+ data.tar.gz: b950b9818a0edb6c6d37df9bc104fef3713dfa8e
5
5
  SHA512:
6
- metadata.gz: 931319352afd8da9f10b6778d857d48afeaf9f82bf11669704a28eb968e72e2ed999848db9334bddf4c6eafaed985d665eabfd7d29e95397601e0bb32e02911f
7
- data.tar.gz: 0dd71b339caa76438d8b327b52e655109a3d3be66ed9391de088346565d4535dd36f478c0e4c3b0843ac426360ffcdf6b37163b7b0b8750c3a7341869e58a735
6
+ metadata.gz: d1f2c12fcd0bc4dd43f3f93803296056279284f976779965de57c93650a3a75c693d37ad6a2fdab3b402b0b3891b705a4d61d7f6ca85802c256c282b5aaa35cc
7
+ data.tar.gz: fa69bf317340e2905118a0f2d9b00e6a701b8c55c73f69e09fba59eebaecf4cdd788e82b1d8fb8ea942ed3dd4c33cd2e26d7c78a165fbee8857f1cbfe7b7b955
@@ -1,91 +1,99 @@
1
1
  $ ->
2
- $('.filter_ajax_select select, .ajax_select select').each (_, select) ->
3
- select = $(select)
4
- valueField = select.data('value-field')
5
- searchFields = select.data('search-fields').split(' ')
6
- staticRansack = select.data('static-ransack')
7
-
8
- ajaxFields = select.data('ajax-search-fields')
9
- if ajaxFields
10
- ajaxFields = ajaxFields.split(' ')
11
- else
12
- ajaxFields = []
13
-
14
- ordering = select.data('ordering')
15
- url = select.data('url')
16
-
17
- loadOptions = (q, callback) ->
18
- $.ajax
19
- url: url
20
- type: 'GET'
21
- dataType: 'json'
22
- data:
23
- q: q
24
- limit: select.data('limit')
25
- order: ordering
26
- error: ->
27
- callback()
28
- success: (res) ->
29
- callback(res)
30
-
31
- relatedInput = (field) ->
32
- $("[name*=#{field}]", select.parents('form'))
33
-
34
- select.selectize
35
- valueField: valueField
36
- labelField: searchFields[0]
37
- searchField: searchFields
38
- sortField: ordering.split(',').map (clause)->
39
- c = clause.trim().split(' ')
40
- { field: c[0], direction: c[1] }
41
- options: []
42
- create: false
43
- render:
44
- option: (item, escape) ->
45
- html = searchFields.map (field, index)->
46
- value = escape(item[field])
47
-
48
- if index == 0
49
- klass = 'primary'
50
- else
51
- klass = 'secondary'
52
-
53
- "<span class='#{klass}'>#{value}</span>"
54
-
55
- "<div class='item'>#{html.join('')}</div>"
56
-
57
- load: (query, callback) ->
58
- if query.length
59
- q = {}
60
- q[select.data('ransack')] = query
2
+ apply_filter_ajax_select = () ->
3
+ $('.filter_ajax_select select, .ajax_select select').each (_, select) ->
4
+ select = $(select)
5
+ valueField = select.data('value-field')
6
+ searchFields = select.data('search-fields').split(' ')
7
+ staticRansack = select.data('static-ransack')
8
+
9
+ ajaxFields = select.data('ajax-search-fields')
10
+ if ajaxFields
11
+ ajaxFields = ajaxFields.split(' ')
12
+ else
13
+ ajaxFields = []
14
+
15
+ ordering = select.data('ordering')
16
+ url = select.data('url')
17
+
18
+ loadOptions = (q, callback) ->
19
+ $.ajax
20
+ url: url
21
+ type: 'GET'
22
+ dataType: 'json'
23
+ data:
24
+ q: q
25
+ limit: select.data('limit')
26
+ order: ordering
27
+ error: ->
28
+ callback()
29
+ success: (res) ->
30
+ callback(res)
31
+
32
+ relatedInput = (field) ->
33
+ $("[name*=#{field}]", select.parents('form'))
34
+
35
+ select.selectize
36
+ valueField: valueField
37
+ labelField: searchFields[0]
38
+ searchField: searchFields
39
+ sortField: ordering.split(',').map (clause)->
40
+ c = clause.trim().split(' ')
41
+ { field: c[0], direction: c[1] }
42
+ options: []
43
+ create: false
44
+ render:
45
+ option: (item, escape) ->
46
+ html = searchFields.map (field, index)->
47
+ value = escape(item[field])
48
+
49
+ if index == 0
50
+ klass = 'primary'
51
+ else
52
+ klass = 'secondary'
53
+
54
+ "<span class='#{klass}'>#{value}</span>"
55
+
56
+ "<div class='item'>#{html.join('')}</div>"
57
+
58
+ load: (query, callback) ->
59
+ if query.length
60
+ q = {}
61
+ q[select.data('ransack')] = query
62
+
63
+ ajaxFields.forEach (field) ->
64
+ q["#{field}_eq"] = relatedInput(field).val()
65
+ # clear cache because it wrong with changing values of ajaxFields
66
+ select.loadedSearches = {}
67
+
68
+ for ransack, value of staticRansack
69
+ q[ransack] = value
70
+
71
+ loadOptions(q, callback)
72
+ else
73
+ callback()
74
+
75
+ onInitialize: ->
76
+ selectize = this
77
+ selectedValue = select.data('selected-value')
78
+ selectedRansack = "#{valueField}_eq"
79
+
80
+ if selectedValue
81
+ q = {}
82
+ q[selectedRansack] = selectedValue
83
+
84
+ loadOptions(q, (res)->
85
+ if res.length
86
+ selectize.addOption(res[0])
87
+ selectize.addItem(res[0][valueField])
88
+ )
61
89
 
62
90
  ajaxFields.forEach (field) ->
63
- q["#{field}_eq"] = relatedInput(field).val()
64
- # clear cache because it wrong with changing values of ajaxFields
65
- select.loadedSearches = {}
66
-
67
- for ransack, value of staticRansack
68
- q[ransack] = value
69
-
70
- loadOptions(q, callback)
71
- else
72
- callback()
73
-
74
- onInitialize: ->
75
- selectize = this
76
- selectedValue = select.data('selected-value')
77
- selectedRansack = "#{valueField}_eq"
78
-
79
- if selectedValue
80
- q = {}
81
- q[selectedRansack] = selectedValue
82
-
83
- loadOptions(q, (res)->
84
- if res.length
85
- selectize.addOption(res[0])
86
- selectize.addItem(res[0][valueField])
87
- )
88
-
89
- ajaxFields.forEach (field) ->
90
- relatedInput(field).change ->
91
- selectize.clearOptions()
91
+ relatedInput(field).change ->
92
+ selectize.clearOptions()
93
+
94
+ # apply ajax filter to all static inputs
95
+ apply_filter_ajax_select()
96
+
97
+ # apply ajax filter on inputs inside has_many entries
98
+ $("form.formtastic .has_many_add").click ->
99
+ setTimeout((-> apply_filter_ajax_select()), 0)
@@ -16,7 +16,7 @@ module ActiveAdmin
16
16
  collection_action :filter, method: :get do
17
17
  scope = collection.ransack(params[:q]).result
18
18
 
19
- render json: scope.order(params[:order]).limit(params[:limit] || 10)
19
+ render text: scope.order(params[:order]).limit(params[:limit] || 10).to_json
20
20
  end
21
21
  end
22
22
  end
@@ -1,5 +1,5 @@
1
1
  module ActiveAdmin
2
2
  module AjaxFilter
3
- VERSION = '0.3.0'
3
+ VERSION = '0.3.1'
4
4
  end
5
5
  end
@@ -7,10 +7,6 @@ module ActiveAdmin
7
7
  klass.reorder("#{method} asc").limit(collection_limit).uniq.pluck(method)
8
8
  end
9
9
 
10
- # def collection_from_association
11
- # super.limit(collection_limit)
12
- # end
13
-
14
10
  def input_html_options
15
11
  super.merge(
16
12
  'data-limit' => collection_limit,
@@ -33,6 +29,10 @@ module ActiveAdmin
33
29
  ajax_data[:limit] || DEFAULT_LIMIT
34
30
  end
35
31
 
32
+ def collection_from_association
33
+ super.try(:limit, collection_limit)
34
+ end
35
+
36
36
  def value_field
37
37
  ajax_data[:value_field] || :id
38
38
  end
@@ -3,10 +3,6 @@ module ActiveAdmin
3
3
  module Filters
4
4
  class AjaxSelectInput < SelectInput
5
5
  include ActiveAdmin::Inputs::AjaxCore
6
-
7
- def collection_from_association
8
- super.limit(collection_limit)
9
- end
10
6
  end
11
7
  end
12
8
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activeadmin-ajax_filter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alex Emelyanov
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-06-14 00:00:00.000000000 Z
11
+ date: 2016-06-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activeadmin