devise-better_routes 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -73,7 +73,11 @@ Do you like this one?
73
73
  PUT /me(.:format) me#update
74
74
  DELETE /me(.:format) me#destroy
75
75
 
76
+ ## Changes
76
77
 
78
+ ### 0.0.3
79
+
80
+ * __Add session routes__
77
81
 
78
82
  ## Installation
79
83
 
@@ -13,11 +13,12 @@ module Devise
13
13
  cancel_registration: 'cancel_#{current_resource_name}'
14
14
  }
15
15
  [:path, :url].each do |path_or_url|
16
+ # TODO: use real helper name
16
17
  class_eval <<-URL_HELPERS, __FILE__, __LINE__ + 1
17
18
  def registration_#{path_or_url}(resource_or_scope, *args)
18
19
  scope = Devise::Mapping.find_scope!(resource_or_scope)
19
20
  current_resource_name = _devise_path_name(scope, "current_\#{scope}")
20
- if respond_to?(:controller_name) && controller_name == current_resource_name.pluralize
21
+ if respond_to?(:controller_name) && controller_name == _devise_registration_controller(scope, current_resource_name)
21
22
  send("\#{current_resource_name}_#{path_or_url}", *args)
22
23
  else
23
24
  send("\#{scope.to_s.pluralize}_#{path_or_url}", *args)
@@ -45,7 +46,17 @@ module Devise
45
46
  def _devise_path_name(scope, name)
46
47
  Devise.mappings[scope].path_names[name.to_sym]
47
48
  end
48
- private :_devise_path_name
49
+ def _devise_registration_controller(scope, name)
50
+ controllers = Devise.mappings[scope].controllers
51
+ if controllers.include?(name.to_sym)
52
+ controllers[name.to_sym]
53
+ elsif controllers.include?(:registrations)
54
+ controllers[:registrations]
55
+ else
56
+ name.pluralize
57
+ end
58
+ end
59
+ private :_devise_path_name, :_devise_registration_controller
49
60
  end
50
61
  end
51
62
 
@@ -60,11 +71,40 @@ module ActionDispatch
60
71
  module Routing
61
72
  class Mapper
62
73
  protected
74
+ def devise_session(mapping, controllers)
75
+ current_resource_name = mapping.path_names["current_#{mapping.singular}".to_sym]
76
+ path, as, @scope[:path], @scope[:as] = @scope[:path], @scope[:as], nil, nil
77
+ resource current_resource_name, only: [] do
78
+ resource :session, only: [:create, :destroy], controller: controllers[:sessions]
79
+ resource :session, only: [], controller: controllers[:sessions], path: '' do
80
+ get :new, path: mapping.path_names[:'session/new'], as: 'new'
81
+ end
82
+ end
83
+ ensure
84
+ @scope[:path], @scope[:as] = path, as
85
+ end
86
+
63
87
  def devise_registration(mapping, controllers)
64
88
  path, as, @scope[:path], @scope[:as] = @scope[:path], @scope[:as], nil, nil
65
- current_resource_name = mapping.path_names["current_#{mapping.singular}".to_sym]
66
- resources mapping.path, only: [:create, :new]
67
- resource current_resource_name, only: [:edit, :update, :destroy] do
89
+ current_resource_name = mapping.path_names["current_#{mapping.name}".to_sym]
90
+ resources_controller_name =
91
+ if controllers.include?(mapping.path.to_sym)
92
+ controllers[mapping.path.to_sym]
93
+ elsif controllers.include?(:registrations)
94
+ controllers[:registrations]
95
+ else
96
+ nil
97
+ end
98
+ current_resource_controller_name =
99
+ if controllers.include?(current_resource_name.to_sym)
100
+ controllers[current_resource_name.to_sym]
101
+ elsif controllers.include?(:registrations)
102
+ controllers[:registrations]
103
+ else
104
+ nil
105
+ end
106
+ resources mapping.path, only: [:create, :new], controller: resources_controller_name
107
+ resource current_resource_name, only: [:edit, :update, :destroy], controller: current_resource_controller_name do
68
108
  get :cancel
69
109
  end
70
110
  ensure
@@ -1,5 +1,5 @@
1
1
  module Devise
2
2
  module BetterRoutes
3
- VERSION = "0.0.2"
3
+ VERSION = "0.0.3"
4
4
  end
