remember_params 0.1.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ce94a13aeb7b7d71f7e6dddd562749a05319447c
4
+ data.tar.gz: 01df42f09a70c39bb44467bd2478dcef55dd7632
5
+ SHA512:
6
+ metadata.gz: ffdef6b59164004d10f2be067f6780ffbb934afc94147bc358cf783681afa16263461604d805c6e739cbb3721b085f0353bbc040cd663e40ba51416e52e18610
7
+ data.tar.gz: 2f78cb364d9ab9130c371f79ff187b9a20c1bf92c60dcc21c085ba577466d3cca71eeb6b627731e49825ee32728f14c55ba0211daa5b7c833559faee58b6965f
@@ -0,0 +1,20 @@
1
+ Copyright 2016 Johannes Treitz
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,54 @@
1
+ # RememberParams
2
+ Rails gem that makes actions remembers GET params like keywords and page.
3
+
4
+ ## Scenario
5
+
6
+ Say you have that index action where you can search, filter and
7
+ paginate through records. Once you click on one of the records
8
+ you lose track of the exact location (that is search keywords,
9
+ filter settings and page).
10
+
11
+ RememberParams will bring users back to that location when they
12
+ return to the index page by remembering the search keywords,
13
+ filter settings and page.
14
+
15
+ ## Usage
16
+
17
+ To make a controller action remember its params simply add the
18
+ `remember_params` line on top like this:
19
+
20
+ ```ruby
21
+ class BooksController < ApplicationController
22
+ remember_params :keywords, :page # defaults are index and 1 hour
23
+ remember_params :client_id, on: :client_list, for: 1.minute
24
+ end
25
+ ```
26
+
27
+ Browsing the action without any params will automatically try to restore
28
+ params and redirect to the same location but with previously set params.
29
+
30
+ To reset params set them to empty string:
31
+
32
+ ```ruby
33
+ link_to 'Books', books_path(keywords: '', page: '')
34
+ ```
35
+
36
+ ## Installation
37
+ Add this line to your application's Gemfile:
38
+
39
+ ```ruby
40
+ gem 'remember_params'
41
+ ```
42
+
43
+ And then execute:
44
+ ```bash
45
+ $ bundle
46
+ ```
47
+
48
+ Or install it yourself as:
49
+ ```bash
50
+ $ gem install remember_params
51
+ ```
52
+
53
+ ## License
54
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,34 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'RememberParams'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.md')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+
18
+
19
+
20
+
21
+
22
+ require 'bundler/gem_tasks'
23
+
24
+ require 'rake/testtask'
25
+
26
+ Rake::TestTask.new(:test) do |t|
27
+ t.libs << 'lib'
28
+ t.libs << 'test'
29
+ t.pattern = 'test/**/*_test.rb'
30
+ t.verbose = false
31
+ end
32
+
33
+
34
+ task default: :test
@@ -0,0 +1,55 @@
1
+ module RememberParams
2
+ extend ActiveSupport::Concern
3
+
4
+ included do
5
+ before_action :restore_or_save_params
6
+
7
+ def self.remember_params(*params, on: :index, duration: 1.hour)
8
+ on = on.to_s
9
+
10
+ raise '[remember_params] must specify one or more params to remember' if
11
+ params.empty?
12
+ raise '[remember_params] param name remembered_at is reserved' if
13
+ params.include? :remembered_at
14
+ raise '[remember_params] \'for\' must be ActiveSupport::Duration' unless
15
+ duration.is_a?(ActiveSupport::Duration)
16
+ raise '[remember_params] \'for\' must be gte 1 second' unless
17
+ duration >= 1.second
18
+
19
+ cattr_accessor :remember_params_config
20
+ self.remember_params_config ||= {}
21
+ self.remember_params_config[on] = {}
22
+ self.remember_params_config[on][:params] = params
23
+ self.remember_params_config[on][:duration] = duration
24
+
25
+ self.before_action :restore_or_save_params
26
+ end
27
+ end
28
+
29
+ def restore_or_save_params
30
+ return unless request.get?
31
+ return unless respond_to? :remember_params_config
32
+ return unless config = self.remember_params_config[action_name]
33
+
34
+ session[:remembered_params] ||= {}
35
+ key = params.slice(:controller, :action).values.join('/').parameterize
36
+ params_to_remember = params.permit(*config[:params]).to_h
37
+
38
+ # Restore params
39
+ if params_to_remember.empty? &&
40
+ session[:remembered_params][key]&.except('remembered_at')&.select{|_,v| v.present?}&.any? &&
41
+ DateTime.parse(session[:remembered_params][key]['remembered_at']) >
42
+ (DateTime.now - config[:duration])
43
+ then
44
+ redirect_to params: session[:remembered_params][key].except('remembered_at')
45
+ end
46
+
47
+ # Save params (also refreshes remembered_at after restore)
48
+ if params_to_remember.any?
49
+ params_to_remember['remembered_at'] = DateTime.now
50
+ session[:remembered_params][key] = params_to_remember
51
+ end
52
+ end
53
+ end
54
+
55
+ ActionController::Base.send :include, RememberParams
@@ -0,0 +1,3 @@
1
+ module RememberParams
2
+ VERSION = '0.1.0'
3
+ end
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: remember_params
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Johannes Treitz
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-08-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '5.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '5.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: timecop
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.8'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.8'
41
+ description: Makes it easy to return to exact position on index pages after clicking
42
+ on records.
43
+ email:
44
+ - jotreitz@gmail.com
45
+ executables: []
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - MIT-LICENSE
50
+ - README.md
51
+ - Rakefile
52
+ - lib/remember_params.rb
53
+ - lib/remember_params/version.rb
54
+ homepage: https://github.com/crispymtn/remember_params
55
+ licenses:
56
+ - MIT
57
+ metadata: {}
58
+ post_install_message:
59
+ rdoc_options: []
60
+ require_paths:
61
+ - lib
62
+ required_ruby_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ requirements: []
73
+ rubyforge_project:
74
+ rubygems_version: 2.5.1
75
+ signing_key:
76
+ specification_version: 4
77
+ summary: Rails gem that makes actions remembers GET params like keywords and page.
78
+ test_files: []