stashable_params 0.0.1 → 0.0.2

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: b66faa23c911c926b57ec2d9876831fb80303358
4
- data.tar.gz: 48c98837e0fb79bbe21daa86c756be7c65e2edc8
3
+ metadata.gz: fd420ee069f2aec597d5c05a30aa4b51c8b90ad5
4
+ data.tar.gz: 814338f879e9e5b06940473a127c388095622e63
5
5
  SHA512:
6
- metadata.gz: 650c93def2ec34c9e4ce54df3e0f4c5918e4c963eeeec28d1cbb1b8e3f6961b96a7907c36efcf46181190de35f79fd41ac99f00ee458be53b4caa2059b0f06f9
7
- data.tar.gz: 7952a00ecda522da8eba2287dea878a54f8a3c2f480866a0bc75abe2630184755c082a02961be1923327e7a7e792cede13bf74db94e0fb97c316c73ecf36c7dc
6
+ metadata.gz: c69ef02aea3a5971da39e285123e1dc8f516ebf81f0e14773c4c2a08460db8ceb09e1e4e79498b3ea8344100725458093f5b0a418bec82624c0c0375a8246e6d
7
+ data.tar.gz: 8d4ff4492d1ca34919e1e1c2f3a3ddf52a6a652b3e05898bb87a0028e511d76ac65cfbc824b1815e17fc5aeaf8b97dfb7500fdd109e5f86b12b075d9fbec46f7
data/README.md CHANGED
@@ -1,6 +1,9 @@
1
1
  # StashableParams
2
2
 
3
- TODO: Write a gem description
3
+ Easily store the current params hash and access them when you need them.
4
+
5
+ Call `stash_params` in your controller to store the current params. Call
6
+ `unstash_params` to retrieve the stashed params and access them from the params hash.
4
7
 
5
8
  ## Installation
6
9
 
@@ -18,7 +21,108 @@ Or install it yourself as:
18
21
 
19
22
  ## Usage
20
23
 
21
- TODO: Write usage instructions here
24
+ `stashed_params` provides helper methods that allow you to stash and
25
+ unstash parameters for later use. To have access to these methods in
26
+ your controllers, include StashableParams in `app/controllers/application_controller.rb`:
27
+
28
+ ```ruby
29
+ class ApplicationController < ActionController::Base
30
+ protect_from_forgery
31
+ include StashableParams
32
+ end
33
+ ```
34
+
35
+ ### Stashing and Unstashing Params
36
+
37
+ Any controller that inherits from ApplicaitonController now has access
38
+ to the `stash_params` and `unstash_params` helper methods.
39
+
40
+ Call `stash_params` to store the current params hash. Call
41
+ `unstash_params` to retrieve the stashed params. After unstashing the
42
+ params, they will be available as part of the current params hash.
43
+
44
+ ### Params Filter
45
+
46
+ `stashable_params` provides a default filter for filtering out
47
+ potentially sensitive parameter keys such as `:password` and
48
+ `:password_confirmation`. These keys will not be stored when
49
+ `stash_params` is called. The `:action` and `:controller` keys are also
50
+ part of the default params filter.
51
+
52
+ ### Customizing the Params Filter
53
+
54
+ If you do not wish to store specific parameter keys you can create a
55
+ custom params filter. To do this, define a `params_filter` method that
56
+ returns an array of keys you do not wish to store. NOTE: This will
57
+ overwrite the default params filter of:
58
+
59
+ `[:password, :password_confirmation, :action, :controller]`
60
+
61
+ It is recommended that you also include these keys in your custom filter.
62
+
63
+ ```ruby
64
+ class ApplicationController < ActionController:Base
65
+ protect_from_forgery
66
+ include StashableParams
67
+
68
+ def params_filter
69
+ [:my_sensitive_key, :password, :password_confirmation, :action, :controller]
70
+ end
71
+ end
72
+ ```
73
+
74
+ ### Example Usage
75
+
76
+ Here's an example of stashing params so we can ask a user to confirm
77
+ their identity before creating a comment.
78
+
79
+ Our application requires that a user must confirm their identity before
80
+ a comment is created if they have not signed in within the last 24
81
+ hours. So, if a user has signed in recently we create the comment. If
82
+ not, we redirect them to the sign in page to confirm their identity and
83
+ redirect them back to the `new_comment_path` so they can resubmit their
84
+ comment.
85
+
86
+ We'll stash the params before the user gets redirected to the sign in
87
+ page and unstash them when the user gets back to the `new_comment_path`
88
+ so we can repopulate the comment fields and save the user from having to
89
+ retype the fields.
90
+
91
+ ```ruby
92
+ class CommentsController < ApplicationController
93
+
94
+ #...
95
+
96
+ def new
97
+ unstash_params
98
+ @comment = Comment.new(comment_params)
99
+ end
100
+
101
+ def create
102
+ if user_not_signed_in_recently
103
+ stash_params
104
+ redirect_to sign_in_path
105
+ end
106
+
107
+ @comment = Comment.new(comment_params)
108
+
109
+ # Code to save comment...
110
+ end
111
+
112
+ private
113
+
114
+ def comment_params
115
+ params.require(:comment).permit(:content) if params(:comment)
116
+ end
117
+
118
+ #...
119
+
120
+ end
121
+ ```
122
+
123
+ Now, when our user gets redirected back to the new comment page the
124
+ comment fields will be populated with the content the user previously
125
+ submitted.
22
126
 
23
127
  ## Contributing
24
128
 
@@ -1,5 +1,27 @@
1
1
  require "stashable_params/version"
2
2
 
3
3
  module StashableParams
4
- # Your code goes here...
4
+ def stash_params
5
+ session[:stashed_params] = filter_params(params, params_filter)
6
+ end
7
+
8
+ def unstash_params
9
+ params.merge!(session.delete(:stashed_params)) if session[:stashed_params]
10
+ end
11
+
12
+ def filter_params(hash, filter)
13
+ filtered_hash = hash
14
+ filtered_hash.each do |k, v|
15
+ if filter.include?(k) || filter.include?(k.to_sym)
16
+ filtered_hash.delete(k)
17
+ else
18
+ filter_params(v, filter) if v.is_a?(Hash)
19
+ end
20
+ end
21
+ filtered_hash
22
+ end
23
+
24
+ def params_filter
25
+ [:password, :password_confirmation, :action, :controller]
26
+ end
5
27
  end
@@ -1,3 +1,3 @@
1
1
  module StashableParams
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -18,6 +18,10 @@ Gem::Specification.new do |spec|
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
19
  spec.require_paths = ["lib"]
20
20
 
21
+ spec.add_runtime_dependency 'rails', '~> 4.0.0'
22
+
21
23
  spec.add_development_dependency "bundler", "~> 1.3"
22
24
  spec.add_development_dependency "rake"
25
+ spec.add_development_dependency 'm'
26
+ spec.add_development_dependency 'byebug'
23
27
  end
@@ -0,0 +1,116 @@
1
+ require 'test_helper'
2
+ require 'byebug'
3
+
4
+ class ApplicationController
5
+ class << self; attr_accessor :session end
6
+ attr_accessor :params
7
+
8
+ @@session = {} # Mimics session available from all controllers in rails app
9
+
10
+ def initialize
11
+ @params = {} # Mimics params hash available within controllers in rails app
12
+ end
13
+
14
+ def session
15
+ # Gives easy access to the session to class instances
16
+ self.class.session
17
+ end
18
+
19
+ def self.session
20
+ @@session
21
+ end
22
+
23
+ def self.session=(hash)
24
+ @@session = hash
25
+ end
26
+ end
27
+
28
+ describe ApplicationController do
29
+ it 'has access to a session and params hash' do
30
+ ApplicationController.session.wont_equal nil
31
+ ApplicationController.new.params.wont_equal nil
32
+ end
33
+ end
34
+
35
+ class StashableController < ApplicationController
36
+ include StashableParams
37
+
38
+ def stash_params_action
39
+ stash_params
40
+ end
41
+
42
+ def unstash_params_action
43
+ unstash_params
44
+ end
45
+
46
+ def self.reset_default_params_filter
47
+ def params_filter
48
+ [:password, :password_confirmation, :action, :controller]
49
+ end
50
+ end
51
+ end
52
+
53
+ describe StashableController do
54
+ let(:params) { { normal_param_key: 'params',
55
+ password: 'password',
56
+ sensitive_param: 'dont save me!',
57
+ nested_hash: { nested_key: "I'm nested!" } } }
58
+
59
+ let(:stashable_controller) { StashableController.new }
60
+
61
+ it 'can stash params' do
62
+ stashable_controller.params = params
63
+
64
+ stashable_controller.stash_params_action
65
+ ApplicationController.session[:stashed_params].must_equal params
66
+ end
67
+
68
+ it 'can unstash params' do
69
+ stashable_controller.params = params
70
+ stashable_controller.stash_params_action
71
+ stashable_controller.params.must_equal(params)
72
+ stashable_controller.params = {}
73
+ stashable_controller.params.must_equal({})
74
+
75
+ stashable_controller.unstash_params_action
76
+
77
+ ApplicationController.session[:stashed_params].must_equal {}
78
+ stashable_controller.params.must_equal(params)
79
+ end
80
+
81
+ it 'provides a default filter for filtering out sensitive params' do
82
+ stashable_controller.params = params
83
+ stashable_controller.stash_params_action
84
+
85
+ ApplicationController.session[:stashed_params].wont_include(:password)
86
+ ApplicationController.session[:stashed_params].must_include(:normal_param_key)
87
+ end
88
+
89
+ it 'can redefine the filter to omit additional params' do
90
+ class StashableController < ApplicationController
91
+ def params_filter
92
+ [:sensitive_param]
93
+ end
94
+ end
95
+
96
+ stashable_controller.params = params
97
+ stashable_controller.stash_params_action
98
+
99
+ ApplicationController.session[:stashed_params].wont_include(:sensitive_param)
100
+ StashableController.reset_default_params_filter
101
+ end
102
+
103
+ it 'filters nested parameter keys' do
104
+ class StashableController < ApplicationController
105
+ def params_filter
106
+ [:nested_key]
107
+ end
108
+ end
109
+
110
+ stashable_controller.params = params
111
+ stashable_controller.stash_params_action
112
+
113
+ ApplicationController.session[:stashed_params][:nested_hash].wont_include(:nested_key)
114
+ StashableController.reset_default_params_filter
115
+ end
116
+ end
@@ -0,0 +1,3 @@
1
+ require 'minitest/autorun'
2
+ require 'minitest/pride'
3
+ require 'stashable_params'
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stashable_params
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lin Reid
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-11-15 00:00:00.000000000 Z
11
+ date: 2013-11-17 00:00:00.000000000 Z
12
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: 4.0.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: 4.0.0
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: bundler
15
29
  requirement: !ruby/object:Gem::Requirement
@@ -38,6 +52,34 @@ dependencies:
38
52
  - - '>='
39
53
  - !ruby/object:Gem::Version
40
54
  version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: m
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: byebug
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
41
83
  description: Easily stash your params for later use.
42
84
  email:
43
85
  - linreid@gmail.com
@@ -53,6 +95,8 @@ files:
53
95
  - lib/stashable_params.rb
54
96
  - lib/stashable_params/version.rb
55
97
  - stashable_params.gemspec
98
+ - test/controller_test.rb
99
+ - test/test_helper.rb
56
100
  homepage: https://github.com/linstula/stashable_params
57
101
  licenses:
58
102
  - MIT
@@ -78,4 +122,6 @@ signing_key:
78
122
  specification_version: 4
79
123
  summary: stashable_params allows you temporarily stash params and unstash them when
80
124
  you need them.
81
- test_files: []
125
+ test_files:
126
+ - test/controller_test.rb
127
+ - test/test_helper.rb