activeadmin-ajax_filter 0.2.2 → 0.3.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 46aa33c1ac28fdf69a6c7a312c77136f5f89f286
4
- data.tar.gz: 16ac2160318afdd2cdcb9e56f291d73647128935
3
+ metadata.gz: 63324cc53660dfec34a886466480f787eb108d04
4
+ data.tar.gz: 1abc7df986952ceadf6e36ef10d8bc62dde92744
5
5
  SHA512:
6
- metadata.gz: 7c5771600ec5d2aeb8e0ef815df17e504b85a6ca23539b6aa08a28405d6280e61356d417a9f2a3562c7f7266fbdd05ec0a5ae24562cad14c0e489bed22f4b8e0
7
- data.tar.gz: 4f7314e3bb8ebc891bfb5896eda8c9a7be0cab5149749d6a656fb5dc2c9cacf312e246aa17f440fa505dea81110018d5c496e6f729ed32a42bdd7fe2bb7ea16f
6
+ metadata.gz: 931319352afd8da9f10b6778d857d48afeaf9f82bf11669704a28eb968e72e2ed999848db9334bddf4c6eafaed985d665eabfd7d29e95397601e0bb32e02911f
7
+ data.tar.gz: 0dd71b339caa76438d8b327b52e655109a3d3be66ed9391de088346565d4535dd36f478c0e4c3b0843ac426360ffcdf6b37163b7b0b8750c3a7341869e58a735
data/README.md CHANGED
@@ -54,10 +54,20 @@ ActiveAdmin.register User do
54
54
  end
55
55
 
56
56
  # Main resource
57
+ # As a filter
57
58
  ActiveAdmin.register Invoice do
58
59
  filter :user, as: :ajax_select, data: { search_fields: [:email, :customer_uid], limit: 7 }
59
60
  # ...
60
61
  end
62
+
63
+ # As a form input
64
+ ActiveAdmin.register Invoice do
65
+ form do |f|
66
+ f.input :language # used by ajax_search_fields
67
+ f.input :user, as: :ajax_select, data: { search_fields: [:name], static_ransack: { active_eq: true }, ajax_search_fields: [:language_id] }
68
+ # ...
69
+ end
70
+ end
61
71
  ```
62
72
 
63
73
  You can use next parameters in `data` hash:
@@ -68,6 +78,8 @@ You can use next parameters in `data` hash:
68
78
  * `ordering` - sort string like `email ASC, customer_uid DESC`, by default it uses first value of `search_fields` with `ASC` direction
69
79
  * `ransack` - ransack query which will be applied, by default it's builded from `search_fields` with `or` and `contains` clauses, e.g.: `email_or_customer_uid_cont`
70
80
  * `url` - url for AJAX query by default is builded from field name
81
+ * `ajax_search_fields` - array of field names. `ajax_select` input depends on `ajax_search_fields` values: e.g. you can scope user by languages.
82
+ * `static_ransack` - hash of ransack predicates which will be applied statically and independently from current input field value
71
83
 
72
84
  ## Development
73
85
 
@@ -1,8 +1,16 @@
1
1
  $ ->
2
- $('.filter_ajax_select select').each (_, select) ->
2
+ $('.filter_ajax_select select, .ajax_select select').each (_, select) ->
3
3
  select = $(select)
4
4
  valueField = select.data('value-field')
5
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
+
6
14
  ordering = select.data('ordering')
7
15
  url = select.data('url')
8
16
 
@@ -20,6 +28,9 @@ $ ->
20
28
  success: (res) ->
21
29
  callback(res)
22
30
 
31
+ relatedInput = (field) ->
32
+ $("[name*=#{field}]", select.parents('form'))
33
+
23
34
  select.selectize
24
35
  valueField: valueField
25
36
  labelField: searchFields[0]
@@ -47,6 +58,15 @@ $ ->
47
58
  if query.length
48
59
  q = {}
49
60
  q[select.data('ransack')] = query
61
+
62
+ 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
+
50
70
  loadOptions(q, callback)
51
71
  else
52
72
  callback()
@@ -64,4 +84,8 @@ $ ->
64
84
  if res.length
65
85
  selectize.addOption(res[0])
66
86
  selectize.addItem(res[0][valueField])
67
- )
87
+ )
88
+
89
+ ajaxFields.forEach (field) ->
90
+ relatedInput(field).change ->
91
+ selectize.clearOptions()
@@ -10,4 +10,24 @@
10
10
  .primary, .secondary {
11
11
  display: block;
12
12
  }
13
+ }
14
+
15
+ .ajax_select {
16
+ .selectize-input {
17
+ width: 70%;
18
+ }
19
+
20
+ .selectize-dropdown {
21
+ .item {
22
+ border-bottom: light-gray 1px solid;
23
+ }
24
+
25
+ .primary {
26
+ font-weight: bold;
27
+ }
28
+
29
+ .primary, .secondary {
30
+ display: block;
31
+ }
32
+ }
13
33
  }
@@ -1,6 +1,8 @@
1
1
  require 'active_admin'
2
2
  require 'active_admin/ajax_filter/engine'
3
3
  require 'active_admin/ajax_filter/version'
4
+ require 'active_admin/inputs/ajax_core'
5
+ require 'active_admin/inputs/ajax_select_input'
4
6
  require 'active_admin/inputs/filters/ajax_select_input'
5
7
 
6
8
  module ActiveAdmin
@@ -1,5 +1,5 @@
1
1
  module ActiveAdmin
2
2
  module AjaxFilter
3
- VERSION = '0.2.2'
3
+ VERSION = '0.3.0'
4
4
  end
5
5
  end
@@ -0,0 +1,71 @@
1
+ module ActiveAdmin
2
+ module Inputs
3
+ module AjaxCore
4
+ DEFAULT_LIMIT = 5
5
+
6
+ def pluck_column
7
+ klass.reorder("#{method} asc").limit(collection_limit).uniq.pluck(method)
8
+ end
9
+
10
+ # def collection_from_association
11
+ # super.limit(collection_limit)
12
+ # end
13
+
14
+ def input_html_options
15
+ super.merge(
16
+ 'data-limit' => collection_limit,
17
+ 'data-value-field' => value_field,
18
+ 'data-search-fields' => search_fields,
19
+ 'data-ajax-search-fields' => ajax_search_fields,
20
+ 'data-ordering' => ordering,
21
+ 'data-ransack' => ransack,
22
+ 'data-static-ransack' => static_ransack,
23
+ 'data-selected-value' => selected_value,
24
+ 'data-url' => url,
25
+ )
26
+ end
27
+
28
+ def ajax_data
29
+ options[:data] || {}
30
+ end
31
+
32
+ def collection_limit
33
+ ajax_data[:limit] || DEFAULT_LIMIT
34
+ end
35
+
36
+ def value_field
37
+ ajax_data[:value_field] || :id
38
+ end
39
+
40
+ def search_fields
41
+ ajax_data[:search_fields] || raise(ArgumentError, 'search_fields in required')
42
+ end
43
+
44
+ def ajax_search_fields
45
+ ajax_data[:ajax_search_fields]
46
+ end
47
+
48
+ def ordering
49
+ ajax_data[:ordering] || "#{search_fields.first} ASC"
50
+ end
51
+
52
+ def ransack
53
+ ajax_data[:ransack] || "#{search_fields.join('_or_')}_cont"
54
+ end
55
+
56
+ def static_ransack
57
+ ajax_data.fetch(:static_ransack, {}).to_json
58
+ end
59
+
60
+ def url
61
+ ajax_data[:url] || "#{method.to_s.pluralize}/filter"
62
+ end
63
+
64
+ def selected_value
65
+ if @object
66
+ @object.try(:send, input_name)
67
+ end
68
+ end
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,7 @@
1
+ module ActiveAdmin
2
+ module Inputs
3
+ class AjaxSelectInput < ::Formtastic::Inputs::SelectInput
4
+ include ActiveAdmin::Inputs::AjaxCore
5
+ end
6
+ end
7
+ end
@@ -2,61 +2,11 @@ module ActiveAdmin
2
2
  module Inputs
3
3
  module Filters
4
4
  class AjaxSelectInput < SelectInput
5
- DEFAULT_LIMIT = 5
6
-
7
- def pluck_column
8
- klass.reorder("#{method} asc").limit(collection_limit).uniq.pluck(method)
9
- end
5
+ include ActiveAdmin::Inputs::AjaxCore
10
6
 
11
7
  def collection_from_association
12
8
  super.limit(collection_limit)
13
9
  end
14
-
15
- def input_html_options
16
- super.merge(
17
- 'data-limit' => collection_limit,
18
- 'data-value-field' => value_field,
19
- 'data-search-fields' => search_fields,
20
- 'data-ordering' => ordering,
21
- 'data-ransack' => ransack,
22
- 'data-selected-value' => selected_value,
23
- 'data-url' => url,
24
- )
25
- end
26
-
27
- def ajax_data
28
- options[:data] || {}
29
- end
30
-
31
- def collection_limit
32
- ajax_data[:limit] || DEFAULT_LIMIT
33
- end
34
-
35
- def value_field
36
- ajax_data[:value_field] || :id
37
- end
38
-
39
- def search_fields
40
- ajax_data[:search_fields] || raise(ArgumentError, 'search_fields in required')
41
- end
42
-
43
- def ordering
44
- ajax_data[:ordering] || "#{search_fields.first} ASC"
45
- end
46
-
47
- def ransack
48
- ajax_data[:ransack] || "#{search_fields.join('_or_')}_cont"
49
- end
50
-
51
- def url
52
- ajax_data[:url] || "#{method.to_s.pluralize}/filter"
53
- end
54
-
55
- def selected_value
56
- if @object
57
- @object.try(:send, input_name)
58
- end
59
- end
60
10
  end
61
11
  end
62
12
  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.2.2
4
+ version: 0.3.0
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-02-03 00:00:00.000000000 Z
11
+ date: 2016-06-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activeadmin
@@ -178,6 +178,8 @@ files:
178
178
  - lib/active_admin/ajax_filter.rb
179
179
  - lib/active_admin/ajax_filter/engine.rb
180
180
  - lib/active_admin/ajax_filter/version.rb
181
+ - lib/active_admin/inputs/ajax_core.rb
182
+ - lib/active_admin/inputs/ajax_select_input.rb
181
183
  - lib/active_admin/inputs/filters/ajax_select_input.rb
182
184
  - lib/activeadmin-ajax_filter.rb
183
185
  homepage: https://github.com/holyketzer/activeadmin-ajax_filter