diff options
Diffstat (limited to 'railties')
-rw-r--r-- | railties/CHANGELOG.md | 7 | ||||
-rw-r--r-- | railties/lib/rails/application.rb | 22 | ||||
-rw-r--r-- | railties/lib/rails/application/configuration.rb | 5 | ||||
-rw-r--r-- | railties/lib/rails/application/route_inspector.rb | 37 | ||||
-rw-r--r-- | railties/lib/rails/code_statistics.rb | 17 | ||||
-rw-r--r-- | railties/lib/rails/engine.rb | 24 | ||||
-rw-r--r-- | railties/lib/rails/generators/actions.rb | 2 | ||||
-rw-r--r-- | railties/lib/rails/tasks/routes.rake | 2 | ||||
-rw-r--r-- | railties/test/application/middleware/cookies_test.rb | 47 | ||||
-rw-r--r-- | railties/test/application/route_inspect_test.rb | 30 | ||||
-rw-r--r-- | railties/test/generators/actions_test.rb | 2 | ||||
-rw-r--r-- | railties/test/railties/engine_test.rb | 126 |
12 files changed, 308 insertions, 13 deletions
diff --git a/railties/CHANGELOG.md b/railties/CHANGELOG.md index b055b051be..25d45d886c 100644 --- a/railties/CHANGELOG.md +++ b/railties/CHANGELOG.md @@ -1,5 +1,12 @@ ## Rails 3.2.0 (unreleased) ## +* Add displaying of mounted engine's routes with `rake routes ENGINES=true` *Piotr Sarnacki* + +* Allow to change the loading order of railties with `config.railties_order=` *Piotr Sarnacki* + + Example: + config.railties_order = [Blog::Engine, :main_app, :all] + * Add a serializer generator and add a hook for it in the scaffold generators *José Valim* * Scaffold returns 204 No Content for API requests without content. This makes scaffold work with jQuery out of the box. *José Valim* diff --git a/railties/lib/rails/application.rb b/railties/lib/rails/application.rb index 0b72a7ed84..847445d29f 100644 --- a/railties/lib/rails/application.rb +++ b/railties/lib/rails/application.rb @@ -127,6 +127,28 @@ module Rails }) end + def ordered_railties + @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.all - order) + all.push(self) unless all.include?(self) + order.push(:all) unless order.include?(:all) + + index = order.index(:all) + order[index] = all + order.reverse.flatten + end + end + def initializers Bootstrap.initializers_for(self) + super + diff --git a/railties/lib/rails/application/configuration.rb b/railties/lib/rails/application/configuration.rb index 8f5b28faf8..e95b0f5495 100644 --- a/railties/lib/rails/application/configuration.rb +++ b/railties/lib/rails/application/configuration.rb @@ -10,8 +10,8 @@ module Rails :dependency_loading, :filter_parameters, :force_ssl, :helpers_paths, :logger, :log_tags, :preload_frameworks, :reload_plugins, :secret_token, :serve_static_assets, - :ssl_options, :static_cache_control, :session_options, - :time_zone, :whiny_nils + :ssl_options, :static_cache_control, :session_options, + :time_zone, :whiny_nils, :railties_order attr_writer :log_level attr_reader :encoding @@ -35,6 +35,7 @@ module Rails @middleware = app_middleware @generators = app_generators @cache_store = [ :file_store, "#{root}/tmp/cache/" ] + @railties_order = [:all] @assets = ActiveSupport::OrderedOptions.new @assets.enabled = false diff --git a/railties/lib/rails/application/route_inspector.rb b/railties/lib/rails/application/route_inspector.rb index 8252f21aa7..8da7de2584 100644 --- a/railties/lib/rails/application/route_inspector.rb +++ b/railties/lib/rails/application/route_inspector.rb @@ -4,12 +4,23 @@ module Rails # This class is just used for displaying route information when someone # executes `rake routes`. People should not use this class. class RouteInspector # :nodoc: + def initialize + @engines = ActiveSupport::OrderedHash.new + end + def format all_routes, filter = nil if filter all_routes = all_routes.select{ |route| route.defaults[:controller] == filter } end - routes = all_routes.collect do |route| + routes = collect_routes(all_routes) + + formatted_routes(routes) + + formatted_routes_for_engines + end + + def collect_routes(routes) + routes = routes.collect do |route| route_reqs = route.requirements rack_app = route.app unless route.app.class.name.to_s =~ /^ActionDispatch::Routing/ @@ -25,12 +36,32 @@ module Rails verb = route.verb.source.gsub(/[$^]/, '') - {:name => route.name.to_s, :verb => verb, :path => route.path.spec.to_s, :reqs => reqs} + collect_engine_routes(reqs, rack_app) + + {:name => route.name.to_s, :verb => verb, :path => route.path.spec.to_s, :reqs => reqs } end # Skip the route if it's internal info route - routes.reject! { |r| r[:path] =~ %r{/rails/info/properties|^/assets} } + routes.reject { |r| r[:path] =~ %r{/rails/info/properties|^/assets} } + end + + def collect_engine_routes(name, rack_app) + return unless rack_app && ENV["ENGINES"] && rack_app.respond_to?(:routes) + return if @engines[name] + + routes = rack_app.routes + if routes.is_a?(ActionDispatch::Routing::RouteSet) + @engines[name] = collect_routes(routes.routes) + end + end + + def formatted_routes_for_engines + @engines.map do |name, routes| + ["\nRoutes for #{name}:"] + formatted_routes(routes) + end.flatten + end + def formatted_routes(routes) name_width = routes.map{ |r| r[:name].length }.max verb_width = routes.map{ |r| r[:verb].length }.max path_width = routes.map{ |r| r[:path].length }.max diff --git a/railties/lib/rails/code_statistics.rb b/railties/lib/rails/code_statistics.rb index e6822b75b7..435ea83ad8 100644 --- a/railties/lib/rails/code_statistics.rb +++ b/railties/lib/rails/code_statistics.rb @@ -38,11 +38,22 @@ class CodeStatistics #:nodoc: next unless file_name =~ pattern f = File.open(directory + "/" + file_name) - + comment_started = false while line = f.gets stats["lines"] += 1 - stats["classes"] += 1 if line =~ /class [A-Z]/ - stats["methods"] += 1 if line =~ /def [a-z]/ + if(comment_started) + if line =~ /^=end/ + comment_started = false + end + next + else + if line =~ /^=begin/ + comment_started = true + next + end + end + stats["classes"] += 1 if line =~ /^\s*class\s+[_A-Z]/ + stats["methods"] += 1 if line =~ /^\s*def\s+[_a-z]/ stats["codelines"] += 1 unless line =~ /^\s*$/ || line =~ /^\s*#/ end end diff --git a/railties/lib/rails/engine.rb b/railties/lib/rails/engine.rb index 335a0fb1b5..5c1af99fe2 100644 --- a/railties/lib/rails/engine.rb +++ b/railties/lib/rails/engine.rb @@ -330,6 +330,17 @@ module Rails # # 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. + # + # Example: + # + # # 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" autoload :Railties, "rails/engine/railties" @@ -480,10 +491,19 @@ module Rails @routes end + def ordered_railties + railties.all + [self] + end + def initializers initializers = [] - railties.all { |r| initializers += r.initializers } - initializers += super + ordered_railties.each do |r| + if r == self + initializers += super + else + initializers += r.initializers + end + end initializers end diff --git a/railties/lib/rails/generators/actions.rb b/railties/lib/rails/generators/actions.rb index b26839644e..184cb2866b 100644 --- a/railties/lib/rails/generators/actions.rb +++ b/railties/lib/rails/generators/actions.rb @@ -68,7 +68,7 @@ module Rails end in_root do - str = "gem #{parts.join(", ")}\n" + str = "\ngem #{parts.join(", ")}\n" str = " " + str if @in_group append_file "Gemfile", str, :verbose => false end diff --git a/railties/lib/rails/tasks/routes.rake b/railties/lib/rails/tasks/routes.rake index 7dc54144da..df72baef67 100644 --- a/railties/lib/rails/tasks/routes.rake +++ b/railties/lib/rails/tasks/routes.rake @@ -1,4 +1,4 @@ -desc 'Print out all defined routes in match order, with names. Target specific controller with CONTROLLER=x.' +desc 'Print out all defined routes in match order, with names. Target specific controller with CONTROLLER=x. Include engine\'s routes with ENGINES=true' task :routes => :environment do Rails.application.reload_routes! all_routes = Rails.application.routes.routes diff --git a/railties/test/application/middleware/cookies_test.rb b/railties/test/application/middleware/cookies_test.rb new file mode 100644 index 0000000000..13556cbed2 --- /dev/null +++ b/railties/test/application/middleware/cookies_test.rb @@ -0,0 +1,47 @@ +require 'isolation/abstract_unit' + +module ApplicationTests + class CookiesTest < Test::Unit::TestCase + include ActiveSupport::Testing::Isolation + + def new_app + File.expand_path("#{app_path}/../new_app") + end + + def setup + build_app + boot_rails + FileUtils.rm_rf("#{app_path}/config/environments") + end + + def teardown + teardown_app + FileUtils.rm_rf(new_app) if File.directory?(new_app) + end + + test 'always_write_cookie is true by default in development' do + require 'rails' + Rails.env = 'development' + require "#{app_path}/config/environment" + assert_equal true, ActionDispatch::Cookies::CookieJar.always_write_cookie + end + + test 'always_write_cookie is false by default in production' do + require 'rails' + Rails.env = 'production' + require "#{app_path}/config/environment" + assert_equal false, ActionDispatch::Cookies::CookieJar.always_write_cookie + end + + test 'always_write_cookie can be overrided' do + add_to_config <<-RUBY + config.action_dispatch.always_write_cookie = false + RUBY + + require 'rails' + Rails.env = 'development' + require "#{app_path}/config/environment" + assert_equal false, ActionDispatch::Cookies::CookieJar.always_write_cookie + end + end +end diff --git a/railties/test/application/route_inspect_test.rb b/railties/test/application/route_inspect_test.rb index 78980705ed..130d4e52f8 100644 --- a/railties/test/application/route_inspect_test.rb +++ b/railties/test/application/route_inspect_test.rb @@ -1,6 +1,7 @@ require 'test/unit' require 'rails/application/route_inspector' require 'action_controller' +require 'rails/engine' module ApplicationTests class RouteInspectTest < Test::Unit::TestCase @@ -9,6 +10,35 @@ module ApplicationTests @inspector = Rails::Application::RouteInspector.new end + def test_displaying_routes_for_engines + ENV["ENGINES"] = "true" + + engine = Class.new(Rails::Engine) do + def self.to_s + "Blog::Engine" + end + end + engine.routes.draw do + get '/cart', :to => 'cart#show' + end + + @set.draw do + get '/custom/assets', :to => 'custom_assets#show' + mount engine => "/blog", :as => "blog" + end + + output = @inspector.format @set.routes + expected = [ + "custom_assets GET /custom/assets(.:format) custom_assets#show", + " blog /blog Blog::Engine", + "\nRoutes for Blog::Engine:", + "cart GET /cart(.:format) cart#show" + ] + assert_equal expected, output + ensure + ENV["ENGINES"] = nil + end + def test_cart_inspect @set.draw do get '/cart', :to => 'cart#show' diff --git a/railties/test/generators/actions_test.rb b/railties/test/generators/actions_test.rb index 51fa2fe16f..917b196777 100644 --- a/railties/test/generators/actions_test.rb +++ b/railties/test/generators/actions_test.rb @@ -113,7 +113,7 @@ class ActionsTest < Rails::Generators::TestCase gem 'fakeweb' end - assert_file 'Gemfile', /\ngroup :development, :test do\n gem "rspec-rails"\nend\n\ngroup :test do\n gem "fakeweb"\nend/ + assert_file 'Gemfile', /\ngroup :development, :test do\n \ngem "rspec-rails"\nend\n\ngroup :test do\n \ngem "fakeweb"\nend/ end def test_environment_should_include_data_in_environment_initializer_block diff --git a/railties/test/railties/engine_test.rb b/railties/test/railties/engine_test.rb index e8081c977f..400cae98b2 100644 --- a/railties/test/railties/engine_test.rb +++ b/railties/test/railties/engine_test.rb @@ -667,6 +667,132 @@ module RailtiesTest assert_equal expected, methods end + test "setting priority for engines with config.railties_order" do + @blog = engine "blog" do |plugin| + plugin.write "lib/blog.rb", <<-RUBY + module Blog + class Engine < ::Rails::Engine + end + end + RUBY + end + + @plugin.write "lib/bukkits.rb", <<-RUBY + module Bukkits + class Engine < ::Rails::Engine + isolate_namespace Bukkits + end + end + RUBY + + controller "main", <<-RUBY + class MainController < ActionController::Base + def foo + render :inline => '<%= render :partial => "shared/foo" %>' + end + + def bar + render :inline => '<%= render :partial => "shared/bar" %>' + end + end + RUBY + + app_file "config/routes.rb", <<-RUBY + Rails.application.routes.draw do + match "/foo" => "main#foo" + match "/bar" => "main#bar" + end + RUBY + + @plugin.write "app/views/shared/_foo.html.erb", <<-RUBY + Bukkit's foo partial + RUBY + + app_file "app/views/shared/_foo.html.erb", <<-RUBY + App's foo partial + RUBY + + @blog.write "app/views/shared/_bar.html.erb", <<-RUBY + Blog's bar partial + RUBY + + app_file "app/views/shared/_bar.html.erb", <<-RUBY + App's bar partial + RUBY + + @plugin.write "app/assets/javascripts/foo.js", <<-RUBY + // Bukkit's foo js + RUBY + + app_file "app/assets/javascripts/foo.js", <<-RUBY + // App's foo js + RUBY + + @blog.write "app/assets/javascripts/bar.js", <<-RUBY + // Blog's bar js + RUBY + + app_file "app/assets/javascripts/bar.js", <<-RUBY + // App's bar js + RUBY + + add_to_config("config.railties_order = [:all, :main_app, Blog::Engine]") + + boot_rails + require "#{rails_root}/config/environment" + + get("/foo") + assert_equal "Bukkit's foo partial", last_response.body.strip + + get("/bar") + assert_equal "App's bar partial", last_response.body.strip + + get("/assets/foo.js") + assert_equal "// Bukkit's foo js\n;", last_response.body.strip + + get("/assets/bar.js") + assert_equal "// App's bar js\n;", last_response.body.strip + end + + test "railties_order adds :all with lowest priority if not given" do + @plugin.write "lib/bukkits.rb", <<-RUBY + module Bukkits + class Engine < ::Rails::Engine + end + end + RUBY + + controller "main", <<-RUBY + class MainController < ActionController::Base + def foo + render :inline => '<%= render :partial => "shared/foo" %>' + end + end + RUBY + + app_file "config/routes.rb", <<-RUBY + Rails.application.routes.draw do + match "/foo" => "main#foo" + end + RUBY + + @plugin.write "app/views/shared/_foo.html.erb", <<-RUBY + Bukkit's foo partial + RUBY + + app_file "app/views/shared/_foo.html.erb", <<-RUBY + App's foo partial + RUBY + + add_to_config("config.railties_order = [Bukkits::Engine]") + + boot_rails + require "#{rails_root}/config/environment" + + get("/foo") + assert_equal "Bukkit's foo partial", last_response.body.strip + end + private def app Rails.application |