ransack_memory 0.0.8 → 0.1.0

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: 78381a44c0c26cf03921807306297dda0f139738
4
- data.tar.gz: 9ad84df843202fbb9d70cd3a4ce881fa8a2d179e
3
+ metadata.gz: ee63c846270102691c62cd2a7af77912d7aa7651
4
+ data.tar.gz: 8dd719ca19c83f10078157448a329f56ed959d9b
5
5
  SHA512:
6
- metadata.gz: 8f1c3810e4a3731a04a51a135ef57e157971d7d5ee50ee4a6a0e87bfe2655ee3de3bc0e67074df0943552abe2a21ef4bf9bd5fce9ee14c8ec3011009944f7c47
7
- data.tar.gz: 8ea4ea7d235af00d2717c500890c573ceed214ac797063fb4c5850f3cbf59f5d58ff2c309ec43abba40a4afc9ac850f2442632fbe8c26b23d6ab0d25c2677651
6
+ metadata.gz: 444c11b00620bd325023254a05b48ea80bd53dd527164c0a6db96dc880bbf9098e0049fc49e7ab68ea3e98369febb53db37571e00497a8ba9e7df14b6f789c90
7
+ data.tar.gz: fdd5cebf99c78325ad1f0744fd41ffbdf03f5a4d617dd0434e767e969cc2b84ebca9326ce1dd3e5024400c3b383e49e3c988e7a7538a07dead13344bc84534ab
data/README.md CHANGED
@@ -9,6 +9,12 @@ Add this line to your application's Gemfile:
9
9
  gem 'ransack_memory'
10
10
  ```
11
11
 
12
+ Run the generator
13
+
14
+ ```shell
15
+ rails generate ransack_memory
16
+ ```
17
+
12
18
  Add this line to your basic controller (typically ApplicationController):
13
19
 
14
20
  ```ruby
@@ -27,12 +33,12 @@ Add this in your views where you have search forms. This is clear button, which
27
33
  You can pass any of link attributes:
28
34
 
29
35
  ```erb
30
- <%= clear_filter, title: 'Clear Filter', class: 'btn btn-primary', data: {confirm: 'Really?', my_data: 'something'} %>
36
+ <%= clear_filter title: 'Clear Filter', class: 'btn btn-primary', data: {confirm: 'Really?', my_data: 'something'} %>
31
37
  ```
32
38
 
33
39
  ## Configuration
34
40
 
35
- Create file in config/initializers/ransack_memory.rb with this content:
41
+ Running ```rails generate ransack_memory``` the console will generate `config/initializers/ransack_memory.rb` with this content:
36
42
 
37
43
  ```ruby
38
44
  RansackMemory::Core.config = {
@@ -41,11 +47,25 @@ RansackMemory::Core.config = {
41
47
  }
42
48
  ```
43
49
 
44
- Or you can generate this config file by running ```rails generate ransack_memory``` in console.
50
+ ## Load saved filters from another controller action
51
+
52
+ In some cases, you want to load saved filters from another controller action. If so, you just create in the same controller this method:
53
+
54
+ ```ruby
55
+ def set_session_key_identifier
56
+ 'projects_index_html' if action_name == 'my_another_action'
57
+ end
58
+ ```
59
+ this means that Ransack Memory load all filters that have been saved in action projects/index.html and load them into projects/my_another_action. This method must be public, not in private section!
60
+ Standard session key building is: ```"#{controller_name}_#{action_name}_#{request.format}"``` so you can load saved filters in any controller action like that.
45
61
 
46
62
  ## Kaminari issue
47
63
 
48
- When you have an issue with Kaminari gem, that you can't go back to the first page, update your kaminari view in `app/views/kaminari/_first_page.html.erb`:
64
+ When you have an issue with Kaminari gem, that you can't go back to the first page, generate a kaminari configuration in the initializers folder
65
+ and set ```config.params_on_first_page = true```.
66
+
67
+ As an alternative update your kaminari view in `app/views/kaminari/_first_page.html.erb`:
49
68
  ```erb
50
69
  <%= link_to_unless current_page.first?, t('views.pagination.first').html_safe, url_for(params.merge({page: 1, cancel_filter: nil})), remote: remote, class: 'btn btn-secondary' %>
51
70
  ```
