go_to_param 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3f0ecdb8725a6803349e51b90dc85872681c6162
4
+ data.tar.gz: 09c706a18e0cfb180d606472bf9a8f459fa9c8f7
5
+ SHA512:
6
+ metadata.gz: 56b20be331349761df60822bf4232c19baaa3a4b20a8364fb78a650a09bbb6f928210cc265b436c2f4ade9cebb110d78ca33da96aa9173265e114f7c66ae941a
7
+ data.tar.gz: 0dcb8ece149ed00d4f82c66f3b4f9de940337377006b7fbc7716011b6a050f384ba59354c7091d80a7c5b77f6ac319031ab83f374a8e4ff61d054286d221bbf7
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in go_to_param.gemspec
4
+ gemspec
@@ -0,0 +1,119 @@
1
+ # GoToParam
2
+
3
+ Small set of Rails "go_to" redirection parameter utilities, e.g. to catch a desired path and redirect to it after logging in.
4
+
5
+ ## Usage
6
+
7
+ Include in some suitable base controller:
8
+
9
+ ``` ruby
10
+ class ApplicationController < ActionController::Base
11
+ include GoToParameter
12
+ end
13
+ ```
14
+
15
+ Now your controllers and views get some methods.
16
+
17
+ ### get_go_to_param
18
+
19
+ Put the current/requested path in a `?go_to=` parameter.
20
+
21
+ ``` ruby
22
+ class ApplicationController < ActionController::Base
23
+ include GoToParam
24
+
25
+ before_filter :ensure_authenticated
26
+
27
+ private
28
+
29
+ def ensure_authenticated
30
+ unless authenticated?
31
+ redirect_to login_path(get_go_to_param)
32
+ end
33
+ end
34
+ end
35
+ ```
36
+
37
+ Only picks up the requested path if it's a GET, since we can't redirect back to a non-GET later.
38
+
39
+ ### hidden_go_to_tag
40
+
41
+ Pass the `go_to` parameter along with a form.
42
+
43
+ ``` erb
44
+ <h1>Log in</h1>
45
+
46
+ <form>
47
+ <%= hidden_go_to_tag %>
48
+
49
+ </form>
50
+ ```
51
+
52
+ ### go_to_param
53
+
54
+ Pass the `go_to` parameter along with a link.
55
+
56
+ ``` erb
57
+ <%= link_to("Reset password", password_reset_path(go_to_param)) %>
58
+ ```
59
+
60
+ You can pass in additional parameters for the given path:
61
+
62
+ ``` erb
63
+ <%= link_to("Reset password", password_reset_path(go_to_param(email: @email))) %>
64
+ ```
65
+
66
+ ### go_to_path_or
67
+
68
+ Finally use the `go_to` parameter.
69
+
70
+ You must pass in a fallback path.
71
+
72
+ ``` ruby
73
+ class SessionsController < ActionController::Base
74
+ def create
75
+ if logged_in?
76
+ redirect_to go_to_path_or(root_path)
77
+ end
78
+ end
79
+ end
80
+ ```
81
+
82
+ ## Installation
83
+
84
+ Add this line to your application's Gemfile:
85
+
86
+ gem 'go_to_param'
87
+
88
+ And then execute:
89
+
90
+ $ bundle
91
+
92
+ Or install it yourself as:
93
+
94
+ $ gem install go_to_param
95
+
96
+ ## License
97
+
98
+ Copyright (c) 2013 Henrik Nyh
99
+
100
+ MIT License
101
+
102
+ Permission is hereby granted, free of charge, to any person obtaining
103
+ a copy of this software and associated documentation files (the
104
+ "Software"), to deal in the Software without restriction, including
105
+ without limitation the rights to use, copy, modify, merge, publish,
106
+ distribute, sublicense, and/or sell copies of the Software, and to
107
+ permit persons to whom the Software is furnished to do so, subject to
108
+ the following conditions:
109
+
110
+ The above copyright notice and this permission notice shall be
111
+ included in all copies or substantial portions of the Software.
112
+
113
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
114
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
115
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
116
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
117
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
118
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
119
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,22 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'go_to_param/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "go_to_param"
7
+ spec.version = GoToParam::VERSION
8
+ spec.authors = ["Henrik N"]
9
+ spec.email = ["henrik@nyh.se"]
10
+ spec.summary = %q{Rails "go_to" redirection param utilities.}
11
+ spec.homepage = ""
12
+ spec.license = "MIT"
13
+
14
+ spec.files = `git ls-files`.split($/)
15
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
16
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
17
+ spec.require_paths = ["lib"]
18
+
19
+ spec.add_development_dependency "bundler", "~> 1.3"
20
+ spec.add_development_dependency "rake"
21
+ spec.add_development_dependency "rspec"
22
+ end
@@ -0,0 +1,38 @@
1
+ require "go_to_param/version"
2
+
3
+ module GoToParam
4
+ def self.included(klass)
5
+ klass.helper_method :hidden_go_to_tag, :go_to_param
6
+ end
7
+
8
+ def hidden_go_to_tag
9
+ view_context.hidden_field_tag :go_to, go_to_value
10
+ end
11
+
12
+ def go_to_param(other_params = {})
13
+ { go_to: go_to_value }.merge(other_params)
14
+ end
15
+
16
+ def get_go_to_param
17
+ if request.get?
18
+ { go_to: request.fullpath }
19
+ else
20
+ {}
21
+ end
22
+ end
23
+
24
+ def go_to_path_or(path)
25
+ # Avoid phishing redirects.
26
+ if go_to_value.to_s.start_with?("/")
27
+ go_to_value
28
+ else
29
+ path
30
+ end
31
+ end
32
+
33
+ private
34
+
35
+ def go_to_value
36
+ params[:go_to]
37
+ end
38
+ end
@@ -0,0 +1,3 @@
1
+ module GoToParam
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,83 @@
1
+ require_relative "../lib/go_to_param"
2
+
3
+ class FakeController
4
+ attr_accessor :params, :view_context, :request
5
+
6
+ def self.helper_method(*methods)
7
+ @helper_methods = methods
8
+ end
9
+
10
+ def self.helper_methods
11
+ @helper_methods
12
+ end
13
+
14
+ include GoToParam
15
+ end
16
+
17
+ describe GoToParam do
18
+ let(:controller) { FakeController.new }
19
+
20
+ describe "#hidden_go_to_tag" do
21
+ it "makes it a helper method" do
22
+ FakeController.helper_methods.should include :hidden_go_to_tag
23
+ end
24
+
25
+ it "adds a hidden field tag" do
26
+ controller.params = { go_to: "/example", id: "1" }
27
+ view = double
28
+ controller.view_context = view
29
+
30
+ view.should_receive(:hidden_field_tag).
31
+ with(:go_to, "/example")
32
+ controller.hidden_go_to_tag
33
+ end
34
+ end
35
+
36
+ describe "#go_to_param" do
37
+ it "makes it a helper method" do
38
+ FakeController.helper_methods.should include :go_to_param
39
+ end
40
+
41
+ it "includes the go_to parameter" do
42
+ controller.params = { go_to: "/example", id: "1" }
43
+
44
+ controller.go_to_param.should == { go_to: "/example" }
45
+ end
46
+
47
+ it "accepts additional parameters" do
48
+ controller.params = { go_to: "/example", id: "1" }
49
+
50
+ controller.go_to_param(a: "b").should == { go_to: "/example", a: "b" }
51
+ end
52
+ end
53
+
54
+ describe "#get_go_to_param" do
55
+ it "gets the request path as the go_to parameter" do
56
+ controller.request = double(get?: true, fullpath: "/example")
57
+ controller.get_go_to_param.should == { go_to: "/example" }
58
+ end
59
+
60
+ it "returns an empty hash for a non-GET request" do
61
+ controller.request = double(get?: false, fullpath: "/example")
62
+ controller.get_go_to_param.should == {}
63
+ end
64
+ end
65
+
66
+ describe "#go_to_path_or" do
67
+ it "is the go_to parameter value" do
68
+ controller.params = { go_to: "/example", id: "1" }
69
+ controller.go_to_path_or("/default").should == "/example"
70
+ end
71
+
72
+ it "falls back to the passed-in value without a parameter value" do
73
+ controller = FakeController.new
74
+ controller.params = { id: "1" }
75
+ controller.go_to_path_or("/default").should == "/default"
76
+ end
77
+
78
+ it "only allows relative paths" do
79
+ controller.params = { go_to: "http://evil.com", id: "1" }
80
+ controller.go_to_path_or("/default").should == "/default"
81
+ end
82
+ end
83
+ end
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: go_to_param
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Henrik N
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-03-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description:
56
+ email:
57
+ - henrik@nyh.se
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - .gitignore
63
+ - Gemfile
64
+ - README.md
65
+ - Rakefile
66
+ - go_to_param.gemspec
67
+ - lib/go_to_param.rb
68
+ - lib/go_to_param/version.rb
69
+ - spec/go_to_spec.rb
70
+ homepage: ''
71
+ licenses:
72
+ - MIT
73
+ metadata: {}
74
+ post_install_message:
75
+ rdoc_options: []
76
+ require_paths:
77
+ - lib
78
+ required_ruby_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ! '>='
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ requirements: []
89
+ rubyforge_project:
90
+ rubygems_version: 2.0.3
91
+ signing_key:
92
+ specification_version: 4
93
+ summary: Rails "go_to" redirection param utilities.
94
+ test_files:
95
+ - spec/go_to_spec.rb
96
+ has_rdoc: