#-*- encoding:utf-8 -*- class AvaScaffoldGenerator < Rails::Generator::NamedBase default_options :skip_timestamps => false, :skip_migration => false, :force_plural => false attr_reader :controller_name, :controller_class_path, :controller_file_path, :controller_class_nesting, :controller_class_nesting_depth, :controller_class_name, :controller_underscore_name, :controller_singular_name, :controller_plural_name alias_method :controller_file_name, :controller_underscore_name alias_method :controller_table_name, :controller_plural_name def initialize(runtime_args, runtime_options = {}) super if @name == @name.pluralize && !options[:force_plural] logger.warning "Plural version of the model detected, using singularized version. Override with --force-plural." @name = @name.singularize assign_names!(@name) end @controller_name = @name.pluralize base_name, @controller_class_path, @controller_file_path, @controller_class_nesting, @controller_class_nesting_depth = extract_modules(@controller_name) @controller_class_name_without_nesting, @controller_underscore_name, @controller_plural_name = inflect_names(base_name) @controller_singular_name=base_name.singularize if @controller_class_nesting.empty? @controller_class_name = @controller_class_name_without_nesting else @controller_class_name = "#{@controller_class_nesting}::#{@controller_class_name_without_nesting}" end end def manifest record do |m| # Check for class naming collisions. m.class_collisions("#{controller_class_name}Controller", "#{controller_class_name}Helper") m.class_collisions(class_name) # Controller, helper, views, test and stylesheets directories. m.directory('app/models') m.directory('app/controllers') m.directory('app/helpers') m.directory(File.join('app/views', controller_file_name)) m.directory('app/views/layouts') m.directory('test/functional') m.directory('test/unit') m.directory('test/unit/helpers') m.directory('public/stylesheets') # Views. for action in scaffold_views m.template("view_#{action}.html.erb", File.join('app/views', controller_file_name, "#{action}.html.erb")) end # Layout and stylesheet. m.template('controller.rb', File.join('app/controllers', "#{controller_file_name}_controller.rb")) m.template('functional_test.rb', File.join('test/functional', "#{controller_file_name}_controller_test.rb")) m.template('helper_test.rb', File.join('test/unit/helpers', "#{controller_file_name}_helper_test.rb")) # routes.rb resource = controller_file_name.to_a.map { |r| r.to_sym.inspect }.join(', ') f = open("config/routes.rb") c = f.read.include?("map.resources #{resource}") f.close unless c m.route_resources controller_file_name sentinel = "map.resources #{resource}" m.gsub_file 'config/routes.rb', /(#{Regexp.escape(sentinel)})/mi do |match| "#{match}, :collection => {:check => :post}, :member => {:confirm => :put, :edit => :put}" end end # translation_ja.yml filename = "config/locales/translation_ja.yml" f = open(filename) c = f.read.include?(" #{name}:") f.close unless c sentinel = " models:\n" m.gsub_file filename, /(#{Regexp.escape(sentinel)})/mi do |match| s = " models:\n #{name}: \"#{name}\"\n" match.gsub(match, s) end sentinel = " attributes:" m.gsub_file filename, /(#{Regexp.escape(sentinel)})/mi do |match| s = <<"EOS" attributes: #{name}: EOS s += " id: \"ID\"\n" for attribute in attributes s += " #{attribute.name}: \"#{attribute.name}\"\n" end match.gsub(match, s) end end # application_helper.rb f = open("app/helpers/application_helper.rb") c = f.read.include?("link_to(:#{name}, :controller => :#{name}s)") f.close unless c sentinel = '# str = str + "
  • #{link_to(:top, :controller => :tops)}
  • \n"' m.gsub_file 'app/helpers/application_helper.rb', /(#{Regexp.escape(sentinel)})/mi do |match| match.gsub(match, sentinel.gsub("top", name).gsub("# ", " ") + "\n" + sentinel) end end # Model. m.dependency 'model', [name] + @args, :collision => :skip end end protected # Override with your own usage banner. def banner "Usage: #{$0} ava_scaffold ModelName [field:type, field:type]" end def add_options!(opt) opt.separator '' opt.separator 'Options:' opt.on("--skip-timestamps", "Don't add timestamps to the migration file for this model") { |v| options[:skip_timestamps] = v } opt.on("--skip-migration", "Don't generate a migration file for this model") { |v| options[:skip_migration] = v } opt.on("--force-plural", "Forces the generation of a plural ModelName") { |v| options[:force_plural] = v } end def scaffold_views %w[ index show new edit check confirm ] end def model_name class_name.demodulize end end