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 +4 -4
- data/README.md +24 -4
- data/app/controllers/concerns/ransack_memory/concern.rb +11 -8
- data/lib/ransack_memory/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ee63c846270102691c62cd2a7af77912d7aa7651
|
4
|
+
data.tar.gz: 8dd719ca19c83f10078157448a329f56ed959d9b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
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
|
-
|
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
|
-
|
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,
|
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
|
-
|
8
|
-
|
9
|
-
|
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}"].
|
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
|