71
+ However beware that this will probably lead to problems in later Rails versions due to a change in the way it allows you to merge parameters.
@@ -3,12 +3,15 @@ module RansackMemory
3
3
  extend ActiveSupport::Concern
4
4
 
5
5
  def save_and_load_filters
6
+ user_set_key_identifier = respond_to?(:set_session_key_identifier) ? send(:set_session_key_identifier) : nil
7
+
6
8
  session_key_identifier = ::RansackMemory::Core.config[:session_key_format]
7
- .gsub('%controller_name%', controller_name)
8
- .gsub('%action_name%', action_name)
9
- .gsub('%request_format%', request.format.symbol.to_s)
9
+ .gsub('%controller_name%', controller_name)
10
+ .gsub('%action_name%', action_name)
11
+ .gsub('%request_format%', request.format.symbol.to_s)
10
12
 
11
- session_key_base = "ranmemory_#{session_key_identifier}"
13
+ session_key_base = user_set_key_identifier.presence || "ranmemory_#{session_key_identifier}"
14
+ session_key_base = "ranmemory_#{session_key_base}" unless session_key_base.starts_with?('ranmemory')
12
15
 
13
16
  # permit search params
14
17
  params[::RansackMemory::Core.config[:param]].permit! if params[::RansackMemory::Core.config[:param]].present? && params[::RansackMemory::Core.config[:param]].respond_to?(:permit)
@@ -21,7 +24,7 @@ module RansackMemory
21
24
  end
22
25
 
23
26
  # search term saving
24
- session["#{session_key_base}"] = params[::RansackMemory::Core.config[:param]] if params[::RansackMemory::Core.config[:param]].present?
27
+ session["#{session_key_base}"] = params[::RansackMemory::Core.config[:param]].to_h if params[::RansackMemory::Core.config[:param]].present?
25
28
 
26
29
  # page number saving
27
30
  session["#{session_key_base}_page"] = params[:page] if params[:page].present?
@@ -30,7 +33,7 @@ module RansackMemory
30
33
  session["#{session_key_base}_per_page"] = params[:per_page] if params[:per_page].present?
31
34
 
32
35
  # search term load
33
- params[::RansackMemory::Core.config[:param]] = session["#{session_key_base}"].presence
36
+ params[::RansackMemory::Core.config[:param]] = session["#{session_key_base}"] if session["#{session_key_base}"].present?
34
37
 
35
38
  # page number load
36
39
  params[:page] = session["#{session_key_base}_page"].presence
@@ -39,12 +42,12 @@ module RansackMemory
39
42
  params[:per_page] = session["#{session_key_base}_per_page"].presence
40
43
 
41
44
  # set page number to 1 if filter has changed
42
- if (params[::RansackMemory::Core.config[:param]].present? && session[:last_q_params] != params[::RansackMemory::Core.config[:param]]) || (params[:cancel_filter].present? && session["#{session_key_base}_page"] != params[:page])
45
+ if (params[::RansackMemory::Core.config[:param]].present? && session[:last_q_params] != params[::RansackMemory::Core.config[:param]].permit!.to_h) || (params[:cancel_filter].present? && session["#{session_key_base}_page"] != params[:page])
43
46
  params[:page] = nil
44
47
  session["#{session_key_base}_page"] = nil
45
48
  end
46
49
 
47
- session[:last_q_params] = params[::RansackMemory::Core.config[:param]]
50
+ session[:last_q_params] = params[::RansackMemory::Core.config[:param]]&.to_unsafe_h
48
51
 
49
52
  # session[:last_page] = params[:page]
50
53
  end
@@ -1,3 +1,3 @@
1
1
  module RansackMemory
2
- VERSION = '0.0.8'
2
+ VERSION = '0.1.0'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ransack_memory
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.8
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Richard Lapiš