5
5
  end
@@ -8,46 +8,146 @@ describe Devise::BetterRoutes do
8
8
  context 'routing' do
9
9
  include RSpec::Rails::RoutingExampleGroup
10
10
  describe 'users' do
11
- it 'routes to users#create' do
12
- expect(post('/users')).to route_to('users#create')
11
+ context 'registration' do
12
+ it 'routes to users#create' do
13
+ expect(post('/users')).to route_to('users#create')
14
+ end
15
+ it 'routes to users#new' do
16
+ expect(get('/users/new')).to route_to('users#new')
17
+ end
18
+ it 'routes to current_users#cancel' do
19
+ expect(get('/current_user/cancel')).to route_to('current_users#cancel')
20
+ end
21
+ it 'routes to current_users#edit' do
22
+ expect(get('/current_user/edit')).to route_to('current_users#edit')
23
+ end
24
+ it 'routes to current_users#update' do
25
+ expect(put('/current_user')).to route_to('current_users#update')
26
+ #expect(patch('/current_user')).to route_to('current_users#update')
27
+ end
28
+ it 'routes to current_users#destroy' do
29
+ expect(delete('/current_user')).to route_to('current_users#destroy')
30
+ end
13
31
  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')
32
+
33
+ context 'session' do
34
+ it 'routes to devise/sessions#create' do
35
+ expect(post('/current_user/session')).to route_to('devise/sessions#create')
36
+ end
37
+ it 'routes to devise/sessions#destroy' do
38
+ expect(delete('/current_user/session')).to route_to('devise/sessions#destroy')
39
+ end
40
+ it 'routes to devise/sessions#new' do
41
+ expect(get('/current_user/session/new')).to route_to('devise/sessions#new')
42
+ end
29
43
  end
30
44
  end
31
45
 
32
46
  describe 'rails_programmers' do
33
- it 'routes to rails_programmers#create' do
34
- expect(post('/rails_programmers')).to route_to('rails_programmers#create')
47
+ context 'registration' do
48
+ it 'routes to rails_programmers#create' do
49
+ expect(post('/rails_programmers')).to route_to('rails_programmers#create')
50
+ end
51
+ it 'routes to rails_programmers#new' do
52
+ expect(get('/rails_programmers/new')).to route_to('rails_programmers#new')
53
+ end
54
+ it 'routes to me#cancel' do
55
+ expect(get('/me/cancel')).to route_to('me#cancel')
56
+ end
57
+ it 'routes to me#edit' do
58
+ expect(get('/me/edit')).to route_to('me#edit')
59
+ end
60
+ it 'routes to me#update' do
61
+ expect(put('/me')).to route_to('me#update')
62
+ #expect(patch('/me')).to route_to('me#update')
63
+ end
64
+ it 'routes to me#destroy' do
65
+ expect(delete('/me')).to route_to('me#destroy')
66
+ end
35
67
  end
36
- it 'routes to rails_programmers#new' do
37
- expect(get('/rails_programmers/new')).to route_to('rails_programmers#new')
68
+
69
+ context 'session' do
70
+ it 'routes to devise/sessions#create' do
71
+ expect(post('/me/session')).to route_to('devise/sessions#create')
72
+ end
73
+ it 'routes to devise/sessions#destroy' do
74
+ expect(delete('/me/session')).to route_to('devise/sessions#destroy')
75
+ end
76
+ it 'routes to devise/sessions#new' do
77
+ expect(get('/me/session/new')).to route_to('devise/sessions#new')
78
+ end
38
79
  end
39
- it 'routes to me#cancel' do
40
- expect(get('/me/cancel')).to route_to('me#cancel')
80
+ end
81
+
82
+ describe 'programmers' do
83
+ context 'registration' do
84
+ it 'routes to rails_programmers#create' do
85
+ expect(post('/programmers')).to route_to('rails_programmers#create')
86
+ end
87
+ it 'routes to rails_programmers#new' do
88
+ expect(get('/programmers/new')).to route_to('rails_programmers#new')
89
+ end
90
+ it 'routes to me#cancel' do
91
+ expect(get('/current_programmer/cancel')).to route_to('me#cancel')
92
+ end
93
+ it 'routes to me#edit' do
94
+ expect(get('/current_programmer/edit')).to route_to('me#edit')
95
+ end
96
+ it 'routes to me#update' do
97
+ expect(put('/current_programmer')).to route_to('me#update')
98
+ #expect(patch('/current_programmer')).to route_to('me#update')
99
+ end
100
+ it 'routes to me#destroy' do
101
+ expect(delete('/current_programmer')).to route_to('me#destroy')
102
+ end
41
103
  end
