class AvaRailsGenerator < Rails::Generator::NamedBase default_options :skip_timestamps => false, :skip_migration => false, :force_plural => false attr_reader :app_color def initialize(runtime_args, runtime_options = {}) super @app_title = runtime_args.size > 0 ? runtime_args[0] : "built-in application name #TODO" @app_company = runtime_args.size > 1 ? runtime_args[1] : "built-in company name #TODO" @app_color = runtime_args.size > 2 ? runtime_args[2] : "blue" end def manifest record do |m| # Controller, helper, views, test and stylesheets directories. m.directory('app/models') m.directory('app/helpers') m.directory('app/views/top') m.directory('app/views/auth') m.directory('app/views/layouts') m.directory('public/stylesheets') m.directory('public/javascripts') m.directory('db/migrate') # Layout and stylesheet. m.file('application_controller.rb', 'app/controllers/application_controller.rb') m.file('auth_controller.rb', 'app/controllers/auth_controller.rb') m.file('top_controller.rb', 'app/controllers/top_controller.rb') m.file('application_search.rb', 'app/models/application_search.rb') m.file('user.rb', 'app/models/user.rb') m.template('index.html.erb', 'app/views/top/index.html.erb') m.template('login.html.erb', 'app/views/auth/login.html.erb') m.template('password.html.erb', 'app/views/auth/password.html.erb') m.template('application.html.erb', 'app/views/layouts/application.html.erb') m.file('application_helper.rb', 'app/helpers/application_helper.rb') m.file('action_view_helper.rb', 'lib/action_view_helper.rb') m.file('csv.rb', 'lib/csv.rb') m.file('application_csv.rb', 'lib/application_csv.rb') m.file('active_support_ja.yml', 'config/locales/active_support_ja.yml') m.file('active_record_ja.yml', 'config/locales/active_record_ja.yml') m.file('action_view_ja.yml', 'config/locales/action_view_ja.yml') m.file('translation_ja.yml', 'config/locales/translation_ja.yml') m.file('001_create_sessions.rb', 'db/migrate/001_create_sessions.rb') m.file('002_create_users.rb', 'db/migrate/002_create_users.rb') Dir.glob(source_path('*.css')).each {|f| m.file(File.basename(f), File.join('public/stylesheets', File.basename(f))) } Dir.glob(source_path('*.js')).each {|f| m.file(File.basename(f), File.join('public/javascripts', File.basename(f))) } Dir.glob(source_path('*.gif')).each {|f| m.file(File.basename(f), File.join('public/images', File.basename(f))) } Dir.glob(source_path('*.png')).each {|f| m.file(File.basename(f), File.join('public/images', File.basename(f))) } File.rename('public/index.html', 'public/index.html.org') if File.exist?('public/index.html') # environment.rb sentinel = "require File.join(File.dirname(__FILE__), 'boot')\n\nRails" m.gsub_file 'config/environment.rb', /(#{Regexp.escape(sentinel)})/mi do |match| s = <<'EOS' # warning version_requirements is deprecated if Gem::VERSION >= "1.3.6" module Rails class GemDependency def requirement r = super (r == Gem::Requirement.default) ? nil : r end end end end EOS match.gsub("\n\n", s) end sentinel = " config.time_zone = 'UTC'" m.gsub_file 'config/environment.rb', /(#{Regexp.escape(sentinel)})/mi do |match| match.gsub(" config", " # commentout default timezone \n # config") end sentinel = " # config.i18n.default_locale = :de\nend" m.gsub_file 'config/environment.rb', /(#{Regexp.escape(sentinel)})/mi do |match| s = <<"EOS" # set default locale config.i18n.default_locale = 'ja' # set migration filename config.active_record.timestamped_migrations = false # set colorize logging config.active_record.colorize_logging = false # set will_paginate config.gem 'mislav-will_paginate', :lib => 'will_paginate', :version => '~> 2.3' # set constant values REGEXP_ZENKAKU = /^[^ -~?-゚]*$/ # zenkaku REGEXP_HANKAKU = /^[ -~?-゚]*$/ # hankaku REGEXP_EMAIL = /^[\w\.\-]+@(?:[\w\-]+\.)+[\w\-]+$/ # mail REGEXP_ZIP = /^\d{3}-?\d{4}$/ # zip REGEXP_TEL = /^\d+-?\d+-?\d+$/ # tel REGEXP_PASSWORD= /^(?=.{8,})(?=.*[a-zA-Z]+)(?=.*[\d]+)[0-9A-Za-z]+$/ # password PAGINATE_PER_PAGE = 20 DISPLAY_TYPE_SIMPLE = 'simple' APP_COMPANY = '#{@app_company}' APP_TITLE = '#{@app_title}' end EOS match.gsub("\nend", s) end # development.rb sentinel = "config.cache_classes = false\n\n# Log" m.gsub_file 'config/environments/development.rb', /(#{Regexp.escape(sentinel)})/mi do |match| s = <<"EOS" # set log level config.log_level = :debug EOS match.gsub("\n\n", s) end # session_store.rb sentinel = '# ActionController::Base.session_store = :active_record_store' m.gsub_file 'config/initializers/session_store.rb', /(#{Regexp.escape(sentinel)})/mi do |match| match.gsub("# ", "") end # routes.rb sentinel = "# map.root :controller => \"welcome\"\n\n # See" m.gsub_file 'config/routes.rb', /(#{Regexp.escape(sentinel)})/mi do |match| s = <<'EOS' map.root :controller => :auth, :action => :login map.top 'top', :controller => :top, :action => :index EOS match.gsub("\n\n", s) end sentinel = "map.connect ':controller/:action/:id.:format'\nend" m.gsub_file 'config/routes.rb', /(#{Regexp.escape(sentinel)})/mi do |match| s = <<'EOS' map.connect '*path', :controller => :application, :action => :error EOS match.gsub("\n", s) end end end protected # Override with your own usage banner. def banner "Usage: #{$0} ava_rails AppName CompanyName" 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