From fc61bca31d899f359671d4b58bceb8b9d6555aa7 Mon Sep 17 00:00:00 2001 From: Robin Dupret Date: Tue, 25 Feb 2014 14:57:23 +0100 Subject: Avoid namespacing routes inside engines Since #11544, invoking the controller generator, any generated route is namespaced according to the class_path method. Since a mountable plugin is namespaced, creating a controller inside would generate a namespaced route based on the engine's name. The controller generator now relies on regular_class_path which does not contain the class hierarchy but the given path. Fixes #14079. --- railties/CHANGELOG.md | 9 +++++++++ .../generators/rails/controller/controller_generator.rb | 4 ++-- railties/test/generators/plugin_generator_test.rb | 12 ++++++++++++ 3 files changed, 23 insertions(+), 2 deletions(-) (limited to 'railties') diff --git a/railties/CHANGELOG.md b/railties/CHANGELOG.md index 18f2546c73..9dce38fc93 100644 --- a/railties/CHANGELOG.md +++ b/railties/CHANGELOG.md @@ -1 +1,10 @@ +* Avoid namespacing routes inside engines. + + Mountable engines are namespaced by default so the generated routes + were too while they should not. + + Fixes #14079. + + *Yves Senn*, *Carlos Antonio da Silva*, *Robin Dupret* + Please check [4-1-stable](https://github.com/rails/rails/blob/4-1-stable/railties/CHANGELOG.md) for previous changes. diff --git a/railties/lib/rails/generators/rails/controller/controller_generator.rb b/railties/lib/rails/generators/rails/controller/controller_generator.rb index 33a0d81bf6..7588a558e7 100644 --- a/railties/lib/rails/generators/rails/controller/controller_generator.rb +++ b/railties/lib/rails/generators/rails/controller/controller_generator.rb @@ -27,11 +27,11 @@ module Rails # end # end def generate_routing_code(action) - depth = class_path.length + depth = regular_class_path.length # Create 'namespace' ladder # namespace :foo do # namespace :bar do - namespace_ladder = class_path.each_with_index.map do |ns, i| + namespace_ladder = regular_class_path.each_with_index.map do |ns, i| indent("namespace :#{ns} do\n", i * 2) end.join diff --git a/railties/test/generators/plugin_generator_test.rb b/railties/test/generators/plugin_generator_test.rb index 932cd75bcb..b2fc3a2a4f 100644 --- a/railties/test/generators/plugin_generator_test.rb +++ b/railties/test/generators/plugin_generator_test.rb @@ -355,6 +355,18 @@ class PluginGeneratorTest < Rails::Generators::TestCase FileUtils.rm gemfile_path end + def test_generating_controller_inside_mountable_engine + run_generator [destination_root, "--mountable"] + + capture(:stdout) do + `#{destination_root}/bin/rails g controller admin/dashboard foo` + end + + assert_file "config/routes.rb" do |contents| + assert_match(/namespace :admin/, contents) + assert_no_match(/namespace :bukkit/, contents) + end + end protected def action(*args, &block) -- cgit v1.2.3 From 3624ff720cf98293034d7d5fe87c08f5520099d6 Mon Sep 17 00:00:00 2001 From: Allen Fair Date: Thu, 20 Feb 2014 17:39:45 -0500 Subject: Fix for external generators extend Erb::Generators HAML and probably other generators extend this class and invoke filename_with_extensions with the old signature (without format). This makes the second argument optional and defaults it to the #format method which could be overridden as well. Closes #14132. --- railties/lib/rails/generators/erb.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'railties') diff --git a/railties/lib/rails/generators/erb.rb b/railties/lib/rails/generators/erb.rb index cfd77097d5..33f8a9da6f 100644 --- a/railties/lib/rails/generators/erb.rb +++ b/railties/lib/rails/generators/erb.rb @@ -17,7 +17,7 @@ module Erb # :nodoc: :erb end - def filename_with_extensions(name, format) + def filename_with_extensions(name, format = self.format) [name, format, handler].compact.join(".") end end -- cgit v1.2.3 From ab1bd7ccf1cc38d271747dd95433a93b28a6e302 Mon Sep 17 00:00:00 2001 From: Carlos Antonio da Silva Date: Tue, 4 Mar 2014 09:42:15 -0300 Subject: Always return an array from formats so there is no need to wrap it when calling --- railties/lib/rails/generators/erb.rb | 2 +- railties/lib/rails/generators/erb/controller/controller_generator.rb | 2 +- railties/lib/rails/generators/erb/scaffold/scaffold_generator.rb | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) (limited to 'railties') diff --git a/railties/lib/rails/generators/erb.rb b/railties/lib/rails/generators/erb.rb index 33f8a9da6f..0755ac335c 100644 --- a/railties/lib/rails/generators/erb.rb +++ b/railties/lib/rails/generators/erb.rb @@ -6,7 +6,7 @@ module Erb # :nodoc: protected def formats - format + [format] end def format diff --git a/railties/lib/rails/generators/erb/controller/controller_generator.rb b/railties/lib/rails/generators/erb/controller/controller_generator.rb index e62aece7c5..94c1b835d1 100644 --- a/railties/lib/rails/generators/erb/controller/controller_generator.rb +++ b/railties/lib/rails/generators/erb/controller/controller_generator.rb @@ -11,7 +11,7 @@ module Erb # :nodoc: actions.each do |action| @action = action - Array(formats).each do |format| + formats.each do |format| @path = File.join(base_path, filename_with_extensions(action, format)) template filename_with_extensions(:view, format), @path end diff --git a/railties/lib/rails/generators/erb/scaffold/scaffold_generator.rb b/railties/lib/rails/generators/erb/scaffold/scaffold_generator.rb index b219f459ac..c94829a0ae 100644 --- a/railties/lib/rails/generators/erb/scaffold/scaffold_generator.rb +++ b/railties/lib/rails/generators/erb/scaffold/scaffold_generator.rb @@ -14,7 +14,7 @@ module Erb # :nodoc: def copy_view_files available_views.each do |view| - Array(formats).each do |format| + formats.each do |format| filename = filename_with_extensions(view, format) template filename, File.join("app/views", controller_file_path, filename) end -- cgit v1.2.3 From cff340f1eda96be952437abeed80591a85ef0194 Mon Sep 17 00:00:00 2001 From: Erik Michaels-Ober Date: Mon, 3 Mar 2014 19:23:29 -0800 Subject: Replace map.flatten with flat_map in railties --- railties/lib/rails/commands/plugin.rb | 2 +- railties/lib/rails/generators/actions.rb | 2 +- railties/lib/rails/paths.rb | 2 +- railties/lib/rails/source_annotation_extractor.rb | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) (limited to 'railties') diff --git a/railties/lib/rails/commands/plugin.rb b/railties/lib/rails/commands/plugin.rb index f7a0b99005..95bbdd4cdf 100644 --- a/railties/lib/rails/commands/plugin.rb +++ b/railties/lib/rails/commands/plugin.rb @@ -11,7 +11,7 @@ else end if File.exist?(railsrc) extra_args_string = File.read(railsrc) - extra_args = extra_args_string.split(/\n+/).map {|l| l.split}.flatten + extra_args = extra_args_string.split(/\n+/).flat_map {|l| l.split} puts "Using #{extra_args.join(" ")} from #{railsrc}" ARGV.insert(1, *extra_args) end diff --git a/railties/lib/rails/generators/actions.rb b/railties/lib/rails/generators/actions.rb index afdbf5c241..625f031c94 100644 --- a/railties/lib/rails/generators/actions.rb +++ b/railties/lib/rails/generators/actions.rb @@ -188,7 +188,7 @@ module Rails # generate(:authenticated, "user session") def generate(what, *args) log :generate, what - argument = args.map {|arg| arg.to_s }.flatten.join(" ") + argument = args.flat_map {|arg| arg.to_s }.join(" ") in_root { run_ruby_script("bin/rails generate #{what} #{argument}", verbose: false) } end diff --git a/railties/lib/rails/paths.rb b/railties/lib/rails/paths.rb index 117bb37487..3eb66c07af 100644 --- a/railties/lib/rails/paths.rb +++ b/railties/lib/rails/paths.rb @@ -101,7 +101,7 @@ module Rails def filter_by(&block) all_paths.find_all(&block).flat_map { |path| paths = path.existent - paths - path.children.map { |p| yield(p) ? [] : p.existent }.flatten + paths - path.children.flat_map { |p| yield(p) ? [] : p.existent } }.uniq end end diff --git a/railties/lib/rails/source_annotation_extractor.rb b/railties/lib/rails/source_annotation_extractor.rb index 3cf6a005ea..83e28090f8 100644 --- a/railties/lib/rails/source_annotation_extractor.rb +++ b/railties/lib/rails/source_annotation_extractor.rb @@ -115,7 +115,7 @@ class SourceAnnotationExtractor # Prints the mapping from filenames to annotations in +results+ ordered by filename. # The +options+ hash is passed to each annotation's +to_s+. def display(results, options={}) - options[:indent] = results.map { |f, a| a.map(&:line) }.flatten.max.to_s.size + options[:indent] = results.flat_map { |f, a| a.map(&:line) }.max.to_s.size results.keys.sort.each do |file| puts "#{file}:" results[file].each do |note| -- cgit v1.2.3 From 2dd2fcf89673afbcf95240ecebaf34826a195164 Mon Sep 17 00:00:00 2001 From: Prem Sichanugrist Date: Tue, 18 Feb 2014 16:13:23 -0500 Subject: Introduce `Rails.gem_version` This method return `Gem::Version.new(Rails.version)`, suggesting a more reliable way to perform version comparison. Example: Rails.version #=> "4.1.2" Rails.gem_version #=> # Rails.version > "4.1.10" #=> false Rails.gem_version > Gem::Version.new("4.1.10") #=> true Gem::Requirement.new("~> 4.1.2") =~ Rails.gem_version #=> true This was originally introduced as `.version` by @charliesome in #8501 but got reverted in #10002 since it was not backward compatible. Also, updating template for `rake update_versions`. --- railties/CHANGELOG.md | 15 +++++++++++++++ railties/lib/rails.rb | 4 ---- railties/lib/rails/gem_version.rb | 15 +++++++++++++++ railties/lib/rails/version.rb | 12 +++++------- railties/test/version_test.rb | 12 ++++++++++++ 5 files changed, 47 insertions(+), 11 deletions(-) create mode 100644 railties/lib/rails/gem_version.rb create mode 100644 railties/test/version_test.rb (limited to 'railties') diff --git a/railties/CHANGELOG.md b/railties/CHANGELOG.md index 9dce38fc93..5096f5324a 100644 --- a/railties/CHANGELOG.md +++ b/railties/CHANGELOG.md @@ -1,3 +1,18 @@ +* Introduce `Rails.gem_version` as a convenience method to return + `Gem::Version.new(Rails.version)`, suggesting a more reliable way to perform + version comparison. + + Example: + + Rails.version #=> "4.1.2" + Rails.gem_version #=> # + + Rails.version > "4.1.10" #=> false + Rails.gem_version > Gem::Version.new("4.1.10") #=> true + Gem::Requirement.new("~> 4.1.2") =~ Rails.gem_version #=> true + + *Prem Sichanugrist* + * Avoid namespacing routes inside engines. Mountable engines are namespaced by default so the generated routes diff --git a/railties/lib/rails.rb b/railties/lib/rails.rb index be7570a5ba..ecd8c22dd8 100644 --- a/railties/lib/rails.rb +++ b/railties/lib/rails.rb @@ -80,10 +80,6 @@ module Rails groups end - def version - VERSION::STRING - end - def public_path application && Pathname.new(application.paths["public"].first) end diff --git a/railties/lib/rails/gem_version.rb b/railties/lib/rails/gem_version.rb new file mode 100644 index 0000000000..c7397c4f15 --- /dev/null +++ b/railties/lib/rails/gem_version.rb @@ -0,0 +1,15 @@ +module Rails + # Returns the version of the currently loaded Rails as a Gem::Version + def self.gem_version + Gem::Version.new VERSION::STRING + end + + module VERSION + MAJOR = 4 + MINOR = 2 + TINY = 0 + PRE = "alpha" + + STRING = [MAJOR, MINOR, TINY, PRE].compact.join(".") + end +end diff --git a/railties/lib/rails/version.rb b/railties/lib/rails/version.rb index 79313c936a..df351c4238 100644 --- a/railties/lib/rails/version.rb +++ b/railties/lib/rails/version.rb @@ -1,10 +1,8 @@ -module Rails - module VERSION - MAJOR = 4 - MINOR = 2 - TINY = 0 - PRE = "alpha" +require_relative 'gem_version' - STRING = [MAJOR, MINOR, TINY, PRE].compact.join(".") +module Rails + # Returns the version of the currently loaded Rails as a string. + def self.version + VERSION::STRING end end diff --git a/railties/test/version_test.rb b/railties/test/version_test.rb new file mode 100644 index 0000000000..f270d8f0c9 --- /dev/null +++ b/railties/test/version_test.rb @@ -0,0 +1,12 @@ +require 'abstract_unit' + +class VersionTest < ActiveSupport::TestCase + def test_rails_version_returns_a_string + assert Rails.version.is_a? String + end + + def test_rails_gem_version_returns_a_correct_gem_version_object + assert Rails.gem_version.is_a? Gem::Version + assert_equal Rails.version, Rails.gem_version.to_s + end +end -- cgit v1.2.3 From 9c53b8b89f0b4aec5a2ec3611cefae8e7b6514a5 Mon Sep 17 00:00:00 2001 From: Robin Dupret Date: Sat, 8 Mar 2014 15:51:49 +0100 Subject: Make the rails:template rake task load initializers Templates could rely on irregular inflections or external libraries for instance so we should load the application's initializers when running the rails:template task. The introducing commit of this feature is f7f11361 ; the initializers have never been loaded invoking this task. Fixes #12133. --- railties/CHANGELOG.md | 6 ++++++ railties/lib/rails/tasks/framework.rake | 2 +- railties/test/application/rake_test.rb | 11 +++++++++++ 3 files changed, 18 insertions(+), 1 deletion(-) (limited to 'railties') diff --git a/railties/CHANGELOG.md b/railties/CHANGELOG.md index 5096f5324a..95f2c25a15 100644 --- a/railties/CHANGELOG.md +++ b/railties/CHANGELOG.md @@ -1,3 +1,9 @@ +* Make the `rails:template` rake task load the application's initializers. + + Fixes #12133. + + *Robin Dupret* + * Introduce `Rails.gem_version` as a convenience method to return `Gem::Version.new(Rails.version)`, suggesting a more reliable way to perform version comparison. diff --git a/railties/lib/rails/tasks/framework.rake b/railties/lib/rails/tasks/framework.rake index e669315934..3c8f8c6b87 100644 --- a/railties/lib/rails/tasks/framework.rake +++ b/railties/lib/rails/tasks/framework.rake @@ -3,7 +3,7 @@ namespace :rails do task update: [ "update:configs", "update:bin" ] desc "Applies the template supplied by LOCATION=(/path/to/template) or URL" - task :template do + task template: :environment do template = ENV["LOCATION"] raise "No LOCATION value given. Please set LOCATION either as path to a file or a URL" if template.blank? template = File.expand_path(template) if template !~ %r{\A[A-Za-z][A-Za-z0-9+\-\.]*://} diff --git a/railties/test/application/rake_test.rb b/railties/test/application/rake_test.rb index 317e73245c..e8c8de9f73 100644 --- a/railties/test/application/rake_test.rb +++ b/railties/test/application/rake_test.rb @@ -271,5 +271,16 @@ module ApplicationTests end end end + + def test_template_load_initializers + app_file "config/initializers/dummy.rb", "puts 'Hello, World!'" + app_file "template.rb", "" + + output = Dir.chdir(app_path) do + `bundle exec rake rails:template LOCATION=template.rb` + end + + assert_match(/Hello, World!/, output) + end end end -- cgit v1.2.3 From 9e61a14d4be5122fd863b8ae8096a053bb47144c Mon Sep 17 00:00:00 2001 From: Arun Agrawal Date: Fri, 26 Jul 2013 17:20:06 +0200 Subject: Removed unnecessary command "application" --- railties/CHANGELOG.md | 4 ++++ railties/lib/rails/commands/commands_tasks.rb | 7 +------ 2 files changed, 5 insertions(+), 6 deletions(-) (limited to 'railties') diff --git a/railties/CHANGELOG.md b/railties/CHANGELOG.md index 95f2c25a15..35b3a379ae 100644 --- a/railties/CHANGELOG.md +++ b/railties/CHANGELOG.md @@ -1,3 +1,7 @@ +* Removed unnecessary `rails application` command. + + *Arun Agrawal* + * Make the `rails:template` rake task load the application's initializers. Fixes #12133. diff --git a/railties/lib/rails/commands/commands_tasks.rb b/railties/lib/rails/commands/commands_tasks.rb index de60423784..f061c2bf74 100644 --- a/railties/lib/rails/commands/commands_tasks.rb +++ b/railties/lib/rails/commands/commands_tasks.rb @@ -20,7 +20,6 @@ The most common rails commands are: new application called MyApp in "./my_app" In addition to those, there are: - application Generate the Rails application code destroy Undo code generated with "generate" (short-cut alias: "d") plugin new Generates skeleton for developing a Rails plugin runner Run a piece of code in the application environment (short-cut alias: "r") @@ -28,7 +27,7 @@ In addition to those, there are: All commands can be run with -h (or --help) for more information. EOT - COMMAND_WHITELIST = %(plugin generate destroy console server dbconsole application runner new version help) + COMMAND_WHITELIST = %(plugin generate destroy console server dbconsole runner new version help) def initialize(argv) @argv = argv @@ -87,10 +86,6 @@ EOT Rails::DBConsole.start end - def application - require_command!("application") - end - def runner require_command!("runner") end -- cgit v1.2.3