diff options
Diffstat (limited to 'railties')
27 files changed, 227 insertions, 66 deletions
diff --git a/railties/CHANGELOG.md b/railties/CHANGELOG.md index ccccc178c5..a6abe5ee97 100644 --- a/railties/CHANGELOG.md +++ b/railties/CHANGELOG.md @@ -1,5 +1,9 @@ ## Rails 4.0.0 (unreleased) ## +* Add runner to Rails::Railtie as a hook called just after runner starts. *José Valim & kennyj* + +* Add `/rails/info/routes` path, displays same information as `rake routes` *Richard Schneeman & Andrew White* + * Improved `rake routes` output for redirects *Łukasz Strzałkowski & Andrew White* * Load all environments available in `config.paths["config/environments"]`. *Piotr Sarnacki* diff --git a/railties/lib/rails/application.rb b/railties/lib/rails/application.rb index d44465e547..32797ee657 100644 --- a/railties/lib/rails/application.rb +++ b/railties/lib/rails/application.rb @@ -158,6 +158,14 @@ module Rails self end + # Load the application runner and invoke the registered hooks. + # Check <tt>Rails::Railtie.runner</tt> for more info. + def load_runner(app=self) + initialize_runner + super + self + end + # Stores some of the Rails initial environment parameters which # will be used by middlewares and engines to configure themselves. def env_config @@ -185,7 +193,7 @@ module Rails end all = (railties.all - order) - all.push(self) unless all.include?(self) + all.push(self) unless (all + order).include?(self) order.push(:all) unless order.include?(:all) index = order.index(:all) @@ -304,6 +312,9 @@ module Rails require "rails/console/helpers" end + def initialize_runner #:nodoc: + end + def build_original_fullpath(env) path_info = env["PATH_INFO"] query_string = env["QUERY_STRING"] diff --git a/railties/lib/rails/application/finisher.rb b/railties/lib/rails/application/finisher.rb index 84f2601f28..60aa40b92f 100644 --- a/railties/lib/rails/application/finisher.rb +++ b/railties/lib/rails/application/finisher.rb @@ -23,6 +23,8 @@ module Rails 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" end end end diff --git a/railties/lib/rails/application/route_inspector.rb b/railties/lib/rails/application/route_inspector.rb index b23fb3e920..942c4f4789 100644 --- a/railties/lib/rails/application/route_inspector.rb +++ b/railties/lib/rails/application/route_inspector.rb @@ -51,7 +51,7 @@ module Rails end def internal? - path =~ %r{/rails/info/properties|^#{Rails.application.config.assets.prefix}} + path =~ %r{/rails/info.*|^#{Rails.application.config.assets.prefix}} end def engine? diff --git a/railties/lib/rails/commands/runner.rb b/railties/lib/rails/commands/runner.rb index 77f1b15fb4..a672258aa6 100644 --- a/railties/lib/rails/commands/runner.rb +++ b/railties/lib/rails/commands/runner.rb @@ -41,6 +41,7 @@ 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." diff --git a/railties/lib/rails/engine.rb b/railties/lib/rails/engine.rb index 4c7199a2e2..806b553b81 100644 --- a/railties/lib/rails/engine.rb +++ b/railties/lib/rails/engine.rb @@ -437,6 +437,11 @@ module Rails super end + def load_runner(app=self) + railties.all { |r| r.load_runner(app) } + super + end + def eager_load! railties.all(&:eager_load!) diff --git a/railties/lib/rails/generators/rails/app/templates/Gemfile b/railties/lib/rails/generators/rails/app/templates/Gemfile index bf47e66cc4..55a6b3f4f2 100644 --- a/railties/lib/rails/generators/rails/app/templates/Gemfile +++ b/railties/lib/rails/generators/rails/app/templates/Gemfile @@ -19,7 +19,7 @@ source 'https://rubygems.org' # gem 'unicorn' # Deploy with Capistrano -# gem 'capistrano', :group => :development +# gem 'capistrano', group: :development # To use debugger # gem 'debugger' diff --git a/railties/lib/rails/generators/rails/plugin_new/plugin_new_generator.rb b/railties/lib/rails/generators/rails/plugin_new/plugin_new_generator.rb index 7088367462..ab0e440bc4 100644 --- a/railties/lib/rails/generators/rails/plugin_new/plugin_new_generator.rb +++ b/railties/lib/rails/generators/rails/plugin_new/plugin_new_generator.rb @@ -232,6 +232,18 @@ task :default => :test 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 @@ -268,18 +280,6 @@ task :default => :test @original_name ||= File.basename(destination_root) end - 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 - def camelized @camelized ||= name.gsub(/\W/, '_').squeeze('_').camelize end diff --git a/railties/lib/rails/info_controller.rb b/railties/lib/rails/info_controller.rb index 6b4bdb2921..5081074395 100644 --- a/railties/lib/rails/info_controller.rb +++ b/railties/lib/rails/info_controller.rb @@ -1,15 +1,33 @@ +require 'rails/application/route_inspector' + class Rails::InfoController < ActionController::Base + self.view_paths = File.join(File.dirname(__FILE__), 'templates') + layout 'application' + + before_filter :require_local! + + def index + redirect_to '/rails/info/routes' + end + def properties - if consider_all_requests_local? || request.local? - render :inline => Rails::Info.to_html - else - render :text => '<p>For security purposes, this information is only available to local requests.</p>', :status => :forbidden - end + @info = Rails::Info.to_html + end + + def routes + inspector = Rails::Application::RouteInspector.new + @info = inspector.format(_routes.routes).join("\n") end protected - def consider_all_requests_local? - Rails.application.config.consider_all_requests_local + 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/railtie.rb b/railties/lib/rails/railtie.rb index 2102f8a03c..c3cc65ab31 100644 --- a/railties/lib/rails/railtie.rb +++ b/railties/lib/rails/railtie.rb @@ -145,6 +145,12 @@ module Rails @load_console end + def runner(&blk) + @load_runner ||= [] + @load_runner << blk if blk + @load_runner + end + def generators(&blk) @generators ||= [] @generators << blk if blk @@ -179,6 +185,10 @@ module Rails self.class.console.each { |block| block.call(app) } end + def load_runner(app=self) + self.class.runner.each { |block| block.call(app) } + end + def load_tasks(app=self) extend Rake::DSL if defined? Rake::DSL self.class.rake_tasks.each { |block| self.instance_exec(app, &block) } diff --git a/railties/lib/rails/tasks/engine.rake b/railties/lib/rails/tasks/engine.rake index eea8abe7d2..70370be3f5 100644 --- a/railties/lib/rails/tasks/engine.rake +++ b/railties/lib/rails/tasks/engine.rake @@ -60,7 +60,7 @@ namespace :db do end def find_engine_path(path) - return if path == "/" + return File.expand_path(Dir.pwd) if path == "/" if Rails::Engine.find(path) path diff --git a/railties/lib/rails/tasks/routes.rake b/railties/lib/rails/tasks/routes.rake index 7dc54144da..5778b22f18 100644 --- a/railties/lib/rails/tasks/routes.rake +++ b/railties/lib/rails/tasks/routes.rake @@ -1,8 +1,6 @@ desc 'Print out all defined routes in match order, with names. Target specific controller with CONTROLLER=x.' task :routes => :environment do - Rails.application.reload_routes! all_routes = Rails.application.routes.routes - require 'rails/application/route_inspector' inspector = Rails::Application::RouteInspector.new puts inspector.format(all_routes, ENV['CONTROLLER']).join "\n" 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..53276d3e7c --- /dev/null +++ b/railties/lib/rails/templates/layouts/application.html.erb @@ -0,0 +1,32 @@ +<!DOCTYPE html> +<html lang="en"> +<head> + <meta charset="utf-8" /> + <title>Routes</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; } + </style> +</head> +<body> +<h2>Your App: <%= link_to 'properties', '/rails/info/properties' %> | <%= link_to 'routes', '/rails/info/routes' %></h2> +<%= 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..890f6f5b03 --- /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> + +<p><pre><%= @info %></pre></p>
\ No newline at end of file diff --git a/railties/test/application/configuration_test.rb b/railties/test/application/configuration_test.rb index 252dd0e31a..d7689863e6 100644 --- a/railties/test/application/configuration_test.rb +++ b/railties/test/application/configuration_test.rb @@ -288,16 +288,16 @@ module ApplicationTests params = {:authenticity_token => token} get "/posts/1" - assert_match /patch/, last_response.body + assert_match(/patch/, last_response.body) patch "/posts/1", params - assert_match /update/, last_response.body + assert_match(/update/, last_response.body) patch "/posts/1", params assert_equal 200, last_response.status put "/posts/1", params - assert_match /update/, last_response.body + assert_match(/update/, last_response.body) put "/posts/1", params assert_equal 200, last_response.status diff --git a/railties/test/application/rake/notes_test.rb b/railties/test/application/rake/notes_test.rb index 05d73dfc5c..3f4db77897 100644 --- a/railties/test/application/rake/notes_test.rb +++ b/railties/test/application/rake/notes_test.rb @@ -33,14 +33,14 @@ module ApplicationTests output = `bundle exec rake notes` lines = output.scan(/\[([0-9\s]+)\](\s)/) - assert_match /note in erb/, output - assert_match /note in haml/, output - assert_match /note in slim/, output - assert_match /note in ruby/, output - assert_match /note in coffee/, output - assert_match /note in js/, output - assert_match /note in css/, output - assert_match /note in scss/, output + assert_match(/note in erb/, output) + assert_match(/note in haml/, output) + assert_match(/note in slim/, output) + assert_match(/note in ruby/, output) + assert_match(/note in coffee/, output) + assert_match(/note in js/, output) + assert_match(/note in css/, output) + assert_match(/note in scss/, output) assert_equal 8, lines.size @@ -72,12 +72,12 @@ module ApplicationTests output = `bundle exec rake notes` lines = output.scan(/\[([0-9\s]+)\]/).flatten - assert_match /note in app directory/, output - assert_match /note in config directory/, output - assert_match /note in lib directory/, output - assert_match /note in script directory/, output - assert_match /note in test directory/, output - assert_no_match /note in some_other directory/, output + assert_match(/note in app directory/, output) + assert_match(/note in config directory/, output) + assert_match(/note in lib directory/, output) + assert_match(/note in script directory/, output) + assert_match(/note in test directory/, output) + assert_no_match(/note in some_other directory/, output) assert_equal 5, lines.size @@ -108,13 +108,13 @@ module ApplicationTests output = `SOURCE_ANNOTATION_DIRECTORIES='some_other_dir' bundle exec rake notes` lines = output.scan(/\[([0-9\s]+)\]/).flatten - assert_match /note in app directory/, output - assert_match /note in config directory/, output - assert_match /note in lib directory/, output - assert_match /note in script directory/, output - assert_match /note in test directory/, output + assert_match(/note in app directory/, output) + assert_match(/note in config directory/, output) + assert_match(/note in lib directory/, output) + assert_match(/note in script directory/, output) + assert_match(/note in test directory/, output) - assert_match /note in some_other directory/, output + assert_match(/note in some_other directory/, output) assert_equal 6, lines.size diff --git a/railties/test/application/rake_test.rb b/railties/test/application/rake_test.rb index 27d521485c..8cf867da3c 100644 --- a/railties/test/application/rake_test.rb +++ b/railties/test/application/rake_test.rb @@ -167,5 +167,28 @@ module ApplicationTests end assert !File.exists?(File.join(app_path, 'db', 'schema_cache.dump')) end + + def test_load_activerecord_base_when_we_use_observers + Dir.chdir(app_path) do + `bundle exec rails g model user; + bundle exec rake db:migrate; + bundle exec rails g observer user;` + + add_to_config "config.active_record.observers = :user_observer" + + assert_equal "0", `bundle exec rails r "puts User.count"`.strip + + app_file "lib/tasks/count_user.rake", <<-RUBY + namespace :user do + task :count => :environment do + puts User.count + end + end + RUBY + + assert_equal "0", `bundle exec rake user:count`.strip + end + end + end end diff --git a/railties/test/application/routing_test.rb b/railties/test/application/routing_test.rb index 977a5fc7e8..d1373ba202 100644 --- a/railties/test/application/routing_test.rb +++ b/railties/test/application/routing_test.rb @@ -15,12 +15,24 @@ module ApplicationTests teardown_app end + test "rails/info/routes in development" do + app("development") + get "/rails/info/routes" + assert_equal 200, last_response.status + end + test "rails/info/properties in development" do app("development") get "/rails/info/properties" assert_equal 200, last_response.status end + test "rails/info/routes in production" do + app("production") + get "/rails/info/routes" + assert_equal 404, last_response.status + end + test "rails/info/properties in production" do app("production") get "/rails/info/properties" diff --git a/railties/test/application/runner_test.rb b/railties/test/application/runner_test.rb index e1d283a7fd..81ed5873a5 100644 --- a/railties/test/application/runner_test.rb +++ b/railties/test/application/runner_test.rb @@ -57,5 +57,15 @@ module ApplicationTests assert_match "script/program_name.rb", Dir.chdir(app_path) { `bundle exec rails runner "script/program_name.rb"` } end + + def test_with_hook + add_to_config <<-RUBY + runner do |app| + app.config.ran = true + end + RUBY + + assert_match "true", Dir.chdir(app_path) { `bundle exec rails runner "puts Rails.application.config.ran"` } + end end end diff --git a/railties/test/commands/console_test.rb b/railties/test/commands/console_test.rb index 9aa1d68675..78648a16b3 100644 --- a/railties/test/commands/console_test.rb +++ b/railties/test/commands/console_test.rb @@ -35,7 +35,7 @@ class Rails::ConsoleTest < ActiveSupport::TestCase start - assert_match /Loading \w+ environment \(Rails/, output + assert_match(/Loading \w+ environment \(Rails/, output) end def test_start_with_debugger @@ -52,7 +52,7 @@ class Rails::ConsoleTest < ActiveSupport::TestCase start ["--sandbox"] - assert_match /Loading \w+ environment in sandbox \(Rails/, output + assert_match(/Loading \w+ environment in sandbox \(Rails/, output) end def test_console_with_environment @@ -61,7 +61,7 @@ class Rails::ConsoleTest < ActiveSupport::TestCase start ["-e production"] - assert_match /production/, output + assert_match(/production/, output) end def test_console_with_rails_environment @@ -70,7 +70,7 @@ class Rails::ConsoleTest < ActiveSupport::TestCase start ["RAILS_ENV=production"] - assert_match /production/, output + assert_match(/production/, output) end diff --git a/railties/test/commands/dbconsole_test.rb b/railties/test/commands/dbconsole_test.rb index 6d0f5ca073..562b83713b 100644 --- a/railties/test/commands/dbconsole_test.rb +++ b/railties/test/commands/dbconsole_test.rb @@ -12,7 +12,7 @@ class Rails::DBConsoleTest < ActiveSupport::TestCase app_config({}) capture_abort { Rails::DBConsole.config } assert aborted - assert_match /No database is configured for the environment '\w+'/, output + assert_match(/No database is configured for the environment '\w+'/, output) app_config(test: "with_init") assert_equal Rails::DBConsole.config, "with_init" @@ -129,7 +129,7 @@ class Rails::DBConsoleTest < ActiveSupport::TestCase def test_unknown_command_line_client start(adapter: 'unknown', database: 'db') assert aborted - assert_match /Unknown command-line client for db/, output + assert_match(/Unknown command-line client for db/, output) end def test_print_help_short @@ -138,7 +138,7 @@ class Rails::DBConsoleTest < ActiveSupport::TestCase end assert aborted assert_equal '', output - assert_match /Usage:.*dbconsole/, stdout + assert_match(/Usage:.*dbconsole/, stdout) end def test_print_help_long @@ -147,7 +147,7 @@ class Rails::DBConsoleTest < ActiveSupport::TestCase end assert aborted assert_equal '', output - assert_match /Usage:.*dbconsole/, stdout + assert_match(/Usage:.*dbconsole/, stdout) end private diff --git a/railties/test/generators_test.rb b/railties/test/generators_test.rb index 60e7e57a91..417d019178 100644 --- a/railties/test/generators_test.rb +++ b/railties/test/generators_test.rb @@ -186,7 +186,7 @@ class GeneratorsTest < Rails::Generators::TestCase mkdir_p(File.dirname(template)) File.open(template, 'w'){ |f| f.write "empty" } - output = capture(:stdout) do + capture(:stdout) do Rails::Generators.invoke :model, ["user"], :destination_root => destination_root end @@ -205,7 +205,7 @@ class GeneratorsTest < Rails::Generators::TestCase def test_usage_with_embedded_ruby require File.expand_path("fixtures/lib/generators/usage_template/usage_template_generator", File.dirname(__FILE__)) output = capture(:stdout) { Rails::Generators.invoke :usage_template, ['--help'] } - assert_match /:: 2 ::/, output + assert_match(/:: 2 ::/, output) end def test_hide_namespace diff --git a/railties/test/paths_test.rb b/railties/test/paths_test.rb index aa04cad033..5d6b6f9f72 100644 --- a/railties/test/paths_test.rb +++ b/railties/test/paths_test.rb @@ -29,7 +29,7 @@ class PathsTest < ActiveSupport::TestCase test "creating a root level path" do @root.add "app" assert_equal ["/foo/bar/app"], @root["app"].to_a - assert_equal [Pathname.new("/foo/bar/app")], @root["app"].paths + assert_equal ["/foo/bar/app"], @root["app"].paths end test "creating a root level path with options" do @@ -192,7 +192,7 @@ class PathsTest < ActiveSupport::TestCase @root["app"] = "/app" @root["app"].glob = "*.rb" assert_equal "*.rb", @root["app"].glob - assert_equal [Pathname.new("/app")], @root["app"].paths + assert_equal ["/foo/bar/app"], @root["app"].paths end test "it should be possible to override a path's default glob without assignment" do diff --git a/railties/test/rails_info_controller_test.rb b/railties/test/rails_info_controller_test.rb index f7a30a16d2..cfb32b7d35 100644 --- a/railties/test/rails_info_controller_test.rb +++ b/railties/test/rails_info_controller_test.rb @@ -12,29 +12,28 @@ class InfoControllerTest < ActionController::TestCase def setup Rails.application.routes.draw do get '/rails/info/properties' => "rails/info#properties" + get '/rails/info/routes' => "rails/info#routes" end - @request.stubs(:local? => true) - @controller.stubs(:consider_all_requests_local? => false) + @controller.stubs(:local_request? => true) @routes = Rails.application.routes Rails::InfoController.send(:include, @routes.url_helpers) end test "info controller does not allow remote requests" do - @request.stubs(:local? => false) + @controller.stubs(:local_request? => false) get :properties assert_response :forbidden end test "info controller renders an error message when request was forbidden" do - @request.stubs(:local? => false) + @controller.stubs(:local_request? => false) get :properties assert_select 'p' end test "info controller allows requests when all requests are considered local" do - @request.stubs(:local? => false) - @controller.stubs(:consider_all_requests_local? => true) + @controller.stubs(:local_request? => true) get :properties assert_response :success end @@ -48,4 +47,10 @@ class InfoControllerTest < ActionController::TestCase get :properties assert_select 'table' end + + test "info controller renders with routes" do + get :routes + assert_select 'pre' + end + end diff --git a/railties/test/railties/engine_test.rb b/railties/test/railties/engine_test.rb index 7a047ef93a..4437e2c8af 100644 --- a/railties/test/railties/engine_test.rb +++ b/railties/test/railties/engine_test.rb @@ -1098,6 +1098,10 @@ YAML get("/assets/bar.js") assert_equal "// App's bar js\n;", last_response.body.strip + + # ensure that railties are not added twice + railties = Rails.application.ordered_railties.map(&:class) + assert_equal railties, railties.uniq end test "railties_order adds :all with lowest priority if not given" do diff --git a/railties/test/railties/railtie_test.rb b/railties/test/railties/railtie_test.rb index cd495320b5..c80b0f63af 100644 --- a/railties/test/railties/railtie_test.rb +++ b/railties/test/railties/railtie_test.rb @@ -163,6 +163,22 @@ module RailtiesTest assert $ran_block end + test "runner block is executed when MyApp.load_runner is called" do + $ran_block = false + + class MyTie < Rails::Railtie + runner do + $ran_block = true + end + end + + require "#{app_path}/config/environment" + + assert !$ran_block + AppTemplate::Application.load_runner + assert $ran_block + end + test "railtie can add initializers" do $ran_block = false |