changelog shortlog tags changeset manifest revisions annotate raw

vendor/plugins/restful_authentication/generators/authenticated/authenticated_generator.rb

changeset 4: 43c5e6930eee
author: moriq@moriq.com
date: Wed Mar 05 01:17:41 2008 +0900 (16 years ago)
permissions: -rw-r--r--
description: add plugin restful_authentication.
1require 'restful_authentication/rails_commands'
2class AuthenticatedGenerator < Rails::Generator::NamedBase
3 default_options :skip_migration => false,
4 :include_activation => false
5
6 attr_reader :controller_name,
7 :controller_class_path,
8 :controller_file_path,
9 :controller_class_nesting,
10 :controller_class_nesting_depth,
11 :controller_class_name,
12 :controller_singular_name,
13 :controller_plural_name,
14 :controller_file_name
15 alias_method :controller_table_name, :controller_plural_name
16 attr_reader :model_controller_name,
17 :model_controller_class_path,
18 :model_controller_file_path,
19 :model_controller_class_nesting,
20 :model_controller_class_nesting_depth,
21 :model_controller_class_name,
22 :model_controller_singular_name,
23 :model_controller_plural_name
24 alias_method :model_controller_file_name, :model_controller_singular_name
25 alias_method :model_controller_table_name, :model_controller_plural_name
26
27 def initialize(runtime_args, runtime_options = {})
28 super
29
30 @rspec = has_rspec?
31
32 @controller_name = args.shift || 'sessions'
33 @model_controller_name = @name.pluralize
34
35 # sessions controller
36 base_name, @controller_class_path, @controller_file_path, @controller_class_nesting, @controller_class_nesting_depth = extract_modules(@controller_name)
37 @controller_class_name_without_nesting, @controller_file_name, @controller_plural_name = inflect_names(base_name)
38 @controller_singular_name = @controller_file_name.singularize
39
40 if @controller_class_nesting.empty?
41 @controller_class_name = @controller_class_name_without_nesting
42 else
43 @controller_class_name = "#{@controller_class_nesting}::#{@controller_class_name_without_nesting}"
44 end
45
46 # model controller
47 base_name, @model_controller_class_path, @model_controller_file_path, @model_controller_class_nesting, @model_controller_class_nesting_depth = extract_modules(@model_controller_name)
48 @model_controller_class_name_without_nesting, @model_controller_singular_name, @model_controller_plural_name = inflect_names(base_name)
49
50 if @model_controller_class_nesting.empty?
51 @model_controller_class_name = @model_controller_class_name_without_nesting
52 else
53 @model_controller_class_name = "#{@model_controller_class_nesting}::#{@model_controller_class_name_without_nesting}"
54 end
55 end
56
57 def manifest
58 recorded_session = record do |m|
59 # Check for class naming collisions.
60 m.class_collisions controller_class_path, "#{controller_class_name}Controller", # Sessions Controller
61 "#{controller_class_name}Helper"
62 m.class_collisions model_controller_class_path, "#{model_controller_class_name}Controller", # Model Controller
63 "#{model_controller_class_name}Helper"
64 m.class_collisions class_path, "#{class_name}", "#{class_name}Mailer", "#{class_name}MailerTest", "#{class_name}Observer"
65 m.class_collisions [], 'AuthenticatedSystem', 'AuthenticatedTestHelper'
66
67 # Controller, helper, views, and test directories.
68 m.directory File.join('app/models', class_path)
69 m.directory File.join('app/controllers', controller_class_path)
70 m.directory File.join('app/controllers', model_controller_class_path)
71 m.directory File.join('app/helpers', controller_class_path)
72 m.directory File.join('app/views', controller_class_path, controller_file_name)
73 m.directory File.join('app/views', class_path, "#{file_name}_mailer") if options[:include_activation]
74
75 m.directory File.join('app/controllers', model_controller_class_path)
76 m.directory File.join('app/helpers', model_controller_class_path)
77 m.directory File.join('app/views', model_controller_class_path, model_controller_file_name)
78
79 if @rspec
80 m.directory File.join('spec/controllers', controller_class_path)
81 m.directory File.join('spec/controllers', model_controller_class_path)
82 m.directory File.join('spec/models', class_path)
83 m.directory File.join('spec/fixtures', class_path)
84 else
85 m.directory File.join('test/functional', controller_class_path)
86 m.directory File.join('test/functional', model_controller_class_path)
87 m.directory File.join('test/unit', class_path)
88 end
89
90 m.template 'model.rb',
91 File.join('app/models',
92 class_path,
93 "#{file_name}.rb")
94
95 if options[:include_activation]
96 %w( mailer observer ).each do |model_type|
97 m.template "#{model_type}.rb", File.join('app/models',
98 class_path,
99 "#{file_name}_#{model_type}.rb")
100 end
101 end
102
103 m.template 'controller.rb',
104 File.join('app/controllers',
105 controller_class_path,
106 "#{controller_file_name}_controller.rb")
107
108 m.template 'model_controller.rb',
109 File.join('app/controllers',
110 model_controller_class_path,
111 "#{model_controller_file_name}_controller.rb")
112
113 m.template 'authenticated_system.rb',
114 File.join('lib', 'authenticated_system.rb')
115
116 m.template 'authenticated_test_helper.rb',
117 File.join('lib', 'authenticated_test_helper.rb')
118
119 if @rspec
120 m.template 'functional_spec.rb',
121 File.join('spec/controllers',
122 controller_class_path,
123 "#{controller_file_name}_controller_spec.rb")
124 m.template 'model_functional_spec.rb',
125 File.join('spec/controllers',
126 model_controller_class_path,
127 "#{model_controller_file_name}_controller_spec.rb")
128 m.template 'unit_spec.rb',
129 File.join('spec/models',
130 class_path,
131 "#{file_name}_spec.rb")
132 m.template 'fixtures.yml',
133 File.join('spec/fixtures',
134 "#{table_name}.yml")
135 else
136 m.template 'functional_test.rb',
137 File.join('test/functional',
138 controller_class_path,
139 "#{controller_file_name}_controller_test.rb")
140 m.template 'model_functional_test.rb',
141 File.join('test/functional',
142 model_controller_class_path,
143 "#{model_controller_file_name}_controller_test.rb")
144 m.template 'unit_test.rb',
145 File.join('test/unit',
146 class_path,
147 "#{file_name}_test.rb")
148 if options[:include_activation]
149 m.template 'mailer_test.rb', File.join('test/unit', class_path, "#{file_name}_mailer_test.rb")
150 end
151 m.template 'fixtures.yml',
152 File.join('test/fixtures',
153 "#{table_name}.yml")
154 end
155
156 m.template 'helper.rb',
157 File.join('app/helpers',
158 controller_class_path,
159 "#{controller_file_name}_helper.rb")
160
161 m.template 'model_helper.rb',
162 File.join('app/helpers',
163 model_controller_class_path,
164 "#{model_controller_file_name}_helper.rb")
165
166
167 # Controller templates
168 m.template 'login.html.erb', File.join('app/views', controller_class_path, controller_file_name, "new.html.erb")
169 m.template 'signup.html.erb', File.join('app/views', model_controller_class_path, model_controller_file_name, "new.html.erb")
170
171 if options[:include_activation]
172 # Mailer templates
173 %w( activation signup_notification ).each do |action|
174 m.template "#{action}.html.erb",
175 File.join('app/views', "#{file_name}_mailer", "#{action}.html.erb")
176 end
177 end
178
179 unless options[:skip_migration]
180 m.migration_template 'migration.rb', 'db/migrate', :assigns => {
181 :migration_name => "Create#{class_name.pluralize.gsub(/::/, '')}"
182 }, :migration_file_name => "create_#{file_path.gsub(/\//, '_').pluralize}"
183 end
184
185 m.route_resource controller_singular_name
186 m.route_resources model_controller_plural_name
187 end
188
189 action = nil
190 action = $0.split("/")[1]
191 case action
192 when "generate"
193 puts
194 puts ("-" * 70)
195 puts "Don't forget to:"
196 puts
197 if options[:include_activation]
198 puts " map.activate '/activate/:activation_code', :controller => '#{model_controller_file_name}', :action => 'activate'"
199 puts
200 puts " - add an observer to config/environment.rb"
201 puts " config.active_record.observers = :#{file_name}_observer"
202 puts
203 end
204 if options[:stateful]
205 puts "Also, don't forget to install the acts_as_state_machine plugin and set your resource:"
206 puts
207 puts " svn co http://elitists.textdriven.com/svn/plugins/acts_as_state_machine/trunk vendor/plugins/acts_as_state_machine"
208 puts
209 puts %w(map.resources :#{model_controller_file_name}, :member => { :suspend => :put, :unsuspend => :put, :purge => :delete })
210 puts
211 end
212 puts "Try these for some familiar login URLs if you like:"
213 puts
214 puts %(map.activate '/activate/:activation_code', :controller => '#{model_controller_file_name}', :action => 'activate', :activation_code => nil)
215 puts %(map.signup '/signup', :controller => '#{model_controller_file_name}', :action => 'new')
216 puts %(map.login '/login', :controller => '#{controller_file_name}', :action => 'new')
217 puts %(map.logout '/logout', :controller => '#{controller_file_name}', :action => 'destroy')
218 puts
219 puts ("-" * 70)
220 puts
221 when "destroy"
222 puts
223 puts ("-" * 70)
224 puts
225 puts "Thanks for using restful_authentication"
226 puts
227 puts "Don't forget to comment out the observer line in environment.rb"
228 puts " (This was optional so it may not even be there)"
229 puts " # config.active_record.observers = :#{file_name}_observer"
230 puts
231 puts ("-" * 70)
232 puts
233 else
234 puts
235 end
236
237 recorded_session
238 end
239
240 def has_rspec?
241 options[:rspec] || (File.exist?('spec') && File.directory?('spec'))
242 end
243
244 protected
245 # Override with your own usage banner.
246 def banner
247 "Usage: #{$0} authenticated ModelName [ControllerName]"
248 end
249
250 def add_options!(opt)
251 opt.separator ''
252 opt.separator 'Options:'
253 opt.on("--skip-migration",
254 "Don't generate a migration file for this model") { |v| options[:skip_migration] = v }
255 opt.on("--include-activation",
256 "Generate signup 'activation code' confirmation via email") { |v| options[:include_activation] = true }
257 opt.on("--stateful",
258 "Use acts_as_state_machine. Assumes --include-activation") { |v| options[:include_activation] = options[:stateful] = true }
259 opt.on("--rspec",
260 "Force rspec mode (checks for RAILS_ROOT/spec by default)") { |v| options[:rspec] = true }
261 end
262end