42
- it 'routes to me#edit' do
43
- expect(get('/me/edit')).to route_to('me#edit')
104
+
105
+ context 'session' do
106
+ it 'routes to devise/sessions#create' do
107
+ expect(post('/current_programmer/session')).to route_to('devise/sessions#create')
108
+ end
109
+ it 'routes to devise/sessions#destroy' do
110
+ expect(delete('/current_programmer/session')).to route_to('devise/sessions#destroy')
111
+ end
112
+ it 'routes to devise/sessions#new' do
113
+ expect(get('/current_programmer/session/new')).to route_to('devise/sessions#new')
114
+ end
44
115
  end
45
- it 'routes to me#update' do
46
- expect(put('/me')).to route_to('me#update')
47
- #expect(patch('/me')).to route_to('me#update')
116
+ end
117
+
118
+ describe 'engineers' do
119
+ context 'registration' do
120
+ it 'routes to rails_programmers#create' do
121
+ expect(post('/engineers')).to route_to('rails_programmers#create')
122
+ end
123
+ it 'routes to rails_programmers#new' do
124
+ expect(get('/engineers/new')).to route_to('rails_programmers#new')
125
+ end
126
+ it 'routes to rails_programmers#cancel' do
127
+ expect(get('/current_engineer/cancel')).to route_to('rails_programmers#cancel')
128
+ end
129
+ it 'routes to rails_programmers#edit' do
130
+ expect(get('/current_engineer/edit')).to route_to('rails_programmers#edit')
131
+ end
132
+ it 'routes to rails_programmers#update' do
133
+ expect(put('/current_engineer')).to route_to('rails_programmers#update')
134
+ #expect(patch('/current_engineer')).to route_to('rails_programmers#update')
135
+ end
136
+ it 'routes to rails_programmers#destroy' do
137
+ expect(delete('/current_engineer')).to route_to('rails_programmers#destroy')
138
+ end
48
139
  end
49
- it 'routes to me#destroy' do
50
- expect(delete('/me')).to route_to('me#destroy')
140
+
141
+ context 'session' do
142
+ it 'routes to devise/sessions#create' do
143
+ expect(post('/current_engineer/session')).to route_to('devise/sessions#create')
144
+ end
145
+ it 'routes to devise/sessions#destroy' do
146
+ expect(delete('/current_engineer/session')).to route_to('devise/sessions#destroy')
147
+ end
148
+ it 'routes to devise/sessions#new' do
149
+ expect(get('/current_engineer/session/new')).to route_to('devise/sessions#new')
150
+ end
51
151
  end
52
152
  end
53
153
  end
@@ -56,52 +156,130 @@ describe Devise::BetterRoutes do
56
156
  include Rails.application.routes.url_helpers
57
157
  include Devise::Controllers::UrlHelpers
58
158
  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
159
+ context 'registration' do
160
+ specify { expect(users_path).to eq '/users' }
161
+ specify { expect(new_user_path).to eq '/users/new' }
162
+ specify { expect(cancel_current_user_path).to eq '/current_user/cancel' }
163
+ specify { expect(edit_current_user_path).to eq '/current_user/edit' }
164
+ specify { expect(current_user_path).to eq '/current_user' }
165
+ it 'registration_path is delegated to users_path' do
166
+ expect(registration_path(:user)).to eq users_path
167
+ end
168
+ it 'registration_path is delegated to current_user_path on current_users' do
169
+ instance_eval { def controller_name; 'current_users' end }
170
+ expect(registration_path(:user)).to eq current_user_path
171
+ instance_eval { undef controller_name }
172
+ end
173
+ it 'new_registration_path is delegated to new_user_path' do
174
+ expect(new_registration_path(:user)).to eq new_user_path
175
+ end
176
+ it 'cancel_registration_path is delegated to cancel_current_user_path' do
177
+ expect(cancel_registration_path(:user)).to eq cancel_current_user_path
178
+ end
179
+ it 'edit_registration_path is delegated to edit_current_user_path' do
180
+ expect(edit_registration_path(:user)).to eq edit_current_user_path
181
+ end
66
182
  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 }
183
+
184
+ context 'session' do
185
+ specify { expect(current_user_session_path).to eq '/current_user/session' }
186
+ specify { expect(new_current_user_session_path).to eq '/current_user/session/new' }
187
+ end
188
+ end
189
+
190
+ describe 'rails_programmers' do
191
+ context 'registration' do
192
+ specify { expect(rails_programmers_path).to eq '/rails_programmers' }
193
+ specify { expect(new_rails_programmer_path).to eq '/rails_programmers/new' }
194
+ specify { expect(cancel_me_path).to eq '/me/cancel' }
195
+ specify { expect(edit_me_path).to eq '/me/edit' }
196
+ specify { expect(me_path).to eq '/me' }
197
+ it 'registration_path is delegated to rails_programmers_path' do
198
+ expect(registration_path(:rails_programmer)).to eq rails_programmers_path
199
+ end
200
+ it 'registration_path is delegated to me_path on me' do
201
+ instance_eval { def controller_name; 'me' end }
202
+ expect(registration_path(:rails_programmer)).to eq me_path
203
+ instance_eval { undef controller_name }
204
+ end
205
+ it 'new_registration_path is delegated to new_rails_programmer_path' do
206
+ expect(new_registration_path(:rails_programmer)).to eq new_rails_programmer_path
207
+ end
208
+ it 'cancel_registration_path is delegated to cancel_me_path' do
209
+ expect(cancel_registration_path(:rails_programmer)).to eq cancel_me_path
210
+ end
211
+ it 'edit_registration_path is delegated to edit_me_path' do
212
+ expect(edit_registration_path(:rails_programmer)).to eq edit_me_path
213
+ end
71
214
  end
72
- it 'new_registration_path is delegated to new_user_path' do
73
- expect(new_registration_path(:user)).to eq new_user_path
215
+
216
+ context 'session' do
217
+ specify { expect(me_session_path).to eq '/me/session' }
218
+ specify { expect(new_me_session_path).to eq '/me/session/new' }
74
219
  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
220
+ end
221
+
222
+ describe 'programmers' do
223
+ context 'registration' do
224
+ specify { expect(programmers_path).to eq '/programmers' }
225
+ specify { expect(new_programmer_path).to eq '/programmers/new' }
226
+ specify { expect(cancel_current_programmer_path).to eq '/current_programmer/cancel' }
227
+ specify { expect(edit_current_programmer_path).to eq '/current_programmer/edit' }
228
+ specify { expect(current_programmer_path).to eq '/current_programmer' }
229
+ it 'registration_path is delegated to programmers_path' do
230
+ expect(registration_path(:programmer)).to eq programmers_path
231
+ end
232
+ it 'registration_path is delegated to current_programmer_path on me' do
233
+ instance_eval { def controller_name; 'me' end }
234
+ expect(registration_path(:programmer)).to eq current_programmer_path
235
+ instance_eval { undef controller_name }
236
+ end
237
+ it 'new_registration_path is delegated to new_programmer_path' do
238
+ expect(new_registration_path(:programmer)).to eq new_programmer_path
239
+ end
240
+ it 'cancel_registration_path is delegated to cancel_current_programmer_path' do
241
+ expect(cancel_registration_path(:programmer)).to eq cancel_current_programmer_path
242
+ end
243
+ it 'edit_registration_path is delegated to edit_current_programmer_path' do
244
+ expect(edit_registration_path(:programmer)).to eq edit_current_programmer_path
245
+ end
77
246
  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
247
+
248
+ context 'session' do
249
+ specify { expect(current_programmer_session_path).to eq '/current_programmer/session' }
250
+ specify { expect(new_current_programmer_session_path).to eq '/current_programmer/session/new' }
80
251
  end
81
252
  end
