devise-better_routes 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +20 -0
- data/.rspec +2 -0
- data/.travis.yml +3 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +76 -0
- data/Rakefile +1 -0
- data/devise-better_routes.gemspec +28 -0
- data/lib/devise/better_routes.rb +72 -0
- data/lib/devise/better_routes/version.rb +5 -0
- data/spec/devise/better_routes_spec.rb +108 -0
- data/spec/rails_app/Rakefile +6 -0
- data/spec/rails_app/app/assets/images/.keep +0 -0
- data/spec/rails_app/app/assets/javascripts/application.js +13 -0
- data/spec/rails_app/app/assets/stylesheets/application.css +13 -0
- data/spec/rails_app/app/controllers/application_controller.rb +5 -0
- data/spec/rails_app/app/controllers/concerns/.keep +0 -0
- data/spec/rails_app/app/controllers/current_rails_programmers_controller.rb +2 -0
- data/spec/rails_app/app/controllers/current_users_controller.rb +2 -0
- data/spec/rails_app/app/controllers/rails_programmers_controller.rb +2 -0
- data/spec/rails_app/app/controllers/users_controller.rb +2 -0
- data/spec/rails_app/app/helpers/application_helper.rb +2 -0
- data/spec/rails_app/app/mailers/.keep +0 -0
- data/spec/rails_app/app/models/.keep +0 -0
- data/spec/rails_app/app/models/concerns/.keep +0 -0
- data/spec/rails_app/app/models/rails_programmer.rb +3 -0
- data/spec/rails_app/app/models/user.rb +3 -0
- data/spec/rails_app/app/views/layouts/application.html.erb +14 -0
- data/spec/rails_app/config.ru +4 -0
- data/spec/rails_app/config/application.rb +26 -0
- data/spec/rails_app/config/boot.rb +5 -0
- data/spec/rails_app/config/database.yml +25 -0
- data/spec/rails_app/config/environment.rb +5 -0
- data/spec/rails_app/config/environments/development.rb +29 -0
- data/spec/rails_app/config/environments/production.rb +80 -0
- data/spec/rails_app/config/environments/test.rb +36 -0
- data/spec/rails_app/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/rails_app/config/initializers/devise.rb +246 -0
- data/spec/rails_app/config/initializers/filter_parameter_logging.rb +4 -0
- data/spec/rails_app/config/initializers/inflections.rb +16 -0
- data/spec/rails_app/config/initializers/mime_types.rb +5 -0
- data/spec/rails_app/config/initializers/secret_token.rb +12 -0
- data/spec/rails_app/config/initializers/session_store.rb +3 -0
- data/spec/rails_app/config/initializers/wrap_parameters.rb +14 -0
- data/spec/rails_app/config/locales/en.yml +23 -0
- data/spec/rails_app/config/routes.rb +4 -0
- data/spec/rails_app/db/migrate/20130623000000_devise_create_users.rb +46 -0
- data/spec/rails_app/db/schema.rb +34 -0
- data/spec/rails_app/lib/assets/.keep +0 -0
- data/spec/rails_app/public/404.html +58 -0
- data/spec/rails_app/public/422.html +58 -0
- data/spec/rails_app/public/500.html +57 -0
- data/spec/rails_app/public/favicon.ico +0 -0
- data/spec/spec_helper.rb +10 -0
- metadata +262 -0
data/.gitignore
ADDED
@@ -0,0 +1,20 @@
|
|
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
|
18
|
+
spec/rails_app/db/*.sqlite3
|
19
|
+
spec/rails_app/log
|
20
|
+
|
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Toru KAWAMURA
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
# Devise::BetterRoutes
|
2
|
+
|
3
|
+
Better routes patch for Devise
|
4
|
+
|
5
|
+
## One of the issues of Devise
|
6
|
+
|
7
|
+
# config/routes.rb
|
8
|
+
devise_for :users
|
9
|
+
|
10
|
+
Devise makes the following registration routes:
|
11
|
+
|
12
|
+
user_registration POST /users(.:format) devise/registrations#create
|
13
|
+
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
|
14
|
+
cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
|
15
|
+
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
|
16
|
+
PATCH /users(.:format) devise/registrations#update
|
17
|
+
PUT /users(.:format) devise/registrations#update
|
18
|
+
DELETE /users(.:format) devise/registrations#destroy
|
19
|
+
|
20
|
+
These routes have a few problems.
|
21
|
+
|
22
|
+
* These confuse the resource which represents "all users" and the resource which represents "myself"
|
23
|
+
* What is the "registrations" which does not correspond to URL?
|
24
|
+
|
25
|
+
## Solution
|
26
|
+
|
27
|
+
`#create` and `#new` are the actions for all users.
|
28
|
+
The rest are the actions for myself.
|
29
|
+
|
30
|
+
Therefore, it should make two distinct resources.
|
31
|
+
|
32
|
+
|
33
|
+
Using this gem makes the following routes:
|
34
|
+
|
35
|
+
users POST /users(.:format) users#create
|
36
|
+
new_user GET /users/new(.:format) users#new
|
37
|
+
cancel_current_user GET /current_user/cancel(.:format) current_users#cancel
|
38
|
+
edit_current_user GET /current_user/edit(.:format) current_users#edit
|
39
|
+
current_user PATCH /current_user(.:format) current_users#update
|
40
|
+
PUT /current_user(.:format) current_users#update
|
41
|
+
DELETE /current_user(.:format) current_users#destroy
|
42
|
+
|
43
|
+
And also makes delegating URL helpers, such as `new_registration_path` -> `new_user_path`.
|
44
|
+
Default views of Devise just work fine.
|
45
|
+
|
46
|
+
(However, you should not use `registration` helper anymore because a registration resource does not exist.)
|
47
|
+
|
48
|
+
Then you can create two controllers as follows:
|
49
|
+
|
50
|
+
# app/controllers/users_controller.rb
|
51
|
+
class UsersController < Devise::RegistrationsController
|
52
|
+
end
|
53
|
+
|
54
|
+
# app/controllers/current_users_controller.rb
|
55
|
+
class CurrentUsersController < Devise::RegistrationsController
|
56
|
+
end
|
57
|
+
|
58
|
+
|
59
|
+
## Installation
|
60
|
+
|
61
|
+
Add this line to your application's Gemfile:
|
62
|
+
|
63
|
+
gem 'devise-better_routes'
|
64
|
+
|
65
|
+
And then execute:
|
66
|
+
|
67
|
+
$ bundle
|
68
|
+
|
69
|
+
|
70
|
+
## Contributing
|
71
|
+
|
72
|
+
1. Fork it
|
73
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
74
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
75
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
76
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'devise/better_routes/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'devise-better_routes'
|
8
|
+
spec.version = Devise::BetterRoutes::VERSION
|
9
|
+
spec.authors = ['Toru KAWAMURA']
|
10
|
+
spec.email = ['tkawa@4bit.net']
|
11
|
+
spec.description = %q{Better routes patch for Devise}
|
12
|
+
spec.summary = %q{Better routes patch for Devise}
|
13
|
+
spec.homepage = ''
|
14
|
+
spec.license = 'MIT'
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ['lib']
|
20
|
+
|
21
|
+
spec.add_dependency 'devise', '>= 3.0.0.rc'
|
22
|
+
spec.add_dependency 'rails', '>= 4.0.0.rc1'
|
23
|
+
spec.add_development_dependency 'bundler', '~> 1.3'
|
24
|
+
spec.add_development_dependency 'rake'
|
25
|
+
spec.add_development_dependency 'rspec'
|
26
|
+
spec.add_development_dependency 'rspec-rails'
|
27
|
+
spec.add_development_dependency 'sqlite3'
|
28
|
+
end
|
@@ -0,0 +1,72 @@
|
|
1
|
+
require 'devise'
|
2
|
+
require 'action_dispatch'
|
3
|
+
require 'active_support'
|
4
|
+
require 'devise/better_routes/version'
|
5
|
+
|
6
|
+
module Devise
|
7
|
+
module Controllers
|
8
|
+
module UrlHelpers
|
9
|
+
def self.override_registration_helpers!(registration_routes = nil)
|
10
|
+
helper_mappings = {
|
11
|
+
new_registration: 'new_#{scope}',
|
12
|
+
edit_registration: 'edit_current_#{scope}',
|
13
|
+
cancel_registration: 'cancel_current_#{scope}'
|
14
|
+
}
|
15
|
+
[:path, :url].each do |path_or_url|
|
16
|
+
class_eval <<-URL_HELPERS, __FILE__, __LINE__ + 1
|
17
|
+
def registration_#{path_or_url}(resource_or_scope, *args)
|
18
|
+
scope = Devise::Mapping.find_scope!(resource_or_scope)
|
19
|
+
if respond_to?(:controller_name) && controller_name == "current_\#{scope.to_s.pluralize}"
|
20
|
+
send("current_\#{scope}_#{path_or_url}", *args)
|
21
|
+
else
|
22
|
+
send("\#{scope.to_s.pluralize}_#{path_or_url}", *args)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
URL_HELPERS
|
26
|
+
helper_mappings.each do |old_name, new_name|
|
27
|
+
method = "#{old_name}_#{path_or_url}"
|
28
|
+
class_eval <<-URL_HELPERS, __FILE__, __LINE__ + 1
|
29
|
+
def #{method}(resource_or_scope, *args)
|
30
|
+
scope = Devise::Mapping.find_scope!(resource_or_scope)
|
31
|
+
send("#{new_name}_#{path_or_url}", *args)
|
32
|
+
end
|
33
|
+
URL_HELPERS
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
override_registration_helpers!(Devise::URL_HELPERS[:registration])
|
38
|
+
|
39
|
+
def self.run_generate_hooks!
|
40
|
+
self.override_registration_helpers!(Devise::URL_HELPERS[:registration])
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def self.regenerate_helpers!
|
46
|
+
Devise::Controllers::UrlHelpers.remove_helpers!
|
47
|
+
Devise::Controllers::UrlHelpers.generate_helpers!
|
48
|
+
Devise::Controllers::UrlHelpers.run_generate_hooks!
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
module ActionDispatch
|
53
|
+
module Routing
|
54
|
+
class Mapper
|
55
|
+
protected
|
56
|
+
def devise_registration(mapping, controllers)
|
57
|
+
path, as, @scope[:path], @scope[:as] = @scope[:path], @scope[:as], nil, nil
|
58
|
+
current_resource_name = mapping.path_names["current_#{mapping.singular}".to_sym]
|
59
|
+
resources mapping.path, only: [:create, :new]
|
60
|
+
resource current_resource_name, only: [:edit, :update, :destroy] do
|
61
|
+
get :cancel
|
62
|
+
end
|
63
|
+
ensure
|
64
|
+
@scope[:path], @scope[:as] = path, as
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
ActiveSupport::Inflector.inflections do |inflect|
|
71
|
+
inflect.uncountable %w( my me )
|
72
|
+
end
|
@@ -0,0 +1,108 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Devise::BetterRoutes do
|
4
|
+
it 'expect to have a version number' do
|
5
|
+
expect(Devise::BetterRoutes::VERSION).not_to be_nil
|
6
|
+
end
|
7
|
+
|
8
|
+
context 'routing' do
|
9
|
+
include RSpec::Rails::RoutingExampleGroup
|
10
|
+
describe 'users' do
|
11
|
+
it 'routes to users#create' do
|
12
|
+
expect(post('/users')).to route_to('users#create')
|
13
|
+
end
|
14
|
+
it 'routes to users#new' do
|
15
|
+
expect(get('/users/new')).to route_to('users#new')
|
16
|
+
end
|
17
|
+
it 'routes to current_users#cancel' do
|
18
|
+
expect(get('/current_user/cancel')).to route_to('current_users#cancel')
|
19
|
+
end
|
20
|
+
it 'routes to current_users#edit' do
|
21
|
+
expect(get('/current_user/edit')).to route_to('current_users#edit')
|
22
|
+
end
|
23
|
+
it 'routes to current_users#update' do
|
24
|
+
expect(put('/current_user')).to route_to('current_users#update')
|
25
|
+
#expect(patch('/current_user')).to route_to('current_users#update')
|
26
|
+
end
|
27
|
+
it 'routes to current_users#destroy' do
|
28
|
+
expect(delete('/current_user')).to route_to('current_users#destroy')
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
describe 'rails_programmers' do
|
33
|
+
it 'routes to rails_programmers#create' do
|
34
|
+
expect(post('/rails_programmers')).to route_to('rails_programmers#create')
|
35
|
+
end
|
36
|
+
it 'routes to rails_programmers#new' do
|
37
|
+
expect(get('/rails_programmers/new')).to route_to('rails_programmers#new')
|
38
|
+
end
|
39
|
+
it 'routes to current_rails_programmers#cancel' do
|
40
|
+
expect(get('/current_rails_programmer/cancel')).to route_to('current_rails_programmers#cancel')
|
41
|
+
end
|
42
|
+
it 'routes to current_rails_programmers#edit' do
|
43
|
+
expect(get('/current_rails_programmer/edit')).to route_to('current_rails_programmers#edit')
|
44
|
+
end
|
45
|
+
it 'routes to current_rails_programmers#update' do
|
46
|
+
expect(put('/current_rails_programmer')).to route_to('current_rails_programmers#update')
|
47
|
+
#expect(patch('/current_rails_programmer')).to route_to('current_rails_programmers#update')
|
48
|
+
end
|
49
|
+
it 'routes to current_rails_programmers#destroy' do
|
50
|
+
expect(delete('/current_rails_programmer')).to route_to('current_rails_programmers#destroy')
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
context 'URL helpers' do
|
56
|
+
include Rails.application.routes.url_helpers
|
57
|
+
include Devise::Controllers::UrlHelpers
|
58
|
+
describe 'users' do
|
59
|
+
specify { expect(users_path).to eq '/users' }
|
60
|
+
specify { expect(new_user_path).to eq '/users/new' }
|
61
|
+
specify { expect(cancel_current_user_path).to eq '/current_user/cancel' }
|
62
|
+
specify { expect(edit_current_user_path).to eq '/current_user/edit' }
|
63
|
+
specify { expect(current_user_path).to eq '/current_user' }
|
64
|
+
it 'registration_path is delegated to users_path' do
|
65
|
+
expect(registration_path(:user)).to eq users_path
|
66
|
+
end
|
67
|
+
it 'registration_path is delegated to current_user_path on current_users' do
|
68
|
+
instance_eval { def controller_name; 'current_users' end }
|
69
|
+
expect(registration_path(:user)).to eq current_user_path
|
70
|
+
instance_eval { undef controller_name }
|
71
|
+
end
|
72
|
+
it 'new_registration_path is delegated to new_user_path' do
|
73
|
+
expect(new_registration_path(:user)).to eq new_user_path
|
74
|
+
end
|
75
|
+
it 'cancel_registration_path is delegated to cancel_current_user_path' do
|
76
|
+
expect(cancel_registration_path(:user)).to eq cancel_current_user_path
|
77
|
+
end
|
78
|
+
it 'edit_registration_path is delegated to edit_current_user_path' do
|
79
|
+
expect(edit_registration_path(:user)).to eq edit_current_user_path
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
describe 'rails_programmers' do
|
84
|
+
specify { expect(rails_programmers_path).to eq '/rails_programmers' }
|
85
|
+
specify { expect(new_rails_programmer_path).to eq '/rails_programmers/new' }
|
86
|
+
specify { expect(cancel_current_rails_programmer_path).to eq '/current_rails_programmer/cancel' }
|
87
|
+
specify { expect(edit_current_rails_programmer_path).to eq '/current_rails_programmer/edit' }
|
88
|
+
specify { expect(current_rails_programmer_path).to eq '/current_rails_programmer' }
|
89
|
+
it 'registration_path is delegated to rails_programmers_path' do
|
90
|
+
expect(registration_path(:rails_programmer)).to eq rails_programmers_path
|
91
|
+
end
|
92
|
+
it 'registration_path is delegated to current_rails_programmer_path on current_rails_programmers' do
|
93
|
+
instance_eval { def controller_name; 'current_rails_programmers' end }
|
94
|
+
expect(registration_path(:rails_programmer)).to eq current_rails_programmer_path
|
95
|
+
instance_eval { undef controller_name }
|
96
|
+
end
|
97
|
+
it 'new_registration_path is delegated to new_rails_programmer_path' do
|
98
|
+
expect(new_registration_path(:rails_programmer)).to eq new_rails_programmer_path
|
99
|
+
end
|
100
|
+
it 'cancel_registration_path is delegated to cancel_current_rails_programmer_path' do
|
101
|
+
expect(cancel_registration_path(:rails_programmer)).to eq cancel_current_rails_programmer_path
|
102
|
+
end
|
103
|
+
it 'edit_registration_path is delegated to edit_current_rails_programmer_path' do
|
104
|
+
expect(edit_registration_path(:rails_programmer)).to eq edit_current_rails_programmer_path
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
File without changes
|
@@ -0,0 +1,13 @@
|
|
1
|
+
// This is a manifest file that'll be compiled into application.js, which will include all the files
|
2
|
+
// listed below.
|
3
|
+
//
|
4
|
+
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
|
5
|
+
// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
|
6
|
+
//
|
7
|
+
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
|
8
|
+
// compiled file.
|
9
|
+
//
|
10
|
+
// Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details
|
11
|
+
// about supported directives.
|
12
|
+
//
|
13
|
+
//= require_tree .
|
@@ -0,0 +1,13 @@
|
|
1
|
+
/*
|
2
|
+
* This is a manifest file that'll be compiled into application.css, which will include all the files
|
3
|
+
* listed below.
|
4
|
+
*
|
5
|
+
* Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
|
6
|
+
* or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path.
|
7
|
+
*
|
8
|
+
* You're free to add application-wide styles to this file and they'll appear at the top of the
|
9
|
+
* compiled file, but it's generally better to create a new file per style scope.
|
10
|
+
*
|
11
|
+
*= require_self
|
12
|
+
*= require_tree .
|
13
|
+
*/
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
@@ -0,0 +1,14 @@
|
|
1
|
+
<!DOCTYPE html>
|
2
|
+
<html>
|
3
|
+
<head>
|
4
|
+
<title>RailsApp</title>
|
5
|
+
<%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true %>
|
6
|
+
<%= javascript_include_tag "application", "data-turbolinks-track" => true %>
|
7
|
+
<%= csrf_meta_tags %>
|
8
|
+
</head>
|
9
|
+
<body>
|
10
|
+
|
11
|
+
<%= yield %>
|
12
|
+
|
13
|
+
</body>
|
14
|
+
</html>
|