effective_datatables 4.9.0 → 4.9.4

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
  SHA256:
3
- metadata.gz: 9344caa15ba3ff549a07b18f5d9cbcb8d9504216d4ba6184e9f591146a8340b2
4
- data.tar.gz: 6b998d2a6d9f1c302005c509450b83d214ddddc27a9902e6748b2518ea618531
3
+ metadata.gz: 768310a5434c778a3e11512125d661bc4ce443a277bb7e15581e50224b81c48f
4
+ data.tar.gz: 6b05137c54ef11ce9801b2e1fc48ee525641d2dd5507b7259e80c3387335404b
5
5
  SHA512:
6
- metadata.gz: 2117dd7f78e7329393504394126083d57da9ec7b7f5666b4d0931cbe7ed53aebfe359c696a6cd051559a5d15101715d656daa255300049a351728e3febc38def
7
- data.tar.gz: 247561bc9c06a9450710f4743cb24c2ba243c64393122763988acb51932cdf2da839fface55ea4705afd8cd7322aa10b27655ec780ce128ed77cab1d0fc04aec
6
+ metadata.gz: edf03e2147e95afd515bc357ac5436dca96c0d3f1aa4fd36ac040bc1cc0885a1a34a0d6ff69897b61bafe8542b46447bc59adc96d42167fd4efc6b98cd65006b
7
+ data.tar.gz: 7027f034cee83b584d50290a34674295b3aca4d7b6cb0121ebd1dd052833ffdfaa0be50e26d493d736c8d8e7730aa89dc666b42c0e1219ba33c6ec495f9c01da
data/README.md CHANGED
@@ -197,6 +197,13 @@ class PostsDatatable < Effective::Datatable
197
197
  # POSTs to the given url with params[:ids], an Array of ids for all selected rows
198
198
  # These actions are assumed to change the underlying collection
199
199
  bulk_action 'Approve all', bulk_approve_posts_path, data: { confirm: 'Approve all selected posts?' }
200
+ # GETs to the given url. Pass the ids via cookie, encoded in url, or LocalStorage.
201
+ # These actions are assumed to redirect the user to some other page.
202
+ bulk_action 'Action 1 | ids encoded in params', action_1_posts_path, data: { method: :get }
203
+
204
+ bulk_action 'Action 2 | ids stored in _ids_ field in local storage', action_2_posts_path, data: { 'payload-mode' => 'local-storage', method: :get }
205
+
206
+ bulk_action 'Action 3 | ids stored in _ids_ field in a cookie', action_3_posts_path, data: { 'payload-mode' => 'cookie', method: :get }
200
207
  bulk_action_divider
201
208
  bulk_action 'Destroy all', bulk_destroy_posts_path, data: { confirm: 'Destroy all selected posts?' }
202
209
  end
@@ -772,6 +779,12 @@ You can also specify `data-method: :get` to instead make a `GET` request with th
772
779
  ```ruby
773
780
  bulk_actions do
774
781
  bulk_action 'Approve all', bulk_approve_posts_path, data: { confirm: 'Approve all selected posts?' }
782
+
783
+ bulk_action 'Action 1 | ids encoded in params', action_1_posts_path, data: { method: :get }
784
+
785
+ bulk_action 'Action 2 | ids stored in _ids_ field in local storage', action_2_posts_path, data: { 'payload-mode' => 'local-storage', method: :get }
786
+
787
+ bulk_action 'Action 3 | ids stored in _ids_ field in a cookie', action_3_posts_path, data: { 'payload-mode' => 'cookie', method: :get }
775
788
  end
776
789
  ```
777
790
 
@@ -781,6 +794,9 @@ In your `routes` file:
781
794
  resources :posts do
782
795
  collection do
783
796
  post :bulk_approve
797
+ get :action_1
798
+ get :action_2
799
+ get :action_3
784
800
  end
785
801
  end
786
802
  ```
@@ -799,6 +815,22 @@ def bulk_approve
799
815
  render json: { status: 500, message: 'An error occured while approving a post.' }
800
816
  end
801
817
  end
818
+
819
+ def action_1
820
+ @posts = Post.where(id: params[:ids])
821
+
822
+ render :some_partial
823
+ end
824
+
825
+ def action_2
826
+ @posts = Post.where(id: cookies[:ids].split(','))
827
+
828
+ render :some_partial
829
+ end
830
+
831
+ def action_3
832
+ render :some_partial # and get ids via JS: localStorage.getItem('ids');
833
+ end
802
834
  ```
803
835
 
