diff options
author | Xavier Noria <fxn@hashref.com> | 2010-06-28 00:12:15 +0200 |
---|---|---|
committer | Xavier Noria <fxn@hashref.com> | 2010-06-28 00:12:15 +0200 |
commit | 4329f8133fee8e4f3e558787f67de59f0c4a4dd1 (patch) | |
tree | 346ef7340d8348e50d119ca749a16c1654c20a08 /railties | |
parent | c37f7d66e49ffe5ac2115cc30e5529fd1c2924a8 (diff) | |
parent | ebee77a28a7267d5f23a28ba23c1eb88a2d7d527 (diff) | |
download | rails-4329f8133fee8e4f3e558787f67de59f0c4a4dd1.tar.gz rails-4329f8133fee8e4f3e558787f67de59f0c4a4dd1.tar.bz2 rails-4329f8133fee8e4f3e558787f67de59f0c4a4dd1.zip |
Merge remote branch 'rails/master'
Diffstat (limited to 'railties')
49 files changed, 324 insertions, 740 deletions
diff --git a/railties/CHANGELOG b/railties/CHANGELOG index c33a4d11dd..61d84e104a 100644 --- a/railties/CHANGELOG +++ b/railties/CHANGELOG @@ -1,6 +1,9 @@ *Rails 3.0.0 [Release Candidate] (unreleased)* +* config.load_(once_)paths in config/application.rb got renamed to config.autoload_(once_)paths. [fxn] + * Abort generation/booting on Ruby 1.9.1. [fxn] + * Made the rails command work even when you're in a subdirectory [Chad Fowler] diff --git a/railties/Rakefile b/railties/Rakefile index bf19961b59..ddc872e18b 100644 --- a/railties/Rakefile +++ b/railties/Rakefile @@ -18,11 +18,11 @@ task :test => 'test:isolated' namespace :test do task :isolated do dir = ENV["TEST_DIR"] || "**" - Dir["test/#{dir}/*_test.rb"].all? do |file| + Dir["test/#{dir}/*_test.rb"].each do |file| next true if file.include?("fixtures") ruby = File.join(*RbConfig::CONFIG.values_at('bindir', 'RUBY_INSTALL_NAME')) system(ruby, '-Itest', "-I#{File.dirname(__FILE__)}/../activesupport/lib", file) - end or raise "Failures" + end end end diff --git a/railties/guides/source/initialization.textile b/railties/guides/source/initialization.textile index bc3a2d0f0b..e458413b35 100644 --- a/railties/guides/source/initialization.textile +++ b/railties/guides/source/initialization.textile @@ -148,7 +148,7 @@ Here the only two gems we need are +rails+ and +sqlite3-ruby+, so it seems. This * mime-types-1.16.gem * polyglot-0.3.1.gem * rack-1.1.0.gem -* rack-mount-0.6.4.gem +* rack-mount-0.6.5.gem * rack-test-0.5.4.gem * rails-3.0.0.beta4.gem * railties-3.0.0.beta4.gem diff --git a/railties/lib/rails.rb b/railties/lib/rails.rb index ed66f493e6..bbf28a8c08 100644 --- a/railties/lib/rails.rb +++ b/railties/lib/rails.rb @@ -9,7 +9,6 @@ require 'active_support/core_ext/logger' require 'rails/application' require 'rails/version' require 'rails/deprecation' -require 'rails/log_subscriber' require 'active_support/railtie' require 'action_dispatch/railtie' diff --git a/railties/lib/rails/application.rb b/railties/lib/rails/application.rb index 8b8ef20b1f..aabe86715c 100644 --- a/railties/lib/rails/application.rb +++ b/railties/lib/rails/application.rb @@ -67,7 +67,7 @@ module Rails raise "You cannot have more than one Rails::Application" if Rails.application super Rails.application = base.instance - Rails.application.add_lib_to_load_paths! + Rails.application.add_lib_to_load_path! ActiveSupport.run_load_hooks(:before_configuration, base.instance) end @@ -97,7 +97,7 @@ module Rails # 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_paths! #:nodoc: + def add_lib_to_load_path! #:nodoc: path = config.root.join('lib').to_s $LOAD_PATH.unshift(path) if File.exists?(path) end @@ -160,6 +160,7 @@ module Rails config.middleware.build(routes) end end + alias :build_middleware_stack :app def call(env) app.call(env.reverse_merge!(env_defaults)) diff --git a/railties/lib/rails/application/configuration.rb b/railties/lib/rails/application/configuration.rb index 25e54e9dce..e3165b2d4c 100644 --- a/railties/lib/rails/application/configuration.rb +++ b/railties/lib/rails/application/configuration.rb @@ -110,7 +110,7 @@ module Rails def colorize_logging=(val) @colorize_logging = val - Rails::LogSubscriber.colorize_logging = val + ActiveSupport::LogSubscriber.colorize_logging = val self.generators.colorize_logging = val end diff --git a/railties/lib/rails/application/finisher.rb b/railties/lib/rails/application/finisher.rb index d7ff489336..855467227b 100644 --- a/railties/lib/rails/application/finisher.rb +++ b/railties/lib/rails/application/finisher.rb @@ -7,14 +7,14 @@ module Rails config.generators.templates.unshift(*paths.lib.templates.to_a) end - initializer :ensure_load_once_paths_as_subset do - extra = ActiveSupport::Dependencies.load_once_paths - - ActiveSupport::Dependencies.load_paths + initializer :ensure_autoload_once_paths_as_subset do + extra = ActiveSupport::Dependencies.autoload_once_paths - + ActiveSupport::Dependencies.autoload_paths unless extra.empty? abort <<-end_error - load_once_paths must be a subset of the load_paths. - Extra items in load_once_paths: #{extra * ','} + autoload_once_paths must be a subset of the autoload_paths. + Extra items in autoload_once_paths: #{extra * ','} end_error end end @@ -32,7 +32,7 @@ module Rails end initializer :build_middleware_stack do - app + build_middleware_stack end initializer :eager_load! do diff --git a/railties/lib/rails/commands/runner.rb b/railties/lib/rails/commands/runner.rb index 278548558e..b97ff086b6 100644 --- a/railties/lib/rails/commands/runner.rb +++ b/railties/lib/rails/commands/runner.rb @@ -44,6 +44,7 @@ begin $stderr.puts "Run '#{$0} -h' for help." exit 1 elsif File.exist?(code_or_file) + $0 = code_or_file eval(File.read(code_or_file), nil, code_or_file) else eval(code_or_file) diff --git a/railties/lib/rails/commands/server.rb b/railties/lib/rails/commands/server.rb index cb9b871875..9d9dd48ea9 100644 --- a/railties/lib/rails/commands/server.rb +++ b/railties/lib/rails/commands/server.rb @@ -9,7 +9,7 @@ module Rails args, options = args.dup, {} opt_parser = OptionParser.new do |opts| - opts.banner = "Usage: rails server [options]" + 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, diff --git a/railties/lib/rails/console/app.rb b/railties/lib/rails/console/app.rb index 4a7701081b..9d9763699d 100644 --- a/railties/lib/rails/console/app.rb +++ b/railties/lib/rails/console/app.rb @@ -30,5 +30,3 @@ def reload!(print=true) ActionDispatch::Callbacks.new(Proc.new {}, false).call({}) true end - -reload!(false) diff --git a/railties/lib/rails/engine.rb b/railties/lib/rails/engine.rb index cdb00a4eff..0a3f21fb1b 100644 --- a/railties/lib/rails/engine.rb +++ b/railties/lib/rails/engine.rb @@ -32,14 +32,14 @@ module Rails # == Configuration # # Besides the Railtie configuration which is shared across the application, in a - # Rails::Engine you can access load_paths, eager_load_paths and load_once_paths, + # Rails::Engine you can access autoload_paths, eager_load_paths and autoload_once_paths, # which differently from a Railtie, are scoped to the current Engine. # # Example: # # class MyEngine < Rails::Engine # # Add a load path for this specific Engine - # config.load_paths << File.expand_path("../lib/some/path", __FILE__) + # config.autoload_paths << File.expand_path("../lib/some/path", __FILE__) # # initializer "my_engine.add_middleware" do |app| # app.middleware.use MyEngine::Middleware @@ -142,7 +142,7 @@ module Rails # Add configured load paths to ruby load paths and remove duplicates. initializer :set_load_path, :before => :bootstrap_hook do - config.load_paths.reverse_each do |path| + config.autoload_paths.reverse_each do |path| $LOAD_PATH.unshift(path) if File.directory?(path) end $LOAD_PATH.uniq! @@ -154,17 +154,17 @@ module Rails # 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 |app| - ActiveSupport::Dependencies.load_paths.unshift(*config.load_paths) + ActiveSupport::Dependencies.autoload_paths.unshift(*config.autoload_paths) if reloadable?(app) - ActiveSupport::Dependencies.load_once_paths.unshift(*config.load_once_paths) + ActiveSupport::Dependencies.autoload_once_paths.unshift(*config.autoload_once_paths) else - ActiveSupport::Dependencies.load_once_paths.unshift(*config.load_paths) + ActiveSupport::Dependencies.autoload_once_paths.unshift(*config.autoload_paths) end # Freeze so future modifications will fail rather than do nothing mysteriously - config.load_paths.freeze - config.load_once_paths.freeze + config.autoload_paths.freeze + config.autoload_once_paths.freeze end initializer :add_routing_paths do |app| diff --git a/railties/lib/rails/engine/configuration.rb b/railties/lib/rails/engine/configuration.rb index 446fe0bda9..4e27180f1e 100644 --- a/railties/lib/rails/engine/configuration.rb +++ b/railties/lib/rails/engine/configuration.rb @@ -4,7 +4,7 @@ module Rails class Engine class Configuration < ::Rails::Railtie::Configuration attr_reader :root - attr_writer :eager_load_paths, :load_once_paths, :load_paths + attr_writer :eager_load_paths, :autoload_once_paths, :autoload_paths def initialize(root=nil) super() @@ -41,12 +41,12 @@ module Rails @eager_load_paths ||= paths.eager_load end - def load_once_paths - @load_once_paths ||= paths.load_once + def autoload_once_paths + @autoload_once_paths ||= paths.load_once end - def load_paths - @load_paths ||= paths.load_paths + def autoload_paths + @autoload_paths ||= paths.load_paths end end end diff --git a/railties/lib/rails/generators.rb b/railties/lib/rails/generators.rb index 41aecea355..8794392a7d 100644 --- a/railties/lib/rails/generators.rb +++ b/railties/lib/rails/generators.rb @@ -50,7 +50,6 @@ module Rails :performance_tool => nil, :resource_controller => :controller, :scaffold_controller => :scaffold_controller, - :singleton => false, :stylesheets => true, :test_framework => nil, :template_engine => :erb @@ -334,4 +333,4 @@ module Rails paths end end -end
\ No newline at end of file +end diff --git a/railties/lib/rails/generators/base.rb b/railties/lib/rails/generators/base.rb index bd2260fc29..67a9a6030d 100644 --- a/railties/lib/rails/generators/base.rb +++ b/railties/lib/rails/generators/base.rb @@ -3,7 +3,7 @@ begin 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 /path/to/rails myapp --dev`" + "as `ruby /path/to/rails new myapp --dev`" exit end diff --git a/railties/lib/rails/generators/erb/scaffold/scaffold_generator.rb b/railties/lib/rails/generators/erb/scaffold/scaffold_generator.rb index 2db7f7bbf3..b2c8d7051b 100644 --- a/railties/lib/rails/generators/erb/scaffold/scaffold_generator.rb +++ b/railties/lib/rails/generators/erb/scaffold/scaffold_generator.rb @@ -8,17 +8,12 @@ module Erb argument :attributes, :type => :array, :default => [], :banner => "field:type field:type" - class_option :singleton, :type => :boolean, :desc => "Supply to skip index view" - def create_root_folder empty_directory File.join("app/views", controller_file_path) end def copy_view_files - views = available_views - views.delete("index") if options[:singleton] - - views.each do |view| + available_views.each do |view| filename = filename_with_extensions(view) template filename, File.join("app/views", controller_file_path, filename) 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 index 9b83207b3f..d12b2ff0e5 100644 --- a/railties/lib/rails/generators/erb/scaffold/templates/_form.html.erb +++ b/railties/lib/rails/generators/erb/scaffold/templates/_form.html.erb @@ -1,10 +1,10 @@ -<%%= form_for(@<%= singular_name %>) do |f| %> - <%% if @<%= singular_name %>.errors.any? %> +<%%= form_for(@<%= singular_table_name %>) do |f| %> + <%% if @<%= singular_table_name %>.errors.any? %> <div id="error_explanation"> - <h2><%%= pluralize(@<%= singular_name %>.errors.count, "error") %> prohibited this <%= singular_name %> from being saved:</h2> + <h2><%%= pluralize(@<%= singular_table_name %>.errors.count, "error") %> prohibited this <%= singular_table_name %> from being saved:</h2> <ul> - <%% @<%= singular_name %>.errors.full_messages.each do |msg| %> + <%% @<%= singular_table_name %>.errors.full_messages.each do |msg| %> <li><%%= msg %></li> <%% end %> </ul> diff --git a/railties/lib/rails/generators/erb/scaffold/templates/edit.html.erb b/railties/lib/rails/generators/erb/scaffold/templates/edit.html.erb index 415f820206..e58b9fbd08 100644 --- a/railties/lib/rails/generators/erb/scaffold/templates/edit.html.erb +++ b/railties/lib/rails/generators/erb/scaffold/templates/edit.html.erb @@ -1,6 +1,6 @@ -<h1>Editing <%= singular_name %></h1> +<h1>Editing <%= singular_table_name %></h1> <%%= render 'form' %> -<%%= link_to 'Show', @<%= singular_name %> %> | +<%%= 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 index d30d306d42..4c46db4d67 100644 --- a/railties/lib/rails/generators/erb/scaffold/templates/index.html.erb +++ b/railties/lib/rails/generators/erb/scaffold/templates/index.html.erb @@ -1,4 +1,4 @@ -<h1>Listing <%= plural_name %></h1> +<h1>Listing <%= plural_table_name %></h1> <table> <tr> @@ -10,18 +10,18 @@ <th></th> </tr> -<%% @<%= plural_name %>.each do |<%= singular_name %>| %> +<%% @<%= plural_table_name %>.each do |<%= singular_table_name %>| %> <tr> <% for attribute in attributes -%> - <td><%%= <%= singular_name %>.<%= attribute.name %> %></td> + <td><%%= <%= singular_table_name %>.<%= attribute.name %> %></td> <% end -%> - <td><%%= link_to 'Show', <%= singular_name %> %></td> - <td><%%= link_to 'Edit', edit_<%= singular_name %>_path(<%= singular_name %>) %></td> - <td><%%= link_to 'Destroy', <%= singular_name %>, :confirm => 'Are you sure?', :method => :delete %></td> + <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 %>, :confirm => 'Are you sure?', :method => :delete %></td> </tr> <%% end %> </table> <br /> -<%%= link_to 'New <%= human_name %>', new_<%= singular_name %>_path %> +<%%= 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 index ddabc9d349..02ae4d015e 100644 --- a/railties/lib/rails/generators/erb/scaffold/templates/new.html.erb +++ b/railties/lib/rails/generators/erb/scaffold/templates/new.html.erb @@ -1,4 +1,4 @@ -<h1>New <%= singular_name %></h1> +<h1>New <%= singular_table_name %></h1> <%%= render 'form' %> diff --git a/railties/lib/rails/generators/erb/scaffold/templates/show.html.erb b/railties/lib/rails/generators/erb/scaffold/templates/show.html.erb index 31b8253b35..c0e5ccff1e 100644 --- a/railties/lib/rails/generators/erb/scaffold/templates/show.html.erb +++ b/railties/lib/rails/generators/erb/scaffold/templates/show.html.erb @@ -3,10 +3,10 @@ <% for attribute in attributes -%> <p> <b><%= attribute.human_name %>:</b> - <%%= @<%= singular_name %>.<%= attribute.name %> %> + <%%= @<%= singular_table_name %>.<%= attribute.name %> %> </p> <% end -%> -<%%= link_to 'Edit', edit_<%= singular_name %>_path(@<%= singular_name %>) %> | +<%%= link_to 'Edit', edit_<%= singular_table_name %>_path(@<%= singular_table_name %>) %> | <%%= link_to 'Back', <%= index_helper %>_path %> diff --git a/railties/lib/rails/generators/named_base.rb b/railties/lib/rails/generators/named_base.rb index 72ec2856d0..44f831e6f3 100644 --- a/railties/lib/rails/generators/named_base.rb +++ b/railties/lib/rails/generators/named_base.rb @@ -51,7 +51,23 @@ module Rails end def index_helper - uncountable? ? "#{plural_name}_index" : plural_name + uncountable? ? "#{plural_table_name}_index" : plural_table_name + end + + def singular_table_name + @singular_table_name ||= table_name.singularize + end + + def plural_table_name + @plural_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. diff --git a/railties/lib/rails/generators/rails/app/templates/Gemfile b/railties/lib/rails/generators/rails/app/templates/Gemfile index 0b922a89c0..a108968b97 100644 --- a/railties/lib/rails/generators/rails/app/templates/Gemfile +++ b/railties/lib/rails/generators/rails/app/templates/Gemfile @@ -32,8 +32,9 @@ gem '<%= gem_for_database %>'<% if require_for_database %>, :require => '<%= req # gem 'sqlite3-ruby', :require => 'sqlite3' # gem 'aws-s3', :require => 'aws/s3' -# Bundle gems for certain environments: -# gem 'rspec', :group => :test -# group :test do +# Bundle gems for the local environment. Make sure to +# put test-only gems in this group so their generators +# and rake tasks are available in development mode: +# group :development, :test do # gem 'webrat' # end diff --git a/railties/lib/rails/generators/rails/app/templates/config/application.rb b/railties/lib/rails/generators/rails/app/templates/config/application.rb index 0066e2b0c2..031466cb86 100644 --- a/railties/lib/rails/generators/rails/app/templates/config/application.rb +++ b/railties/lib/rails/generators/rails/app/templates/config/application.rb @@ -22,7 +22,7 @@ module <%= app_const_base %> # -- all .rb files in that directory are automatically loaded. # Add additional load paths for your own custom dirs - # config.load_paths += %W( #{config.root}/extras ) + # config.autoload_paths += %W( #{config.root}/extras ) # Only load the plugins named here, in the order given (default is alphabetical). # :all can be used as a placeholder for all plugins not explicitly named diff --git a/railties/lib/rails/generators/rails/model/USAGE b/railties/lib/rails/generators/rails/model/USAGE index db98a2dd1b..67f76aad01 100644 --- a/railties/lib/rails/generators/rails/model/USAGE +++ b/railties/lib/rails/generators/rails/model/USAGE @@ -40,6 +40,6 @@ Examples: Module: app/models/admin.rb Model: app/models/admin/account.rb Test: test/unit/admin/account_test.rb - Fixtures: test/fixtures/admin_accounts.yml + Fixtures: test/fixtures/admin/accounts.yml Migration: db/migrate/XXX_add_admin_accounts.rb diff --git a/railties/lib/rails/generators/rails/resource/resource_generator.rb b/railties/lib/rails/generators/rails/resource/resource_generator.rb index 8a46708009..fc070026d6 100644 --- a/railties/lib/rails/generators/rails/resource/resource_generator.rb +++ b/railties/lib/rails/generators/rails/resource/resource_generator.rb @@ -14,23 +14,13 @@ module Rails class_option :actions, :type => :array, :banner => "ACTION ACTION", :default => [], :desc => "Actions for the resource controller" - class_option :singleton, :type => :boolean, :desc => "Supply to create a singleton controller" - def add_resource_route return if options[:actions].present? - route "resource#{:s unless options[:singleton]} :#{pluralize?(file_name)}" + route_config = class_path.collect{|namespace| "namespace :#{namespace} do " }.join(" ") + route_config << "resources :#{file_name.pluralize}" + route_config << " end" * class_path.size + route route_config end - - protected - - def pluralize?(name) - if options[:singleton] - name - else - name.pluralize - end - end - end end end 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 index 49af2974cd..2271c6f9c1 100644 --- a/railties/lib/rails/generators/rails/scaffold_controller/scaffold_controller_generator.rb +++ b/railties/lib/rails/generators/rails/scaffold_controller/scaffold_controller_generator.rb @@ -10,8 +10,6 @@ module Rails class_option :orm, :banner => "NAME", :type => :string, :required => true, :desc => "ORM to generate the controller for" - class_option :singleton, :type => :boolean, :desc => "Supply to create a singleton controller" - def create_controller_files template 'controller.rb', File.join('app/controllers', class_path, "#{controller_file_name}_controller.rb") 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 index b5f19b6d15..b21340f755 100644 --- a/railties/lib/rails/generators/rails/scaffold_controller/templates/controller.rb +++ b/railties/lib/rails/generators/rails/scaffold_controller/templates/controller.rb @@ -1,53 +1,51 @@ class <%= controller_class_name %>Controller < ApplicationController -<% unless options[:singleton] -%> - # GET /<%= table_name %> - # GET /<%= table_name %>.xml + # GET <%= route_url %> + # GET <%= route_url %>.xml def index - @<%= table_name %> = <%= orm_class.all(class_name) %> + @<%= plural_table_name %> = <%= orm_class.all(class_name) %> respond_to do |format| format.html # index.html.erb - format.xml { render :xml => @<%= table_name %> } + format.xml { render :xml => @<%= plural_table_name %> } end end -<% end -%> - # GET /<%= table_name %>/1 - # GET /<%= table_name %>/1.xml + # GET <%= route_url %>/1 + # GET <%= route_url %>/1.xml def show - @<%= file_name %> = <%= orm_class.find(class_name, "params[:id]") %> + @<%= singular_table_name %> = <%= orm_class.find(class_name, "params[:id]") %> respond_to do |format| format.html # show.html.erb - format.xml { render :xml => @<%= file_name %> } + format.xml { render :xml => @<%= singular_table_name %> } end end - # GET /<%= table_name %>/new - # GET /<%= table_name %>/new.xml + # GET <%= route_url %>/new + # GET <%= route_url %>/new.xml def new - @<%= file_name %> = <%= orm_class.build(class_name) %> + @<%= singular_table_name %> = <%= orm_class.build(class_name) %> respond_to do |format| format.html # new.html.erb - format.xml { render :xml => @<%= file_name %> } + format.xml { render :xml => @<%= singular_table_name %> } end end - # GET /<%= table_name %>/1/edit + # GET <%= route_url %>/1/edit def edit - @<%= file_name %> = <%= orm_class.find(class_name, "params[:id]") %> + @<%= singular_table_name %> = <%= orm_class.find(class_name, "params[:id]") %> end - # POST /<%= table_name %> - # POST /<%= table_name %>.xml + # POST <%= route_url %> + # POST <%= route_url %>.xml def create - @<%= file_name %> = <%= orm_class.build(class_name, "params[:#{file_name}]") %> + @<%= singular_table_name %> = <%= orm_class.build(class_name, "params[:#{singular_table_name}]") %> respond_to do |format| if @<%= orm_instance.save %> - format.html { redirect_to(@<%= file_name %>, :notice => '<%= human_name %> was successfully created.') } - format.xml { render :xml => @<%= file_name %>, :status => :created, :location => @<%= file_name %> } + format.html { redirect_to(@<%= singular_table_name %>, :notice => '<%= human_name %> was successfully created.') } + format.xml { render :xml => @<%= singular_table_name %>, :status => :created, :location => @<%= singular_table_name %> } else format.html { render :action => "new" } format.xml { render :xml => @<%= orm_instance.errors %>, :status => :unprocessable_entity } @@ -55,14 +53,14 @@ class <%= controller_class_name %>Controller < ApplicationController end end - # PUT /<%= table_name %>/1 - # PUT /<%= table_name %>/1.xml + # PUT <%= route_url %>/1 + # PUT <%= route_url %>/1.xml def update - @<%= file_name %> = <%= orm_class.find(class_name, "params[:id]") %> + @<%= singular_table_name %> = <%= orm_class.find(class_name, "params[:id]") %> respond_to do |format| - if @<%= orm_instance.update_attributes("params[:#{file_name}]") %> - format.html { redirect_to(@<%= file_name %>, :notice => '<%= human_name %> was successfully updated.') } + if @<%= orm_instance.update_attributes("params[:#{singular_table_name}]") %> + format.html { redirect_to(@<%= singular_table_name %>, :notice => '<%= human_name %> was successfully updated.') } format.xml { head :ok } else format.html { render :action => "edit" } @@ -71,10 +69,10 @@ class <%= controller_class_name %>Controller < ApplicationController end end - # DELETE /<%= table_name %>/1 - # DELETE /<%= table_name %>/1.xml + # DELETE <%= route_url %>/1 + # DELETE <%= route_url %>/1.xml def destroy - @<%= file_name %> = <%= orm_class.find(class_name, "params[:id]") %> + @<%= singular_table_name %> = <%= orm_class.find(class_name, "params[:id]") %> @<%= orm_instance.destroy %> respond_to do |format| diff --git a/railties/lib/rails/generators/resource_helpers.rb b/railties/lib/rails/generators/resource_helpers.rb index 3a98a8f9c1..829f4b200a 100644 --- a/railties/lib/rails/generators/resource_helpers.rb +++ b/railties/lib/rails/generators/resource_helpers.rb @@ -72,7 +72,7 @@ module Rails end # Initialize ORM::Generators::ActiveModel to access instance methods. - def orm_instance(name=file_name) + def orm_instance(name=singular_table_name) @orm_instance ||= @orm_class.new(name) 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 index 609b815683..c1dd535dd3 100644 --- a/railties/lib/rails/generators/test_unit/model/model_generator.rb +++ b/railties/lib/rails/generators/test_unit/model/model_generator.rb @@ -16,7 +16,7 @@ module TestUnit def create_fixture_file if options[:fixture] && options[:fixture_replacement].nil? - template 'fixtures.yml', File.join('test/fixtures', "#{table_name}.yml") + template 'fixtures.yml', File.join('test/fixtures', class_path, "#{plural_file_name}.yml") end end end diff --git a/railties/lib/rails/generators/test_unit/scaffold/scaffold_generator.rb b/railties/lib/rails/generators/test_unit/scaffold/scaffold_generator.rb index c0315c7fe6..f7e907a017 100644 --- a/railties/lib/rails/generators/test_unit/scaffold/scaffold_generator.rb +++ b/railties/lib/rails/generators/test_unit/scaffold/scaffold_generator.rb @@ -6,7 +6,6 @@ module TestUnit class ScaffoldGenerator < Base include Rails::Generators::ResourceHelpers - class_option :singleton, :type => :boolean, :desc => "Supply to create a singleton controller" check_class_collision :suffix => "ControllerTest" def create_test_files 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 index d5d3d6d5cd..f23e495450 100644 --- a/railties/lib/rails/generators/test_unit/scaffold/templates/functional_test.rb +++ b/railties/lib/rails/generators/test_unit/scaffold/templates/functional_test.rb @@ -2,48 +2,46 @@ require 'test_helper' class <%= controller_class_name %>ControllerTest < ActionController::TestCase setup do - @<%= file_name %> = <%= table_name %>(:one) + @<%= singular_table_name %> = <%= table_name %>(:one) end -<% unless options[:singleton] -%> test "should get index" do get :index assert_response :success assert_not_nil assigns(:<%= table_name %>) end -<% end -%> test "should get new" do get :new assert_response :success end - test "should create <%= file_name %>" do + test "should create <%= singular_table_name %>" do assert_difference('<%= class_name %>.count') do - post :create, :<%= file_name %> => @<%= file_name %>.attributes + post :create, :<%= singular_table_name %> => @<%= singular_table_name %>.attributes end - assert_redirected_to <%= file_name %>_path(assigns(:<%= file_name %>)) + assert_redirected_to <%= singular_table_name %>_path(assigns(:<%= singular_table_name %>)) end - test "should show <%= file_name %>" do - get :show, :id => @<%= file_name %>.to_param + test "should show <%= singular_table_name %>" do + get :show, :id => @<%= singular_table_name %>.to_param assert_response :success end test "should get edit" do - get :edit, :id => @<%= file_name %>.to_param + get :edit, :id => @<%= singular_table_name %>.to_param assert_response :success end - test "should update <%= file_name %>" do - put :update, :id => @<%= file_name %>.to_param, :<%= file_name %> => @<%= file_name %>.attributes - assert_redirected_to <%= file_name %>_path(assigns(:<%= file_name %>)) + test "should update <%= singular_table_name %>" do + put :update, :id => @<%= singular_table_name %>.to_param, :<%= singular_table_name %> => @<%= singular_table_name %>.attributes + assert_redirected_to <%= singular_table_name %>_path(assigns(:<%= singular_table_name %>)) end - test "should destroy <%= file_name %>" do + test "should destroy <%= singular_table_name %>" do assert_difference('<%= class_name %>.count', -1) do - delete :destroy, :id => @<%= file_name %>.to_param + delete :destroy, :id => @<%= singular_table_name %>.to_param end assert_redirected_to <%= index_helper %>_path diff --git a/railties/lib/rails/initializable.rb b/railties/lib/rails/initializable.rb index 9a82e051e7..686a2dc0cb 100644 --- a/railties/lib/rails/initializable.rb +++ b/railties/lib/rails/initializable.rb @@ -39,11 +39,6 @@ module Rails select { |i| i.before == initializer.name || i.name == initializer.after }.each(&block) end - def initialize(initializers = []) - super(initializers) - replace(tsort) - end - def +(other) Collection.new(to_a + other.to_a) end @@ -51,7 +46,7 @@ module Rails def run_initializers(*args) return if instance_variable_defined?(:@ran) - initializers.each do |initializer| + initializers.tsort.each do |initializer| initializer.run(*args) end @ran = true @@ -63,7 +58,7 @@ module Rails module ClassMethods def initializers - @initializers ||= [] + @initializers ||= Collection.new end def initializers_chain @@ -84,14 +79,6 @@ module Rails opts[:after] ||= initializers.last.name unless initializers.empty? || initializers.find { |i| i.name == opts[:before] } initializers << Initializer.new(name, nil, opts, &blk) end - - def run_initializers(*args) - return if @ran - initializers_chain.each do |initializer| - instance_exec(*args, &initializer.block) - end - @ran = true - end end end -end
\ No newline at end of file +end diff --git a/railties/lib/rails/log_subscriber.rb b/railties/lib/rails/log_subscriber.rb deleted file mode 100644 index a30701d4d5..0000000000 --- a/railties/lib/rails/log_subscriber.rb +++ /dev/null @@ -1,115 +0,0 @@ -require 'active_support/core_ext/class/inheritable_attributes' -require 'active_support/notifications' - -module Rails - # Rails::LogSubscriber is an object set to consume ActiveSupport::Notifications - # on initialization with solely purpose of logging. The log subscriber dispatches - # notifications to a regirested object based on its given namespace. - # - # An example would be Active Record log subscriber responsible for logging queries: - # - # module ActiveRecord - # class Railtie - # class LogSubscriber < Rails::LogSubscriber - # def sql(event) - # "#{event.payload[:name]} (#{event.duration}) #{event.payload[:sql]}" - # end - # end - # end - # end - # - # It's finally registed as: - # - # Rails::LogSubscriber.add :active_record, ActiveRecord::Railtie::LogSubscriber.new - # - # So whenever a "sql.active_record" notification arrive to Rails::LogSubscriber, - # it will properly dispatch the event (ActiveSupport::Notifications::Event) to - # the sql method. - # - # This is useful because it avoids spanning several log subscribers just for logging - # purposes(which slows down the main thread). Besides of providing a centralized - # facility on top of Rails.logger. - # - # Log subscriber also has some helpers to deal with logging and automatically flushes - # all logs when the request finishes (via action_dispatch.callback notification). - class LogSubscriber - mattr_accessor :colorize_logging - self.colorize_logging = true - - # Embed in a String to clear all previous ANSI sequences. - CLEAR = "\e[0m" - BOLD = "\e[1m" - - # Colors - BLACK = "\e[30m" - RED = "\e[31m" - GREEN = "\e[32m" - YELLOW = "\e[33m" - BLUE = "\e[34m" - MAGENTA = "\e[35m" - CYAN = "\e[36m" - WHITE = "\e[37m" - - def self.add(namespace, log_subscriber, notifier = ActiveSupport::Notifications) - log_subscribers << log_subscriber - @flushable_loggers = nil - - log_subscriber.public_methods(false).each do |event| - notifier.subscribe("#{event}.#{namespace}") do |*args| - next if log_subscriber.logger.nil? - - begin - log_subscriber.send(event, ActiveSupport::Notifications::Event.new(*args)) - rescue Exception => e - Rails.logger.error "Could not log #{args[0].inspect} event. #{e.class}: #{e.message}" - end - end - end - end - - def self.log_subscribers - @log_subscribers ||= [] - end - - def self.flushable_loggers - @flushable_loggers ||= begin - loggers = log_subscribers.map(&:logger) - loggers.uniq! - loggers.select { |l| l.respond_to?(:flush) } - end - end - - # Flush all log_subscribers' logger. - def self.flush_all! - flushable_loggers.each(&:flush) - end - - # By default, we use the Rails.logger for logging. - def logger - Rails.logger - end - - protected - - %w(info debug warn error fatal unknown).each do |level| - class_eval <<-METHOD, __FILE__, __LINE__ + 1 - def #{level}(*args, &block) - return unless logger - logger.#{level}(*args, &block) - end - METHOD - end - - # Set color by using a string or one of the defined constants. If a third - # option is set to true, it also adds bold to the string. This is based - # on Highline implementation and it automatically appends CLEAR to the end - # of the returned String. - # - def color(text, color, bold=false) - return text unless colorize_logging - color = self.class.const_get(color.to_s.upcase) if color.is_a?(Symbol) - bold = bold ? BOLD : "" - "#{bold}#{color}#{text}#{CLEAR}" - end - end -end diff --git a/railties/lib/rails/log_subscriber/test_helper.rb b/railties/lib/rails/log_subscriber/test_helper.rb deleted file mode 100644 index 9b7b0738cd..0000000000 --- a/railties/lib/rails/log_subscriber/test_helper.rb +++ /dev/null @@ -1,97 +0,0 @@ -require 'rails/log_subscriber' - -module Rails - class LogSubscriber - # Provides some helpers to deal with testing log subscribers by setting up - # notifications. Take for instance Active Record subscriber tests: - # - # class SyncLogSubscriberTest < ActiveSupport::TestCase - # include Rails::LogSubscriber::TestHelper - # Rails::LogSubscriber.add(:active_record, ActiveRecord::Railties::LogSubscriber.new) - # - # def test_basic_query_logging - # Developer.all - # wait - # assert_equal 1, @logger.logged(:debug).size - # assert_match /Developer Load/, @logger.logged(:debug).last - # assert_match /SELECT \* FROM "developers"/, @logger.logged(:debug).last - # end - # - # class SyncLogSubscriberTest < ActiveSupport::TestCase - # include Rails::LogSubscriber::SyncTestHelper - # include LogSubscriberTest - # end - # - # class AsyncLogSubscriberTest < ActiveSupport::TestCase - # include Rails::LogSubscriber::AsyncTestHelper - # include LogSubscriberTest - # end - # end - # - # All you need to do is to ensure that your log subscriber is added to Rails::Subscriber, - # as in the second line of the code above. The test helpers is reponsible for setting - # up the queue, subscriptions and turning colors in logs off. - # - # The messages are available in the @logger instance, which is a logger with limited - # powers (it actually do not send anything to your output), and you can collect them - # doing @logger.logged(level), where level is the level used in logging, like info, - # debug, warn and so on. - # - module TestHelper - def setup - @logger = MockLogger.new - @notifier = ActiveSupport::Notifications::Notifier.new(queue) - - Rails::LogSubscriber.colorize_logging = false - - set_logger(@logger) - ActiveSupport::Notifications.notifier = @notifier - end - - def teardown - set_logger(nil) - ActiveSupport::Notifications.notifier = nil - end - - class MockLogger - attr_reader :flush_count - - def initialize - @flush_count = 0 - @logged = Hash.new { |h,k| h[k] = [] } - end - - def method_missing(level, message) - @logged[level] << message - end - - def logged(level) - @logged[level].compact.map { |l| l.to_s.strip } - end - - def flush - @flush_count += 1 - end - end - - # Wait notifications to be published. - def wait - @notifier.wait - end - - # Overwrite if you use another logger in your log subscriber: - # - # def logger - # ActiveRecord::Base.logger = @logger - # end - # - def set_logger(logger) - Rails.logger = logger - end - - def queue - ActiveSupport::Notifications::Fanout.new - end - end - end -end
\ No newline at end of file diff --git a/railties/lib/rails/rack/logger.rb b/railties/lib/rails/rack/logger.rb index 73e9af3b41..b3dc1f894c 100644 --- a/railties/lib/rails/rack/logger.rb +++ b/railties/lib/rails/rack/logger.rb @@ -1,10 +1,9 @@ -require 'rails/log_subscriber' require 'active_support/core_ext/time/conversions' module Rails module Rack # Log the request started and flush all loggers after it. - class Logger < Rails::LogSubscriber + class Logger < ActiveSupport::LogSubscriber def initialize(app) @app = app end @@ -16,20 +15,19 @@ module Rails after_dispatch(env) end - protected + protected - def before_dispatch(env) - request = ActionDispatch::Request.new(env) - path = request.fullpath + def before_dispatch(env) + request = ActionDispatch::Request.new(env) + path = request.fullpath - info "\n\nStarted #{env["REQUEST_METHOD"]} \"#{path}\" " \ - "for #{request.ip} at #{Time.now.to_default_s}" - end - - def after_dispatch(env) - Rails::LogSubscriber.flush_all! - end + info "\n\nStarted #{env["REQUEST_METHOD"]} \"#{path}\" " \ + "for #{request.ip} at #{Time.now.to_default_s}" + end + def after_dispatch(env) + ActiveSupport::LogSubscriber.flush_all! + end end end end diff --git a/railties/lib/rails/railtie.rb b/railties/lib/rails/railtie.rb index ad776933f2..dbdbfea509 100644 --- a/railties/lib/rails/railtie.rb +++ b/railties/lib/rails/railtie.rb @@ -114,36 +114,6 @@ module Rails # end # end # - # == Adding your subscriber - # - # Since version 3.0, Rails ships with a notification system which is used for several - # purposes, including logging. If you are sending notifications in your Railtie, you may - # want to add a subscriber to consume such notifications for logging purposes. - # - # The subscriber is added under the railtie_name namespace and only consumes notifications - # under the given namespace. For example, let's suppose your railtie is publishing the - # following "something_expensive" instrumentation: - # - # ActiveSupport::Notifications.instrument "my_railtie.something_expensive" do - # # something expensive - # end - # - # You can log this instrumentation with your own Rails::Subscriber: - # - # class MyRailtie::Subscriber < Rails::Subscriber - # def something_expensive(event) - # info("Something expensive took %.1fms" % event.duration) - # end - # end - # - # By registering it: - # - # class MyRailtie < Railtie - # subscriber :my_gem, MyRailtie::Subscriber.new - # end - # - # Take a look in Rails::Subscriber docs for more information. - # # == Application, Plugin and Engine # # A Rails::Engine is nothing more than a Railtie with some initializers already set. @@ -176,8 +146,8 @@ module Rails ActiveSupport::Deprecation.warn "railtie_name is deprecated and has no effect", caller end - def log_subscriber(name, log_subscriber) - Rails::LogSubscriber.add(name, log_subscriber) + def log_subscriber(*) + ActiveSupport::Deprecation.warn "log_subscriber is deprecated and has no effect", caller end def rake_tasks(&blk) diff --git a/railties/lib/rails/webrick_server.rb b/railties/lib/rails/webrick_server.rb deleted file mode 100644 index f3b74c28d3..0000000000 --- a/railties/lib/rails/webrick_server.rb +++ /dev/null @@ -1,156 +0,0 @@ -# Donated by Florian Gross - -require 'webrick' -require 'cgi' -require 'stringio' -require 'dispatcher' - -include WEBrick - -class CGI #:nodoc: - def stdinput - @stdin || $stdin - end - - def env_table - @env_table || ENV - end - - def initialize(type = "query", table = nil, stdin = nil) - @env_table, @stdin = table, stdin - - if defined?(MOD_RUBY) && !ENV.key?("GATEWAY_INTERFACE") - Apache.request.setup_cgi_env - end - - extend QueryExtension - @multipart = false - if defined?(CGI_PARAMS) - warn "do not use CGI_PARAMS and CGI_COOKIES" - @params = CGI_PARAMS.dup - @cookies = CGI_COOKIES.dup - else - initialize_query() # set @params, @cookies - end - @output_cookies = nil - @output_hidden = nil - end -end - -# A custom dispatch servlet for use with WEBrick. It dispatches requests -# (using the Rails Dispatcher) to the appropriate controller/action. By default, -# it restricts WEBrick to a managing a single Rails request at a time, but you -# can change this behavior by setting ActionController::Base.allow_concurrency -# to true. -class DispatchServlet < WEBrick::HTTPServlet::AbstractServlet - # Start the WEBrick server with the given options, mounting the - # DispatchServlet at <tt>/</tt>. - def self.dispatch(options = {}) - Socket.do_not_reverse_lookup = true # patch for OS X - - params = { :Port => options[:port].to_i, - :ServerType => options[:server_type], - :BindAddress => options[:ip] } - params[:MimeTypes] = options[:mime_types] if options[:mime_types] - - server = WEBrick::HTTPServer.new(params) - server.mount('/', DispatchServlet, options) - - trap("INT") { server.shutdown } - server.start - end - - def initialize(server, options) #:nodoc: - @server_options = options - @file_handler = WEBrick::HTTPServlet::FileHandler.new(server, options[:server_root]) - # Change to the Rails.root, since Webrick::Daemon.start does a Dir::cwd("/") - # OPTIONS['working_directory'] is an absolute path of the Rails.root, set in railties/lib/commands/servers/webrick.rb - Dir.chdir(OPTIONS['working_directory']) if defined?(OPTIONS) && File.directory?(OPTIONS['working_directory']) - super - end - - def service(req, res) #:nodoc: - unless handle_file(req, res) - unless handle_dispatch(req, res) - raise WEBrick::HTTPStatus::NotFound, "`#{req.path}' not found." - end - end - end - - def handle_file(req, res) #:nodoc: - begin - req = req.dup - path = req.path.dup - - # Add .html if the last path piece has no . in it - path << '.html' if path != '/' && (%r{(^|/)[^./]+$} =~ path) - path.gsub!('+', ' ') # Unescape + since FileHandler doesn't do so. - - req.instance_variable_set(:@path_info, path) # Set the modified path... - - @file_handler.send(:service, req, res) - return true - rescue HTTPStatus::PartialContent, HTTPStatus::NotModified => err - res.set_error(err) - return true - rescue => err - return false - end - end - - def handle_dispatch(req, res, origin = nil) #:nodoc: - data = StringIO.new - Dispatcher.dispatch( - CGI.new("query", create_env_table(req, origin), StringIO.new(req.body || "")), - ActionController::CgiRequest::DEFAULT_SESSION_OPTIONS, - data - ) - - header, body = extract_header_and_body(data) - - set_charset(header) - assign_status(res, header) - res.cookies.concat(header.delete('set-cookie') || []) - header.each { |key, val| res[key] = val.join(", ") } - - res.body = body - return true - rescue => err - p err, err.backtrace - return false - end - - private - def create_env_table(req, origin) - env = req.meta_vars.clone - env.delete "SCRIPT_NAME" - env["QUERY_STRING"] = req.request_uri.query - env["REQUEST_URI"] = origin if origin - return env - end - - def extract_header_and_body(data) - data.rewind - data = data.read - - raw_header, body = *data.split(/^[\xd\xa]{2}/on, 2) - header = WEBrick::HTTPUtils::parse_header(raw_header) - - return header, body - end - - def set_charset(header) - ct = header["content-type"] - if ct.any? { |x| x =~ /^text\// } && ! ct.any? { |x| x =~ /charset=/ } - ch = @server_options[:charset] || "UTF-8" - ct.find { |x| x =~ /^text\// } << ("; charset=" + ch) - end - end - - def assign_status(res, header) - if /^(\d+)/ =~ header['status'][0] - res.status = $1.to_i - header.delete('status') - end - end -end diff --git a/railties/test/abstract_unit.rb b/railties/test/abstract_unit.rb index d04a2aa1f3..a05bae5dcc 100644 --- a/railties/test/abstract_unit.rb +++ b/railties/test/abstract_unit.rb @@ -1,5 +1,3 @@ -ORIG_ARGV = ARGV.dup - require File.expand_path("../../../load_paths", __FILE__) require 'stringio' diff --git a/railties/test/application/initializers/frameworks_test.rb b/railties/test/application/initializers/frameworks_test.rb index 35ea2729d3..7269a7c5a8 100644 --- a/railties/test/application/initializers/frameworks_test.rb +++ b/railties/test/application/initializers/frameworks_test.rb @@ -28,8 +28,10 @@ module ApplicationTests RUBY require "#{app_path}/config/environment" - ActionController::Base.view_paths.include?(File.expand_path("app/views", app_path)) - ActionMailer::Base.view_paths.include?(File.expand_path("app/views", app_path)) + + expanded_path = File.expand_path("app/views", app_path) + assert_equal ActionController::Base.view_paths[0].to_s, expanded_path + assert_equal ActionMailer::Base.view_paths[0].to_s, expanded_path end test "allows me to configure default url options for ActionMailer" do @@ -40,7 +42,7 @@ module ApplicationTests RUBY require "#{app_path}/config/environment" - assert "test.rails", ActionMailer::Base.default_url_options[:host] + assert_equal "test.rails", ActionMailer::Base.default_url_options[:host] end # AS @@ -57,7 +59,7 @@ module ApplicationTests Dir.chdir("#{app_path}/app") do require "#{app_path}/config/environment" - assert_raises(NoMethodError) { [1,2,3].sample } + assert_raises(NoMethodError) { [1,2,3].forty_two } end end diff --git a/railties/test/application/middleware_test.rb b/railties/test/application/middleware_test.rb index 999f666a64..e66e81ea2c 100644 --- a/railties/test/application/middleware_test.rb +++ b/railties/test/application/middleware_test.rb @@ -83,6 +83,17 @@ module ApplicationTests assert_equal "Rack::Config", middleware.second end + test "RAILS_CACHE does not respond to middleware" do + add_to_config "config.cache_store = :memory_store" + boot! + assert_equal "Rack::Runtime", middleware.third + end + + test "RAILS_CACHE does respond to middleware" do + boot! + assert_equal "Rack::Runtime", middleware.fourth + end + test "insert middleware before" do add_to_config "config.middleware.insert_before ActionDispatch::Static, Rack::Config" boot! diff --git a/railties/test/application/runner_test.rb b/railties/test/application/runner_test.rb new file mode 100644 index 0000000000..d37b7649e2 --- /dev/null +++ b/railties/test/application/runner_test.rb @@ -0,0 +1,49 @@ +require 'isolation/abstract_unit' + +module ApplicationTests + class RunnerTest < Test::Unit::TestCase + include ActiveSupport::Testing::Isolation + + def setup + build_app + boot_rails + + # Lets create a model so we have something to play with + app_file "app/models/user.rb", <<-MODEL + class User + def self.count + 42 + end + end + MODEL + end + + def test_should_run_ruby_statement + assert_match "42", Dir.chdir(app_path) { `bundle exec rails runner "puts User.count"` } + end + + def test_should_run_file + app_file "script/count_users.rb", <<-SCRIPT + puts User.count + SCRIPT + + assert_match "42", Dir.chdir(app_path) { `bundle exec rails runner "script/count_users.rb"` } + end + + def test_should_set_dollar_0_to_file + app_file "script/dollar0.rb", <<-SCRIPT + puts $0 + SCRIPT + + assert_match "script/dollar0.rb", Dir.chdir(app_path) { `bundle exec rails runner "script/dollar0.rb"` } + end + + def test_should_set_dollar_program_name_to_file + app_file "script/program_name.rb", <<-SCRIPT + puts $PROGRAM_NAME + SCRIPT + + assert_match "script/program_name.rb", Dir.chdir(app_path) { `bundle exec rails runner "script/program_name.rb"` } + end + end +end diff --git a/railties/test/generators/actions_test.rb b/railties/test/generators/actions_test.rb index 65fbf61902..0472ca73a8 100644 --- a/railties/test/generators/actions_test.rb +++ b/railties/test/generators/actions_test.rb @@ -130,9 +130,9 @@ class ActionsTest < Rails::Generators::TestCase def test_environment_should_include_data_in_environment_initializer_block run_generator - load_paths = 'config.load_paths += %w["#{Rails.root}/app/extras"]' - action :environment, load_paths - assert_file 'config/application.rb', /#{Regexp.escape(load_paths)}/ + autoload_paths = 'config.autoload_paths += %w["#{Rails.root}/app/extras"]' + action :environment, autoload_paths + assert_file 'config/application.rb', /#{Regexp.escape(autoload_paths)}/ end def test_environment_with_block_should_include_block_contents_in_environment_initializer_block diff --git a/railties/test/generators/resource_generator_test.rb b/railties/test/generators/resource_generator_test.rb index 96fd7a0a72..55d5bd6f83 100644 --- a/railties/test/generators/resource_generator_test.rb +++ b/railties/test/generators/resource_generator_test.rb @@ -59,14 +59,6 @@ class ResourceGeneratorTest < Rails::Generators::TestCase end end - def test_singleton_resource - run_generator ["account", "--singleton"] - - assert_file "config/routes.rb" do |route| - assert_match /resource :account$/, route - end - end - def test_plural_names_are_singularized content = run_generator ["accounts".freeze] assert_file "app/models/account.rb", /class Account < ActiveRecord::Base/ diff --git a/railties/test/generators/scaffold_controller_generator_test.rb b/railties/test/generators/scaffold_controller_generator_test.rb index 8040b22fe6..d55ed22975 100644 --- a/railties/test/generators/scaffold_controller_generator_test.rb +++ b/railties/test/generators/scaffold_controller_generator_test.rb @@ -78,20 +78,6 @@ class ScaffoldControllerGeneratorTest < Rails::Generators::TestCase end end - def test_generates_singleton_controller - run_generator ["User", "name:string", "age:integer", "--singleton"] - - assert_file "app/controllers/users_controller.rb" do |content| - assert_no_match /def index/, content - end - - assert_file "test/functional/users_controller_test.rb" do |content| - assert_no_match /test "should get index"/, content - end - - assert_no_file "app/views/users/index.html.erb" - end - def test_skip_helper_if_required run_generator ["User", "name:string", "age:integer", "--no-helper"] assert_no_file "app/helpers/users_helper.rb" diff --git a/railties/test/generators/scaffold_generator_test.rb b/railties/test/generators/scaffold_generator_test.rb index e8e622fe5c..ea469cb3c8 100644 --- a/railties/test/generators/scaffold_generator_test.rb +++ b/railties/test/generators/scaffold_generator_test.rb @@ -110,4 +110,110 @@ class ScaffoldGeneratorTest < Rails::Generators::TestCase # Stylesheets (should not be removed) assert_file "public/stylesheets/scaffold.css" end + + def test_scaffold_with_namespace_on_invoke + run_generator [ "admin/role", "name:string", "description:string" ] + + # Model + assert_file "app/models/admin.rb", /module Admin/ + assert_file "app/models/admin/role.rb", /class Admin::Role < ActiveRecord::Base/ + assert_file "test/unit/admin/role_test.rb", /class Admin::RoleTest < ActiveSupport::TestCase/ + assert_file "test/fixtures/admin/roles.yml" + assert_migration "db/migrate/create_admin_roles.rb" + + # Route + assert_file "config/routes.rb" do |route| + assert_match /namespace :admin do resources :roles end$/, route + end + + # Controller + assert_file "app/controllers/admin/roles_controller.rb" do |content| + assert_match /class Admin::RolesController < ApplicationController/, content + + assert_instance_method :index, content do |m| + assert_match /@admin_roles = Admin::Role\.all/, m + end + + assert_instance_method :show, content do |m| + assert_match /@admin_role = Admin::Role\.find\(params\[:id\]\)/, m + end + + assert_instance_method :new, content do |m| + assert_match /@admin_role = Admin::Role\.new/, m + end + + assert_instance_method :edit, content do |m| + assert_match /@admin_role = Admin::Role\.find\(params\[:id\]\)/, m + end + + assert_instance_method :create, content do |m| + assert_match /@admin_role = Admin::Role\.new\(params\[:admin_role\]\)/, m + assert_match /@admin_role\.save/, m + assert_match /@admin_role\.errors/, m + end + + assert_instance_method :update, content do |m| + assert_match /@admin_role = Admin::Role\.find\(params\[:id\]\)/, m + assert_match /@admin_role\.update_attributes\(params\[:admin_role\]\)/, m + assert_match /@admin_role\.errors/, m + end + + assert_instance_method :destroy, content do |m| + assert_match /@admin_role = Admin::Role\.find\(params\[:id\]\)/, m + assert_match /@admin_role\.destroy/, m + end + end + + assert_file "test/functional/admin/roles_controller_test.rb", + /class Admin::RolesControllerTest < ActionController::TestCase/ + + # Views + %w( + index + edit + new + show + _form + ).each { |view| assert_file "app/views/admin/roles/#{view}.html.erb" } + assert_no_file "app/views/layouts/admin/roles.html.erb" + + # Helpers + assert_file "app/helpers/admin/roles_helper.rb" + assert_file "test/unit/helpers/admin/roles_helper_test.rb" + + # Stylesheets + assert_file "public/stylesheets/scaffold.css" + end + + def test_scaffold_with_namespace_on_revoke + run_generator [ "admin/role", "name:string", "description:string" ] + run_generator [ "admin/role" ], :behavior => :revoke + + # Model + assert_file "app/models/admin.rb" # ( should not be remove ) + assert_no_file "app/models/admin/role.rb" + assert_no_file "test/unit/admin/role_test.rb" + assert_no_file "test/fixtures/admin/roles.yml" + assert_no_migration "db/migrate/create_admin_roles.rb" + + # Route + assert_file "config/routes.rb" do |route| + assert_no_match /namespace :admin do resources :roles end$/, route + end + + # Controller + assert_no_file "app/controllers/admin/roles_controller.rb" + assert_no_file "test/functional/admin/roles_controller_test.rb" + + # Views + assert_no_file "app/views/admin/roles" + assert_no_file "app/views/layouts/admin/roles.html.erb" + + # Helpers + assert_no_file "app/helpers/admin/roles_helper.rb" + assert_no_file "test/unit/helpers/admin/roles_helper_test.rb" + + # Stylesheets (should not be removed) + assert_file "public/stylesheets/scaffold.css" + end end diff --git a/railties/test/initializable_test.rb b/railties/test/initializable_test.rb index 74301a5dc5..72c35879c5 100644 --- a/railties/test/initializable_test.rb +++ b/railties/test/initializable_test.rb @@ -5,10 +5,7 @@ module InitializableTests class Foo include Rails::Initializable - - class << self - attr_accessor :foo, :bar - end + attr_accessor :foo, :bar initializer :start do @foo ||= 0 @@ -158,30 +155,22 @@ module InitializableTests include ActiveSupport::Testing::Isolation test "initializers run" do - Foo.run_initializers - assert_equal 1, Foo.foo + foo = Foo.new + foo.run_initializers + assert_equal 1, foo.foo end test "initializers are inherited" do - Bar.run_initializers - assert_equal [1, 1], [Bar.foo, Bar.bar] + bar = Bar.new + bar.run_initializers + assert_equal [1, 1], [bar.foo, bar.bar] end test "initializers only get run once" do - Foo.run_initializers - Foo.run_initializers - assert_equal 1, Foo.foo - end - - test "running initializers on children does not effect the parent" do - Bar.run_initializers - assert_nil Foo.foo - assert_nil Foo.bar - end - - test "initializing with modules" do - Word.run_initializers - assert_equal "bird", $word + foo = Foo.new + foo.run_initializers + foo.run_initializers + assert_equal 1, foo.foo end test "creating initializer without a block raises an error" do @@ -198,19 +187,19 @@ module InitializableTests class BeforeAfter < ActiveSupport::TestCase test "running on parent" do $arr = [] - Parent.run_initializers + Parent.new.run_initializers assert_equal [5, 1, 2], $arr end test "running on child" do $arr = [] - Child.run_initializers + Child.new.run_initializers assert_equal [5, 3, 1, 4, 2], $arr end test "handles dependencies introduced before all initializers are loaded" do $arr = [] - Interdependent::Application.run_initializers + Interdependent::Application.new.run_initializers assert_equal [1, 2, 3, 4], $arr end end diff --git a/railties/test/log_subscriber_test.rb b/railties/test/log_subscriber_test.rb deleted file mode 100644 index a3a755ae62..0000000000 --- a/railties/test/log_subscriber_test.rb +++ /dev/null @@ -1,123 +0,0 @@ -require 'abstract_unit' -require 'rails/log_subscriber/test_helper' - -class MyLogSubscriber < Rails::LogSubscriber - attr_reader :event - - def some_event(event) - @event = event - info event.name - end - - def foo(event) - debug "debug" - info "info" - warn "warn" - end - - def bar(event) - info "#{color("cool", :red)}, #{color("isn't it?", :blue, true)}" - end - - def puke(event) - raise "puke" - end -end - -class SyncLogSubscriberTest < ActiveSupport::TestCase - include Rails::LogSubscriber::TestHelper - - def setup - super - @log_subscriber = MyLogSubscriber.new - end - - def teardown - super - Rails::LogSubscriber.log_subscribers.clear - end - - def instrument(*args, &block) - ActiveSupport::Notifications.instrument(*args, &block) - end - - def test_proxies_method_to_rails_logger - @log_subscriber.foo(nil) - assert_equal %w(debug), @logger.logged(:debug) - assert_equal %w(info), @logger.logged(:info) - assert_equal %w(warn), @logger.logged(:warn) - end - - def test_set_color_for_messages - Rails::LogSubscriber.colorize_logging = true - @log_subscriber.bar(nil) - assert_equal "\e[31mcool\e[0m, \e[1m\e[34misn't it?\e[0m", @logger.logged(:info).last - end - - def test_does_not_set_color_if_colorize_logging_is_set_to_false - @log_subscriber.bar(nil) - assert_equal "cool, isn't it?", @logger.logged(:info).last - end - - def test_event_is_sent_to_the_registered_class - Rails::LogSubscriber.add :my_log_subscriber, @log_subscriber - instrument "some_event.my_log_subscriber" - wait - assert_equal %w(some_event.my_log_subscriber), @logger.logged(:info) - end - - def test_event_is_an_active_support_notifications_event - Rails::LogSubscriber.add :my_log_subscriber, @log_subscriber - instrument "some_event.my_log_subscriber" - wait - assert_kind_of ActiveSupport::Notifications::Event, @log_subscriber.event - end - - def test_does_not_send_the_event_if_it_doesnt_match_the_class - Rails::LogSubscriber.add :my_log_subscriber, @log_subscriber - instrument "unknown_event.my_log_subscriber" - wait - # If we get here, it means that NoMethodError was not raised. - end - - def test_does_not_send_the_event_if_logger_is_nil - Rails.logger = nil - @log_subscriber.expects(:some_event).never - Rails::LogSubscriber.add :my_log_subscriber, @log_subscriber - instrument "some_event.my_log_subscriber" - wait - end - - def test_does_not_fail_with_non_namespaced_events - Rails::LogSubscriber.add :my_log_subscriber, @log_subscriber - instrument "whatever" - wait - end - - def test_flushes_loggers - Rails::LogSubscriber.add :my_log_subscriber, @log_subscriber - Rails::LogSubscriber.flush_all! - assert_equal 1, @logger.flush_count - end - - def test_flushes_the_same_logger_just_once - Rails::LogSubscriber.add :my_log_subscriber, @log_subscriber - Rails::LogSubscriber.add :another, @log_subscriber - Rails::LogSubscriber.flush_all! - wait - assert_equal 1, @logger.flush_count - end - - def test_logging_does_not_die_on_failures - Rails::LogSubscriber.add :my_log_subscriber, @log_subscriber - instrument "puke.my_log_subscriber" - instrument "some_event.my_log_subscriber" - wait - - assert_equal 1, @logger.logged(:info).size - assert_equal 'some_event.my_log_subscriber', @logger.logged(:info).last - - assert_equal 1, @logger.logged(:error).size - assert_equal 'Could not log "puke.my_log_subscriber" event. RuntimeError: puke', @logger.logged(:error).last - end -end
\ No newline at end of file diff --git a/railties/test/railties/engine_test.rb b/railties/test/railties/engine_test.rb index b3f65fd00d..3fe01e543c 100644 --- a/railties/test/railties/engine_test.rb +++ b/railties/test/railties/engine_test.rb @@ -41,7 +41,7 @@ module RailtiesTest boot_rails - initializers = Rails.application.initializers + initializers = Rails.application.initializers.tsort index = initializers.index { |i| i.name == "dummy_initializer" } selection = initializers[(index-3)..(index)].map(&:name).map(&:to_s) diff --git a/railties/test/railties/railtie_test.rb b/railties/test/railties/railtie_test.rb index 2accaca855..c74cc01dc1 100644 --- a/railties/test/railties/railtie_test.rb +++ b/railties/test/railties/railtie_test.rb @@ -48,15 +48,6 @@ module RailtiesTest assert_equal "hello", AppTemplate::Application.config.foo.greetings end - test "railtie can add log subscribers" do - begin - class Foo < Rails::Railtie ; log_subscriber(:foo, Rails::LogSubscriber.new) ; end - assert_kind_of Rails::LogSubscriber, Rails::LogSubscriber.log_subscribers[0] - ensure - Rails::LogSubscriber.log_subscribers.clear - end - end - test "railtie can add to_prepare callbacks" do $to_prepare = false class Foo < Rails::Railtie ; config.to_prepare { $to_prepare = true } ; end |