diff options
Diffstat (limited to 'railties/lib/rails')
235 files changed, 11897 insertions, 0 deletions
diff --git a/railties/lib/rails/all.rb b/railties/lib/rails/all.rb new file mode 100644 index 0000000000..2e83c0fe14 --- /dev/null +++ b/railties/lib/rails/all.rb @@ -0,0 +1,15 @@ +require "rails" + +%w( + active_record + action_controller + action_view + action_mailer + rails/test_unit + sprockets +).each do |framework| + begin + require "#{framework}/railtie" + rescue LoadError + end +end diff --git a/railties/lib/rails/api/task.rb b/railties/lib/rails/api/task.rb new file mode 100644 index 0000000000..3e32576040 --- /dev/null +++ b/railties/lib/rails/api/task.rb @@ -0,0 +1,163 @@ +require 'rdoc/task' + +module Rails + module API + class Task < RDoc::Task + RDOC_FILES = { + 'activesupport' => { + :include => %w( + README.rdoc + lib/active_support/**/*.rb + ), + :exclude => 'lib/active_support/vendor/*' + }, + + 'activerecord' => { + :include => %w( + README.rdoc + lib/active_record/**/*.rb + ) + }, + + 'activemodel' => { + :include => %w( + README.rdoc + lib/active_model/**/*.rb + ) + }, + + 'actionpack' => { + :include => %w( + README.rdoc + lib/abstract_controller/**/*.rb + lib/action_controller/**/*.rb + lib/action_dispatch/**/*.rb + ) + }, + + 'actionview' => { + :include => %w( + README.rdoc + lib/action_view/**/*.rb + ), + :exclude => 'lib/action_view/vendor/*' + }, + + 'actionmailer' => { + :include => %w( + README.rdoc + lib/action_mailer/**/*.rb + ) + }, + + 'railties' => { + :include => %w( + README.rdoc + lib/**/*.rb + ), + :exclude => 'lib/rails/generators/rails/**/templates/**/*.rb' + } + } + + def initialize(name) + super + + # Every time rake runs this task is instantiated as all the rest. + # Be lazy computing stuff to have as light impact as possible to + # the rest of tasks. + before_running_rdoc do + load_and_configure_sdoc + configure_rdoc_files + setup_horo_variables + end + end + + # Hack, ignore the desc calls performed by the original initializer. + def desc(description) + # no-op + end + + def load_and_configure_sdoc + require 'sdoc' + + self.title = 'Ruby on Rails API' + self.rdoc_dir = api_dir + + options << '-m' << api_main + options << '-e' << 'UTF-8' + + options << '-f' << 'sdoc' + options << '-T' << 'rails' + rescue LoadError + $stderr.puts %(Unable to load SDoc, please add\n\n gem 'sdoc', require: false\n\nto the Gemfile.) + exit 1 + end + + def configure_rdoc_files + rdoc_files.include(api_main) + + RDOC_FILES.each do |component, cfg| + cdr = component_root_dir(component) + + Array(cfg[:include]).each do |pattern| + rdoc_files.include("#{cdr}/#{pattern}") + end + + Array(cfg[:exclude]).each do |pattern| + rdoc_files.exclude("#{cdr}/#{pattern}") + end + end + end + + def setup_horo_variables + ENV['HORO_PROJECT_NAME'] = 'Ruby on Rails' + ENV['HORO_PROJECT_VERSION'] = rails_version + end + + def api_main + component_root_dir('railties') + '/RDOC_MAIN.rdoc' + end + end + + class RepoTask < Task + def load_and_configure_sdoc + super + options << '-g' # link to GitHub, SDoc flag + end + + def component_root_dir(component) + component + end + + def api_dir + 'doc/rdoc' + end + end + + class EdgeTask < RepoTask + def rails_version + "master@#{`git rev-parse HEAD`[0, 7]}" + end + end + + class StableTask < RepoTask + def rails_version + File.read('RAILS_VERSION').strip + end + end + + class AppTask < Task + def component_root_dir(gem_name) + $:.grep(%r{#{gem_name}[\w.-]*/lib\z}).first[0..-5] + end + + def api_dir + 'doc/api' + end + + def rails_version + Rails::VERSION::STRING + end + end + end +end diff --git a/railties/lib/rails/app_rails_loader.rb b/railties/lib/rails/app_rails_loader.rb new file mode 100644 index 0000000000..39d8007333 --- /dev/null +++ b/railties/lib/rails/app_rails_loader.rb @@ -0,0 +1,63 @@ +require 'pathname' + +module Rails + module AppRailsLoader + extend self + + RUBY = Gem.ruby + EXECUTABLES = ['bin/rails', 'script/rails'] + BUNDLER_WARNING = <<EOS +Looks like your app's ./bin/rails is a stub that was generated by Bundler. + +In Rails 4, your app's bin/ directory contains executables that are versioned +like any other source code, rather than stubs that are generated on demand. + +Here's how to upgrade: + + bundle config --delete bin # Turn off Bundler's stub generator + rake rails:update:bin # Use the new Rails 4 executables + git add bin # Add bin/ to source control + +You may need to remove bin/ from your .gitignore as well. + +When you install a gem whose executable you want to use in your app, +generate it and add it to source control: + + bundle binstubs some-gem-name + git add bin/new-executable + +EOS + + def exec_app_rails + original_cwd = Dir.pwd + + loop do + if exe = find_executable + contents = File.read(exe) + + if contents =~ /(APP|ENGINE)_PATH/ + exec RUBY, exe, *ARGV + break # non reachable, hack to be able to stub exec in the test suite + elsif exe.end_with?('bin/rails') && contents.include?('This file was generated by Bundler') + $stderr.puts(BUNDLER_WARNING) + Object.const_set(:APP_PATH, File.expand_path('config/application', Dir.pwd)) + require File.expand_path('../boot', APP_PATH) + require 'rails/commands' + break + end + end + + # If we exhaust the search there is no executable, this could be a + # call to generate a new application, so restore the original cwd. + Dir.chdir(original_cwd) and return if Pathname.new(Dir.pwd).root? + + # Otherwise keep moving upwards in search of an executable. + Dir.chdir('..') + end + end + + def find_executable + EXECUTABLES.find { |exe| File.file?(exe) } + end + end +end diff --git a/railties/lib/rails/application.rb b/railties/lib/rails/application.rb new file mode 100644 index 0000000000..61639be7c6 --- /dev/null +++ b/railties/lib/rails/application.rb @@ -0,0 +1,513 @@ +require 'fileutils' +require 'active_support/core_ext/hash/keys' +require 'active_support/core_ext/object/blank' +require 'active_support/key_generator' +require 'active_support/message_verifier' +require 'rails/engine' + +module Rails + # In Rails 3.0, a Rails::Application object was introduced which is nothing more than + # an Engine but with the responsibility of coordinating the whole boot process. + # + # == Initialization + # + # Rails::Application is responsible for executing all railties and engines + # initializers. It also executes some bootstrap initializers (check + # Rails::Application::Bootstrap) and finishing initializers, after all the others + # are executed (check Rails::Application::Finisher). + # + # == Configuration + # + # Besides providing the same configuration as Rails::Engine and Rails::Railtie, + # the application object has several specific configurations, for example + # "cache_classes", "consider_all_requests_local", "filter_parameters", + # "logger" and so forth. + # + # Check Rails::Application::Configuration to see them all. + # + # == Routes + # + # The application object is also responsible for holding the routes and reloading routes + # whenever the files change in development. + # + # == Middlewares + # + # The Application is also responsible for building the middleware stack. + # + # == Booting process + # + # The application is also responsible for setting up and executing the booting + # process. From the moment you require "config/application.rb" in your app, + # the booting process goes like this: + # + # 1) require "config/boot.rb" to setup load paths + # 2) require railties and engines + # 3) Define Rails.application as "class MyApp::Application < Rails::Application" + # 4) Run config.before_configuration callbacks + # 5) Load config/environments/ENV.rb + # 6) Run config.before_initialize callbacks + # 7) Run Railtie#initializer defined by railties, engines and application. + # One by one, each engine sets up its load paths, routes and runs its config/initializers/* files. + # 8) Custom Railtie#initializers added by railties, engines and applications are executed + # 9) Build the middleware stack and run to_prepare callbacks + # 10) Run config.before_eager_load and eager_load! if eager_load is true + # 11) Run config.after_initialize callbacks + # + # == Multiple Applications + # + # If you decide to define multiple applications, then the first application + # that is initialized will be set to +Rails.application+, unless you override + # it with a different application. + # + # To create a new application, you can instantiate a new instance of a class + # that has already been created: + # + # class Application < Rails::Application + # end + # + # first_application = Application.new + # second_application = Application.new(config: first_application.config) + # + # In the above example, the configuration from the first application was used + # to initialize the second application. You can also use the +initialize_copy+ + # on one of the applications to create a copy of the application which shares + # the configuration. + # + # If you decide to define rake tasks, runners, or initializers in an + # application other than +Rails.application+, then you must run those + # these manually. + class Application < Engine + autoload :Bootstrap, 'rails/application/bootstrap' + autoload :Configuration, 'rails/application/configuration' + autoload :DefaultMiddlewareStack, 'rails/application/default_middleware_stack' + autoload :Finisher, 'rails/application/finisher' + autoload :Railties, 'rails/engine/railties' + autoload :RoutesReloader, 'rails/application/routes_reloader' + + class << self + def inherited(base) + super + Rails.app_class = base + end + + def instance + super.run_load_hooks! + end + + def create(initial_variable_values = {}, &block) + new(initial_variable_values, &block).run_load_hooks! + end + + # Makes the +new+ method public. + # + # Note that Rails::Application inherits from Rails::Engine, which + # inherits from Rails::Railtie and the +new+ method on Rails::Railtie is + # private + public :new + end + + attr_accessor :assets, :sandbox + alias_method :sandbox?, :sandbox + attr_reader :reloaders + + delegate :default_url_options, :default_url_options=, to: :routes + + INITIAL_VARIABLES = [:config, :railties, :routes_reloader, :reloaders, + :routes, :helpers, :app_env_config, :secrets] # :nodoc: + + def initialize(initial_variable_values = {}, &block) + super() + @initialized = false + @reloaders = [] + @routes_reloader = nil + @app_env_config = nil + @ordered_railties = nil + @railties = nil + @message_verifiers = {} + @ran_load_hooks = false + + # are these actually used? + @initial_variable_values = initial_variable_values + @block = block + + add_lib_to_load_path! + end + + # Returns true if the application is initialized. + def initialized? + @initialized + end + + def run_load_hooks! # :nodoc: + return self if @ran_load_hooks + @ran_load_hooks = true + ActiveSupport.run_load_hooks(:before_configuration, self) + + @initial_variable_values.each do |variable_name, value| + if INITIAL_VARIABLES.include?(variable_name) + instance_variable_set("@#{variable_name}", value) + end + end + + instance_eval(&@block) if @block + self + end + + # Implements call according to the Rack API. It simply + # dispatches the request to the underlying middleware stack. + def call(env) + env["ORIGINAL_FULLPATH"] = build_original_fullpath(env) + env["ORIGINAL_SCRIPT_NAME"] = env["SCRIPT_NAME"] + super(env) + end + + # Reload application routes regardless if they changed or not. + def reload_routes! + routes_reloader.reload! + end + + # Return the application's KeyGenerator + def key_generator + # number of iterations selected based on consultation with the google security + # team. Details at https://github.com/rails/rails/pull/6952#issuecomment-7661220 + @caching_key_generator ||= + if secrets.secret_key_base + key_generator = ActiveSupport::KeyGenerator.new(secrets.secret_key_base, iterations: 1000) + ActiveSupport::CachingKeyGenerator.new(key_generator) + else + ActiveSupport::LegacyKeyGenerator.new(config.secret_token) + end + end + + # Returns a message verifier object. + # + # This verifier can be used to generate and verify signed messages in the application. + # + # It is recommended not to use the same verifier for different things, so you can get different + # verifiers passing the +verifier_name+ argument. + # + # ==== Parameters + # + # * +verifier_name+ - the name of the message verifier. + # + # ==== Examples + # + # message = Rails.application.message_verifier('sensitive_data').generate('my sensible data') + # Rails.application.message_verifier('sensitive_data').verify(message) + # # => 'my sensible data' + # + # See the +ActiveSupport::MessageVerifier+ documentation for more information. + def message_verifier(verifier_name) + @message_verifiers[verifier_name] ||= begin + secret = key_generator.generate_key(verifier_name.to_s) + ActiveSupport::MessageVerifier.new(secret) + end + end + + # Convenience for loading config/foo.yml for the current Rails env. + # + # Example: + # + # # config/exception_notification.yml: + # production: + # url: http://127.0.0.1:8080 + # namespace: my_app_production + # development: + # url: http://localhost:3001 + # namespace: my_app_development + # + # # config/production.rb + # MyApp::Application.configure do + # config.middleware.use ExceptionNotifier, config_for(:exception_notification) + # end + def config_for(name) + yaml = Pathname.new("#{paths["config"].existent.first}/#{name}.yml") + + if yaml.exist? + require "yaml" + require "erb" + (YAML.load(ERB.new(yaml.read).result) || {})[Rails.env] || {} + else + raise "Could not load configuration. No such file - #{yaml}" + end + rescue Psych::SyntaxError => e + raise "YAML syntax error occurred while parsing #{yaml}. " \ + "Please note that YAML must be consistently indented using spaces. Tabs are not allowed. " \ + "Error: #{e.message}" + end + + # Stores some of the Rails initial environment parameters which + # will be used by middlewares and engines to configure themselves. + def env_config + @app_env_config ||= begin + validate_secret_key_config! + + super.merge({ + "action_dispatch.parameter_filter" => config.filter_parameters, + "action_dispatch.redirect_filter" => config.filter_redirect, + "action_dispatch.secret_token" => config.secret_token, + "action_dispatch.secret_key_base" => secrets.secret_key_base, + "action_dispatch.show_exceptions" => config.action_dispatch.show_exceptions, + "action_dispatch.show_detailed_exceptions" => config.consider_all_requests_local, + "action_dispatch.logger" => Rails.logger, + "action_dispatch.backtrace_cleaner" => Rails.backtrace_cleaner, + "action_dispatch.key_generator" => key_generator, + "action_dispatch.http_auth_salt" => config.action_dispatch.http_auth_salt, + "action_dispatch.signed_cookie_salt" => config.action_dispatch.signed_cookie_salt, + "action_dispatch.encrypted_cookie_salt" => config.action_dispatch.encrypted_cookie_salt, + "action_dispatch.encrypted_signed_cookie_salt" => config.action_dispatch.encrypted_signed_cookie_salt, + "action_dispatch.cookies_serializer" => config.action_dispatch.cookies_serializer + }) + end + end + + # If you try to define a set of rake tasks on the instance, these will get + # passed up to the rake tasks defined on the application's class. + def rake_tasks(&block) + self.class.rake_tasks(&block) + end + + # Sends the initializers to the +initializer+ method defined in the + # Rails::Initializable module. Each Rails::Application class has its own + # set of initializers, as defined by the Initializable module. + def initializer(name, opts={}, &block) + self.class.initializer(name, opts, &block) + end + + # Sends any runner called in the instance of a new application up + # to the +runner+ method defined in Rails::Railtie. + def runner(&blk) + self.class.runner(&blk) + end + + # Sends any console called in the instance of a new application up + # to the +console+ method defined in Rails::Railtie. + def console(&blk) + self.class.console(&blk) + end + + # Sends any generators called in the instance of a new application up + # to the +generators+ method defined in Rails::Railtie. + def generators(&blk) + self.class.generators(&blk) + end + + # Sends the +isolate_namespace+ method up to the class method. + def isolate_namespace(mod) + self.class.isolate_namespace(mod) + end + + ## Rails internal API + + # This method is called just after an application inherits from Rails::Application, + # allowing the developer to load classes in lib and use them during application + # configuration. + # + # class MyApplication < Rails::Application + # require "my_backend" # in lib/my_backend + # config.i18n.backend = MyBackend + # end + # + # Notice this method takes into consideration the default root path. So if you + # are changing config.root inside your application definition or having a custom + # Rails application, you will need to add lib to $LOAD_PATH on your own in case + # you need to load files in lib/ during the application configuration as well. + def add_lib_to_load_path! #:nodoc: + path = File.join config.root, 'lib' + if File.exist?(path) && !$LOAD_PATH.include?(path) + $LOAD_PATH.unshift(path) + end + end + + def require_environment! #:nodoc: + environment = paths["config/environment"].existent.first + require environment if environment + end + + def routes_reloader #:nodoc: + @routes_reloader ||= RoutesReloader.new + end + + # Returns an array of file paths appended with a hash of + # directories-extensions suitable for ActiveSupport::FileUpdateChecker + # API. + def watchable_args #:nodoc: + files, dirs = config.watchable_files.dup, config.watchable_dirs.dup + + ActiveSupport::Dependencies.autoload_paths.each do |path| + dirs[path.to_s] = [:rb] + end + + [files, dirs] + end + + # Initialize the application passing the given group. By default, the + # group is :default + def initialize!(group=:default) #:nodoc: + raise "Application has been already initialized." if @initialized + run_initializers(group, self) + @initialized = true + self + end + + def initializers #:nodoc: + Bootstrap.initializers_for(self) + + railties_initializers(super) + + Finisher.initializers_for(self) + end + + def config #:nodoc: + @config ||= Application::Configuration.new(find_root_with_flag("config.ru", Dir.pwd)) + end + + def config=(configuration) #:nodoc: + @config = configuration + end + + def secrets #:nodoc: + @secrets ||= begin + secrets = ActiveSupport::OrderedOptions.new + yaml = config.paths["config/secrets"].first + if File.exist?(yaml) + require "erb" + all_secrets = YAML.load(ERB.new(IO.read(yaml)).result) || {} + env_secrets = all_secrets[Rails.env] + secrets.merge!(env_secrets.symbolize_keys) if env_secrets + end + + # Fallback to config.secret_key_base if secrets.secret_key_base isn't set + secrets.secret_key_base ||= config.secret_key_base + + secrets + end + end + + def secrets=(secrets) #:nodoc: + @secrets = secrets + end + + def to_app #:nodoc: + self + end + + def helpers_paths #:nodoc: + config.helpers_paths + end + + console do + require "pp" + end + + console do + unless ::Kernel.private_method_defined?(:y) + if RUBY_VERSION >= '2.0' + require "psych/y" + else + module ::Kernel + def y(*objects) + puts ::Psych.dump_stream(*objects) + end + private :y + end + end + end + end + + def migration_railties # :nodoc: + (ordered_railties & railties_without_main_app).reverse + end + + protected + + alias :build_middleware_stack :app + + def run_tasks_blocks(app) #:nodoc: + railties.each { |r| r.run_tasks_blocks(app) } + super + require "rails/tasks" + task :environment do + ActiveSupport.on_load(:before_initialize) { config.eager_load = false } + + require_environment! + end + end + + def run_generators_blocks(app) #:nodoc: + railties.each { |r| r.run_generators_blocks(app) } + super + end + + def run_runner_blocks(app) #:nodoc: + railties.each { |r| r.run_runner_blocks(app) } + super + end + + def run_console_blocks(app) #:nodoc: + railties.each { |r| r.run_console_blocks(app) } + super + end + + def railties_without_main_app # :nodoc: + @railties_without_main_app ||= Rails::Railtie.subclasses.map(&:instance) + + Rails::Engine.subclasses.map(&:instance) + end + + # Returns the ordered railties for this application considering railties_order. + def ordered_railties #:nodoc: + @ordered_railties ||= begin + order = config.railties_order.map do |railtie| + if railtie == :main_app + self + elsif railtie.respond_to?(:instance) + railtie.instance + else + railtie + end + end + + all = (railties - order) + all.push(self) unless (all + order).include?(self) + order.push(:all) unless order.include?(:all) + + index = order.index(:all) + order[index] = all + order.reverse.flatten + end + end + + def railties_initializers(current) #:nodoc: + initializers = [] + ordered_railties.each do |r| + if r == self + initializers += current + else + initializers += r.initializers + end + end + initializers + end + + def default_middleware_stack #:nodoc: + default_stack = DefaultMiddlewareStack.new(self, config, paths) + default_stack.build_stack + end + + def build_original_fullpath(env) #:nodoc: + path_info = env["PATH_INFO"] + query_string = env["QUERY_STRING"] + script_name = env["SCRIPT_NAME"] + + if query_string.present? + "#{script_name}#{path_info}?#{query_string}" + else + "#{script_name}#{path_info}" + end + end + + def validate_secret_key_config! #:nodoc: + if secrets.secret_key_base.blank? && config.secret_token.blank? + raise "Missing `secret_key_base` for '#{Rails.env}' environment, set this value in `config/secrets.yml`" + end + end + end +end diff --git a/railties/lib/rails/application/bootstrap.rb b/railties/lib/rails/application/bootstrap.rb new file mode 100644 index 0000000000..a26d41c0cf --- /dev/null +++ b/railties/lib/rails/application/bootstrap.rb @@ -0,0 +1,80 @@ +require "active_support/notifications" +require "active_support/dependencies" +require "active_support/descendants_tracker" + +module Rails + class Application + module Bootstrap + include Initializable + + initializer :load_environment_hook, group: :all do end + + initializer :load_active_support, group: :all do + require "active_support/all" unless config.active_support.bare + end + + initializer :set_eager_load, group: :all do + if config.eager_load.nil? + warn <<-INFO +config.eager_load is set to nil. Please update your config/environments/*.rb files accordingly: + + * development - set it to false + * test - set it to false (unless you use a tool that preloads your test environment) + * production - set it to true + +INFO + config.eager_load = config.cache_classes + end + end + + # Initialize the logger early in the stack in case we need to log some deprecation. + initializer :initialize_logger, group: :all do + Rails.logger ||= config.logger || begin + path = config.paths["log"].first + unless File.exist? File.dirname path + FileUtils.mkdir_p File.dirname path + end + + f = File.open path, 'a' + f.binmode + f.sync = config.autoflush_log # if true make sure every write flushes + + logger = ActiveSupport::Logger.new f + logger.formatter = config.log_formatter + logger = ActiveSupport::TaggedLogging.new(logger) + logger + rescue StandardError + logger = ActiveSupport::TaggedLogging.new(ActiveSupport::Logger.new(STDERR)) + logger.level = ActiveSupport::Logger::WARN + logger.warn( + "Rails Error: Unable to access log file. Please ensure that #{path} exists and is chmod 0666. " + + "The log level has been raised to WARN and the output directed to STDERR until the problem is fixed." + ) + logger + end + + Rails.logger.level = ActiveSupport::Logger.const_get(config.log_level.to_s.upcase) + end + + # Initialize cache early in the stack so railties can make use of it. + initializer :initialize_cache, group: :all do + unless Rails.cache + Rails.cache = ActiveSupport::Cache.lookup_store(config.cache_store) + + if Rails.cache.respond_to?(:middleware) + config.middleware.insert_before("Rack::Runtime", Rails.cache.middleware) + end + end + end + + # Sets the dependency loading mechanism. + initializer :initialize_dependency_mechanism, group: :all do + ActiveSupport::Dependencies.mechanism = config.cache_classes ? :require : :load + end + + initializer :bootstrap_hook, group: :all do |app| + ActiveSupport.run_load_hooks(:before_initialize, app) + end + end + end +end diff --git a/railties/lib/rails/application/configuration.rb b/railties/lib/rails/application/configuration.rb new file mode 100644 index 0000000000..782bc4b0f1 --- /dev/null +++ b/railties/lib/rails/application/configuration.rb @@ -0,0 +1,171 @@ +require 'active_support/core_ext/kernel/reporting' +require 'active_support/file_update_checker' +require 'rails/engine/configuration' +require 'rails/source_annotation_extractor' + +module Rails + class Application + class Configuration < ::Rails::Engine::Configuration + attr_accessor :allow_concurrency, :asset_host, :assets, :autoflush_log, + :cache_classes, :cache_store, :consider_all_requests_local, :console, + :eager_load, :exceptions_app, :file_watcher, :filter_parameters, + :force_ssl, :helpers_paths, :logger, :log_formatter, :log_tags, + :railties_order, :relative_url_root, :secret_key_base, :secret_token, + :serve_static_assets, :ssl_options, :static_cache_control, :session_options, + :time_zone, :reload_classes_only_on_change, + :beginning_of_week, :filter_redirect, :x + + attr_writer :log_level + attr_reader :encoding + + def initialize(*) + super + self.encoding = "utf-8" + @allow_concurrency = nil + @consider_all_requests_local = false + @filter_parameters = [] + @filter_redirect = [] + @helpers_paths = [] + @serve_static_assets = true + @static_cache_control = nil + @force_ssl = false + @ssl_options = {} + @session_store = :cookie_store + @session_options = {} + @time_zone = "UTC" + @beginning_of_week = :monday + @log_level = nil + @middleware = app_middleware + @generators = app_generators + @cache_store = [ :file_store, "#{root}/tmp/cache/" ] + @railties_order = [:all] + @relative_url_root = ENV["RAILS_RELATIVE_URL_ROOT"] + @reload_classes_only_on_change = true + @file_watcher = ActiveSupport::FileUpdateChecker + @exceptions_app = nil + @autoflush_log = true + @log_formatter = ActiveSupport::Logger::SimpleFormatter.new + @eager_load = nil + @secret_token = nil + @secret_key_base = nil + @x = Custom.new + + @assets = ActiveSupport::OrderedOptions.new + @assets.enabled = true + @assets.paths = [] + @assets.precompile = [ Proc.new { |path, fn| fn =~ /app\/assets/ && !%w(.js .css).include?(File.extname(path)) }, + /(?:\/|\\|\A)application\.(css|js)$/ ] + @assets.prefix = "/assets" + @assets.version = '1.0' + @assets.debug = false + @assets.compile = true + @assets.digest = false + @assets.cache_store = [ :file_store, "#{root}/tmp/cache/assets/#{Rails.env}/" ] + @assets.js_compressor = nil + @assets.css_compressor = nil + @assets.logger = nil + end + + def encoding=(value) + @encoding = value + silence_warnings do + Encoding.default_external = value + Encoding.default_internal = value + end + end + + def paths + @paths ||= begin + paths = super + paths.add "config/database", with: "config/database.yml" + paths.add "config/secrets", with: "config/secrets.yml" + paths.add "config/environment", with: "config/environment.rb" + paths.add "lib/templates" + paths.add "log", with: "log/#{Rails.env}.log" + paths.add "public" + paths.add "public/javascripts" + paths.add "public/stylesheets" + paths.add "tmp" + paths + end + end + + # Loads and returns the entire raw configuration of database from + # values stored in `config/database.yml`. + def database_configuration + yaml = Pathname.new(paths["config/database"].existent.first || "") + + config = if yaml.exist? + require "yaml" + require "erb" + YAML.load(ERB.new(yaml.read).result) || {} + elsif ENV['DATABASE_URL'] + # Value from ENV['DATABASE_URL'] is set to default database connection + # by Active Record. + {} + else + raise "Could not load database configuration. No such file - #{yaml}" + end + + config + rescue Psych::SyntaxError => e + raise "YAML syntax error occurred while parsing #{paths["config/database"].first}. " \ + "Please note that YAML must be consistently indented using spaces. Tabs are not allowed. " \ + "Error: #{e.message}" + rescue => e + raise e, "Cannot load `Rails.application.database_configuration`:\n#{e.message}", e.backtrace + end + + def log_level + @log_level ||= Rails.env.production? ? :info : :debug + end + + def colorize_logging + ActiveSupport::LogSubscriber.colorize_logging + end + + def colorize_logging=(val) + ActiveSupport::LogSubscriber.colorize_logging = val + self.generators.colorize_logging = val + end + + def session_store(*args) + if args.empty? + case @session_store + when :disabled + nil + when :active_record_store + begin + ActionDispatch::Session::ActiveRecordStore + rescue NameError + raise "`ActiveRecord::SessionStore` is extracted out of Rails into a gem. " \ + "Please add `activerecord-session_store` to your Gemfile to use it." + end + when Symbol + ActionDispatch::Session.const_get(@session_store.to_s.camelize) + else + @session_store + end + else + @session_store = args.shift + @session_options = args.shift || {} + end + end + + def annotations + SourceAnnotationExtractor::Annotation + end + + private + class Custom + def initialize + @configurations = Hash.new + end + + def method_missing(method, *args) + @configurations[method] ||= ActiveSupport::OrderedOptions.new + end + end + end + end +end diff --git a/railties/lib/rails/application/default_middleware_stack.rb b/railties/lib/rails/application/default_middleware_stack.rb new file mode 100644 index 0000000000..a00afe008c --- /dev/null +++ b/railties/lib/rails/application/default_middleware_stack.rb @@ -0,0 +1,99 @@ +module Rails + class Application + class DefaultMiddlewareStack + attr_reader :config, :paths, :app + + def initialize(app, config, paths) + @app = app + @config = config + @paths = paths + end + + def build_stack + ActionDispatch::MiddlewareStack.new.tap do |middleware| + if config.force_ssl + middleware.use ::ActionDispatch::SSL, config.ssl_options + end + + middleware.use ::Rack::Sendfile, config.action_dispatch.x_sendfile_header + + if config.serve_static_assets + middleware.use ::ActionDispatch::Static, paths["public"].first, config.static_cache_control + end + + if rack_cache = load_rack_cache + require "action_dispatch/http/rack_cache" + middleware.use ::Rack::Cache, rack_cache + end + + middleware.use ::Rack::Lock unless allow_concurrency? + middleware.use ::Rack::Runtime + middleware.use ::Rack::MethodOverride + middleware.use ::ActionDispatch::RequestId + + # Must come after Rack::MethodOverride to properly log overridden methods + middleware.use ::Rails::Rack::Logger, config.log_tags + middleware.use ::ActionDispatch::ShowExceptions, show_exceptions_app + middleware.use ::ActionDispatch::DebugExceptions, app + middleware.use ::ActionDispatch::RemoteIp, config.action_dispatch.ip_spoofing_check, config.action_dispatch.trusted_proxies + + unless config.cache_classes + middleware.use ::ActionDispatch::Reloader, lambda { reload_dependencies? } + end + + middleware.use ::ActionDispatch::Callbacks + middleware.use ::ActionDispatch::Cookies + + if config.session_store + if config.force_ssl && !config.session_options.key?(:secure) + config.session_options[:secure] = true + end + middleware.use config.session_store, config.session_options + middleware.use ::ActionDispatch::Flash + end + + middleware.use ::ActionDispatch::ParamsParser + middleware.use ::Rack::Head + middleware.use ::Rack::ConditionalGet + middleware.use ::Rack::ETag, "no-cache" + end + end + + private + + def reload_dependencies? + config.reload_classes_only_on_change != true || app.reloaders.map(&:updated?).any? + end + + def allow_concurrency? + config.allow_concurrency.nil? ? config.cache_classes : config.allow_concurrency + end + + def load_rack_cache + rack_cache = config.action_dispatch.rack_cache + return unless rack_cache + + begin + require 'rack/cache' + rescue LoadError => error + error.message << ' Be sure to add rack-cache to your Gemfile' + raise + end + + if rack_cache == true + { + metastore: "rails:/", + entitystore: "rails:/", + verbose: false + } + else + rack_cache + end + end + + def show_exceptions_app + config.exceptions_app || ActionDispatch::PublicExceptions.new(Rails.public_path) + end + end + end +end diff --git a/railties/lib/rails/application/finisher.rb b/railties/lib/rails/application/finisher.rb new file mode 100644 index 0000000000..7a1bb1e25c --- /dev/null +++ b/railties/lib/rails/application/finisher.rb @@ -0,0 +1,113 @@ +module Rails + class Application + module Finisher + include Initializable + + initializer :add_generator_templates do + config.generators.templates.unshift(*paths["lib/templates"].existent) + end + + initializer :ensure_autoload_once_paths_as_subset do + extra = ActiveSupport::Dependencies.autoload_once_paths - + ActiveSupport::Dependencies.autoload_paths + + unless extra.empty? + abort <<-end_error + autoload_once_paths must be a subset of the autoload_paths. + Extra items in autoload_once_paths: #{extra * ','} + end_error + end + end + + initializer :add_builtin_route do |app| + if Rails.env.development? + app.routes.append do + get '/rails/info/properties' => "rails/info#properties" + get '/rails/info/routes' => "rails/info#routes" + get '/rails/info' => "rails/info#index" + get '/' => "rails/welcome#index" + end + end + end + + initializer :build_middleware_stack do + build_middleware_stack + end + + initializer :define_main_app_helper do |app| + app.routes.define_mounted_helper(:main_app) + end + + initializer :add_to_prepare_blocks do + config.to_prepare_blocks.each do |block| + ActionDispatch::Reloader.to_prepare(&block) + end + end + + # This needs to happen before eager load so it happens + # in exactly the same point regardless of config.cache_classes + initializer :run_prepare_callbacks do + ActionDispatch::Reloader.prepare! + end + + initializer :eager_load! do + if config.eager_load + ActiveSupport.run_load_hooks(:before_eager_load, self) + config.eager_load_namespaces.each(&:eager_load!) + end + end + + # All initialization is done, including eager loading in production + initializer :finisher_hook do + ActiveSupport.run_load_hooks(:after_initialize, self) + end + + # Set routes reload after the finisher hook to ensure routes added in + # the hook are taken into account. + initializer :set_routes_reloader_hook do + reloader = routes_reloader + reloader.execute_if_updated + self.reloaders << reloader + ActionDispatch::Reloader.to_prepare do + # We configure #execute rather than #execute_if_updated because if + # autoloaded constants are cleared we need to reload routes also in + # case any was used there, as in + # + # mount MailPreview => 'mail_view' + # + # This means routes are also reloaded if i18n is updated, which + # might not be necessary, but in order to be more precise we need + # some sort of reloaders dependency support, to be added. + reloader.execute + end + end + + # Set clearing dependencies after the finisher hook to ensure paths + # added in the hook are taken into account. + initializer :set_clear_dependencies_hook, group: :all do + callback = lambda do + ActiveSupport::DescendantsTracker.clear + ActiveSupport::Dependencies.clear + end + + if config.reload_classes_only_on_change + reloader = config.file_watcher.new(*watchable_args, &callback) + self.reloaders << reloader + + # Prepend this callback to have autoloaded constants cleared before + # any other possible reloading, in case they need to autoload fresh + # constants. + ActionDispatch::Reloader.to_prepare(prepend: true) do + # In addition to changes detected by the file watcher, if routes + # or i18n have been updated we also need to clear constants, + # that's why we run #execute rather than #execute_if_updated, this + # callback has to clear autoloaded constants after any update. + reloader.execute + end + else + ActionDispatch::Reloader.to_cleanup(&callback) + end + end + end + end +end diff --git a/railties/lib/rails/application/routes_reloader.rb b/railties/lib/rails/application/routes_reloader.rb new file mode 100644 index 0000000000..737977adf9 --- /dev/null +++ b/railties/lib/rails/application/routes_reloader.rb @@ -0,0 +1,56 @@ +require "active_support/core_ext/module/delegation" + +module Rails + class Application + class RoutesReloader + attr_reader :route_sets, :paths + delegate :execute_if_updated, :execute, :updated?, to: :updater + + def initialize + @paths = [] + @route_sets = [] + end + + def reload! + clear! + load_paths + finalize! + ensure + revert + end + + private + + def updater + @updater ||= begin + updater = ActiveSupport::FileUpdateChecker.new(paths) { reload! } + updater.execute + updater + end + end + + def clear! + route_sets.each do |routes| + routes.disable_clear_and_finalize = true + routes.clear! + end + end + + def load_paths + paths.each { |path| load(path) } + end + + def finalize! + route_sets.each do |routes| + routes.finalize! + end + end + + def revert + route_sets.each do |routes| + routes.disable_clear_and_finalize = false + end + end + end + end +end diff --git a/railties/lib/rails/application_controller.rb b/railties/lib/rails/application_controller.rb new file mode 100644 index 0000000000..9a29ec21cf --- /dev/null +++ b/railties/lib/rails/application_controller.rb @@ -0,0 +1,16 @@ +class Rails::ApplicationController < ActionController::Base # :nodoc: + self.view_paths = File.expand_path('../templates', __FILE__) + layout 'application' + + protected + + def require_local! + unless local_request? + render text: '<p>For security purposes, this information is only available to local requests.</p>', status: :forbidden + end + end + + def local_request? + Rails.application.config.consider_all_requests_local || request.local? + end +end diff --git a/railties/lib/rails/backtrace_cleaner.rb b/railties/lib/rails/backtrace_cleaner.rb new file mode 100644 index 0000000000..8cc8eb1103 --- /dev/null +++ b/railties/lib/rails/backtrace_cleaner.rb @@ -0,0 +1,27 @@ +require 'active_support/backtrace_cleaner' + +module Rails + class BacktraceCleaner < ActiveSupport::BacktraceCleaner + APP_DIRS_PATTERN = /^\/?(app|config|lib|test)/ + RENDER_TEMPLATE_PATTERN = /:in `_render_template_\w*'/ + + def initialize + super + add_filter { |line| line.sub("#{Rails.root}/", '') } + add_filter { |line| line.sub(RENDER_TEMPLATE_PATTERN, '') } + add_filter { |line| line.sub('./', '/') } # for tests + + add_gem_filters + add_silencer { |line| line !~ APP_DIRS_PATTERN } + end + + private + def add_gem_filters + gems_paths = (Gem.path | [Gem.default_dir]).map { |p| Regexp.escape(p) } + return if gems_paths.empty? + + gems_regexp = %r{(#{gems_paths.join('|')})/gems/([^/]+)-([\w.]+)/(.*)} + add_filter { |line| line.sub(gems_regexp, '\2 (\3) \4') } + end + end +end diff --git a/railties/lib/rails/cli.rb b/railties/lib/rails/cli.rb new file mode 100644 index 0000000000..dd70c272c6 --- /dev/null +++ b/railties/lib/rails/cli.rb @@ -0,0 +1,15 @@ +require 'rails/app_rails_loader' + +# If we are inside a Rails application this method performs an exec and thus +# the rest of this script is not run. +Rails::AppRailsLoader.exec_app_rails + +require 'rails/ruby_version_check' +Signal.trap("INT") { puts; exit(1) } + +if ARGV.first == 'plugin' + ARGV.shift + require 'rails/commands/plugin' +else + require 'rails/commands/application' +end diff --git a/railties/lib/rails/code_statistics.rb b/railties/lib/rails/code_statistics.rb new file mode 100644 index 0000000000..0ae6d2a455 --- /dev/null +++ b/railties/lib/rails/code_statistics.rb @@ -0,0 +1,103 @@ +require 'rails/code_statistics_calculator' + +class CodeStatistics #:nodoc: + + TEST_TYPES = ['Controller tests', + 'Helper tests', + 'Model tests', + 'Mailer tests', + 'Integration tests', + 'Functional tests (old)', + 'Unit tests (old)'] + + def initialize(*pairs) + @pairs = pairs + @statistics = calculate_statistics + @total = calculate_total if pairs.length > 1 + end + + def to_s + print_header + @pairs.each { |pair| print_line(pair.first, @statistics[pair.first]) } + print_splitter + + if @total + print_line("Total", @total) + print_splitter + end + + print_code_test_stats + end + + private + def calculate_statistics + Hash[@pairs.map{|pair| [pair.first, calculate_directory_statistics(pair.last)]}] + end + + def calculate_directory_statistics(directory, pattern = /.*\.(rb|js|coffee)$/) + stats = CodeStatisticsCalculator.new + + Dir.foreach(directory) do |file_name| + path = "#{directory}/#{file_name}" + + if File.directory?(path) && (/^\./ !~ file_name) + stats.add(calculate_directory_statistics(path, pattern)) + end + + next unless file_name =~ pattern + + stats.add_by_file_path(path) + end + + stats + end + + def calculate_total + @statistics.each_with_object(CodeStatisticsCalculator.new) do |pair, total| + total.add(pair.last) + end + end + + def calculate_code + code_loc = 0 + @statistics.each { |k, v| code_loc += v.code_lines unless TEST_TYPES.include? k } + code_loc + end + + def calculate_tests + test_loc = 0 + @statistics.each { |k, v| test_loc += v.code_lines if TEST_TYPES.include? k } + test_loc + end + + def print_header + print_splitter + puts "| Name | Lines | LOC | Classes | Methods | M/C | LOC/M |" + print_splitter + end + + def print_splitter + puts "+----------------------+-------+-------+---------+---------+-----+-------+" + end + + def print_line(name, statistics) + m_over_c = (statistics.methods / statistics.classes) rescue m_over_c = 0 + loc_over_m = (statistics.code_lines / statistics.methods) - 2 rescue loc_over_m = 0 + + puts "| #{name.ljust(20)} " \ + "| #{statistics.lines.to_s.rjust(5)} " \ + "| #{statistics.code_lines.to_s.rjust(5)} " \ + "| #{statistics.classes.to_s.rjust(7)} " \ + "| #{statistics.methods.to_s.rjust(7)} " \ + "| #{m_over_c.to_s.rjust(3)} " \ + "| #{loc_over_m.to_s.rjust(5)} |" + end + + def print_code_test_stats + code = calculate_code + tests = calculate_tests + + puts " Code LOC: #{code} Test LOC: #{tests} Code to Test Ratio: 1:#{sprintf("%.1f", tests.to_f/code)}" + puts "" + end +end diff --git a/railties/lib/rails/code_statistics_calculator.rb b/railties/lib/rails/code_statistics_calculator.rb new file mode 100644 index 0000000000..60e4aef9b7 --- /dev/null +++ b/railties/lib/rails/code_statistics_calculator.rb @@ -0,0 +1,79 @@ +class CodeStatisticsCalculator #:nodoc: + attr_reader :lines, :code_lines, :classes, :methods + + PATTERNS = { + rb: { + line_comment: /^\s*#/, + begin_block_comment: /^=begin/, + end_block_comment: /^=end/, + class: /^\s*class\s+[_A-Z]/, + method: /^\s*def\s+[_a-z]/, + }, + js: { + line_comment: %r{^\s*//}, + begin_block_comment: %r{^\s*/\*}, + end_block_comment: %r{\*/}, + method: /function(\s+[_a-zA-Z][\da-zA-Z]*)?\s*\(/, + }, + coffee: { + line_comment: /^\s*#/, + begin_block_comment: /^\s*###/, + end_block_comment: /^\s*###/, + class: /^\s*class\s+[_A-Z]/, + method: /[-=]>/, + } + } + + def initialize(lines = 0, code_lines = 0, classes = 0, methods = 0) + @lines = lines + @code_lines = code_lines + @classes = classes + @methods = methods + end + + def add(code_statistics_calculator) + @lines += code_statistics_calculator.lines + @code_lines += code_statistics_calculator.code_lines + @classes += code_statistics_calculator.classes + @methods += code_statistics_calculator.methods + end + + def add_by_file_path(file_path) + File.open(file_path) do |f| + self.add_by_io(f, file_type(file_path)) + end + end + + def add_by_io(io, file_type) + patterns = PATTERNS[file_type] || {} + + comment_started = false + + while line = io.gets + @lines += 1 + + if comment_started + if patterns[:end_block_comment] && line =~ patterns[:end_block_comment] + comment_started = false + end + next + else + if patterns[:begin_block_comment] && line =~ patterns[:begin_block_comment] + comment_started = true + next + end + end + + @classes += 1 if patterns[:class] && line =~ patterns[:class] + @methods += 1 if patterns[:method] && line =~ patterns[:method] + if line !~ /^\s*$/ && (patterns[:line_comment].nil? || line !~ patterns[:line_comment]) + @code_lines += 1 + end + end + end + + private + def file_type(file_path) + File.extname(file_path).sub(/\A\./, '').downcase.to_sym + end +end diff --git a/railties/lib/rails/commands.rb b/railties/lib/rails/commands.rb new file mode 100644 index 0000000000..f32bf772a5 --- /dev/null +++ b/railties/lib/rails/commands.rb @@ -0,0 +1,17 @@ +ARGV << '--help' if ARGV.empty? + +aliases = { + "g" => "generate", + "d" => "destroy", + "c" => "console", + "s" => "server", + "db" => "dbconsole", + "r" => "runner" +} + +command = ARGV.shift +command = aliases[command] || command + +require 'rails/commands/commands_tasks' + +Rails::CommandsTasks.new(ARGV).run_command!(command) diff --git a/railties/lib/rails/commands/application.rb b/railties/lib/rails/commands/application.rb new file mode 100644 index 0000000000..c998e6b6a8 --- /dev/null +++ b/railties/lib/rails/commands/application.rb @@ -0,0 +1,17 @@ +require 'rails/generators' +require 'rails/generators/rails/app/app_generator' + +module Rails + module Generators + class AppGenerator # :nodoc: + # We want to exit on failure to be kind to other libraries + # This is only when accessing via CLI + def self.exit_on_failure? + true + end + end + end +end + +args = Rails::Generators::ARGVScrubber.new(ARGV).prepare! +Rails::Generators::AppGenerator.start args diff --git a/railties/lib/rails/commands/commands_tasks.rb b/railties/lib/rails/commands/commands_tasks.rb new file mode 100644 index 0000000000..6cfbc70c51 --- /dev/null +++ b/railties/lib/rails/commands/commands_tasks.rb @@ -0,0 +1,169 @@ +module Rails + # This is a class which takes in a rails command and initiates the appropriate + # initiation sequence. + # + # Warning: This class mutates ARGV because some commands require manipulating + # it before they are run. + class CommandsTasks # :nodoc: + attr_reader :argv + + HELP_MESSAGE = <<-EOT +Usage: rails COMMAND [ARGS] + +The most common rails commands are: + generate Generate new code (short-cut alias: "g") + console Start the Rails console (short-cut alias: "c") + server Start the Rails server (short-cut alias: "s") + dbconsole Start a console for the database specified in config/database.yml + (short-cut alias: "db") + new Create a new Rails application. "rails new my_app" creates a + new application called MyApp in "./my_app" + +In addition to those, there are: + destroy Undo code generated with "generate" (short-cut alias: "d") + plugin new Generates skeleton for developing a Rails plugin + runner Run a piece of code in the application environment (short-cut alias: "r") + +All commands can be run with -h (or --help) for more information. +EOT + + COMMAND_WHITELIST = %w(plugin generate destroy console server dbconsole runner new version help) + + def initialize(argv) + @argv = argv + end + + def run_command!(command) + command = parse_command(command) + if COMMAND_WHITELIST.include?(command) + send(command) + else + write_error_message(command) + end + end + + def plugin + require_command!("plugin") + end + + def generate + generate_or_destroy(:generate) + end + + def destroy + generate_or_destroy(:destroy) + end + + def console + require_command!("console") + options = Rails::Console.parse_arguments(argv) + + # RAILS_ENV needs to be set before config/application is required + ENV['RAILS_ENV'] = options[:environment] if options[:environment] + + # shift ARGV so IRB doesn't freak + shift_argv! + + require_application_and_environment! + Rails::Console.start(Rails.application, options) + end + + def server + set_application_directory! + require_command!("server") + + Rails::Server.new.tap do |server| + # We need to require application after the server sets environment, + # otherwise the --environment option given to the server won't propagate. + require APP_PATH + Dir.chdir(Rails.application.root) + server.start + end + end + + def dbconsole + require_command!("dbconsole") + Rails::DBConsole.start + end + + def runner + require_command!("runner") + end + + def new + if %w(-h --help).include?(argv.first) + require_command!("application") + else + exit_with_initialization_warning! + end + end + + def version + argv.unshift '--version' + require_command!("application") + end + + def help + write_help_message + end + + private + + def exit_with_initialization_warning! + puts "Can't initialize a new Rails application within the directory of another, please change to a non-Rails directory first.\n" + puts "Type 'rails' for help." + exit(1) + end + + def shift_argv! + argv.shift if argv.first && argv.first[0] != '-' + end + + def require_command!(command) + require "rails/commands/#{command}" + end + + def generate_or_destroy(command) + require 'rails/generators' + require_application_and_environment! + Rails.application.load_generators + require "rails/commands/#{command}" + end + + # Change to the application's path if there is no config.ru file in current directory. + # This allows us to run `rails server` from other directories, but still get + # the main config.ru and properly set the tmp directory. + def set_application_directory! + Dir.chdir(File.expand_path('../../', APP_PATH)) unless File.exist?(File.expand_path("config.ru")) + end + + def require_application_and_environment! + require APP_PATH + Rails.application.require_environment! + end + + def write_help_message + puts HELP_MESSAGE + end + + def write_error_message(command) + puts "Error: Command '#{command}' not recognized" + if %x{rake #{command} --dry-run 2>&1 } && $?.success? + puts "Did you mean: `$ rake #{command}` ?\n\n" + end + write_help_message + exit(1) + end + + def parse_command(command) + case command + when '--version', '-v' + 'version' + when '--help', '-h' + 'help' + else + command + end + end + end +end diff --git a/railties/lib/rails/commands/console.rb b/railties/lib/rails/commands/console.rb new file mode 100644 index 0000000000..555d8f31e1 --- /dev/null +++ b/railties/lib/rails/commands/console.rb @@ -0,0 +1,113 @@ +require 'optparse' +require 'irb' +require 'irb/completion' + +module Rails + class Console + class << self + def start(*args) + new(*args).start + end + + def parse_arguments(arguments) + options = {} + + OptionParser.new do |opt| + opt.banner = "Usage: rails console [environment] [options]" + opt.on('-s', '--sandbox', 'Rollback database modifications on exit.') { |v| options[:sandbox] = v } + opt.on("-e", "--environment=name", String, + "Specifies the environment to run this console under (test/development/production).", + "Default: development") { |v| options[:environment] = v.strip } + opt.on("--debugger", 'Enable the debugger.') do |v| + if RUBY_VERSION < '2.0.0' + options[:debugger] = v + else + puts "=> Notice: debugger option is ignored since ruby 2.0 and " \ + "it will be removed in future versions" + end + end + opt.parse!(arguments) + end + + if arguments.first && arguments.first[0] != '-' + env = arguments.first + if available_environments.include? env + options[:environment] = env + else + options[:environment] = %w(production development test).detect {|e| e =~ /^#{env}/} || env + end + end + + options + end + + private + + def available_environments + Dir['config/environments/*.rb'].map { |fname| File.basename(fname, '.*') } + end + end + + attr_reader :options, :app, :console + + def initialize(app, options={}) + @app = app + @options = options + + app.sandbox = sandbox? + app.load_console + + @console = app.config.console || IRB + end + + def sandbox? + options[:sandbox] + end + + def environment + options[:environment] ||= ENV['RAILS_ENV'] || ENV['RACK_ENV'] || 'development' + end + + def environment? + environment + end + + def set_environment! + Rails.env = environment + end + + if RUBY_VERSION < '2.0.0' + def debugger? + options[:debugger] + end + + def require_debugger + require 'debugger' + puts "=> Debugger enabled" + rescue LoadError + puts "You're missing the 'debugger' gem. Add it to your Gemfile, bundle it and try again." + exit(1) + end + end + + def start + if RUBY_VERSION < '2.0.0' + require_debugger if debugger? + end + + set_environment! if environment? + + if sandbox? + puts "Loading #{Rails.env} environment in sandbox (Rails #{Rails.version})" + puts "Any modifications you make will be rolled back on exit" + else + puts "Loading #{Rails.env} environment (Rails #{Rails.version})" + end + + if defined?(console::ExtendCommandBundle) + console::ExtendCommandBundle.send :include, Rails::ConsoleMethods + end + console.start + end + end +end diff --git a/railties/lib/rails/commands/dbconsole.rb b/railties/lib/rails/commands/dbconsole.rb new file mode 100644 index 0000000000..1a2613a8d0 --- /dev/null +++ b/railties/lib/rails/commands/dbconsole.rb @@ -0,0 +1,177 @@ +require 'erb' +require 'yaml' +require 'optparse' +require 'rbconfig' + +module Rails + class DBConsole + attr_reader :arguments + + def self.start + new.start + end + + def initialize(arguments = ARGV) + @arguments = arguments + end + + def start + options = parse_arguments(arguments) + ENV['RAILS_ENV'] = options[:environment] || environment + + case config["adapter"] + when /^(jdbc)?mysql/ + args = { + 'host' => '--host', + 'port' => '--port', + 'socket' => '--socket', + 'username' => '--user', + 'encoding' => '--default-character-set', + 'sslca' => '--ssl-ca', + 'sslcert' => '--ssl-cert', + 'sslcapath' => '--ssl-capath', + 'sslcipher' => '--ssh-cipher', + 'sslkey' => '--ssl-key' + }.map { |opt, arg| "#{arg}=#{config[opt]}" if config[opt] }.compact + + if config['password'] && options['include_password'] + args << "--password=#{config['password']}" + elsif config['password'] && !config['password'].to_s.empty? + args << "-p" + end + + args << config['database'] + + find_cmd_and_exec(['mysql', 'mysql5'], *args) + + when "postgresql", "postgres", "postgis" + ENV['PGUSER'] = config["username"] if config["username"] + ENV['PGHOST'] = config["host"] if config["host"] + ENV['PGPORT'] = config["port"].to_s if config["port"] + ENV['PGPASSWORD'] = config["password"].to_s if config["password"] && options['include_password'] + find_cmd_and_exec('psql', config["database"]) + + when "sqlite" + find_cmd_and_exec('sqlite', config["database"]) + + when "sqlite3" + args = [] + + args << "-#{options['mode']}" if options['mode'] + args << "-header" if options['header'] + args << File.expand_path(config['database'], Rails.respond_to?(:root) ? Rails.root : nil) + + find_cmd_and_exec('sqlite3', *args) + + when "oracle", "oracle_enhanced" + logon = "" + + if config['username'] + logon = config['username'] + logon << "/#{config['password']}" if config['password'] && options['include_password'] + logon << "@#{config['database']}" if config['database'] + end + + find_cmd_and_exec('sqlplus', logon) + + else + abort "Unknown command-line client for #{config['database']}. Submit a Rails patch to add support!" + end + end + + def config + @config ||= begin + if configurations[environment].blank? + raise ActiveRecord::AdapterNotSpecified, "'#{environment}' database is not configured. Available configuration: #{configurations.inspect}" + else + configurations[environment] + end + end + end + + def environment + if Rails.respond_to?(:env) + Rails.env + else + ENV["RAILS_ENV"] || ENV["RACK_ENV"] || "development" + end + end + + protected + + def configurations + require APP_PATH + ActiveRecord::Base.configurations = Rails.application.config.database_configuration + ActiveRecord::Base.configurations + end + + def parse_arguments(arguments) + options = {} + + OptionParser.new do |opt| + opt.banner = "Usage: rails dbconsole [environment] [options]" + opt.on("-p", "--include-password", "Automatically provide the password from database.yml") do |v| + options['include_password'] = true + end + + opt.on("--mode [MODE]", ['html', 'list', 'line', 'column'], + "Automatically put the sqlite3 database in the specified mode (html, list, line, column).") do |mode| + options['mode'] = mode + end + + opt.on("--header") do |h| + options['header'] = h + end + + opt.on("-h", "--help", "Show this help message.") do + puts opt + exit + end + + opt.on("-e", "--environment=name", String, + "Specifies the environment to run this console under (test/development/production).", + "Default: development" + ) { |v| options[:environment] = v.strip } + + opt.parse!(arguments) + abort opt.to_s unless (0..1).include?(arguments.size) + end + + if arguments.first && arguments.first[0] != '-' + env = arguments.first + if available_environments.include? env + options[:environment] = env + else + options[:environment] = %w(production development test).detect {|e| e =~ /^#{env}/} || env + end + end + + options + end + + def available_environments + Dir['config/environments/*.rb'].map { |fname| File.basename(fname, '.*') } + end + + def find_cmd_and_exec(commands, *args) + commands = Array(commands) + + dirs_on_path = ENV['PATH'].to_s.split(File::PATH_SEPARATOR) + commands += commands.map{|cmd| "#{cmd}.exe"} if RbConfig::CONFIG['host_os'] =~ /mswin|mingw/ + + full_path_command = nil + found = commands.detect do |cmd| + dirs_on_path.detect do |path| + full_path_command = File.join(path, cmd) + File.executable? full_path_command + end + end + + if found + exec full_path_command, *args + else + abort("Couldn't find database client: #{commands.join(', ')}. Check your $PATH and try again.") + end + end + end +end diff --git a/railties/lib/rails/commands/destroy.rb b/railties/lib/rails/commands/destroy.rb new file mode 100644 index 0000000000..5479da86a0 --- /dev/null +++ b/railties/lib/rails/commands/destroy.rb @@ -0,0 +1,9 @@ +require 'rails/generators' + +if [nil, "-h", "--help"].include?(ARGV.first) + Rails::Generators.help 'destroy' + exit +end + +name = ARGV.shift +Rails::Generators.invoke name, ARGV, behavior: :revoke, destination_root: Rails.root diff --git a/railties/lib/rails/commands/generate.rb b/railties/lib/rails/commands/generate.rb new file mode 100644 index 0000000000..351c59c645 --- /dev/null +++ b/railties/lib/rails/commands/generate.rb @@ -0,0 +1,11 @@ +require 'rails/generators' + +if [nil, "-h", "--help"].include?(ARGV.first) + Rails::Generators.help 'generate' + exit +end + +name = ARGV.shift + +root = defined?(ENGINE_ROOT) ? ENGINE_ROOT : Rails.root +Rails::Generators.invoke name, ARGV, behavior: :invoke, destination_root: root diff --git a/railties/lib/rails/commands/plugin.rb b/railties/lib/rails/commands/plugin.rb new file mode 100644 index 0000000000..95bbdd4cdf --- /dev/null +++ b/railties/lib/rails/commands/plugin.rb @@ -0,0 +1,23 @@ +if ARGV.first != "new" + ARGV[0] = "--help" +else + ARGV.shift + unless ARGV.delete("--no-rc") + customrc = ARGV.index{ |x| x.include?("--rc=") } + railsrc = if customrc + File.expand_path(ARGV.delete_at(customrc).gsub(/--rc=/, "")) + else + File.join(File.expand_path("~"), '.railsrc') + end + if File.exist?(railsrc) + extra_args_string = File.read(railsrc) + extra_args = extra_args_string.split(/\n+/).flat_map {|l| l.split} + puts "Using #{extra_args.join(" ")} from #{railsrc}" + ARGV.insert(1, *extra_args) + end + end +end + +require 'rails/generators' +require 'rails/generators/rails/plugin/plugin_generator' +Rails::Generators::PluginGenerator.start diff --git a/railties/lib/rails/commands/runner.rb b/railties/lib/rails/commands/runner.rb new file mode 100644 index 0000000000..3a71f8d3f8 --- /dev/null +++ b/railties/lib/rails/commands/runner.rb @@ -0,0 +1,63 @@ +require 'optparse' +require 'rbconfig' + +options = { environment: (ENV['RAILS_ENV'] || ENV['RACK_ENV'] || "development").dup } +code_or_file = nil + +if ARGV.first.nil? + ARGV.push "-h" +end + +ARGV.clone.options do |opts| + opts.banner = "Usage: rails runner [options] [<'Some.ruby(code)'> | <filename.rb>]" + + opts.separator "" + + opts.on("-e", "--environment=name", String, + "Specifies the environment for the runner to operate under (test/development/production).", + "Default: development") { |v| options[:environment] = v } + + opts.separator "" + + opts.on("-h", "--help", + "Show this help message.") { $stdout.puts opts; exit } + + opts.separator "" + opts.separator "Examples: " + + opts.separator " rails runner 'puts Rails.env'" + opts.separator " This runs the code `puts Rails.env` after loading the app" + opts.separator "" + opts.separator " rails runner path/to/filename.rb" + opts.separator " This runs the Ruby file located at `path/to/filename.rb` after loading the app" + + if RbConfig::CONFIG['host_os'] !~ /mswin|mingw/ + opts.separator "" + opts.separator "You can also use runner as a shebang line for your executables:" + opts.separator " -------------------------------------------------------------" + opts.separator " #!/usr/bin/env #{File.expand_path($0)} runner" + opts.separator "" + opts.separator " Product.all.each { |p| p.price *= 2 ; p.save! }" + opts.separator " -------------------------------------------------------------" + end + + opts.order! { |o| code_or_file ||= o } rescue retry +end + +ARGV.delete(code_or_file) + +ENV["RAILS_ENV"] = options[:environment] + +require APP_PATH +Rails.application.require_environment! +Rails.application.load_runner + +if code_or_file.nil? + $stderr.puts "Run '#{$0} -h' for help." + exit 1 +elsif File.exist?(code_or_file) + $0 = code_or_file + Kernel.load code_or_file +else + eval(code_or_file, binding, __FILE__, __LINE__) +end diff --git a/railties/lib/rails/commands/server.rb b/railties/lib/rails/commands/server.rb new file mode 100644 index 0000000000..c3b7bb6f84 --- /dev/null +++ b/railties/lib/rails/commands/server.rb @@ -0,0 +1,152 @@ +require 'fileutils' +require 'optparse' +require 'action_dispatch' +require 'rails' + +module Rails + class Server < ::Rack::Server + class Options + def parse!(args) + args, options = args.dup, {} + + option_parser(options).parse! args + + options[:log_stdout] = options[:daemonize].blank? && (options[:environment] || Rails.env) == "development" + options[:server] = args.shift + options + end + + private + + def option_parser(options) + OptionParser.new do |opts| + opts.banner = "Usage: rails server [mongrel, thin, etc] [options]" + opts.on("-p", "--port=port", Integer, + "Runs Rails on the specified port.", "Default: 3000") { |v| options[:Port] = v } + opts.on("-b", "--binding=ip", String, + "Binds Rails to the specified ip.", "Default: 0.0.0.0") { |v| options[:Host] = v } + opts.on("-c", "--config=file", String, + "Use custom rackup configuration file") { |v| options[:config] = v } + opts.on("-d", "--daemon", "Make server run as a Daemon.") { options[:daemonize] = true } + opts.on("-u", "--debugger", "Enable the debugger") do + if RUBY_VERSION < '2.0.0' + options[:debugger] = true + else + puts "=> Notice: debugger option is ignored since ruby 2.0 and " \ + "it will be removed in future versions" + end + end + opts.on("-e", "--environment=name", String, + "Specifies the environment to run this server under (test/development/production).", + "Default: development") { |v| options[:environment] = v } + opts.on("-P", "--pid=pid", String, + "Specifies the PID file.", + "Default: tmp/pids/server.pid") { |v| options[:pid] = v } + + opts.separator "" + + opts.on("-h", "--help", "Show this help message.") { puts opts; exit } + end + end + end + + def initialize(*) + super + set_environment + end + + # TODO: this is no longer required but we keep it for the moment to support older config.ru files. + def app + @app ||= begin + app = super + app.respond_to?(:to_app) ? app.to_app : app + end + end + + def opt_parser + Options.new + end + + def set_environment + ENV["RAILS_ENV"] ||= options[:environment] + end + + def start + print_boot_information + trap(:INT) { exit } + create_tmp_directories + log_to_stdout if options[:log_stdout] + + super + ensure + # The '-h' option calls exit before @options is set. + # If we call 'options' with it unset, we get double help banners. + puts 'Exiting' unless @options && options[:daemonize] + end + + def middleware + middlewares = [] + if RUBY_VERSION < '2.0.0' + middlewares << [Rails::Rack::Debugger] if options[:debugger] + end + middlewares << [::Rack::ContentLength] + + # FIXME: add Rack::Lock in the case people are using webrick. + # This is to remain backwards compatible for those who are + # running webrick in production. We should consider removing this + # in development. + if server.name == 'Rack::Handler::WEBrick' + middlewares << [::Rack::Lock] + end + + Hash.new(middlewares) + end + + def log_path + "log/#{options[:environment]}.log" + end + + def default_options + super.merge({ + Port: 3000, + DoNotReverseLookup: true, + environment: (ENV['RAILS_ENV'] || ENV['RACK_ENV'] || "development").dup, + daemonize: false, + debugger: false, + pid: File.expand_path("tmp/pids/server.pid"), + config: File.expand_path("config.ru") + }) + end + + private + + def print_boot_information + url = "#{options[:SSLEnable] ? 'https' : 'http'}://#{options[:Host]}:#{options[:Port]}" + puts "=> Booting #{ActiveSupport::Inflector.demodulize(server)}" + puts "=> Rails #{Rails.version} application starting in #{Rails.env} on #{url}" + puts "=> Run `rails server -h` for more startup options" + + if options[:Host].to_s.match(/0\.0\.0\.0/) + puts "=> Notice: server is listening on all interfaces (#{options[:Host]}). Consider using 127.0.0.1 (--binding option)" + end + + puts "=> Ctrl-C to shutdown server" unless options[:daemonize] + end + + def create_tmp_directories + %w(cache pids sessions sockets).each do |dir_to_make| + FileUtils.mkdir_p(File.join(Rails.root, 'tmp', dir_to_make)) + end + end + + def log_to_stdout + wrapped_app # touch the app so the logger is set up + + console = ActiveSupport::Logger.new($stdout) + console.formatter = Rails.logger.formatter + console.level = Rails.logger.level + + Rails.logger.extend(ActiveSupport::Logger.broadcast(console)) + end + end +end diff --git a/railties/lib/rails/commands/update.rb b/railties/lib/rails/commands/update.rb new file mode 100644 index 0000000000..59fae5c337 --- /dev/null +++ b/railties/lib/rails/commands/update.rb @@ -0,0 +1,9 @@ +require File.expand_path(File.join(File.dirname(__FILE__), '..', 'generators')) + +if ARGV.size == 0 + Rails::Generators.help + exit +end + +name = ARGV.shift +Rails::Generators.invoke name, ARGV, behavior: :skip diff --git a/railties/lib/rails/configuration.rb b/railties/lib/rails/configuration.rb new file mode 100644 index 0000000000..f5d7dede66 --- /dev/null +++ b/railties/lib/rails/configuration.rb @@ -0,0 +1,119 @@ +require 'active_support/ordered_options' +require 'active_support/core_ext/object' +require 'rails/paths' +require 'rails/rack' + +module Rails + module Configuration + # MiddlewareStackProxy is a proxy for the Rails middleware stack that allows + # you to configure middlewares in your application. It works basically as a + # command recorder, saving each command to be applied after initialization + # over the default middleware stack, so you can add, swap, or remove any + # middleware in Rails. + # + # You can add your own middlewares by using the +config.middleware.use+ method: + # + # config.middleware.use Magical::Unicorns + # + # This will put the <tt>Magical::Unicorns</tt> middleware on the end of the stack. + # You can use +insert_before+ if you wish to add a middleware before another: + # + # config.middleware.insert_before ActionDispatch::Head, Magical::Unicorns + # + # There's also +insert_after+ which will insert a middleware after another: + # + # config.middleware.insert_after ActionDispatch::Head, Magical::Unicorns + # + # Middlewares can also be completely swapped out and replaced with others: + # + # config.middleware.swap ActionDispatch::Flash, Magical::Unicorns + # + # And finally they can also be removed from the stack completely: + # + # config.middleware.delete ActionDispatch::Flash + # + class MiddlewareStackProxy + def initialize + @operations = [] + end + + def insert_before(*args, &block) + @operations << [__method__, args, block] + end + + alias :insert :insert_before + + def insert_after(*args, &block) + @operations << [__method__, args, block] + end + + def swap(*args, &block) + @operations << [__method__, args, block] + end + + def use(*args, &block) + @operations << [__method__, args, block] + end + + def delete(*args, &block) + @operations << [__method__, args, block] + end + + def unshift(*args, &block) + @operations << [__method__, args, block] + end + + def merge_into(other) #:nodoc: + @operations.each do |operation, args, block| + other.send(operation, *args, &block) + end + other + end + end + + class Generators #:nodoc: + attr_accessor :aliases, :options, :templates, :fallbacks, :colorize_logging + attr_reader :hidden_namespaces + + def initialize + @aliases = Hash.new { |h,k| h[k] = {} } + @options = Hash.new { |h,k| h[k] = {} } + @fallbacks = {} + @templates = [] + @colorize_logging = true + @hidden_namespaces = [] + end + + def initialize_copy(source) + @aliases = @aliases.deep_dup + @options = @options.deep_dup + @fallbacks = @fallbacks.deep_dup + @templates = @templates.dup + end + + def hide_namespace(namespace) + @hidden_namespaces << namespace + end + + def method_missing(method, *args) + method = method.to_s.sub(/=$/, '').to_sym + + return @options[method] if args.empty? + + if method == :rails || args.first.is_a?(Hash) + namespace, configuration = method, args.shift + else + namespace, configuration = args.shift, args.shift + namespace = namespace.to_sym if namespace.respond_to?(:to_sym) + @options[:rails][method] = namespace + end + + if configuration + aliases = configuration.delete(:aliases) + @aliases[namespace].merge!(aliases) if aliases + @options[namespace].merge!(configuration) + end + end + end + end +end diff --git a/railties/lib/rails/console/app.rb b/railties/lib/rails/console/app.rb new file mode 100644 index 0000000000..2a69c26deb --- /dev/null +++ b/railties/lib/rails/console/app.rb @@ -0,0 +1,32 @@ +require 'active_support/all' +require 'action_controller' + +module Rails + module ConsoleMethods + # reference the global "app" instance, created on demand. To recreate the + # instance, pass a non-false value as the parameter. + def app(create=false) + @app_integration_instance = nil if create + @app_integration_instance ||= new_session do |sess| + sess.host! "www.example.com" + end + end + + # create a new session. If a block is given, the new session will be yielded + # to the block before being returned. + def new_session + app = Rails.application + session = ActionDispatch::Integration::Session.new(app) + yield session if block_given? + session + end + + # reloads the environment + def reload!(print=true) + puts "Reloading..." if print + ActionDispatch::Reloader.cleanup! + ActionDispatch::Reloader.prepare! + true + end + end +end diff --git a/railties/lib/rails/console/helpers.rb b/railties/lib/rails/console/helpers.rb new file mode 100644 index 0000000000..b775f1ff8d --- /dev/null +++ b/railties/lib/rails/console/helpers.rb @@ -0,0 +1,17 @@ +module Rails + module ConsoleMethods + # Gets the helper methods available to the controller. + # + # This method assumes an +ApplicationController+ exists, and it extends +ActionController::Base+ + def helper + @helper ||= ApplicationController.helpers + end + + # Gets a new instance of a controller object. + # + # This method assumes an +ApplicationController+ exists, and it extends +ActionController::Base+ + def controller + @controller ||= ApplicationController.new + end + end +end diff --git a/railties/lib/rails/deprecation.rb b/railties/lib/rails/deprecation.rb new file mode 100644 index 0000000000..89f54069e9 --- /dev/null +++ b/railties/lib/rails/deprecation.rb @@ -0,0 +1,19 @@ +require 'active_support/deprecation/proxy_wrappers' + +module Rails + class DeprecatedConstant < ActiveSupport::Deprecation::DeprecatedConstantProxy + def self.deprecate(old, current) + # double assignment is used to avoid "assigned but unused variable" warning + constant = constant = new(old, current) + eval "::#{old} = constant" + end + + private + + def target + ::Kernel.eval @new_const.to_s + end + end + + DeprecatedConstant.deprecate('RAILS_CACHE', '::Rails.cache') +end diff --git a/railties/lib/rails/engine.rb b/railties/lib/rails/engine.rb new file mode 100644 index 0000000000..dc3da1eb41 --- /dev/null +++ b/railties/lib/rails/engine.rb @@ -0,0 +1,691 @@ +require 'rails/railtie' +require 'rails/engine/railties' +require 'active_support/core_ext/module/delegation' +require 'pathname' + +module Rails + # <tt>Rails::Engine</tt> allows you to wrap a specific Rails application or subset of + # functionality and share it with other applications or within a larger packaged application. + # Since Rails 3.0, every <tt>Rails::Application</tt> is just an engine, which allows for simple + # feature and application sharing. + # + # Any <tt>Rails::Engine</tt> is also a <tt>Rails::Railtie</tt>, so the same + # methods (like <tt>rake_tasks</tt> and +generators+) and configuration + # options that are available in railties can also be used in engines. + # + # == Creating an Engine + # + # In Rails versions prior to 3.0, your gems automatically behaved as engines, however, + # this coupled Rails to Rubygems. Since Rails 3.0, if you want a gem to automatically + # behave as an engine, you have to specify an +Engine+ for it somewhere inside + # your plugin's +lib+ folder (similar to how we specify a +Railtie+): + # + # # lib/my_engine.rb + # module MyEngine + # class Engine < Rails::Engine + # end + # end + # + # Then ensure that this file is loaded at the top of your <tt>config/application.rb</tt> + # (or in your +Gemfile+) and it will automatically load models, controllers and helpers + # inside +app+, load routes at <tt>config/routes.rb</tt>, load locales at + # <tt>config/locales/*</tt>, and load tasks at <tt>lib/tasks/*</tt>. + # + # == Configuration + # + # Besides the +Railtie+ configuration which is shared across the application, in a + # <tt>Rails::Engine</tt> you can access <tt>autoload_paths</tt>, <tt>eager_load_paths</tt> + # and <tt>autoload_once_paths</tt>, which, differently from a <tt>Railtie</tt>, are scoped to + # the current engine. + # + # class MyEngine < Rails::Engine + # # Add a load path for this specific Engine + # config.autoload_paths << File.expand_path("../lib/some/path", __FILE__) + # + # initializer "my_engine.add_middleware" do |app| + # app.middleware.use MyEngine::Middleware + # end + # end + # + # == Generators + # + # You can set up generators for engines with <tt>config.generators</tt> method: + # + # class MyEngine < Rails::Engine + # config.generators do |g| + # g.orm :active_record + # g.template_engine :erb + # g.test_framework :test_unit + # end + # end + # + # You can also set generators for an application by using <tt>config.app_generators</tt>: + # + # class MyEngine < Rails::Engine + # # note that you can also pass block to app_generators in the same way you + # # can pass it to generators method + # config.app_generators.orm :datamapper + # end + # + # == Paths + # + # Since Rails 3.0, applications and engines have more flexible path configuration (as + # opposed to the previous hardcoded path configuration). This means that you are not + # required to place your controllers at <tt>app/controllers</tt>, but in any place + # which you find convenient. + # + # For example, let's suppose you want to place your controllers in <tt>lib/controllers</tt>. + # You can set that as an option: + # + # class MyEngine < Rails::Engine + # paths["app/controllers"] = "lib/controllers" + # end + # + # You can also have your controllers loaded from both <tt>app/controllers</tt> and + # <tt>lib/controllers</tt>: + # + # class MyEngine < Rails::Engine + # paths["app/controllers"] << "lib/controllers" + # end + # + # The available paths in an engine are: + # + # class MyEngine < Rails::Engine + # paths["app"] # => ["app"] + # paths["app/controllers"] # => ["app/controllers"] + # paths["app/helpers"] # => ["app/helpers"] + # paths["app/models"] # => ["app/models"] + # paths["app/views"] # => ["app/views"] + # paths["lib"] # => ["lib"] + # paths["lib/tasks"] # => ["lib/tasks"] + # paths["config"] # => ["config"] + # paths["config/initializers"] # => ["config/initializers"] + # paths["config/locales"] # => ["config/locales"] + # paths["config/routes.rb"] # => ["config/routes.rb"] + # end + # + # The <tt>Application</tt> class adds a couple more paths to this set. And as in your + # <tt>Application</tt>, all folders under +app+ are automatically added to the load path. + # If you have an <tt>app/services</tt> folder for example, it will be added by default. + # + # == Endpoint + # + # An engine can be also a rack application. It can be useful if you have a rack application that + # you would like to wrap with +Engine+ and provide some of the +Engine+'s features. + # + # To do that, use the +endpoint+ method: + # + # module MyEngine + # class Engine < Rails::Engine + # endpoint MyRackApplication + # end + # end + # + # Now you can mount your engine in application's routes just like that: + # + # Rails.application.routes.draw do + # mount MyEngine::Engine => "/engine" + # end + # + # == Middleware stack + # + # As an engine can now be a rack endpoint, it can also have a middleware + # stack. The usage is exactly the same as in <tt>Application</tt>: + # + # module MyEngine + # class Engine < Rails::Engine + # middleware.use SomeMiddleware + # end + # end + # + # == Routes + # + # If you don't specify an endpoint, routes will be used as the default + # endpoint. You can use them just like you use an application's routes: + # + # # ENGINE/config/routes.rb + # MyEngine::Engine.routes.draw do + # get "/" => "posts#index" + # end + # + # == Mount priority + # + # Note that now there can be more than one router in your application, and it's better to avoid + # passing requests through many routers. Consider this situation: + # + # Rails.application.routes.draw do + # mount MyEngine::Engine => "/blog" + # get "/blog/omg" => "main#omg" + # end + # + # +MyEngine+ is mounted at <tt>/blog</tt>, and <tt>/blog/omg</tt> points to application's + # controller. In such a situation, requests to <tt>/blog/omg</tt> will go through +MyEngine+, + # and if there is no such route in +Engine+'s routes, it will be dispatched to <tt>main#omg</tt>. + # It's much better to swap that: + # + # Rails.application.routes.draw do + # get "/blog/omg" => "main#omg" + # mount MyEngine::Engine => "/blog" + # end + # + # Now, +Engine+ will get only requests that were not handled by +Application+. + # + # == Engine name + # + # There are some places where an Engine's name is used: + # + # * routes: when you mount an Engine with <tt>mount(MyEngine::Engine => '/my_engine')</tt>, + # it's used as default <tt>:as</tt> option + # * rake task for installing migrations <tt>my_engine:install:migrations</tt> + # + # Engine name is set by default based on class name. For <tt>MyEngine::Engine</tt> it will be + # <tt>my_engine_engine</tt>. You can change it manually using the <tt>engine_name</tt> method: + # + # module MyEngine + # class Engine < Rails::Engine + # engine_name "my_engine" + # end + # end + # + # == Isolated Engine + # + # Normally when you create controllers, helpers and models inside an engine, they are treated + # as if they were created inside the application itself. This means that all helpers and + # named routes from the application will be available to your engine's controllers as well. + # + # However, sometimes you want to isolate your engine from the application, especially if your engine + # has its own router. To do that, you simply need to call +isolate_namespace+. This method requires + # you to pass a module where all your controllers, helpers and models should be nested to: + # + # module MyEngine + # class Engine < Rails::Engine + # isolate_namespace MyEngine + # end + # end + # + # With such an engine, everything that is inside the +MyEngine+ module will be isolated from + # the application. + # + # Consider such controller: + # + # module MyEngine + # class FooController < ActionController::Base + # end + # end + # + # If an engine is marked as isolated, +FooController+ has access only to helpers from +Engine+ and + # <tt>url_helpers</tt> from <tt>MyEngine::Engine.routes</tt>. + # + # The next thing that changes in isolated engines is the behavior of routes. Normally, when you namespace + # your controllers, you also need to do namespace all your routes. With an isolated engine, + # the namespace is applied by default, so you can ignore it in routes: + # + # MyEngine::Engine.routes.draw do + # resources :articles + # end + # + # The routes above will automatically point to <tt>MyEngine::ArticlesController</tt>. Furthermore, you don't + # need to use longer url helpers like <tt>my_engine_articles_path</tt>. Instead, you should simply use + # <tt>articles_path</tt> as you would do with your application. + # + # To make that behavior consistent with other parts of the framework, an isolated engine also has influence on + # <tt>ActiveModel::Naming</tt>. When you use a namespaced model, like <tt>MyEngine::Article</tt>, it will normally + # use the prefix "my_engine". In an isolated engine, the prefix will be omitted in url helpers and + # form fields for convenience. + # + # polymorphic_url(MyEngine::Article.new) # => "articles_path" + # + # form_for(MyEngine::Article.new) do + # text_field :title # => <input type="text" name="article[title]" id="article_title" /> + # end + # + # Additionally, an isolated engine will set its name according to namespace, so + # MyEngine::Engine.engine_name will be "my_engine". It will also set MyEngine.table_name_prefix + # to "my_engine_", changing the MyEngine::Article model to use the my_engine_articles table. + # + # == Using Engine's routes outside Engine + # + # Since you can now mount an engine inside application's routes, you do not have direct access to +Engine+'s + # <tt>url_helpers</tt> inside +Application+. When you mount an engine in an application's routes, a special helper is + # created to allow you to do that. Consider such a scenario: + # + # # config/routes.rb + # Rails.application.routes.draw do + # mount MyEngine::Engine => "/my_engine", as: "my_engine" + # get "/foo" => "foo#index" + # end + # + # Now, you can use the <tt>my_engine</tt> helper inside your application: + # + # class FooController < ApplicationController + # def index + # my_engine.root_url # => /my_engine/ + # end + # end + # + # There is also a <tt>main_app</tt> helper that gives you access to application's routes inside Engine: + # + # module MyEngine + # class BarController + # def index + # main_app.foo_path # => /foo + # end + # end + # end + # + # Note that the <tt>:as</tt> option given to mount takes the <tt>engine_name</tt> as default, so most of the time + # you can simply omit it. + # + # Finally, if you want to generate a url to an engine's route using + # <tt>polymorphic_url</tt>, you also need to pass the engine helper. Let's + # say that you want to create a form pointing to one of the engine's routes. + # All you need to do is pass the helper as the first element in array with + # attributes for url: + # + # form_for([my_engine, @user]) + # + # This code will use <tt>my_engine.user_path(@user)</tt> to generate the proper route. + # + # == Isolated engine's helpers + # + # Sometimes you may want to isolate engine, but use helpers that are defined for it. + # If you want to share just a few specific helpers you can add them to application's + # helpers in ApplicationController: + # + # class ApplicationController < ActionController::Base + # helper MyEngine::SharedEngineHelper + # end + # + # If you want to include all of the engine's helpers, you can use #helper method on an engine's + # instance: + # + # class ApplicationController < ActionController::Base + # helper MyEngine::Engine.helpers + # end + # + # It will include all of the helpers from engine's directory. Take into account that this does + # not include helpers defined in controllers with helper_method or other similar solutions, + # only helpers defined in the helpers directory will be included. + # + # == Migrations & seed data + # + # Engines can have their own migrations. The default path for migrations is exactly the same + # as in application: <tt>db/migrate</tt> + # + # To use engine's migrations in application you can use rake task, which copies them to + # application's dir: + # + # rake ENGINE_NAME:install:migrations + # + # Note that some of the migrations may be skipped if a migration with the same name already exists + # in application. In such a situation you must decide whether to leave that migration or rename the + # migration in the application and rerun copying migrations. + # + # If your engine has migrations, you may also want to prepare data for the database in + # the <tt>db/seeds.rb</tt> file. You can load that data using the <tt>load_seed</tt> method, e.g. + # + # MyEngine::Engine.load_seed + # + # == Loading priority + # + # In order to change engine's priority you can use +config.railties_order+ in main application. + # It will affect the priority of loading views, helpers, assets and all the other files + # related to engine or application. + # + # # load Blog::Engine with highest priority, followed by application and other railties + # config.railties_order = [Blog::Engine, :main_app, :all] + class Engine < Railtie + autoload :Configuration, "rails/engine/configuration" + + class << self + attr_accessor :called_from, :isolated + + alias :isolated? :isolated + alias :engine_name :railtie_name + + delegate :eager_load!, to: :instance + + def inherited(base) + unless base.abstract_railtie? + Rails::Railtie::Configuration.eager_load_namespaces << base + + base.called_from = begin + call_stack = if Kernel.respond_to?(:caller_locations) + caller_locations.map(&:path) + else + # Remove the line number from backtraces making sure we don't leave anything behind + caller.map { |p| p.sub(/:\d+.*/, '') } + end + + File.dirname(call_stack.detect { |p| p !~ %r[railties[\w.-]*/lib/rails|rack[\w.-]*/lib/rack] }) + end + end + + super + end + + def endpoint(endpoint = nil) + @endpoint ||= nil + @endpoint = endpoint if endpoint + @endpoint + end + + def isolate_namespace(mod) + engine_name(generate_railtie_name(mod.name)) + + self.routes.default_scope = { module: ActiveSupport::Inflector.underscore(mod.name) } + self.isolated = true + + unless mod.respond_to?(:railtie_namespace) + name, railtie = engine_name, self + + mod.singleton_class.instance_eval do + define_method(:railtie_namespace) { railtie } + + unless mod.respond_to?(:table_name_prefix) + define_method(:table_name_prefix) { "#{name}_" } + end + + unless mod.respond_to?(:use_relative_model_naming?) + class_eval "def use_relative_model_naming?; true; end", __FILE__, __LINE__ + end + + unless mod.respond_to?(:railtie_helpers_paths) + define_method(:railtie_helpers_paths) { railtie.helpers_paths } + end + + unless mod.respond_to?(:railtie_routes_url_helpers) + define_method(:railtie_routes_url_helpers) {|include_path_helpers = true| railtie.routes.url_helpers(include_path_helpers) } + end + end + end + end + + # Finds engine with given path + def find(path) + expanded_path = File.expand_path path + Rails::Engine.subclasses.each do |klass| + engine = klass.instance + return engine if File.expand_path(engine.root) == expanded_path + end + nil + end + end + + delegate :middleware, :root, :paths, to: :config + delegate :engine_name, :isolated?, to: :class + + def initialize + @_all_autoload_paths = nil + @_all_load_paths = nil + @app = nil + @config = nil + @env_config = nil + @helpers = nil + @routes = nil + super + end + + # Load console and invoke the registered hooks. + # Check <tt>Rails::Railtie.console</tt> for more info. + def load_console(app=self) + require "rails/console/app" + require "rails/console/helpers" + run_console_blocks(app) + self + end + + # Load Rails runner and invoke the registered hooks. + # Check <tt>Rails::Railtie.runner</tt> for more info. + def load_runner(app=self) + run_runner_blocks(app) + self + end + + # Load Rake, railties tasks and invoke the registered hooks. + # Check <tt>Rails::Railtie.rake_tasks</tt> for more info. + def load_tasks(app=self) + require "rake" + run_tasks_blocks(app) + self + end + + # Load Rails generators and invoke the registered hooks. + # Check <tt>Rails::Railtie.generators</tt> for more info. + def load_generators(app=self) + require "rails/generators" + run_generators_blocks(app) + Rails::Generators.configure!(app.config.generators) + self + end + + # Eager load the application by loading all ruby + # files inside eager_load paths. + def eager_load! + config.eager_load_paths.each do |load_path| + matcher = /\A#{Regexp.escape(load_path.to_s)}\/(.*)\.rb\Z/ + Dir.glob("#{load_path}/**/*.rb").sort.each do |file| + require_dependency file.sub(matcher, '\1') + end + end + end + + def railties + @railties ||= Railties.new + end + + # Returns a module with all the helpers defined for the engine. + def helpers + @helpers ||= begin + helpers = Module.new + all = ActionController::Base.all_helpers_from_path(helpers_paths) + ActionController::Base.modules_for_helpers(all).each do |mod| + helpers.send(:include, mod) + end + helpers + end + end + + # Returns all registered helpers paths. + def helpers_paths + paths["app/helpers"].existent + end + + # Returns the underlying rack application for this engine. + def app + @app ||= begin + config.middleware = config.middleware.merge_into(default_middleware_stack) + config.middleware.build(endpoint) + end + end + + # Returns the endpoint for this engine. If none is registered, + # defaults to an ActionDispatch::Routing::RouteSet. + def endpoint + self.class.endpoint || routes + end + + # Define the Rack API for this engine. + def call(env) + env.merge!(env_config) + if env['SCRIPT_NAME'] + env["ROUTES_#{routes.object_id}_SCRIPT_NAME"] = env['SCRIPT_NAME'].dup + end + app.call(env) + end + + # Defines additional Rack env configuration that is added on each call. + def env_config + @env_config ||= { + 'action_dispatch.routes' => routes + } + end + + # Defines the routes for this engine. If a block is given to + # routes, it is appended to the engine. + def routes + @routes ||= ActionDispatch::Routing::RouteSet.new + @routes.append(&Proc.new) if block_given? + @routes + end + + # Define the configuration object for the engine. + def config + @config ||= Engine::Configuration.new(find_root_with_flag("lib")) + end + + # Load data from db/seeds.rb file. It can be used in to load engines' + # seeds, e.g.: + # + # Blog::Engine.load_seed + def load_seed + seed_file = paths["db/seeds.rb"].existent.first + load(seed_file) if seed_file + end + + # Add configured load paths to ruby load paths and remove duplicates. + initializer :set_load_path, before: :bootstrap_hook do + _all_load_paths.reverse_each do |path| + $LOAD_PATH.unshift(path) if File.directory?(path) + end + $LOAD_PATH.uniq! + end + + # Set the paths from which Rails will automatically load source files, + # and the load_once paths. + # + # This needs to be an initializer, since it needs to run once + # per engine and get the engine as a block parameter + initializer :set_autoload_paths, before: :bootstrap_hook do + ActiveSupport::Dependencies.autoload_paths.unshift(*_all_autoload_paths) + ActiveSupport::Dependencies.autoload_once_paths.unshift(*_all_autoload_once_paths) + + # Freeze so future modifications will fail rather than do nothing mysteriously + config.autoload_paths.freeze + config.eager_load_paths.freeze + config.autoload_once_paths.freeze + end + + initializer :add_routing_paths do |app| + paths = self.paths["config/routes.rb"].existent + + if routes? || paths.any? + app.routes_reloader.paths.unshift(*paths) + app.routes_reloader.route_sets << routes + end + end + + # I18n load paths are a special case since the ones added + # later have higher priority. + initializer :add_locales do + config.i18n.railties_load_path.concat(paths["config/locales"].existent) + end + + initializer :add_view_paths do + views = paths["app/views"].existent + unless views.empty? + ActiveSupport.on_load(:action_controller){ prepend_view_path(views) if respond_to?(:prepend_view_path) } + ActiveSupport.on_load(:action_mailer){ prepend_view_path(views) } + end + end + + initializer :load_environment_config, before: :load_environment_hook, group: :all do + paths["config/environments"].existent.each do |environment| + require environment + end + end + + initializer :append_assets_path, group: :all do |app| + app.config.assets.paths.unshift(*paths["vendor/assets"].existent_directories) + app.config.assets.paths.unshift(*paths["lib/assets"].existent_directories) + app.config.assets.paths.unshift(*paths["app/assets"].existent_directories) + end + + initializer :prepend_helpers_path do |app| + if !isolated? || (app == self) + app.config.helpers_paths.unshift(*paths["app/helpers"].existent) + end + end + + initializer :load_config_initializers do + config.paths["config/initializers"].existent.sort.each do |initializer| + load_config_initializer(initializer) + end + end + + initializer :engines_blank_point do + # We need this initializer so all extra initializers added in engines are + # consistently executed after all the initializers above across all engines. + end + + rake_tasks do + next if self.is_a?(Rails::Application) + next unless has_migrations? + + namespace railtie_name do + namespace :install do + desc "Copy migrations from #{railtie_name} to application" + task :migrations do + ENV["FROM"] = railtie_name + if Rake::Task.task_defined?("railties:install:migrations") + Rake::Task["railties:install:migrations"].invoke + else + Rake::Task["app:railties:install:migrations"].invoke + end + end + end + end + end + + def routes? #:nodoc: + @routes + end + + protected + + def load_config_initializer(initializer) + ActiveSupport::Notifications.instrument('load_config_initializer.railties', initializer: initializer) do + load(initializer) + end + end + + def run_tasks_blocks(*) #:nodoc: + super + paths["lib/tasks"].existent.sort.each { |ext| load(ext) } + end + + def has_migrations? #:nodoc: + paths["db/migrate"].existent.any? + end + + def find_root_with_flag(flag, default=nil) #:nodoc: + root_path = self.class.called_from + + while root_path && File.directory?(root_path) && !File.exist?("#{root_path}/#{flag}") + parent = File.dirname(root_path) + root_path = parent != root_path && parent + end + + root = File.exist?("#{root_path}/#{flag}") ? root_path : default + raise "Could not find root path for #{self}" unless root + + Pathname.new File.realpath root + end + + def default_middleware_stack #:nodoc: + ActionDispatch::MiddlewareStack.new + end + + def _all_autoload_once_paths #:nodoc: + config.autoload_once_paths + end + + def _all_autoload_paths #:nodoc: + @_all_autoload_paths ||= (config.autoload_paths + config.eager_load_paths + config.autoload_once_paths).uniq + end + + def _all_load_paths #:nodoc: + @_all_load_paths ||= (config.paths.load_paths + _all_autoload_paths).uniq + end + end +end diff --git a/railties/lib/rails/engine/commands.rb b/railties/lib/rails/engine/commands.rb new file mode 100644 index 0000000000..f39f926109 --- /dev/null +++ b/railties/lib/rails/engine/commands.rb @@ -0,0 +1,41 @@ +ARGV << '--help' if ARGV.empty? + +aliases = { + "g" => "generate", + "d" => "destroy" +} + +command = ARGV.shift +command = aliases[command] || command + +require ENGINE_PATH +engine = ::Rails::Engine.find(ENGINE_ROOT) + +case command +when 'generate', 'destroy' + require 'rails/generators' + Rails::Generators.namespace = engine.railtie_namespace + engine.load_generators + require "rails/commands/#{command}" + +when '--version', '-v' + ARGV.unshift '--version' + require 'rails/commands/application' + +else + puts "Error: Command not recognized" unless %w(-h --help).include?(command) + puts <<-EOT +Usage: rails COMMAND [ARGS] + +The common Rails commands available for engines are: + generate Generate new code (short-cut alias: "g") + destroy Undo code generated with "generate" (short-cut alias: "d") + +All commands can be run with -h for more information. + +If you want to run any commands that need to be run in context +of the application, like `rails server` or `rails console`, +you should do it from application's directory (typically test/dummy). + EOT + exit(1) +end diff --git a/railties/lib/rails/engine/configuration.rb b/railties/lib/rails/engine/configuration.rb new file mode 100644 index 0000000000..10d1821709 --- /dev/null +++ b/railties/lib/rails/engine/configuration.rb @@ -0,0 +1,91 @@ +require 'rails/railtie/configuration' + +module Rails + class Engine + class Configuration < ::Rails::Railtie::Configuration + attr_reader :root + attr_writer :middleware, :eager_load_paths, :autoload_once_paths, :autoload_paths + + def initialize(root=nil) + super() + @root = root + @generators = app_generators.dup + end + + # Returns the middleware stack for the engine. + def middleware + @middleware ||= Rails::Configuration::MiddlewareStackProxy.new + end + + # Holds generators configuration: + # + # config.generators do |g| + # g.orm :data_mapper, migration: true + # g.template_engine :haml + # g.test_framework :rspec + # end + # + # If you want to disable color in console, do: + # + # config.generators.colorize_logging = false + # + def generators #:nodoc: + @generators ||= Rails::Configuration::Generators.new + yield(@generators) if block_given? + @generators + end + + def paths + @paths ||= begin + paths = Rails::Paths::Root.new(@root) + + paths.add "app", eager_load: true, glob: "*" + paths.add "app/assets", glob: "*" + paths.add "app/controllers", eager_load: true + paths.add "app/helpers", eager_load: true + paths.add "app/models", eager_load: true + paths.add "app/mailers", eager_load: true + paths.add "app/views" + + paths.add "app/controllers/concerns", eager_load: true + paths.add "app/models/concerns", eager_load: true + + paths.add "lib", load_path: true + paths.add "lib/assets", glob: "*" + paths.add "lib/tasks", glob: "**/*.rake" + + paths.add "config" + paths.add "config/environments", glob: "#{Rails.env}.rb" + paths.add "config/initializers", glob: "**/*.rb" + paths.add "config/locales", glob: "*.{rb,yml}" + paths.add "config/routes.rb" + + paths.add "db" + paths.add "db/migrate" + paths.add "db/seeds.rb" + + paths.add "vendor", load_path: true + paths.add "vendor/assets", glob: "*" + + paths + end + end + + def root=(value) + @root = paths.path = Pathname.new(value).expand_path + end + + def eager_load_paths + @eager_load_paths ||= paths.eager_load + end + + def autoload_once_paths + @autoload_once_paths ||= paths.autoload_once + end + + def autoload_paths + @autoload_paths ||= paths.autoload_paths + end + end + end +end diff --git a/railties/lib/rails/engine/railties.rb b/railties/lib/rails/engine/railties.rb new file mode 100644 index 0000000000..9969a1475d --- /dev/null +++ b/railties/lib/rails/engine/railties.rb @@ -0,0 +1,21 @@ +module Rails + class Engine < Railtie + class Railties + include Enumerable + attr_reader :_all + + def initialize + @_all ||= ::Rails::Railtie.subclasses.map(&:instance) + + ::Rails::Engine.subclasses.map(&:instance) + end + + def each(*args, &block) + _all.each(*args, &block) + end + + def -(others) + _all - others + end + end + end +end diff --git a/railties/lib/rails/gem_version.rb b/railties/lib/rails/gem_version.rb new file mode 100644 index 0000000000..c7397c4f15 --- /dev/null +++ b/railties/lib/rails/gem_version.rb @@ -0,0 +1,15 @@ +module Rails + # Returns the version of the currently loaded Rails as a <tt>Gem::Version</tt> + def self.gem_version + Gem::Version.new VERSION::STRING + end + + module VERSION + MAJOR = 4 + MINOR = 2 + TINY = 0 + PRE = "alpha" + + STRING = [MAJOR, MINOR, TINY, PRE].compact.join(".") + end +end diff --git a/railties/lib/rails/generators.rb b/railties/lib/rails/generators.rb new file mode 100644 index 0000000000..bf2390cb7e --- /dev/null +++ b/railties/lib/rails/generators.rb @@ -0,0 +1,373 @@ +activesupport_path = File.expand_path('../../../../activesupport/lib', __FILE__) +$:.unshift(activesupport_path) if File.directory?(activesupport_path) && !$:.include?(activesupport_path) + +require 'thor/group' + +require 'active_support' +require 'active_support/core_ext/object/blank' +require 'active_support/core_ext/kernel/singleton_class' +require 'active_support/core_ext/array/extract_options' +require 'active_support/core_ext/hash/deep_merge' +require 'active_support/core_ext/module/attribute_accessors' +require 'active_support/core_ext/string/inflections' + +module Rails + module Generators + autoload :Actions, 'rails/generators/actions' + autoload :ActiveModel, 'rails/generators/active_model' + autoload :Base, 'rails/generators/base' + autoload :Migration, 'rails/generators/migration' + autoload :NamedBase, 'rails/generators/named_base' + autoload :ResourceHelpers, 'rails/generators/resource_helpers' + autoload :TestCase, 'rails/generators/test_case' + + mattr_accessor :namespace + + DEFAULT_ALIASES = { + rails: { + actions: '-a', + orm: '-o', + javascripts: '-j', + javascript_engine: '-je', + resource_controller: '-c', + scaffold_controller: '-c', + stylesheets: '-y', + stylesheet_engine: '-se', + template_engine: '-e', + test_framework: '-t' + }, + + test_unit: { + fixture_replacement: '-r', + } + } + + DEFAULT_OPTIONS = { + rails: { + assets: true, + force_plural: false, + helper: true, + integration_tool: nil, + javascripts: true, + javascript_engine: :js, + orm: false, + resource_controller: :controller, + resource_route: true, + scaffold_controller: :scaffold_controller, + stylesheets: true, + stylesheet_engine: :css, + test_framework: false, + template_engine: :erb + } + } + + def self.configure!(config) #:nodoc: + no_color! unless config.colorize_logging + aliases.deep_merge! config.aliases + options.deep_merge! config.options + fallbacks.merge! config.fallbacks + templates_path.concat config.templates + templates_path.uniq! + hide_namespaces(*config.hidden_namespaces) + end + + def self.templates_path #:nodoc: + @templates_path ||= [] + end + + def self.aliases #:nodoc: + @aliases ||= DEFAULT_ALIASES.dup + end + + def self.options #:nodoc: + @options ||= DEFAULT_OPTIONS.dup + end + + # Hold configured generators fallbacks. If a plugin developer wants a + # generator group to fallback to another group in case of missing generators, + # they can add a fallback. + # + # For example, shoulda is considered a test_framework and is an extension + # of test_unit. However, most part of shoulda generators are similar to + # test_unit ones. + # + # Shoulda then can tell generators to search for test_unit generators when + # some of them are not available by adding a fallback: + # + # Rails::Generators.fallbacks[:shoulda] = :test_unit + def self.fallbacks + @fallbacks ||= {} + end + + # Remove the color from output. + def self.no_color! + Thor::Base.shell = Thor::Shell::Basic + end + + # Track all generators subclasses. + def self.subclasses + @subclasses ||= [] + end + + # Rails finds namespaces similar to thor, it only adds one rule: + # + # Generators names must end with "_generator.rb". This is required because Rails + # looks in load paths and loads the generator just before it's going to be used. + # + # find_by_namespace :webrat, :rails, :integration + # + # Will search for the following generators: + # + # "rails:webrat", "webrat:integration", "webrat" + # + # Notice that "rails:generators:webrat" could be loaded as well, what + # Rails looks for is the first and last parts of the namespace. + def self.find_by_namespace(name, base=nil, context=nil) #:nodoc: + lookups = [] + lookups << "#{base}:#{name}" if base + lookups << "#{name}:#{context}" if context + + unless base || context + unless name.to_s.include?(?:) + lookups << "#{name}:#{name}" + lookups << "rails:#{name}" + end + lookups << "#{name}" + end + + lookup(lookups) + + namespaces = Hash[subclasses.map { |klass| [klass.namespace, klass] }] + + lookups.each do |namespace| + klass = namespaces[namespace] + return klass if klass + end + + invoke_fallbacks_for(name, base) || invoke_fallbacks_for(context, name) + end + + # Receives a namespace, arguments and the behavior to invoke the generator. + # It's used as the default entry point for generate, destroy and update + # commands. + def self.invoke(namespace, args=ARGV, config={}) + names = namespace.to_s.split(':') + if klass = find_by_namespace(names.pop, names.any? && names.join(':')) + args << "--help" if args.empty? && klass.arguments.any? { |a| a.required? } + klass.start(args, config) + else + options = sorted_groups.map(&:last).flatten + suggestions = options.sort_by {|suggested| levenshtein_distance(namespace.to_s, suggested) }.first(3) + msg = "Could not find generator '#{namespace}'. " + msg << "Maybe you meant #{ suggestions.map {|s| "'#{s}'"}.join(" or ") }\n" + msg << "Run `rails generate --help` for more options." + puts msg + end + end + + # Returns an array of generator namespaces that are hidden. + # Generator namespaces may be hidden for a variety of reasons. + # Some are aliased such as "rails:migration" and can be + # invoked with the shorter "migration", others are private to other generators + # such as "css:scaffold". + def self.hidden_namespaces + @hidden_namespaces ||= begin + orm = options[:rails][:orm] + test = options[:rails][:test_framework] + template = options[:rails][:template_engine] + css = options[:rails][:stylesheet_engine] + + [ + "rails", + "resource_route", + "#{orm}:migration", + "#{orm}:model", + "#{test}:controller", + "#{test}:helper", + "#{test}:integration", + "#{test}:mailer", + "#{test}:model", + "#{test}:scaffold", + "#{test}:view", + "#{template}:controller", + "#{template}:scaffold", + "#{template}:mailer", + "#{css}:scaffold", + "#{css}:assets", + "css:assets", + "css:scaffold" + ] + end + end + + class << self + def hide_namespaces(*namespaces) + hidden_namespaces.concat(namespaces) + end + alias hide_namespace hide_namespaces + end + + # Show help message with available generators. + def self.help(command = 'generate') + puts "Usage: rails #{command} GENERATOR [args] [options]" + puts + puts "General options:" + puts " -h, [--help] # Print generator's options and usage" + puts " -p, [--pretend] # Run but do not make any changes" + puts " -f, [--force] # Overwrite files that already exist" + puts " -s, [--skip] # Skip files that already exist" + puts " -q, [--quiet] # Suppress status output" + puts + puts "Please choose a generator below." + puts + + print_generators + end + + def self.public_namespaces + lookup! + subclasses.map { |k| k.namespace } + end + + def self.print_generators + sorted_groups.each { |b, n| print_list(b, n) } + end + + def self.sorted_groups + namespaces = public_namespaces + namespaces.sort! + groups = Hash.new { |h,k| h[k] = [] } + namespaces.each do |namespace| + base = namespace.split(':').first + groups[base] << namespace + end + rails = groups.delete("rails") + rails.map! { |n| n.sub(/^rails:/, '') } + rails.delete("app") + rails.delete("plugin") + + hidden_namespaces.each { |n| groups.delete(n.to_s) } + + [["rails", rails]] + groups.sort.to_a + end + + protected + + # This code is based directly on the Text gem implementation + # Returns a value representing the "cost" of transforming str1 into str2 + def self.levenshtein_distance str1, str2 + s = str1 + t = str2 + n = s.length + m = t.length + max = n/2 + + return m if (0 == n) + return n if (0 == m) + return n if (n - m).abs > max + + d = (0..m).to_a + x = nil + + str1.each_char.each_with_index do |char1,i| + e = i+1 + + str2.each_char.each_with_index do |char2,j| + cost = (char1 == char2) ? 0 : 1 + x = [ + d[j+1] + 1, # insertion + e + 1, # deletion + d[j] + cost # substitution + ].min + d[j] = e + e = x + end + + d[m] = x + end + + return x + end + + # Prints a list of generators. + def self.print_list(base, namespaces) #:nodoc: + namespaces = namespaces.reject do |n| + hidden_namespaces.include?(n) + end + + return if namespaces.empty? + puts "#{base.camelize}:" + + namespaces.each do |namespace| + puts(" #{namespace}") + end + + puts + end + + # Try fallbacks for the given base. + def self.invoke_fallbacks_for(name, base) #:nodoc: + return nil unless base && fallbacks[base.to_sym] + invoked_fallbacks = [] + + Array(fallbacks[base.to_sym]).each do |fallback| + next if invoked_fallbacks.include?(fallback) + invoked_fallbacks << fallback + + klass = find_by_namespace(name, fallback) + return klass if klass + end + + nil + end + + # Receives namespaces in an array and tries to find matching generators + # in the load path. + def self.lookup(namespaces) #:nodoc: + paths = namespaces_to_paths(namespaces) + + paths.each do |raw_path| + ["rails/generators", "generators"].each do |base| + path = "#{base}/#{raw_path}_generator" + + begin + require path + return + rescue LoadError => e + raise unless e.message =~ /#{Regexp.escape(path)}$/ + rescue Exception => e + warn "[WARNING] Could not load generator #{path.inspect}. Error: #{e.message}.\n#{e.backtrace.join("\n")}" + end + end + end + end + + # This will try to load any generator in the load path to show in help. + def self.lookup! #:nodoc: + $LOAD_PATH.each do |base| + Dir[File.join(base, "{rails/generators,generators}", "**", "*_generator.rb")].each do |path| + begin + path = path.sub("#{base}/", "") + require path + rescue Exception + # No problem + end + end + end + end + + # Convert namespaces to paths by replacing ":" for "/" and adding + # an extra lookup. For example, "rails:model" should be searched + # in both: "rails/model/model_generator" and "rails/model_generator". + def self.namespaces_to_paths(namespaces) #:nodoc: + paths = [] + namespaces.each do |namespace| + pieces = namespace.split(":") + paths << pieces.dup.push(pieces.last).join("/") + paths << pieces.join("/") + end + paths.uniq! + paths + end + end +end diff --git a/railties/lib/rails/generators/actions.rb b/railties/lib/rails/generators/actions.rb new file mode 100644 index 0000000000..4709914947 --- /dev/null +++ b/railties/lib/rails/generators/actions.rb @@ -0,0 +1,280 @@ +require 'open-uri' +require 'rbconfig' + +module Rails + module Generators + module Actions + def initialize(*) # :nodoc: + super + @in_group = nil + @after_bundle_callbacks = [] + end + + # Adds an entry into +Gemfile+ for the supplied gem. + # + # gem "rspec", group: :test + # gem "technoweenie-restful-authentication", lib: "restful-authentication", source: "http://gems.github.com/" + # gem "rails", "3.0", git: "git://github.com/rails/rails" + def gem(*args) + options = args.extract_options! + name, version = args + + # Set the message to be shown in logs. Uses the git repo if one is given, + # otherwise use name (version). + parts, message = [ quote(name) ], name + if version ||= options.delete(:version) + parts << quote(version) + message << " (#{version})" + end + message = options[:git] if options[:git] + + log :gemfile, message + + options.each do |option, value| + parts << "#{option}: #{quote(value)}" + end + + in_root do + str = "gem #{parts.join(", ")}" + str = " " + str if @in_group + str = "\n" + str + append_file "Gemfile", str, verbose: false + end + end + + # Wraps gem entries inside a group. + # + # gem_group :development, :test do + # gem "rspec-rails" + # end + def gem_group(*names, &block) + name = names.map(&:inspect).join(", ") + log :gemfile, "group #{name}" + + in_root do + append_file "Gemfile", "\ngroup #{name} do", force: true + + @in_group = true + instance_eval(&block) + @in_group = false + + append_file "Gemfile", "\nend\n", force: true + end + end + + # Add the given source to +Gemfile+ + # + # add_source "http://gems.github.com/" + def add_source(source, options={}) + log :source, source + + in_root do + prepend_file "Gemfile", "source #{quote(source)}\n", verbose: false + end + end + + # Adds a line inside the Application class for <tt>config/application.rb</tt>. + # + # If options <tt>:env</tt> is specified, the line is appended to the corresponding + # file in <tt>config/environments</tt>. + # + # environment do + # "config.autoload_paths += %W(#{config.root}/extras)" + # end + # + # environment(nil, env: "development") do + # "config.autoload_paths += %W(#{config.root}/extras)" + # end + def environment(data=nil, options={}, &block) + sentinel = /class [a-z_:]+ < Rails::Application/i + env_file_sentinel = /Rails\.application\.configure do/ + data = block.call if !data && block_given? + + in_root do + if options[:env].nil? + inject_into_file 'config/application.rb', "\n #{data}", after: sentinel, verbose: false + else + Array(options[:env]).each do |env| + inject_into_file "config/environments/#{env}.rb", "\n #{data}", after: env_file_sentinel, verbose: false + end + end + end + end + alias :application :environment + + # Run a command in git. + # + # git :init + # git add: "this.file that.rb" + # git add: "onefile.rb", rm: "badfile.cxx" + def git(commands={}) + if commands.is_a?(Symbol) + run "git #{commands}" + else + commands.each do |cmd, options| + run "git #{cmd} #{options}" + end + end + end + + # Create a new file in the <tt>vendor/</tt> directory. Code can be specified + # in a block or a data string can be given. + # + # vendor("sekrit.rb") do + # sekrit_salt = "#{Time.now}--#{3.years.ago}--#{rand}--" + # "salt = '#{sekrit_salt}'" + # end + # + # vendor("foreign.rb", "# Foreign code is fun") + def vendor(filename, data=nil, &block) + log :vendor, filename + create_file("vendor/#{filename}", data, verbose: false, &block) + end + + # Create a new file in the lib/ directory. Code can be specified + # in a block or a data string can be given. + # + # lib("crypto.rb") do + # "crypted_special_value = '#{rand}--#{Time.now}--#{rand(1337)}--'" + # end + # + # lib("foreign.rb", "# Foreign code is fun") + def lib(filename, data=nil, &block) + log :lib, filename + create_file("lib/#{filename}", data, verbose: false, &block) + end + + # Create a new +Rakefile+ with the provided code (either in a block or a string). + # + # rakefile("bootstrap.rake") do + # project = ask("What is the UNIX name of your project?") + # + # <<-TASK + # namespace :#{project} do + # task :bootstrap do + # puts "I like boots!" + # end + # end + # TASK + # end + # + # rakefile('seed.rake', 'puts "Planting seeds"') + def rakefile(filename, data=nil, &block) + log :rakefile, filename + create_file("lib/tasks/#{filename}", data, verbose: false, &block) + end + + # Create a new initializer with the provided code (either in a block or a string). + # + # initializer("globals.rb") do + # data = "" + # + # ['MY_WORK', 'ADMINS', 'BEST_COMPANY_EVAR'].each do |const| + # data << "#{const} = :entp\n" + # end + # + # data + # end + # + # initializer("api.rb", "API_KEY = '123456'") + def initializer(filename, data=nil, &block) + log :initializer, filename + create_file("config/initializers/#{filename}", data, verbose: false, &block) + end + + # Generate something using a generator from Rails or a plugin. + # The second parameter is the argument string that is passed to + # the generator or an Array that is joined. + # + # generate(:authenticated, "user session") + def generate(what, *args) + log :generate, what + argument = args.flat_map {|arg| arg.to_s }.join(" ") + + in_root { run_ruby_script("bin/rails generate #{what} #{argument}", verbose: false) } + end + + # Runs the supplied rake task + # + # rake("db:migrate") + # rake("db:migrate", env: "production") + # rake("gems:install", sudo: true) + def rake(command, options={}) + log :rake, command + env = options[:env] || ENV["RAILS_ENV"] || 'development' + sudo = options[:sudo] && RbConfig::CONFIG['host_os'] !~ /mswin|mingw/ ? 'sudo ' : '' + in_root { run("#{sudo}#{extify(:rake)} #{command} RAILS_ENV=#{env}", verbose: false) } + end + + # Just run the capify command in root + # + # capify! + def capify! + log :capify, "" + in_root { run("#{extify(:capify)} .", verbose: false) } + end + + # Make an entry in Rails routing file <tt>config/routes.rb</tt> + # + # route "root 'welcome#index'" + def route(routing_code) + log :route, routing_code + sentinel = /\.routes\.draw do\s*$/ + + in_root do + inject_into_file 'config/routes.rb', "\n #{routing_code}", { after: sentinel, verbose: false } + end + end + + # Reads the given file at the source root and prints it in the console. + # + # readme "README" + def readme(path) + log File.read(find_in_source_paths(path)) + end + + # Registers a callback to be executed after bundle and spring binstubs + # have run. + # + # after_bundle do + # git add: '.' + # end + def after_bundle(&block) + @after_bundle_callbacks << block + end + + protected + + # Define log for backwards compatibility. If just one argument is sent, + # invoke say, otherwise invoke say_status. Differently from say and + # similarly to say_status, this method respects the quiet? option given. + def log(*args) + if args.size == 1 + say args.first.to_s unless options.quiet? + else + args << (self.behavior == :invoke ? :green : :red) + say_status(*args) + end + end + + # Add an extension to the given name based on the platform. + def extify(name) + if RbConfig::CONFIG['host_os'] =~ /mswin|mingw/ + "#{name}.bat" + else + name + end + end + + # Surround string with single quotes if there is no quotes. + # Otherwise fall back to double quotes + def quote(str) + if str.include?("'") + str.inspect + else + "'#{str}'" + end + end + end + end +end diff --git a/railties/lib/rails/generators/actions/create_migration.rb b/railties/lib/rails/generators/actions/create_migration.rb new file mode 100644 index 0000000000..682092fdf2 --- /dev/null +++ b/railties/lib/rails/generators/actions/create_migration.rb @@ -0,0 +1,69 @@ +require 'thor/actions' + +module Rails + module Generators + module Actions + class CreateMigration < Thor::Actions::CreateFile + + def migration_dir + File.dirname(@destination) + end + + def migration_file_name + @base.migration_file_name + end + + def identical? + exists? && File.binread(existing_migration) == render + end + + def revoke! + say_destination = exists? ? relative_existing_migration : relative_destination + say_status :remove, :red, say_destination + return unless exists? + ::FileUtils.rm_rf(existing_migration) unless pretend? + existing_migration + end + + def relative_existing_migration + base.relative_to_original_destination_root(existing_migration) + end + + def existing_migration + @existing_migration ||= begin + @base.class.migration_exists?(migration_dir, migration_file_name) || + File.exist?(@destination) && @destination + end + end + alias :exists? :existing_migration + + protected + + def on_conflict_behavior(&block) + options = base.options.merge(config) + if identical? + say_status :identical, :blue, relative_existing_migration + elsif options[:force] + say_status :remove, :green, relative_existing_migration + say_status :create, :green + unless pretend? + ::FileUtils.rm_rf(existing_migration) + block.call + end + elsif options[:skip] + say_status :skip, :yellow + else + say_status :conflict, :red + raise Error, "Another migration is already named #{migration_file_name}: " + + "#{existing_migration}. Use --force to replace this migration " + + "or --skip to ignore conflicted file." + end + end + + def say_status(status, color, message = relative_destination) + base.shell.say_status(status, message, color) if config[:verbose] + end + end + end + end +end diff --git a/railties/lib/rails/generators/active_model.rb b/railties/lib/rails/generators/active_model.rb new file mode 100644 index 0000000000..6183944bb0 --- /dev/null +++ b/railties/lib/rails/generators/active_model.rb @@ -0,0 +1,78 @@ +module Rails + module Generators + # ActiveModel is a class to be implemented by each ORM to allow Rails to + # generate customized controller code. + # + # The API has the same methods as ActiveRecord, but each method returns a + # string that matches the ORM API. + # + # For example: + # + # ActiveRecord::Generators::ActiveModel.find(Foo, "params[:id]") + # # => "Foo.find(params[:id])" + # + # DataMapper::Generators::ActiveModel.find(Foo, "params[:id]") + # # => "Foo.get(params[:id])" + # + # On initialization, the ActiveModel accepts the instance name that will + # receive the calls: + # + # builder = ActiveRecord::Generators::ActiveModel.new "@foo" + # builder.save # => "@foo.save" + # + # The only exception in ActiveModel for ActiveRecord is the use of self.build + # instead of self.new. + # + class ActiveModel + attr_reader :name + + def initialize(name) + @name = name + end + + # GET index + def self.all(klass) + "#{klass}.all" + end + + # GET show + # GET edit + # PATCH/PUT update + # DELETE destroy + def self.find(klass, params=nil) + "#{klass}.find(#{params})" + end + + # GET new + # POST create + def self.build(klass, params=nil) + if params + "#{klass}.new(#{params})" + else + "#{klass}.new" + end + end + + # POST create + def save + "#{name}.save" + end + + # PATCH/PUT update + def update(params=nil) + "#{name}.update(#{params})" + end + + # POST create + # PATCH/PUT update + def errors + "#{name}.errors" + end + + # DELETE destroy + def destroy + "#{name}.destroy" + end + end + end +end diff --git a/railties/lib/rails/generators/app_base.rb b/railties/lib/rails/generators/app_base.rb new file mode 100644 index 0000000000..caaaae09e6 --- /dev/null +++ b/railties/lib/rails/generators/app_base.rb @@ -0,0 +1,368 @@ +require 'digest/md5' +require 'active_support/core_ext/string/strip' +require 'rails/version' unless defined?(Rails::VERSION) +require 'open-uri' +require 'uri' +require 'rails/generators' +require 'active_support/core_ext/array/extract_options' + +module Rails + module Generators + class AppBase < Base # :nodoc: + DATABASES = %w( mysql oracle postgresql sqlite3 frontbase ibm_db sqlserver ) + JDBC_DATABASES = %w( jdbcmysql jdbcsqlite3 jdbcpostgresql jdbc ) + DATABASES.concat(JDBC_DATABASES) + + attr_accessor :rails_template + add_shebang_option! + + argument :app_path, type: :string + + def self.strict_args_position + false + end + + def self.add_shared_options_for(name) + class_option :template, type: :string, aliases: '-m', + desc: "Path to some #{name} template (can be a filesystem path or URL)" + + class_option :skip_gemfile, type: :boolean, default: false, + desc: "Don't create a Gemfile" + + class_option :skip_bundle, type: :boolean, aliases: '-B', default: false, + desc: "Don't run bundle install" + + class_option :skip_git, type: :boolean, aliases: '-G', default: false, + desc: 'Skip .gitignore file' + + class_option :skip_keeps, type: :boolean, default: false, + desc: 'Skip source control .keep files' + + class_option :skip_active_record, type: :boolean, aliases: '-O', default: false, + desc: 'Skip Active Record files' + + class_option :skip_gems, type: :array, default: [], + desc: 'Skip the provided gems files' + + class_option :skip_action_view, type: :boolean, aliases: '-V', default: false, + desc: 'Skip Action View files' + + class_option :skip_sprockets, type: :boolean, aliases: '-S', default: false, + desc: 'Skip Sprockets files' + + class_option :skip_spring, type: :boolean, default: false, + desc: "Don't install Spring application preloader" + + class_option :database, type: :string, aliases: '-d', default: 'sqlite3', + desc: "Preconfigure for selected database (options: #{DATABASES.join('/')})" + + class_option :javascript, type: :string, aliases: '-j', default: 'jquery', + desc: 'Preconfigure for selected JavaScript library' + + class_option :skip_javascript, type: :boolean, aliases: '-J', default: false, + desc: 'Skip JavaScript files' + + class_option :dev, type: :boolean, default: false, + desc: "Setup the #{name} with Gemfile pointing to your Rails checkout" + + class_option :edge, type: :boolean, default: false, + desc: "Setup the #{name} with Gemfile pointing to Rails repository" + + class_option :skip_test_unit, type: :boolean, aliases: '-T', default: false, + desc: 'Skip Test::Unit files' + + class_option :rc, type: :string, default: false, + desc: "Path to file containing extra configuration options for rails command" + + class_option :no_rc, type: :boolean, default: false, + desc: 'Skip loading of extra configuration options from .railsrc file' + + class_option :help, type: :boolean, aliases: '-h', group: :rails, + desc: 'Show this help message and quit' + end + + def initialize(*args) + @gem_filter = lambda { |gem| !options[:skip_gems].include?(gem.name) } + @extra_entries = [] + super + convert_database_option_for_jruby + end + + protected + + def gemfile_entry(name, *args) + options = args.extract_options! + version = args.first + github = options[:github] + path = options[:path] + + if github + @extra_entries << GemfileEntry.github(name, github) + elsif path + @extra_entries << GemfileEntry.path(name, path) + else + @extra_entries << GemfileEntry.version(name, version) + end + self + end + + def gemfile_entries + [rails_gemfile_entry, + database_gemfile_entry, + assets_gemfile_entry, + javascript_gemfile_entry, + jbuilder_gemfile_entry, + sdoc_gemfile_entry, + psych_gemfile_entry, + @extra_entries].flatten.find_all(&@gem_filter) + end + + def add_gem_entry_filter + @gem_filter = lambda { |next_filter, entry| + yield(entry) && next_filter.call(entry) + }.curry[@gem_filter] + end + + def builder + @builder ||= begin + builder_class = get_builder_class + builder_class.send(:include, ActionMethods) + builder_class.new(self) + end + end + + def build(meth, *args) + builder.send(meth, *args) if builder.respond_to?(meth) + end + + def create_root + valid_const? + + empty_directory '.' + FileUtils.cd(destination_root) unless options[:pretend] + end + + def apply_rails_template + apply rails_template if rails_template + rescue Thor::Error, LoadError, Errno::ENOENT => e + raise Error, "The template [#{rails_template}] could not be loaded. Error: #{e}" + end + + def set_default_accessors! + self.destination_root = File.expand_path(app_path, destination_root) + self.rails_template = case options[:template] + when /^https?:\/\// + options[:template] + when String + File.expand_path(options[:template], Dir.pwd) + else + options[:template] + end + end + + def database_gemfile_entry + return [] if options[:skip_active_record] + GemfileEntry.version gem_for_database, nil, + "Use #{options[:database]} as the database for Active Record" + end + + def include_all_railties? + !options[:skip_active_record] && !options[:skip_action_view] && !options[:skip_test_unit] && !options[:skip_sprockets] + end + + def comment_if(value) + options[value] ? '# ' : '' + end + + def sqlite3? + !options[:skip_active_record] && options[:database] == 'sqlite3' + end + + class GemfileEntry < Struct.new(:name, :version, :comment, :options, :commented_out) + def initialize(name, version, comment, options = {}, commented_out = false) + super + end + + def self.github(name, github, comment = nil) + new(name, nil, comment, github: github) + end + + def self.version(name, version, comment = nil) + new(name, version, comment) + end + + def self.path(name, path, comment = nil) + new(name, nil, comment, path: path) + end + end + + def rails_gemfile_entry + if options.dev? + [GemfileEntry.path('rails', Rails::Generators::RAILS_DEV_PATH), + GemfileEntry.github('arel', 'rails/arel'), + GemfileEntry.github('rack', 'rack/rack'), + GemfileEntry.github('i18n', 'svenfuchs/i18n')] + elsif options.edge? + [GemfileEntry.github('rails', 'rails/rails'), + GemfileEntry.github('arel', 'rails/arel'), + GemfileEntry.github('rack', 'rack/rack'), + GemfileEntry.github('i18n', 'svenfuchs/i18n')] + else + [GemfileEntry.version('rails', + Rails::VERSION::STRING, + "Bundle edge Rails instead: gem 'rails', github: 'rails/rails'")] + end + end + + def gem_for_database + # %w( mysql oracle postgresql sqlite3 frontbase ibm_db sqlserver jdbcmysql jdbcsqlite3 jdbcpostgresql ) + case options[:database] + when "oracle" then "ruby-oci8" + when "postgresql" then "pg" + when "frontbase" then "ruby-frontbase" + when "mysql" then "mysql2" + when "sqlserver" then "activerecord-sqlserver-adapter" + when "jdbcmysql" then "activerecord-jdbcmysql-adapter" + when "jdbcsqlite3" then "activerecord-jdbcsqlite3-adapter" + when "jdbcpostgresql" then "activerecord-jdbcpostgresql-adapter" + when "jdbc" then "activerecord-jdbc-adapter" + else options[:database] + end + end + + def convert_database_option_for_jruby + if defined?(JRUBY_VERSION) + case options[:database] + when "oracle" then options[:database].replace "jdbc" + when "postgresql" then options[:database].replace "jdbcpostgresql" + when "mysql" then options[:database].replace "jdbcmysql" + when "sqlite3" then options[:database].replace "jdbcsqlite3" + end + end + end + + def assets_gemfile_entry + return [] if options[:skip_sprockets] + + gems = [] + if options.dev? || options.edge? + gems << GemfileEntry.github('sprockets-rails', 'rails/sprockets-rails', + 'Use edge version of sprockets-rails') + gems << GemfileEntry.github('sass-rails', 'rails/sass-rails', + 'Use SCSS for stylesheets') + else + gems << GemfileEntry.version('sass-rails', + '~> 4.0.3', + 'Use SCSS for stylesheets') + end + + gems << GemfileEntry.version('uglifier', + '>= 1.3.0', + 'Use Uglifier as compressor for JavaScript assets') + + gems + end + + def jbuilder_gemfile_entry + comment = 'Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder' + GemfileEntry.version('jbuilder', '~> 2.0', comment) + end + + def sdoc_gemfile_entry + comment = 'bundle exec rake doc:rails generates the API under doc/api.' + GemfileEntry.new('sdoc', '~> 0.4.0', comment, group: :doc) + end + + def coffee_gemfile_entry + comment = 'Use CoffeeScript for .js.coffee assets and views' + if options.dev? || options.edge? + GemfileEntry.github 'coffee-rails', 'rails/coffee-rails', comment + else + GemfileEntry.version 'coffee-rails', '~> 4.0.0', comment + end + end + + def javascript_gemfile_entry + if options[:skip_javascript] + [] + else + gems = [coffee_gemfile_entry, javascript_runtime_gemfile_entry] + gems << GemfileEntry.version("#{options[:javascript]}-rails", nil, + "Use #{options[:javascript]} as the JavaScript library") + + gems << GemfileEntry.version("turbolinks", nil, + "Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks") + gems + end + end + + def javascript_runtime_gemfile_entry + comment = 'See https://github.com/sstephenson/execjs#readme for more supported runtimes' + if defined?(JRUBY_VERSION) + GemfileEntry.version 'therubyrhino', nil, comment + else + GemfileEntry.new 'therubyracer', nil, comment, { platforms: :ruby }, true + end + end + + def psych_gemfile_entry + return [] unless defined?(Rubinius) + + comment = 'Use Psych as the YAML engine, instead of Syck, so serialized ' \ + 'data can be read safely from different rubies (see http://git.io/uuLVag)' + GemfileEntry.new('psych', '~> 2.0', comment, platforms: :rbx) + end + + def bundle_command(command) + say_status :run, "bundle #{command}" + + # We are going to shell out rather than invoking Bundler::CLI.new(command) + # because `rails new` loads the Thor gem and on the other hand bundler uses + # its own vendored Thor, which could be a different version. Running both + # things in the same process is a recipe for a night with paracetamol. + # + # We use backticks and #print here instead of vanilla #system because it + # is easier to silence stdout in the existing test suite this way. The + # end-user gets the bundler commands called anyway, so no big deal. + # + # We unset temporary bundler variables to load proper bundler and Gemfile. + # + # Thanks to James Tucker for the Gem tricks involved in this call. + _bundle_command = Gem.bin_path('bundler', 'bundle') + + require 'bundler' + Bundler.with_clean_env do + output = `"#{Gem.ruby}" "#{_bundle_command}" #{command}` + print output unless options[:quiet] + end + end + + def bundle_install? + !(options[:skip_gemfile] || options[:skip_bundle] || options[:pretend]) + end + + def spring_install? + !options[:skip_spring] && Process.respond_to?(:fork) + end + + def run_bundle + bundle_command('install') if bundle_install? + end + + def generate_spring_binstubs + if bundle_install? && spring_install? + bundle_command("exec spring binstub --all") + end + end + + def empty_directory_with_keep_file(destination, config = {}) + empty_directory(destination, config) + keep_file(destination) + end + + def keep_file(destination) + create_file("#{destination}/.keep") unless options[:skip_keeps] + end + end + end +end diff --git a/railties/lib/rails/generators/base.rb b/railties/lib/rails/generators/base.rb new file mode 100644 index 0000000000..9af6435f23 --- /dev/null +++ b/railties/lib/rails/generators/base.rb @@ -0,0 +1,379 @@ +begin + require 'thor/group' +rescue LoadError + puts "Thor is not available.\nIf you ran this command from a git checkout " \ + "of Rails, please make sure thor is installed,\nand run this command " \ + "as `ruby #{$0} #{(ARGV | ['--dev']).join(" ")}`" + exit +end + +module Rails + module Generators + class Error < Thor::Error # :nodoc: + end + + class Base < Thor::Group + include Thor::Actions + include Rails::Generators::Actions + + add_runtime_options! + strict_args_position! + + # Returns the source root for this generator using default_source_root as default. + def self.source_root(path=nil) + @_source_root = path if path + @_source_root ||= default_source_root + end + + # Tries to get the description from a USAGE file one folder above the source + # root otherwise uses a default description. + def self.desc(description=nil) + return super if description + + @desc ||= if usage_path + ERB.new(File.read(usage_path)).result(binding) + else + "Description:\n Create #{base_name.humanize.downcase} files for #{generator_name} generator." + end + end + + # Convenience method to get the namespace from the class name. It's the + # same as Thor default except that the Generator at the end of the class + # is removed. + def self.namespace(name=nil) + return super if name + @namespace ||= super.sub(/_generator$/, '').sub(/:generators:/, ':') + end + + # Convenience method to hide this generator from the available ones when + # running rails generator command. + def self.hide! + Rails::Generators.hide_namespace self.namespace + end + + # Invoke a generator based on the value supplied by the user to the + # given option named "name". A class option is created when this method + # is invoked and you can set a hash to customize it. + # + # ==== Examples + # + # module Rails::Generators + # class ControllerGenerator < Base + # hook_for :test_framework, aliases: "-t" + # end + # end + # + # The example above will create a test framework option and will invoke + # a generator based on the user supplied value. + # + # For example, if the user invoke the controller generator as: + # + # rails generate controller Account --test-framework=test_unit + # + # The controller generator will then try to invoke the following generators: + # + # "rails:test_unit", "test_unit:controller", "test_unit" + # + # Notice that "rails:generators:test_unit" could be loaded as well, what + # Rails looks for is the first and last parts of the namespace. This is what + # allows any test framework to hook into Rails as long as it provides any + # of the hooks above. + # + # ==== Options + # + # The first and last part used to find the generator to be invoked are + # guessed based on class invokes hook_for, as noticed in the example above. + # This can be customized with two options: :in and :as. + # + # Let's suppose you are creating a generator that needs to invoke the + # controller generator from test unit. Your first attempt is: + # + # class AwesomeGenerator < Rails::Generators::Base + # hook_for :test_framework + # end + # + # The lookup in this case for test_unit as input is: + # + # "test_unit:awesome", "test_unit" + # + # Which is not the desired lookup. You can change it by providing the + # :as option: + # + # class AwesomeGenerator < Rails::Generators::Base + # hook_for :test_framework, as: :controller + # end + # + # And now it will lookup at: + # + # "test_unit:controller", "test_unit" + # + # Similarly, if you want it to also lookup in the rails namespace, you just + # need to provide the :in value: + # + # class AwesomeGenerator < Rails::Generators::Base + # hook_for :test_framework, in: :rails, as: :controller + # end + # + # And the lookup is exactly the same as previously: + # + # "rails:test_unit", "test_unit:controller", "test_unit" + # + # ==== Switches + # + # All hooks come with switches for user interface. If you do not want + # to use any test framework, you can do: + # + # rails generate controller Account --skip-test-framework + # + # Or similarly: + # + # rails generate controller Account --no-test-framework + # + # ==== Boolean hooks + # + # In some cases, you may want to provide a boolean hook. For example, webrat + # developers might want to have webrat available on controller generator. + # This can be achieved as: + # + # Rails::Generators::ControllerGenerator.hook_for :webrat, type: :boolean + # + # Then, if you want webrat to be invoked, just supply: + # + # rails generate controller Account --webrat + # + # The hooks lookup is similar as above: + # + # "rails:generators:webrat", "webrat:generators:controller", "webrat" + # + # ==== Custom invocations + # + # You can also supply a block to hook_for to customize how the hook is + # going to be invoked. The block receives two arguments, an instance + # of the current class and the class to be invoked. + # + # For example, in the resource generator, the controller should be invoked + # with a pluralized class name. But by default it is invoked with the same + # name as the resource generator, which is singular. To change this, we + # can give a block to customize how the controller can be invoked. + # + # hook_for :resource_controller do |instance, controller| + # instance.invoke controller, [ instance.name.pluralize ] + # end + # + def self.hook_for(*names, &block) + options = names.extract_options! + in_base = options.delete(:in) || base_name + as_hook = options.delete(:as) || generator_name + + names.each do |name| + unless class_options.key?(name) + defaults = if options[:type] == :boolean + { } + elsif [true, false].include?(default_value_for_option(name, options)) + { banner: "" } + else + { desc: "#{name.to_s.humanize} to be invoked", banner: "NAME" } + end + + class_option(name, defaults.merge!(options)) + end + + hooks[name] = [ in_base, as_hook ] + invoke_from_option(name, options, &block) + end + end + + # Remove a previously added hook. + # + # remove_hook_for :orm + def self.remove_hook_for(*names) + remove_invocation(*names) + + names.each do |name| + hooks.delete(name) + end + end + + # Make class option aware of Rails::Generators.options and Rails::Generators.aliases. + def self.class_option(name, options={}) #:nodoc: + options[:desc] = "Indicates when to generate #{name.to_s.humanize.downcase}" unless options.key?(:desc) + options[:aliases] = default_aliases_for_option(name, options) + options[:default] = default_value_for_option(name, options) + super(name, options) + end + + # Returns the default source root for a given generator. This is used internally + # by rails to set its generators source root. If you want to customize your source + # root, you should use source_root. + def self.default_source_root + return unless base_name && generator_name + return unless default_generator_root + path = File.join(default_generator_root, 'templates') + path if File.exist?(path) + end + + # Returns the base root for a common set of generators. This is used to dynamically + # guess the default source root. + def self.base_root + File.dirname(__FILE__) + end + + # Cache source root and add lib/generators/base/generator/templates to + # source paths. + def self.inherited(base) #:nodoc: + super + + # Invoke source_root so the default_source_root is set. + base.source_root + + if base.name && base.name !~ /Base$/ + Rails::Generators.subclasses << base + + Rails::Generators.templates_path.each do |path| + if base.name.include?('::') + base.source_paths << File.join(path, base.base_name, base.generator_name) + else + base.source_paths << File.join(path, base.generator_name) + end + end + end + end + + protected + + # Check whether the given class names are already taken by user + # application or Ruby on Rails. + def class_collisions(*class_names) #:nodoc: + return unless behavior == :invoke + + class_names.flatten.each do |class_name| + class_name = class_name.to_s + next if class_name.strip.empty? + + # Split the class from its module nesting + nesting = class_name.split('::') + last_name = nesting.pop + last = extract_last_module(nesting) + + if last && last.const_defined?(last_name.camelize, false) + raise Error, "The name '#{class_name}' is either already used in your application " << + "or reserved by Ruby on Rails. Please choose an alternative and run " << + "this generator again." + end + end + end + + # Takes in an array of nested modules and extracts the last module + def extract_last_module(nesting) + nesting.inject(Object) do |last_module, nest| + break unless last_module.const_defined?(nest, false) + last_module.const_get(nest) + end + end + + # Use Rails default banner. + def self.banner + "rails generate #{namespace.sub(/^rails:/,'')} #{self.arguments.map{ |a| a.usage }.join(' ')} [options]".gsub(/\s+/, ' ') + end + + # Sets the base_name taking into account the current class namespace. + def self.base_name + @base_name ||= begin + if base = name.to_s.split('::').first + base.underscore + end + end + end + + # Removes the namespaces and get the generator name. For example, + # Rails::Generators::ModelGenerator will return "model" as generator name. + def self.generator_name + @generator_name ||= begin + if generator = name.to_s.split('::').last + generator.sub!(/Generator$/, '') + generator.underscore + end + end + end + + # Returns the default value for the option name given doing a lookup in + # Rails::Generators.options. + def self.default_value_for_option(name, options) + default_for_option(Rails::Generators.options, name, options, options[:default]) + end + + # Return default aliases for the option name given doing a lookup in + # Rails::Generators.aliases. + def self.default_aliases_for_option(name, options) + default_for_option(Rails::Generators.aliases, name, options, options[:aliases]) + end + + # Return default for the option name given doing a lookup in config. + def self.default_for_option(config, name, options, default) + if generator_name and c = config[generator_name.to_sym] and c.key?(name) + c[name] + elsif base_name and c = config[base_name.to_sym] and c.key?(name) + c[name] + elsif config[:rails].key?(name) + config[:rails][name] + else + default + end + end + + # Keep hooks configuration that are used on prepare_for_invocation. + def self.hooks #:nodoc: + @hooks ||= from_superclass(:hooks, {}) + end + + # Prepare class invocation to search on Rails namespace if a previous + # added hook is being used. + def self.prepare_for_invocation(name, value) #:nodoc: + return super unless value.is_a?(String) || value.is_a?(Symbol) + + if value && constants = self.hooks[name] + value = name if TrueClass === value + Rails::Generators.find_by_namespace(value, *constants) + elsif klass = Rails::Generators.find_by_namespace(value) + klass + else + super + end + end + + # Small macro to add ruby as an option to the generator with proper + # default value plus an instance helper method called shebang. + def self.add_shebang_option! + class_option :ruby, type: :string, aliases: "-r", default: Thor::Util.ruby_command, + desc: "Path to the Ruby binary of your choice", banner: "PATH" + + no_tasks { + define_method :shebang do + @shebang ||= begin + command = if options[:ruby] == Thor::Util.ruby_command + "/usr/bin/env #{File.basename(Thor::Util.ruby_command)}" + else + options[:ruby] + end + "#!#{command}" + end + end + } + end + + def self.usage_path + paths = [ + source_root && File.expand_path("../USAGE", source_root), + default_generator_root && File.join(default_generator_root, "USAGE") + ] + paths.compact.detect { |path| File.exist? path } + end + + def self.default_generator_root + path = File.expand_path(File.join(base_name, generator_name), base_root) + path if File.exist?(path) + end + + end + end +end diff --git a/railties/lib/rails/generators/css/assets/assets_generator.rb b/railties/lib/rails/generators/css/assets/assets_generator.rb new file mode 100644 index 0000000000..e4a305f4b3 --- /dev/null +++ b/railties/lib/rails/generators/css/assets/assets_generator.rb @@ -0,0 +1,13 @@ +require "rails/generators/named_base" + +module Css # :nodoc: + module Generators # :nodoc: + class AssetsGenerator < Rails::Generators::NamedBase # :nodoc: + source_root File.expand_path("../templates", __FILE__) + + def copy_stylesheet + copy_file "stylesheet.css", File.join('app/assets/stylesheets', class_path, "#{file_name}.css") + end + end + end +end diff --git a/railties/lib/rails/generators/css/assets/templates/stylesheet.css b/railties/lib/rails/generators/css/assets/templates/stylesheet.css new file mode 100644 index 0000000000..afad32db02 --- /dev/null +++ b/railties/lib/rails/generators/css/assets/templates/stylesheet.css @@ -0,0 +1,4 @@ +/* + Place all the styles related to the matching controller here. + They will automatically be included in application.css. +*/ diff --git a/railties/lib/rails/generators/css/scaffold/scaffold_generator.rb b/railties/lib/rails/generators/css/scaffold/scaffold_generator.rb new file mode 100644 index 0000000000..cf534030f9 --- /dev/null +++ b/railties/lib/rails/generators/css/scaffold/scaffold_generator.rb @@ -0,0 +1,16 @@ +require "rails/generators/named_base" + +module Css # :nodoc: + module Generators # :nodoc: + class ScaffoldGenerator < Rails::Generators::NamedBase # :nodoc: + # In order to allow the Sass generators to pick up the default Rails CSS and + # transform it, we leave it in a standard location for the CSS stylesheet + # generators to handle. For the simple, default case, just copy it over. + def copy_stylesheet + dir = Rails::Generators::ScaffoldGenerator.source_root + file = File.join(dir, "scaffold.css") + create_file "app/assets/stylesheets/scaffold.css", File.read(file) + end + end + end +end diff --git a/railties/lib/rails/generators/erb.rb b/railties/lib/rails/generators/erb.rb new file mode 100644 index 0000000000..0755ac335c --- /dev/null +++ b/railties/lib/rails/generators/erb.rb @@ -0,0 +1,25 @@ +require 'rails/generators/named_base' + +module Erb # :nodoc: + module Generators # :nodoc: + class Base < Rails::Generators::NamedBase #:nodoc: + protected + + def formats + [format] + end + + def format + :html + end + + def handler + :erb + end + + def filename_with_extensions(name, format = self.format) + [name, format, handler].compact.join(".") + end + end + end +end diff --git a/railties/lib/rails/generators/erb/controller/controller_generator.rb b/railties/lib/rails/generators/erb/controller/controller_generator.rb new file mode 100644 index 0000000000..94c1b835d1 --- /dev/null +++ b/railties/lib/rails/generators/erb/controller/controller_generator.rb @@ -0,0 +1,22 @@ +require 'rails/generators/erb' + +module Erb # :nodoc: + module Generators # :nodoc: + class ControllerGenerator < Base # :nodoc: + argument :actions, type: :array, default: [], banner: "action action" + + def copy_view_files + base_path = File.join("app/views", class_path, file_name) + empty_directory base_path + + actions.each do |action| + @action = action + formats.each do |format| + @path = File.join(base_path, filename_with_extensions(action, format)) + template filename_with_extensions(:view, format), @path + end + end + end + end + end +end diff --git a/railties/lib/rails/generators/erb/controller/templates/view.html.erb b/railties/lib/rails/generators/erb/controller/templates/view.html.erb new file mode 100644 index 0000000000..cd54d13d83 --- /dev/null +++ b/railties/lib/rails/generators/erb/controller/templates/view.html.erb @@ -0,0 +1,2 @@ +<h1><%= class_name %>#<%= @action %></h1> +<p>Find me in <%= @path %></p> diff --git a/railties/lib/rails/generators/erb/mailer/mailer_generator.rb b/railties/lib/rails/generators/erb/mailer/mailer_generator.rb new file mode 100644 index 0000000000..66b17bd10e --- /dev/null +++ b/railties/lib/rails/generators/erb/mailer/mailer_generator.rb @@ -0,0 +1,13 @@ +require 'rails/generators/erb/controller/controller_generator' + +module Erb # :nodoc: + module Generators # :nodoc: + class MailerGenerator < ControllerGenerator # :nodoc: + protected + + def formats + [:text, :html] + end + end + end +end diff --git a/railties/lib/rails/generators/erb/mailer/templates/view.html.erb b/railties/lib/rails/generators/erb/mailer/templates/view.html.erb new file mode 100644 index 0000000000..b5045671b3 --- /dev/null +++ b/railties/lib/rails/generators/erb/mailer/templates/view.html.erb @@ -0,0 +1,5 @@ +<h1><%= class_name %>#<%= @action %></h1> + +<p> + <%%= @greeting %>, find me in <%= @path %> +</p> diff --git a/railties/lib/rails/generators/erb/mailer/templates/view.text.erb b/railties/lib/rails/generators/erb/mailer/templates/view.text.erb new file mode 100644 index 0000000000..342285df19 --- /dev/null +++ b/railties/lib/rails/generators/erb/mailer/templates/view.text.erb @@ -0,0 +1,3 @@ +<%= class_name %>#<%= @action %> + +<%%= @greeting %>, find me in <%= @path %> diff --git a/railties/lib/rails/generators/erb/scaffold/scaffold_generator.rb b/railties/lib/rails/generators/erb/scaffold/scaffold_generator.rb new file mode 100644 index 0000000000..c94829a0ae --- /dev/null +++ b/railties/lib/rails/generators/erb/scaffold/scaffold_generator.rb @@ -0,0 +1,31 @@ +require 'rails/generators/erb' +require 'rails/generators/resource_helpers' + +module Erb # :nodoc: + module Generators # :nodoc: + class ScaffoldGenerator < Base # :nodoc: + include Rails::Generators::ResourceHelpers + + argument :attributes, type: :array, default: [], banner: "field:type field:type" + + def create_root_folder + empty_directory File.join("app/views", controller_file_path) + end + + def copy_view_files + available_views.each do |view| + formats.each do |format| + filename = filename_with_extensions(view, format) + template filename, File.join("app/views", controller_file_path, filename) + end + end + end + + protected + + def available_views + %w(index edit show new _form) + end + end + end +end diff --git a/railties/lib/rails/generators/erb/scaffold/templates/_form.html.erb b/railties/lib/rails/generators/erb/scaffold/templates/_form.html.erb new file mode 100644 index 0000000000..bba9141fb8 --- /dev/null +++ b/railties/lib/rails/generators/erb/scaffold/templates/_form.html.erb @@ -0,0 +1,32 @@ +<%%= form_for(@<%= singular_table_name %>) do |f| %> + <%% if @<%= singular_table_name %>.errors.any? %> + <div id="error_explanation"> + <h2><%%= pluralize(@<%= singular_table_name %>.errors.count, "error") %> prohibited this <%= singular_table_name %> from being saved:</h2> + + <ul> + <%% @<%= singular_table_name %>.errors.full_messages.each do |message| %> + <li><%%= message %></li> + <%% end %> + </ul> + </div> + <%% end %> + +<% attributes.each do |attribute| -%> + <div class="field"> +<% if attribute.password_digest? -%> + <%%= f.label :password %><br> + <%%= f.password_field :password %> + </div> + <div class="field"> + <%%= f.label :password_confirmation %><br> + <%%= f.password_field :password_confirmation %> +<% else -%> + <%%= f.label :<%= attribute.column_name %> %><br> + <%%= f.<%= attribute.field_type %> :<%= attribute.column_name %> %> +<% end -%> + </div> +<% end -%> + <div class="actions"> + <%%= f.submit %> + </div> +<%% end %> diff --git a/railties/lib/rails/generators/erb/scaffold/templates/edit.html.erb b/railties/lib/rails/generators/erb/scaffold/templates/edit.html.erb new file mode 100644 index 0000000000..5620fcc850 --- /dev/null +++ b/railties/lib/rails/generators/erb/scaffold/templates/edit.html.erb @@ -0,0 +1,6 @@ +<h1>Editing <%= singular_table_name.titleize %></h1> + +<%%= render 'form' %> + +<%%= link_to 'Show', @<%= singular_table_name %> %> | +<%%= link_to 'Back', <%= index_helper %>_path %> diff --git a/railties/lib/rails/generators/erb/scaffold/templates/index.html.erb b/railties/lib/rails/generators/erb/scaffold/templates/index.html.erb new file mode 100644 index 0000000000..5e194783ff --- /dev/null +++ b/railties/lib/rails/generators/erb/scaffold/templates/index.html.erb @@ -0,0 +1,31 @@ +<p id="notice"><%%= notice %></p> + +<h1>Listing <%= plural_table_name.titleize %></h1> + +<table> + <thead> + <tr> +<% attributes.reject(&:password_digest?).each do |attribute| -%> + <th><%= attribute.human_name %></th> +<% end -%> + <th colspan="3"></th> + </tr> + </thead> + + <tbody> + <%% @<%= plural_table_name %>.each do |<%= singular_table_name %>| %> + <tr> +<% attributes.reject(&:password_digest?).each do |attribute| -%> + <td><%%= <%= singular_table_name %>.<%= attribute.name %> %></td> +<% end -%> + <td><%%= link_to 'Show', <%= singular_table_name %> %></td> + <td><%%= link_to 'Edit', edit_<%= singular_table_name %>_path(<%= singular_table_name %>) %></td> + <td><%%= link_to 'Destroy', <%= singular_table_name %>, method: :delete, data: { confirm: 'Are you sure?' } %></td> + </tr> + <%% end %> + </tbody> +</table> + +<br> + +<%%= link_to 'New <%= human_name %>', new_<%= singular_table_name %>_path %> diff --git a/railties/lib/rails/generators/erb/scaffold/templates/new.html.erb b/railties/lib/rails/generators/erb/scaffold/templates/new.html.erb new file mode 100644 index 0000000000..db13a5d870 --- /dev/null +++ b/railties/lib/rails/generators/erb/scaffold/templates/new.html.erb @@ -0,0 +1,5 @@ +<h1>New <%= singular_table_name.titleize %></h1> + +<%%= render 'form' %> + +<%%= link_to 'Back', <%= index_helper %>_path %> diff --git a/railties/lib/rails/generators/erb/scaffold/templates/show.html.erb b/railties/lib/rails/generators/erb/scaffold/templates/show.html.erb new file mode 100644 index 0000000000..5e634153be --- /dev/null +++ b/railties/lib/rails/generators/erb/scaffold/templates/show.html.erb @@ -0,0 +1,11 @@ +<p id="notice"><%%= notice %></p> + +<% attributes.reject(&:password_digest?).each do |attribute| -%> +<p> + <strong><%= attribute.human_name %>:</strong> + <%%= @<%= singular_table_name %>.<%= attribute.name %> %> +</p> + +<% end -%> +<%%= link_to 'Edit', edit_<%= singular_table_name %>_path(@<%= singular_table_name %>) %> | +<%%= link_to 'Back', <%= index_helper %>_path %> diff --git a/railties/lib/rails/generators/generated_attribute.rb b/railties/lib/rails/generators/generated_attribute.rb new file mode 100644 index 0000000000..c5326d70d1 --- /dev/null +++ b/railties/lib/rails/generators/generated_attribute.rb @@ -0,0 +1,150 @@ +require 'active_support/time' + +module Rails + module Generators + class GeneratedAttribute # :nodoc: + INDEX_OPTIONS = %w(index uniq) + UNIQ_INDEX_OPTIONS = %w(uniq) + + attr_accessor :name, :type + attr_reader :attr_options + attr_writer :index_name + + class << self + def parse(column_definition) + name, type, has_index = column_definition.split(':') + + # if user provided "name:index" instead of "name:string:index" + # type should be set blank so GeneratedAttribute's constructor + # could set it to :string + has_index, type = type, nil if INDEX_OPTIONS.include?(type) + + type, attr_options = *parse_type_and_options(type) + type = type.to_sym if type + + if type && reference?(type) + references_index = UNIQ_INDEX_OPTIONS.include?(has_index) ? { unique: true } : true + attr_options[:index] = references_index + end + + new(name, type, has_index, attr_options) + end + + def reference?(type) + [:references, :belongs_to].include? type + end + + private + + # parse possible attribute options like :limit for string/text/binary/integer, :precision/:scale for decimals or :polymorphic for references/belongs_to + # when declaring options curly brackets should be used + def parse_type_and_options(type) + case type + when /(string|text|binary|integer)\{(\d+)\}/ + return $1, limit: $2.to_i + when /decimal\{(\d+)[,.-](\d+)\}/ + return :decimal, precision: $1.to_i, scale: $2.to_i + when /(references|belongs_to)\{polymorphic\}/ + return $1, polymorphic: true + else + return type, {} + end + end + end + + def initialize(name, type=nil, index_type=false, attr_options={}) + @name = name + @type = type || :string + @has_index = INDEX_OPTIONS.include?(index_type) + @has_uniq_index = UNIQ_INDEX_OPTIONS.include?(index_type) + @attr_options = attr_options + end + + def field_type + @field_type ||= case type + when :integer then :number_field + when :float, :decimal then :text_field + when :time then :time_select + when :datetime, :timestamp then :datetime_select + when :date then :date_select + when :text then :text_area + when :boolean then :check_box + else + :text_field + end + end + + def default + @default ||= case type + when :integer then 1 + when :float then 1.5 + when :decimal then "9.99" + when :datetime, :timestamp, :time then Time.now.to_s(:db) + when :date then Date.today.to_s(:db) + when :string then name == "type" ? "" : "MyString" + when :text then "MyText" + when :boolean then false + when :references, :belongs_to then nil + else + "" + end + end + + def plural_name + name.sub(/_id$/, '').pluralize + end + + def singular_name + name.sub(/_id$/, '').singularize + end + + def human_name + name.humanize + end + + def index_name + @index_name ||= if polymorphic? + %w(id type).map { |t| "#{name}_#{t}" } + else + column_name + end + end + + def column_name + @column_name ||= reference? ? "#{name}_id" : name + end + + def foreign_key? + !!(name =~ /_id$/) + end + + def reference? + self.class.reference?(type) + end + + def polymorphic? + self.attr_options.has_key?(:polymorphic) + end + + def has_index? + @has_index + end + + def has_uniq_index? + @has_uniq_index + end + + def password_digest? + name == 'password' && type == :digest + end + + def inject_options + "".tap { |s| @attr_options.each { |k,v| s << ", #{k}: #{v.inspect}" } } + end + + def inject_index_options + has_uniq_index? ? ", unique: true" : "" + end + end + end +end diff --git a/railties/lib/rails/generators/js/assets/assets_generator.rb b/railties/lib/rails/generators/js/assets/assets_generator.rb new file mode 100644 index 0000000000..1e925b2cd2 --- /dev/null +++ b/railties/lib/rails/generators/js/assets/assets_generator.rb @@ -0,0 +1,13 @@ +require "rails/generators/named_base" + +module Js # :nodoc: + module Generators # :nodoc: + class AssetsGenerator < Rails::Generators::NamedBase # :nodoc: + source_root File.expand_path("../templates", __FILE__) + + def copy_javascript + copy_file "javascript.js", File.join('app/assets/javascripts', class_path, "#{file_name}.js") + end + end + end +end diff --git a/railties/lib/rails/generators/js/assets/templates/javascript.js b/railties/lib/rails/generators/js/assets/templates/javascript.js new file mode 100644 index 0000000000..dee720facd --- /dev/null +++ b/railties/lib/rails/generators/js/assets/templates/javascript.js @@ -0,0 +1,2 @@ +// Place all the behaviors and hooks related to the matching controller here. +// All this logic will automatically be available in application.js. diff --git a/railties/lib/rails/generators/migration.rb b/railties/lib/rails/generators/migration.rb new file mode 100644 index 0000000000..cd388e590a --- /dev/null +++ b/railties/lib/rails/generators/migration.rb @@ -0,0 +1,69 @@ +require 'active_support/concern' +require 'rails/generators/actions/create_migration' + +module Rails + module Generators + # Holds common methods for migrations. It assumes that migrations has the + # [0-9]*_name format and can be used by another frameworks (like Sequel) + # just by implementing the next migration version method. + module Migration + extend ActiveSupport::Concern + attr_reader :migration_number, :migration_file_name, :migration_class_name + + module ClassMethods + def migration_lookup_at(dirname) #:nodoc: + Dir.glob("#{dirname}/[0-9]*_*.rb") + end + + def migration_exists?(dirname, file_name) #:nodoc: + migration_lookup_at(dirname).grep(/\d+_#{file_name}.rb$/).first + end + + def current_migration_number(dirname) #:nodoc: + migration_lookup_at(dirname).collect do |file| + File.basename(file).split("_").first.to_i + end.max.to_i + end + + def next_migration_number(dirname) #:nodoc: + raise NotImplementedError + end + end + + def create_migration(destination, data, config = {}, &block) + action Rails::Generators::Actions::CreateMigration.new(self, destination, block || data.to_s, config) + end + + def set_migration_assigns!(destination) + destination = File.expand_path(destination, self.destination_root) + + migration_dir = File.dirname(destination) + @migration_number = self.class.next_migration_number(migration_dir) + @migration_file_name = File.basename(destination, '.rb') + @migration_class_name = @migration_file_name.camelize + end + + # Creates a migration template at the given destination. The difference + # to the default template method is that the migration version is appended + # to the destination file name. + # + # The migration version, migration file name, migration class name are + # available as instance variables in the template to be rendered. + # + # migration_template "migration.rb", "db/migrate/add_foo_to_bar.rb" + def migration_template(source, destination, config = {}) + source = File.expand_path(find_in_source_paths(source.to_s)) + + set_migration_assigns!(destination) + context = instance_eval('binding') + + dir, base = File.split(destination) + numbered_destination = File.join(dir, ["%migration_number%", base].join('_')) + + create_migration numbered_destination, nil, config do + ERB.new(::File.binread(source), nil, '-', '@output_buffer').result(context) + end + end + end + end +end diff --git a/railties/lib/rails/generators/model_helpers.rb b/railties/lib/rails/generators/model_helpers.rb new file mode 100644 index 0000000000..42c646543e --- /dev/null +++ b/railties/lib/rails/generators/model_helpers.rb @@ -0,0 +1,28 @@ +require 'rails/generators/active_model' + +module Rails + module Generators + module ModelHelpers # :nodoc: + PLURAL_MODEL_NAME_WARN_MESSAGE = "[WARNING] The model name '%s' was recognized as a plural, using the singular '%s' instead. " \ + "Override with --force-plural or setup custom inflection rules for this noun before running the generator." + mattr_accessor :skip_warn + + def self.included(base) #:nodoc: + base.class_option :force_plural, type: :boolean, default: false, desc: 'Forces the use of the given model name' + end + + def initialize(args, *_options) + super + if name == name.pluralize && name.singularize != name.pluralize && !options[:force_plural] + singular = name.singularize + unless ModelHelpers.skip_warn + say PLURAL_MODEL_NAME_WARN_MESSAGE % [name, singular] + ModelHelpers.skip_warn = true + end + name.replace singular + assign_names!(name) + end + end + end + end +end diff --git a/railties/lib/rails/generators/named_base.rb b/railties/lib/rails/generators/named_base.rb new file mode 100644 index 0000000000..b7da44ca2d --- /dev/null +++ b/railties/lib/rails/generators/named_base.rb @@ -0,0 +1,205 @@ +require 'active_support/core_ext/module/introspection' +require 'rails/generators/base' +require 'rails/generators/generated_attribute' + +module Rails + module Generators + class NamedBase < Base + argument :name, type: :string + class_option :skip_namespace, type: :boolean, default: false, + desc: "Skip namespace (affects only isolated applications)" + + def initialize(args, *options) #:nodoc: + @inside_template = nil + # Unfreeze name in case it's given as a frozen string + args[0] = args[0].dup if args[0].is_a?(String) && args[0].frozen? + super + assign_names!(self.name) + parse_attributes! if respond_to?(:attributes) + end + + # Defines the template that would be used for the migration file. + # The arguments include the source template file, the migration filename etc. + no_tasks do + def template(source, *args, &block) + inside_template do + super + end + end + end + + protected + attr_reader :file_name + + # FIXME: We are avoiding to use alias because a bug on thor that make + # this method public and add it to the task list. + def singular_name + file_name + end + + # Wrap block with namespace of current application + # if namespace exists and is not skipped + def module_namespacing(&block) + content = capture(&block) + content = wrap_with_namespace(content) if namespaced? + concat(content) + end + + def indent(content, multiplier = 2) + spaces = " " * multiplier + content.each_line.map {|line| line.blank? ? line : "#{spaces}#{line}" }.join + end + + def wrap_with_namespace(content) + content = indent(content).chomp + "module #{namespace.name}\n#{content}\nend\n" + end + + def inside_template + @inside_template = true + yield + ensure + @inside_template = false + end + + def inside_template? + @inside_template + end + + def namespace + Rails::Generators.namespace + end + + def namespaced? + !options[:skip_namespace] && namespace + end + + def file_path + @file_path ||= (class_path + [file_name]).join('/') + end + + def class_path + inside_template? || !namespaced? ? regular_class_path : namespaced_class_path + end + + def regular_class_path + @class_path + end + + def namespaced_file_path + @namespaced_file_path ||= namespaced_class_path.join("/") + end + + def namespaced_class_path + @namespaced_class_path ||= [namespaced_path] + @class_path + end + + def namespaced_path + @namespaced_path ||= namespace.name.split("::").first.underscore + end + + def class_name + (class_path + [file_name]).map!{ |m| m.camelize }.join('::') + end + + def human_name + @human_name ||= singular_name.humanize + end + + def plural_name + @plural_name ||= singular_name.pluralize + end + + def i18n_scope + @i18n_scope ||= file_path.tr('/', '.') + end + + def table_name + @table_name ||= begin + base = pluralize_table_names? ? plural_name : singular_name + (class_path + [base]).join('_') + end + end + + def uncountable? + singular_name == plural_name + end + + def index_helper + uncountable? ? "#{plural_table_name}_index" : plural_table_name + end + + def singular_table_name + @singular_table_name ||= (pluralize_table_names? ? table_name.singularize : table_name) + end + + def plural_table_name + @plural_table_name ||= (pluralize_table_names? ? table_name : table_name.pluralize) + end + + def plural_file_name + @plural_file_name ||= file_name.pluralize + end + + def route_url + @route_url ||= class_path.collect {|dname| "/" + dname }.join + "/" + plural_file_name + end + + # Tries to retrieve the application name or simple return application. + def application_name + if defined?(Rails) && Rails.application + Rails.application.class.name.split('::').first.underscore + else + "application" + end + end + + def assign_names!(name) #:nodoc: + @class_path = name.include?('/') ? name.split('/') : name.split('::') + @class_path.map! { |m| m.underscore } + @file_name = @class_path.pop + end + + # Convert attributes array into GeneratedAttribute objects. + def parse_attributes! #:nodoc: + self.attributes = (attributes || []).map do |attr| + Rails::Generators::GeneratedAttribute.parse(attr) + end + end + + def attributes_names + @attributes_names ||= attributes.each_with_object([]) do |a, names| + names << a.column_name + names << 'password_confirmation' if a.password_digest? + names << "#{a.name}_type" if a.polymorphic? + end + end + + def pluralize_table_names? + !defined?(ActiveRecord::Base) || ActiveRecord::Base.pluralize_table_names + end + + # Add a class collisions name to be checked on class initialization. You + # can supply a hash with a :prefix or :suffix to be tested. + # + # ==== Examples + # + # check_class_collision suffix: "Decorator" + # + # If the generator is invoked with class name Admin, it will check for + # the presence of "AdminDecorator". + # + def self.check_class_collision(options={}) + define_method :check_class_collision do + name = if self.respond_to?(:controller_class_name) # for ScaffoldBase + controller_class_name + else + class_name + end + + class_collisions "#{options[:prefix]}#{name}#{options[:suffix]}" + end + end + end + end +end diff --git a/railties/lib/rails/generators/rails/app/USAGE b/railties/lib/rails/generators/rails/app/USAGE new file mode 100644 index 0000000000..691095f33f --- /dev/null +++ b/railties/lib/rails/generators/rails/app/USAGE @@ -0,0 +1,15 @@ +Description: + The 'rails new' command creates a new Rails application with a default + directory structure and configuration at the path you specify. + + You can specify extra command-line arguments to be used every time + 'rails new' runs in the .railsrc configuration file in your home directory. + + Note that the arguments specified in the .railsrc file don't affect the + defaults values shown above in this help message. + +Example: + rails new ~/Code/Ruby/weblog + + This generates a skeletal Rails installation in ~/Code/Ruby/weblog. + See the README in the newly created application to get going. diff --git a/railties/lib/rails/generators/rails/app/app_generator.rb b/railties/lib/rails/generators/rails/app/app_generator.rb new file mode 100644 index 0000000000..9110c129d1 --- /dev/null +++ b/railties/lib/rails/generators/rails/app/app_generator.rb @@ -0,0 +1,407 @@ +require 'rails/generators/app_base' + +module Rails + module ActionMethods # :nodoc: + attr_reader :options + + def initialize(generator) + @generator = generator + @options = generator.options + end + + private + %w(template copy_file directory empty_directory inside + empty_directory_with_keep_file create_file chmod shebang).each do |method| + class_eval <<-RUBY, __FILE__, __LINE__ + 1 + def #{method}(*args, &block) + @generator.send(:#{method}, *args, &block) + end + RUBY + end + + # TODO: Remove once this is fully in place + def method_missing(meth, *args, &block) + @generator.send(meth, *args, &block) + end + end + + # The application builder allows you to override elements of the application + # generator without being forced to reverse the operations of the default + # generator. + # + # This allows you to override entire operations, like the creation of the + # Gemfile, README, or JavaScript files, without needing to know exactly + # what those operations do so you can create another template action. + class AppBuilder + def rakefile + template "Rakefile" + end + + def readme + copy_file "README.rdoc", "README.rdoc" + end + + def gemfile + template "Gemfile" + end + + def configru + template "config.ru" + end + + def gitignore + template "gitignore", ".gitignore" + end + + def app + directory 'app' + + keep_file 'app/assets/images' + keep_file 'app/mailers' + keep_file 'app/models' + + keep_file 'app/controllers/concerns' + keep_file 'app/models/concerns' + end + + def bin + directory "bin" do |content| + "#{shebang}\n" + content + end + chmod "bin", 0755 & ~File.umask, verbose: false + end + + def config + empty_directory "config" + + inside "config" do + template "routes.rb" + template "application.rb" + template "environment.rb" + template "secrets.yml" + + directory "environments" + directory "initializers" + directory "locales" + end + end + + def config_when_updating + cookie_serializer_config_exist = File.exist?('config/initializers/cookies_serializer.rb') + + config + + unless cookie_serializer_config_exist + gsub_file 'config/initializers/cookies_serializer.rb', /json/, 'marshal' + end + end + + def database_yml + template "config/databases/#{options[:database]}.yml", "config/database.yml" + end + + def db + directory "db" + end + + def lib + empty_directory 'lib' + empty_directory_with_keep_file 'lib/tasks' + empty_directory_with_keep_file 'lib/assets' + end + + def log + empty_directory_with_keep_file 'log' + end + + def public_directory + directory "public", "public", recursive: false + end + + def test + empty_directory_with_keep_file 'test/fixtures' + empty_directory_with_keep_file 'test/controllers' + empty_directory_with_keep_file 'test/mailers' + empty_directory_with_keep_file 'test/models' + empty_directory_with_keep_file 'test/helpers' + empty_directory_with_keep_file 'test/integration' + + template 'test/test_helper.rb' + end + + def tmp + empty_directory "tmp/cache" + empty_directory "tmp/cache/assets" + end + + def vendor + vendor_javascripts + vendor_stylesheets + end + + def vendor_javascripts + unless options[:skip_javascript] + empty_directory_with_keep_file 'vendor/assets/javascripts' + end + end + + def vendor_stylesheets + empty_directory_with_keep_file 'vendor/assets/stylesheets' + end + end + + module Generators + # We need to store the RAILS_DEV_PATH in a constant, otherwise the path + # can change in Ruby 1.8.7 when we FileUtils.cd. + RAILS_DEV_PATH = File.expand_path("../../../../../..", File.dirname(__FILE__)) + RESERVED_NAMES = %w[application destroy plugin runner test] + + class AppGenerator < AppBase # :nodoc: + add_shared_options_for "application" + + # Add bin/rails options + class_option :version, type: :boolean, aliases: "-v", group: :rails, + desc: "Show Rails version number and quit" + + def initialize(*args) + super + + unless app_path + raise Error, "Application name should be provided in arguments. For details run: rails --help" + end + + if !options[:skip_active_record] && !DATABASES.include?(options[:database]) + raise Error, "Invalid value for --database option. Supported for preconfiguration are: #{DATABASES.join(", ")}." + end + end + + public_task :set_default_accessors! + public_task :create_root + + def create_root_files + build(:readme) + build(:rakefile) + build(:configru) + build(:gitignore) unless options[:skip_git] + build(:gemfile) unless options[:skip_gemfile] + end + + def create_app_files + build(:app) + end + + def create_bin_files + build(:bin) + end + + def create_config_files + build(:config) + end + + def update_config_files + build(:config_when_updating) + end + remove_task :update_config_files + + def create_boot_file + template "config/boot.rb" + end + + def create_active_record_files + return if options[:skip_active_record] + build(:database_yml) + end + + def create_db_files + build(:db) + end + + def create_lib_files + build(:lib) + end + + def create_log_files + build(:log) + end + + def create_public_files + build(:public_directory) + end + + def create_test_files + build(:test) unless options[:skip_test_unit] + end + + def create_tmp_files + build(:tmp) + end + + def create_vendor_files + build(:vendor) + end + + def delete_js_folder_skipping_javascript + if options[:skip_javascript] + remove_dir 'app/assets/javascripts' + end + end + + def delete_assets_initializer_skipping_sprockets + if options[:skip_sprockets] + remove_file 'config/initializers/assets.rb' + end + end + + def finish_template + build(:leftovers) + end + + public_task :apply_rails_template, :run_bundle + public_task :generate_spring_binstubs + + def run_after_bundle_callbacks + @after_bundle_callbacks.each do |callback| + callback.call + end + end + + protected + + def self.banner + "rails new #{self.arguments.map(&:usage).join(' ')} [options]" + end + + # Define file as an alias to create_file for backwards compatibility. + def file(*args, &block) + create_file(*args, &block) + end + + def app_name + @app_name ||= (defined_app_const_base? ? defined_app_name : File.basename(destination_root)).tr('\\', '').tr(". ", "_") + end + + def defined_app_name + defined_app_const_base.underscore + end + + def defined_app_const_base + Rails.respond_to?(:application) && defined?(Rails::Application) && + Rails.application.is_a?(Rails::Application) && Rails.application.class.name.sub(/::Application$/, "") + end + + alias :defined_app_const_base? :defined_app_const_base + + def app_const_base + @app_const_base ||= defined_app_const_base || app_name.gsub(/\W/, '_').squeeze('_').camelize + end + alias :camelized :app_const_base + + def app_const + @app_const ||= "#{app_const_base}::Application" + end + + def valid_const? + if app_const =~ /^\d/ + raise Error, "Invalid application name #{app_name}. Please give a name which does not start with numbers." + elsif RESERVED_NAMES.include?(app_name) + raise Error, "Invalid application name #{app_name}. Please give a name which does not match one of the reserved rails words." + elsif Object.const_defined?(app_const_base) + raise Error, "Invalid application name #{app_name}, constant #{app_const_base} is already in use. Please choose another application name." + end + end + + def app_secret + SecureRandom.hex(64) + end + + def mysql_socket + @mysql_socket ||= [ + "/tmp/mysql.sock", # default + "/var/run/mysqld/mysqld.sock", # debian/gentoo + "/var/tmp/mysql.sock", # freebsd + "/var/lib/mysql/mysql.sock", # fedora + "/opt/local/lib/mysql/mysql.sock", # fedora + "/opt/local/var/run/mysqld/mysqld.sock", # mac + darwinports + mysql + "/opt/local/var/run/mysql4/mysqld.sock", # mac + darwinports + mysql4 + "/opt/local/var/run/mysql5/mysqld.sock", # mac + darwinports + mysql5 + "/opt/lampp/var/mysql/mysql.sock" # xampp for linux + ].find { |f| File.exist?(f) } unless RbConfig::CONFIG['host_os'] =~ /mswin|mingw/ + end + + def get_builder_class + defined?(::AppBuilder) ? ::AppBuilder : Rails::AppBuilder + end + end + + # This class handles preparation of the arguments before the AppGenerator is + # called. The class provides version or help information if they were + # requested, and also constructs the railsrc file (used for extra configuration + # options). + # + # This class should be called before the AppGenerator is required and started + # since it configures and mutates ARGV correctly. + class ARGVScrubber # :nodoc + def initialize(argv = ARGV) + @argv = argv + end + + def prepare! + handle_version_request!(@argv.first) + handle_invalid_command!(@argv.first, @argv) do + handle_rails_rc!(@argv.drop(1)) + end + end + + def self.default_rc_file + File.expand_path('~/.railsrc') + end + + private + + def handle_version_request!(argument) + if ['--version', '-v'].include?(argument) + require 'rails/version' + puts "Rails #{Rails::VERSION::STRING}" + exit(0) + end + end + + def handle_invalid_command!(argument, argv) + if argument == "new" + yield + else + ['--help'] + argv.drop(1) + end + end + + def handle_rails_rc!(argv) + if argv.find { |arg| arg == '--no-rc' } + argv.reject { |arg| arg == '--no-rc' } + else + railsrc(argv) { |rc_argv, rc| insert_railsrc_into_argv!(rc_argv, rc) } + end + end + + def railsrc(argv) + if (customrc = argv.index{ |x| x.include?("--rc=") }) + fname = File.expand_path(argv[customrc].gsub(/--rc=/, "")) + yield(argv.take(customrc) + argv.drop(customrc + 1), fname) + else + yield argv, self.class.default_rc_file + end + end + + def read_rc_file(railsrc) + extra_args = File.readlines(railsrc).flat_map(&:split) + puts "Using #{extra_args.join(" ")} from #{railsrc}" + extra_args + end + + def insert_railsrc_into_argv!(argv, railsrc) + return argv unless File.exist?(railsrc) + extra_args = read_rc_file railsrc + argv.take(1) + extra_args + argv.drop(1) + end + end + end +end diff --git a/railties/lib/rails/generators/rails/app/templates/Gemfile b/railties/lib/rails/generators/rails/app/templates/Gemfile new file mode 100644 index 0000000000..8b51fda359 --- /dev/null +++ b/railties/lib/rails/generators/rails/app/templates/Gemfile @@ -0,0 +1,45 @@ +source 'https://rubygems.org' + +<% gemfile_entries.each do |gem| -%> +<% if gem.comment -%> + +# <%= gem.comment %> +<% end -%> +<%= gem.commented_out ? '# ' : '' %>gem '<%= gem.name %>'<%= %(, '#{gem.version}') if gem.version -%> +<% if gem.options.any? -%> +, <%= gem.options.map { |k,v| + "#{k}: #{v.inspect}" }.join(', ') %> +<% end -%> +<% end -%> + +# Use ActiveModel has_secure_password +# gem 'bcrypt', '~> 3.1.7' + +# Use Unicorn as the app server +# gem 'unicorn' + +# Use Capistrano for deployment +# gem 'capistrano-rails', group: :development + +group :development, :test do +<% unless defined?(JRUBY_VERSION) -%> + # Call 'debugger' anywhere in the code to stop execution and get a debugger console + <%- if RUBY_VERSION < '2.0.0' -%> + gem 'debugger' + <%- else -%> + gem 'byebug' + <%- end -%> + + # Access an IRB console on exceptions page and /console in development + gem 'web-console' +<%- if spring_install? %> + # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring + gem 'spring' +<% end -%> +<% end -%> +end + +<% if RUBY_PLATFORM.match(/bccwin|cygwin|emx|mingw|mswin|wince/) -%> +# Windows does not include zoneinfo files, so bundle the tzinfo-data gem +gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw] +<% end -%> diff --git a/railties/lib/rails/generators/rails/app/templates/README.rdoc b/railties/lib/rails/generators/rails/app/templates/README.rdoc new file mode 100644 index 0000000000..dd4e97e22e --- /dev/null +++ b/railties/lib/rails/generators/rails/app/templates/README.rdoc @@ -0,0 +1,28 @@ +== README + +This README would normally document whatever steps are necessary to get the +application up and running. + +Things you may want to cover: + +* Ruby version + +* System dependencies + +* Configuration + +* Database creation + +* Database initialization + +* How to run the test suite + +* Services (job queues, cache servers, search engines, etc.) + +* Deployment instructions + +* ... + + +Please feel free to use a different markup language if you do not plan to run +<tt>rake doc:app</tt>. diff --git a/railties/lib/rails/generators/rails/app/templates/Rakefile b/railties/lib/rails/generators/rails/app/templates/Rakefile new file mode 100644 index 0000000000..ba6b733dd2 --- /dev/null +++ b/railties/lib/rails/generators/rails/app/templates/Rakefile @@ -0,0 +1,6 @@ +# Add your own tasks in files placed in lib/tasks ending in .rake, +# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake. + +require File.expand_path('../config/application', __FILE__) + +Rails.application.load_tasks diff --git a/railties/lib/rails/generators/rails/app/templates/app/assets/javascripts/application.js.tt b/railties/lib/rails/generators/rails/app/templates/app/assets/javascripts/application.js.tt new file mode 100644 index 0000000000..07ea09cdbd --- /dev/null +++ b/railties/lib/rails/generators/rails/app/templates/app/assets/javascripts/application.js.tt @@ -0,0 +1,20 @@ +// This is a manifest file that'll be compiled into application.js, which will include all the files +// listed below. +// +// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts, +// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path. +// +// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the +// compiled file. +// +// Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details +// about supported directives. +// +<% unless options[:skip_javascript] -%> +//= require <%= options[:javascript] %> +//= require <%= options[:javascript] %>_ujs +<% if gemfile_entries.any? { |m| m.name == "turbolinks" } -%> +//= require turbolinks +<% end -%> +<% end -%> +//= require_tree . diff --git a/railties/lib/rails/generators/rails/app/templates/app/assets/stylesheets/application.css b/railties/lib/rails/generators/rails/app/templates/app/assets/stylesheets/application.css new file mode 100644 index 0000000000..a443db3401 --- /dev/null +++ b/railties/lib/rails/generators/rails/app/templates/app/assets/stylesheets/application.css @@ -0,0 +1,15 @@ +/* + * This is a manifest file that'll be compiled into application.css, which will include all the files + * listed below. + * + * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets, + * or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path. + * + * You're free to add application-wide styles to this file and they'll appear at the bottom of the + * compiled file so the styles you add here take precedence over styles defined in any styles + * defined in the other CSS/SCSS files in this directory. It is generally better to create a new + * file per style scope. + * + *= require_tree . + *= require_self + */ diff --git a/railties/lib/rails/generators/rails/app/templates/app/controllers/application_controller.rb.tt b/railties/lib/rails/generators/rails/app/templates/app/controllers/application_controller.rb.tt new file mode 100644 index 0000000000..d83690e1b9 --- /dev/null +++ b/railties/lib/rails/generators/rails/app/templates/app/controllers/application_controller.rb.tt @@ -0,0 +1,5 @@ +class ApplicationController < ActionController::Base + # Prevent CSRF attacks by raising an exception. + # For APIs, you may want to use :null_session instead. + protect_from_forgery with: :exception +end diff --git a/railties/lib/rails/generators/rails/app/templates/app/helpers/application_helper.rb b/railties/lib/rails/generators/rails/app/templates/app/helpers/application_helper.rb new file mode 100644 index 0000000000..de6be7945c --- /dev/null +++ b/railties/lib/rails/generators/rails/app/templates/app/helpers/application_helper.rb @@ -0,0 +1,2 @@ +module ApplicationHelper +end diff --git a/railties/lib/rails/generators/rails/app/templates/app/views/layouts/application.html.erb.tt b/railties/lib/rails/generators/rails/app/templates/app/views/layouts/application.html.erb.tt new file mode 100644 index 0000000000..75ea52828e --- /dev/null +++ b/railties/lib/rails/generators/rails/app/templates/app/views/layouts/application.html.erb.tt @@ -0,0 +1,23 @@ +<!DOCTYPE html> +<html> +<head> + <title><%= camelized %></title> + <%- if options[:skip_javascript] -%> + <%%= stylesheet_link_tag 'application', media: 'all' %> + <%- else -%> + <%- if gemfile_entries.any? { |m| m.name == 'turbolinks' } -%> + <%%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %> + <%%= javascript_include_tag 'application', 'data-turbolinks-track' => true %> + <%- else -%> + <%%= stylesheet_link_tag 'application', media: 'all' %> + <%%= javascript_include_tag 'application' %> + <%- end -%> + <%- end -%> + <%%= csrf_meta_tags %> +</head> +<body> + +<%%= yield %> + +</body> +</html> diff --git a/railties/lib/rails/generators/rails/app/templates/bin/bundle b/railties/lib/rails/generators/rails/app/templates/bin/bundle new file mode 100644 index 0000000000..1123dcf501 --- /dev/null +++ b/railties/lib/rails/generators/rails/app/templates/bin/bundle @@ -0,0 +1,2 @@ +ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__) +load Gem.bin_path('bundler', 'bundle') diff --git a/railties/lib/rails/generators/rails/app/templates/bin/rails b/railties/lib/rails/generators/rails/app/templates/bin/rails new file mode 100644 index 0000000000..6a128b95e5 --- /dev/null +++ b/railties/lib/rails/generators/rails/app/templates/bin/rails @@ -0,0 +1,3 @@ +APP_PATH = File.expand_path('../../config/application', __FILE__) +require_relative '../config/boot' +require 'rails/commands' diff --git a/railties/lib/rails/generators/rails/app/templates/bin/rake b/railties/lib/rails/generators/rails/app/templates/bin/rake new file mode 100644 index 0000000000..d14fc8395b --- /dev/null +++ b/railties/lib/rails/generators/rails/app/templates/bin/rake @@ -0,0 +1,3 @@ +require_relative '../config/boot' +require 'rake' +Rake.application.run diff --git a/railties/lib/rails/generators/rails/app/templates/bin/setup b/railties/lib/rails/generators/rails/app/templates/bin/setup new file mode 100644 index 0000000000..0e22b3fa5c --- /dev/null +++ b/railties/lib/rails/generators/rails/app/templates/bin/setup @@ -0,0 +1,28 @@ +require 'pathname' + +# path to your application root. +APP_ROOT = Pathname.new File.expand_path('../../', __FILE__) + +Dir.chdir APP_ROOT do + # This script is a starting point to setup your application. + # Add necessary setup steps to this file: + + puts "== Installing dependencies ==" + system "gem install bundler --conservative" + system "bundle check || bundle install" + + # puts "\n== Copying sample files ==" + # unless File.exist?("config/database.yml") + # system "cp config/database.yml.sample config/database.yml" + # end + + puts "\n== Preparing database ==" + system "bin/rake db:setup" + + puts "\n== Removing old logs and tempfiles ==" + system "rm -f log/*" + system "rm -rf tmp/cache" + + puts "\n== Restarting application server ==" + system "touch tmp/restart.txt" +end diff --git a/railties/lib/rails/generators/rails/app/templates/config.ru b/railties/lib/rails/generators/rails/app/templates/config.ru new file mode 100644 index 0000000000..5bc2a619e8 --- /dev/null +++ b/railties/lib/rails/generators/rails/app/templates/config.ru @@ -0,0 +1,4 @@ +# This file is used by Rack-based servers to start the application. + +require ::File.expand_path('../config/environment', __FILE__) +run Rails.application diff --git a/railties/lib/rails/generators/rails/app/templates/config/application.rb b/railties/lib/rails/generators/rails/app/templates/config/application.rb new file mode 100644 index 0000000000..16fe50bab8 --- /dev/null +++ b/railties/lib/rails/generators/rails/app/templates/config/application.rb @@ -0,0 +1,34 @@ +require File.expand_path('../boot', __FILE__) + +<% if include_all_railties? -%> +require 'rails/all' +<% else -%> +# Pick the frameworks you want: +require "active_model/railtie" +<%= comment_if :skip_active_record %>require "active_record/railtie" +require "action_controller/railtie" +require "action_mailer/railtie" +<%= comment_if :skip_action_view %>require "action_view/railtie" +<%= comment_if :skip_sprockets %>require "sprockets/railtie" +<%= comment_if :skip_test_unit %>require "rails/test_unit/railtie" +<% end -%> + +# Require the gems listed in Gemfile, including any gems +# you've limited to :test, :development, or :production. +Bundler.require(*Rails.groups) + +module <%= app_const_base %> + class Application < Rails::Application + # Settings in config/environments/* take precedence over those specified here. + # Application configuration should go into files in config/initializers + # -- all .rb files in that directory are automatically loaded. + + # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone. + # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC. + # config.time_zone = 'Central Time (US & Canada)' + + # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded. + # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s] + # config.i18n.default_locale = :de + end +end diff --git a/railties/lib/rails/generators/rails/app/templates/config/boot.rb b/railties/lib/rails/generators/rails/app/templates/config/boot.rb new file mode 100644 index 0000000000..6b750f00b1 --- /dev/null +++ b/railties/lib/rails/generators/rails/app/templates/config/boot.rb @@ -0,0 +1,3 @@ +ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__) + +require 'bundler/setup' # Set up gems listed in the Gemfile. diff --git a/railties/lib/rails/generators/rails/app/templates/config/databases/frontbase.yml b/railties/lib/rails/generators/rails/app/templates/config/databases/frontbase.yml new file mode 100644 index 0000000000..34fc0e3465 --- /dev/null +++ b/railties/lib/rails/generators/rails/app/templates/config/databases/frontbase.yml @@ -0,0 +1,49 @@ +# FrontBase versions 4.x +# +# Get the bindings: +# gem install ruby-frontbase +# +# Configure Using Gemfile +# gem 'ruby-frontbase' +# +default: &default + adapter: frontbase + host: localhost + username: <%= app_name %> + password: '' + +development: + <<: *default + database: <%= app_name %>_development + +# Warning: The database defined as "test" will be erased and +# re-generated from your development database when you run "rake". +# Do not set this db to the same as development or production. +test: + <<: *default + database: <%= app_name %>_test + +# As with config/secrets.yml, you never want to store sensitive information, +# like your database password, in your source code. If your source code is +# ever seen by anyone, they now have access to your database. +# +# Instead, provide the password as a unix environment variable when you boot +# the app. Read http://guides.rubyonrails.org/configuring.html#configuring-a-database +# for a full rundown on how to provide these environment variables in a +# production deployment. +# +# On Heroku and other platform providers, you may have a full connection URL +# available as an environment variable. For example: +# +# DATABASE_URL="frontbase://myuser:mypass@localhost/somedatabase" +# +# You can use this database configuration with: +# +# production: +# url: <%%= ENV['DATABASE_URL'] %> +# +production: + <<: *default + database: <%= app_name %>_production + username: <%= app_name %> + password: <%%= ENV['<%= app_name.upcase %>_DATABASE_PASSWORD'] %> diff --git a/railties/lib/rails/generators/rails/app/templates/config/databases/ibm_db.yml b/railties/lib/rails/generators/rails/app/templates/config/databases/ibm_db.yml new file mode 100644 index 0000000000..187ff01bac --- /dev/null +++ b/railties/lib/rails/generators/rails/app/templates/config/databases/ibm_db.yml @@ -0,0 +1,85 @@ +# IBM Dataservers +# +# Home Page +# https://github.com/dparnell/ibm_db +# +# To install the ibm_db gem: +# +# On Linux: +# . /home/db2inst1/sqllib/db2profile +# export IBM_DB_INCLUDE=/opt/ibm/db2/V9.7/include +# export IBM_DB_LIB=/opt/ibm/db2/V9.7/lib32 +# gem install ibm_db +# +# On Mac OS X 10.5: +# . /home/db2inst1/sqllib/db2profile +# export IBM_DB_INCLUDE=/opt/ibm/db2/V9.7/include +# export IBM_DB_LIB=/opt/ibm/db2/V9.7/lib32 +# export ARCHFLAGS="-arch i386" +# gem install ibm_db +# +# On Mac OS X 10.6: +# . /home/db2inst1/sqllib/db2profile +# export IBM_DB_INCLUDE=/opt/ibm/db2/V9.7/include +# export IBM_DB_LIB=/opt/ibm/db2/V9.7/lib64 +# export ARCHFLAGS="-arch x86_64" +# gem install ibm_db +# +# On Windows: +# Issue the command: gem install ibm_db +# +# Configure Using Gemfile +# gem 'ibm_db' +# +# +default: &default + adapter: ibm_db + username: db2inst1 + password: + #schema: db2inst1 + #host: localhost + #port: 50000 + #account: my_account + #app_user: my_app_user + #application: my_application + #workstation: my_workstation + #security: SSL + #timeout: 10 + #authentication: SERVER + #parameterized: false + +development: + <<: *default + database: <%= app_name[0,4] %>_dev + +# Warning: The database defined as "test" will be erased and +# re-generated from your development database when you run "rake". +# Do not set this db to the same as development or production. +test: + <<: *default + database: <%= app_name[0,4] %>_tst + +# As with config/secrets.yml, you never want to store sensitive information, +# like your database password, in your source code. If your source code is +# ever seen by anyone, they now have access to your database. +# +# Instead, provide the password as a unix environment variable when you boot +# the app. Read http://guides.rubyonrails.org/configuring.html#configuring-a-database +# for a full rundown on how to provide these environment variables in a +# production deployment. +# +# On Heroku and other platform providers, you may have a full connection URL +# available as an environment variable. For example: +# +# DATABASE_URL="ibm-db://myuser:mypass@localhost/somedatabase" +# +# You can use this database configuration with: +# +# production: +# url: <%%= ENV['DATABASE_URL'] %> +# +production: + <<: *default + database: <%= app_name %>_production + username: <%= app_name %> + password: <%%= ENV['<%= app_name.upcase %>_DATABASE_PASSWORD'] %> diff --git a/railties/lib/rails/generators/rails/app/templates/config/databases/jdbc.yml b/railties/lib/rails/generators/rails/app/templates/config/databases/jdbc.yml new file mode 100644 index 0000000000..db0a429753 --- /dev/null +++ b/railties/lib/rails/generators/rails/app/templates/config/databases/jdbc.yml @@ -0,0 +1,68 @@ +# If you are using mssql, derby, hsqldb, or h2 with one of the +# ActiveRecord JDBC adapters, install the appropriate driver, e.g.,: +# gem install activerecord-jdbcmssql-adapter +# +# Configure using Gemfile: +# gem 'activerecord-jdbcmssql-adapter' +# +# development: +# adapter: mssql +# username: <%= app_name %> +# password: +# host: localhost +# database: <%= app_name %>_development +# +# Warning: The database defined as "test" will be erased and +# re-generated from your development database when you run "rake". +# Do not set this db to the same as development or production. +# +# test: +# adapter: mssql +# username: <%= app_name %> +# password: +# host: localhost +# database: <%= app_name %>_test +# +# production: +# adapter: mssql +# username: <%= app_name %> +# password: +# host: localhost +# database: <%= app_name %>_production + +# If you are using oracle, db2, sybase, informix or prefer to use the plain +# JDBC adapter, configure your database setting as the example below (requires +# you to download and manually install the database vendor's JDBC driver .jar +# file). See your driver documentation for the appropriate driver class and +# connection string: + +default: &default + adapter: jdbc + username: <%= app_name %> + password: + driver: + +development: + <<: *default + url: jdbc:db://localhost/<%= app_name %>_development + +# Warning: The database defined as "test" will be erased and +# re-generated from your development database when you run "rake". +# Do not set this db to the same as development or production. +test: + <<: *default + url: jdbc:db://localhost/<%= app_name %>_test + +# As with config/secrets.yml, you never want to store sensitive information, +# like your database password, in your source code. If your source code is +# ever seen by anyone, they now have access to your database. +# +# Instead, provide the password as a unix environment variable when you boot +# the app. Read http://guides.rubyonrails.org/configuring.html#configuring-a-database +# for a full rundown on how to provide these environment variables in a +# production deployment. +# +production: + url: jdbc:db://localhost/<%= app_name %>_production + username: <%= app_name %> + password: <%%= ENV['<%= app_name.upcase %>_DATABASE_PASSWORD'] %> diff --git a/railties/lib/rails/generators/rails/app/templates/config/databases/jdbcmysql.yml b/railties/lib/rails/generators/rails/app/templates/config/databases/jdbcmysql.yml new file mode 100644 index 0000000000..acb93939e1 --- /dev/null +++ b/railties/lib/rails/generators/rails/app/templates/config/databases/jdbcmysql.yml @@ -0,0 +1,52 @@ +# MySQL. Versions 4.1 and 5.0 are recommended. +# +# Install the MySQL driver: +# gem install activerecord-jdbcmysql-adapter +# +# Configure Using Gemfile +# gem 'activerecord-jdbcmysql-adapter' +# +# And be sure to use new-style password hashing: +# http://dev.mysql.com/doc/refman/5.0/en/old-client.html +# +default: &default + adapter: mysql + username: root + password: + host: localhost + +development: + <<: *default + database: <%= app_name %>_development + +# Warning: The database defined as "test" will be erased and +# re-generated from your development database when you run "rake". +# Do not set this db to the same as development or production. +test: + <<: *default + database: <%= app_name %>_test + +# As with config/secrets.yml, you never want to store sensitive information, +# like your database password, in your source code. If your source code is +# ever seen by anyone, they now have access to your database. +# +# Instead, provide the password as a unix environment variable when you boot +# the app. Read http://guides.rubyonrails.org/configuring.html#configuring-a-database +# for a full rundown on how to provide these environment variables in a +# production deployment. +# +# On Heroku and other platform providers, you may have a full connection URL +# available as an environment variable. For example: +# +# DATABASE_URL="mysql://myuser:mypass@localhost/somedatabase" +# +# You can use this database configuration with: +# +# production: +# url: <%%= ENV['DATABASE_URL'] %> +# +production: + <<: *default + database: <%= app_name %>_production + username: <%= app_name %> + password: <%%= ENV['<%= app_name.upcase %>_DATABASE_PASSWORD'] %> diff --git a/railties/lib/rails/generators/rails/app/templates/config/databases/jdbcpostgresql.yml b/railties/lib/rails/generators/rails/app/templates/config/databases/jdbcpostgresql.yml new file mode 100644 index 0000000000..9e99264d33 --- /dev/null +++ b/railties/lib/rails/generators/rails/app/templates/config/databases/jdbcpostgresql.yml @@ -0,0 +1,68 @@ +# PostgreSQL. Versions 8.2 and up are supported. +# +# Configure Using Gemfile +# gem 'activerecord-jdbcpostgresql-adapter' +# +default: &default + adapter: postgresql + encoding: unicode + +development: + <<: *default + database: <%= app_name %>_development + + # The specified database role being used to connect to postgres. + # To create additional roles in postgres see `$ createuser --help`. + # When left blank, postgres will use the default role. This is + # the same name as the operating system user that initialized the database. + #username: <%= app_name %> + + # The password associated with the postgres role (username). + #password: + + # Connect on a TCP socket. Omitted by default since the client uses a + # domain socket that doesn't need configuration. Windows does not have + # domain sockets, so uncomment these lines. + #host: localhost + #port: 5432 + + # Schema search path. The server defaults to $user,public + #schema_search_path: myapp,sharedapp,public + + # Minimum log levels, in increasing order: + # debug5, debug4, debug3, debug2, debug1, + # log, notice, warning, error, fatal, and panic + # Defaults to warning. + #min_messages: notice + +# Warning: The database defined as "test" will be erased and +# re-generated from your development database when you run "rake". +# Do not set this db to the same as development or production. +test: + <<: *default + database: <%= app_name %>_test + +# As with config/secrets.yml, you never want to store sensitive information, +# like your database password, in your source code. If your source code is +# ever seen by anyone, they now have access to your database. +# +# Instead, provide the password as a unix environment variable when you boot +# the app. Read http://guides.rubyonrails.org/configuring.html#configuring-a-database +# for a full rundown on how to provide these environment variables in a +# production deployment. +# +# On Heroku and other platform providers, you may have a full connection URL +# available as an environment variable. For example: +# +# DATABASE_URL="postgres://myuser:mypass@localhost/somedatabase" +# +# You can use this database configuration with: +# +# production: +# url: <%%= ENV['DATABASE_URL'] %> +# +production: + <<: *default + database: <%= app_name %>_production + username: <%= app_name %> + password: <%%= ENV['<%= app_name.upcase %>_DATABASE_PASSWORD'] %> diff --git a/railties/lib/rails/generators/rails/app/templates/config/databases/jdbcsqlite3.yml b/railties/lib/rails/generators/rails/app/templates/config/databases/jdbcsqlite3.yml new file mode 100644 index 0000000000..28c36eb82f --- /dev/null +++ b/railties/lib/rails/generators/rails/app/templates/config/databases/jdbcsqlite3.yml @@ -0,0 +1,23 @@ +# SQLite version 3.x +# gem 'activerecord-jdbcsqlite3-adapter' +# +# Configure Using Gemfile +# gem 'activerecord-jdbcsqlite3-adapter' +# +default: &default + adapter: sqlite3 + +development: + <<: *default + database: db/development.sqlite3 + +# Warning: The database defined as "test" will be erased and +# re-generated from your development database when you run "rake". +# Do not set this db to the same as development or production. +test: + <<: *default + database: db/test.sqlite3 + +production: + <<: *default + database: db/production.sqlite3 diff --git a/railties/lib/rails/generators/rails/app/templates/config/databases/mysql.yml b/railties/lib/rails/generators/rails/app/templates/config/databases/mysql.yml new file mode 100644 index 0000000000..4b2e6646c7 --- /dev/null +++ b/railties/lib/rails/generators/rails/app/templates/config/databases/mysql.yml @@ -0,0 +1,58 @@ +# MySQL. Versions 5.0+ are recommended. +# +# Install the MYSQL driver +# gem install mysql2 +# +# Ensure the MySQL gem is defined in your Gemfile +# gem 'mysql2' +# +# And be sure to use new-style password hashing: +# http://dev.mysql.com/doc/refman/5.0/en/old-client.html +# +default: &default + adapter: mysql2 + encoding: utf8 + pool: 5 + username: root + password: +<% if mysql_socket -%> + socket: <%= mysql_socket %> +<% else -%> + host: localhost +<% end -%> + +development: + <<: *default + database: <%= app_name %>_development + +# Warning: The database defined as "test" will be erased and +# re-generated from your development database when you run "rake". +# Do not set this db to the same as development or production. +test: + <<: *default + database: <%= app_name %>_test + +# As with config/secrets.yml, you never want to store sensitive information, +# like your database password, in your source code. If your source code is +# ever seen by anyone, they now have access to your database. +# +# Instead, provide the password as a unix environment variable when you boot +# the app. Read http://guides.rubyonrails.org/configuring.html#configuring-a-database +# for a full rundown on how to provide these environment variables in a +# production deployment. +# +# On Heroku and other platform providers, you may have a full connection URL +# available as an environment variable. For example: +# +# DATABASE_URL="mysql2://myuser:mypass@localhost/somedatabase" +# +# You can use this database configuration with: +# +# production: +# url: <%%= ENV['DATABASE_URL'] %> +# +production: + <<: *default + database: <%= app_name %>_production + username: <%= app_name %> + password: <%%= ENV['<%= app_name.upcase %>_DATABASE_PASSWORD'] %> diff --git a/railties/lib/rails/generators/rails/app/templates/config/databases/oracle.yml b/railties/lib/rails/generators/rails/app/templates/config/databases/oracle.yml new file mode 100644 index 0000000000..9aedcc15cb --- /dev/null +++ b/railties/lib/rails/generators/rails/app/templates/config/databases/oracle.yml @@ -0,0 +1,58 @@ +# Oracle/OCI 8i, 9, 10g +# +# Requires Ruby/OCI8: +# https://github.com/kubo/ruby-oci8 +# +# Specify your database using any valid connection syntax, such as a +# tnsnames.ora service name, or an SQL connect string of the form: +# +# //host:[port][/service name] +# +# By default prefetch_rows (OCI_ATTR_PREFETCH_ROWS) is set to 100. And +# until true bind variables are supported, cursor_sharing is set by default +# to 'similar'. Both can be changed in the configuration below; the defaults +# are equivalent to specifying: +# +# prefetch_rows: 100 +# cursor_sharing: similar +# +default: &default + adapter: oracle + username: <%= app_name %> + password: + +development: + <<: *default + database: <%= app_name %>_development + +# Warning: The database defined as "test" will be erased and +# re-generated from your development database when you run "rake". +# Do not set this db to the same as development or production. +test: + <<: *default + database: <%= app_name %>_test + +# As with config/secrets.yml, you never want to store sensitive information, +# like your database password, in your source code. If your source code is +# ever seen by anyone, they now have access to your database. +# +# Instead, provide the password as a unix environment variable when you boot +# the app. Read http://guides.rubyonrails.org/configuring.html#configuring-a-database +# for a full rundown on how to provide these environment variables in a +# production deployment. +# +# On Heroku and other platform providers, you may have a full connection URL +# available as an environment variable. For example: +# +# DATABASE_URL="oracle://myuser:mypass@localhost/somedatabase" +# +# You can use this database configuration with: +# +# production: +# url: <%%= ENV['DATABASE_URL'] %> +# +production: + <<: *default + database: <%= app_name %>_production + username: <%= app_name %> + password: <%%= ENV['<%= app_name.upcase %>_DATABASE_PASSWORD'] %> diff --git a/railties/lib/rails/generators/rails/app/templates/config/databases/postgresql.yml b/railties/lib/rails/generators/rails/app/templates/config/databases/postgresql.yml new file mode 100644 index 0000000000..feb25bbc6b --- /dev/null +++ b/railties/lib/rails/generators/rails/app/templates/config/databases/postgresql.yml @@ -0,0 +1,85 @@ +# PostgreSQL. Versions 8.2 and up are supported. +# +# Install the pg driver: +# gem install pg +# On OS X with Homebrew: +# gem install pg -- --with-pg-config=/usr/local/bin/pg_config +# On OS X with MacPorts: +# gem install pg -- --with-pg-config=/opt/local/lib/postgresql84/bin/pg_config +# On Windows: +# gem install pg +# Choose the win32 build. +# Install PostgreSQL and put its /bin directory on your path. +# +# Configure Using Gemfile +# gem 'pg' +# +default: &default + adapter: postgresql + encoding: unicode + # For details on connection pooling, see rails configuration guide + # http://guides.rubyonrails.org/configuring.html#database-pooling + pool: 5 + +development: + <<: *default + database: <%= app_name %>_development + + # The specified database role being used to connect to postgres. + # To create additional roles in postgres see `$ createuser --help`. + # When left blank, postgres will use the default role. This is + # the same name as the operating system user that initialized the database. + #username: <%= app_name %> + + # The password associated with the postgres role (username). + #password: + + # Connect on a TCP socket. Omitted by default since the client uses a + # domain socket that doesn't need configuration. Windows does not have + # domain sockets, so uncomment these lines. + #host: localhost + + # The TCP port the server listens on. Defaults to 5432. + # If your server runs on a different port number, change accordingly. + #port: 5432 + + # Schema search path. The server defaults to $user,public + #schema_search_path: myapp,sharedapp,public + + # Minimum log levels, in increasing order: + # debug5, debug4, debug3, debug2, debug1, + # log, notice, warning, error, fatal, and panic + # Defaults to warning. + #min_messages: notice + +# Warning: The database defined as "test" will be erased and +# re-generated from your development database when you run "rake". +# Do not set this db to the same as development or production. +test: + <<: *default + database: <%= app_name %>_test + +# As with config/secrets.yml, you never want to store sensitive information, +# like your database password, in your source code. If your source code is +# ever seen by anyone, they now have access to your database. +# +# Instead, provide the password as a unix environment variable when you boot +# the app. Read http://guides.rubyonrails.org/configuring.html#configuring-a-database +# for a full rundown on how to provide these environment variables in a +# production deployment. +# +# On Heroku and other platform providers, you may have a full connection URL +# available as an environment variable. For example: +# +# DATABASE_URL="postgres://myuser:mypass@localhost/somedatabase" +# +# You can use this database configuration with: +# +# production: +# url: <%%= ENV['DATABASE_URL'] %> +# +production: + <<: *default + database: <%= app_name %>_production + username: <%= app_name %> + password: <%%= ENV['<%= app_name.upcase %>_DATABASE_PASSWORD'] %> diff --git a/railties/lib/rails/generators/rails/app/templates/config/databases/sqlite3.yml b/railties/lib/rails/generators/rails/app/templates/config/databases/sqlite3.yml new file mode 100644 index 0000000000..1c1a37ca8d --- /dev/null +++ b/railties/lib/rails/generators/rails/app/templates/config/databases/sqlite3.yml @@ -0,0 +1,25 @@ +# SQLite version 3.x +# gem install sqlite3 +# +# Ensure the SQLite 3 gem is defined in your Gemfile +# gem 'sqlite3' +# +default: &default + adapter: sqlite3 + pool: 5 + timeout: 5000 + +development: + <<: *default + database: db/development.sqlite3 + +# Warning: The database defined as "test" will be erased and +# re-generated from your development database when you run "rake". +# Do not set this db to the same as development or production. +test: + <<: *default + database: db/test.sqlite3 + +production: + <<: *default + database: db/production.sqlite3 diff --git a/railties/lib/rails/generators/rails/app/templates/config/databases/sqlserver.yml b/railties/lib/rails/generators/rails/app/templates/config/databases/sqlserver.yml new file mode 100644 index 0000000000..30b0df34a8 --- /dev/null +++ b/railties/lib/rails/generators/rails/app/templates/config/databases/sqlserver.yml @@ -0,0 +1,68 @@ +# SQL Server (2005 or higher recommended) +# +# Install the adapters and driver +# gem install tiny_tds +# gem install activerecord-sqlserver-adapter +# +# Ensure the activerecord adapter and db driver gems are defined in your Gemfile +# gem 'tiny_tds' +# gem 'activerecord-sqlserver-adapter' +# +# You should make sure freetds is configured correctly first. +# freetds.conf contains host/port/protocol_versions settings. +# http://freetds.schemamania.org/userguide/freetdsconf.htm +# +# A typical Microsoft server +# [mssql] +# host = mssqlserver.yourdomain.com +# port = 1433 +# tds version = 7.1 + +# If you can connect with "tsql -S servername", your basic FreeTDS installation is working. +# 'man tsql' for more info +# Set timeout to a larger number if valid queries against a live db fail +# +default: &default + adapter: sqlserver + encoding: utf8 + reconnect: false + username: <%= app_name %> + password: + timeout: 25 + dataserver: from_freetds.conf + +development: + <<: *default + database: <%= app_name %>_development + +# Warning: The database defined as "test" will be erased and +# re-generated from your development database when you run "rake". +# Do not set this db to the same as development or production. +test: + <<: *default + database: <%= app_name %>_test + +# As with config/secrets.yml, you never want to store sensitive information, +# like your database password, in your source code. If your source code is +# ever seen by anyone, they now have access to your database. +# +# Instead, provide the password as a unix environment variable when you boot +# the app. Read http://guides.rubyonrails.org/configuring.html#configuring-a-database +# for a full rundown on how to provide these environment variables in a +# production deployment. +# +# On Heroku and other platform providers, you may have a full connection URL +# available as an environment variable. For example: +# +# DATABASE_URL="sqlserver://myuser:mypass@localhost/somedatabase" +# +# You can use this database configuration with: +# +# production: +# url: <%%= ENV['DATABASE_URL'] %> +# +production: + <<: *default + database: <%= app_name %>_production + username: <%= app_name %> + password: <%%= ENV['<%= app_name.upcase %>_DATABASE_PASSWORD'] %> diff --git a/railties/lib/rails/generators/rails/app/templates/config/environment.rb b/railties/lib/rails/generators/rails/app/templates/config/environment.rb new file mode 100644 index 0000000000..ee8d90dc65 --- /dev/null +++ b/railties/lib/rails/generators/rails/app/templates/config/environment.rb @@ -0,0 +1,5 @@ +# Load the Rails application. +require File.expand_path('../application', __FILE__) + +# Initialize the Rails application. +Rails.application.initialize! diff --git a/railties/lib/rails/generators/rails/app/templates/config/environments/development.rb.tt b/railties/lib/rails/generators/rails/app/templates/config/environments/development.rb.tt new file mode 100644 index 0000000000..35e3035a0b --- /dev/null +++ b/railties/lib/rails/generators/rails/app/templates/config/environments/development.rb.tt @@ -0,0 +1,45 @@ +Rails.application.configure do + # Settings specified here will take precedence over those in config/application.rb. + + # In the development environment your application's code is reloaded on + # every request. This slows down response time but is perfect for development + # since you don't have to restart the web server when you make code changes. + config.cache_classes = false + + # Do not eager load code on boot. + config.eager_load = false + + # Show full error reports and disable caching. + config.consider_all_requests_local = true + config.action_controller.perform_caching = false + + # Don't care if the mailer can't send. + config.action_mailer.raise_delivery_errors = false + + # Print deprecation notices to the Rails logger. + config.active_support.deprecation = :log + + <%- unless options.skip_active_record? -%> + # Raise an error on page load if there are pending migrations. + config.active_record.migration_error = :page_load + + <%- end -%> + <%- unless options.skip_sprockets? -%> + # Debug mode disables concatenation and preprocessing of assets. + # This option may cause significant delays in view rendering with a large + # number of complex assets. + config.assets.debug = true + + # Asset digests allow you to set far-future HTTP expiration dates on all assets, + # yet still be able to expire them through the digest params. + config.assets.digest = true + + # Adds additional error checking when serving assets at runtime. + # Checks for improperly declared sprockets dependencies. + # Raises helpful error messages. + config.assets.raise_runtime_errors = true + <%- end -%> + + # Raises error for missing translations + # config.action_view.raise_on_missing_translations = true +end diff --git a/railties/lib/rails/generators/rails/app/templates/config/environments/production.rb.tt b/railties/lib/rails/generators/rails/app/templates/config/environments/production.rb.tt new file mode 100644 index 0000000000..277fe01e89 --- /dev/null +++ b/railties/lib/rails/generators/rails/app/templates/config/environments/production.rb.tt @@ -0,0 +1,83 @@ +Rails.application.configure do + # Settings specified here will take precedence over those in config/application.rb. + + # Code is not reloaded between requests. + config.cache_classes = true + + # Eager load code on boot. This eager loads most of Rails and + # your application in memory, allowing both threaded web servers + # and those relying on copy on write to perform better. + # Rake tasks automatically ignore this option for performance. + config.eager_load = true + + # Full error reports are disabled and caching is turned on. + config.consider_all_requests_local = false + config.action_controller.perform_caching = true + + # Enable Rack::Cache to put a simple HTTP cache in front of your application + # Add `rack-cache` to your Gemfile before enabling this. + # For large-scale production use, consider using a caching reverse proxy like NGINX, varnish or squid. + # config.action_dispatch.rack_cache = true + + # Disable Rails's static asset server (Apache or NGINX will already do this). + config.serve_static_assets = false + + <%- unless options.skip_sprockets? -%> + # Compress JavaScripts and CSS. + config.assets.js_compressor = :uglifier + # config.assets.css_compressor = :sass + + # Do not fallback to assets pipeline if a precompiled asset is missed. + config.assets.compile = false + + # Asset digests allow you to set far-future HTTP expiration dates on all assets, + # yet still be able to expire them through the digest params. + config.assets.digest = true + + # `config.assets.precompile` and `config.assets.version` have moved to config/initializers/assets.rb + <%- end -%> + + # Specifies the header that your server uses for sending files. + # config.action_dispatch.x_sendfile_header = "X-Sendfile" # for Apache + # config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for NGINX + + # Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies. + # config.force_ssl = true + + # Set to :info to decrease the log volume. + config.log_level = :debug + + # Prepend all log lines with the following tags. + # config.log_tags = [ :subdomain, :uuid ] + + # Use a different logger for distributed setups. + # config.logger = ActiveSupport::TaggedLogging.new(SyslogLogger.new) + + # Use a different cache store in production. + # config.cache_store = :mem_cache_store + + # Enable serving of images, stylesheets, and JavaScripts from an asset server. + # config.action_controller.asset_host = "http://assets.example.com" + + # Ignore bad email addresses and do not raise email delivery errors. + # Set this to true and configure the email server for immediate delivery to raise delivery errors. + # config.action_mailer.raise_delivery_errors = false + + # Enable locale fallbacks for I18n (makes lookups for any locale fall back to + # the I18n.default_locale when a translation cannot be found). + config.i18n.fallbacks = true + + # Send deprecation notices to registered listeners. + config.active_support.deprecation = :notify + + # Disable automatic flushing of the log to improve performance. + # config.autoflush_log = false + + # Use default logging formatter so that PID and timestamp are not suppressed. + config.log_formatter = ::Logger::Formatter.new + <%- unless options.skip_active_record? -%> + + # Do not dump schema after migrations. + config.active_record.dump_schema_after_migration = false + <%- end -%> +end diff --git a/railties/lib/rails/generators/rails/app/templates/config/environments/test.rb.tt b/railties/lib/rails/generators/rails/app/templates/config/environments/test.rb.tt new file mode 100644 index 0000000000..053f5b66d7 --- /dev/null +++ b/railties/lib/rails/generators/rails/app/templates/config/environments/test.rb.tt @@ -0,0 +1,39 @@ +Rails.application.configure do + # Settings specified here will take precedence over those in config/application.rb. + + # The test environment is used exclusively to run your application's + # test suite. You never need to work with it otherwise. Remember that + # your test database is "scratch space" for the test suite and is wiped + # and recreated between test runs. Don't rely on the data there! + config.cache_classes = true + + # Do not eager load code on boot. This avoids loading your whole application + # just for the purpose of running a single test. If you are using a tool that + # preloads Rails for running tests, you may have to set it to true. + config.eager_load = false + + # Configure static asset server for tests with Cache-Control for performance. + config.serve_static_assets = true + config.static_cache_control = 'public, max-age=3600' + + # Show full error reports and disable caching. + config.consider_all_requests_local = true + config.action_controller.perform_caching = false + + # Raise exceptions instead of rendering exception templates. + config.action_dispatch.show_exceptions = false + + # Disable request forgery protection in test environment. + config.action_controller.allow_forgery_protection = false + + # Tell Action Mailer not to deliver emails to the real world. + # The :test delivery method accumulates sent emails in the + # ActionMailer::Base.deliveries array. + config.action_mailer.delivery_method = :test + + # Print deprecation notices to the stderr. + config.active_support.deprecation = :stderr + + # Raises error for missing translations + # config.action_view.raise_on_missing_translations = true +end diff --git a/railties/lib/rails/generators/rails/app/templates/config/initializers/assets.rb.tt b/railties/lib/rails/generators/rails/app/templates/config/initializers/assets.rb.tt new file mode 100644 index 0000000000..01ef3e6630 --- /dev/null +++ b/railties/lib/rails/generators/rails/app/templates/config/initializers/assets.rb.tt @@ -0,0 +1,11 @@ +# Be sure to restart your server when you modify this file. + +# Version of your assets, change this if you want to expire all your assets. +Rails.application.config.assets.version = '1.0' + +# Add additional assets to the asset load path +# Rails.application.config.assets.paths << Emoji.images_path + +# Precompile additional assets. +# application.js, application.css, and all non-JS/CSS in app/assets folder are already added. +# Rails.application.config.assets.precompile += %w( search.js ) diff --git a/railties/lib/rails/generators/rails/app/templates/config/initializers/backtrace_silencers.rb b/railties/lib/rails/generators/rails/app/templates/config/initializers/backtrace_silencers.rb new file mode 100644 index 0000000000..59385cdf37 --- /dev/null +++ b/railties/lib/rails/generators/rails/app/templates/config/initializers/backtrace_silencers.rb @@ -0,0 +1,7 @@ +# Be sure to restart your server when you modify this file. + +# You can add backtrace silencers for libraries that you're using but don't wish to see in your backtraces. +# Rails.backtrace_cleaner.add_silencer { |line| line =~ /my_noisy_library/ } + +# You can also remove all the silencers if you're trying to debug a problem that might stem from framework code. +# Rails.backtrace_cleaner.remove_silencers! diff --git a/railties/lib/rails/generators/rails/app/templates/config/initializers/cookies_serializer.rb b/railties/lib/rails/generators/rails/app/templates/config/initializers/cookies_serializer.rb new file mode 100644 index 0000000000..7f70458dee --- /dev/null +++ b/railties/lib/rails/generators/rails/app/templates/config/initializers/cookies_serializer.rb @@ -0,0 +1,3 @@ +# Be sure to restart your server when you modify this file. + +Rails.application.config.action_dispatch.cookies_serializer = :json diff --git a/railties/lib/rails/generators/rails/app/templates/config/initializers/filter_parameter_logging.rb b/railties/lib/rails/generators/rails/app/templates/config/initializers/filter_parameter_logging.rb new file mode 100644 index 0000000000..4a994e1e7b --- /dev/null +++ b/railties/lib/rails/generators/rails/app/templates/config/initializers/filter_parameter_logging.rb @@ -0,0 +1,4 @@ +# Be sure to restart your server when you modify this file. + +# Configure sensitive parameters which will be filtered from the log file. +Rails.application.config.filter_parameters += [:password] diff --git a/railties/lib/rails/generators/rails/app/templates/config/initializers/inflections.rb b/railties/lib/rails/generators/rails/app/templates/config/initializers/inflections.rb new file mode 100644 index 0000000000..ac033bf9dc --- /dev/null +++ b/railties/lib/rails/generators/rails/app/templates/config/initializers/inflections.rb @@ -0,0 +1,16 @@ +# Be sure to restart your server when you modify this file. + +# Add new inflection rules using the following format. Inflections +# are locale specific, and you may define rules for as many different +# locales as you wish. All of these examples are active by default: +# ActiveSupport::Inflector.inflections(:en) do |inflect| +# inflect.plural /^(ox)$/i, '\1en' +# inflect.singular /^(ox)en/i, '\1' +# inflect.irregular 'person', 'people' +# inflect.uncountable %w( fish sheep ) +# end + +# These inflection rules are supported but not enabled by default: +# ActiveSupport::Inflector.inflections(:en) do |inflect| +# inflect.acronym 'RESTful' +# end diff --git a/railties/lib/rails/generators/rails/app/templates/config/initializers/mime_types.rb b/railties/lib/rails/generators/rails/app/templates/config/initializers/mime_types.rb new file mode 100644 index 0000000000..dc1899682b --- /dev/null +++ b/railties/lib/rails/generators/rails/app/templates/config/initializers/mime_types.rb @@ -0,0 +1,4 @@ +# Be sure to restart your server when you modify this file. + +# Add new mime types for use in respond_to blocks: +# Mime::Type.register "text/richtext", :rtf diff --git a/railties/lib/rails/generators/rails/app/templates/config/initializers/session_store.rb.tt b/railties/lib/rails/generators/rails/app/templates/config/initializers/session_store.rb.tt new file mode 100644 index 0000000000..2bb9b82c61 --- /dev/null +++ b/railties/lib/rails/generators/rails/app/templates/config/initializers/session_store.rb.tt @@ -0,0 +1,3 @@ +# Be sure to restart your server when you modify this file. + +Rails.application.config.session_store :cookie_store, key: <%= "'_#{app_name}_session'" %> diff --git a/railties/lib/rails/generators/rails/app/templates/config/initializers/wrap_parameters.rb.tt b/railties/lib/rails/generators/rails/app/templates/config/initializers/wrap_parameters.rb.tt new file mode 100644 index 0000000000..f2110c2c70 --- /dev/null +++ b/railties/lib/rails/generators/rails/app/templates/config/initializers/wrap_parameters.rb.tt @@ -0,0 +1,16 @@ +# Be sure to restart your server when you modify this file. + +# This file contains settings for ActionController::ParamsWrapper which +# is enabled by default. + +# Enable parameter wrapping for JSON. You can disable this by setting :format to an empty array. +ActiveSupport.on_load(:action_controller) do + wrap_parameters format: [:json] if respond_to?(:wrap_parameters) +end +<%- unless options.skip_active_record? -%> + +# To enable root element in JSON for ActiveRecord objects. +# ActiveSupport.on_load(:active_record) do +# self.include_root_in_json = true +# end +<%- end -%> diff --git a/railties/lib/rails/generators/rails/app/templates/config/locales/en.yml b/railties/lib/rails/generators/rails/app/templates/config/locales/en.yml new file mode 100644 index 0000000000..0653957166 --- /dev/null +++ b/railties/lib/rails/generators/rails/app/templates/config/locales/en.yml @@ -0,0 +1,23 @@ +# Files in the config/locales directory are used for internationalization +# and are automatically loaded by Rails. If you want to use locales other +# than English, add the necessary files in this directory. +# +# To use the locales, use `I18n.t`: +# +# I18n.t 'hello' +# +# In views, this is aliased to just `t`: +# +# <%= t('hello') %> +# +# To use a different locale, set it with `I18n.locale`: +# +# I18n.locale = :es +# +# This would use the information in config/locales/es.yml. +# +# To learn more, please read the Rails Internationalization guide +# available at http://guides.rubyonrails.org/i18n.html. + +en: + hello: "Hello world" diff --git a/railties/lib/rails/generators/rails/app/templates/config/routes.rb b/railties/lib/rails/generators/rails/app/templates/config/routes.rb new file mode 100644 index 0000000000..3f66539d54 --- /dev/null +++ b/railties/lib/rails/generators/rails/app/templates/config/routes.rb @@ -0,0 +1,56 @@ +Rails.application.routes.draw do + # The priority is based upon order of creation: first created -> highest priority. + # See how all your routes lay out with "rake routes". + + # You can have the root of your site routed with "root" + # root 'welcome#index' + + # Example of regular route: + # get 'products/:id' => 'catalog#view' + + # Example of named route that can be invoked with purchase_url(id: product.id) + # get 'products/:id/purchase' => 'catalog#purchase', as: :purchase + + # Example resource route (maps HTTP verbs to controller actions automatically): + # resources :products + + # Example resource route with options: + # resources :products do + # member do + # get 'short' + # post 'toggle' + # end + # + # collection do + # get 'sold' + # end + # end + + # Example resource route with sub-resources: + # resources :products do + # resources :comments, :sales + # resource :seller + # end + + # Example resource route with more complex sub-resources: + # resources :products do + # resources :comments + # resources :sales do + # get 'recent', on: :collection + # end + # end + + # Example resource route with concerns: + # concern :toggleable do + # post 'toggle' + # end + # resources :posts, concerns: :toggleable + # resources :photos, concerns: :toggleable + + # Example resource route within a namespace: + # namespace :admin do + # # Directs /admin/products/* to Admin::ProductsController + # # (app/controllers/admin/products_controller.rb) + # resources :products + # end +end diff --git a/railties/lib/rails/generators/rails/app/templates/config/secrets.yml b/railties/lib/rails/generators/rails/app/templates/config/secrets.yml new file mode 100644 index 0000000000..b2669a0f79 --- /dev/null +++ b/railties/lib/rails/generators/rails/app/templates/config/secrets.yml @@ -0,0 +1,22 @@ +# Be sure to restart your server when you modify this file. + +# Your secret key is used for verifying the integrity of signed cookies. +# If you change this key, all old signed cookies will become invalid! + +# Make sure the secret is at least 30 characters and all random, +# no regular words or you'll be exposed to dictionary attacks. +# You can use `rake secret` to generate a secure secret key. + +# Make sure the secrets in this file are kept private +# if you're sharing your code publicly. + +development: + secret_key_base: <%= app_secret %> + +test: + secret_key_base: <%= app_secret %> + +# Do not keep production secrets in the repository, +# instead read values from the environment. +production: + secret_key_base: <%%= ENV["SECRET_KEY_BASE"] %> diff --git a/railties/lib/rails/generators/rails/app/templates/db/seeds.rb.tt b/railties/lib/rails/generators/rails/app/templates/db/seeds.rb.tt new file mode 100644 index 0000000000..4edb1e857e --- /dev/null +++ b/railties/lib/rails/generators/rails/app/templates/db/seeds.rb.tt @@ -0,0 +1,7 @@ +# This file should contain all the record creation needed to seed the database with its default values. +# The data can then be loaded with the rake db:seed (or created alongside the db with db:setup). +# +# Examples: +# +# cities = City.create([{ name: 'Chicago' }, { name: 'Copenhagen' }]) +# Mayor.create(name: 'Emanuel', city: cities.first) diff --git a/railties/lib/rails/generators/rails/app/templates/gitignore b/railties/lib/rails/generators/rails/app/templates/gitignore new file mode 100644 index 0000000000..8775e5e235 --- /dev/null +++ b/railties/lib/rails/generators/rails/app/templates/gitignore @@ -0,0 +1,18 @@ +# See https://help.github.com/articles/ignoring-files for more about ignoring files. +# +# If you find yourself ignoring temporary files generated by your text editor +# or operating system, you probably want to add a global ignore instead: +# git config --global core.excludesfile '~/.gitignore_global' + +# Ignore bundler config. +/.bundle + +<% if sqlite3? -%> +# Ignore the default SQLite database. +/db/*.sqlite3 +/db/*.sqlite3-journal + +<% end -%> +# Ignore all logfiles and tempfiles. +/log/*.log +/tmp diff --git a/railties/lib/rails/generators/rails/app/templates/public/404.html b/railties/lib/rails/generators/rails/app/templates/public/404.html new file mode 100644 index 0000000000..b612547fc2 --- /dev/null +++ b/railties/lib/rails/generators/rails/app/templates/public/404.html @@ -0,0 +1,67 @@ +<!DOCTYPE html> +<html> +<head> + <title>The page you were looking for doesn't exist (404)</title> + <meta name="viewport" content="width=device-width,initial-scale=1"> + <style> + body { + background-color: #EFEFEF; + color: #2E2F30; + text-align: center; + font-family: arial, sans-serif; + margin: 0; + } + + div.dialog { + width: 95%; + max-width: 33em; + margin: 4em auto 0; + } + + div.dialog > div { + border: 1px solid #CCC; + border-right-color: #999; + border-left-color: #999; + border-bottom-color: #BBB; + border-top: #B00100 solid 4px; + border-top-left-radius: 9px; + border-top-right-radius: 9px; + background-color: white; + padding: 7px 12% 0; + box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17); + } + + h1 { + font-size: 100%; + color: #730E15; + line-height: 1.5em; + } + + div.dialog > p { + margin: 0 0 1em; + padding: 1em; + background-color: #F7F7F7; + border: 1px solid #CCC; + border-right-color: #999; + border-left-color: #999; + border-bottom-color: #999; + border-bottom-left-radius: 4px; + border-bottom-right-radius: 4px; + border-top-color: #DADADA; + color: #666; + box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17); + } + </style> +</head> + +<body> + <!-- This file lives in public/404.html --> + <div class="dialog"> + <div> + <h1>The page you were looking for doesn't exist.</h1> + <p>You may have mistyped the address or the page may have moved.</p> + </div> + <p>If you are the application owner check the logs for more information.</p> + </div> +</body> +</html> diff --git a/railties/lib/rails/generators/rails/app/templates/public/422.html b/railties/lib/rails/generators/rails/app/templates/public/422.html new file mode 100644 index 0000000000..a21f82b3bd --- /dev/null +++ b/railties/lib/rails/generators/rails/app/templates/public/422.html @@ -0,0 +1,67 @@ +<!DOCTYPE html> +<html> +<head> + <title>The change you wanted was rejected (422)</title> + <meta name="viewport" content="width=device-width,initial-scale=1"> + <style> + body { + background-color: #EFEFEF; + color: #2E2F30; + text-align: center; + font-family: arial, sans-serif; + margin: 0; + } + + div.dialog { + width: 95%; + max-width: 33em; + margin: 4em auto 0; + } + + div.dialog > div { + border: 1px solid #CCC; + border-right-color: #999; + border-left-color: #999; + border-bottom-color: #BBB; + border-top: #B00100 solid 4px; + border-top-left-radius: 9px; + border-top-right-radius: 9px; + background-color: white; + padding: 7px 12% 0; + box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17); + } + + h1 { + font-size: 100%; + color: #730E15; + line-height: 1.5em; + } + + div.dialog > p { + margin: 0 0 1em; + padding: 1em; + background-color: #F7F7F7; + border: 1px solid #CCC; + border-right-color: #999; + border-left-color: #999; + border-bottom-color: #999; + border-bottom-left-radius: 4px; + border-bottom-right-radius: 4px; + border-top-color: #DADADA; + color: #666; + box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17); + } + </style> +</head> + +<body> + <!-- This file lives in public/422.html --> + <div class="dialog"> + <div> + <h1>The change you wanted was rejected.</h1> + <p>Maybe you tried to change something you didn't have access to.</p> + </div> + <p>If you are the application owner check the logs for more information.</p> + </div> +</body> +</html> diff --git a/railties/lib/rails/generators/rails/app/templates/public/500.html b/railties/lib/rails/generators/rails/app/templates/public/500.html new file mode 100644 index 0000000000..061abc587d --- /dev/null +++ b/railties/lib/rails/generators/rails/app/templates/public/500.html @@ -0,0 +1,66 @@ +<!DOCTYPE html> +<html> +<head> + <title>We're sorry, but something went wrong (500)</title> + <meta name="viewport" content="width=device-width,initial-scale=1"> + <style> + body { + background-color: #EFEFEF; + color: #2E2F30; + text-align: center; + font-family: arial, sans-serif; + margin: 0; + } + + div.dialog { + width: 95%; + max-width: 33em; + margin: 4em auto 0; + } + + div.dialog > div { + border: 1px solid #CCC; + border-right-color: #999; + border-left-color: #999; + border-bottom-color: #BBB; + border-top: #B00100 solid 4px; + border-top-left-radius: 9px; + border-top-right-radius: 9px; + background-color: white; + padding: 7px 12% 0; + box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17); + } + + h1 { + font-size: 100%; + color: #730E15; + line-height: 1.5em; + } + + div.dialog > p { + margin: 0 0 1em; + padding: 1em; + background-color: #F7F7F7; + border: 1px solid #CCC; + border-right-color: #999; + border-left-color: #999; + border-bottom-color: #999; + border-bottom-left-radius: 4px; + border-bottom-right-radius: 4px; + border-top-color: #DADADA; + color: #666; + box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17); + } + </style> +</head> + +<body> + <!-- This file lives in public/500.html --> + <div class="dialog"> + <div> + <h1>We're sorry, but something went wrong.</h1> + </div> + <p>If you are the application owner check the logs for more information.</p> + </div> +</body> +</html> diff --git a/railties/lib/rails/generators/rails/app/templates/public/favicon.ico b/railties/lib/rails/generators/rails/app/templates/public/favicon.ico new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/railties/lib/rails/generators/rails/app/templates/public/favicon.ico diff --git a/railties/lib/rails/generators/rails/app/templates/public/robots.txt b/railties/lib/rails/generators/rails/app/templates/public/robots.txt new file mode 100644 index 0000000000..3c9c7c01f3 --- /dev/null +++ b/railties/lib/rails/generators/rails/app/templates/public/robots.txt @@ -0,0 +1,5 @@ +# See http://www.robotstxt.org/robotstxt.html for documentation on how to use the robots.txt file +# +# To ban all spiders from the entire site uncomment the next two lines: +# User-agent: * +# Disallow: / diff --git a/railties/lib/rails/generators/rails/app/templates/test/test_helper.rb b/railties/lib/rails/generators/rails/app/templates/test/test_helper.rb new file mode 100644 index 0000000000..87b8fe3516 --- /dev/null +++ b/railties/lib/rails/generators/rails/app/templates/test/test_helper.rb @@ -0,0 +1,12 @@ +ENV['RAILS_ENV'] ||= 'test' +require File.expand_path('../../config/environment', __FILE__) +require 'rails/test_help' + +class ActiveSupport::TestCase +<% unless options[:skip_active_record] -%> + # Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order. + fixtures :all + +<% end -%> + # Add more helper methods to be used by all tests here... +end diff --git a/railties/lib/rails/generators/rails/assets/USAGE b/railties/lib/rails/generators/rails/assets/USAGE new file mode 100644 index 0000000000..d2e5ed4482 --- /dev/null +++ b/railties/lib/rails/generators/rails/assets/USAGE @@ -0,0 +1,20 @@ +Description: + Stubs out new asset placeholders. Pass the asset name, either CamelCased + or under_scored. + + To create an asset within a folder, specify the asset's name as a + path like 'parent/name'. + + This generates a JavaScript stub in app/assets/javascripts and a stylesheet + stub in app/assets/stylesheets. + + If CoffeeScript is available, JavaScripts will be generated with the .coffee extension. + If Sass 3 is available, stylesheets will be generated with the .scss extension. + +Example: + `rails generate assets posts` + + Posts assets. + JavaScript: app/assets/javascripts/posts.js + Stylesheet: app/assets/stylesheets/posts.css + diff --git a/railties/lib/rails/generators/rails/assets/assets_generator.rb b/railties/lib/rails/generators/rails/assets/assets_generator.rb new file mode 100644 index 0000000000..6f4b86e708 --- /dev/null +++ b/railties/lib/rails/generators/rails/assets/assets_generator.rb @@ -0,0 +1,25 @@ +module Rails + module Generators + class AssetsGenerator < NamedBase # :nodoc: + class_option :javascripts, type: :boolean, desc: "Generate JavaScripts" + class_option :stylesheets, type: :boolean, desc: "Generate Stylesheets" + + class_option :javascript_engine, desc: "Engine for JavaScripts" + class_option :stylesheet_engine, desc: "Engine for Stylesheets" + + protected + + def asset_name + file_name + end + + hook_for :javascript_engine do |javascript_engine| + invoke javascript_engine, [name] if options[:javascripts] + end + + hook_for :stylesheet_engine do |stylesheet_engine| + invoke stylesheet_engine, [name] if options[:stylesheets] + end + end + end +end diff --git a/railties/lib/rails/generators/rails/assets/templates/javascript.js b/railties/lib/rails/generators/rails/assets/templates/javascript.js new file mode 100644 index 0000000000..dee720facd --- /dev/null +++ b/railties/lib/rails/generators/rails/assets/templates/javascript.js @@ -0,0 +1,2 @@ +// Place all the behaviors and hooks related to the matching controller here. +// All this logic will automatically be available in application.js. diff --git a/railties/lib/rails/generators/rails/assets/templates/stylesheet.css b/railties/lib/rails/generators/rails/assets/templates/stylesheet.css new file mode 100644 index 0000000000..7594abf268 --- /dev/null +++ b/railties/lib/rails/generators/rails/assets/templates/stylesheet.css @@ -0,0 +1,4 @@ +/* + Place all the styles related to the matching controller here. + They will automatically be included in application.css. +*/ diff --git a/railties/lib/rails/generators/rails/controller/USAGE b/railties/lib/rails/generators/rails/controller/USAGE new file mode 100644 index 0000000000..64239ad599 --- /dev/null +++ b/railties/lib/rails/generators/rails/controller/USAGE @@ -0,0 +1,18 @@ +Description: + Stubs out a new controller and its views. Pass the controller name, either + CamelCased or under_scored, and a list of views as arguments. + + To create a controller within a module, specify the controller name as a + path like 'parent_module/controller_name'. + + This generates a controller class in app/controllers and invokes helper, + template engine, assets, and test framework generators. + +Example: + `rails generate controller CreditCards open debit credit close` + + CreditCards controller with URLs like /credit_cards/debit. + Controller: app/controllers/credit_cards_controller.rb + Test: test/controllers/credit_cards_controller_test.rb + Views: app/views/credit_cards/debit.html.erb [...] + Helper: app/helpers/credit_cards_helper.rb diff --git a/railties/lib/rails/generators/rails/controller/controller_generator.rb b/railties/lib/rails/generators/rails/controller/controller_generator.rb new file mode 100644 index 0000000000..fbecab1823 --- /dev/null +++ b/railties/lib/rails/generators/rails/controller/controller_generator.rb @@ -0,0 +1,58 @@ +module Rails + module Generators + class ControllerGenerator < NamedBase # :nodoc: + argument :actions, type: :array, default: [], banner: "action action" + class_option :skip_routes, type: :boolean, desc: "Dont' add routes to config/routes.rb." + + check_class_collision suffix: "Controller" + + def create_controller_files + template 'controller.rb', File.join('app/controllers', class_path, "#{file_name}_controller.rb") + end + + def add_routes + unless options[:skip_routes] + actions.reverse.each do |action| + route generate_routing_code(action) + end + end + end + + hook_for :template_engine, :test_framework, :helper, :assets + + private + + # This method creates nested route entry for namespaced resources. + # For eg. rails g controller foo/bar/baz index + # Will generate - + # namespace :foo do + # namespace :bar do + # get 'baz/index' + # end + # end + def generate_routing_code(action) + depth = regular_class_path.length + # Create 'namespace' ladder + # namespace :foo do + # namespace :bar do + namespace_ladder = regular_class_path.each_with_index.map do |ns, i| + indent("namespace :#{ns} do\n", i * 2) + end.join + + # Create route + # get 'baz/index' + route = indent(%{get '#{file_name}/#{action}'\n}, depth * 2) + + # Create `end` ladder + # end + # end + end_ladder = (1..depth).reverse_each.map do |i| + indent("end\n", i * 2) + end.join + + # Combine the 3 parts to generate complete route entry + namespace_ladder + route + end_ladder + end + end + end +end diff --git a/railties/lib/rails/generators/rails/controller/templates/controller.rb b/railties/lib/rails/generators/rails/controller/templates/controller.rb new file mode 100644 index 0000000000..633e0b3177 --- /dev/null +++ b/railties/lib/rails/generators/rails/controller/templates/controller.rb @@ -0,0 +1,13 @@ +<% if namespaced? -%> +require_dependency "<%= namespaced_path %>/application_controller" + +<% end -%> +<% module_namespacing do -%> +class <%= class_name %>Controller < ApplicationController +<% actions.each do |action| -%> + def <%= action %> + end +<%= "\n" unless action == actions.last -%> +<% end -%> +end +<% end -%> diff --git a/railties/lib/rails/generators/rails/generator/USAGE b/railties/lib/rails/generators/rails/generator/USAGE new file mode 100644 index 0000000000..799383050c --- /dev/null +++ b/railties/lib/rails/generators/rails/generator/USAGE @@ -0,0 +1,13 @@ +Description: + Stubs out a new generator at lib/generators. Pass the generator name as an argument, + either CamelCased or snake_cased. + +Example: + `rails generate generator Awesome` + + creates a standard awesome generator: + lib/generators/awesome/ + lib/generators/awesome/awesome_generator.rb + lib/generators/awesome/USAGE + lib/generators/awesome/templates/ + test/lib/generators/awesome_generator_test.rb diff --git a/railties/lib/rails/generators/rails/generator/generator_generator.rb b/railties/lib/rails/generators/rails/generator/generator_generator.rb new file mode 100644 index 0000000000..15d88f06ac --- /dev/null +++ b/railties/lib/rails/generators/rails/generator/generator_generator.rb @@ -0,0 +1,27 @@ +module Rails + module Generators + class GeneratorGenerator < NamedBase # :nodoc: + check_class_collision suffix: "Generator" + + class_option :namespace, type: :boolean, default: true, + desc: "Namespace generator under lib/generators/name" + + def create_generator_files + directory '.', generator_dir + end + + hook_for :test_framework + + protected + + def generator_dir + if options[:namespace] + File.join("lib", "generators", regular_class_path, file_name) + else + File.join("lib", "generators", regular_class_path) + end + end + + end + end +end diff --git a/railties/lib/rails/generators/rails/generator/templates/%file_name%_generator.rb.tt b/railties/lib/rails/generators/rails/generator/templates/%file_name%_generator.rb.tt new file mode 100644 index 0000000000..d0575772bc --- /dev/null +++ b/railties/lib/rails/generators/rails/generator/templates/%file_name%_generator.rb.tt @@ -0,0 +1,3 @@ +class <%= class_name %>Generator < Rails::Generators::NamedBase + source_root File.expand_path('../templates', __FILE__) +end diff --git a/railties/lib/rails/generators/rails/generator/templates/USAGE.tt b/railties/lib/rails/generators/rails/generator/templates/USAGE.tt new file mode 100644 index 0000000000..1bb8df840d --- /dev/null +++ b/railties/lib/rails/generators/rails/generator/templates/USAGE.tt @@ -0,0 +1,8 @@ +Description: + Explain the generator + +Example: + rails generate <%= file_name %> Thing + + This will create: + what/will/it/create diff --git a/railties/lib/rails/generators/rails/generator/templates/templates/.empty_directory b/railties/lib/rails/generators/rails/generator/templates/templates/.empty_directory new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/railties/lib/rails/generators/rails/generator/templates/templates/.empty_directory diff --git a/railties/lib/rails/generators/rails/helper/USAGE b/railties/lib/rails/generators/rails/helper/USAGE new file mode 100644 index 0000000000..8855ef3b01 --- /dev/null +++ b/railties/lib/rails/generators/rails/helper/USAGE @@ -0,0 +1,13 @@ +Description: + Stubs out a new helper. Pass the helper name, either CamelCased + or under_scored. + + To create a helper within a module, specify the helper name as a + path like 'parent_module/helper_name'. + +Example: + `rails generate helper CreditCard` + + Credit card helper. + Helper: app/helpers/credit_card_helper.rb + diff --git a/railties/lib/rails/generators/rails/helper/helper_generator.rb b/railties/lib/rails/generators/rails/helper/helper_generator.rb new file mode 100644 index 0000000000..5ff38e4111 --- /dev/null +++ b/railties/lib/rails/generators/rails/helper/helper_generator.rb @@ -0,0 +1,13 @@ +module Rails + module Generators + class HelperGenerator < NamedBase # :nodoc: + check_class_collision suffix: "Helper" + + def create_helper_files + template 'helper.rb', File.join('app/helpers', class_path, "#{file_name}_helper.rb") + end + + hook_for :test_framework + end + end +end diff --git a/railties/lib/rails/generators/rails/helper/templates/helper.rb b/railties/lib/rails/generators/rails/helper/templates/helper.rb new file mode 100644 index 0000000000..b4173151b4 --- /dev/null +++ b/railties/lib/rails/generators/rails/helper/templates/helper.rb @@ -0,0 +1,4 @@ +<% module_namespacing do -%> +module <%= class_name %>Helper +end +<% end -%> diff --git a/railties/lib/rails/generators/rails/integration_test/USAGE b/railties/lib/rails/generators/rails/integration_test/USAGE new file mode 100644 index 0000000000..57ee3543e6 --- /dev/null +++ b/railties/lib/rails/generators/rails/integration_test/USAGE @@ -0,0 +1,10 @@ +Description: + Stubs out a new integration test. Pass the name of the test, either + CamelCased or under_scored, as an argument. + + This generator invokes the current integration tool, which defaults to + TestUnit. + +Example: + `rails generate integration_test GeneralStories` creates a GeneralStories + integration test in test/integration/general_stories_test.rb diff --git a/railties/lib/rails/generators/rails/integration_test/integration_test_generator.rb b/railties/lib/rails/generators/rails/integration_test/integration_test_generator.rb new file mode 100644 index 0000000000..70770ddcb8 --- /dev/null +++ b/railties/lib/rails/generators/rails/integration_test/integration_test_generator.rb @@ -0,0 +1,7 @@ +module Rails + module Generators + class IntegrationTestGenerator < NamedBase # :nodoc: + hook_for :integration_tool, as: :integration + end + end +end diff --git a/railties/lib/rails/generators/rails/migration/USAGE b/railties/lib/rails/generators/rails/migration/USAGE new file mode 100644 index 0000000000..baf3d9894f --- /dev/null +++ b/railties/lib/rails/generators/rails/migration/USAGE @@ -0,0 +1,35 @@ +Description: + Stubs out a new database migration. Pass the migration name, either + CamelCased or under_scored, and an optional list of attribute pairs as arguments. + + A migration class is generated in db/migrate prefixed by a timestamp of the current date and time. + + You can name your migration in either of these formats to generate add/remove + column lines from supplied attributes: AddColumnsToTable or RemoveColumnsFromTable + +Example: + `rails generate migration AddSslFlag` + + If the current date is May 14, 2008 and the current time 09:09:12, this creates the AddSslFlag migration + db/migrate/20080514090912_add_ssl_flag.rb + + `rails generate migration AddTitleBodyToPost title:string body:text published:boolean` + + This will create the AddTitleBodyToPost in db/migrate/20080514090912_add_title_body_to_post.rb with this in the Change migration: + + add_column :posts, :title, :string + add_column :posts, :body, :text + add_column :posts, :published, :boolean + +Migration names containing JoinTable will generate join tables for use with +has_and_belongs_to_many associations. + +Example: + `rails g migration CreateMediaJoinTable artists musics:uniq` + + will create the migration + + create_join_table :artists, :musics do |t| + # t.index [:artist_id, :music_id] + t.index [:music_id, :artist_id], unique: true + end diff --git a/railties/lib/rails/generators/rails/migration/migration_generator.rb b/railties/lib/rails/generators/rails/migration/migration_generator.rb new file mode 100644 index 0000000000..965c42db36 --- /dev/null +++ b/railties/lib/rails/generators/rails/migration/migration_generator.rb @@ -0,0 +1,8 @@ +module Rails + module Generators + class MigrationGenerator < NamedBase # :nodoc: + argument :attributes, type: :array, default: [], banner: "field[:type][:index] field[:type][:index]" + hook_for :orm, required: true + end + end +end diff --git a/railties/lib/rails/generators/rails/model/USAGE b/railties/lib/rails/generators/rails/model/USAGE new file mode 100644 index 0000000000..2a6b8700e3 --- /dev/null +++ b/railties/lib/rails/generators/rails/model/USAGE @@ -0,0 +1,110 @@ +Description: + Stubs out a new model. Pass the model name, either CamelCased or + under_scored, and an optional list of attribute pairs as arguments. + + Attribute pairs are field:type arguments specifying the + model's attributes. Timestamps are added by default, so you don't have to + specify them by hand as 'created_at:datetime updated_at:datetime'. + + As a special case, specifying 'password:digest' will generate a + password_digest field of string type, and configure your generated model and + tests for use with ActiveModel has_secure_password (assuming the default ORM + and test framework are being used). + + You don't have to think up every attribute up front, but it helps to + sketch out a few so you can start working with the model immediately. + + This generator invokes your configured ORM and test framework, which + defaults to ActiveRecord and TestUnit. + + Finally, if --parent option is given, it's used as superclass of the + created model. This allows you create Single Table Inheritance models. + + If you pass a namespaced model name (e.g. admin/account or Admin::Account) + then the generator will create a module with a table_name_prefix method + to prefix the model's table name with the module name (e.g. admin_account) + +Available field types: + + Just after the field name you can specify a type like text or boolean. + It will generate the column with the associated SQL type. For instance: + + `rails generate model post title:string body:text` + + will generate a title column with a varchar type and a body column with a text + type. If no type is specified the string type will be used by default. + You can use the following types: + + integer + primary_key + decimal + float + boolean + binary + string + text + date + time + datetime + timestamp + + You can also consider `references` as a kind of type. For instance, if you run: + + `rails generate model photo title:string album:references` + + It will generate an `album_id` column. You should generate these kinds of fields when + you will use a `belongs_to` association, for instance. `references` also supports + polymorphism, you can enable polymorphism like this: + + `rails generate model product supplier:references{polymorphic}` + + For integer, string, text and binary fields, an integer in curly braces will + be set as the limit: + + `rails generate model user pseudo:string{30}` + + For decimal, two integers separated by a comma in curly braces will be used + for precision and scale: + + `rails generate model product 'price:decimal{10,2}'` + + You can add a `:uniq` or `:index` suffix for unique or standard indexes + respectively: + + `rails generate model user pseudo:string:uniq` + `rails generate model user pseudo:string:index` + + You can combine any single curly brace option with the index options: + + `rails generate model user username:string{30}:uniq` + `rails generate model product supplier:references{polymorphic}:index` + + If you require a `password_digest` string column for use with + has_secure_password, you should specify `password:digest`: + + `rails generate model user password:digest` + +Examples: + `rails generate model account` + + For ActiveRecord and TestUnit it creates: + + Model: app/models/account.rb + Test: test/models/account_test.rb + Fixtures: test/fixtures/accounts.yml + Migration: db/migrate/XXX_create_accounts.rb + + `rails generate model post title:string body:text published:boolean` + + Creates a Post model with a string title, text body, and published flag. + + `rails generate model admin/account` + + For ActiveRecord and TestUnit it creates: + + Module: app/models/admin.rb + Model: app/models/admin/account.rb + Test: test/models/admin/account_test.rb + Fixtures: test/fixtures/admin/accounts.yml + Migration: db/migrate/XXX_create_admin_accounts.rb + diff --git a/railties/lib/rails/generators/rails/model/model_generator.rb b/railties/lib/rails/generators/rails/model/model_generator.rb new file mode 100644 index 0000000000..87bab129bb --- /dev/null +++ b/railties/lib/rails/generators/rails/model/model_generator.rb @@ -0,0 +1,12 @@ +require 'rails/generators/model_helpers' + +module Rails + module Generators + class ModelGenerator < NamedBase # :nodoc: + include Rails::Generators::ModelHelpers + + argument :attributes, type: :array, default: [], banner: "field[:type][:index] field[:type][:index]" + hook_for :orm, required: true + end + end +end diff --git a/railties/lib/rails/generators/rails/plugin/USAGE b/railties/lib/rails/generators/rails/plugin/USAGE new file mode 100644 index 0000000000..9a7bf9f396 --- /dev/null +++ b/railties/lib/rails/generators/rails/plugin/USAGE @@ -0,0 +1,10 @@ +Description: + The 'rails plugin new' command creates a skeleton for developing any + kind of Rails extension with ability to run tests using dummy Rails + application. + +Example: + rails plugin new ~/Code/Ruby/blog + + This generates a skeletal Rails plugin in ~/Code/Ruby/blog. + See the README in the newly created plugin to get going. diff --git a/railties/lib/rails/generators/rails/plugin/plugin_generator.rb b/railties/lib/rails/generators/rails/plugin/plugin_generator.rb new file mode 100644 index 0000000000..584f776c01 --- /dev/null +++ b/railties/lib/rails/generators/rails/plugin/plugin_generator.rb @@ -0,0 +1,393 @@ +require 'active_support/core_ext/hash/slice' +require "rails/generators/rails/app/app_generator" +require 'date' + +module Rails + # The plugin builder allows you to override elements of the plugin + # generator without being forced to reverse the operations of the default + # generator. + # + # This allows you to override entire operations, like the creation of the + # Gemfile, README, or JavaScript files, without needing to know exactly + # what those operations do so you can create another template action. + class PluginBuilder + def rakefile + template "Rakefile" + end + + def app + if mountable? + directory 'app' + empty_directory_with_keep_file "app/assets/images/#{name}" + elsif full? + empty_directory_with_keep_file 'app/models' + empty_directory_with_keep_file 'app/controllers' + empty_directory_with_keep_file 'app/views' + empty_directory_with_keep_file 'app/helpers' + empty_directory_with_keep_file 'app/mailers' + empty_directory_with_keep_file "app/assets/images/#{name}" + end + end + + def readme + template "README.rdoc" + end + + def gemfile + template "Gemfile" + end + + def license + template "MIT-LICENSE" + end + + def gemspec + template "%name%.gemspec" + end + + def gitignore + template "gitignore", ".gitignore" + end + + def lib + template "lib/%name%.rb" + template "lib/tasks/%name%_tasks.rake" + template "lib/%name%/version.rb" + template "lib/%name%/engine.rb" if engine? + end + + def config + template "config/routes.rb" if engine? + end + + def test + template "test/test_helper.rb" + template "test/%name%_test.rb" + append_file "Rakefile", <<-EOF +#{rakefile_test_tasks} + +task default: :test + EOF + if engine? + template "test/integration/navigation_test.rb" + end + end + + PASSTHROUGH_OPTIONS = [ + :skip_active_record, :skip_javascript, :database, :javascript, :quiet, :pretend, :force, :skip + ] + + def generate_test_dummy(force = false) + opts = (options || {}).slice(*PASSTHROUGH_OPTIONS) + opts[:force] = force + opts[:skip_bundle] = true + + invoke Rails::Generators::AppGenerator, + [ File.expand_path(dummy_path, destination_root) ], opts + end + + def test_dummy_config + template "rails/boot.rb", "#{dummy_path}/config/boot.rb", force: true + template "rails/application.rb", "#{dummy_path}/config/application.rb", force: true + if mountable? + template "rails/routes.rb", "#{dummy_path}/config/routes.rb", force: true + end + end + + def test_dummy_assets + template "rails/javascripts.js", "#{dummy_path}/app/assets/javascripts/application.js", force: true + template "rails/stylesheets.css", "#{dummy_path}/app/assets/stylesheets/application.css", force: true + end + + def test_dummy_clean + inside dummy_path do + remove_file ".gitignore" + remove_file "db/seeds.rb" + remove_file "doc" + remove_file "Gemfile" + remove_file "lib/tasks" + remove_file "public/robots.txt" + remove_file "README" + remove_file "test" + remove_file "vendor" + end + end + + def stylesheets + if mountable? + copy_file "rails/stylesheets.css", + "app/assets/stylesheets/#{name}/application.css" + elsif full? + empty_directory_with_keep_file "app/assets/stylesheets/#{name}" + end + end + + def javascripts + return if options.skip_javascript? + + if mountable? + template "rails/javascripts.js", + "app/assets/javascripts/#{name}/application.js" + elsif full? + empty_directory_with_keep_file "app/assets/javascripts/#{name}" + end + end + + def bin(force = false) + return unless engine? + + directory "bin", force: force do |content| + "#{shebang}\n" + content + end + chmod "bin", 0755, verbose: false + end + + def gemfile_entry + return unless inside_application? + + gemfile_in_app_path = File.join(rails_app_path, "Gemfile") + if File.exist? gemfile_in_app_path + entry = "gem '#{name}', path: '#{relative_path}'" + append_file gemfile_in_app_path, entry + end + end + end + + module Generators + class PluginGenerator < AppBase # :nodoc: + add_shared_options_for "plugin" + + alias_method :plugin_path, :app_path + + class_option :dummy_path, type: :string, default: "test/dummy", + desc: "Create dummy application at given path" + + class_option :full, type: :boolean, default: false, + desc: "Generate a rails engine with bundled Rails application for testing" + + class_option :mountable, type: :boolean, default: false, + desc: "Generate mountable isolated application" + + class_option :skip_gemspec, type: :boolean, default: false, + desc: "Skip gemspec file" + + class_option :skip_gemfile_entry, type: :boolean, default: false, + desc: "If creating plugin in application's directory " + + "skip adding entry to Gemfile" + + def initialize(*args) + @dummy_path = nil + super + + unless plugin_path + raise Error, "Plugin name should be provided in arguments. For details run: rails plugin new --help" + end + end + + public_task :set_default_accessors! + public_task :create_root + + def create_root_files + build(:readme) + build(:rakefile) + build(:gemspec) unless options[:skip_gemspec] + build(:license) + build(:gitignore) unless options[:skip_git] + build(:gemfile) unless options[:skip_gemfile] + end + + def create_app_files + build(:app) + end + + def create_config_files + build(:config) + end + + def create_lib_files + build(:lib) + end + + def create_public_stylesheets_files + build(:stylesheets) + end + + def create_javascript_files + build(:javascripts) + end + + def create_images_directory + build(:images) + end + + def create_bin_files + build(:bin) + end + + def create_test_files + build(:test) unless options[:skip_test_unit] + end + + def create_test_dummy_files + return unless with_dummy_app? + create_dummy_app + end + + def update_gemfile + build(:gemfile_entry) unless options[:skip_gemfile_entry] + end + + def finish_template + build(:leftovers) + end + + public_task :apply_rails_template, :run_bundle + + def name + @name ||= begin + # same as ActiveSupport::Inflector#underscore except not replacing '-' + underscored = original_name.dup + underscored.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2') + underscored.gsub!(/([a-z\d])([A-Z])/,'\1_\2') + underscored.downcase! + + underscored + end + end + + protected + + def app_templates_dir + "../../app/templates" + end + + def create_dummy_app(path = nil) + dummy_path(path) if path + + say_status :vendor_app, dummy_path + mute do + build(:generate_test_dummy) + store_application_definition! + build(:test_dummy_config) + build(:test_dummy_assets) + build(:test_dummy_clean) + # ensure that bin/rails has proper dummy_path + build(:bin, true) + end + end + + def engine? + full? || mountable? + end + + def full? + options[:full] + end + + def mountable? + options[:mountable] + end + + def skip_git? + options[:skip_git] + end + + def with_dummy_app? + options[:skip_test_unit].blank? || options[:dummy_path] != 'test/dummy' + end + + def self.banner + "rails plugin new #{self.arguments.map(&:usage).join(' ')} [options]" + end + + def original_name + @original_name ||= File.basename(destination_root) + end + + def camelized + @camelized ||= name.gsub(/\W/, '_').squeeze('_').camelize + end + + def author + default = "TODO: Write your name" + if skip_git? + @author = default + else + @author = `git config user.name`.chomp rescue default + end + end + + def email + default = "TODO: Write your email address" + if skip_git? + @email = default + else + @email = `git config user.email`.chomp rescue default + end + end + + def valid_const? + if original_name =~ /[^0-9a-zA-Z_]+/ + raise Error, "Invalid plugin name #{original_name}. Please give a name which use only alphabetic or numeric or \"_\" characters." + elsif camelized =~ /^\d/ + raise Error, "Invalid plugin name #{original_name}. Please give a name which does not start with numbers." + elsif RESERVED_NAMES.include?(name) + raise Error, "Invalid plugin name #{original_name}. Please give a name which does not match one of the reserved rails words." + elsif Object.const_defined?(camelized) + raise Error, "Invalid plugin name #{original_name}, constant #{camelized} is already in use. Please choose another plugin name." + end + end + + def application_definition + @application_definition ||= begin + + dummy_application_path = File.expand_path("#{dummy_path}/config/application.rb", destination_root) + unless options[:pretend] || !File.exist?(dummy_application_path) + contents = File.read(dummy_application_path) + contents[(contents.index(/module ([\w]+)\n(.*)class Application/m))..-1] + end + end + end + alias :store_application_definition! :application_definition + + def get_builder_class + defined?(::PluginBuilder) ? ::PluginBuilder : Rails::PluginBuilder + end + + def rakefile_test_tasks + <<-RUBY +require 'rake/testtask' + +Rake::TestTask.new(:test) do |t| + t.libs << 'lib' + t.libs << 'test' + t.pattern = 'test/**/*_test.rb' + t.verbose = false +end + RUBY + end + + def dummy_path(path = nil) + @dummy_path = path if path + @dummy_path || options[:dummy_path] + end + + def mute(&block) + shell.mute(&block) + end + + def rails_app_path + APP_PATH.sub("/config/application", "") if defined?(APP_PATH) + end + + def inside_application? + rails_app_path && app_path =~ /^#{rails_app_path}/ + end + + def relative_path + return unless inside_application? + app_path.sub(/^#{rails_app_path}\//, '') + end + end + end +end diff --git a/railties/lib/rails/generators/rails/plugin/templates/%name%.gemspec b/railties/lib/rails/generators/rails/plugin/templates/%name%.gemspec new file mode 100644 index 0000000000..919c349470 --- /dev/null +++ b/railties/lib/rails/generators/rails/plugin/templates/%name%.gemspec @@ -0,0 +1,27 @@ +$:.push File.expand_path("../lib", __FILE__) + +# Maintain your gem's version: +require "<%= name %>/version" + +# Describe your gem and declare its dependencies: +Gem::Specification.new do |s| + s.name = "<%= name %>" + s.version = <%= camelized %>::VERSION + s.authors = ["<%= author %>"] + s.email = ["<%= email %>"] + s.homepage = "TODO" + s.summary = "TODO: Summary of <%= camelized %>." + s.description = "TODO: Description of <%= camelized %>." + s.license = "MIT" + + s.files = Dir["{app,config,db,lib}/**/*", "MIT-LICENSE", "Rakefile", "README.rdoc"] +<% unless options.skip_test_unit? -%> + s.test_files = Dir["test/**/*"] +<% end -%> + + <%= '# ' if options.dev? || options.edge? -%>s.add_dependency "rails", "~> <%= Rails::VERSION::STRING %>" +<% unless options[:skip_active_record] -%> + + s.add_development_dependency "<%= gem_for_database %>" +<% end -%> +end diff --git a/railties/lib/rails/generators/rails/plugin/templates/Gemfile b/railties/lib/rails/generators/rails/plugin/templates/Gemfile new file mode 100644 index 0000000000..35ad9fbf9e --- /dev/null +++ b/railties/lib/rails/generators/rails/plugin/templates/Gemfile @@ -0,0 +1,47 @@ +source 'https://rubygems.org' + +<% if options[:skip_gemspec] -%> +<%= '# ' if options.dev? || options.edge? -%>gem 'rails', '~> <%= Rails::VERSION::STRING %>' +<% else -%> +# Declare your gem's dependencies in <%= name %>.gemspec. +# Bundler will treat runtime dependencies like base dependencies, and +# development dependencies will be added by default to the :development group. +gemspec +<% end -%> + +<% if options[:skip_gemspec] -%> +group :development do + gem '<%= gem_for_database %>' +end +<% else -%> +# Declare any dependencies that are still in development here instead of in +# your gemspec. These might include edge Rails or gems from your path or +# Git. Remember to move these dependencies to your gemspec before releasing +# your gem to rubygems.org. +<% end -%> + +<% if options.dev? || options.edge? -%> +# Your gem is dependent on dev or edge Rails. Once you can lock this +# dependency down to a specific version, move it to your gemspec. +<% max_width = gemfile_entries.map { |g| g.name.length }.max -%> +<% gemfile_entries.each do |gem| -%> +<% if gem.comment -%> + +# <%= gem.comment %> +<% end -%> +<%= gem.commented_out ? '# ' : '' %>gem '<%= gem.name %>'<%= %(, '#{gem.version}') if gem.version -%> +<% if gem.options.any? -%> +, <%= gem.options.map { |k,v| + "#{k}: #{v.inspect}" }.join(', ') %> +<% end -%> +<% end -%> + +<% end -%> +<% unless defined?(JRUBY_VERSION) -%> +# To use a debugger + <%- if RUBY_VERSION < '2.0.0' -%> +# gem 'debugger', group: [:development, :test] + <%- else -%> +# gem 'byebug', group: [:development, :test] + <%- end -%> +<% end -%> diff --git a/railties/lib/rails/generators/rails/plugin/templates/MIT-LICENSE b/railties/lib/rails/generators/rails/plugin/templates/MIT-LICENSE new file mode 100644 index 0000000000..ff2fb3ba4e --- /dev/null +++ b/railties/lib/rails/generators/rails/plugin/templates/MIT-LICENSE @@ -0,0 +1,20 @@ +Copyright <%= Date.today.year %> <%= author %> + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/railties/lib/rails/generators/rails/plugin/templates/README.rdoc b/railties/lib/rails/generators/rails/plugin/templates/README.rdoc new file mode 100644 index 0000000000..301d647731 --- /dev/null +++ b/railties/lib/rails/generators/rails/plugin/templates/README.rdoc @@ -0,0 +1,3 @@ += <%= camelized %> + +This project rocks and uses MIT-LICENSE.
\ No newline at end of file diff --git a/railties/lib/rails/generators/rails/plugin/templates/Rakefile b/railties/lib/rails/generators/rails/plugin/templates/Rakefile new file mode 100644 index 0000000000..c338a0bdb1 --- /dev/null +++ b/railties/lib/rails/generators/rails/plugin/templates/Rakefile @@ -0,0 +1,29 @@ +begin + require 'bundler/setup' +rescue LoadError + puts 'You must `gem install bundler` and `bundle install` to run rake tasks' +end + +require 'rdoc/task' + +RDoc::Task.new(:rdoc) do |rdoc| + rdoc.rdoc_dir = 'rdoc' + rdoc.title = '<%= camelized %>' + rdoc.options << '--line-numbers' + rdoc.rdoc_files.include('README.rdoc') + rdoc.rdoc_files.include('lib/**/*.rb') +end + +<% if engine? && !options[:skip_active_record] && with_dummy_app? -%> +APP_RAKEFILE = File.expand_path("../<%= dummy_path -%>/Rakefile", __FILE__) +load 'rails/tasks/engine.rake' +<% end %> + +<% if engine? -%> +load 'rails/tasks/statistics.rake' +<% end %> + +<% unless options[:skip_gemspec] -%> + +Bundler::GemHelper.install_tasks +<% end %> diff --git a/railties/lib/rails/generators/rails/plugin/templates/app/controllers/%name%/application_controller.rb.tt b/railties/lib/rails/generators/rails/plugin/templates/app/controllers/%name%/application_controller.rb.tt new file mode 100644 index 0000000000..448ad7f989 --- /dev/null +++ b/railties/lib/rails/generators/rails/plugin/templates/app/controllers/%name%/application_controller.rb.tt @@ -0,0 +1,4 @@ +module <%= camelized %> + class ApplicationController < ActionController::Base + end +end diff --git a/railties/lib/rails/generators/rails/plugin/templates/app/helpers/%name%/application_helper.rb.tt b/railties/lib/rails/generators/rails/plugin/templates/app/helpers/%name%/application_helper.rb.tt new file mode 100644 index 0000000000..40ae9f52c2 --- /dev/null +++ b/railties/lib/rails/generators/rails/plugin/templates/app/helpers/%name%/application_helper.rb.tt @@ -0,0 +1,4 @@ +module <%= camelized %> + module ApplicationHelper + end +end diff --git a/railties/lib/rails/generators/rails/plugin/templates/app/mailers/.empty_directory b/railties/lib/rails/generators/rails/plugin/templates/app/mailers/.empty_directory new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/railties/lib/rails/generators/rails/plugin/templates/app/mailers/.empty_directory diff --git a/railties/lib/rails/generators/rails/plugin/templates/app/models/.empty_directory b/railties/lib/rails/generators/rails/plugin/templates/app/models/.empty_directory new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/railties/lib/rails/generators/rails/plugin/templates/app/models/.empty_directory diff --git a/railties/lib/rails/generators/rails/plugin/templates/app/views/layouts/%name%/application.html.erb.tt b/railties/lib/rails/generators/rails/plugin/templates/app/views/layouts/%name%/application.html.erb.tt new file mode 100644 index 0000000000..1d380420b4 --- /dev/null +++ b/railties/lib/rails/generators/rails/plugin/templates/app/views/layouts/%name%/application.html.erb.tt @@ -0,0 +1,14 @@ +<!DOCTYPE html> +<html> +<head> + <title><%= camelized %></title> + <%%= stylesheet_link_tag "<%= name %>/application", media: "all" %> + <%%= javascript_include_tag "<%= name %>/application" %> + <%%= csrf_meta_tags %> +</head> +<body> + +<%%= yield %> + +</body> +</html> diff --git a/railties/lib/rails/generators/rails/plugin/templates/bin/rails.tt b/railties/lib/rails/generators/rails/plugin/templates/bin/rails.tt new file mode 100644 index 0000000000..c3314d7e68 --- /dev/null +++ b/railties/lib/rails/generators/rails/plugin/templates/bin/rails.tt @@ -0,0 +1,11 @@ +# This command will automatically be run when you run "rails" with Rails 4 gems installed from the root of your application. + +ENGINE_ROOT = File.expand_path('../..', __FILE__) +ENGINE_PATH = File.expand_path('../../lib/<%= name -%>/engine', __FILE__) + +# Set up gems listed in the Gemfile. +ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__) +require 'bundler/setup' if File.exist?(ENV['BUNDLE_GEMFILE']) + +require 'rails/all' +require 'rails/engine/commands' diff --git a/railties/lib/rails/generators/rails/plugin/templates/config/routes.rb b/railties/lib/rails/generators/rails/plugin/templates/config/routes.rb new file mode 100644 index 0000000000..8e158d5831 --- /dev/null +++ b/railties/lib/rails/generators/rails/plugin/templates/config/routes.rb @@ -0,0 +1,6 @@ +<% if mountable? -%> +<%= camelized %>::Engine.routes.draw do +<% else -%> +Rails.application.routes.draw do +<% end -%> +end diff --git a/railties/lib/rails/generators/rails/plugin/templates/gitignore b/railties/lib/rails/generators/rails/plugin/templates/gitignore new file mode 100644 index 0000000000..086d87818a --- /dev/null +++ b/railties/lib/rails/generators/rails/plugin/templates/gitignore @@ -0,0 +1,10 @@ +.bundle/ +log/*.log +pkg/ +<% unless options[:skip_test_unit] && options[:dummy_path] == 'test/dummy' -%> +<%= dummy_path %>/db/*.sqlite3 +<%= dummy_path %>/db/*.sqlite3-journal +<%= dummy_path %>/log/*.log +<%= dummy_path %>/tmp/ +<%= dummy_path %>/.sass-cache +<% end -%>
\ No newline at end of file diff --git a/railties/lib/rails/generators/rails/plugin/templates/lib/%name%.rb b/railties/lib/rails/generators/rails/plugin/templates/lib/%name%.rb new file mode 100644 index 0000000000..40c074cced --- /dev/null +++ b/railties/lib/rails/generators/rails/plugin/templates/lib/%name%.rb @@ -0,0 +1,6 @@ +<% if engine? -%> +require "<%= name %>/engine" + +<% end -%> +module <%= camelized %> +end diff --git a/railties/lib/rails/generators/rails/plugin/templates/lib/%name%/engine.rb b/railties/lib/rails/generators/rails/plugin/templates/lib/%name%/engine.rb new file mode 100644 index 0000000000..967668fe66 --- /dev/null +++ b/railties/lib/rails/generators/rails/plugin/templates/lib/%name%/engine.rb @@ -0,0 +1,7 @@ +module <%= camelized %> + class Engine < ::Rails::Engine +<% if mountable? -%> + isolate_namespace <%= camelized %> +<% end -%> + end +end diff --git a/railties/lib/rails/generators/rails/plugin/templates/lib/%name%/version.rb b/railties/lib/rails/generators/rails/plugin/templates/lib/%name%/version.rb new file mode 100644 index 0000000000..ef07ef2e19 --- /dev/null +++ b/railties/lib/rails/generators/rails/plugin/templates/lib/%name%/version.rb @@ -0,0 +1,3 @@ +module <%= camelized %> + VERSION = "0.0.1" +end diff --git a/railties/lib/rails/generators/rails/plugin/templates/lib/tasks/%name%_tasks.rake b/railties/lib/rails/generators/rails/plugin/templates/lib/tasks/%name%_tasks.rake new file mode 100644 index 0000000000..7121f5ae23 --- /dev/null +++ b/railties/lib/rails/generators/rails/plugin/templates/lib/tasks/%name%_tasks.rake @@ -0,0 +1,4 @@ +# desc "Explaining what the task does" +# task :<%= name %> do +# # Task goes here +# end diff --git a/railties/lib/rails/generators/rails/plugin/templates/rails/application.rb b/railties/lib/rails/generators/rails/plugin/templates/rails/application.rb new file mode 100644 index 0000000000..5508829f6b --- /dev/null +++ b/railties/lib/rails/generators/rails/plugin/templates/rails/application.rb @@ -0,0 +1,18 @@ +require File.expand_path('../boot', __FILE__) + +<% if include_all_railties? -%> +require 'rails/all' +<% else -%> +# Pick the frameworks you want: +<%= comment_if :skip_active_record %>require "active_record/railtie" +require "action_controller/railtie" +require "action_mailer/railtie" +<%= comment_if :skip_action_view %>require "action_view/railtie" +<%= comment_if :skip_sprockets %>require "sprockets/railtie" +<%= comment_if :skip_test_unit %>require "rails/test_unit/railtie" +<% end -%> + +Bundler.require(*Rails.groups) +require "<%= name %>" + +<%= application_definition %> diff --git a/railties/lib/rails/generators/rails/plugin/templates/rails/boot.rb b/railties/lib/rails/generators/rails/plugin/templates/rails/boot.rb new file mode 100644 index 0000000000..6266cfc509 --- /dev/null +++ b/railties/lib/rails/generators/rails/plugin/templates/rails/boot.rb @@ -0,0 +1,5 @@ +# Set up gems listed in the Gemfile. +ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../../../Gemfile', __FILE__) + +require 'bundler/setup' if File.exist?(ENV['BUNDLE_GEMFILE']) +$LOAD_PATH.unshift File.expand_path('../../../../lib', __FILE__) diff --git a/railties/lib/rails/generators/rails/plugin/templates/rails/javascripts.js b/railties/lib/rails/generators/rails/plugin/templates/rails/javascripts.js new file mode 100644 index 0000000000..5bc2e1c8b5 --- /dev/null +++ b/railties/lib/rails/generators/rails/plugin/templates/rails/javascripts.js @@ -0,0 +1,13 @@ +// This is a manifest file that'll be compiled into application.js, which will include all the files +// listed below. +// +// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts, +// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path. +// +// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the +// compiled file. +// +// Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details +// about supported directives. +// +//= require_tree . diff --git a/railties/lib/rails/generators/rails/plugin/templates/rails/routes.rb b/railties/lib/rails/generators/rails/plugin/templates/rails/routes.rb new file mode 100644 index 0000000000..730ee31c3d --- /dev/null +++ b/railties/lib/rails/generators/rails/plugin/templates/rails/routes.rb @@ -0,0 +1,4 @@ +Rails.application.routes.draw do + + mount <%= camelized %>::Engine => "/<%= name %>" +end diff --git a/railties/lib/rails/generators/rails/plugin/templates/rails/stylesheets.css b/railties/lib/rails/generators/rails/plugin/templates/rails/stylesheets.css new file mode 100644 index 0000000000..a443db3401 --- /dev/null +++ b/railties/lib/rails/generators/rails/plugin/templates/rails/stylesheets.css @@ -0,0 +1,15 @@ +/* + * This is a manifest file that'll be compiled into application.css, which will include all the files + * listed below. + * + * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets, + * or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path. + * + * You're free to add application-wide styles to this file and they'll appear at the bottom of the + * compiled file so the styles you add here take precedence over styles defined in any styles + * defined in the other CSS/SCSS files in this directory. It is generally better to create a new + * file per style scope. + * + *= require_tree . + *= require_self + */ diff --git a/railties/lib/rails/generators/rails/plugin/templates/test/%name%_test.rb b/railties/lib/rails/generators/rails/plugin/templates/test/%name%_test.rb new file mode 100644 index 0000000000..0a8bbd4aaf --- /dev/null +++ b/railties/lib/rails/generators/rails/plugin/templates/test/%name%_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class <%= camelized %>Test < ActiveSupport::TestCase + test "truth" do + assert_kind_of Module, <%= camelized %> + end +end diff --git a/railties/lib/rails/generators/rails/plugin/templates/test/integration/navigation_test.rb b/railties/lib/rails/generators/rails/plugin/templates/test/integration/navigation_test.rb new file mode 100644 index 0000000000..824caecb24 --- /dev/null +++ b/railties/lib/rails/generators/rails/plugin/templates/test/integration/navigation_test.rb @@ -0,0 +1,12 @@ +require 'test_helper' + +class NavigationTest < ActionDispatch::IntegrationTest +<% unless options[:skip_active_record] -%> + fixtures :all +<% end -%> + + # test "the truth" do + # assert true + # end +end + diff --git a/railties/lib/rails/generators/rails/plugin/templates/test/test_helper.rb b/railties/lib/rails/generators/rails/plugin/templates/test/test_helper.rb new file mode 100644 index 0000000000..1e26a313cd --- /dev/null +++ b/railties/lib/rails/generators/rails/plugin/templates/test/test_helper.rb @@ -0,0 +1,15 @@ +# Configure Rails Environment +ENV["RAILS_ENV"] = "test" + +require File.expand_path("../dummy/config/environment.rb", __FILE__) +require "rails/test_help" + +Rails.backtrace_cleaner.remove_silencers! + +# Load support files +Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f } + +# Load fixtures from the engine +if ActiveSupport::TestCase.method_defined?(:fixture_path=) + ActiveSupport::TestCase.fixture_path = File.expand_path("../fixtures", __FILE__) +end diff --git a/railties/lib/rails/generators/rails/resource/USAGE b/railties/lib/rails/generators/rails/resource/USAGE new file mode 100644 index 0000000000..e359cd574f --- /dev/null +++ b/railties/lib/rails/generators/rails/resource/USAGE @@ -0,0 +1,23 @@ +Description: + Stubs out a new resource including an empty model and controller suitable + for a restful, resource-oriented application. Pass the singular model name, + either CamelCased or under_scored, as the first argument, and an optional + list of attribute pairs. + + Attribute pairs are field:type arguments specifying the + model's attributes. Timestamps are added by default, so you don't have to + specify them by hand as 'created_at:datetime updated_at:datetime'. + + You don't have to think up every attribute up front, but it helps to + sketch out a few so you can start working with the model immediately. + + This generator invokes your configured ORM and test framework, besides + creating helpers and add routes to config/routes.rb. + + Unlike the scaffold generator, the resource generator does not create + views or add any methods to the generated controller. + +Examples: + `rails generate resource post` # no attributes + `rails generate resource post title:string body:text published:boolean` + `rails generate resource purchase order_id:integer amount:decimal` diff --git a/railties/lib/rails/generators/rails/resource/resource_generator.rb b/railties/lib/rails/generators/rails/resource/resource_generator.rb new file mode 100644 index 0000000000..8014feb75f --- /dev/null +++ b/railties/lib/rails/generators/rails/resource/resource_generator.rb @@ -0,0 +1,20 @@ +require 'rails/generators/resource_helpers' +require 'rails/generators/rails/model/model_generator' +require 'active_support/core_ext/object/blank' + +module Rails + module Generators + class ResourceGenerator < ModelGenerator # :nodoc: + include ResourceHelpers + + hook_for :resource_controller, required: true do |controller| + invoke controller, [ controller_name, options[:actions] ] + end + + class_option :actions, type: :array, banner: "ACTION ACTION", default: [], + desc: "Actions for the resource controller" + + hook_for :resource_route, required: true + end + end +end diff --git a/railties/lib/rails/generators/rails/resource_route/resource_route_generator.rb b/railties/lib/rails/generators/rails/resource_route/resource_route_generator.rb new file mode 100644 index 0000000000..e4a2bc2b0f --- /dev/null +++ b/railties/lib/rails/generators/rails/resource_route/resource_route_generator.rb @@ -0,0 +1,50 @@ +module Rails + module Generators + class ResourceRouteGenerator < NamedBase # :nodoc: + + # Properly nests namespaces passed into a generator + # + # $ rails generate resource admin/users/products + # + # should give you + # + # namespace :admin do + # namespace :users do + # resources :products + # end + # end + def add_resource_route + return if options[:actions].present? + + # iterates over all namespaces and opens up blocks + regular_class_path.each_with_index do |namespace, index| + write("namespace :#{namespace} do", index + 1) + end + + # inserts the primary resource + write("resources :#{file_name.pluralize}", route_length + 1) + + # ends blocks + regular_class_path.each_index do |index| + write("end", route_length - index) + end + + # route prepends two spaces onto the front of the string that is passed, this corrects that + route route_string[2..-1] + end + + private + def route_string + @route_string ||= "" + end + + def write(str, indent) + route_string << "#{" " * indent}#{str}\n" + end + + def route_length + regular_class_path.length + end + end + end +end diff --git a/railties/lib/rails/generators/rails/scaffold/USAGE b/railties/lib/rails/generators/rails/scaffold/USAGE new file mode 100644 index 0000000000..1b2a944103 --- /dev/null +++ b/railties/lib/rails/generators/rails/scaffold/USAGE @@ -0,0 +1,41 @@ +Description: + Scaffolds an entire resource, from model and migration to controller and + views, along with a full test suite. The resource is ready to use as a + starting point for your RESTful, resource-oriented application. + + Pass the name of the model (in singular form), either CamelCased or + under_scored, as the first argument, and an optional list of attribute + pairs. + + Attributes are field arguments specifying the model's attributes. You can + optionally pass the type and an index to each field. For instance: + 'title body:text tracking_id:integer:uniq' will generate a title field of + string type, a body with text type and a tracking_id as an integer with an + unique index. "index" could also be given instead of "uniq" if one desires + a non unique index. + + As a special case, specifying 'password:digest' will generate a + password_digest field of string type, and configure your generated model, + controller, views, and test suite for use with ActiveModel + has_secure_password (assuming they are using Rails defaults). + + Timestamps are added by default, so you don't have to specify them by hand + as 'created_at:datetime updated_at:datetime'. + + You don't have to think up every attribute up front, but it helps to + sketch out a few so you can start working with the resource immediately. + + For example, 'scaffold post title body:text published:boolean' gives + you a model with those three attributes, a controller that handles + the create/show/update/destroy, forms to create and edit your posts, and + an index that lists them all, as well as a resources :posts declaration + in config/routes.rb. + + If you want to remove all the generated files, run + 'rails destroy scaffold ModelName'. + +Examples: + `rails generate scaffold post` + `rails generate scaffold post title body:text published:boolean` + `rails generate scaffold purchase amount:decimal tracking_id:integer:uniq` + `rails generate scaffold user email:uniq password:digest` diff --git a/railties/lib/rails/generators/rails/scaffold/scaffold_generator.rb b/railties/lib/rails/generators/rails/scaffold/scaffold_generator.rb new file mode 100644 index 0000000000..e89789e72b --- /dev/null +++ b/railties/lib/rails/generators/rails/scaffold/scaffold_generator.rb @@ -0,0 +1,32 @@ +require 'rails/generators/rails/resource/resource_generator' + +module Rails + module Generators + class ScaffoldGenerator < ResourceGenerator # :nodoc: + remove_hook_for :resource_controller + remove_class_option :actions + + class_option :stylesheets, type: :boolean, desc: "Generate Stylesheets" + class_option :stylesheet_engine, desc: "Engine for Stylesheets" + class_option :assets, type: :boolean + class_option :resource_route, type: :boolean + + def handle_skip + @options = @options.merge(stylesheets: false) unless options[:assets] + @options = @options.merge(stylesheet_engine: false) unless options[:stylesheets] + end + + hook_for :scaffold_controller, required: true + + hook_for :assets do |assets| + invoke assets, [controller_name] + end + + hook_for :stylesheet_engine do |stylesheet_engine| + if behavior == :invoke + invoke stylesheet_engine, [controller_name] + end + end + end + end +end diff --git a/railties/lib/rails/generators/rails/scaffold/templates/scaffold.css b/railties/lib/rails/generators/rails/scaffold/templates/scaffold.css new file mode 100644 index 0000000000..1ae7000299 --- /dev/null +++ b/railties/lib/rails/generators/rails/scaffold/templates/scaffold.css @@ -0,0 +1,56 @@ +body { background-color: #fff; color: #333; } + +body, p, ol, ul, td { + font-family: verdana, arial, helvetica, sans-serif; + font-size: 13px; + line-height: 18px; +} + +pre { + background-color: #eee; + padding: 10px; + font-size: 11px; +} + +a { color: #000; } +a:visited { color: #666; } +a:hover { color: #fff; background-color:#000; } + +div.field, div.actions { + margin-bottom: 10px; +} + +#notice { + color: green; +} + +.field_with_errors { + padding: 2px; + background-color: red; + display: table; +} + +#error_explanation { + width: 450px; + border: 2px solid red; + padding: 7px; + padding-bottom: 0; + margin-bottom: 20px; + background-color: #f0f0f0; +} + +#error_explanation h2 { + text-align: left; + font-weight: bold; + padding: 5px 5px 5px 15px; + font-size: 12px; + margin: -7px; + margin-bottom: 0px; + background-color: #c00; + color: #fff; +} + +#error_explanation ul li { + font-size: 12px; + list-style: square; +} diff --git a/railties/lib/rails/generators/rails/scaffold_controller/USAGE b/railties/lib/rails/generators/rails/scaffold_controller/USAGE new file mode 100644 index 0000000000..8ba4c5ccbc --- /dev/null +++ b/railties/lib/rails/generators/rails/scaffold_controller/USAGE @@ -0,0 +1,19 @@ +Description: + Stubs out a scaffolded controller, its seven RESTful actions and related + views. Pass the model name, either CamelCased or under_scored. The + controller name is retrieved as a pluralized version of the model name. + + To create a controller within a module, specify the model name as a + path like 'parent_module/controller_name'. + + This generates a controller class in app/controllers and invokes helper, + template engine and test framework generators. + +Example: + `rails generate scaffold_controller CreditCard` + + Credit card controller with URLs like /credit_card/debit. + Controller: app/controllers/credit_cards_controller.rb + Test: test/controllers/credit_cards_controller_test.rb + Views: app/views/credit_cards/index.html.erb [...] + Helper: app/helpers/credit_cards_helper.rb diff --git a/railties/lib/rails/generators/rails/scaffold_controller/scaffold_controller_generator.rb b/railties/lib/rails/generators/rails/scaffold_controller/scaffold_controller_generator.rb new file mode 100644 index 0000000000..6bf0a33a5f --- /dev/null +++ b/railties/lib/rails/generators/rails/scaffold_controller/scaffold_controller_generator.rb @@ -0,0 +1,27 @@ +require 'rails/generators/resource_helpers' + +module Rails + module Generators + class ScaffoldControllerGenerator < NamedBase # :nodoc: + include ResourceHelpers + + check_class_collision suffix: "Controller" + + class_option :orm, banner: "NAME", type: :string, required: true, + desc: "ORM to generate the controller for" + + argument :attributes, type: :array, default: [], banner: "field:type field:type" + + def create_controller_files + template "controller.rb", File.join('app/controllers', controller_class_path, "#{controller_file_name}_controller.rb") + end + + hook_for :template_engine, :test_framework, as: :scaffold + + # Invoke the helper using the controller name (pluralized) + hook_for :helper, as: :scaffold do |invoked| + invoke invoked, [ controller_name ] + end + end + end +end diff --git a/railties/lib/rails/generators/rails/scaffold_controller/templates/controller.rb b/railties/lib/rails/generators/rails/scaffold_controller/templates/controller.rb new file mode 100644 index 0000000000..2c3b04043f --- /dev/null +++ b/railties/lib/rails/generators/rails/scaffold_controller/templates/controller.rb @@ -0,0 +1,68 @@ +<% if namespaced? -%> +require_dependency "<%= namespaced_file_path %>/application_controller" + +<% end -%> +<% module_namespacing do -%> +class <%= controller_class_name %>Controller < ApplicationController + before_action :set_<%= singular_table_name %>, only: [:show, :edit, :update, :destroy] + + # GET <%= route_url %> + def index + @<%= plural_table_name %> = <%= orm_class.all(class_name) %> + end + + # GET <%= route_url %>/1 + def show + end + + # GET <%= route_url %>/new + def new + @<%= singular_table_name %> = <%= orm_class.build(class_name) %> + end + + # GET <%= route_url %>/1/edit + def edit + end + + # POST <%= route_url %> + def create + @<%= singular_table_name %> = <%= orm_class.build(class_name, "#{singular_table_name}_params") %> + + if @<%= orm_instance.save %> + redirect_to @<%= singular_table_name %>, notice: <%= "'#{human_name} was successfully created.'" %> + else + render :new + end + end + + # PATCH/PUT <%= route_url %>/1 + def update + if @<%= orm_instance.update("#{singular_table_name}_params") %> + redirect_to @<%= singular_table_name %>, notice: <%= "'#{human_name} was successfully updated.'" %> + else + render :edit + end + end + + # DELETE <%= route_url %>/1 + def destroy + @<%= orm_instance.destroy %> + redirect_to <%= index_helper %>_url, notice: <%= "'#{human_name} was successfully destroyed.'" %> + end + + private + # Use callbacks to share common setup or constraints between actions. + def set_<%= singular_table_name %> + @<%= singular_table_name %> = <%= orm_class.find(class_name, "params[:id]") %> + end + + # Only allow a trusted parameter "white list" through. + def <%= "#{singular_table_name}_params" %> + <%- if attributes_names.empty? -%> + params[:<%= singular_table_name %>] + <%- else -%> + params.require(:<%= singular_table_name %>).permit(<%= attributes_names.map { |name| ":#{name}" }.join(', ') %>) + <%- end -%> + end +end +<% end -%> diff --git a/railties/lib/rails/generators/rails/task/USAGE b/railties/lib/rails/generators/rails/task/USAGE new file mode 100644 index 0000000000..dbe9bbaf08 --- /dev/null +++ b/railties/lib/rails/generators/rails/task/USAGE @@ -0,0 +1,9 @@ +Description: + Stubs out a new Rake task. Pass the namespace name, and a list of tasks as arguments. + + This generates a task file in lib/tasks. + +Example: + `rails generate task feeds fetch erase add` + + Task: lib/tasks/feeds.rake
\ No newline at end of file diff --git a/railties/lib/rails/generators/rails/task/task_generator.rb b/railties/lib/rails/generators/rails/task/task_generator.rb new file mode 100644 index 0000000000..754824ca0c --- /dev/null +++ b/railties/lib/rails/generators/rails/task/task_generator.rb @@ -0,0 +1,12 @@ +module Rails + module Generators + class TaskGenerator < NamedBase # :nodoc: + argument :actions, type: :array, default: [], banner: "action action" + + def create_task_files + template 'task.rb', File.join('lib/tasks', "#{file_name}.rake") + end + + end + end +end diff --git a/railties/lib/rails/generators/rails/task/templates/task.rb b/railties/lib/rails/generators/rails/task/templates/task.rb new file mode 100644 index 0000000000..1e3ed5f158 --- /dev/null +++ b/railties/lib/rails/generators/rails/task/templates/task.rb @@ -0,0 +1,8 @@ +namespace :<%= file_name %> do +<% actions.each do |action| -%> + desc "TODO" + task <%= action %>: :environment do + end + +<% end -%> +end diff --git a/railties/lib/rails/generators/resource_helpers.rb b/railties/lib/rails/generators/resource_helpers.rb new file mode 100644 index 0000000000..4669935156 --- /dev/null +++ b/railties/lib/rails/generators/resource_helpers.rb @@ -0,0 +1,82 @@ +require 'rails/generators/active_model' +require 'rails/generators/model_helpers' + +module Rails + module Generators + # Deal with controller names on scaffold and add some helpers to deal with + # ActiveModel. + module ResourceHelpers # :nodoc: + + def self.included(base) #:nodoc: + base.send :include, Rails::Generators::ModelHelpers + base.class_option :model_name, type: :string, desc: "ModelName to be used" + end + + # Set controller variables on initialization. + def initialize(*args) #:nodoc: + super + controller_name = name + if options[:model_name] + self.name = options[:model_name] + assign_names!(self.name) + end + + assign_controller_names!(controller_name.pluralize) + end + + protected + + attr_reader :controller_name, :controller_file_name + + def controller_class_path + if options[:model_name] + @controller_class_path + else + class_path + end + end + + def assign_controller_names!(name) + @controller_name = name + @controller_class_path = name.include?('/') ? name.split('/') : name.split('::') + @controller_class_path.map! { |m| m.underscore } + @controller_file_name = @controller_class_path.pop + end + + def controller_file_path + @controller_file_path ||= (controller_class_path + [controller_file_name]).join('/') + end + + def controller_class_name + (controller_class_path + [controller_file_name]).map!{ |m| m.camelize }.join('::') + end + + def controller_i18n_scope + @controller_i18n_scope ||= controller_file_path.tr('/', '.') + end + + # Loads the ORM::Generators::ActiveModel class. This class is responsible + # to tell scaffold entities how to generate an specific method for the + # ORM. Check Rails::Generators::ActiveModel for more information. + def orm_class + @orm_class ||= begin + # Raise an error if the class_option :orm was not defined. + unless self.class.class_options[:orm] + raise "You need to have :orm as class option to invoke orm_class and orm_instance" + end + + begin + "#{options[:orm].to_s.camelize}::Generators::ActiveModel".constantize + rescue NameError + Rails::Generators::ActiveModel + end + end + end + + # Initialize ORM::Generators::ActiveModel to access instance methods. + def orm_instance(name=singular_table_name) + @orm_instance ||= orm_class.new(name) + end + end + end +end diff --git a/railties/lib/rails/generators/test_case.rb b/railties/lib/rails/generators/test_case.rb new file mode 100644 index 0000000000..58592b4f8e --- /dev/null +++ b/railties/lib/rails/generators/test_case.rb @@ -0,0 +1,36 @@ +require 'rails/generators' +require 'rails/generators/testing/behaviour' +require 'rails/generators/testing/setup_and_teardown' +require 'rails/generators/testing/assertions' +require 'fileutils' + +module Rails + module Generators + # Disable color in output. Easier to debug. + no_color! + + # This class provides a TestCase for testing generators. To setup, you need + # just to configure the destination and set which generator is being tested: + # + # class AppGeneratorTest < Rails::Generators::TestCase + # tests AppGenerator + # destination File.expand_path("../tmp", File.dirname(__FILE__)) + # end + # + # If you want to ensure your destination root is clean before running each test, + # you can set a setup callback: + # + # class AppGeneratorTest < Rails::Generators::TestCase + # tests AppGenerator + # destination File.expand_path("../tmp", File.dirname(__FILE__)) + # setup :prepare_destination + # end + class TestCase < ActiveSupport::TestCase + include Rails::Generators::Testing::Behaviour + include Rails::Generators::Testing::SetupAndTeardown + include Rails::Generators::Testing::Assertions + include FileUtils + + end + end +end diff --git a/railties/lib/rails/generators/test_unit.rb b/railties/lib/rails/generators/test_unit.rb new file mode 100644 index 0000000000..fe45c9e15d --- /dev/null +++ b/railties/lib/rails/generators/test_unit.rb @@ -0,0 +1,8 @@ +require 'rails/generators/named_base' + +module TestUnit # :nodoc: + module Generators # :nodoc: + class Base < Rails::Generators::NamedBase # :nodoc: + end + end +end diff --git a/railties/lib/rails/generators/test_unit/controller/controller_generator.rb b/railties/lib/rails/generators/test_unit/controller/controller_generator.rb new file mode 100644 index 0000000000..b5aa581769 --- /dev/null +++ b/railties/lib/rails/generators/test_unit/controller/controller_generator.rb @@ -0,0 +1,15 @@ +require 'rails/generators/test_unit' + +module TestUnit # :nodoc: + module Generators # :nodoc: + class ControllerGenerator < Base # :nodoc: + argument :actions, type: :array, default: [], banner: "action action" + check_class_collision suffix: "ControllerTest" + + def create_test_files + template 'functional_test.rb', + File.join('test/controllers', class_path, "#{file_name}_controller_test.rb") + end + end + end +end diff --git a/railties/lib/rails/generators/test_unit/controller/templates/functional_test.rb b/railties/lib/rails/generators/test_unit/controller/templates/functional_test.rb new file mode 100644 index 0000000000..509bd60564 --- /dev/null +++ b/railties/lib/rails/generators/test_unit/controller/templates/functional_test.rb @@ -0,0 +1,19 @@ +require 'test_helper' + +<% module_namespacing do -%> +class <%= class_name %>ControllerTest < ActionController::TestCase +<% if actions.empty? -%> + # test "the truth" do + # assert true + # end +<% else -%> +<% actions.each do |action| -%> + test "should get <%= action %>" do + get :<%= action %> + assert_response :success + end + +<% end -%> +<% end -%> +end +<% end -%> diff --git a/railties/lib/rails/generators/test_unit/generator/generator_generator.rb b/railties/lib/rails/generators/test_unit/generator/generator_generator.rb new file mode 100644 index 0000000000..d7307398ce --- /dev/null +++ b/railties/lib/rails/generators/test_unit/generator/generator_generator.rb @@ -0,0 +1,26 @@ +require 'rails/generators/test_unit' + +module TestUnit # :nodoc: + module Generators # :nodoc: + class GeneratorGenerator < Base # :nodoc: + check_class_collision suffix: "GeneratorTest" + + class_option :namespace, type: :boolean, default: true, + desc: "Namespace generator under lib/generators/name" + + def create_generator_files + template 'generator_test.rb', File.join('test/lib/generators', class_path, "#{file_name}_generator_test.rb") + end + + protected + + def generator_path + if options[:namespace] + File.join("generators", regular_class_path, file_name, "#{file_name}_generator") + else + File.join("generators", regular_class_path, "#{file_name}_generator") + end + end + end + end +end diff --git a/railties/lib/rails/generators/test_unit/generator/templates/generator_test.rb b/railties/lib/rails/generators/test_unit/generator/templates/generator_test.rb new file mode 100644 index 0000000000..a7f1fc4fba --- /dev/null +++ b/railties/lib/rails/generators/test_unit/generator/templates/generator_test.rb @@ -0,0 +1,16 @@ +require 'test_helper' +require '<%= generator_path %>' + +<% module_namespacing do -%> +class <%= class_name %>GeneratorTest < Rails::Generators::TestCase + tests <%= class_name %>Generator + destination Rails.root.join('tmp/generators') + setup :prepare_destination + + # test "generator runs without errors" do + # assert_nothing_raised do + # run_generator ["arguments"] + # end + # end +end +<% end -%> diff --git a/railties/lib/rails/generators/test_unit/helper/helper_generator.rb b/railties/lib/rails/generators/test_unit/helper/helper_generator.rb new file mode 100644 index 0000000000..bde4e88915 --- /dev/null +++ b/railties/lib/rails/generators/test_unit/helper/helper_generator.rb @@ -0,0 +1,9 @@ +require 'rails/generators/test_unit' + +module TestUnit # :nodoc: + module Generators # :nodoc: + class HelperGenerator < Base # :nodoc: + # Rails does not generate anything here. + end + end +end diff --git a/railties/lib/rails/generators/test_unit/integration/integration_generator.rb b/railties/lib/rails/generators/test_unit/integration/integration_generator.rb new file mode 100644 index 0000000000..e004835bd5 --- /dev/null +++ b/railties/lib/rails/generators/test_unit/integration/integration_generator.rb @@ -0,0 +1,13 @@ +require 'rails/generators/test_unit' + +module TestUnit # :nodoc: + module Generators # :nodoc: + class IntegrationGenerator < Base # :nodoc: + check_class_collision suffix: "Test" + + def create_test_files + template 'integration_test.rb', File.join('test/integration', class_path, "#{file_name}_test.rb") + end + end + end +end diff --git a/railties/lib/rails/generators/test_unit/integration/templates/integration_test.rb b/railties/lib/rails/generators/test_unit/integration/templates/integration_test.rb new file mode 100644 index 0000000000..dea7e22196 --- /dev/null +++ b/railties/lib/rails/generators/test_unit/integration/templates/integration_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class <%= class_name %>Test < ActionDispatch::IntegrationTest + # test "the truth" do + # assert true + # end +end diff --git a/railties/lib/rails/generators/test_unit/mailer/mailer_generator.rb b/railties/lib/rails/generators/test_unit/mailer/mailer_generator.rb new file mode 100644 index 0000000000..85dee1a066 --- /dev/null +++ b/railties/lib/rails/generators/test_unit/mailer/mailer_generator.rb @@ -0,0 +1,21 @@ +require 'rails/generators/test_unit' + +module TestUnit # :nodoc: + module Generators # :nodoc: + class MailerGenerator < Base # :nodoc: + argument :actions, type: :array, default: [], banner: "method method" + + def check_class_collision + class_collisions "#{class_name}Test", "#{class_name}Preview" + end + + def create_test_files + template "functional_test.rb", File.join('test/mailers', class_path, "#{file_name}_test.rb") + end + + def create_preview_files + template "preview.rb", File.join('test/mailers/previews', class_path, "#{file_name}_preview.rb") + end + end + end +end diff --git a/railties/lib/rails/generators/test_unit/mailer/templates/functional_test.rb b/railties/lib/rails/generators/test_unit/mailer/templates/functional_test.rb new file mode 100644 index 0000000000..7e204105a3 --- /dev/null +++ b/railties/lib/rails/generators/test_unit/mailer/templates/functional_test.rb @@ -0,0 +1,21 @@ +require 'test_helper' + +<% module_namespacing do -%> +class <%= class_name %>Test < ActionMailer::TestCase +<% actions.each do |action| -%> + test "<%= action %>" do + mail = <%= class_name %>.<%= action %> + assert_equal <%= action.to_s.humanize.inspect %>, mail.subject + assert_equal ["to@example.org"], mail.to + assert_equal ["from@example.com"], mail.from + assert_match "Hi", mail.body.encoded + end + +<% end -%> +<% if actions.blank? -%> + # test "the truth" do + # assert true + # end +<% end -%> +end +<% end -%> diff --git a/railties/lib/rails/generators/test_unit/mailer/templates/preview.rb b/railties/lib/rails/generators/test_unit/mailer/templates/preview.rb new file mode 100644 index 0000000000..3bfd5426e8 --- /dev/null +++ b/railties/lib/rails/generators/test_unit/mailer/templates/preview.rb @@ -0,0 +1,13 @@ +<% module_namespacing do -%> +# Preview all emails at http://localhost:3000/rails/mailers/<%= file_path %> +class <%= class_name %>Preview < ActionMailer::Preview +<% actions.each do |action| -%> + + # Preview this email at http://localhost:3000/rails/mailers/<%= file_path %>/<%= action %> + def <%= action %> + <%= class_name %>.<%= action %> + end +<% end -%> + +end +<% end -%> diff --git a/railties/lib/rails/generators/test_unit/model/model_generator.rb b/railties/lib/rails/generators/test_unit/model/model_generator.rb new file mode 100644 index 0000000000..2826a3ffa1 --- /dev/null +++ b/railties/lib/rails/generators/test_unit/model/model_generator.rb @@ -0,0 +1,36 @@ +require 'rails/generators/test_unit' + +module TestUnit # :nodoc: + module Generators # :nodoc: + class ModelGenerator < Base # :nodoc: + + RESERVED_YAML_KEYWORDS = %w(y yes n no true false on off null) + + argument :attributes, type: :array, default: [], banner: "field:type field:type" + class_option :fixture, type: :boolean + + check_class_collision suffix: "Test" + + def create_test_file + template 'unit_test.rb', File.join('test/models', class_path, "#{file_name}_test.rb") + end + + hook_for :fixture_replacement + + def create_fixture_file + if options[:fixture] && options[:fixture_replacement].nil? + template 'fixtures.yml', File.join('test/fixtures', class_path, "#{plural_file_name}.yml") + end + end + + private + def yaml_key_value(key, value) + if RESERVED_YAML_KEYWORDS.include?(key.downcase) + "'#{key}': #{value}" + else + "#{key}: #{value}" + end + end + end + end +end diff --git a/railties/lib/rails/generators/test_unit/model/templates/fixtures.yml b/railties/lib/rails/generators/test_unit/model/templates/fixtures.yml new file mode 100644 index 0000000000..f19e9d1d87 --- /dev/null +++ b/railties/lib/rails/generators/test_unit/model/templates/fixtures.yml @@ -0,0 +1,27 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html +<% unless attributes.empty? -%> +<% %w(one two).each do |name| %> +<%= name %>: +<% attributes.each do |attribute| -%> + <%- if attribute.password_digest? -%> + password_digest: <%%= BCrypt::Password.create('secret') %> + <%- else -%> + <%= yaml_key_value(attribute.column_name, attribute.default) %> + <%- end -%> + <%- if attribute.polymorphic? -%> + <%= yaml_key_value("#{attribute.name}_type", attribute.human_name) %> + <%- end -%> +<% end -%> +<% end -%> +<% else -%> + +# This model initially had no columns defined. If you add columns to the +# model remove the '{}' from the fixture names and add the columns immediately +# below each fixture, per the syntax in the comments below +# +one: {} +# column: value +# +two: {} +# column: value +<% end -%> diff --git a/railties/lib/rails/generators/test_unit/model/templates/unit_test.rb b/railties/lib/rails/generators/test_unit/model/templates/unit_test.rb new file mode 100644 index 0000000000..c9bc7d5b90 --- /dev/null +++ b/railties/lib/rails/generators/test_unit/model/templates/unit_test.rb @@ -0,0 +1,9 @@ +require 'test_helper' + +<% module_namespacing do -%> +class <%= class_name %>Test < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end +<% end -%> diff --git a/railties/lib/rails/generators/test_unit/plugin/plugin_generator.rb b/railties/lib/rails/generators/test_unit/plugin/plugin_generator.rb new file mode 100644 index 0000000000..b5d4f38444 --- /dev/null +++ b/railties/lib/rails/generators/test_unit/plugin/plugin_generator.rb @@ -0,0 +1,13 @@ +require 'rails/generators/test_unit' + +module TestUnit # :nodoc: + module Generators # :nodoc: + class PluginGenerator < Base # :nodoc: + check_class_collision suffix: "Test" + + def create_test_files + directory '.', 'test' + end + end + end +end diff --git a/railties/lib/rails/generators/test_unit/plugin/templates/%file_name%_test.rb.tt b/railties/lib/rails/generators/test_unit/plugin/templates/%file_name%_test.rb.tt new file mode 100644 index 0000000000..0cbae1120e --- /dev/null +++ b/railties/lib/rails/generators/test_unit/plugin/templates/%file_name%_test.rb.tt @@ -0,0 +1,7 @@ +require 'test_helper' + +class <%= class_name %>Test < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/railties/lib/rails/generators/test_unit/plugin/templates/test_helper.rb b/railties/lib/rails/generators/test_unit/plugin/templates/test_helper.rb new file mode 100644 index 0000000000..30a861f09d --- /dev/null +++ b/railties/lib/rails/generators/test_unit/plugin/templates/test_helper.rb @@ -0,0 +1,2 @@ +require 'active_support/testing/autorun' +require 'active_support' diff --git a/railties/lib/rails/generators/test_unit/scaffold/scaffold_generator.rb b/railties/lib/rails/generators/test_unit/scaffold/scaffold_generator.rb new file mode 100644 index 0000000000..2e1f55f2a6 --- /dev/null +++ b/railties/lib/rails/generators/test_unit/scaffold/scaffold_generator.rb @@ -0,0 +1,33 @@ +require 'rails/generators/test_unit' +require 'rails/generators/resource_helpers' + +module TestUnit # :nodoc: + module Generators # :nodoc: + class ScaffoldGenerator < Base # :nodoc: + include Rails::Generators::ResourceHelpers + + check_class_collision suffix: "ControllerTest" + + argument :attributes, type: :array, default: [], banner: "field:type field:type" + + def create_test_files + template "functional_test.rb", + File.join("test/controllers", controller_class_path, "#{controller_file_name}_controller_test.rb") + end + + private + + def attributes_hash + return if attributes_names.empty? + + attributes_names.map do |name| + if %w(password password_confirmation).include?(name) && attributes.any?(&:password_digest?) + "#{name}: 'secret'" + else + "#{name}: @#{singular_table_name}.#{name}" + end + end.sort.join(', ') + end + end + end +end diff --git a/railties/lib/rails/generators/test_unit/scaffold/templates/functional_test.rb b/railties/lib/rails/generators/test_unit/scaffold/templates/functional_test.rb new file mode 100644 index 0000000000..18bd1ece9d --- /dev/null +++ b/railties/lib/rails/generators/test_unit/scaffold/templates/functional_test.rb @@ -0,0 +1,51 @@ +require 'test_helper' + +<% module_namespacing do -%> +class <%= controller_class_name %>ControllerTest < ActionController::TestCase + setup do + @<%= singular_table_name %> = <%= table_name %>(:one) + end + + test "should get index" do + get :index + assert_response :success + assert_not_nil assigns(:<%= table_name %>) + end + + test "should get new" do + get :new + assert_response :success + end + + test "should create <%= singular_table_name %>" do + assert_difference('<%= class_name %>.count') do + post :create, <%= "#{singular_table_name}: { #{attributes_hash} }" %> + end + + assert_redirected_to <%= singular_table_name %>_path(assigns(:<%= singular_table_name %>)) + end + + test "should show <%= singular_table_name %>" do + get :show, id: <%= "@#{singular_table_name}" %> + assert_response :success + end + + test "should get edit" do + get :edit, id: <%= "@#{singular_table_name}" %> + assert_response :success + end + + test "should update <%= singular_table_name %>" do + patch :update, id: <%= "@#{singular_table_name}" %>, <%= "#{singular_table_name}: { #{attributes_hash} }" %> + assert_redirected_to <%= singular_table_name %>_path(assigns(:<%= singular_table_name %>)) + end + + test "should destroy <%= singular_table_name %>" do + assert_difference('<%= class_name %>.count', -1) do + delete :destroy, id: <%= "@#{singular_table_name}" %> + end + + assert_redirected_to <%= index_helper %>_path + end +end +<% end -%> diff --git a/railties/lib/rails/generators/testing/assertions.rb b/railties/lib/rails/generators/testing/assertions.rb new file mode 100644 index 0000000000..bd069e4bd0 --- /dev/null +++ b/railties/lib/rails/generators/testing/assertions.rb @@ -0,0 +1,123 @@ +require 'shellwords' + +module Rails + module Generators + module Testing + module Assertions + # Asserts a given file exists. You need to supply an absolute path or a path relative + # to the configured destination: + # + # assert_file "config/environment.rb" + # + # You can also give extra arguments. If the argument is a regexp, it will check if the + # regular expression matches the given file content. If it's a string, it compares the + # file with the given string: + # + # assert_file "config/environment.rb", /initialize/ + # + # Finally, when a block is given, it yields the file content: + # + # assert_file "app/controllers/products_controller.rb" do |controller| + # assert_instance_method :index, controller do |index| + # assert_match(/Product\.all/, index) + # end + # end + def assert_file(relative, *contents) + absolute = File.expand_path(relative, destination_root).shellescape + assert File.exist?(absolute), "Expected file #{relative.inspect} to exist, but does not" + + read = File.read(absolute) if block_given? || !contents.empty? + yield read if block_given? + + contents.each do |content| + case content + when String + assert_equal content, read + when Regexp + assert_match content, read + end + end + end + alias :assert_directory :assert_file + + # Asserts a given file does not exist. You need to supply an absolute path or a + # path relative to the configured destination: + # + # assert_no_file "config/random.rb" + def assert_no_file(relative) + absolute = File.expand_path(relative, destination_root) + assert !File.exist?(absolute), "Expected file #{relative.inspect} to not exist, but does" + end + alias :assert_no_directory :assert_no_file + + # Asserts a given migration exists. You need to supply an absolute path or a + # path relative to the configured destination: + # + # assert_migration "db/migrate/create_products.rb" + # + # This method manipulates the given path and tries to find any migration which + # matches the migration name. For example, the call above is converted to: + # + # assert_file "db/migrate/003_create_products.rb" + # + # Consequently, assert_migration accepts the same arguments has assert_file. + def assert_migration(relative, *contents, &block) + file_name = migration_file_name(relative) + assert file_name, "Expected migration #{relative} to exist, but was not found" + assert_file file_name, *contents, &block + end + + # Asserts a given migration does not exist. You need to supply an absolute path or a + # path relative to the configured destination: + # + # assert_no_migration "db/migrate/create_products.rb" + def assert_no_migration(relative) + file_name = migration_file_name(relative) + assert_nil file_name, "Expected migration #{relative} to not exist, but found #{file_name}" + end + + # Asserts the given class method exists in the given content. This method does not detect + # class methods inside (class << self), only class methods which starts with "self.". + # When a block is given, it yields the content of the method. + # + # assert_migration "db/migrate/create_products.rb" do |migration| + # assert_class_method :up, migration do |up| + # assert_match(/create_table/, up) + # end + # end + def assert_class_method(method, content, &block) + assert_instance_method "self.#{method}", content, &block + end + + # Asserts the given method exists in the given content. When a block is given, + # it yields the content of the method. + # + # assert_file "app/controllers/products_controller.rb" do |controller| + # assert_instance_method :index, controller do |index| + # assert_match(/Product\.all/, index) + # end + # end + def assert_instance_method(method, content) + assert content =~ /(\s+)def #{method}(\(.+\))?(.*?)\n\1end/m, "Expected to have method #{method}" + yield $3.strip if block_given? + end + alias :assert_method :assert_instance_method + + # Asserts the given attribute type gets translated to a field type + # properly: + # + # assert_field_type :date, :date_select + def assert_field_type(attribute_type, field_type) + assert_equal(field_type, create_generated_attribute(attribute_type).field_type) + end + + # Asserts the given attribute type gets a proper default value: + # + # assert_field_default_value :string, "MyString" + def assert_field_default_value(attribute_type, value) + assert_equal(value, create_generated_attribute(attribute_type).default) + end + end + end + end +end diff --git a/railties/lib/rails/generators/testing/behaviour.rb b/railties/lib/rails/generators/testing/behaviour.rb new file mode 100644 index 0000000000..e0600d0b59 --- /dev/null +++ b/railties/lib/rails/generators/testing/behaviour.rb @@ -0,0 +1,123 @@ +require 'active_support/core_ext/class/attribute' +require 'active_support/core_ext/module/delegation' +require 'active_support/core_ext/hash/reverse_merge' +require 'active_support/core_ext/kernel/reporting' +require 'active_support/concern' +require 'rails/generators' + +module Rails + module Generators + module Testing + module Behaviour + extend ActiveSupport::Concern + + included do + class_attribute :destination_root, :current_path, :generator_class, :default_arguments + + # Generators frequently change the current path using +FileUtils.cd+. + # So we need to store the path at file load and revert back to it after each test. + self.current_path = File.expand_path(Dir.pwd) + self.default_arguments = [] + end + + module ClassMethods + # Sets which generator should be tested: + # + # tests AppGenerator + def tests(klass) + self.generator_class = klass + end + + # Sets default arguments on generator invocation. This can be overwritten when + # invoking it. + # + # arguments %w(app_name --skip-active-record) + def arguments(array) + self.default_arguments = array + end + + # Sets the destination of generator files: + # + # destination File.expand_path("../tmp", File.dirname(__FILE__)) + def destination(path) + self.destination_root = path + end + end + + # Runs the generator configured for this class. The first argument is an array like + # command line arguments: + # + # class AppGeneratorTest < Rails::Generators::TestCase + # tests AppGenerator + # destination File.expand_path("../tmp", File.dirname(__FILE__)) + # teardown :cleanup_destination_root + # + # test "database.yml is not created when skipping Active Record" do + # run_generator %w(myapp --skip-active-record) + # assert_no_file "config/database.yml" + # end + # end + # + # You can provide a configuration hash as second argument. This method returns the output + # printed by the generator. + def run_generator(args=self.default_arguments, config={}) + capture(:stdout) do + args += ['--skip-bundle'] unless args.include? '--dev' + self.generator_class.start(args, config.reverse_merge(destination_root: destination_root)) + end + end + + # Instantiate the generator. + def generator(args=self.default_arguments, options={}, config={}) + @generator ||= self.generator_class.new(args, options, config.reverse_merge(destination_root: destination_root)) + end + + # Create a Rails::Generators::GeneratedAttribute by supplying the + # attribute type and, optionally, the attribute name: + # + # create_generated_attribute(:string, 'name') + def create_generated_attribute(attribute_type, name = 'test', index = nil) + Rails::Generators::GeneratedAttribute.parse([name, attribute_type, index].compact.join(':')) + end + + protected + + def destination_root_is_set? # :nodoc: + raise "You need to configure your Rails::Generators::TestCase destination root." unless destination_root + end + + def ensure_current_path # :nodoc: + cd current_path + end + + def prepare_destination # :nodoc: + rm_rf(destination_root) + mkdir_p(destination_root) + end + + def migration_file_name(relative) # :nodoc: + absolute = File.expand_path(relative, destination_root) + dirname, file_name = File.dirname(absolute), File.basename(absolute).sub(/\.rb$/, '') + Dir.glob("#{dirname}/[0-9]*_*.rb").grep(/\d+_#{file_name}.rb$/).first + end + + def capture(stream) + stream = stream.to_s + captured_stream = Tempfile.new(stream) + stream_io = eval("$#{stream}") + origin_stream = stream_io.dup + stream_io.reopen(captured_stream) + + yield + + stream_io.rewind + return captured_stream.read + ensure + captured_stream.close + captured_stream.unlink + stream_io.reopen(origin_stream) + end + end + end + end +end diff --git a/railties/lib/rails/generators/testing/setup_and_teardown.rb b/railties/lib/rails/generators/testing/setup_and_teardown.rb new file mode 100644 index 0000000000..73102a283f --- /dev/null +++ b/railties/lib/rails/generators/testing/setup_and_teardown.rb @@ -0,0 +1,18 @@ +module Rails + module Generators + module Testing + module SetupAndTeardown + def setup # :nodoc: + destination_root_is_set? + ensure_current_path + super + end + + def teardown # :nodoc: + ensure_current_path + super + end + end + end + end +end diff --git a/railties/lib/rails/info.rb b/railties/lib/rails/info.rb new file mode 100644 index 0000000000..9502876ebb --- /dev/null +++ b/railties/lib/rails/info.rb @@ -0,0 +1,118 @@ +require "cgi" + +module Rails + module Info + mattr_accessor :properties + class << (@@properties = []) + def names + map {|val| val.first } + end + + def value_for(property_name) + if property = assoc(property_name) + property.last + end + end + end + + class << self #:nodoc: + def property(name, value = nil) + value ||= yield + properties << [name, value] if value + rescue Exception + end + + def frameworks + %w( active_record action_pack action_view action_mailer active_support active_model ) + end + + def framework_version(framework) + if Object.const_defined?(framework.classify) + require "#{framework}/version" + framework.classify.constantize.version.to_s + end + end + + def to_s + column_width = properties.names.map {|name| name.length}.max + info = properties.map do |name, value| + value = value.join(", ") if value.is_a?(Array) + "%-#{column_width}s %s" % [name, value] + end + info.unshift "About your application's environment" + info * "\n" + end + + alias inspect to_s + + def to_html + '<table>'.tap do |table| + properties.each do |(name, value)| + table << %(<tr><td class="name">#{CGI.escapeHTML(name.to_s)}</td>) + formatted_value = if value.kind_of?(Array) + "<ul>" + value.map { |v| "<li>#{CGI.escapeHTML(v.to_s)}</li>" }.join + "</ul>" + else + CGI.escapeHTML(value.to_s) + end + table << %(<td class="value">#{formatted_value}</td></tr>) + end + table << '</table>' + end + end + end + + # The Ruby version and platform, e.g. "2.0.0-p247 (x86_64-darwin12.4.0)". + property 'Ruby version' do + "#{RUBY_VERSION}-p#{RUBY_PATCHLEVEL} (#{RUBY_PLATFORM})" + end + + # The RubyGems version, if it's installed. + property 'RubyGems version' do + Gem::RubyGemsVersion + end + + property 'Rack version' do + ::Rack.release + end + + # The Rails version. + property 'Rails version' do + Rails.version.to_s + end + + property 'JavaScript Runtime' do + ExecJS.runtime.name + end + + # Versions of each Rails framework (Active Record, Action Pack, + # Action Mailer, and Active Support). + frameworks.each do |framework| + property "#{framework.titlecase} version" do + framework_version(framework) + end + end + + property 'Middleware' do + Rails.configuration.middleware.map(&:inspect) + end + + # The application's location on the filesystem. + property 'Application root' do + File.expand_path(Rails.root) + end + + # The current Rails environment (development, test, or production). + property 'Environment' do + Rails.env + end + + # The name of the database adapter for the current environment. + property 'Database adapter' do + ActiveRecord::Base.configurations[Rails.env]['adapter'] + end + + property 'Database schema version' do + ActiveRecord::Migrator.current_version rescue nil + end + end +end diff --git a/railties/lib/rails/info_controller.rb b/railties/lib/rails/info_controller.rb new file mode 100644 index 0000000000..49e5431a16 --- /dev/null +++ b/railties/lib/rails/info_controller.rb @@ -0,0 +1,23 @@ +require 'rails/application_controller' +require 'action_dispatch/routing/inspector' + +class Rails::InfoController < Rails::ApplicationController # :nodoc: + prepend_view_path ActionDispatch::DebugExceptions::RESCUES_TEMPLATE_PATH + layout -> { request.xhr? ? false : 'application' } + + before_action :require_local! + + def index + redirect_to action: :routes + end + + def properties + @info = Rails::Info.to_html + @page_title = 'Properties' + end + + def routes + @routes_inspector = ActionDispatch::Routing::RoutesInspector.new(_routes.routes) + @page_title = 'Routes' + end +end diff --git a/railties/lib/rails/initializable.rb b/railties/lib/rails/initializable.rb new file mode 100644 index 0000000000..1a0b6d1e1a --- /dev/null +++ b/railties/lib/rails/initializable.rb @@ -0,0 +1,89 @@ +require 'tsort' + +module Rails + module Initializable + def self.included(base) #:nodoc: + base.extend ClassMethods + end + + class Initializer + attr_reader :name, :block + + def initialize(name, context, options, &block) + options[:group] ||= :default + @name, @context, @options, @block = name, context, options, block + end + + def before + @options[:before] + end + + def after + @options[:after] + end + + def belongs_to?(group) + @options[:group] == group || @options[:group] == :all + end + + def run(*args) + @context.instance_exec(*args, &block) + end + + def bind(context) + return self if @context + Initializer.new(@name, context, @options, &block) + end + end + + class Collection < Array + include TSort + + alias :tsort_each_node :each + def tsort_each_child(initializer, &block) + select { |i| i.before == initializer.name || i.name == initializer.after }.each(&block) + end + + def +(other) + Collection.new(to_a + other.to_a) + end + end + + def run_initializers(group=:default, *args) + return if instance_variable_defined?(:@ran) + initializers.tsort_each do |initializer| + initializer.run(*args) if initializer.belongs_to?(group) + end + @ran = true + end + + def initializers + @initializers ||= self.class.initializers_for(self) + end + + module ClassMethods + def initializers + @initializers ||= Collection.new + end + + def initializers_chain + initializers = Collection.new + ancestors.reverse_each do |klass| + next unless klass.respond_to?(:initializers) + initializers = initializers + klass.initializers + end + initializers + end + + def initializers_for(binding) + Collection.new(initializers_chain.map { |i| i.bind(binding) }) + end + + def initializer(name, opts = {}, &blk) + raise ArgumentError, "A block must be passed when defining an initializer" unless blk + opts[:after] ||= initializers.last.name unless initializers.empty? || initializers.find { |i| i.name == opts[:before] } + initializers << Initializer.new(name, nil, opts, &blk) + end + end + end +end diff --git a/railties/lib/rails/mailers_controller.rb b/railties/lib/rails/mailers_controller.rb new file mode 100644 index 0000000000..32740d66da --- /dev/null +++ b/railties/lib/rails/mailers_controller.rb @@ -0,0 +1,73 @@ +require 'rails/application_controller' + +class Rails::MailersController < Rails::ApplicationController # :nodoc: + prepend_view_path ActionDispatch::DebugExceptions::RESCUES_TEMPLATE_PATH + + before_action :require_local! + before_action :find_preview, only: :preview + + def index + @previews = ActionMailer::Preview.all + @page_title = "Mailer Previews" + end + + def preview + if params[:path] == @preview.preview_name + @page_title = "Mailer Previews for #{@preview.preview_name}" + render action: 'mailer' + else + email = File.basename(params[:path]) + + if @preview.email_exists?(email) + @email = @preview.call(email) + + if params[:part] + part_type = Mime::Type.lookup(params[:part]) + + if part = find_part(part_type) + response.content_type = part_type + render text: part.respond_to?(:decoded) ? part.decoded : part + else + raise AbstractController::ActionNotFound, "Email part '#{part_type}' not found in #{@preview.name}##{email}" + end + else + @part = find_preferred_part(request.format, Mime::HTML, Mime::TEXT) + render action: 'email', layout: false, formats: %w[html] + end + else + raise AbstractController::ActionNotFound, "Email '#{email}' not found in #{@preview.name}" + end + end + end + + protected + def find_preview + candidates = [] + params[:path].to_s.scan(%r{/|$}){ candidates << $` } + preview = candidates.detect{ |candidate| ActionMailer::Preview.exists?(candidate) } + + if preview + @preview = ActionMailer::Preview.find(preview) + else + raise AbstractController::ActionNotFound, "Mailer preview '#{params[:path]}' not found" + end + end + + def find_preferred_part(*formats) + if @email.multipart? + formats.each do |format| + return find_part(format) if @email.parts.any?{ |p| p.mime_type == format } + end + else + @email + end + end + + def find_part(format) + if @email.multipart? + @email.parts.find{ |p| p.mime_type == format } + elsif @email.mime_type == format + @email + end + end +end diff --git a/railties/lib/rails/paths.rb b/railties/lib/rails/paths.rb new file mode 100644 index 0000000000..3eb66c07af --- /dev/null +++ b/railties/lib/rails/paths.rb @@ -0,0 +1,211 @@ +module Rails + module Paths + # This object is an extended hash that behaves as root of the <tt>Rails::Paths</tt> system. + # It allows you to collect information about how you want to structure your application + # paths by a Hash like API. It requires you to give a physical path on initialization. + # + # root = Root.new "/rails" + # root.add "app/controllers", eager_load: true + # + # The command above creates a new root object and add "app/controllers" as a path. + # This means we can get a <tt>Rails::Paths::Path</tt> object back like below: + # + # path = root["app/controllers"] + # path.eager_load? # => true + # path.is_a?(Rails::Paths::Path) # => true + # + # The +Path+ object is simply an enumerable and allows you to easily add extra paths: + # + # path.is_a?(Enumerable) # => true + # path.to_ary.inspect # => ["app/controllers"] + # + # path << "lib/controllers" + # path.to_ary.inspect # => ["app/controllers", "lib/controllers"] + # + # Notice that when you add a path using +add+, the path object created already + # contains the path with the same path value given to +add+. In some situations, + # you may not want this behavior, so you can give <tt>:with</tt> as option. + # + # root.add "config/routes", with: "config/routes.rb" + # root["config/routes"].inspect # => ["config/routes.rb"] + # + # The +add+ method accepts the following options as arguments: + # eager_load, autoload, autoload_once and glob. + # + # Finally, the +Path+ object also provides a few helpers: + # + # root = Root.new "/rails" + # root.add "app/controllers" + # + # root["app/controllers"].expanded # => ["/rails/app/controllers"] + # root["app/controllers"].existent # => ["/rails/app/controllers"] + # + # Check the <tt>Rails::Paths::Path</tt> documentation for more information. + class Root + attr_accessor :path + + def initialize(path) + @current = nil + @path = path + @root = {} + end + + def []=(path, value) + glob = self[path] ? self[path].glob : nil + add(path, with: value, glob: glob) + end + + def add(path, options = {}) + with = Array(options.fetch(:with, path)) + @root[path] = Path.new(self, path, with, options) + end + + def [](path) + @root[path] + end + + def values + @root.values + end + + def keys + @root.keys + end + + def values_at(*list) + @root.values_at(*list) + end + + def all_paths + values.tap { |v| v.uniq! } + end + + def autoload_once + filter_by { |p| p.autoload_once? } + end + + def eager_load + filter_by { |p| p.eager_load? } + end + + def autoload_paths + filter_by { |p| p.autoload? } + end + + def load_paths + filter_by { |p| p.load_path? } + end + + private + + def filter_by(&block) + all_paths.find_all(&block).flat_map { |path| + paths = path.existent + paths - path.children.flat_map { |p| yield(p) ? [] : p.existent } + }.uniq + end + end + + class Path + include Enumerable + + attr_accessor :glob + + def initialize(root, current, paths, options = {}) + @paths = paths + @current = current + @root = root + @glob = options[:glob] + + options[:autoload_once] ? autoload_once! : skip_autoload_once! + options[:eager_load] ? eager_load! : skip_eager_load! + options[:autoload] ? autoload! : skip_autoload! + options[:load_path] ? load_path! : skip_load_path! + end + + def children + keys = @root.keys.find_all { |k| + k.start_with?(@current) && k != @current + } + @root.values_at(*keys.sort) + end + + def first + expanded.first + end + + def last + expanded.last + end + + %w(autoload_once eager_load autoload load_path).each do |m| + class_eval <<-RUBY, __FILE__, __LINE__ + 1 + def #{m}! # def eager_load! + @#{m} = true # @eager_load = true + end # end + # + def skip_#{m}! # def skip_eager_load! + @#{m} = false # @eager_load = false + end # end + # + def #{m}? # def eager_load? + @#{m} # @eager_load + end # end + RUBY + end + + def each(&block) + @paths.each(&block) + end + + def <<(path) + @paths << path + end + alias :push :<< + + def concat(paths) + @paths.concat paths + end + + def unshift(path) + @paths.unshift path + end + + def to_ary + @paths + end + + # Expands all paths against the root and return all unique values. + def expanded + raise "You need to set a path root" unless @root.path + result = [] + + each do |p| + path = File.expand_path(p, @root.path) + + if @glob && File.directory?(path) + Dir.chdir(path) do + result.concat(Dir.glob(@glob).map { |file| File.join path, file }.sort) + end + else + result << path + end + end + + result.uniq! + result + end + + # Returns all expanded paths but only if they exist in the filesystem. + def existent + expanded.select { |f| File.exist?(f) } + end + + def existent_directories + expanded.select { |d| File.directory?(d) } + end + + alias to_a expanded + end + end +end diff --git a/railties/lib/rails/rack.rb b/railties/lib/rails/rack.rb new file mode 100644 index 0000000000..886f0e52e1 --- /dev/null +++ b/railties/lib/rails/rack.rb @@ -0,0 +1,7 @@ +module Rails + module Rack + autoload :Debugger, "rails/rack/debugger" if RUBY_VERSION < '2.0.0' + autoload :Logger, "rails/rack/logger" + autoload :LogTailer, "rails/rack/log_tailer" + end +end diff --git a/railties/lib/rails/rack/debugger.rb b/railties/lib/rails/rack/debugger.rb new file mode 100644 index 0000000000..f7b77bcb3b --- /dev/null +++ b/railties/lib/rails/rack/debugger.rb @@ -0,0 +1,24 @@ +module Rails + module Rack + class Debugger + def initialize(app) + @app = app + + ARGV.clear # clear ARGV so that rails server options aren't passed to IRB + + require 'debugger' + + ::Debugger.start + ::Debugger.settings[:autoeval] = true if ::Debugger.respond_to?(:settings) + puts "=> Debugger enabled" + rescue LoadError + puts "You're missing the 'debugger' gem. Add it to your Gemfile, bundle it and try again." + exit(1) + end + + def call(env) + @app.call(env) + end + end + end +end diff --git a/railties/lib/rails/rack/log_tailer.rb b/railties/lib/rails/rack/log_tailer.rb new file mode 100644 index 0000000000..bc26421a9e --- /dev/null +++ b/railties/lib/rails/rack/log_tailer.rb @@ -0,0 +1,38 @@ +require 'active_support/deprecation' + +module Rails + module Rack + class LogTailer + def initialize(app, log = nil) + ActiveSupport::Deprecation.warn "LogTailer is deprecated and will be removed on Rails 5" + + @app = app + + path = Pathname.new(log || "#{::File.expand_path(Rails.root)}/log/#{Rails.env}.log").cleanpath + + @cursor = @file = nil + if ::File.exist?(path) + @cursor = ::File.size(path) + @file = ::File.open(path, 'r') + end + end + + def call(env) + response = @app.call(env) + tail! + response + end + + def tail! + return unless @cursor + @file.seek @cursor + + unless @file.eof? + contents = @file.read + @cursor = @file.tell + $stdout.print contents + end + end + end + end +end diff --git a/railties/lib/rails/rack/logger.rb b/railties/lib/rails/rack/logger.rb new file mode 100644 index 0000000000..9962e6d943 --- /dev/null +++ b/railties/lib/rails/rack/logger.rb @@ -0,0 +1,86 @@ +require 'active_support/core_ext/time/conversions' +require 'active_support/core_ext/object/blank' +require 'active_support/log_subscriber' +require 'action_dispatch/http/request' +require 'rack/body_proxy' + +module Rails + module Rack + # Sets log tags, logs the request, calls the app, and flushes the logs. + class Logger < ActiveSupport::LogSubscriber + def initialize(app, taggers = nil) + @app = app + @taggers = taggers || [] + end + + def call(env) + request = ActionDispatch::Request.new(env) + + if logger.respond_to?(:tagged) + logger.tagged(compute_tags(request)) { call_app(request, env) } + else + call_app(request, env) + end + end + + protected + + def call_app(request, env) + # Put some space between requests in development logs. + if development? + logger.debug '' + logger.debug '' + end + + instrumenter = ActiveSupport::Notifications.instrumenter + instrumenter.start 'request.action_dispatch', request: request + logger.info { started_request_message(request) } + resp = @app.call(env) + resp[2] = ::Rack::BodyProxy.new(resp[2]) { finish(request) } + resp + rescue Exception + finish(request) + raise + ensure + ActiveSupport::LogSubscriber.flush_all! + end + + # Started GET "/session/new" for 127.0.0.1 at 2012-09-26 14:51:42 -0700 + def started_request_message(request) + 'Started %s "%s" for %s at %s' % [ + request.request_method, + request.filtered_path, + request.ip, + Time.now.to_default_s ] + end + + def compute_tags(request) + @taggers.collect do |tag| + case tag + when Proc + tag.call(request) + when Symbol + request.send(tag) + else + tag + end + end + end + + private + + def finish(request) + instrumenter = ActiveSupport::Notifications.instrumenter + instrumenter.finish 'request.action_dispatch', request: request + end + + def development? + Rails.env.development? + end + + def logger + Rails.logger + end + end + end +end diff --git a/railties/lib/rails/railtie.rb b/railties/lib/rails/railtie.rb new file mode 100644 index 0000000000..2b33beaa2b --- /dev/null +++ b/railties/lib/rails/railtie.rb @@ -0,0 +1,250 @@ +require 'rails/initializable' +require 'rails/configuration' +require 'active_support/inflector' +require 'active_support/core_ext/module/introspection' +require 'active_support/core_ext/module/delegation' + +module Rails + # Railtie is the core of the Rails framework and provides several hooks to extend + # Rails and/or modify the initialization process. + # + # Every major component of Rails (Action Mailer, Action Controller, + # Action View and Active Record) is a Railtie. Each of + # them is responsible for their own initialization. This makes Rails itself + # absent of any component hooks, allowing other components to be used in + # place of any of the Rails defaults. + # + # Developing a Rails extension does _not_ require any implementation of + # Railtie, but if you need to interact with the Rails framework during + # or after boot, then Railtie is needed. + # + # For example, an extension doing any of the following would require Railtie: + # + # * creating initializers + # * configuring a Rails framework for the application, like setting a generator + # * adding <tt>config.*</tt> keys to the environment + # * setting up a subscriber with ActiveSupport::Notifications + # * adding rake tasks + # + # == Creating your Railtie + # + # To extend Rails using Railtie, create a Railtie class which inherits + # from Rails::Railtie within your extension's namespace. This class must be + # loaded during the Rails boot process. + # + # The following example demonstrates an extension which can be used with or without Rails. + # + # # lib/my_gem/railtie.rb + # module MyGem + # class Railtie < Rails::Railtie + # end + # end + # + # # lib/my_gem.rb + # require 'my_gem/railtie' if defined?(Rails) + # + # == Initializers + # + # To add an initialization step from your Railtie to Rails boot process, you just need + # to create an initializer block: + # + # class MyRailtie < Rails::Railtie + # initializer "my_railtie.configure_rails_initialization" do + # # some initialization behavior + # end + # end + # + # If specified, the block can also receive the application object, in case you + # need to access some application specific configuration, like middleware: + # + # class MyRailtie < Rails::Railtie + # initializer "my_railtie.configure_rails_initialization" do |app| + # app.middleware.use MyRailtie::Middleware + # end + # end + # + # Finally, you can also pass <tt>:before</tt> and <tt>:after</tt> as option to initializer, + # in case you want to couple it with a specific step in the initialization process. + # + # == Configuration + # + # Inside the Railtie class, you can access a config object which contains configuration + # shared by all railties and the application: + # + # class MyRailtie < Rails::Railtie + # # Customize the ORM + # config.app_generators.orm :my_railtie_orm + # + # # Add a to_prepare block which is executed once in production + # # and before each request in development + # config.to_prepare do + # MyRailtie.setup! + # end + # end + # + # == Loading rake tasks and generators + # + # If your railtie has rake tasks, you can tell Rails to load them through the method + # rake_tasks: + # + # class MyRailtie < Rails::Railtie + # rake_tasks do + # load "path/to/my_railtie.tasks" + # end + # end + # + # By default, Rails load generators from your load path. However, if you want to place + # your generators at a different location, you can specify in your Railtie a block which + # will load them during normal generators lookup: + # + # class MyRailtie < Rails::Railtie + # generators do + # require "path/to/my_railtie_generator" + # end + # end + # + # == Application and Engine + # + # A Rails::Engine is nothing more than a Railtie with some initializers already set. + # And since Rails::Application is an engine, the same configuration described here + # can be used in both. + # + # Be sure to look at the documentation of those specific classes for more information. + # + class Railtie + autoload :Configuration, "rails/railtie/configuration" + + include Initializable + + ABSTRACT_RAILTIES = %w(Rails::Railtie Rails::Engine Rails::Application) + + class << self + private :new + delegate :config, to: :instance + + def subclasses + @subclasses ||= [] + end + + def inherited(base) + unless base.abstract_railtie? + subclasses << base + end + end + + def rake_tasks(&blk) + @rake_tasks ||= [] + @rake_tasks << blk if blk + @rake_tasks + end + + def console(&blk) + @load_console ||= [] + @load_console << blk if blk + @load_console + end + + def runner(&blk) + @load_runner ||= [] + @load_runner << blk if blk + @load_runner + end + + def generators(&blk) + @generators ||= [] + @generators << blk if blk + @generators + end + + def abstract_railtie? + ABSTRACT_RAILTIES.include?(name) + end + + def railtie_name(name = nil) + @railtie_name = name.to_s if name + @railtie_name ||= generate_railtie_name(self.name) + end + + # Since Rails::Railtie cannot be instantiated, any methods that call + # +instance+ are intended to be called only on subclasses of a Railtie. + def instance + @instance ||= new + end + + def respond_to_missing?(*args) + instance.respond_to?(*args) || super + end + + # Allows you to configure the railtie. This is the same method seen in + # Railtie::Configurable, but this module is no longer required for all + # subclasses of Railtie so we provide the class method here. + def configure(&block) + instance.configure(&block) + end + + protected + def generate_railtie_name(string) + ActiveSupport::Inflector.underscore(string).tr("/", "_") + end + + # If the class method does not have a method, then send the method call + # to the Railtie instance. + def method_missing(name, *args, &block) + if instance.respond_to?(name) + instance.public_send(name, *args, &block) + else + super + end + end + end + + delegate :railtie_name, to: :class + + def initialize + if self.class.abstract_railtie? + raise "#{self.class.name} is abstract, you cannot instantiate it directly." + end + end + + def configure(&block) + instance_eval(&block) + end + + def config + @config ||= Railtie::Configuration.new + end + + def railtie_namespace + @railtie_namespace ||= self.class.parents.detect { |n| n.respond_to?(:railtie_namespace) } + end + + protected + + def run_console_blocks(app) #:nodoc: + each_registered_block(:console) { |block| block.call(app) } + end + + def run_generators_blocks(app) #:nodoc: + each_registered_block(:generators) { |block| block.call(app) } + end + + def run_runner_blocks(app) #:nodoc: + each_registered_block(:runner) { |block| block.call(app) } + end + + def run_tasks_blocks(app) #:nodoc: + extend Rake::DSL + each_registered_block(:rake_tasks) { |block| instance_exec(app, &block) } + end + + private + + def each_registered_block(type, &block) + klass = self.class + while klass.respond_to?(type) + klass.public_send(type).each(&block) + klass = klass.superclass + end + end + end +end diff --git a/railties/lib/rails/railtie/configurable.rb b/railties/lib/rails/railtie/configurable.rb new file mode 100644 index 0000000000..1572af0b2a --- /dev/null +++ b/railties/lib/rails/railtie/configurable.rb @@ -0,0 +1,35 @@ +require 'active_support/concern' + +module Rails + class Railtie + module Configurable + extend ActiveSupport::Concern + + module ClassMethods + delegate :config, to: :instance + + def inherited(base) + raise "You cannot inherit from a #{self.superclass.name} child" + end + + def instance + @instance ||= new + end + + def respond_to?(*args) + super || instance.respond_to?(*args) + end + + def configure(&block) + class_eval(&block) + end + + protected + + def method_missing(*args, &block) + instance.send(*args, &block) + end + end + end + end +end diff --git a/railties/lib/rails/railtie/configuration.rb b/railties/lib/rails/railtie/configuration.rb new file mode 100644 index 0000000000..eb3b2d8ef4 --- /dev/null +++ b/railties/lib/rails/railtie/configuration.rb @@ -0,0 +1,100 @@ +require 'rails/configuration' + +module Rails + class Railtie + class Configuration + def initialize + @@options ||= {} + end + + # Expose the eager_load_namespaces at "module" level for convenience. + def self.eager_load_namespaces #:nodoc: + @@eager_load_namespaces ||= [] + end + + # All namespaces that are eager loaded + def eager_load_namespaces + @@eager_load_namespaces ||= [] + end + + # Add files that should be watched for change. + def watchable_files + @@watchable_files ||= [] + end + + # Add directories that should be watched for change. + # The key of the hashes should be directories and the values should + # be an array of extensions to match in each directory. + def watchable_dirs + @@watchable_dirs ||= {} + end + + # This allows you to modify the application's middlewares from Engines. + # + # All operations you run on the app_middleware will be replayed on the + # application once it is defined and the default_middlewares are + # created + def app_middleware + @@app_middleware ||= Rails::Configuration::MiddlewareStackProxy.new + end + + # This allows you to modify application's generators from Railties. + # + # Values set on app_generators will become defaults for application, unless + # application overwrites them. + def app_generators + @@app_generators ||= Rails::Configuration::Generators.new + yield(@@app_generators) if block_given? + @@app_generators + end + + # First configurable block to run. Called before any initializers are run. + def before_configuration(&block) + ActiveSupport.on_load(:before_configuration, yield: true, &block) + end + + # Third configurable block to run. Does not run if +config.cache_classes+ + # set to false. + def before_eager_load(&block) + ActiveSupport.on_load(:before_eager_load, yield: true, &block) + end + + # Second configurable block to run. Called before frameworks initialize. + def before_initialize(&block) + ActiveSupport.on_load(:before_initialize, yield: true, &block) + end + + # Last configurable block to run. Called after frameworks initialize. + def after_initialize(&block) + ActiveSupport.on_load(:after_initialize, yield: true, &block) + end + + # Array of callbacks defined by #to_prepare. + def to_prepare_blocks + @@to_prepare_blocks ||= [] + end + + # Defines generic callbacks to run before #after_initialize. Useful for + # Rails::Railtie subclasses. + def to_prepare(&blk) + to_prepare_blocks << blk if blk + end + + def respond_to?(name, include_private = false) + super || @@options.key?(name.to_sym) + end + + private + + def method_missing(name, *args, &blk) + if name.to_s =~ /=$/ + @@options[$`.to_sym] = args.first + elsif @@options.key?(name) + @@options[name] + else + super + end + end + end + end +end diff --git a/railties/lib/rails/ruby_version_check.rb b/railties/lib/rails/ruby_version_check.rb new file mode 100644 index 0000000000..df74643a59 --- /dev/null +++ b/railties/lib/rails/ruby_version_check.rb @@ -0,0 +1,13 @@ +if RUBY_VERSION < '1.9.3' + desc = defined?(RUBY_DESCRIPTION) ? RUBY_DESCRIPTION : "ruby #{RUBY_VERSION} (#{RUBY_RELEASE_DATE})" + abort <<-end_message + + Rails 4 prefers to run on Ruby 2.1 or newer. + + You're running + #{desc} + + Please upgrade to Ruby 1.9.3 or newer to continue. + + end_message +end diff --git a/railties/lib/rails/rubyprof_ext.rb b/railties/lib/rails/rubyprof_ext.rb new file mode 100644 index 0000000000..017eba3a76 --- /dev/null +++ b/railties/lib/rails/rubyprof_ext.rb @@ -0,0 +1,35 @@ +require 'prof' + +module Prof #:nodoc: + # Adapted from Shugo Maeda's unprof.rb + def self.print_profile(results, io = $stderr) + total = results.detect { |i| + i.method_class.nil? && i.method_id == :"#toplevel" + }.total_time + total = 0.001 if total < 0.001 + + io.puts " %% cumulative self self total" + io.puts " time seconds seconds calls ms/call ms/call name" + + sum = 0.0 + results.each do |r| + sum += r.self_time + + name = if r.method_class.nil? + r.method_id.to_s + elsif r.method_class.is_a?(Class) + "#{r.method_class}##{r.method_id}" + else + "#{r.method_class}.#{r.method_id}" + end + io.printf "%6.2f %8.3f %8.3f %8d %8.2f %8.2f %s\n", + r.self_time / total * 100, + sum, + r.self_time, + r.count, + r.self_time * 1000 / r.count, + r.total_time * 1000 / r.count, + name + end + end +end diff --git a/railties/lib/rails/source_annotation_extractor.rb b/railties/lib/rails/source_annotation_extractor.rb new file mode 100644 index 0000000000..201532d299 --- /dev/null +++ b/railties/lib/rails/source_annotation_extractor.rb @@ -0,0 +1,134 @@ +# Implements the logic behind the rake tasks for annotations like +# +# rake notes +# rake notes:optimize +# +# and friends. See <tt>rake -T notes</tt> and <tt>railties/lib/tasks/annotations.rake</tt>. +# +# Annotation objects are triplets <tt>:line</tt>, <tt>:tag</tt>, <tt>:text</tt> that +# represent the line where the annotation lives, its tag, and its text. Note +# the filename is not stored. +# +# Annotations are looked for in comments and modulus whitespace they have to +# start with the tag optionally followed by a colon. Everything up to the end +# of the line (or closing ERB comment tag) is considered to be their text. +class SourceAnnotationExtractor + class Annotation < Struct.new(:line, :tag, :text) + def self.directories + @@directories ||= %w(app config db lib test) + (ENV['SOURCE_ANNOTATION_DIRECTORIES'] || '').split(',') + end + + def self.extensions + @@extensions ||= {} + end + + # Registers new Annotations File Extensions + # SourceAnnotationExtractor::Annotation.register_extensions("css", "scss", "sass", "less", "js") { |tag| /\/\/\s*(#{tag}):?\s*(.*)$/ } + def self.register_extensions(*exts, &block) + extensions[/\.(#{exts.join("|")})$/] = block + end + + register_extensions("builder", "rb", "rake", "yml", "yaml", "ruby") { |tag| /#\s*(#{tag}):?\s*(.*)$/ } + register_extensions("css", "js") { |tag| /\/\/\s*(#{tag}):?\s*(.*)$/ } + register_extensions("erb") { |tag| /<%\s*#\s*(#{tag}):?\s*(.*?)\s*%>/ } + + # Returns a representation of the annotation that looks like this: + # + # [126] [TODO] This algorithm is simple and clearly correct, make it faster. + # + # If +options+ has a flag <tt>:tag</tt> the tag is shown as in the example above. + # Otherwise the string contains just line and text. + def to_s(options={}) + s = "[#{line.to_s.rjust(options[:indent])}] " + s << "[#{tag}] " if options[:tag] + s << text + end + end + + # Prints all annotations with tag +tag+ under the root directories +app+, + # +config+, +db+, +lib+, and +test+ (recursively). + # + # Additional directories may be added using a comma-delimited list set using + # <tt>ENV['SOURCE_ANNOTATION_DIRECTORIES']</tt>. + # + # Directories may also be explicitly set using the <tt>:dirs</tt> key in +options+. + # + # SourceAnnotationExtractor.enumerate 'TODO|FIXME', dirs: %w(app lib), tag: true + # + # If +options+ has a <tt>:tag</tt> flag, it will be passed to each annotation's +to_s+. + # + # See <tt>#find_in</tt> for a list of file extensions that will be taken into account. + # + # This class method is the single entry point for the rake tasks. + def self.enumerate(tag, options={}) + extractor = new(tag) + dirs = options.delete(:dirs) || Annotation.directories + extractor.display(extractor.find(dirs), options) + end + + attr_reader :tag + + def initialize(tag) + @tag = tag + end + + # Returns a hash that maps filenames under +dirs+ (recursively) to arrays + # with their annotations. + def find(dirs) + dirs.inject({}) { |h, dir| h.update(find_in(dir)) } + end + + # Returns a hash that maps filenames under +dir+ (recursively) to arrays + # with their annotations. Only files with annotations are included. Files + # with extension +.builder+, +.rb+, +.erb+, +.haml+, +.slim+, +.css+, + # +.scss+, +.js+, +.coffee+, +.rake+, +.sass+ and +.less+ + # are taken into account. + def find_in(dir) + results = {} + + Dir.glob("#{dir}/*") do |item| + next if File.basename(item)[0] == ?. + + if File.directory?(item) + results.update(find_in(item)) + else + extension = Annotation.extensions.detect do |regexp, _block| + regexp.match(item) + end + + if extension + pattern = extension.last.call(tag) + results.update(extract_annotations_from(item, pattern)) if pattern + end + end + end + + results + end + + # If +file+ is the filename of a file that contains annotations this method returns + # a hash with a single entry that maps +file+ to an array of its annotations. + # Otherwise it returns an empty hash. + def extract_annotations_from(file, pattern) + lineno = 0 + result = File.readlines(file).inject([]) do |list, line| + lineno += 1 + next list unless line =~ pattern + list << Annotation.new(lineno, $1, $2) + end + result.empty? ? {} : { file => result } + end + + # Prints the mapping from filenames to annotations in +results+ ordered by filename. + # The +options+ hash is passed to each annotation's +to_s+. + def display(results, options={}) + options[:indent] = results.flat_map { |f, a| a.map(&:line) }.max.to_s.size + results.keys.sort.each do |file| + puts "#{file}:" + results[file].each do |note| + puts " * #{note.to_s(options)}" + end + puts + end + end +end diff --git a/railties/lib/rails/tasks.rb b/railties/lib/rails/tasks.rb new file mode 100644 index 0000000000..af5f2707b1 --- /dev/null +++ b/railties/lib/rails/tasks.rb @@ -0,0 +1,14 @@ +# Load Rails Rakefile extensions +%w( + annotations + documentation + framework + log + middleware + misc + routes + statistics + tmp +).each do |task| + load "rails/tasks/#{task}.rake" +end diff --git a/railties/lib/rails/tasks/annotations.rake b/railties/lib/rails/tasks/annotations.rake new file mode 100644 index 0000000000..386ecf44be --- /dev/null +++ b/railties/lib/rails/tasks/annotations.rake @@ -0,0 +1,20 @@ +require 'rails/source_annotation_extractor' + +desc "Enumerate all annotations (use notes:optimize, :fixme, :todo for focus)" +task :notes do + SourceAnnotationExtractor.enumerate "OPTIMIZE|FIXME|TODO", tag: true +end + +namespace :notes do + ["OPTIMIZE", "FIXME", "TODO"].each do |annotation| + # desc "Enumerate all #{annotation} annotations" + task annotation.downcase.intern do + SourceAnnotationExtractor.enumerate annotation + end + end + + desc "Enumerate a custom annotation, specify with ANNOTATION=CUSTOM" + task :custom do + SourceAnnotationExtractor.enumerate ENV['ANNOTATION'] + end +end
\ No newline at end of file diff --git a/railties/lib/rails/tasks/documentation.rake b/railties/lib/rails/tasks/documentation.rake new file mode 100644 index 0000000000..8544890553 --- /dev/null +++ b/railties/lib/rails/tasks/documentation.rake @@ -0,0 +1,70 @@ +begin + require 'rdoc/task' +rescue LoadError + # Rubinius installs RDoc as a gem, and for this interpreter "rdoc/task" is + # available only if the application bundle includes "rdoc" (normally as a + # dependency of the "sdoc" gem.) + # + # If RDoc is not available it is fine that we do not generate the tasks that + # depend on it. Just be robust to this gotcha and go on. +else + require 'rails/api/task' + + # Monkey-patch to remove redoc'ing and clobber descriptions to cut down on rake -T noise + class RDocTaskWithoutDescriptions < RDoc::Task + include ::Rake::DSL + + def define + task rdoc_task_name + + task rerdoc_task_name => [clobber_task_name, rdoc_task_name] + + task clobber_task_name do + rm_r rdoc_dir rescue nil + end + + task :clobber => [clobber_task_name] + + directory @rdoc_dir + task rdoc_task_name => [rdoc_target] + file rdoc_target => @rdoc_files + [Rake.application.rakefile] do + rm_r @rdoc_dir rescue nil + @before_running_rdoc.call if @before_running_rdoc + args = option_list + @rdoc_files + if @external + argstring = args.join(' ') + sh %{ruby -Ivendor vendor/rd #{argstring}} + else + require 'rdoc/rdoc' + RDoc::RDoc.new.document(args) + end + end + self + end + end + + namespace :doc do + RDocTaskWithoutDescriptions.new("app") { |rdoc| + rdoc.rdoc_dir = 'doc/app' + rdoc.template = ENV['template'] if ENV['template'] + rdoc.title = ENV['title'] || "Rails Application Documentation" + rdoc.options << '--line-numbers' + rdoc.options << '--charset' << 'utf-8' + rdoc.rdoc_files.include('README.rdoc') + rdoc.rdoc_files.include('app/**/*.rb') + rdoc.rdoc_files.include('lib/**/*.rb') + } + Rake::Task['doc:app'].comment = "Generate docs for the app -- also available doc:rails, doc:guides (options: TEMPLATE=/rdoc-template.rb, TITLE=\"Custom Title\")" + + # desc 'Generate documentation for the Rails framework.' + Rails::API::AppTask.new('rails') + end +end + +namespace :doc do + task :guides do + rails_gem_dir = Gem::Specification.find_by_name("rails").gem_dir + require File.expand_path(File.join(rails_gem_dir, "/guides/rails_guides")) + RailsGuides::Generator.new(Rails.root.join("doc/guides")).generate + end +end diff --git a/railties/lib/rails/tasks/engine.rake b/railties/lib/rails/tasks/engine.rake new file mode 100644 index 0000000000..16ad1bfc84 --- /dev/null +++ b/railties/lib/rails/tasks/engine.rake @@ -0,0 +1,72 @@ +task "load_app" do + namespace :app do + load APP_RAKEFILE + end + task :environment => "app:environment" + + if !defined?(ENGINE_PATH) || !ENGINE_PATH + ENGINE_PATH = find_engine_path(APP_RAKEFILE) + end +end + +def app_task(name) + task name => [:load_app, "app:db:#{name}"] +end + +namespace :db do + app_task "reset" + + desc "Migrate the database (options: VERSION=x, VERBOSE=false)." + app_task "migrate" + app_task "migrate:up" + app_task "migrate:down" + app_task "migrate:redo" + app_task "migrate:reset" + + desc "Display status of migrations" + app_task "migrate:status" + + desc 'Create the database from config/database.yml for the current Rails.env (use db:create:all to create all databases in the config)' + app_task "create" + app_task "create:all" + + desc 'Drops the database for the current Rails.env (use db:drop:all to drop all databases)' + app_task "drop" + app_task "drop:all" + + desc "Load fixtures into the current environment's database." + app_task "fixtures:load" + + desc "Rolls the schema back to the previous version (specify steps w/ STEP=n)." + app_task "rollback" + + desc "Create a db/schema.rb file that can be portably used against any DB supported by AR" + app_task "schema:dump" + + desc "Load a schema.rb file into the database" + app_task "schema:load" + + desc "Load the seed data from db/seeds.rb" + app_task "seed" + + desc "Create the database, load the schema, and initialize with the seed data (use db:reset to also drop the db first)" + app_task "setup" + + desc "Dump the database structure to an SQL file" + app_task "structure:dump" + + desc "Retrieves the current schema version number" + app_task "version" +end + +def find_engine_path(path) + return File.expand_path(Dir.pwd) if path == "/" + + if Rails::Engine.find(path) + path + else + find_engine_path(File.expand_path('..', path)) + end +end + +Rake.application.invoke_task(:load_app) diff --git a/railties/lib/rails/tasks/framework.rake b/railties/lib/rails/tasks/framework.rake new file mode 100644 index 0000000000..a1c805f8aa --- /dev/null +++ b/railties/lib/rails/tasks/framework.rake @@ -0,0 +1,66 @@ +namespace :rails do + desc "Update configs and some other initially generated files (or use just update:configs or update:bin)" + task update: [ "update:configs", "update:bin" ] + + desc "Applies the template supplied by LOCATION=(/path/to/template) or URL" + task template: :environment do + template = ENV["LOCATION"] + raise "No LOCATION value given. Please set LOCATION either as path to a file or a URL" if template.blank? + template = File.expand_path(template) if template !~ %r{\A[A-Za-z][A-Za-z0-9+\-\.]*://} + require 'rails/generators' + require 'rails/generators/rails/app/app_generator' + generator = Rails::Generators::AppGenerator.new [Rails.root], {}, destination_root: Rails.root + generator.apply template, verbose: false + end + + namespace :templates do + # desc "Copy all the templates from rails to the application directory for customization. Already existing local copies will be overwritten" + task :copy do + generators_lib = File.expand_path("../../generators", __FILE__) + project_templates = "#{Rails.root}/lib/templates" + + default_templates = { "erb" => %w{controller mailer scaffold}, + "rails" => %w{controller helper scaffold_controller assets} } + + default_templates.each do |type, names| + local_template_type_dir = File.join(project_templates, type) + FileUtils.mkdir_p local_template_type_dir + + names.each do |name| + dst_name = File.join(local_template_type_dir, name) + src_name = File.join(generators_lib, type, name, "templates") + FileUtils.cp_r src_name, dst_name + end + end + end + end + + namespace :update do + def invoke_from_app_generator(method) + app_generator.send(method) + end + + def app_generator + @app_generator ||= begin + require 'rails/generators' + require 'rails/generators/rails/app/app_generator' + gen = Rails::Generators::AppGenerator.new ["rails"], { with_dispatchers: true }, + destination_root: Rails.root + File.exist?(Rails.root.join("config", "application.rb")) ? + gen.send(:app_const) : gen.send(:valid_const?) + gen + end + end + + # desc "Update config/boot.rb from your current rails install" + task :configs do + invoke_from_app_generator :create_boot_file + invoke_from_app_generator :update_config_files + end + + # desc "Adds new executables to the application bin/ directory" + task :bin do + invoke_from_app_generator :create_bin_files + end + end +end diff --git a/railties/lib/rails/tasks/log.rake b/railties/lib/rails/tasks/log.rake new file mode 100644 index 0000000000..877f175ef3 --- /dev/null +++ b/railties/lib/rails/tasks/log.rake @@ -0,0 +1,23 @@ +namespace :log do + desc "Truncates all *.log files in log/ to zero bytes (specify which logs with LOGS=test,development)" + task :clear do + log_files.each do |file| + clear_log_file(file) + end + end + + def log_files + if ENV['LOGS'] + ENV['LOGS'].split(',') + .map { |file| "log/#{file.strip}.log" } + .select { |file| File.exist?(file) } + else + FileList["log/*.log"] + end + end + + def clear_log_file(file) + f = File.open(file, "w") + f.close + end +end diff --git a/railties/lib/rails/tasks/middleware.rake b/railties/lib/rails/tasks/middleware.rake new file mode 100644 index 0000000000..31e961b483 --- /dev/null +++ b/railties/lib/rails/tasks/middleware.rake @@ -0,0 +1,7 @@ +desc 'Prints out your Rack middleware stack' +task middleware: :environment do + Rails.configuration.middleware.each do |middleware| + puts "use #{middleware.inspect}" + end + puts "run #{Rails.application.class.name}.routes" +end diff --git a/railties/lib/rails/tasks/misc.rake b/railties/lib/rails/tasks/misc.rake new file mode 100644 index 0000000000..4195106961 --- /dev/null +++ b/railties/lib/rails/tasks/misc.rake @@ -0,0 +1,60 @@ +desc 'Generate a cryptographically secure secret key (this is typically used to generate a secret for cookie sessions).' +task :secret do + require 'securerandom' + puts SecureRandom.hex(64) +end + +desc 'List versions of all Rails frameworks and the environment' +task about: :environment do + puts Rails::Info +end + +namespace :time do + namespace :zones do + desc 'Displays all time zones, also available: time:zones:us, time:zones:local -- filter with OFFSET parameter, e.g., OFFSET=-6' + task :all do + build_time_zone_list(:all) + end + + # desc 'Displays names of US time zones recognized by the Rails TimeZone class, grouped by offset. Results can be filtered with optional OFFSET parameter, e.g., OFFSET=-6' + task :us do + build_time_zone_list(:us_zones) + end + + # desc 'Displays names of time zones recognized by the Rails TimeZone class with the same offset as the system local time' + task :local do + require 'active_support' + require 'active_support/time' + jan_offset = Time.now.beginning_of_year.utc_offset + jul_offset = Time.now.beginning_of_year.change(month: 7).utc_offset + offset = jan_offset < jul_offset ? jan_offset : jul_offset + build_time_zone_list(:all, offset) + end + + # to find UTC -06:00 zones, OFFSET can be set to either -6, -6:00 or 21600 + def build_time_zone_list(method, offset = ENV['OFFSET']) + require 'active_support' + require 'active_support/time' + if offset + offset = if offset.to_s.match(/(\+|-)?(\d+):(\d+)/) + sign = $1 == '-' ? -1 : 1 + hours, minutes = $2.to_f, $3.to_f + ((hours * 3600) + (minutes.to_f * 60)) * sign + elsif offset.to_f.abs <= 13 + offset.to_f * 3600 + else + offset.to_f + end + end + previous_offset = nil + ActiveSupport::TimeZone.__send__(method).each do |zone| + if offset.nil? || offset == zone.utc_offset + puts "\n* UTC #{zone.formatted_offset} *" unless zone.utc_offset == previous_offset + puts zone.name + previous_offset = zone.utc_offset + end + end + puts "\n" + end + end +end diff --git a/railties/lib/rails/tasks/routes.rake b/railties/lib/rails/tasks/routes.rake new file mode 100644 index 0000000000..1815c2fdc7 --- /dev/null +++ b/railties/lib/rails/tasks/routes.rake @@ -0,0 +1,7 @@ +desc 'Print out all defined routes in match order, with names. Target specific controller with CONTROLLER=x.' +task routes: :environment do + all_routes = Rails.application.routes.routes + require 'action_dispatch/routing/inspector' + inspector = ActionDispatch::Routing::RoutesInspector.new(all_routes) + puts inspector.format(ActionDispatch::Routing::ConsoleFormatter.new, ENV['CONTROLLER']) +end diff --git a/railties/lib/rails/tasks/statistics.rake b/railties/lib/rails/tasks/statistics.rake new file mode 100644 index 0000000000..ae5a7d2759 --- /dev/null +++ b/railties/lib/rails/tasks/statistics.rake @@ -0,0 +1,27 @@ +# while having global constant is not good, +# many 3rd party tools depend on it, like rspec-rails, cucumber-rails, etc +# so if will be removed - deprecation warning is needed +STATS_DIRECTORIES = [ + %w(Controllers app/controllers), + %w(Helpers app/helpers), + %w(Models app/models), + %w(Mailers app/mailers), + %w(Javascripts app/assets/javascripts), + %w(Libraries lib/), + %w(APIs app/apis), + %w(Controller\ tests test/controllers), + %w(Helper\ tests test/helpers), + %w(Model\ tests test/models), + %w(Mailer\ tests test/mailers), + %w(Integration\ tests test/integration), + %w(Functional\ tests\ (old) test/functional), + %w(Unit\ tests \ (old) test/unit) +].collect do |name, dir| + [ name, "#{File.dirname(Rake.application.rakefile_location)}/#{dir}" ] +end.select { |name, dir| File.directory?(dir) } + +desc "Report code statistics (KLOCs, etc) from the application or engine" +task :stats do + require 'rails/code_statistics' + CodeStatistics.new(*STATS_DIRECTORIES).to_s +end
\ No newline at end of file diff --git a/railties/lib/rails/tasks/tmp.rake b/railties/lib/rails/tasks/tmp.rake new file mode 100644 index 0000000000..116988665f --- /dev/null +++ b/railties/lib/rails/tasks/tmp.rake @@ -0,0 +1,45 @@ +namespace :tmp do + desc "Clear session, cache, and socket files from tmp/ (narrow w/ tmp:sessions:clear, tmp:cache:clear, tmp:sockets:clear)" + task clear: [ "tmp:sessions:clear", "tmp:cache:clear", "tmp:sockets:clear"] + + tmp_dirs = [ 'tmp/sessions', + 'tmp/cache', + 'tmp/sockets', + 'tmp/pids', + 'tmp/cache/assets/development', + 'tmp/cache/assets/test', + 'tmp/cache/assets/production' ] + + tmp_dirs.each { |d| directory d } + + desc "Creates tmp directories for sessions, cache, sockets, and pids" + task create: tmp_dirs + + namespace :sessions do + # desc "Clears all files in tmp/sessions" + task :clear do + FileUtils.rm(Dir['tmp/sessions/[^.]*']) + end + end + + namespace :cache do + # desc "Clears all files and directories in tmp/cache" + task :clear do + FileUtils.rm_rf(Dir['tmp/cache/[^.]*']) + end + end + + namespace :sockets do + # desc "Clears all files in tmp/sockets" + task :clear do + FileUtils.rm(Dir['tmp/sockets/[^.]*']) + end + end + + namespace :pids do + # desc "Clears all files in tmp/pids" + task :clear do + FileUtils.rm(Dir['tmp/pids/[^.]*']) + end + end +end diff --git a/railties/lib/rails/templates/layouts/application.html.erb b/railties/lib/rails/templates/layouts/application.html.erb new file mode 100644 index 0000000000..5aecaa712a --- /dev/null +++ b/railties/lib/rails/templates/layouts/application.html.erb @@ -0,0 +1,36 @@ +<!DOCTYPE html> +<html lang="en"> +<head> + <meta charset="utf-8" /> + <title><%= @page_title %></title> + <style> + body { background-color: #fff; color: #333; } + + body, p, ol, ul, td { + font-family: helvetica, verdana, arial, sans-serif; + font-size: 13px; + line-height: 18px; + } + + pre { + background-color: #eee; + padding: 10px; + font-size: 11px; + white-space: pre-wrap; + } + + a { color: #000; } + a:visited { color: #666; } + a:hover { color: #fff; background-color:#000; } + + h2 { padding-left: 10px; } + + <%= yield :style %> + </style> +</head> +<body> + +<%= yield %> + +</body> +</html> diff --git a/railties/lib/rails/templates/rails/info/properties.html.erb b/railties/lib/rails/templates/rails/info/properties.html.erb new file mode 100644 index 0000000000..d47cbab202 --- /dev/null +++ b/railties/lib/rails/templates/rails/info/properties.html.erb @@ -0,0 +1 @@ +<%= @info.html_safe %>
\ No newline at end of file diff --git a/railties/lib/rails/templates/rails/info/routes.html.erb b/railties/lib/rails/templates/rails/info/routes.html.erb new file mode 100644 index 0000000000..2d8a190986 --- /dev/null +++ b/railties/lib/rails/templates/rails/info/routes.html.erb @@ -0,0 +1,9 @@ +<h2> + Routes +</h2> + +<p> + Routes match in priority from top to bottom +</p> + +<%= @routes_inspector.format(ActionDispatch::Routing::HtmlTableFormatter.new(self)) %> diff --git a/railties/lib/rails/templates/rails/mailers/email.html.erb b/railties/lib/rails/templates/rails/mailers/email.html.erb new file mode 100644 index 0000000000..977feb922b --- /dev/null +++ b/railties/lib/rails/templates/rails/mailers/email.html.erb @@ -0,0 +1,98 @@ +<!DOCTYPE html> +<html><head> +<meta name="viewport" content="width=device-width" /> +<style type="text/css"> + header { + width: 100%; + padding: 10px 0 0 0; + margin: 0; + background: white; + font: 12px "Lucida Grande", sans-serif; + border-bottom: 1px solid #dedede; + overflow: hidden; + } + + dl { + margin: 0 0 10px 0; + padding: 0; + } + + dt { + width: 80px; + padding: 1px; + float: left; + clear: left; + text-align: right; + color: #7f7f7f; + } + + dd { + margin-left: 90px; /* 80px + 10px */ + padding: 1px; + } + + iframe { + border: 0; + width: 100%; + height: 800px; + } +</style> +</head> + +<body> +<header> + <dl> + <% if @email.respond_to?(:smtp_envelope_from) && Array(@email.from) != Array(@email.smtp_envelope_from) %> + <dt>SMTP-From:</dt> + <dd><%= @email.smtp_envelope_from %></dd> + <% end %> + + <% if @email.respond_to?(:smtp_envelope_to) && @email.to != @email.smtp_envelope_to %> + <dt>SMTP-To:</dt> + <dd><%= @email.smtp_envelope_to %></dd> + <% end %> + + <dt>From:</dt> + <dd><%= @email.header['from'] %></dd> + + <% if @email.reply_to %> + <dt>Reply-To:</dt> + <dd><%= @email.header['reply-to'] %></dd> + <% end %> + + <dt>To:</dt> + <dd><%= @email.header['to'] %></dd> + + <% if @email.cc %> + <dt>CC:</dt> + <dd><%= @email.header['cc'] %></dd> + <% end %> + + <dt>Date:</dt> + <dd><%= Time.current.rfc2822 %></dd> + + <dt>Subject:</dt> + <dd><strong><%= @email.subject %></strong></dd> + + <% unless @email.attachments.nil? || @email.attachments.empty? %> + <dt>Attachments:</dt> + <dd> + <%= @email.attachments.map { |a| a.respond_to?(:original_filename) ? a.original_filename : a.filename }.inspect %> + </dd> + <% end %> + + <% if @email.multipart? %> + <dd> + <select onchange="document.getElementsByName('messageBody')[0].src=this.options[this.selectedIndex].value;"> + <option <%= request.format == Mime::HTML ? 'selected' : '' %> value="?part=text%2Fhtml">View as HTML email</option> + <option <%= request.format == Mime::TEXT ? 'selected' : '' %> value="?part=text%2Fplain">View as plain-text email</option> + </select> + </dd> + <% end %> + </dl> +</header> + +<iframe seamless name="messageBody" src="?part=<%= Rack::Utils.escape(@part.mime_type) %>"></iframe> + +</body> +</html>
\ No newline at end of file diff --git a/railties/lib/rails/templates/rails/mailers/index.html.erb b/railties/lib/rails/templates/rails/mailers/index.html.erb new file mode 100644 index 0000000000..c4c9757d57 --- /dev/null +++ b/railties/lib/rails/templates/rails/mailers/index.html.erb @@ -0,0 +1,8 @@ +<% @previews.each do |preview| %> +<h3><%= link_to preview.preview_name.titleize, "/rails/mailers/#{preview.preview_name}" %></h3> +<ul> +<% preview.emails.each do |email| %> +<li><%= link_to email, "/rails/mailers/#{preview.preview_name}/#{email}" %></li> +<% end %> +</ul> +<% end %> diff --git a/railties/lib/rails/templates/rails/mailers/mailer.html.erb b/railties/lib/rails/templates/rails/mailers/mailer.html.erb new file mode 100644 index 0000000000..607c8d1677 --- /dev/null +++ b/railties/lib/rails/templates/rails/mailers/mailer.html.erb @@ -0,0 +1,6 @@ +<h3><%= @preview.preview_name.titleize %></h3> +<ul> +<% @preview.emails.each do |email| %> +<li><%= link_to email, "/rails/mailers/#{@preview.preview_name}/#{email}" %></li> +<% end %> +</ul> diff --git a/railties/lib/rails/templates/rails/welcome/index.html.erb b/railties/lib/rails/templates/rails/welcome/index.html.erb new file mode 100644 index 0000000000..eb620caa00 --- /dev/null +++ b/railties/lib/rails/templates/rails/welcome/index.html.erb @@ -0,0 +1,245 @@ +<!DOCTYPE html> +<html> + <head> + <title>Ruby on Rails: Welcome aboard</title> + <style media="screen"> + body { + margin: 0; + margin-bottom: 25px; + padding: 0; + background-color: #f0f0f0; + font-family: "Lucida Grande", "Bitstream Vera Sans", "Verdana"; + font-size: 13px; + color: #333; + } + + h1 { + font-size: 28px; + color: #000; + } + + a {color: #03c} + a:hover { + background-color: #03c; + color: white; + text-decoration: none; + } + + + #page { + background-color: #f0f0f0; + width: 750px; + margin: 0; + margin-left: auto; + margin-right: auto; + } + + #content { + float: left; + background-color: white; + border: 3px solid #aaa; + border-top: none; + padding: 25px; + width: 500px; + } + + #sidebar { + float: right; + width: 175px; + } + + #footer { + clear: both; + } + + #header, #about, #getting-started { + padding-left: 75px; + padding-right: 30px; + } + + + #header { + background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADIAAABACAYAAABY1SR7AAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAGZhJREFUeNqsWwmUXGWV/t5Sr9aurl6qO0l3Z9/DEoJh18gZQGAUxPHIyQHH7eioZ8bjnAFHZ0RndNxxRBhGcUbxoKIHBkTEcUYREIHIGpKQjUDS6U660/tSVV3Lq/fefPf/Xy2dBFGYx3npqvde/e/e/97v3u/e/8e4Lt2L8DCCAFcGwF8ZBjYbgM1rAZoO+WLwZhDMu9y4+YcOozbAqzwXNA3GdzX/5hV+KnKO2+GXFj/AvzmW8e72iG202CYiphbY403f9/k3QHZtJ9oWtyCQe7wGX79TKVb7rP9pXJPDVxf0Rz+oyxm4HNWrahFNixdk3EAJbERMWOm4ulctVODNVeEVK0DeRVDgb1wfJgcqUo6duaKnFOH7bm6JmH+5LOEgZprwRIHAV3JYfLjKM55Noz3bBqdcgt0Wg52Kq/cHHkXns0qIukKBlltk9rU2QaiouiefPQ+RdBuseAJeqYTK1CTH8mE4NsyIpRWu8nssCs+xULWpjGVwTvieKl/sV6mIXzOib/OftzuG8d6l8SiVMODyRb46oazg8YPP2Wnvy9ISNqplzsxYAW6hjGhHEmYiBoPC+hRMfFMrESgrBC5n0KS+lq1nPahZh2OXymg9bSNWX/u3FKyKI//7Exx96B4Y8RiCEseq8t0VznyxjMDidFIJ8QSf3hJEOFbZEAHVhIkFTX54fxtnIW5pJUQIeZ8ooZShkInuDOLpFIX1ldtCBix7KI/k4E7OwbTjcNIdiCQzsONp2LEk7GgUnZsuQN9lW2En45xlukrUghWzeZq8FsXsi8+gND6MSCqD9k3nwulIUShKZxt0LYPWortRSY0NXreC8J6pZNDChEDh53PT1NIPLaEnLbQKNTETEaR7sycA0jD1INXZAnzObjTbiWh7Vr1A3Knn4nciu+lCvstUig09cp96cVCtcELoFpEIFUjjyIM/osWIg+IMXS3DcfNwZ3NQHmmKU9OqroX2jWdgatduuPkpmA4ViZrK9RqKABEBtg9tDeW+oUIyTIYuFaX7eCG4aqbU+hhKocD3UBoZISBLiC9cpAQKyq5SQo6OjVswtec5VHLTiHUuIN4WonXlqUj2riS0DIUXwZlERFHSK+SQGzqI3MHdmNm7CzMvvowF527B8qvejZ3/+iXk9vVTao5tiTKN0OUHISZEGS/8W6UbRdoTSHe3E1f+CRaR3xhBLVJSIQ7qleZQGBigZYoYdR+ElUjBaW3H6JMPIrV0Hdo2bEayZ7my0KsdLctPBS64EuWZMYw/9wTGnvod0mtzWH71Vuz66o10bVpK8FIx6orUMejpCKYBTvfM9HXBJtA8z3/1BKDivaksVJmaYsgsYPDnd6LzzAuw8I1XUIGleC1HtDWLnguv5BiX4+jDD2D4sQeV1bQvNXBi6vAb1MGtrEEHjRPgqfZ0qMRJElYYSudfq12nmzAvtJ2yib69iRadRGnySD0Uv5bDtCPou/gqnPY3N6DnLRczgtHxCf4aVnUeUdgw6i6FqM1w292Ujo/TJdB5wHcJ2iDCaBTRmVfw4rkw4yksuvQyJJf0YvrgNiayvBLESS9AYuFqJLLLCPb4SQWulosojhxmeCeoDeaQSoVuy8lPtSKxYKnC2Bmf+DwtvBgv3/qfTI6uEtGuJV7PCBTIq5zNtt5uxBgyvap30pf55TISfX1Y/PatGPrVvcgPvEyAJ1GenaPZLSy//G2IL+qki43CNCMwk620iovy9FGUJgYwm8gwpK/guRJOS5dyD688h+n9z2L28F4Ujx2ia04jEl8Ad3oGVTePaGcnQ3sKLb1rkD3nIqx594dRIh733n6PmmrrvGj671sjVlxczRWAkxZ0r+rTrhfMJ0uEM8xKUYXONR+5nr57BdpP24TCsX6M/f5F5AYLWPauK9F11htUwjOIL8GNZH1qpKwiyVGELk0OoDj2EtziFOaODSN3aC/v24xmZzAU51TgcJKd/DktHo9jyRXvg0Or7PvejTj22KPKiyafew6zg8MYypVLNsLkJ2bxaZXM4i5EmCBPsEaoWJUUpfeSK7DgvEtQmh4ihTDQdf5FOHDHr7HqPVeh99KL4OVzpE50N18CtqnCdBCY6rsEcTsqIGUGD6rY9e3bMPzIHmTWLsbqa7ai84wL6YrTqEyOqEmwonEExSoO//R7dLcJWiWCueF+7P7mjZAUY8YdJZqySMo24j5zQSybQdeyhdrX5imho4NhEEnkRbkDQyjSRVJLeziCgef/6avIrFuOtR95P2lJNSSshg4l6rdm+Ht9inWsqIOX7voN+u/eRoEM5PvHMbbjGcwcfg7jO3YxbCcRiaaYQOXnpEaFGeahGQaMCidJRidt8RghS6Q344XQIowmFq2QXdLNdwsx8zUFqCOQNIECVqdp8pESB53Fvhdux9T2FxBb1AWX4XbjDX/HFzjEmgedB4XYKT5D4T0VTLRCtIiTwOBvfovpvS8T+Bm4MyW6jw13tIIDt/9G/TTWk8HKvzgbmd4+YldYQIdixgHJYkC82Ul6UDnQSbEGdsFGZlEWyUyLyiEyYwajRVAoAXNlEjR+pjUCUmiDQcKOORwwgpFfP4cg5mPzTZ9FoqePdGVWuZRPYQNPcgrd0/dCpqpdy3DIsQ4fxtiTu7Hxkx8iRXkcB+94iM86/K0Jx4opi5aOzGJs14toWeLAdYXWxFQCtJlkA+LUq+bI7QR3mj3YoqVNgGcXd5NWUOiZAk9GH86S4jK25jWBLVREl1uK5Voywz6WXf1WLHjTm0lPigSyxoUpnEqU8c26Wyk/Y24RMjhw/yMoj+cQbWvH0isuwuijL6BwaJwcyq7XUTaBP7N3HOU3ke7HSONJb8RTBGoGKZPFyTE8saTZyCPtrC2coxOoTuY5+x4UTzHNsNjR6d6Qa8JJ5BIV8ksVtKzpwcr3v5dyOrzHKMWXizsZAnK6k1ImPDmAqjOmdr9AwXcodzr4kwfQfuY6VKbzyhpGU96S75WxIqb2DaPnvNWKklQD4WSuzB+sVILjOYjm/VARSWKTBQQzlZCFmErYeubzVJJR14SlQtVQMjO0xrXvoulXkq3OKnxAXqSsoSmNUbOM/BV35RjDDz9JrBXpnnEM3vsYjj38LLyZihI8QNAgQhITOCmTO46i+6w1MPm86RVIiC09/RJUGcECCe2UU0G6QIyUjEC5hGaCNd4RqHKU6VuDylQlI2N8hfXDWibEdyhCKXREuZUVUX8lyhh2+Jl5Q/6akSgT4izGn3wBFu+JwYOKj8qwtsbJaYmJuYEZ5AYmFOWXPCN1jTodzeuqM0WtSI1rzXrV0LSNKRFuZLYQ2EYVPjEQVuQUMsCya65GvL1HWUwJS+FNUcBsUiZUQv7aLGlndr+I8ug4XUMVAJw4U7FmI8SFETTmUaGK2gas1SeeP8znoizIEso9DaUIy2FWkNU5V0VYs/azWXKncuCHqgQq1CHiY831H8TGr34erRvXKdD6LD3b+HnRn12qGgdqlmxHZe2aRcy6NbQScl8y8dSOfWQE1yK9YYmqXYww3xhNObemUI2IWraF2d1HMTeeh83MbkUiylKiiMdy2wjzXBjxWYdRiSkhfDVVKGSstxM9l16JxZe/E2+848c49bPXK9D2vPUyEsBOVZMINmpCW6HgEOuIQjXF6FYuAV2aHsWyrVfj9C9er5SR5Kms0PTf8QoZtIo7WSJW+mmRJLGSpDK2ipzV2bK6X6fxtWOCicYVqyhGXkXn+WeTcfape5ZDsPGM91C5iy8LI0s445bd9FkrAFHICt1N8DE+gdyeQczs34+uzeei68LNLGfdea50st6VbiyYmHq+nxTFRSSRVsD3ii7xyeQbdt/M5h/MERMT4i6GjlAWeUxh6HCN8+LIz+5H5zlUbtHSOnVp4MCa51JaIQ16i0kwP9CP0uExPP+JL2DggfuYN8jTJClYxnH4aNimdpp0r7nDkyx9h5gE0+RqSVTyZXXTsMz5FaJyMJrrGLNopyWUIImj//1LjPzuUZLCC5gzVqMwPIglV7/rxCaihFaCPCDOxDUl1EoylFP4mUlFCgPDStLKWB47PnUjrSSsNqrJsa/zR02ZwGjYRoVkEZh0ZHzbfmTPXE85SWrnKip6GeFE2I1iKVBCzNK9pmiVhS1x+Axx7myRJesvgHvvR3rNKmQ3n/OKPVGND1MVXTqHiFK6qVFiwlXgTVDhkq+ChhnyJCW9GeaoIGQOdV0M9YhYZWbvUXrIJJ+rKL6lJ9CYj5Fai0iKqyPkx0HcUsJYrBbtREIJ2H72GxTI/2CL1zAbLkZ8WIxYgUvsKebq6Zl3rEZvymx6echo1N+au9XcS3oHsxWMPrGTFH+CLhsmbhMNRWrNB4SZVSwyJ5WDFRb3DAAmaXf2rPP+6BpbkmStkBLAWwkHmdNWKfYqFaZRp2GGdo+mhpv6bBkNhepRzERpdASeW1aKSZ5RidpoUsRAvQ+NJCnJHHl+bcZ80vjkij661vo/rWMQSitWskgnNv7LP+MNN38NadYuCPtYCItIFTjMRgfeqClkhkFZ+FXCQmpFuyKXii7xNI93LT9szdrUMsNZnJkuwZX6zlKdaqRXrESiq/e19kBC3NisLt+Gc/7jW0gtZ51Bl1MCmUaoM//aRv0aapnF0l362KIUnI6EyuhCUOuWrIVfAZcRAj5NJWJ0C5epP19y1awJLWhdt/a1t3KcGF8Yxb5bbsLItoeYmxZRkRWq46IrR9StX/tcw4oKsYH+nlrZpmbcZQ7R1tDPBvMbdIwofLpVKIfcJy5nCa5WRhnDFkVOx+s5kr29GPzpfUxsuxg0zlQUxSZudG/CqNOSIJxYCclGCA7fDRDpiCK6gIVfidVmWXrHRh0fmBd+eSYIIEcWdRhdJJsWp+aQT1vI9nYjnl3wuhSJLuhAJJ1WQWDisadUELCi0bD1WlscMpq6lrV1Ft0riC9tVcFD8odfDVS9bod5pNGgC3+XFnxsXA2rsw25/gHMTcwiRxdbvLgPsY7s61IktWSZinw6l8SbupNGvUlphB1yZY3aIhfZtRmz4XS3oMoA5JP6BywdvBIr24ytMdzsWjHaMcnI0nXRG5FkdCrnS6gy6QzccxeMZDsJW+r1KbJ4pbKAVy6huXoyauVUaAUjRK5WjN9cH05PCiZl84VfsXaSVTKf191C6F61qCXjtjAORtvTSPb0sgYoEi/UmEmnMj6JkpXA6z2cTAbxxV26GdEEZB12DVVV63BrIuwYaWpCGZyuJBWSFSxPLTB5PH1+rhDDKlQbuvajNUzE+UVyRTTdQt+zWIrGWIJOozo8hjmashq8PkXsZAoty1Yqi/gVnq6ru+p1pUKFTM3dENJzu421TiqKKq3hhUp45apSyM1VGMH0xOi+liz0yOxUyijs2w2DlRjI+8tHB3XUIP+fGBxA9+LFr1kRgwV769p1fPkEQ+9KRq+dKE9MsGKc1BmxltEC7W6CEdW0aUtocIvw0tcSt5JGu3R4OA+zIxW1uKoUOUZzFxmxRp/ai+iz+xi9CK5EVJGdqBNBlG4xdvBlRq9eTQteawhm0MgPLsSGj92gVqjKk8ew/TOfxPjjz8BKxhvLFGHjWUBuJh0Cu6pqD7WCTGz4BDqKpE30rIlj05rw6sKFxuCXPP9O8MEjxQqOTuQwNjJLa1mItaRRGB3GLHnO6znaNmxC/nA/cocPKNoS61iEZVdfEy5LBHVKUieCLY5eeKIiXp6RapJuNVJFMCamYGnOUFyslBo0Xronai0dIfXmnZIqtKhgNIaj/F3ULSLx4j60dnXXy8s/OZe0dyGW7cLOL34arevXI9rayWgYhZPtoJtNqsTbyPKUgwzamyCw867MtG5NBUF9bSBXLCkeKOzDroUutaZODax52yUk5sfgsyrL897+PXtQHTmK7vWnomPpCkSTf3pI7j7/Qmz/5HWY3r5LNziYeC3WPlYsovOJJ7VKVbuPENcgXEyvuV3IbKXpPlcqqh0acqGe2S1oq1jzqmZ+b0mGDJNaM2bnjrHuPnYUifZOtDMKda9ah1RnZ30F99WO9jM2MzouZw0vLdJIuCsiUInOz0vbiVNa9DSBtITyWo3VAV/XG/KmPEuBKrmard7rNxKiyCoN7EBnpXlLCiYTmfibuEHSSSkLV4uzGNr5NEYP7EZb31J0rd6AzMIevtf+g4oIg+7e8iYM3H03J5muw9n3ZquqfwU3aGDdMBqdztr+lXBbhyg+R2xYTb5jN7YG6SKnyh870r8Ki6Py0CiO3fcTNWaCBU3E8FVDr7ZPRjbcDLHO30N/TmazdLk+JFMxVoZh6errUrcmnDQp5o4MocrI4o3N6dmXhp1hoHkOFV2R5CXtVwm3Qc0aBip8Z6lY0HtRpJ8GYz5pVFgxgkaHiaCuDE1gfOAhFdNbJIKxplCKNJqqyoqi0CT9tp9/IyyPE2SryYyDKD9LVKxKUqXbuFOM+yVDN/Rq+0ia1mLmtYNqK8rhTiSpLLNbLkDLuZvQ0X8QBoG+//5fIMjP4AQ/kJkuM+vW+sS1wkgiVSTi0Fq2XqoLFfFYMMkyHSFL2mOpHQmy+aU4xXHoLk6rrIkYiE1JNpZOJjO1ivduOLSkZeuk6/YBwR54jaVv6chXpmZQmJnEssveQjwVcPCXv1IWt4//sUVB7K4WpGTREqhvJCrO5MhtGLMTKWU5pUSpDKs1glhbB4W3VCSpTM6gOl2GQzxJt+RQUMFcOoENrXG0FEhESSvMmIVIZ6uaHL9QZn6Y067VNJueV4bdmYDdktJ7pAJNKKfG+pG/cz8GH/gfGLIARF4o9fs8RWSrUmZxN7Z+9za0sooTPiRuI22bsUMHsevWW1B+iFnYdOgqFWTPPxWnXPdxtK5eV8fB9IH92Pn1m1hz7MQh00Xm/C34+K23MiOXsPvLX8bgbXej5bz1OPs7tzIhduHgnXfghX/8OplEsr6U4ZtV9G69HMvf8wEkKUfgaUeWbs4zX/8Sxm/+AbzxCRVF1VpFM9hrvS2ZmZbuRUh2LpxPw7t60EWK8vgHPoCZ5w4i1pvBps99Bu2nbJ73XLyzB4kvLcAPt27F2LFR9MTjSKbb1L1h4mIq4iNL14u2ZRFJysazZCNHqA0DZXRcuBGnf+bz6v4JLDqVgk3r247DnMdJDkOzffJtDfoY2b0dg08/gbZlq7BiyyWk+MuQ2bAGU9v2snTtQnxBj3pu9OnfYXr/Hiq1EZ0bz0ZsUS+sFUvgDB+DFfh1v3X9Kg4xknfLRNZ21h2/RYTX29avU0pUSwUcuP07KLw0oBZrA5bGozt2MlA5updgzGuJnYyp6rt7778HP37fX+OJW77ZaKzKoo5eOdfRhMehO3+EbXzu8H/dXW/SOTwj0gZqeoVck+h3xES9LDjpVp3QXeRdqSVLkDllrepy5oeHMPH0brq2qdteRmNJwj7pYKFVlr75YrztKw6ya9aFTzF8Tk+pBZrmXRGRdCsSLMiQbKlfE7PLrjarCcSSA0QZvQQevGKncnrfXpVwZTde3+XvqN9b8d4PYfuNn8O+b9zO56K6oGpOiMYreNfSc7eoE+FO00P334XJx3fQzM685zd8/Hqs/uCHGGEy9QEslaT0Cm9t7rVyYqnGWogEGIl+nqUTmyxwTj62HTs/91ks3XqN2u8VBLKZoVt14pe/42oc/O6dzB2+qnEMNGHEPHHbSfiSqloakGP7D7+Dpz79BfT6cRXu5rHatk51Nh9aEaOJu2mOZIf36uDu6EDi3PVoIQGV5efiwSG1Rjny8COY3P4sI1WM2HKx4bpPYdEFlzA9RMOlhCAsLJssYqGxRbcZI8//9MfIrliDvjPOwqqL/xwD996P6rY9zGHWPNMNPf8UJl9+Cdm169G9YWOdapjB8auShsJMc85YdekVWL7lQgroKHd68qMfRcAEu+lrX1GdSdmBKjQn0aOrU9lso5bK53uSLiyscNu10tAy66FganAQD9zwD6jM5ZBe2IeLbvoGWs5YofZQyfKXxbpejl133omfXfth7P/Fz8NRLbXgb0nGNe26GhGST5MzFmEYll2oCl+sd2IZCcWtTKxd6rokwdYVpyK9fB1z1KnIrD0NDt1WiNGB738X3kxJVapiWVmR5pCurc2iSaIkmNJ0Hr+9+WYkMu0YfHI7Dv9+J+766Eew8vSNiFP4WGsGBanhh6bw1K3fRjSdwfSel5FikTT67At4+t9vgVssojA0Rp6VwOyhfjx9262qABrfw1KaJW15YprXvsVcEG1sT5eCji40fXSURVyAvTd9TSmv6nTVifQx/uwzmHiU7kb3Clu+GC27MsY247p07+SihN0m/Kgc6EXRIjmMgDvCF9mcsXJxDgniZSnN3xFLIcc6Yormd1mhCX2QpWc7SteolNUpNUQkIUvJpDkUrsrfqy1L8ZjaFSTrJKLsCbvz6BqxaBwdBReWbJmF3kTa2NYRVYFGHEYKqqFKFXtzMg6uUhaJyzZyQ/d/FdUm8LwmAuYwO/vhQBU+m+ddmy+NpBKNWpIzF7EdRSxrOygMMl6LruUw2tQXOTy1akNFk/XtU/V70H3g6YyNNk5GtOIp/DYvlKp9LoJLWuIl2fADfJ/X71PQ8Jo2Vzbv620OAFI9jtIqCQ7tnfC/JxhNT4dShds4UKvB66s1ftPnRqOh/l13hDDqWGhxqUgTsIV1Fzg5Y7TEpKsK+B/w+sdqUWuqv1CxUN8K/MqHLMnhj/g/J/4/juDky9VSg0kh/zQj322897Pao/8nwAC+AZicLeuzngAAAABJRU5ErkJggg==); + background-repeat: no-repeat; + background-position: top left; + height: 64px; + } + #header h1, #header h2 {margin: 0} + #header h2 { + color: #888; + font-weight: normal; + font-size: 16px; + } + + + #about h3 { + margin: 0; + margin-bottom: 10px; + font-size: 14px; + } + + #about-content { + background-color: #ffd; + border: 1px solid #fc0; + margin-left: -55px; + margin-right: -10px; + } + #about-content table { + margin-top: 10px; + margin-bottom: 10px; + font-size: 11px; + border-collapse: collapse; + } + #about-content td { + padding: 10px; + padding-top: 3px; + padding-bottom: 3px; + } + #about-content td.name {color: #555} + #about-content td.value {color: #000} + + #about-content ul { + padding: 0; + list-style-type: none; + } + + #about-content.failure { + background-color: #fcc; + border: 1px solid #f00; + } + #about-content.failure p { + margin: 0; + padding: 10px; + } + + + #getting-started { + border-top: 1px solid #ccc; + margin-top: 25px; + padding-top: 15px; + } + #getting-started h1 { + margin: 0; + font-size: 20px; + } + #getting-started h2 { + margin: 0; + font-size: 14px; + font-weight: normal; + color: #333; + margin-bottom: 25px; + } + #getting-started ol { + margin-left: 0; + padding-left: 0; + } + #getting-started li { + font-size: 18px; + color: #888; + margin-bottom: 25px; + } + #getting-started li h2 { + margin: 0; + font-weight: normal; + font-size: 18px; + color: #333; + } + #getting-started li p { + color: #555; + font-size: 13px; + } + + + #sidebar ul { + margin-left: 0; + padding-left: 0; + } + #sidebar ul h3 { + margin-top: 25px; + font-size: 16px; + padding-bottom: 10px; + border-bottom: 1px solid #ccc; + } + #sidebar li { + list-style-type: none; + } + #sidebar ul.links li { + margin-bottom: 5px; + } + + .filename { + font-style: italic; + } + </style> + <script> + function about() { + var info = document.getElementById('about-content'), + xhr; + + if (info.innerHTML === '') { + xhr = new XMLHttpRequest(); + xhr.open("GET", "/rails/info/properties", false); + xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest"); + xhr.send(""); + info.innerHTML = xhr.responseText; + } + + info.style.display = info.style.display === 'none' ? 'block' : 'none'; + } + </script> + </head> + <body> + <div id="page"> + <div id="sidebar"> + <ul id="sidebar-items"> + <li> + <h3>Browse the documentation</h3> + <ul class="links"> + <li><a href="http://guides.rubyonrails.org/">Rails Guides</a></li> + <li><a href="http://api.rubyonrails.org/">Rails API</a></li> + <li><a href="http://www.ruby-doc.org/core/">Ruby core</a></li> + <li><a href="http://www.ruby-doc.org/stdlib/">Ruby standard library</a></li> + </ul> + </li> + </ul> + </div> + + <div id="content"> + <div id="header"> + <h1>Welcome aboard</h1> + <h2>You’re riding Ruby on Rails!</h2> + </div> + + <div id="about"> + <h3><a href="/rails/info/properties" onclick="about(); return false">About your application’s environment</a></h3> + <div id="about-content" style="display: none"></div> + </div> + + <div id="getting-started"> + <h1>Getting started</h1> + <h2>Here’s how to get rolling:</h2> + + <ol> + <li> + <h2>Use <code>rails generate</code> to create your models and controllers</h2> + <p>To see all available options, run it without parameters.</p> + </li> + + <li> + <h2>Set up a root route to replace this page</h2> + <p>You're seeing this page because you're running in development mode and you haven't set a root route yet.</p> + <p>Routes are set up in <span class="filename">config/routes.rb</span>.</p> + </li> + + <li> + <h2>Configure your database</h2> + <p>If you're not using SQLite (the default), edit <span class="filename">config/database.yml</span> with your username and password.</p> + </li> + </ol> + </div> + </div> + + <div id="footer"> </div> + </div> + </body> +</html> diff --git a/railties/lib/rails/test_help.rb b/railties/lib/rails/test_help.rb new file mode 100644 index 0000000000..c837fadb40 --- /dev/null +++ b/railties/lib/rails/test_help.rb @@ -0,0 +1,43 @@ +# Make double-sure the RAILS_ENV is not set to production, +# so fixtures aren't loaded into that environment +abort("Abort testing: Your Rails environment is running in production mode!") if Rails.env.production? + +require 'active_support/testing/autorun' +require 'active_support/test_case' +require 'action_controller' +require 'action_controller/test_case' +require 'action_dispatch/testing/integration' +require 'rails/generators/test_case' + +# Config Rails backtrace in tests. +require 'rails/backtrace_cleaner' +if ENV["BACKTRACE"].nil? + Minitest.backtrace_filter = Rails.backtrace_cleaner +end + +if defined?(ActiveRecord::Base) + ActiveRecord::Migration.maintain_test_schema! + + class ActiveSupport::TestCase + include ActiveRecord::TestFixtures + self.fixture_path = "#{Rails.root}/test/fixtures/" + end + + ActionDispatch::IntegrationTest.fixture_path = ActiveSupport::TestCase.fixture_path + + def create_fixtures(*fixture_set_names, &block) + FixtureSet.create_fixtures(ActiveSupport::TestCase.fixture_path, fixture_set_names, {}, &block) + end +end + +class ActionController::TestCase + setup do + @routes = Rails.application.routes + end +end + +class ActionDispatch::IntegrationTest + setup do + @routes = Rails.application.routes + end +end diff --git a/railties/lib/rails/test_unit/railtie.rb b/railties/lib/rails/test_unit/railtie.rb new file mode 100644 index 0000000000..75180ff978 --- /dev/null +++ b/railties/lib/rails/test_unit/railtie.rb @@ -0,0 +1,18 @@ +if defined?(Rake.application) && Rake.application.top_level_tasks.grep(/^(default$|test(:|$))/).any? + ENV['RAILS_ENV'] ||= 'test' +end + +module Rails + class TestUnitRailtie < Rails::Railtie + config.app_generators do |c| + c.test_framework :test_unit, fixture: true, + fixture_replacement: nil + + c.integration_tool :test_unit + end + + rake_tasks do + load "rails/test_unit/testing.rake" + end + end +end diff --git a/railties/lib/rails/test_unit/sub_test_task.rb b/railties/lib/rails/test_unit/sub_test_task.rb new file mode 100644 index 0000000000..6fa96d2ced --- /dev/null +++ b/railties/lib/rails/test_unit/sub_test_task.rb @@ -0,0 +1,126 @@ +require 'rake/testtask' + +module Rails + class TestTask < Rake::TestTask # :nodoc: all + # A utility class which is used primarily in "rails/test_unit/testing.rake" + # to help define rake tasks corresponding to <tt>rake test</tt>. + # + # This class takes a TestInfo class and defines the appropriate rake task + # based on the information, then invokes it. + class TestCreator # :nodoc: + def initialize(info) + @info = info + end + + def invoke_rake_task + if @info.files.any? + create_and_run_single_test + reset_application_tasks + else + Rake::Task[ENV['TEST'] ? 'test:single' : 'test:run'].invoke + end + end + + private + + def create_and_run_single_test + Rails::TestTask.new('test:single') { |t| + t.test_files = @info.files + } + ENV['TESTOPTS'] ||= @info.opts + Rake::Task['test:single'].invoke + end + + def reset_application_tasks + Rake.application.top_level_tasks.replace @info.tasks + end + end + + # This is a utility class used by the <tt>TestTask::TestCreator</tt> class. + # This class takes a set of test tasks and checks to see if they correspond + # to test files (or can be transformed into test files). Calling <tt>files</tt> + # provides the set of test files and is used when initializing tests after + # a call to <tt>rake test</tt>. + class TestInfo # :nodoc: + def initialize(tasks) + @tasks = tasks + @files = nil + end + + def files + @files ||= @tasks.map { |task| + [task, translate(task)].find { |file| test_file?(file) } + }.compact + end + + def translate(file) + if file =~ /^app\/(.*)$/ + "test/#{$1.sub(/\.rb$/, '')}_test.rb" + else + "test/#{file}_test.rb" + end + end + + def tasks + @tasks - test_file_tasks - opt_names + end + + def opts + opts = opt_names + if opts.any? + "-n #{opts.join ' '}" + end + end + + private + + def test_file_tasks + @tasks.find_all { |task| + [task, translate(task)].any? { |file| test_file?(file) } + } + end + + def test_file?(file) + file =~ /^test/ && File.file?(file) && !File.directory?(file) + end + + def opt_names + (@tasks - test_file_tasks).reject { |t| task_defined? t } + end + + def task_defined?(task) + Rake::Task.task_defined? task + end + end + + def self.test_creator(tasks) + info = TestInfo.new(tasks) + TestCreator.new(info) + end + + def initialize(name = :test) + super + @libs << "test" # lib *and* test seem like a better default + end + + def define + task @name do + if ENV['TESTOPTS'] + ARGV.replace Shellwords.split ENV['TESTOPTS'] + end + libs = @libs - $LOAD_PATH + $LOAD_PATH.unshift(*libs) + file_list.each { |fl| + FileList[fl].to_a.each { |f| require File.expand_path f } + } + end + end + end + + # Silence the default description to cut down on `rake -T` noise. + class SubTestTask < Rake::TestTask # :nodoc: + def desc(string) + # Ignore the description. + end + end +end diff --git a/railties/lib/rails/test_unit/testing.rake b/railties/lib/rails/test_unit/testing.rake new file mode 100644 index 0000000000..285e2ce846 --- /dev/null +++ b/railties/lib/rails/test_unit/testing.rake @@ -0,0 +1,48 @@ +require 'rake/testtask' +require 'rails/test_unit/sub_test_task' + +task default: :test + +desc 'Runs test:units, test:functionals, test:generators, test:integration together' +task :test do + Rails::TestTask.test_creator(Rake.application.top_level_tasks).invoke_rake_task +end + +namespace :test do + task :prepare do + # Placeholder task for other Railtie and plugins to enhance. See Active Record for an example. + end + + task :run => ['test:units', 'test:functionals', 'test:generators', 'test:integration'] + + # Inspired by: http://ngauthier.com/2012/02/quick-tests-with-bash.html + desc "Run tests quickly by merging all types and not resetting db" + Rails::TestTask.new(:all) do |t| + t.pattern = "test/**/*_test.rb" + end + + namespace :all do + desc "Run tests quickly, but also reset db" + task :db => %w[db:test:prepare test:all] + end + + Rails::TestTask.new(single: "test:prepare") + + ["models", "helpers", "controllers", "mailers", "integration"].each do |name| + Rails::TestTask.new(name => "test:prepare") do |t| + t.pattern = "test/#{name}/**/*_test.rb" + end + end + + Rails::TestTask.new(generators: "test:prepare") do |t| + t.pattern = "test/lib/generators/**/*_test.rb" + end + + Rails::TestTask.new(units: "test:prepare") do |t| + t.pattern = 'test/{models,helpers,unit}/**/*_test.rb' + end + + Rails::TestTask.new(functionals: "test:prepare") do |t| + t.pattern = 'test/{controllers,mailers,functional}/**/*_test.rb' + end +end diff --git a/railties/lib/rails/version.rb b/railties/lib/rails/version.rb new file mode 100644 index 0000000000..df351c4238 --- /dev/null +++ b/railties/lib/rails/version.rb @@ -0,0 +1,8 @@ +require_relative 'gem_version' + +module Rails + # Returns the version of the currently loaded Rails as a string. + def self.version + VERSION::STRING + end +end diff --git a/railties/lib/rails/welcome_controller.rb b/railties/lib/rails/welcome_controller.rb new file mode 100644 index 0000000000..de9cd18b01 --- /dev/null +++ b/railties/lib/rails/welcome_controller.rb @@ -0,0 +1,8 @@ +require 'rails/application_controller' + +class Rails::WelcomeController < Rails::ApplicationController # :nodoc: + layout false + + def index + end +end |