804
836
  or if using [effective_resources](https://github.com/code-and-effect/effective_resources):
@@ -41,11 +41,25 @@ restoreSelected = ($table, selected) ->
41
41
 
42
42
  if present then $bulkActions.removeAttr('disabled') else $bulkActions.attr('disabled', 'disabled')
43
43
 
44
- #### Bulk Action link behaviour
44
+ # rails_ujs data-confirm requires special attention
45
+ # https://github.com/rails/rails/blob/main/actionview/app/assets/javascripts/rails-ujs/features/confirm.coffee
46
+ $(document).on 'confirm:complete', '.dataTables_wrapper .buttons-bulk-actions a', (event) ->
47
+ if event.originalEvent.detail && event.originalEvent.detail[0] == true
48
+ doBulkActionPost(event)
49
+ (window.Rails || $.rails).stopEverything(event)
50
+ return false
51
+
45
52
  $(document).on 'click', '.dataTables_wrapper .buttons-bulk-actions a', (event) ->
46
- event.preventDefault() # prevent the click
53
+ unless $(event.currentTarget).data('confirm')
54
+ doBulkActionPost(event)
55
+ event.preventDefault()
56
+
57
+ doBulkActionPost = (event) ->
58
+ $bulkAction = $(event.currentTarget) # This is the regular <a href=...> tag maybe with data-confirm
59
+
60
+ document.cookie = 'ids=; expires=Thu, 01 Jan 1970 00:00:00 GMT'
61
+ localStorage.removeItem('ids')
47
62
 
48
- $bulkAction = $(event.currentTarget) # This is a regular <a href=...> tag
49
63
  $wrapper = $bulkAction.closest('.dataTables_wrapper')
50
64
  $table = $wrapper.find('table.dataTable').first()
51
65
  $processing = $table.siblings('.dataTables_processing').first()
@@ -54,6 +68,7 @@ $(document).on 'click', '.dataTables_wrapper .buttons-bulk-actions a', (event) -
54
68
  url = $bulkAction.attr('href')
55
69
  title = $bulkAction.text()
56
70
  download = $bulkAction.data('bulk-download')
71
+ payload_mode = $bulkAction.data('payload-mode')
57
72
  token = $table.data('authenticity-token')
58
73
  values = $.map($selected, (input) -> input.getAttribute('value'))
59
74
  method = $bulkAction.data('ajax-method')
@@ -61,10 +76,17 @@ $(document).on 'click', '.dataTables_wrapper .buttons-bulk-actions a', (event) -
61
76
  return unless url && values
62
77
 
63
78
  if method == 'GET'
64
- if url.includes('?')
65
- window.location.assign(url + '&' + $.param({ids: values}))
79
+ if payload_mode == 'cookie'
80
+ document.cookie = "ids=#{values}";
81
+ window.location.assign(url)
82
+ else if payload_mode == 'local-storage'
83
+ localStorage.setItem('ids', values);
84
+ window.location.assign(url)
66
85
  else
67
- window.location.assign(url + '?' + $.param({ids: values}))
86
+ if url.includes('?')
87
+ window.location.assign(url + '&' + $.param({ids: values}))
88
+ else
89
+ window.location.assign(url + '?' + $.param({ids: values}))
68
90
 
69
91
  return
70
92
 
@@ -64,7 +64,7 @@ module EffectiveDatatablesHelper
64
64
  }
65
65
  }
66
66
 
67
- if (charts || filters)
67
+ retval = if (charts || filters)
68
68
  output = ''.html_safe
69
69
 
70
70
  if charts
@@ -85,6 +85,10 @@ module EffectiveDatatablesHelper
85
85
  locals: { datatable: datatable, effective_datatable_params: effective_datatable_params }
86
86
  )
87
87
  end
88
+
89
+ Rails.logger.info(" Rendered datatable #{datatable.class} #{datatable.source_location}")
90
+
91
+ retval
88
92
  end
89
93
 
90
94
  def render_inline_datatable(datatable)
@@ -21,6 +21,9 @@ module Effective
21
21
  # The view
22
22
  attr_reader :view
23
23
 
24
+ # Set by DSL so we can track where this datatable is coming from
25
+ attr_accessor :source_location
26
+
24
27
  extend Effective::EffectiveDatatable::Dsl
25
28
 
26
29
  include Effective::EffectiveDatatable::Attributes
@@ -10,7 +10,7 @@ module Effective
10
10
 
11
11
  def load_cookie!
12
12
  return unless EffectiveDatatables.save_state
13
- return unless view.respond_to?(:cookies)
13
+ return unless (view.cookies rescue false) # Rails 6.1 view doesn't respond_to?(:cookies)
14
14
 
15
15
  @dt_cookie = view.cookies.signed['_effective_dt']
16
16
 
@@ -34,7 +34,7 @@ module Effective
34
34
 
35
35
  def save_cookie!
36
36
  return unless EffectiveDatatables.save_state
37
- return unless view.respond_to?(:cookies)
37
+ return unless (view.cookies rescue false)
38
38
 
39
39
  @dt_cookie ||= []
40
40
  @dt_cookie << [cookie_key, cookie_payload]
@@ -24,6 +24,8 @@ module Effective
24
24
  dsl_tool.in_datatables_do_block = true
25
25
  dsl_tool.instance_exec(&block)
26
26
  dsl_tool.in_datatables_do_block = false
27
+
28
+ self.source_location = block.source_location.first if block.respond_to?(:source_location)
27
29
  end
28
30
  end
29
31
 
@@ -1,3 +1,3 @@
1
1
  module EffectiveDatatables
2
- VERSION = '4.9.0'.freeze
2
+ VERSION = '4.9.4'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: effective_datatables
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.9.0
4
+ version: 4.9.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Code and Effect
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-03-23 00:00:00.000000000 Z
11
+ date: 2021-09-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails