thoughtbot-clearance 0.3.7 → 0.3.8
Sign up to get free protection for your applications and to get access to all the features.
- data/README.textile +147 -60
- data/Rakefile +29 -21
- data/TODO.textile +1 -15
- data/generators/clearance/clearance_generator.rb +31 -9
- data/generators/clearance/lib/insert_commands.rb +103 -0
- data/generators/clearance/lib/rake_commands.rb +22 -0
- data/generators/clearance/templates/README +37 -0
- data/generators/clearance/templates/app/views/clearance_mailer/change_password.html.erb +3 -1
- data/generators/clearance/templates/app/views/passwords/edit.html.erb +4 -4
- data/generators/clearance/templates/db/migrate/create_users_with_clearance_columns.rb +21 -0
- data/generators/clearance/templates/db/migrate/update_users_with_clearance_columns.rb +41 -0
- data/generators/clearance/templates/test/{factories.rb → factories/clearance_user.rb} +1 -1
- data/lib/clearance.rb +3 -4
- data/lib/clearance/app/controllers/application_controller.rb +47 -61
- data/lib/clearance/app/controllers/confirmations_controller.rb +25 -33
- data/lib/clearance/app/controllers/passwords_controller.rb +41 -46
- data/lib/clearance/app/controllers/sessions_controller.rb +49 -56
- data/lib/clearance/app/controllers/users_controller.rb +21 -29
- data/lib/clearance/app/models/clearance_mailer.rb +15 -19
- data/lib/clearance/app/models/user.rb +53 -56
- data/lib/clearance/test/functional/confirmations_controller_test.rb +4 -35
- data/lib/clearance/test/functional/passwords_controller_test.rb +9 -5
- data/lib/clearance/test/functional/sessions_controller_test.rb +67 -17
- data/lib/clearance/test/functional/users_controller_test.rb +15 -7
- data/lib/clearance/test/test_helper.rb +10 -81
- data/lib/clearance/test/unit/clearance_mailer_test.rb +5 -5
- data/lib/clearance/test/unit/user_test.rb +42 -59
- data/rails/init.rb +1 -0
- data/shoulda_macros/clearance.rb +45 -0
- metadata +14 -6
- data/generators/clearance/templates/app/views/confirmations/new.html.erb +0 -6
- data/lib/clearance/version.rb +0 -7
data/README.textile
CHANGED
@@ -1,33 +1,38 @@
|
|
1
1
|
h1. Clearance
|
2
2
|
|
3
|
-
|
3
|
+
Rails authentication for developers who write tests.
|
4
4
|
|
5
5
|
"We have clearance, Clarence.":http://www.youtube.com/v/mNRXJEE3Nz8
|
6
6
|
|
7
|
+
h2. Integration with Suspenders
|
8
|
+
|
9
|
+
Clearance is based on the same conventions and tools as "Suspenders":http://github.com/thoughtbot/suspenders Thus if you use it, you may already have some configuration mentioned below in place.
|
10
|
+
|
7
11
|
h2. Gem installation (Rails 2.1+)
|
8
12
|
|
9
|
-
In config/
|
13
|
+
In config/environment.rb:
|
10
14
|
|
11
15
|
config.gem 'mocha'
|
12
16
|
config.gem 'thoughtbot-shoulda',
|
13
|
-
:lib
|
14
|
-
:source
|
17
|
+
:lib => 'shoulda',
|
18
|
+
:source => "http://gems.github.com",
|
19
|
+
:version => '>= 2.0.6'
|
15
20
|
config.gem 'thoughtbot-factory_girl',
|
16
|
-
:lib
|
17
|
-
:source
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
21
|
+
:lib => 'factory_girl',
|
22
|
+
:source => "http://gems.github.com",
|
23
|
+
:version => '>= 1.1.5'
|
24
|
+
config.gem "thoughtbot-clearance",
|
25
|
+
:lib => 'clearance',
|
26
|
+
:source => 'http://gems.github.com'
|
22
27
|
|
23
28
|
Then:
|
24
29
|
|
25
30
|
rake gems:install
|
26
31
|
rake gems:unpack
|
27
32
|
|
28
|
-
h2.
|
33
|
+
h2. The generator
|
29
34
|
|
30
|
-
In a greenfield application, just run the generator:
|
35
|
+
In a greenfield application, make sure the development database exists and just run the generator:
|
31
36
|
|
32
37
|
script/generate clearance
|
33
38
|
|
@@ -39,7 +44,6 @@ This will create:
|
|
39
44
|
app/controllers/users_controller.rb
|
40
45
|
app/models/user.rb
|
41
46
|
app/models/user_mailer.rb
|
42
|
-
app/views/confirmations/new.html.erb
|
43
47
|
app/views/passwords/edit.html.erb
|
44
48
|
app/views/passwords/new.html.erb
|
45
49
|
app/views/sessions/new.html.erb
|
@@ -52,29 +56,64 @@ This will create:
|
|
52
56
|
test/functional/passwords_controller_test.rb
|
53
57
|
test/functional/sessions_controller_test.rb
|
54
58
|
test/functional/users_controller_test.rb
|
55
|
-
test/unit/
|
59
|
+
test/unit/clearance_mailer_test.rb
|
56
60
|
test/unit/user_test.rb
|
57
61
|
|
58
|
-
|
62
|
+
You may already have some of these files. Don't worry. You'll be asked if you want to overwrite them.
|
59
63
|
|
60
|
-
|
61
|
-
include Clearance::App::Models::User
|
64
|
+
h2. Modules
|
62
65
|
|
63
|
-
|
66
|
+
Clearance works by mixing behavior into tests, controllers, and models. For any file that you do not want to overwrite, include the corresponding Clearance module. They are namespaced exactly like the directory structure of a Rails app.
|
64
67
|
|
65
|
-
|
68
|
+
Application controller example:
|
66
69
|
|
67
|
-
|
68
|
-
|
70
|
+
class ApplicationController < ActionController::Base
|
71
|
+
include Clearance::App::Controllers::ApplicationController
|
69
72
|
end
|
70
73
|
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
74
|
+
User model example:
|
75
|
+
|
76
|
+
class User < ActiveRecord::Base
|
77
|
+
include Clearance::App::Models::User
|
75
78
|
end
|
76
79
|
|
77
|
-
|
80
|
+
User test example:
|
81
|
+
|
82
|
+
class UserTest < Test::Unit::TestCase
|
83
|
+
include Clearance::Test::Unit::UserTest
|
84
|
+
end
|
85
|
+
|
86
|
+
h2. The migration
|
87
|
+
|
88
|
+
The generator will also create a migration to add a "users" table and run it. If the table already exists in the database, the migration will just add fields and indexes that are missing and required by Clearance. If the migration fails, the generator will revert all changes back.
|
89
|
+
|
90
|
+
h2. Routes
|
91
|
+
|
92
|
+
Clearance will add these routes to your routes.rb:
|
93
|
+
|
94
|
+
map.resources :users, :has_one => [:password, :confirmation]
|
95
|
+
map.resource :session
|
96
|
+
map.resources :passwords
|
97
|
+
|
98
|
+
Please note that Clearance depends on root_url, so please make sure that it is defined to *something* in your config/routes.rb:
|
99
|
+
|
100
|
+
map.root :controller => 'home'
|
101
|
+
|
102
|
+
h2. Environments
|
103
|
+
|
104
|
+
You need to define HOST constant in your environments files. In config/environments/test.rb and config/environments/development.rb it can be:
|
105
|
+
|
106
|
+
HOST = "localhost"
|
107
|
+
|
108
|
+
While in config/environments/production.rb it must be the actual host your application is deployed to because the constant is used by mailers to generate URLs in emails.
|
109
|
+
|
110
|
+
In config/environment.rb:
|
111
|
+
|
112
|
+
DO_NOT_REPLY = "donotreply@example.com"
|
113
|
+
|
114
|
+
h2. Tests
|
115
|
+
|
116
|
+
The tests use "Shoulda":http://thoughtbot.com/projects/shoulda >= 2.0.4 and "Factory Girl":http://thoughtbot.com/projects/factory_girl. You need to add the Clearance module to your test/test_helper.rb:
|
78
117
|
|
79
118
|
class Test::Unit::TestCase
|
80
119
|
self.use_transactional_fixtures = true
|
@@ -82,53 +121,100 @@ In test/test_helper.rb:
|
|
82
121
|
include Clearance::Test::TestHelper
|
83
122
|
end
|
84
123
|
|
85
|
-
|
124
|
+
The generator will create a user factory in test/factories/clearance_user.rb unless
|
125
|
+
you have it defined somewhere else.
|
86
126
|
|
87
|
-
|
127
|
+
h2. Usage: basic workflow
|
88
128
|
|
89
|
-
|
90
|
-
helper :all
|
91
|
-
protect_from_forgery
|
92
|
-
include Clearance::App::Controllers::ApplicationController
|
93
|
-
end
|
129
|
+
Rails authentication with Clearance uses the standard approach thoughtbot and our clients have agreed upon.
|
94
130
|
|
95
|
-
|
131
|
+
Users register (UsersController) using an email address and a password (User model). They get an email with a confirmation link (ClearanceMailer) to confirm their registration (ConfirmationController).
|
96
132
|
|
97
|
-
|
133
|
+
Registered users can sign in and out (SessionsController). If they forget their password, they request an email containing a link to change it (PasswordsController).
|
98
134
|
|
99
|
-
|
100
|
-
t.string :email
|
101
|
-
t.string :crypted_password, :limit => 40
|
102
|
-
t.string :salt, :limit => 40
|
103
|
-
t.string :remember_token
|
104
|
-
t.datetime :remember_token_expires_at
|
105
|
-
t.boolean :confirmed, :default => false, :null => false
|
106
|
-
end
|
135
|
+
h2. Usage: actions which require an authenticated user
|
107
136
|
|
108
|
-
|
109
|
-
add_index :users, [:id, :salt]
|
110
|
-
add_index :users, :email
|
111
|
-
add_index :users, :remember_token
|
137
|
+
To protect your controllers with authentication add:
|
112
138
|
|
113
|
-
|
139
|
+
class ProtectedController < ApplicationController
|
140
|
+
before_filter :authenticate
|
114
141
|
|
115
|
-
|
116
|
-
map.resource :session
|
117
|
-
map.resources :users, :has_one => :password
|
118
|
-
map.resources :users, :has_one => :confirmation
|
119
|
-
map.resources :passwords
|
120
|
-
map.root :controller => 'sessions', :action => 'new'
|
142
|
+
The filter will ensure that only authenticated users can access the controller. If someone who's not logged in tries to access a protected action:
|
121
143
|
|
122
|
-
|
144
|
+
* the URL is stored in the session,
|
145
|
+
* the user is redirected to log in page, and
|
146
|
+
* after successful authentication will be be redirected back to that URL.
|
123
147
|
|
124
|
-
|
148
|
+
h2. Usage: logged_in?, current_user
|
125
149
|
|
126
|
-
|
150
|
+
Clearance provides two methods that can be used in controllers, helpers, and views to check if current user is authenticated and get the actual user:
|
127
151
|
|
128
|
-
|
152
|
+
* logged_in?
|
153
|
+
* current_user
|
129
154
|
|
130
|
-
|
131
|
-
|
155
|
+
This may be familiar to you if you've used "restful_authentication":http://github.com/technoweenie/restful-authentication.
|
156
|
+
|
157
|
+
<% if logged_in? -%>
|
158
|
+
Hello, <%= current_user.name %>!
|
159
|
+
<% else -%>
|
160
|
+
Please <%= link_to 'Log in', new_session_path %>
|
161
|
+
<% end -%>
|
162
|
+
|
163
|
+
h2. Usage: mass assignment
|
164
|
+
|
165
|
+
Please note that all User attributes except email, password and password_confirmation are protected from mass assignment by default. Use attr_accessible to enable it for your custom attributes.
|
166
|
+
|
167
|
+
class User < ActiveRecord::Base
|
168
|
+
include Clearance::App::Models::User
|
169
|
+
attr_accessible :first_name, :last_name
|
170
|
+
...
|
171
|
+
|
172
|
+
h2. Hooks and overwritables: return_to parameter
|
173
|
+
|
174
|
+
If you want to specify where to redirect a user (say you want to have a login box on every page and redirect the user to the same page) after he/she signs in, you can add a "return_to" parameter to the request (thanks to "Phillippe":http://www.sivarg.com/2009/01/clearance-coming-from-where-your-were.html for the tip):
|
175
|
+
|
176
|
+
<% form_for :session, :url => session_path(:return_to => request.request_uri) do |form| %>
|
177
|
+
...
|
178
|
+
|
179
|
+
h2. Hooks and overwritables: url_after_create, url_after_destroy
|
180
|
+
|
181
|
+
Actions that redirect (create and destroy) in Clearance controllers are customizable. If you want to redirect a user to a specific route after signing in, overwrite the "url_after_create" method in the SessionsController:
|
182
|
+
|
183
|
+
class SessionsController < ApplicationController
|
184
|
+
include Clearance::App::Controllers::SessionsController
|
185
|
+
|
186
|
+
private
|
187
|
+
|
188
|
+
def url_after_create
|
189
|
+
new_blog_post_path
|
190
|
+
end
|
191
|
+
end
|
192
|
+
|
193
|
+
There are similar methods in other controllers as well:
|
194
|
+
|
195
|
+
UsersController#url_after_create (sign up)
|
196
|
+
SessionsController#url_after_create (sign in)
|
197
|
+
SessionsController#url_after_destroy (sign out)
|
198
|
+
PasswordsController#url_after_create (password reset request)
|
199
|
+
ConfirmationsController#url_after_create (confirmation)
|
200
|
+
|
201
|
+
h2. Hooks and overrides: log_user_in
|
202
|
+
|
203
|
+
Say you want to add a last_logged_in_at attribute to your User model. You would want to update it when the User logs in.
|
204
|
+
|
205
|
+
Clearance has a method named log_user_in that you can override with that logic. Be sure to call login(user) at the end (and write tests!).
|
206
|
+
|
207
|
+
class ApplicationController < ActionController::Base
|
208
|
+
include Clearance::App::Controllers::ApplicationController
|
209
|
+
|
210
|
+
private
|
211
|
+
|
212
|
+
def log_user_in(user)
|
213
|
+
# store current time to display "last logged in at" message
|
214
|
+
user.update_attribute(:last_logged_in_at, Time.now)
|
215
|
+
login(user)
|
216
|
+
end
|
217
|
+
end
|
132
218
|
|
133
219
|
h2. Authors
|
134
220
|
|
@@ -138,3 +224,4 @@ h2. Authors
|
|
138
224
|
* Mike Burns
|
139
225
|
* Josh Nichols
|
140
226
|
* Mike Breen
|
227
|
+
* Eugene Bolshakov
|
data/Rakefile
CHANGED
@@ -3,22 +3,30 @@ require 'rake/testtask'
|
|
3
3
|
|
4
4
|
test_files_pattern = 'test/rails_root/test/{unit,functional,other}/**/*_test.rb'
|
5
5
|
namespace :test do
|
6
|
-
Rake::TestTask.new(:all => 'generator:
|
7
|
-
|
8
|
-
|
9
|
-
|
6
|
+
Rake::TestTask.new(:all => ['generator:cleanup', 'generator:generate']) do |task|
|
7
|
+
task.libs << 'lib'
|
8
|
+
task.pattern = test_files_pattern
|
9
|
+
task.verbose = false
|
10
10
|
end
|
11
11
|
end
|
12
12
|
|
13
13
|
namespace :generator do
|
14
|
-
desc "
|
15
|
-
task :
|
16
|
-
FileList["generators/clearance/templates/**/*.*"].each do |
|
17
|
-
|
14
|
+
desc "Cleans up the test app before running the generator"
|
15
|
+
task :cleanup do
|
16
|
+
FileList["generators/clearance/templates/**/*.*"].each do |each|
|
17
|
+
file = "test/rails_root/#{each.gsub("generators/clearance/templates/",'')}"
|
18
|
+
File.delete(file) if File.exists?(file)
|
18
19
|
end
|
19
|
-
|
20
|
+
|
21
|
+
FileUtils.rm_rf("test/rails_root/db/migrate")
|
22
|
+
FileUtils.rm_rf("test/rails_root/vendor/plugins/clearance")
|
23
|
+
system "cp generators/clearance/templates/config/routes.rb test/rails_root/config"
|
20
24
|
system "mkdir -p test/rails_root/vendor/plugins/clearance"
|
21
|
-
system "cp -R generators test/rails_root/vendor/plugins/clearance"
|
25
|
+
system "cp -R generators test/rails_root/vendor/plugins/clearance"
|
26
|
+
end
|
27
|
+
|
28
|
+
desc "Run the generator on the tests"
|
29
|
+
task :generate do
|
22
30
|
system "cd test/rails_root && ./script/generate clearance"
|
23
31
|
end
|
24
32
|
end
|
@@ -26,20 +34,20 @@ end
|
|
26
34
|
desc "Run the test suite"
|
27
35
|
task :default => 'test:all'
|
28
36
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
37
|
+
gem_spec = Gem::Specification.new do |gem_spec|
|
38
|
+
gem_spec.name = "clearance"
|
39
|
+
gem_spec.version = '0.3.8'
|
40
|
+
gem_spec.summary = "Simple, complete Rails authentication."
|
41
|
+
gem_spec.email = "support@thoughtbot.com"
|
42
|
+
gem_spec.homepage = "http://github.com/thoughtbot/clearance"
|
43
|
+
gem_spec.description = "Simple, complete Rails authentication scheme."
|
44
|
+
gem_spec.authors = ["thoughtbot, inc.", "Josh Nichols", "Mike Breen"]
|
45
|
+
gem_spec.files = FileList["[A-Z]*", "{generators,lib,shoulda_macros,rails}/**/*"]
|
38
46
|
end
|
39
47
|
|
40
48
|
desc "Generate a gemspec file"
|
41
49
|
task :gemspec do
|
42
|
-
File.open("#{
|
43
|
-
f.write
|
50
|
+
File.open("#{gem_spec.name}.gemspec", 'w') do |f|
|
51
|
+
f.write gem_spec.to_yaml
|
44
52
|
end
|
45
53
|
end
|
data/TODO.textile
CHANGED
@@ -1,20 +1,6 @@
|
|
1
1
|
(highest priority first)
|
2
2
|
|
3
|
-
#
|
4
|
-
# remove dependency on root_url?
|
5
|
-
# generator should print out instructions to include modules existing files
|
6
|
-
# check to make sure attr_accessible doesn't override and w/ attr_protected
|
7
|
-
# move shoulda macros in test_helper to shoulda_macros folder
|
8
|
-
# refactor Mailer default_url_options[:host] to something cleaner
|
9
|
-
# application controller uses protected, all other controllers use private
|
10
|
-
# add information about url_after_create to README or github wikis
|
11
|
-
|
12
|
-
ideas to steal from merb-auth:
|
13
|
-
|
14
|
-
# store current_user on the session, not controller
|
15
|
-
# respond with 401 Unauthorized when request requires authentication
|
16
|
-
# respond with 405 Method Not Allowed when action requested isn’t allowed
|
17
|
-
# 401 and 405 need to be in Exceptions controller or use safety_valve
|
3
|
+
# existing_user? methods ... if salt is wrong, user may not be found b/c of invalid credentials. is :not_found the correct code to return in that use case? if not, method probably needs to be split into another conditional.
|
18
4
|
# email confirmation is a strategy
|
19
5
|
# forgot password is a strategy
|
20
6
|
# salted password is a strategy
|
@@ -1,10 +1,20 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + "/lib/insert_commands.rb")
|
2
|
+
require File.expand_path(File.dirname(__FILE__) + "/lib/rake_commands.rb")
|
3
|
+
require 'factory_girl'
|
4
|
+
|
1
5
|
class ClearanceGenerator < Rails::Generator::Base
|
2
6
|
|
3
7
|
def manifest
|
4
8
|
record do |m|
|
5
9
|
m.directory File.join("app", "controllers")
|
6
|
-
|
7
|
-
|
10
|
+
file = "app/controllers/application.rb"
|
11
|
+
if File.exists?(file)
|
12
|
+
m.insert_into file, "include Clearance::App::Controllers::ApplicationController"
|
13
|
+
else
|
14
|
+
m.file file, file
|
15
|
+
end
|
16
|
+
|
17
|
+
["app/controllers/confirmations_controller.rb",
|
8
18
|
"app/controllers/passwords_controller.rb",
|
9
19
|
"app/controllers/sessions_controller.rb",
|
10
20
|
"app/controllers/users_controller.rb"].each do |file|
|
@@ -12,16 +22,11 @@ class ClearanceGenerator < Rails::Generator::Base
|
|
12
22
|
end
|
13
23
|
|
14
24
|
m.directory File.join("app", "models")
|
15
|
-
["app/models/user.rb",
|
16
|
-
"app/models/clearance_mailer.rb"].each do |file|
|
25
|
+
["app/models/user.rb", "app/models/clearance_mailer.rb"].each do |file|
|
17
26
|
m.file file, file
|
18
27
|
end
|
19
28
|
|
20
29
|
m.directory File.join("app", "views")
|
21
|
-
m.directory File.join("app", "views", "confirmations")
|
22
|
-
["app/views/confirmations/new.html.erb"].each do |file|
|
23
|
-
m.file file, file
|
24
|
-
end
|
25
30
|
|
26
31
|
m.directory File.join("app", "views", "passwords")
|
27
32
|
["app/views/passwords/new.html.erb",
|
@@ -61,9 +66,26 @@ class ClearanceGenerator < Rails::Generator::Base
|
|
61
66
|
m.file file, file
|
62
67
|
end
|
63
68
|
|
64
|
-
|
69
|
+
m.directory File.join("test", "factories")
|
70
|
+
["test/factories/clearance_user.rb"].each do |file|
|
65
71
|
m.file file, file
|
66
72
|
end
|
73
|
+
|
74
|
+
m.route_resources ':passwords'
|
75
|
+
m.route_resource ':session'
|
76
|
+
m.route_resources ':users, :has_one => [:password, :confirmation]'
|
77
|
+
|
78
|
+
if ActiveRecord::Base.connection.table_exists?(:users)
|
79
|
+
m.migration_template 'db/migrate/update_users_with_clearance_columns.rb',
|
80
|
+
'db/migrate', :migration_file_name => 'create_or_update_users_with_clearance_columns'
|
81
|
+
else
|
82
|
+
m.migration_template 'db/migrate/create_users_with_clearance_columns.rb',
|
83
|
+
'db/migrate', :migration_file_name => 'create_or_update_users_with_clearance_columns'
|
84
|
+
end
|
85
|
+
|
86
|
+
m.rake_db_migrate
|
87
|
+
|
88
|
+
m.readme "README"
|
67
89
|
end
|
68
90
|
end
|
69
91
|
|
@@ -0,0 +1,103 @@
|
|
1
|
+
# Mostly pinched from http://github.com/ryanb/nifty-generators/tree/master
|
2
|
+
|
3
|
+
Rails::Generator::Commands::Base.class_eval do
|
4
|
+
def file_contains?(relative_destination, line)
|
5
|
+
File.read(destination_path(relative_destination)).include?(line)
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
Rails::Generator::Commands::Create.class_eval do
|
10
|
+
|
11
|
+
def route_resources(resource_list)
|
12
|
+
sentinel = 'ActionController::Routing::Routes.draw do |map|'
|
13
|
+
|
14
|
+
logger.route "map.resources #{resource_list}"
|
15
|
+
unless options[:pretend] || file_contains?('config/routes.rb', resource_list)
|
16
|
+
gsub_file 'config/routes.rb', /(#{Regexp.escape(sentinel)})/mi do |match|
|
17
|
+
"#{match}\n map.resources #{resource_list}"
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def route_resource(resource_list)
|
23
|
+
sentinel = 'ActionController::Routing::Routes.draw do |map|'
|
24
|
+
|
25
|
+
logger.route "map.resource #{resource_list}"
|
26
|
+
unless options[:pretend] || file_contains?('config/routes.rb', resource_list)
|
27
|
+
gsub_file 'config/routes.rb', /(#{Regexp.escape(sentinel)})/mi do |match|
|
28
|
+
"#{match}\n map.resource #{resource_list}"
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def route_name(name, path, route_options = {})
|
34
|
+
sentinel = 'ActionController::Routing::Routes.draw do |map|'
|
35
|
+
|
36
|
+
logger.route "map.#{name} '#{path}', :controller => '#{route_options[:controller]}', :action => '#{route_options[:action]}'"
|
37
|
+
unless options[:pretend]
|
38
|
+
gsub_file_once 'config/routes.rb', /(#{Regexp.escape(sentinel)})/mi do |match|
|
39
|
+
"#{match}\n map.#{name} '#{path}', :controller => '#{route_options[:controller]}', :action => '#{route_options[:action]}'"
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def insert_into(file, line)
|
45
|
+
logger.insert "#{line} into #{file}"
|
46
|
+
unless options[:pretend] || file_contains?(file, line)
|
47
|
+
gsub_file file, /^(class|module) .+$/ do |match|
|
48
|
+
"#{match}\n #{line}"
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
Rails::Generator::Commands::Destroy.class_eval do
|
55
|
+
def route_resource(resource_list)
|
56
|
+
look_for = " map.resource #{resource_list}\n".gsub(/[\[\]]/, '\\\\\0')
|
57
|
+
logger.route "map.resource #{resource_list} #{look_for}"
|
58
|
+
unless options[:pretend]
|
59
|
+
gsub_file 'config/routes.rb', /(#{look_for})/mi, ''
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def route_resources(resource_list)
|
64
|
+
look_for = " map.resources #{resource_list}\n".gsub(/[\[\]]/, '\\\\\0')
|
65
|
+
logger.route "map.resources #{resource_list} #{look_for}"
|
66
|
+
unless options[:pretend]
|
67
|
+
gsub_file 'config/routes.rb', /(#{look_for})/mi, ''
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
def route_name(name, path, route_options = {})
|
72
|
+
look_for = "\n map.#{name} '#{path}', :controller => '#{route_options[:controller]}', :action => '#{route_options[:action]}'"
|
73
|
+
logger.route "map.#{name} '#{path}', :controller => '#{route_options[:controller]}', :action => '#{route_options[:action]}'"
|
74
|
+
unless options[:pretend]
|
75
|
+
gsub_file 'config/routes.rb', /(#{look_for})/mi, ''
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
def insert_into(file, line)
|
80
|
+
logger.remove "#{line} from #{file}"
|
81
|
+
unless options[:pretend]
|
82
|
+
gsub_file file, "\n #{line}", ''
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
Rails::Generator::Commands::List.class_eval do
|
88
|
+
def route_resource(resources_list)
|
89
|
+
logger.route "map.resource #{resource_list}"
|
90
|
+
end
|
91
|
+
|
92
|
+
def route_resources(resources_list)
|
93
|
+
logger.route "map.resource #{resource_list}"
|
94
|
+
end
|
95
|
+
|
96
|
+
def route_name(name, path, options = {})
|
97
|
+
logger.route "map.#{name} '#{path}', :controller => '{options[:controller]}', :action => '#{options[:action]}'"
|
98
|
+
end
|
99
|
+
|
100
|
+
def insert_into(file, line)
|
101
|
+
logger.insert "#{line} into #{file}"
|
102
|
+
end
|
103
|
+
end
|