require'rails_generator/base'require'rails_generator/generators/components/controller/controller_generator'classExtensionControllerGenerator<ControllerGeneratorattr_accessor:extension_namedefault_options:with_test_unit=>falsedefinitialize(runtime_args,runtime_options={})runtime_args=runtime_args.dup@extension_name=runtime_args.shiftsuper(runtime_args,runtime_options)enddefmanifestifextension_uses_rspec?rspec_manifestelsesuperendenddefrspec_manifestrecorddo|m|# Check for class naming collisions.m.class_collisionsclass_path,"#{class_name}Controller","#{class_name}Helper"# Controller, helper, views, and spec directories.m.directoryFile.join('app/controllers',class_path)m.directoryFile.join('app/helpers',class_path)m.directoryFile.join('app/views',class_path,file_name)m.directoryFile.join('spec/controllers',class_path)m.directoryFile.join('spec/helpers',class_path)m.directoryFile.join('spec/views',class_path,file_name)# Controller spec, class, and helper.m.template'controller_spec.rb',File.join('spec/controllers',class_path,"#{file_name}_controller_spec.rb")m.template'helper_spec.rb',File.join('spec/helpers',class_path,"#{file_name}_helper_spec.rb")m.template'controller:controller.rb',File.join('app/controllers',class_path,"#{file_name}_controller.rb")m.template'controller:helper.rb',File.join('app/helpers',class_path,"#{file_name}_helper.rb")# Spec and view template for each action.actions.eachdo|action|m.template'view_spec.rb',File.join('spec/views',class_path,file_name,"#{action}_view_spec.rb"),:assigns=>{:action=>action,:model=>file_name}path=File.join('app/views',class_path,file_name,"#{action}.html.erb")m.template'controller:view.html.erb',path,:assigns=>{:action=>action,:path=>path}endendenddefbanner"Usage: #{$0}#{spec.name} ExtensionName #{spec.name.camelize}Name [options]"enddefextension_pathFile.join('vendor','extensions',@extension_name.underscore)enddefdestination_rootFile.join(Rails.root,extension_path)enddefextension_uses_rspec?File.exists?(File.join(destination_root,'spec'))&&!options[:with_test_unit]enddefadd_options!(opt)opt.separator''opt.separator'Options:'opt.on("--with-test-unit","Use Test::Unit tests instead sof RSpec."){|v|options[:with_test_unit]=v}endend