rails_source_path 1.0.1
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 +7 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/.travis.yml +7 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +21 -0
- data/README.md +67 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/rails_source_path/rails_source_path.rb +37 -0
- data/lib/rails_source_path/version.rb +3 -0
- data/lib/rails_source_path.rb +2 -0
- data/rails_source_path.gemspec +41 -0
- metadata +149 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 59d12c6cde889b2cc180d0a0892bc5cb80fea2e6
|
4
|
+
data.tar.gz: 4e53fb325e25ec0a427b3a9595f98b7a470804d2
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: d9cd8f35d9a96c56285dd00be44292d72febdb2ad0c28aafb31e331769ad37359f298e5ce49aa1f4b053691f99696bbd7c2e7c41aa14a54b7744f34286faebcf
|
7
|
+
data.tar.gz: 9c86a1727d18127f8a5f5334e5c99196a35444ed16e6ee0b35c1d1e1c1f2ff159df5efc4b500e069a9365a2e3554b9b84d6026d5e7c5bd40d9034265191d9bb4
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2019 Leo Li
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
# RailsSourcePath
|
2
|
+
|
3
|
+
In rails project, one common scenario is:
|
4
|
+
|
5
|
+
One form used to create or update an object can be routed from more than one page, when the object is created or updated, it should be redirected back to wherever it came from.
|
6
|
+
|
7
|
+
Rails `redirect_back` doesn't work in this case because:
|
8
|
+
- `redirect_back` in create/update action will go back to new/edit form.
|
9
|
+
- usually the form is re-rendered if any error exists, which basically breaks the `redirect_back`.
|
10
|
+
|
11
|
+
`rails-source-path` can hanlde this by explicily specifying the entry actions and remember the
|
12
|
+
previous route in `session` store, hence can be used later in the whole controller. Also a helper
|
13
|
+
method is providered so it can be used in view like `back` or `cancel` button.
|
14
|
+
|
15
|
+
## Installation
|
16
|
+
|
17
|
+
Add this line to your application's Gemfile:
|
18
|
+
|
19
|
+
```ruby
|
20
|
+
gem 'rails_source_path'
|
21
|
+
```
|
22
|
+
|
23
|
+
And then execute:
|
24
|
+
|
25
|
+
$ bundle
|
26
|
+
|
27
|
+
Or install it yourself as:
|
28
|
+
|
29
|
+
$ gem install rails_source_path
|
30
|
+
|
31
|
+
## Usage
|
32
|
+
```
|
33
|
+
class YourController < ApplicationController
|
34
|
+
rails_source_path_entry_actions :new, :edit
|
35
|
+
...
|
36
|
+
|
37
|
+
def create
|
38
|
+
if @object.create
|
39
|
+
redirect_to_source
|
40
|
+
else
|
41
|
+
...
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def update
|
46
|
+
if @object.save
|
47
|
+
redirect_to_source
|
48
|
+
else
|
49
|
+
...
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
# in your view/partial
|
55
|
+
link_to 'Cancel', rails_source_path, class: 'button'
|
56
|
+
```
|
57
|
+
|
58
|
+
Both `redirect_to_source` and `rails_source_path` accept an optional argument which is the path when there is no previous route remembered.
|
59
|
+
|
60
|
+
```
|
61
|
+
def rails_source_path(default_path = root_path)
|
62
|
+
def redirect_to_source(default_path = root_path)
|
63
|
+
```
|
64
|
+
|
65
|
+
## License
|
66
|
+
|
67
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "rails_source_path"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
module RailsSourcePath
|
2
|
+
extend ActiveSupport::Concern
|
3
|
+
|
4
|
+
included do
|
5
|
+
helper_method :rails_source_path
|
6
|
+
end
|
7
|
+
|
8
|
+
class_methods do
|
9
|
+
# Used in controller:
|
10
|
+
# rails_source_path_entry_actions :new, :edit
|
11
|
+
def rails_source_path_entry_actions(*methods)
|
12
|
+
before_action :set_rails_source_path, only: methods.flatten
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
# rails_source_path is used in View like 'cancel/back' button
|
17
|
+
def rails_source_path(default_path = root_path)
|
18
|
+
session[:rails_source_path] || default_path
|
19
|
+
end
|
20
|
+
|
21
|
+
# used in controller when object is successfully created or updated
|
22
|
+
def redirect_to_source(default_path = root_path)
|
23
|
+
path = rails_source_path(default_path)
|
24
|
+
session.delete(:rails_source_path)
|
25
|
+
redirect_to path
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
def set_rails_source_path
|
31
|
+
session[:rails_source_path] = begin
|
32
|
+
URI(request.referer).request_uri
|
33
|
+
rescue
|
34
|
+
nil
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "rails_source_path/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "rails_source_path"
|
8
|
+
spec.version = RailsSourcePath::VERSION
|
9
|
+
spec.authors = ["Leo Li"]
|
10
|
+
spec.email = ["lxz.tty@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Remember the route prior to the current controller and redirect/use later}
|
13
|
+
spec.description = %q{In rails project, one common case is one form used to create or update
|
14
|
+
an object can be routed from more than one page, when the object is created or updated, it
|
15
|
+
should be redirected back to wherever it came from. Rails redirect_back doesn't work in this
|
16
|
+
case because: 1. redirect_back in create/update action will go back to new/edit form. 2.
|
17
|
+
usually the form is re-rendered if any error exists, which basically breaks the redirect_back.
|
18
|
+
rails-source-path can hanlde this by explicily specifying the entry actions and remember the
|
19
|
+
previous route in session store, hence can be used in the whole controller. Also a helper
|
20
|
+
method is providered so it can be used in view like 'back' or 'cancel' button.}
|
21
|
+
spec.homepage = "https://github.com/xiuzhong/rails_source_path"
|
22
|
+
spec.license = "MIT"
|
23
|
+
|
24
|
+
# Specify which files should be added to the gem when it is released.
|
25
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
26
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
27
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
28
|
+
end
|
29
|
+
spec.bindir = "exe"
|
30
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
31
|
+
spec.require_paths = ["lib"]
|
32
|
+
|
33
|
+
spec.add_development_dependency "bundler", "~> 1.17"
|
34
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
35
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
36
|
+
spec.add_development_dependency "rspec-rails"
|
37
|
+
|
38
|
+
spec.add_dependency "rails", '>= 4.0.2'
|
39
|
+
spec.add_dependency "activesupport", '>= 3.0'
|
40
|
+
|
41
|
+
end
|
metadata
ADDED
@@ -0,0 +1,149 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rails_source_path
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Leo Li
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-09-06 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.17'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.17'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.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: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec-rails
|
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: rails
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 4.0.2
|
76
|
+
type: :runtime
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 4.0.2
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: activesupport
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '3.0'
|
90
|
+
type: :runtime
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '3.0'
|
97
|
+
description: |-
|
98
|
+
In rails project, one common case is one form used to create or update
|
99
|
+
an object can be routed from more than one page, when the object is created or updated, it
|
100
|
+
should be redirected back to wherever it came from. Rails redirect_back doesn't work in this
|
101
|
+
case because: 1. redirect_back in create/update action will go back to new/edit form. 2.
|
102
|
+
usually the form is re-rendered if any error exists, which basically breaks the redirect_back.
|
103
|
+
rails-source-path can hanlde this by explicily specifying the entry actions and remember the
|
104
|
+
previous route in session store, hence can be used in the whole controller. Also a helper
|
105
|
+
method is providered so it can be used in view like 'back' or 'cancel' button.
|
106
|
+
email:
|
107
|
+
- lxz.tty@gmail.com
|
108
|
+
executables: []
|
109
|
+
extensions: []
|
110
|
+
extra_rdoc_files: []
|
111
|
+
files:
|
112
|
+
- ".gitignore"
|
113
|
+
- ".rspec"
|
114
|
+
- ".travis.yml"
|
115
|
+
- Gemfile
|
116
|
+
- LICENSE.txt
|
117
|
+
- README.md
|
118
|
+
- Rakefile
|
119
|
+
- bin/console
|
120
|
+
- bin/setup
|
121
|
+
- lib/rails_source_path.rb
|
122
|
+
- lib/rails_source_path/rails_source_path.rb
|
123
|
+
- lib/rails_source_path/version.rb
|
124
|
+
- rails_source_path.gemspec
|
125
|
+
homepage: https://github.com/xiuzhong/rails_source_path
|
126
|
+
licenses:
|
127
|
+
- MIT
|
128
|
+
metadata: {}
|
129
|
+
post_install_message:
|
130
|
+
rdoc_options: []
|
131
|
+
require_paths:
|
132
|
+
- lib
|
133
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
134
|
+
requirements:
|
135
|
+
- - ">="
|
136
|
+
- !ruby/object:Gem::Version
|
137
|
+
version: '0'
|
138
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
139
|
+
requirements:
|
140
|
+
- - ">="
|
141
|
+
- !ruby/object:Gem::Version
|
142
|
+
version: '0'
|
143
|
+
requirements: []
|
144
|
+
rubyforge_project:
|
145
|
+
rubygems_version: 2.5.2.3
|
146
|
+
signing_key:
|
147
|
+
specification_version: 4
|
148
|
+
summary: Remember the route prior to the current controller and redirect/use later
|
149
|
+
test_files: []
|