go_to_param 0.0.10 → 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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: fea100cfef7c5232e51c706f26493d87d58d00d7
4
- data.tar.gz: 1151e3917f4c57433a2c2aa9bf5506ab217a1b0c
3
+ metadata.gz: 5bcdbdc10ad6e75c56494c44096fcc98e3da7034
4
+ data.tar.gz: 1b9a9d937839c56c37a6c172ef424e500f9dbeed
5
5
  SHA512:
6
- metadata.gz: 19ea749058f98d09dd82ff29d3e530096176b2e618b2f4fe03928bd23257af0f65e1f6057b4f6317fe14892564e20453aaf62cc7d70c91940648b60753eea6f7
7
- data.tar.gz: c00954fd5782fcc27411ac6b3271404c986afefc8a19f88b96c7b6bb2f996dcc85f421f898721c8b83fca32758392c52bdcb00d0d114fcb91c7cb4f23f603944
6
+ metadata.gz: ea38bfd2bd3a76343a031e19b6fd106bb856863ed35017fcfefe3609150e3e5e72cd906bf2eb1a3e082ab16dc3a5987dfd6717c6832379872f3d274beed3f901
7
+ data.tar.gz: 446c1c4f865e199c74d42cdc3575e1a9adee83962078b7450b8aff67b0fac812ec767ce5380293bee7a57a9d2710d4c2d7ee4047e7ca78f0364c0a73a4b1e4cb
data/README.md CHANGED
@@ -42,11 +42,20 @@ Or a view:
42
42
 
43
43
  ``` erb
44
44
  <h1>Show item</h1>
45
- <%= link_to("Edit item", edit_item_path(@item, go_to_here_params))
45
+ <%= link_to("Edit item", edit_item_path(@item, go_to_here_params)) %>
46
46
  ```
47
47
 
48
48
  This only picks up the requested path if it's a GET, since we can't redirect back to a non-GET later. Otherwise an empty hash is returned.
49
49
 
50
+ You can pass additional query parameters to include, which could be suitable if you want to trigger some action after redirecting:
51
+
52
+ ``` erb
53
+ <%= link_to("Add item after logging in", login_path(@item, go_to_here_params(perform_action: "add"))) %>
54
+ ```
55
+
56
+ Note that these parameters always become transformed into a query string: if you're using Ruby on Rails, they won't be interpreted through your route definitions.
57
+
58
+
50
59
  ### hidden_go_to_tag
51
60
 
52
61
  Pass the `go_to` parameter along with a form.
@@ -90,6 +99,8 @@ class SessionsController < ActionController::Base
90
99
  end
91
100
  ```
92
101
 
102
+ Returns nil if the parameter value is not a relative path, to counter phishing attempts like `/login?go_to=http://evil.com/success_now_give_me_your_cc_details`.
103
+
93
104
  ### go_to_path_or
94
105
 
95
106
  Syntactic sugar. These are equivalent:
@@ -1,3 +1,3 @@
1
1
  module GoToParam
2
- VERSION = "0.0.10"
2
+ VERSION = "0.1.0"
3
3
  end
data/lib/go_to_param.rb CHANGED
@@ -1,3 +1,4 @@
1
+ require "cgi"
1
2
  require "go_to_param/version"
2
3
 
3
4
  module GoToParam
@@ -15,9 +16,10 @@ module GoToParam
15
16
  { go_to: go_to_path }.merge(other_params)
16
17
  end
17
18
 
18
- def go_to_here_params
19
+ def go_to_here_params(additional_query_params = {})
19
20
  if request.get?
20
- { go_to: request.fullpath }
21
+ path = _go_to_add_query_string_from_hash(request.fullpath, additional_query_params)
22
+ { go_to: path }
21
23
  else
22
24
  {}
23
25
  end
@@ -41,4 +43,15 @@ module GoToParam
41
43
  def go_to_param_value
42
44
  params[:go_to]
43
45
  end
46
+
47
+ # Named this way to avoid conflicts. TODO: http://thepugautomatic.com/2014/02/private-api/
48
+ def _go_to_add_query_string_from_hash(path, hash)
49
+ if hash.empty?
50
+ path
51
+ else
52
+ separator = path.include?("?") ? "&" : "?"
53
+ query_string = hash.map { |k, v| "#{k}=#{CGI.escape v.to_s}" }.join("&")
54
+ [ path, separator, query_string ].join
55
+ end
56
+ end
44
57
  end
@@ -19,7 +19,7 @@ describe GoToParam do
19
19
 
20
20
  describe "#hidden_go_to_tag" do
21
21
  it "becomes a helper method" do
22
- FakeController.helper_methods.should include :hidden_go_to_tag
22
+ expect(FakeController.helper_methods).to include :hidden_go_to_tag
23
23
  end
24
24
 
25
25
  it "adds a hidden field tag" do
@@ -27,75 +27,84 @@ describe GoToParam do
27
27
  view = double
28
28
  controller.view_context = view
29
29
 
30
- view.should_receive(:hidden_field_tag).
31
- with(:go_to, "/example")
30
+ expect(view).to receive(:hidden_field_tag).with(:go_to, "/example")
32
31
  controller.hidden_go_to_tag
33
32
  end
34
33
  end
35
34
 
36
35
  describe "#go_to_params" do
37
36
  it "becomes a helper method" do
38
- FakeController.helper_methods.should include :go_to_params
37
+ expect(FakeController.helper_methods).to include :go_to_params
39
38
  end
40
39
 
41
40
  it "includes the go_to parameter" do
42
41
  controller.params = { go_to: "/example", id: "1" }
43
42
 
44
- controller.go_to_params.should == { go_to: "/example" }
43
+ expect(controller.go_to_params).to eq({ go_to: "/example" })
45
44
  end
46
45
 
47
46
  it "accepts additional parameters" do
48
47
  controller.params = { go_to: "/example", id: "1" }
49
48
 
50
- controller.go_to_params(a: "b").should == { go_to: "/example", a: "b" }
49
+ expect(controller.go_to_params(a: "b")).to eq({ go_to: "/example", a: "b" })
51
50
  end
52
51
  end
53
52
 
54
53
  describe "#go_to_here_params" do
55
54
  it "becomes a helper method" do
56
- FakeController.helper_methods.should include :go_to_here_params
55
+ expect(FakeController.helper_methods).to include :go_to_here_params
57
56
  end
58
57
 
59
58
  it "gets the request path as the go_to parameter" do
60
59
  controller.request = double(get?: true, fullpath: "/example")
61
- controller.go_to_here_params.should == { go_to: "/example" }
60
+ expect(controller.go_to_here_params).to eq({ go_to: "/example" })
62
61
  end
63
62
 
64
63
  it "returns an empty hash for a non-GET request" do
65
64
  controller.request = double(get?: false, fullpath: "/example")
66
- controller.go_to_here_params.should == {}
65
+ expect(controller.go_to_here_params).to eq({})
67
66
  end
67
+
68
+ it "accepts additional query parameters" do
69
+ controller.request = double(get?: true, fullpath: "/example")
70
+ expect(controller.go_to_here_params(foo: "1 2", bar: 3)).to eq({ go_to: "/example?foo=1+2&bar=3" })
71
+
72
+ # Handles pre-existing "?"
73
+ controller.request = double(get?: true, fullpath: "/example?foo")
74
+ expect(controller.go_to_here_params(bar: 3)).to eq({ go_to: "/example?foo&bar=3" })
75
+ end
76
+
68
77
  end
69
78
 
70
79
  describe "#go_to_path" do
71
80
  it "becomes a helper method" do
72
- FakeController.helper_methods.should include :go_to_path
81
+ expect(FakeController.helper_methods).to include :go_to_path
73
82
  end
74
83
 
75
84
  it "is the go_to parameter value" do
76
85
  controller.params = { go_to: "/example", id: "1" }
77
- controller.go_to_path.should == "/example"
86
+ expect(controller.go_to_path).to eq("/example")
78
87
  end
79
88
 
80
89
  it "is nil if the parameter value is not a relative path" do
81
90
  controller.params = { go_to: "http://evil.com", id: "1" }
82
- controller.go_to_path.should be_nil
91
+ expect(controller.go_to_path).to be_nil
83
92
  end
84
93
  end
85
94
 
86
95
  describe "#go_to_path_or" do
87
96
  it "becomes a helper method" do
88
- FakeController.helper_methods.should include :go_to_path_or
97
+ expect(FakeController.helper_methods).to include :go_to_path_or
89
98
  end
90
99
 
91
100
  it "is the go_to parameter value" do
92
101
  controller.params = { go_to: "/example", id: "1" }
93
- controller.go_to_path_or("/default").should == "/example"
102
+ expect(controller.go_to_path_or("/default")).to eq("/example")
94
103
  end
95
104
 
96
105
  it "is the passed-in value if the parameter value is not a relative path" do
97
106
  controller.params = { go_to: "http://evil.com", id: "1" }
98
- controller.go_to_path_or("/default").should == "/default"
107
+ expect(controller.go_to_path_or("/default")).to eq("/default")
99
108
  end
100
109
  end
101
110
  end
metadata CHANGED
@@ -1,55 +1,55 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: go_to_param
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.10
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Henrik N
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-03-24 00:00:00.000000000 Z
11
+ date: 2015-05-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ~>
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
19
  version: '1.3'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ~>
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '1.3'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - ! '>='
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
33
  version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - ! '>='
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rspec
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - ! '>='
45
+ - - ">="
46
46
  - !ruby/object:Gem::Version
47
47
  version: '0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - ! '>='
52
+ - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
55
  description:
@@ -59,7 +59,7 @@ executables: []
59
59
  extensions: []
60
60
  extra_rdoc_files: []
61
61
  files:
62
- - .gitignore
62
+ - ".gitignore"
63
63
  - Gemfile
64
64
  - README.md
65
65
  - Rakefile
@@ -77,20 +77,19 @@ require_paths:
77
77
  - lib
78
78
  required_ruby_version: !ruby/object:Gem::Requirement
79
79
  requirements:
80
- - - ! '>='
80
+ - - ">="
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
83
  required_rubygems_version: !ruby/object:Gem::Requirement
84
84
  requirements:
85
- - - ! '>='
85
+ - - ">="
86
86
  - !ruby/object:Gem::Version
87
87
  version: '0'
88
88
  requirements: []
89
89
  rubyforge_project:
90
- rubygems_version: 2.0.3
90
+ rubygems_version: 2.2.2
91
91
  signing_key:
92
92
  specification_version: 4
93
93
  summary: Rails "go_to" redirection param utilities.
94
94
  test_files:
95
95
  - spec/go_to_param_spec.rb
96
- has_rdoc: