1 require 'rails_generator/base'
2 require 'rails_generator/generators/components/mailer/mailer_generator'
3
4 class ExtensionMailerGenerator < MailerGenerator
5
6 attr_accessor :extension_name
7 default_options :with_test_unit => false
8
9 def initialize(runtime_args, runtime_options = {})
10 runtime_args = runtime_args.dup
11 @extension_name = runtime_args.shift
12 super(runtime_args, runtime_options)
13 end
14
15 def manifest
16 if extension_uses_rspec?
17 rspec_manifest
18 else
19 super
20 end
21 end
22
23 def rspec_manifest
24 record do |m|
25 # Check for class naming collisions.
26 m.class_collisions class_path, class_name
27
28 # Mailer, view, test, and fixture directories.
29 m.directory File.join('app/models', class_path)
30 m.directory File.join('app/views', file_path)
31
32 # Mailer class and unit test.
33 m.template "mailer:mailer.rb", File.join('app/models', class_path, "#{file_name}.rb")
34
35 # View template and fixture for each action.
36 actions.each do |action|
37 relative_path = File.join(file_path, action)
38 view_path = File.join('app/views', "#{relative_path}.erb")
39
40 m.template "mailer:view.erb", view_path,
41 :assigns => { :action => action, :path => view_path }
42 end
43 end
44 end
45
46 def banner
47 "Usage: #{$0} #{spec.name} ExtensionName #{spec.name.camelize}Name [options]"
48 end
49
50 def extension_path
51 File.join('vendor', 'extensions', @extension_name.underscore)
52 end
53
54 def destination_root
55 File.join(RAILS_ROOT, extension_path)
56 end
57
58 def extension_uses_rspec?
59 File.exists?(File.join(destination_root, 'spec')) && !options[:with_test_unit]
60 end
61
62 def add_options!(opt)
63 opt.separator ''
64 opt.separator 'Options:'
65 opt.on("--with-test-unit",
66 "Use Test::Unit tests instead sof RSpec.") { |v| options[:with_test_unit] = v }
67 end
68 end