82
253
 
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_me_path).to eq '/me/cancel' }
87
- specify { expect(edit_me_path).to eq '/me/edit' }
88
- specify { expect(me_path).to eq '/me' }
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 me_path on me' do
93
- instance_eval { def controller_name; 'me' end }
94
- expect(registration_path(:rails_programmer)).to eq me_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_me_path' do
101
- expect(cancel_registration_path(:rails_programmer)).to eq cancel_me_path
102
- end
103
- it 'edit_registration_path is delegated to edit_me_path' do
104
- expect(edit_registration_path(:rails_programmer)).to eq edit_me_path
254
+ describe 'engineers' do
255
+ context 'registration' do
256
+ specify { expect(engineers_path).to eq '/engineers' }
257
+ specify { expect(new_engineer_path).to eq '/engineers/new' }
258
+ specify { expect(cancel_current_engineer_path).to eq '/current_engineer/cancel' }
259
+ specify { expect(edit_current_engineer_path).to eq '/current_engineer/edit' }
260
+ specify { expect(current_engineer_path).to eq '/current_engineer' }
261
+ it 'registration_path is delegated to engineers_path' do
262
+ expect(registration_path(:engineer)).to eq engineers_path
263
+ end
264
+ it 'registration_path is delegated to current_engineer_path on me' do
265
+ instance_eval { def controller_name; 'rails_programmers' end }
266
+ expect(registration_path(:engineer)).to eq current_engineer_path
267
+ instance_eval { undef controller_name }
268
+ end
269
+ it 'new_registration_path is delegated to new_engineer_path' do
270
+ expect(new_registration_path(:engineer)).to eq new_engineer_path
271
+ end
272
+ it 'cancel_registration_path is delegated to cancel_current_engineer_path' do
273
+ expect(cancel_registration_path(:engineer)).to eq cancel_current_engineer_path
274
+ end
275
+ it 'edit_registration_path is delegated to edit_current_engineer_path' do
276
+ expect(edit_registration_path(:engineer)).to eq edit_current_engineer_path
277
+ end
278
+ end
279
+
280
+ context 'session' do
281
+ specify { expect(current_engineer_session_path).to eq '/current_engineer/session' }
282
+ specify { expect(new_current_engineer_session_path).to eq '/current_engineer/session/new' }
105
283
  end
106
284
  end
107
285
  end
@@ -1,3 +1,6 @@
1
1
  class User < ActiveRecord::Base
2
- devise :registerable
2
+ devise :database_authenticatable, :registerable
3
3
  end
4
+ class RailsProgrammer < User; end
5
+ class Programmer < User; end
6
+ class Engineer < User; end
@@ -1,4 +1,6 @@
1
1
  RailsApp::Application.routes.draw do
2
2
  devise_for :users
3
3
  devise_for :rails_programmers, path_names: {current_rails_programmer: 'me'}
4
+ devise_for :programmers, controllers: {programmers: 'rails_programmers', current_programmer: 'me'}
5
+ devise_for :engineers, controllers: {registrations: 'rails_programmers'}
4
6
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: devise-better_routes
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-06-25 00:00:00.000000000 Z
12
+ date: 2013-07-12 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: devise
@@ -155,7 +155,6 @@ files:
155
155
  - spec/rails_app/app/mailers/.keep
156
156
  - spec/rails_app/app/models/.keep
157
157
  - spec/rails_app/app/models/concerns/.keep
158
- - spec/rails_app/app/models/rails_programmer.rb
159
158
  - spec/rails_app/app/models/user.rb
160
159
  - spec/rails_app/app/views/layouts/application.html.erb
161
160
  - spec/rails_app/config.ru
@@ -199,7 +198,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
199
198
  version: '0'
200
199
  segments:
201
200
  - 0
202
- hash: 4262153083475956853
201
+ hash: -574275781529839639
203
202
  required_rubygems_version: !ruby/object:Gem::Requirement
204
203
  none: false
205
204
  requirements:
@@ -208,7 +207,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
208
207
  version: '0'
209
208
  segments:
210
209
  - 0
211
- hash: 4262153083475956853
210
+ hash: -574275781529839639
212
211
  requirements: []
213
212
  rubyforge_project:
214
213
  rubygems_version: 1.8.24
@@ -231,7 +230,6 @@ test_files:
231
230
  - spec/rails_app/app/mailers/.keep
232
231
  - spec/rails_app/app/models/.keep
233
232
  - spec/rails_app/app/models/concerns/.keep
234
- - spec/rails_app/app/models/rails_programmer.rb
235
233
  - spec/rails_app/app/models/user.rb
236
234
  - spec/rails_app/app/views/layouts/application.html.erb
237
235
  - spec/rails_app/config.ru
@@ -1,3 +0,0 @@
1
- class RailsProgrammer < User
2
- devise :registerable
3
- end