diff options
Diffstat (limited to 'railties')
49 files changed, 519 insertions, 430 deletions
diff --git a/railties/CHANGELOG.md b/railties/CHANGELOG.md index e9a69e5a0c..c6ffe30ad3 100644 --- a/railties/CHANGELOG.md +++ b/railties/CHANGELOG.md @@ -1,5 +1,60 @@ ## Rails 4.0.0 (unreleased) ## +* Fixes database.yml when creating a new rails application with '.' + Fix #8304 + + *Jeremy W. Rowe* + +* Deprecate the `eager_load_paths` configuration and alias it to `autoload_paths`. + Since the default in Rails 4.0 is to run in 'threadsafe' mode we need to eager + load all of the paths in `autoload_paths`. This may have unintended consequences + if you have added 'lib' to `autoload_paths` such as loading unneeded code or + code intended only for development and/or test environments. If this applies to + your application you should thoroughly check what is being eager loaded. + + *Andrew White* + +* Restore Rails::Engine::Railties#engines with deprecation to ensure + compatibility with gems such as Thinking Sphinx + Fix #8551 + + *Tim Raymond* + +* Specify which logs to clear when using the `rake log:clear` task. + (e.g. rake log:clear LOGS=test,staging) + + *Matt Bridges* + +* Allow a `:dirs` key in the `SourceAnnotationExtractor.enumerate` options + to explicitly set the directories to be traversed so it's easier to define + custom rake tasks. + + *Brian D. Burns* + +* Deprecate `Rails::Generators::ActiveModel#update_attributes` in favor of `#update`. + + ORMs that implement `Generators::ActiveModel#update_attributes` should change + to `#update`. Scaffold controller generators should change calls like: + + @orm_instance.update_attributes(...) + + to: + + @orm_instance.update(...) + + This goes along with the addition of `ActiveRecord::Base#update`. + + *Carlos Antonio da Silva* + +* Include `jbuilder` by default and rely on its scaffold generator to show json API. + Check https://github.com/rails/jbuilder for more info and examples. + + *DHH* + +* Scaffold now generates HTML-only controller by default. + + *DHH + Pavel Pravosud* + * The generated `README.rdoc` for new applications invites the user to document the necessary steps to get the application up and running. diff --git a/railties/lib/rails/application.rb b/railties/lib/rails/application.rb index cff75872b2..05cc49d40a 100644 --- a/railties/lib/rails/application.rb +++ b/railties/lib/rails/application.rb @@ -55,6 +55,7 @@ module Rails autoload :Bootstrap, 'rails/application/bootstrap' autoload :Configuration, 'rails/application/configuration' autoload :Finisher, 'rails/application/finisher' + autoload :Railties, 'rails/engine/railties' autoload :RoutesReloader, 'rails/application/routes_reloader' class << self @@ -232,11 +233,6 @@ module Rails config.helpers_paths end - def railties #:nodoc: - @railties ||= Rails::Railtie.subclasses.map(&:instance) + - Rails::Engine.subclasses.map(&:instance) - end - protected alias :build_middleware_stack :app diff --git a/railties/lib/rails/commands.rb b/railties/lib/rails/commands.rb index 9cb11f66c6..aacde52cfc 100644 --- a/railties/lib/rails/commands.rb +++ b/railties/lib/rails/commands.rb @@ -24,8 +24,6 @@ The most common rails commands are: In addition to those, there are: application Generate the Rails application code destroy Undo code generated with "generate" (short-cut alias: "d") - benchmarker See how fast a piece of code runs - profiler Get profile information from a piece of code plugin new Generates skeleton for developing a Rails plugin runner Run a piece of code in the application environment (short-cut alias: "r") @@ -51,11 +49,6 @@ when 'generate', 'destroy', 'plugin' require "rails/commands/#{command}" end -when 'benchmarker', 'profiler' - require APP_PATH - Rails.application.require_environment! - require "rails/commands/#{command}" - when 'console' require 'rails/commands/console' options = Rails::Console.parse_arguments(ARGV) diff --git a/railties/lib/rails/commands/benchmarker.rb b/railties/lib/rails/commands/benchmarker.rb deleted file mode 100644 index b745b45e17..0000000000 --- a/railties/lib/rails/commands/benchmarker.rb +++ /dev/null @@ -1,34 +0,0 @@ -require 'optparse' -require 'rails/test_help' -require 'rails/performance_test_help' - -ARGV.push('--benchmark') # HAX -require 'active_support/testing/performance' -ARGV.pop - -def options - options = {} - defaults = ActiveSupport::Testing::Performance::DEFAULTS - - OptionParser.new do |opt| - opt.banner = "Usage: rails benchmarker 'Ruby.code' 'Ruby.more_code' ... [OPTS]" - opt.on('-r', '--runs N', Numeric, 'Number of runs.', "Default: #{defaults[:runs]}") { |r| options[:runs] = r } - opt.on('-o', '--output PATH', String, 'Directory to use when writing the results.', "Default: #{defaults[:output]}") { |o| options[:output] = o } - opt.on('-m', '--metrics a,b,c', Array, 'Metrics to use.', "Default: #{defaults[:metrics].join(",")}") { |m| options[:metrics] = m.map(&:to_sym) } - opt.parse!(ARGV) - end - - options -end - -class BenchmarkerTest < ActionDispatch::PerformanceTest #:nodoc: - self.profile_options = options - - ARGV.each do |expression| - eval <<-RUBY - def test_#{expression.parameterize('_')} - #{expression} - end - RUBY - end -end diff --git a/railties/lib/rails/commands/profiler.rb b/railties/lib/rails/commands/profiler.rb deleted file mode 100644 index 315bcccf61..0000000000 --- a/railties/lib/rails/commands/profiler.rb +++ /dev/null @@ -1,32 +0,0 @@ -require 'optparse' -require 'rails/test_help' -require 'rails/performance_test_help' -require 'active_support/testing/performance' - -def options - options = {} - defaults = ActiveSupport::Testing::Performance::DEFAULTS - - OptionParser.new do |opt| - opt.banner = "Usage: rails profiler 'Ruby.code' 'Ruby.more_code' ... [OPTS]" - opt.on('-r', '--runs N', Numeric, 'Number of runs.', "Default: #{defaults[:runs]}") { |r| options[:runs] = r } - opt.on('-o', '--output PATH', String, 'Directory to use when writing the results.', "Default: #{defaults[:output]}") { |o| options[:output] = o } - opt.on('-m', '--metrics a,b,c', Array, 'Metrics to use.', "Default: #{defaults[:metrics].join(",")}") { |m| options[:metrics] = m.map(&:to_sym) } - opt.on('-f', '--formats x,y,z', Array, 'Formats to output to.', "Default: #{defaults[:formats].join(",")}") { |m| options[:formats] = m.map(&:to_sym) } - opt.parse!(ARGV) - end - - options -end - -class ProfilerTest < ActionDispatch::PerformanceTest #:nodoc: - self.profile_options = options - - ARGV.each do |expression| - eval <<-RUBY - def test_#{expression.parameterize('_')} - #{expression} - end - RUBY - end -end diff --git a/railties/lib/rails/engine.rb b/railties/lib/rails/engine.rb index 0ae2f16aba..8a6d1f34aa 100644 --- a/railties/lib/rails/engine.rb +++ b/railties/lib/rails/engine.rb @@ -34,9 +34,8 @@ module Rails # == Configuration # # Besides the +Railtie+ configuration which is shared across the application, in a - # <tt>Rails::Engine</tt> you can access <tt>autoload_paths</tt>, <tt>eager_load_paths</tt> - # and <tt>autoload_once_paths</tt>, which, differently from a <tt>Railtie</tt>, are scoped to - # the current engine. + # <tt>Rails::Engine</tt> you can access <tt>autoload_paths</tt> and <tt>autoload_once_paths</tt>, + # which, differently from a <tt>Railtie</tt>, are scoped to the current engine. # # class MyEngine < Rails::Engine # # Add a load path for this specific Engine @@ -456,9 +455,9 @@ module Rails end # Eager load the application by loading all ruby - # files inside eager_load paths. + # files inside autoload_paths. def eager_load! - config.eager_load_paths.each do |load_path| + config.autoload_paths.each do |load_path| matcher = /\A#{Regexp.escape(load_path)}\/(.*)\.rb\Z/ Dir.glob("#{load_path}/**/*.rb").sort.each do |file| require_dependency file.sub(matcher, '\1') @@ -466,6 +465,10 @@ module Rails end end + def railties + @railties ||= self.class::Railties.new + end + # Returns a module with all the helpers defined for the engine. def helpers @helpers ||= begin @@ -554,7 +557,6 @@ module Rails # Freeze so future modifications will fail rather than do nothing mysteriously config.autoload_paths.freeze - config.eager_load_paths.freeze config.autoload_once_paths.freeze end @@ -667,7 +669,7 @@ module Rails end def _all_autoload_paths #:nodoc: - @_all_autoload_paths ||= (config.autoload_paths + config.eager_load_paths + config.autoload_once_paths).uniq + @_all_autoload_paths ||= (config.autoload_paths + config.autoload_once_paths).uniq end def _all_load_paths #:nodoc: diff --git a/railties/lib/rails/engine/configuration.rb b/railties/lib/rails/engine/configuration.rb index 10d1821709..2b23d8be38 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 :middleware, :eager_load_paths, :autoload_once_paths, :autoload_paths + attr_writer :middleware, :autoload_once_paths, :autoload_paths def initialize(root=nil) super() @@ -39,16 +39,16 @@ module Rails @paths ||= begin paths = Rails::Paths::Root.new(@root) - paths.add "app", eager_load: true, glob: "*" + paths.add "app", autoload: true, glob: "*" paths.add "app/assets", glob: "*" - paths.add "app/controllers", eager_load: true - paths.add "app/helpers", eager_load: true - paths.add "app/models", eager_load: true - paths.add "app/mailers", eager_load: true + paths.add "app/controllers", autoload: true + paths.add "app/helpers", autoload: true + paths.add "app/models", autoload: true + paths.add "app/mailers", autoload: true paths.add "app/views" - paths.add "app/controllers/concerns", eager_load: true - paths.add "app/models/concerns", eager_load: true + paths.add "app/controllers/concerns", autoload: true + paths.add "app/models/concerns", autoload: true paths.add "lib", load_path: true paths.add "lib/assets", glob: "*" @@ -76,7 +76,13 @@ module Rails end def eager_load_paths - @eager_load_paths ||= paths.eager_load + ActiveSupport::Deprecation.warn "eager_load_paths is deprecated and all autoload_paths are now eagerly loaded." + autoload_paths + end + + def eager_load_paths=(paths) + ActiveSupport::Deprecation.warn "eager_load_paths is deprecated and all autoload_paths are now eagerly loaded." + self.autoload_paths = paths end def autoload_once_paths diff --git a/railties/lib/rails/engine/railties.rb b/railties/lib/rails/engine/railties.rb new file mode 100644 index 0000000000..1081700cd0 --- /dev/null +++ b/railties/lib/rails/engine/railties.rb @@ -0,0 +1,29 @@ +module Rails + class Engine < Railtie + class Railties + include Enumerable + attr_reader :_all + + def initialize + @_all ||= ::Rails::Railtie.subclasses.map(&:instance) + + ::Rails::Engine.subclasses.map(&:instance) + end + + def self.engines + @engines ||= ::Rails::Engine.subclasses.map(&:instance) + end + + def each(*args, &block) + _all.each(*args, &block) + end + + def -(others) + _all - others + end + + delegate :engines, to: "self.class" + end + end +end + +ActiveSupport::Deprecation.deprecate_methods(Rails::Engine::Railties, :engines) diff --git a/railties/lib/rails/generators.rb b/railties/lib/rails/generators.rb index d9a91b74d1..4b767ea0c6 100644 --- a/railties/lib/rails/generators.rb +++ b/railties/lib/rails/generators.rb @@ -50,7 +50,6 @@ module Rails javascripts: true, javascript_engine: :js, orm: false, - performance_tool: nil, resource_controller: :controller, resource_route: true, scaffold_controller: :scaffold_controller, @@ -179,7 +178,6 @@ module Rails "#{test}:model", "#{test}:scaffold", "#{test}:view", - "#{test}:performance", "#{template}:controller", "#{template}:scaffold", "#{template}:mailer", diff --git a/railties/lib/rails/generators/active_model.rb b/railties/lib/rails/generators/active_model.rb index 6183944bb0..e5373704d7 100644 --- a/railties/lib/rails/generators/active_model.rb +++ b/railties/lib/rails/generators/active_model.rb @@ -1,3 +1,5 @@ +require 'active_support/deprecation' + module Rails module Generators # ActiveModel is a class to be implemented by each ORM to allow Rails to @@ -63,6 +65,12 @@ module Rails "#{name}.update(#{params})" end + def update_attributes(*args) # :nodoc: + ActiveSupport::Deprecation.warn("Calling '@orm_instance.update_attributes' " \ + "is deprecated, please use '@orm_instance.update' instead.") + update(*args) + end + # POST create # PATCH/PUT update def errors diff --git a/railties/lib/rails/generators/rails/app/app_generator.rb b/railties/lib/rails/generators/rails/app/app_generator.rb index bf60616e2b..b2d1be9b51 100644 --- a/railties/lib/rails/generators/rails/app/app_generator.rb +++ b/railties/lib/rails/generators/rails/app/app_generator.rb @@ -38,7 +38,7 @@ module Rails end def readme - copy_file "README", "README.rdoc" + copy_file "README.rdoc", "README.rdoc" end def gemfile @@ -114,7 +114,6 @@ module Rails empty_directory_with_keep_file 'test/helpers' empty_directory_with_keep_file 'test/integration' - template 'test/performance/browsing_test.rb' template 'test/test_helper.rb' end @@ -141,7 +140,7 @@ module Rails # We need to store the RAILS_DEV_PATH in a constant, otherwise the path # can change in Ruby 1.8.7 when we FileUtils.cd. RAILS_DEV_PATH = File.expand_path("../../../../../..", File.dirname(__FILE__)) - RESERVED_NAMES = %w[application destroy benchmarker profiler plugin runner test] + RESERVED_NAMES = %w[application destroy plugin runner test] class AppGenerator < AppBase # :nodoc: add_shared_options_for "application" @@ -237,7 +236,7 @@ module Rails end def app_name - @app_name ||= defined_app_const_base? ? defined_app_name : File.basename(destination_root) + @app_name ||= (defined_app_const_base? ? defined_app_name : File.basename(destination_root)).tr(".", "_") end def defined_app_name diff --git a/railties/lib/rails/generators/rails/app/templates/Gemfile b/railties/lib/rails/generators/rails/app/templates/Gemfile index c4846b2c11..b5db3d2187 100644 --- a/railties/lib/rails/generators/rails/app/templates/Gemfile +++ b/railties/lib/rails/generators/rails/app/templates/Gemfile @@ -9,12 +9,12 @@ source 'https://rubygems.org' <%= assets_gemfile_entry %> <%= javascript_gemfile_entry -%> +# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder +gem 'jbuilder', '~> 1.0.1' + # To use ActiveModel has_secure_password # gem 'bcrypt-ruby', '~> 3.0.0' -# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder -# gem 'jbuilder' - # Use unicorn as the app server # gem 'unicorn' diff --git a/railties/lib/rails/generators/rails/app/templates/README b/railties/lib/rails/generators/rails/app/templates/README.rdoc index 8d1b0f42e5..dd4e97e22e 100644 --- a/railties/lib/rails/generators/rails/app/templates/README +++ b/railties/lib/rails/generators/rails/app/templates/README.rdoc @@ -23,6 +23,6 @@ Things you may want to cover: * ... -If you plan to generate application documentation with `rake doc:app` this file -is expected to be `README.rdoc`, otherwise please feel free to rename it and use -a different markup language. + +Please feel free to use a different markup language if you do not plan to run +<tt>rake doc:app</tt>. diff --git a/railties/lib/rails/generators/rails/app/templates/bin/bundle b/railties/lib/rails/generators/rails/app/templates/bin/bundle index e0df7f4440..1123dcf501 100644 --- a/railties/lib/rails/generators/rails/app/templates/bin/bundle +++ b/railties/lib/rails/generators/rails/app/templates/bin/bundle @@ -1,3 +1,2 @@ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__) -require 'rubygems' load Gem.bin_path('bundler', 'bundle') 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 f5d7d698a3..d149413e2e 100644 --- a/railties/lib/rails/generators/rails/app/templates/config/application.rb +++ b/railties/lib/rails/generators/rails/app/templates/config/application.rb @@ -22,6 +22,14 @@ module <%= app_const_base %> # Custom directories with classes and modules you want to be autoloadable. # config.autoload_paths += %W(#{config.root}/extras) + + # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone. + # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC. + # config.time_zone = 'Central Time (US & Canada)' + + # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded. + # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s] + # config.i18n.default_locale = :de <% if options.skip_sprockets? -%> # Disable the asset pipeline. diff --git a/railties/lib/rails/generators/rails/app/templates/config/initializers/locale.rb b/railties/lib/rails/generators/rails/app/templates/config/initializers/locale.rb deleted file mode 100644 index d89dac7c6a..0000000000 --- a/railties/lib/rails/generators/rails/app/templates/config/initializers/locale.rb +++ /dev/null @@ -1,9 +0,0 @@ -# Be sure to restart your server when you modify this file. - -# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone. -# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC. -# Rails.application.config.time_zone = 'Central Time (US & Canada)' - -# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded. -# Rails.application.config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s] -# Rails.application.config.i18n.default_locale = :de diff --git a/railties/lib/rails/generators/rails/app/templates/test/performance/browsing_test.rb b/railties/lib/rails/generators/rails/app/templates/test/performance/browsing_test.rb deleted file mode 100644 index d09ce5ad34..0000000000 --- a/railties/lib/rails/generators/rails/app/templates/test/performance/browsing_test.rb +++ /dev/null @@ -1,12 +0,0 @@ -require 'test_helper' -require 'rails/performance_test_help' - -class BrowsingTest < ActionDispatch::PerformanceTest - # Refer to the documentation for all available options - # self.profile_options = { runs: 5, metrics: [:wall_time, :memory], - # output: 'tmp/performance', formats: [:flat] } - - test "homepage" do - get '/' - end -end diff --git a/railties/lib/rails/generators/rails/performance_test/USAGE b/railties/lib/rails/generators/rails/performance_test/USAGE deleted file mode 100644 index 9dc799559c..0000000000 --- a/railties/lib/rails/generators/rails/performance_test/USAGE +++ /dev/null @@ -1,10 +0,0 @@ -Description: - Stubs out a new performance test. Pass the name of the test, either - CamelCased or under_scored, as an argument. - - This generator invokes the current performance tool, which defaults to - TestUnit. - -Example: - `rails generate performance_test GeneralStories` creates a GeneralStories - performance test in test/performance/general_stories_test.rb diff --git a/railties/lib/rails/generators/rails/performance_test/performance_test_generator.rb b/railties/lib/rails/generators/rails/performance_test/performance_test_generator.rb deleted file mode 100644 index 56cd562f3d..0000000000 --- a/railties/lib/rails/generators/rails/performance_test/performance_test_generator.rb +++ /dev/null @@ -1,7 +0,0 @@ -module Rails - module Generators - class PerformanceTestGenerator < NamedBase # :nodoc: - hook_for :performance_tool, as: :performance - end - end -end diff --git a/railties/lib/rails/generators/rails/scaffold/scaffold_generator.rb b/railties/lib/rails/generators/rails/scaffold/scaffold_generator.rb index dd636ed3cf..36589d65e2 100644 --- a/railties/lib/rails/generators/rails/scaffold/scaffold_generator.rb +++ b/railties/lib/rails/generators/rails/scaffold/scaffold_generator.rb @@ -9,13 +9,8 @@ module Rails class_option :stylesheets, type: :boolean, desc: "Generate Stylesheets" class_option :stylesheet_engine, desc: "Engine for Stylesheets" - class_option :html, type: :boolean, default: true, - desc: "Generate a scaffold with HTML output" - def handle_skip - if !options[:html] || !options[:stylesheets] - @options = @options.merge(stylesheet_engine: false) - end + @options = @options.merge(stylesheet_engine: false) unless options[:stylesheets] end hook_for :scaffold_controller, required: true 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 32fa54a362..4f36b612ae 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,17 +10,8 @@ module Rails class_option :orm, banner: "NAME", type: :string, required: true, desc: "ORM to generate the controller for" - class_option :html, type: :boolean, default: true, - desc: "Generate a scaffold with HTML output" - argument :attributes, type: :array, default: [], banner: "field:type field:type" - def handle_skip - unless options[:html] - @options = @options.merge(template_engine: false, helper: false) - end - end - 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 e11d357314..e813437d75 100644 --- a/railties/lib/rails/generators/rails/scaffold_controller/templates/controller.rb +++ b/railties/lib/rails/generators/rails/scaffold_controller/templates/controller.rb @@ -7,95 +7,47 @@ class <%= controller_class_name %>Controller < ApplicationController before_action :set_<%= singular_table_name %>, only: [:show, :edit, :update, :destroy] # GET <%= route_url %> - # GET <%= route_url %>.json def index @<%= plural_table_name %> = <%= orm_class.all(class_name) %> - - respond_to do |format| - <%- if options[:html] -%> - format.html # index.html.erb - <%- end -%> - format.json { render json: <%= "@#{plural_table_name}" %> } - end end # GET <%= route_url %>/1 - # GET <%= route_url %>/1.json def show - respond_to do |format| - <%- if options[:html] -%> - format.html # show.html.erb - <%- end -%> - format.json { render json: <%= "@#{singular_table_name}" %> } - end end - <%- if options[:html] -%> # GET <%= route_url %>/new - # GET <%= route_url %>/new.json def new @<%= singular_table_name %> = <%= orm_class.build(class_name) %> - - respond_to do |format| - format.html # new.html.erb - format.json { render json: <%= "@#{singular_table_name}" %> } - end end # GET <%= route_url %>/1/edit def edit end - <%- end -%> # POST <%= route_url %> - # POST <%= route_url %>.json def create @<%= singular_table_name %> = <%= orm_class.build(class_name, "#{singular_table_name}_params") %> - respond_to do |format| - if @<%= orm_instance.save %> - <%- if options[:html] -%> - format.html { redirect_to @<%= singular_table_name %>, notice: <%= "'#{human_name} was successfully created.'" %> } - <%- end -%> - format.json { render json: <%= "@#{singular_table_name}" %>, status: :created, location: <%= "@#{singular_table_name}" %> } - else - <%- if options[:html] -%> - format.html { render action: "new" } - <%- end -%> - format.json { render json: <%= "@#{orm_instance.errors}" %>, status: :unprocessable_entity } - end + if @<%= orm_instance.save %> + redirect_to @<%= singular_table_name %>, notice: <%= "'#{human_name} was successfully created.'" %> + else + render action: 'new' end end # PATCH/PUT <%= route_url %>/1 - # PATCH/PUT <%= route_url %>/1.json def update - respond_to do |format| - if @<%= orm_instance.update("#{singular_table_name}_params") %> - <%- if options[:html] -%> - format.html { redirect_to @<%= singular_table_name %>, notice: <%= "'#{human_name} was successfully updated.'" %> } - <%- end -%> - format.json { head :no_content } - else - <%- if options[:html] -%> - format.html { render action: "edit" } - <%- end -%> - format.json { render json: <%= "@#{orm_instance.errors}" %>, status: :unprocessable_entity } - end + if @<%= orm_instance.update("#{singular_table_name}_params") %> + redirect_to @<%= singular_table_name %>, notice: <%= "'#{human_name} was successfully updated.'" %> + else + render action: 'edit' end end # DELETE <%= route_url %>/1 - # DELETE <%= route_url %>/1.json def destroy @<%= orm_instance.destroy %> - - respond_to do |format| - <%- if options[:html] -%> - format.html { redirect_to <%= index_helper %>_url } - <%- end -%> - format.json { head :no_content } - end + redirect_to <%= index_helper %>_url end private diff --git a/railties/lib/rails/generators/test_unit/performance/performance_generator.rb b/railties/lib/rails/generators/test_unit/performance/performance_generator.rb deleted file mode 100644 index 5552edeee4..0000000000 --- a/railties/lib/rails/generators/test_unit/performance/performance_generator.rb +++ /dev/null @@ -1,13 +0,0 @@ -require 'rails/generators/test_unit' - -module TestUnit # :nodoc: - module Generators # :nodoc: - class PerformanceGenerator < Base # :nodoc: - check_class_collision suffix: "Test" - - def create_test_files - template 'performance_test.rb', File.join('test/performance', class_path, "#{file_name}_test.rb") - end - end - end -end diff --git a/railties/lib/rails/generators/test_unit/performance/templates/performance_test.rb b/railties/lib/rails/generators/test_unit/performance/templates/performance_test.rb deleted file mode 100644 index 2bcb482d68..0000000000 --- a/railties/lib/rails/generators/test_unit/performance/templates/performance_test.rb +++ /dev/null @@ -1,12 +0,0 @@ -require 'test_helper' -require 'rails/performance_test_help' - -class <%= class_name %>Test < ActionDispatch::PerformanceTest - # Refer to the documentation for all available options - # self.profile_options = { runs: 5, metrics: [:wall_time, :memory], - # output: 'tmp/performance', formats: [:flat] } - - test "homepage" do - get '/' - end -end diff --git a/railties/lib/rails/info.rb b/railties/lib/rails/info.rb index aacc1be2fc..592e74726e 100644 --- a/railties/lib/rails/info.rb +++ b/railties/lib/rails/info.rb @@ -46,7 +46,7 @@ module Rails alias inspect to_s def to_html - (table = '<table>').tap do + '<table>'.tap do |table| properties.each do |(name, value)| table << %(<tr><td class="name">#{CGI.escapeHTML(name.to_s)}</td>) formatted_value = if value.kind_of?(Array) diff --git a/railties/lib/rails/paths.rb b/railties/lib/rails/paths.rb index e52d1a8b90..80ba144441 100644 --- a/railties/lib/rails/paths.rb +++ b/railties/lib/rails/paths.rb @@ -5,13 +5,13 @@ module Rails # paths by a Hash like API. It requires you to give a physical path on initialization. # # root = Root.new "/rails" - # root.add "app/controllers", eager_load: true + # root.add "app/controllers", autoload: true # # The command above creates a new root object and add "app/controllers" as a path. # This means we can get a <tt>Rails::Paths::Path</tt> object back like below: # # path = root["app/controllers"] - # path.eager_load? # => true + # path.autoload? # => true # path.is_a?(Rails::Paths::Path) # => true # # The +Path+ object is simply an enumerable and allows you to easily add extra paths: @@ -30,7 +30,7 @@ module Rails # root["config/routes"].inspect # => ["config/routes.rb"] # # The +add+ method accepts the following options as arguments: - # eager_load, autoload, autoload_once and glob. + # autoload, autoload_once and glob. # # Finally, the +Path+ object also provides a few helpers: # @@ -55,8 +55,8 @@ module Rails add(path, with: value, glob: glob) end - def add(path, options={}) - with = Array(options[:with] || path) + def add(path, options = {}) + with = Array(options.fetch(:with, path)) @root[path] = Path.new(self, path, with, options) end @@ -85,7 +85,8 @@ module Rails end def eager_load - filter_by(:eager_load?) + ActiveSupport::Deprecation.warn "eager_load is deprecated and all autoload_paths are now eagerly loaded." + filter_by(:autoload?) end def autoload_paths @@ -124,9 +125,13 @@ module Rails @glob = options[:glob] options[:autoload_once] ? autoload_once! : skip_autoload_once! - options[:eager_load] ? eager_load! : skip_eager_load! options[:autoload] ? autoload! : skip_autoload! options[:load_path] ? load_path! : skip_load_path! + + if !options.key?(:autoload) && options.key?(:eager_load) + ActiveSupport::Deprecation.warn "the :eager_load option is deprecated and all :autoload paths are now eagerly loaded." + options[:eager_load] ? autoload! : skip_autoload! + end end def children @@ -143,22 +148,37 @@ module Rails expanded.last end - %w(autoload_once eager_load autoload load_path).each do |m| + %w(autoload_once autoload load_path).each do |m| class_eval <<-RUBY, __FILE__, __LINE__ + 1 - def #{m}! # def eager_load! - @#{m} = true # @eager_load = true + def #{m}! # def autoload! + @#{m} = true # @autoload = true end # end # - def skip_#{m}! # def skip_eager_load! - @#{m} = false # @eager_load = false + def skip_#{m}! # def skip_autoload! + @#{m} = false # @autoload = false end # end # - def #{m}? # def eager_load? - @#{m} # @eager_load + def #{m}? # def autoload? + @#{m} # @autoload end # end RUBY end + def eager_load! + ActiveSupport::Deprecation.warn "eager_load paths are deprecated and all autoload paths are now eagerly loaded." + autoload! + end + + def skip_eager_load! + ActiveSupport::Deprecation.warn "eager_load paths are deprecated and all autoload paths are now eagerly loaded." + skip_autoload! + end + + def eager_load? + ActiveSupport::Deprecation.warn "eager_load paths are deprecated and all autoload paths are now eagerly loaded." + autoload? + end + def each(&block) @paths.each(&block) end @@ -189,9 +209,9 @@ module Rails path = File.expand_path(p, @root.path) if @glob && File.directory?(path) - result.concat Dir.chdir(path) { - Dir.glob(@glob).map { |file| File.join path, file }.sort - } + Dir.chdir(path) do + result.concat(Dir.glob(@glob).map { |file| File.join path, file }.sort) + end else result << path end diff --git a/railties/lib/rails/performance_test_help.rb b/railties/lib/rails/performance_test_help.rb deleted file mode 100644 index b1285efde2..0000000000 --- a/railties/lib/rails/performance_test_help.rb +++ /dev/null @@ -1,3 +0,0 @@ -ActionController::Base.perform_caching = true -ActiveSupport::Dependencies.mechanism = :require -Rails.logger.level = ActiveSupport::Logger::INFO diff --git a/railties/lib/rails/rack/logger.rb b/railties/lib/rails/rack/logger.rb index 7be2333981..6ed6722c44 100644 --- a/railties/lib/rails/rack/logger.rb +++ b/railties/lib/rails/rack/logger.rb @@ -1,19 +1,24 @@ require 'active_support/core_ext/time/conversions' require 'active_support/core_ext/object/blank' +require 'active_support/log_subscriber' +require 'action_dispatch/http/request' +require 'rack/body_proxy' module Rails module Rack # Sets log tags, logs the request, calls the app, and flushes the logs. class Logger < ActiveSupport::LogSubscriber def initialize(app, taggers = nil) - @app, @taggers = app, taggers || [] + @app = app + @taggers = taggers || [] + @instrumenter = ActiveSupport::Notifications.instrumenter end def call(env) request = ActionDispatch::Request.new(env) - if Rails.logger.respond_to?(:tagged) - Rails.logger.tagged(compute_tags(request)) { call_app(request, env) } + if logger.respond_to?(:tagged) + logger.tagged(compute_tags(request)) { call_app(request, env) } else call_app(request, env) end @@ -23,13 +28,19 @@ module Rails def call_app(request, env) # Put some space between requests in development logs. - if Rails.env.development? - Rails.logger.debug '' - Rails.logger.debug '' + if development? + logger.debug '' + logger.debug '' end - Rails.logger.info started_request_message(request) - @app.call(env) + @instrumenter.start 'action_dispatch.request', request: request + logger.info started_request_message(request) + resp = @app.call(env) + resp[2] = ::Rack::BodyProxy.new(resp[2]) { finish(request) } + resp + rescue + finish(request) + raise ensure ActiveSupport::LogSubscriber.flush_all! end @@ -55,6 +66,20 @@ module Rails end end end + + private + + def finish(request) + @instrumenter.finish 'action_dispatch.request', request: request + end + + def development? + Rails.env.development? + end + + def logger + Rails.logger + end end end end diff --git a/railties/lib/rails/source_annotation_extractor.rb b/railties/lib/rails/source_annotation_extractor.rb index 971fbf627b..2cbb0a435c 100644 --- a/railties/lib/rails/source_annotation_extractor.rb +++ b/railties/lib/rails/source_annotation_extractor.rb @@ -32,15 +32,24 @@ class SourceAnnotationExtractor end # Prints all annotations with tag +tag+ under the root directories +app+, - # +config+, +lib+, and +test+ (recursively). Filenames with extension - # +.builder+, +.rb+, +.erb+, +.haml+, +.slim+, +.css+, +.scss+, +.js+, - # +.coffee+, and +.rake+ are taken into account. The +options+ hash is - # passed to each annotation's +to_s+. + # +config+, +db+, +lib+, and +test+ (recursively). + # + # Additional directories may be added using a comma-delimited list set using + # <tt>ENV['SOURCE_ANNOTATION_DIRECTORIES']</tt>. + # + # Directories may also be explicitly set using the <tt>:dirs</tt> key in +options+. + # + # SourceAnnotationExtractor.enumerate 'TODO|FIXME', dirs: %w(app lib), tag: true + # + # If +options+ has a <tt>:tag</tt> flag, it will be passed to each annotation's +to_s+. + # + # See <tt>#find_in</tt> for a list of file extensions that will be taken into account. # # This class method is the single entry point for the rake tasks. def self.enumerate(tag, options={}) extractor = new(tag) - extractor.display(extractor.find, options) + dirs = options.delete(:dirs) || Annotation.directories + extractor.display(extractor.find(dirs), options) end attr_reader :tag @@ -51,7 +60,7 @@ class SourceAnnotationExtractor # Returns a hash that maps filenames under +dirs+ (recursively) to arrays # with their annotations. - def find(dirs = Annotation.directories) + def find(dirs) dirs.inject({}) { |h, dir| h.update(find_in(dir)) } end @@ -68,16 +77,22 @@ class SourceAnnotationExtractor if File.directory?(item) results.update(find_in(item)) - elsif item =~ /\.(builder|rb|coffee|rake)$/ - results.update(extract_annotations_from(item, /#\s*(#{tag}):?\s*(.*)$/)) - elsif item =~ /\.(css|scss|js)$/ - results.update(extract_annotations_from(item, /\/\/\s*(#{tag}):?\s*(.*)$/)) - elsif item =~ /\.erb$/ - results.update(extract_annotations_from(item, /<%\s*#\s*(#{tag}):?\s*(.*?)\s*%>/)) - elsif item =~ /\.haml$/ - results.update(extract_annotations_from(item, /-\s*#\s*(#{tag}):?\s*(.*)$/)) - elsif item =~ /\.slim$/ - results.update(extract_annotations_from(item, /\/\s*\s*(#{tag}):?\s*(.*)$/)) + else + pattern = + case item + when /\.(builder|rb|coffee|rake)$/ + /#\s*(#{tag}):?\s*(.*)$/ + when /\.(css|scss|js)$/ + /\/\/\s*(#{tag}):?\s*(.*)$/ + when /\.erb$/ + /<%\s*#\s*(#{tag}):?\s*(.*?)\s*%>/ + when /\.haml$/ + /-\s*#\s*(#{tag}):?\s*(.*)$/ + when /\.slim$/ + /\/\s*\s*(#{tag}):?\s*(.*)$/ + else nil + end + results.update(extract_annotations_from(item, pattern)) if pattern end end diff --git a/railties/lib/rails/tasks/log.rake b/railties/lib/rails/tasks/log.rake index 6e1334692e..6c3f02eb0c 100644 --- a/railties/lib/rails/tasks/log.rake +++ b/railties/lib/rails/tasks/log.rake @@ -1,9 +1,23 @@ namespace :log do - desc "Truncates all *.log files in log/ to zero bytes" + desc "Truncates all *.log files in log/ to zero bytes (specify which logs with LOGS=test,development)" task :clear do - FileList["log/*.log"].each do |log_file| - f = File.open(log_file, "w") - f.close + log_files.each do |file| + clear_log_file(file) end end + + def log_files + if ENV['LOGS'] + ENV['LOGS'].split(',') + .map { |file| "log/#{file.strip}.log" } + .select { |file| File.exists?(file) } + else + FileList["log/*.log"] + end + end + + def clear_log_file(file) + f = File.open(file, "w") + f.close + end end diff --git a/railties/lib/rails/test_unit/railtie.rb b/railties/lib/rails/test_unit/railtie.rb index ed89ce4128..f52c4c44b7 100644 --- a/railties/lib/rails/test_unit/railtie.rb +++ b/railties/lib/rails/test_unit/railtie.rb @@ -5,7 +5,6 @@ module Rails fixture_replacement: nil c.integration_tool :test_unit - c.performance_tool :test_unit end rake_tasks do diff --git a/railties/lib/rails/test_unit/testing.rake b/railties/lib/rails/test_unit/testing.rake index 9ad3a4e6d6..f0d46fd959 100644 --- a/railties/lib/rails/test_unit/testing.rake +++ b/railties/lib/rails/test_unit/testing.rake @@ -45,7 +45,7 @@ end task default: :test -desc 'Runs test:units, test:functionals, test:integration together (also available: test:benchmark, test:profile)' +desc 'Runs test:units, test:functionals, test:integration together' task :test do Rake::Task[ENV['TEST'] ? 'test:single' : 'test:run'].invoke end @@ -146,15 +146,4 @@ namespace :test do t.libs << "test" t.pattern = 'test/integration/**/*_test.rb' end - - Rails::SubTestTask.new(benchmark: 'test:prepare') do |t| - t.libs << 'test' - t.pattern = 'test/performance/**/*_test.rb' - t.options = '-- --benchmark' - end - - Rails::SubTestTask.new(profile: 'test:prepare') do |t| - t.libs << 'test' - t.pattern = 'test/performance/**/*_test.rb' - end end diff --git a/railties/railties.gemspec b/railties/railties.gemspec index e39430560f..a55bf012da 100644 --- a/railties/railties.gemspec +++ b/railties/railties.gemspec @@ -27,6 +27,6 @@ Gem::Specification.new do |s| s.add_dependency 'actionpack', version s.add_dependency 'rake', '>= 0.8.7' - s.add_dependency 'thor', '>= 0.15.4', '< 2.0' + s.add_dependency 'thor', '>= 0.17.0', '< 2.0' s.add_dependency 'rdoc', '~> 3.4' end diff --git a/railties/test/application/configuration_test.rb b/railties/test/application/configuration_test.rb index f9108ed7b9..7b45623f6c 100644 --- a/railties/test/application/configuration_test.rb +++ b/railties/test/application/configuration_test.rb @@ -417,17 +417,7 @@ module ApplicationTests require "#{app_path}/config/environment" - assert_equal Time.find_zone!("Wellington"), Time.zone_default - end - - test "timezone can be set on initializers" do - app_file "config/initializers/locale.rb", <<-RUBY - Rails.application.config.time_zone = "Central Time (US & Canada)" - RUBY - - require "#{app_path}/config/environment" - - assert_equal Time.find_zone!("Central Time (US & Canada)"), Time.zone_default + assert_equal "Wellington", Rails.application.config.time_zone end test "raises when an invalid timezone is defined in the config" do @@ -577,6 +567,54 @@ module ApplicationTests assert_equal 'permitted', last_response.body end + test "config.action_controller.action_on_unpermitted_parameters = :raise" do + app_file 'app/controllers/posts_controller.rb', <<-RUBY + class PostsController < ActionController::Base + def create + render text: params.require(:post).permit(:name) + end + end + RUBY + + add_to_config <<-RUBY + routes.prepend do + resources :posts + end + config.action_controller.action_on_unpermitted_parameters = :raise + RUBY + + require "#{app_path}/config/environment" + + assert_equal :raise, ActionController::Parameters.action_on_unpermitted_parameters + + post "/posts", {post: {"title" =>"zomg"}} + assert_match "We're sorry, but something went wrong", last_response.body + end + + test "config.action_controller.action_on_unpermitted_parameters is :log by default on development" do + ENV["RAILS_ENV"] = "development" + + require "#{app_path}/config/environment" + + assert_equal :log, ActionController::Parameters.action_on_unpermitted_parameters + end + + test "config.action_controller.action_on_unpermitted_parameters is :log by defaul on test" do + ENV["RAILS_ENV"] = "test" + + require "#{app_path}/config/environment" + + assert_equal :log, ActionController::Parameters.action_on_unpermitted_parameters + end + + test "config.action_controller.action_on_unpermitted_parameters is false by default on production" do + ENV["RAILS_ENV"] = "production" + + require "#{app_path}/config/environment" + + assert_equal false, ActionController::Parameters.action_on_unpermitted_parameters + end + test "config.action_dispatch.ignore_accept_header" do make_basic_app do |app| app.config.action_dispatch.ignore_accept_header = true diff --git a/railties/test/application/initializers/load_path_test.rb b/railties/test/application/initializers/load_path_test.rb index 31811e7f92..595f58bd91 100644 --- a/railties/test/application/initializers/load_path_test.rb +++ b/railties/test/application/initializers/load_path_test.rb @@ -64,7 +64,7 @@ module ApplicationTests add_to_config <<-RUBY config.root = "#{app_path}" - config.eager_load_paths << "#{app_path}/lib" + config.autoload_paths << "#{app_path}/lib" RUBY require "#{app_path}/config/environment" diff --git a/railties/test/application/middleware/cache_test.rb b/railties/test/application/middleware/cache_test.rb index c99666d7e4..b8e0c9be60 100644 --- a/railties/test/application/middleware/cache_test.rb +++ b/railties/test/application/middleware/cache_test.rb @@ -81,8 +81,8 @@ module ApplicationTests add_to_config "config.action_dispatch.rack_cache = true" get "/expires/expires_header" - assert_equal "miss, store", last_response.headers["X-Rack-Cache"] - assert_equal "max-age=10, public", last_response.headers["Cache-Control"] + assert_equal "miss, ignore, store", last_response.headers["X-Rack-Cache"] + assert_equal "max-age=10, public", last_response.headers["Cache-Control"] body = last_response.body @@ -115,8 +115,8 @@ module ApplicationTests add_to_config "config.action_dispatch.rack_cache = true" get "/expires/expires_etag" - assert_equal "miss, store", last_response.headers["X-Rack-Cache"] - assert_equal "public", last_response.headers["Cache-Control"] + assert_equal "miss, ignore, store", last_response.headers["X-Rack-Cache"] + assert_equal "public", last_response.headers["Cache-Control"] body = last_response.body etag = last_response.headers["ETag"] @@ -149,8 +149,8 @@ module ApplicationTests add_to_config "config.action_dispatch.rack_cache = true" get "/expires/expires_last_modified" - assert_equal "miss, store", last_response.headers["X-Rack-Cache"] - assert_equal "public", last_response.headers["Cache-Control"] + assert_equal "miss, ignore, store", last_response.headers["X-Rack-Cache"] + assert_equal "public", last_response.headers["Cache-Control"] body = last_response.body last = last_response.headers["Last-Modified"] diff --git a/railties/test/application/middleware/static_test.rb b/railties/test/application/middleware/static_test.rb new file mode 100644 index 0000000000..0a793f8f60 --- /dev/null +++ b/railties/test/application/middleware/static_test.rb @@ -0,0 +1,31 @@ +# encoding: utf-8 +require 'isolation/abstract_unit' +require 'rack/test' + +module ApplicationTests + class MiddlewareStaticTest < ActiveSupport::TestCase + include ActiveSupport::Testing::Isolation + include Rack::Test::Methods + + def setup + build_app + FileUtils.rm_rf "#{app_path}/config/environments" + end + + def teardown + teardown_app + end + + # Regression test to #8907 + # See https://github.com/rails/rails/commit/9cc82b77196d21a5c7021f6dca59ab9b2b158a45#commitcomment-2416514 + test "doesn't set Cache-Control header when it is nil" do + app_file "public/foo.html", 'static' + + require "#{app_path}/config/environment" + + get 'foo' + + assert_not last_response.headers.has_key?('Cache-Control'), "Cache-Control should not be set" + end + end +end diff --git a/railties/test/application/paths_test.rb b/railties/test/application/paths_test.rb index 4029984ce9..f6a77a0d17 100644 --- a/railties/test/application/paths_test.rb +++ b/railties/test/application/paths_test.rb @@ -54,11 +54,11 @@ module ApplicationTests assert_equal root("app", "controllers"), @paths["app/controllers"].expanded.first end - test "booting up Rails yields a list of paths that are eager" do - eager_load = @paths.eager_load - assert eager_load.include?(root("app/controllers")) - assert eager_load.include?(root("app/helpers")) - assert eager_load.include?(root("app/models")) + test "booting up Rails yields a list of paths that will be eager loaded" do + autoload_paths = @paths.autoload_paths + assert autoload_paths.include?(root("app/controllers")) + assert autoload_paths.include?(root("app/helpers")) + assert autoload_paths.include?(root("app/models")) end test "environments has a glob equal to the current environment" do diff --git a/railties/test/application/rake/notes_test.rb b/railties/test/application/rake/notes_test.rb index 5893d58925..3508f4225a 100644 --- a/railties/test/application/rake/notes_test.rb +++ b/railties/test/application/rake/notes_test.rb @@ -130,6 +130,45 @@ module ApplicationTests end end + test 'custom rake task finds specific notes in specific directories' do + app_file "app/controllers/some_controller.rb", "# TODO: note in app directory" + app_file "lib/some_file.rb", "# OPTIMIZE: note in lib directory\n" << "# FIXME: note in lib directory" + app_file "test/some_test.rb", 1000.times.map { "" }.join("\n") << "# TODO: note in test directory" + + app_file "lib/tasks/notes_custom.rake", <<-EOS + require 'rails/source_annotation_extractor' + task :notes_custom do + tags = 'TODO|FIXME' + opts = { dirs: %w(lib test), tag: true } + SourceAnnotationExtractor.enumerate(tags, opts) + end + EOS + + boot_rails + + require 'rake' + require 'rdoc/task' + require 'rake/testtask' + + Rails.application.load_tasks + + Dir.chdir(app_path) do + output = `bundle exec rake notes_custom` + lines = output.scan(/\[([0-9\s]+)\]/).flatten + + assert_match(/\[FIXME\] note in lib directory/, output) + assert_match(/\[TODO\] note in test directory/, output) + assert_no_match(/OPTIMIZE/, output) + assert_no_match(/note in app directory/, output) + + assert_equal 2, lines.size + + lines.each do |line_number| + assert_equal 4, line_number.size + end + end + end + private def boot_rails super diff --git a/railties/test/application/runner_test.rb b/railties/test/application/runner_test.rb index f65b5e2f2d..6595c40f8b 100644 --- a/railties/test/application/runner_test.rb +++ b/railties/test/application/runner_test.rb @@ -37,27 +37,27 @@ module ApplicationTests end def test_should_run_file - app_file "script/count_users.rb", <<-SCRIPT + app_file "bin/count_users.rb", <<-SCRIPT puts User.count SCRIPT - assert_match "42", Dir.chdir(app_path) { `bundle exec rails runner "script/count_users.rb"` } + assert_match "42", Dir.chdir(app_path) { `bundle exec rails runner "bin/count_users.rb"` } end def test_should_set_dollar_0_to_file - app_file "script/dollar0.rb", <<-SCRIPT + app_file "bin/dollar0.rb", <<-SCRIPT puts $0 SCRIPT - assert_match "script/dollar0.rb", Dir.chdir(app_path) { `bundle exec rails runner "script/dollar0.rb"` } + assert_match "bin/dollar0.rb", Dir.chdir(app_path) { `bundle exec rails runner "bin/dollar0.rb"` } end def test_should_set_dollar_program_name_to_file - app_file "script/program_name.rb", <<-SCRIPT + app_file "bin/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"` } + assert_match "bin/program_name.rb", Dir.chdir(app_path) { `bundle exec rails runner "bin/program_name.rb"` } end def test_with_hook diff --git a/railties/test/application/test_test.rb b/railties/test/application/test_test.rb index f6bcaa466a..c7ad2fba8f 100644 --- a/railties/test/application/test_test.rb +++ b/railties/test/application/test_test.rb @@ -52,31 +52,6 @@ module ApplicationTests run_test_file 'integration/posts_test.rb' end - test "performance test" do - controller 'posts', <<-RUBY - class PostsController < ActionController::Base - end - RUBY - - app_file 'app/views/posts/index.html.erb', <<-HTML - Posts#index - HTML - - app_file 'test/performance/posts_test.rb', <<-RUBY - require 'test_helper' - require 'rails/performance_test_help' - - class PostsTest < ActionDispatch::PerformanceTest - def test_index - get '/posts' - assert_response :success - end - end - RUBY - - run_test_file 'performance/posts_test.rb' - end - private def run_test_file(name) result = ruby '-Itest', "#{app_path}/test/#{name}" diff --git a/railties/test/generators/app_generator_test.rb b/railties/test/generators/app_generator_test.rb index 5d5be689e6..b2deeb011d 100644 --- a/railties/test/generators/app_generator_test.rb +++ b/railties/test/generators/app_generator_test.rb @@ -34,7 +34,6 @@ DEFAULT_APP_FILES = %w( test/helpers test/mailers test/integration - test/performance vendor vendor/assets tmp/cache @@ -165,6 +164,11 @@ class AppGeneratorTest < Rails::Generators::TestCase end end + def test_config_database_app_name_with_period + run_generator [File.join(destination_root, "common.usage.com"), "-d", "postgresql"] + assert_file "common.usage.com/config/database.yml", /common_usage_com/ + end + def test_config_postgresql_database run_generator([destination_root, "-d", "postgresql"]) assert_file "config/database.yml", /postgresql/ @@ -218,7 +222,6 @@ class AppGeneratorTest < Rails::Generators::TestCase assert_file "test/test_helper.rb" do |helper_content| assert_no_match(/fixtures :all/, helper_content) end - assert_file "test/performance/browsing_test.rb" end def test_generator_if_skip_sprockets_is_given @@ -241,7 +244,6 @@ class AppGeneratorTest < Rails::Generators::TestCase assert_no_match(/config\.assets\.css_compressor = :sass/, content) assert_no_match(/config\.assets\.version = '1\.0'/, content) end - assert_file "test/performance/browsing_test.rb" end def test_inclusion_of_javascript_runtime diff --git a/railties/test/generators/performance_test_generator_test.rb b/railties/test/generators/performance_test_generator_test.rb deleted file mode 100644 index 37f9857193..0000000000 --- a/railties/test/generators/performance_test_generator_test.rb +++ /dev/null @@ -1,12 +0,0 @@ -require 'generators/generators_test_helper' -require 'rails/generators/rails/performance_test/performance_test_generator' - -class PerformanceTestGeneratorTest < Rails::Generators::TestCase - include GeneratorsTestHelper - arguments %w(performance) - - def test_performance_test_skeleton_is_created - run_generator - assert_file "test/performance/performance_test.rb", /class PerformanceTest < ActionDispatch::PerformanceTest/ - end -end diff --git a/railties/test/generators/scaffold_controller_generator_test.rb b/railties/test/generators/scaffold_controller_generator_test.rb index ab00586a64..c34ce285e3 100644 --- a/railties/test/generators/scaffold_controller_generator_test.rb +++ b/railties/test/generators/scaffold_controller_generator_test.rb @@ -31,12 +31,10 @@ class ScaffoldControllerGeneratorTest < Rails::Generators::TestCase assert_instance_method :create, content do |m| assert_match(/@user = User\.new\(user_params\)/, m) assert_match(/@user\.save/, m) - assert_match(/@user\.errors/, m) end assert_instance_method :update, content do |m| assert_match(/@user\.update\(user_params\)/, m) - assert_match(/@user\.errors/, m) end assert_instance_method :destroy, content do |m| @@ -127,18 +125,6 @@ class ScaffoldControllerGeneratorTest < Rails::Generators::TestCase assert_no_file "app/views/layouts/users.html.erb" end - def test_skip_html_if_required - run_generator [ "User", "name:string", "age:integer", "--no-html" ] - assert_no_file "app/helpers/users_helper.rb" - assert_no_file "app/views/users" - - assert_file "app/controllers/users_controller.rb" do |content| - assert_no_match(/format\.html/, content) - assert_no_match(/def edit/, content) - assert_no_match(/def new/, content) - end - end - def test_default_orm_is_used run_generator ["User", "--orm=unknown"] @@ -176,7 +162,7 @@ class ScaffoldControllerGeneratorTest < Rails::Generators::TestCase def test_new_hash_style run_generator assert_file "app/controllers/users_controller.rb" do |content| - assert_match(/\{ render action: "new" \}/, content) + assert_match(/render action: 'new'/, content) end end end diff --git a/railties/test/generators/scaffold_generator_test.rb b/railties/test/generators/scaffold_generator_test.rb index 431b23b014..357f472a3f 100644 --- a/railties/test/generators/scaffold_generator_test.rb +++ b/railties/test/generators/scaffold_generator_test.rb @@ -41,12 +41,10 @@ class ScaffoldGeneratorTest < Rails::Generators::TestCase assert_instance_method :create, content do |m| assert_match(/@product_line = ProductLine\.new\(product_line_params\)/, m) assert_match(/@product_line\.save/, m) - assert_match(/@product_line\.errors/, m) end assert_instance_method :update, content do |m| assert_match(/@product_line\.update\(product_line_params\)/, m) - assert_match(/@product_line\.errors/, m) end assert_instance_method :destroy, content do |m| @@ -158,12 +156,10 @@ class ScaffoldGeneratorTest < Rails::Generators::TestCase assert_instance_method :create, content do |m| assert_match(/@admin_role = Admin::Role\.new\(admin_role_params\)/, 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\.update\(admin_role_params\)/, m) - assert_match(/@admin_role\.errors/, m) end assert_instance_method :destroy, content do |m| @@ -257,11 +253,6 @@ class ScaffoldGeneratorTest < Rails::Generators::TestCase assert_no_file "app/assets/stylesheets/posts.css" end - def test_scaffold_generator_no_html - run_generator [ "posts", "--no-html" ] - assert_no_file "app/assets/stylesheets/scaffold.css" - end - def test_scaffold_generator_no_javascripts run_generator [ "posts", "--no-javascripts" ] assert_file "app/assets/stylesheets/scaffold.css" diff --git a/railties/test/paths_test.rb b/railties/test/paths_test.rb index 12f18b9dbf..6f860469fd 100644 --- a/railties/test/paths_test.rb +++ b/railties/test/paths_test.rb @@ -135,56 +135,48 @@ class PathsTest < ActiveSupport::TestCase assert_equal 2, @root.autoload_once.size end - test "it is possible to mark a path as eager loaded" do + test "marking a path as eager loaded is deprecated" do @root["app"] = "/app" - @root["app"].eager_load! - assert @root["app"].eager_load? - assert @root.eager_load.include?(@root["app"].to_a.first) + assert_deprecated{ @root["app"].eager_load! } + assert_deprecated{ assert @root["app"].eager_load? } + assert_deprecated{ assert @root.eager_load.include?(@root["app"].to_a.first) } end - test "it is possible to skip a path from eager loading" do + test "skipping a path from eager loading is deprecated" do @root["app"] = "/app" - @root["app"].eager_load! - assert @root["app"].eager_load? + assert_deprecated{ @root["app"].eager_load! } + assert_deprecated{ assert @root["app"].eager_load? } - @root["app"].skip_eager_load! - assert !@root["app"].eager_load? - assert !@root.eager_load.include?(@root["app"].to_a.first) + assert_deprecated{ @root["app"].skip_eager_load! } + assert_deprecated{ assert !@root["app"].eager_load? } + assert_deprecated{ assert !@root.eager_load.include?(@root["app"].to_a.first) } end - test "it is possible to add a path without assignment and mark it as eager" do - @root.add "app", with: "/app", eager_load: true - assert @root["app"].eager_load? - assert @root.eager_load.include?("/app") + test "adding a path with eager_load option is deprecated" do + assert_deprecated{ @root.add "app", with: "/app", eager_load: true } + assert_deprecated{ assert @root["app"].eager_load? } + assert_deprecated{ assert @root.eager_load.include?("/app") } end - test "it is possible to add multiple paths without assignment and mark them as eager" do - @root.add "app", with: ["/app", "/app2"], eager_load: true - assert @root["app"].eager_load? - assert @root.eager_load.include?("/app") - assert @root.eager_load.include?("/app2") - end - - test "it is possible to create a path without assignment and mark it both as eager and load once" do - @root.add "app", with: "/app", eager_load: true, autoload_once: true - assert @root["app"].eager_load? - assert @root["app"].autoload_once? - assert @root.eager_load.include?("/app") - assert @root.autoload_once.include?("/app") + test "adding multiple paths with eager_load option is deprecated" do + assert_deprecated{ @root.add "app", with: ["/app", "/app2"], eager_load: true } + assert_deprecated{ assert @root["app"].eager_load? } + assert_deprecated{ assert @root.eager_load.include?("/app") } + assert_deprecated{ assert @root.eager_load.include?("/app2") } end test "making a path eager more than once only includes it once in @root.eager_paths" do @root["app"] = "/app" - @root["app"].eager_load! - @root["app"].eager_load! - assert_equal 1, @root.eager_load.select {|p| p == @root["app"].expanded.first }.size + assert_deprecated{ @root["app"].eager_load! } + assert_deprecated{ @root["app"].eager_load! } + assert_deprecated{ assert_equal 1, @root.eager_load.select {|p| p == @root["app"].expanded.first }.size } end test "paths added to a eager_load path should be added to the eager_load collection" do @root["app"] = "/app" - @root["app"].eager_load! + assert_deprecated{ @root["app"].eager_load! } @root["app"] << "/app2" - assert_equal 2, @root.eager_load.size + assert_deprecated{ assert_equal 2, @root.eager_load.size } end test "it should be possible to add a path's default glob" do diff --git a/railties/test/rack_logger_test.rb b/railties/test/rack_logger_test.rb new file mode 100644 index 0000000000..3a9392fd26 --- /dev/null +++ b/railties/test/rack_logger_test.rb @@ -0,0 +1,71 @@ +require 'active_support/testing/autorun' +require 'active_support/test_case' +require 'rails/rack/logger' +require 'logger' + +module Rails + module Rack + class LoggerTest < ActiveSupport::TestCase + class TestLogger < Rails::Rack::Logger + NULL = ::Logger.new File::NULL + + attr_reader :logger + + def initialize(logger = NULL, taggers = nil, &block) + super(->(_) { block.call; [200, {}, []] }, taggers) + @logger = logger + end + + def development?; false; end + end + + class Subscriber < Struct.new(:starts, :finishes) + def initialize(starts = [], finishes = []) + super + end + + def start(name, id, payload) + starts << [name, id, payload] + end + + def finish(name, id, payload) + finishes << [name, id, payload] + end + end + + attr_reader :subscriber, :notifier + + def setup + @subscriber = Subscriber.new + @notifier = ActiveSupport::Notifications.notifier + notifier.subscribe 'action_dispatch.request', subscriber + end + + def teardown + notifier.unsubscribe subscriber + end + + def test_notification + logger = TestLogger.new { } + + assert_difference('subscriber.starts.length') do + assert_difference('subscriber.finishes.length') do + logger.call('REQUEST_METHOD' => 'GET').last.close + end + end + end + + def test_notification_on_raise + logger = TestLogger.new { raise } + + assert_difference('subscriber.starts.length') do + assert_difference('subscriber.finishes.length') do + assert_raises(RuntimeError) do + logger.call 'REQUEST_METHOD' => 'GET' + end + end + end + end + end + end +end diff --git a/railties/test/railties/engine_test.rb b/railties/test/railties/engine_test.rb index a4a75fe459..37d0be107c 100644 --- a/railties/test/railties/engine_test.rb +++ b/railties/test/railties/engine_test.rb @@ -1241,6 +1241,12 @@ YAML assert_equal '/foo/bukkits/bukkit', last_response.body end + test "engines method is properly deprecated" do + boot_rails + + assert_deprecated { app.railties.engines } + end + private def app Rails.application diff --git a/railties/test/railties/mounted_engine_test.rb b/railties/test/railties/mounted_engine_test.rb index a1c52f01dc..80559a6e36 100644 --- a/railties/test/railties/mounted_engine_test.rb +++ b/railties/test/railties/mounted_engine_test.rb @@ -89,6 +89,7 @@ module ApplicationTests get '/generate_application_route', to: 'posts#generate_application_route' get '/application_route_in_view', to: 'posts#application_route_in_view' get '/engine_polymorphic_path', to: 'posts#engine_polymorphic_path' + get '/engine_asset_path', to: 'posts#engine_asset_path' end RUBY @@ -113,6 +114,10 @@ module ApplicationTests def engine_polymorphic_path render text: polymorphic_path(Post.new) end + + def engine_asset_path + render inline: "<%= asset_path 'images/foo.png' %>" + end end end RUBY @@ -211,6 +216,10 @@ module ApplicationTests # and in an application get "/application_polymorphic_path" assert_equal "/posts/44", last_response.body + + # test that asset path will not get script_name when generated in the engine + get "/someone/blog/engine_asset_path" + assert_equal "/images/foo.png", last_response.body end test "route path for controller action when engine is mounted at root" do |