From b870daba5ff71973b237616fb95f90bb321ae7fb Mon Sep 17 00:00:00 2001 From: wycats Date: Sun, 30 May 2010 12:11:50 +0200 Subject: Update CHANGELOG --- actionpack/CHANGELOG | 2 ++ railties/CHANGELOG | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG index 54c7771f4c..faa0d674dc 100644 --- a/actionpack/CHANGELOG +++ b/actionpack/CHANGELOG @@ -1,5 +1,7 @@ *Rails 3.0.0 [beta 4/release candidate] (unreleased)* +* Remove middleware laziness [José Valim] + * Make session stores rely on request.cookie_jar and change set_session semantics to return the cookie value instead of a boolean. [José Valim] * OAuth 2: HTTP Token Authorization support to complement Basic and Digest Authorization. [Rick Olson] diff --git a/railties/CHANGELOG b/railties/CHANGELOG index 1d76c77ade..fe3ab3cdcf 100644 --- a/railties/CHANGELOG +++ b/railties/CHANGELOG @@ -1,7 +1,7 @@ *Rails 3.0.0 [beta 4/release candidate] (unreleased)* * Version bump - +* Removed Rails Metal [YK & JV]. *Rails 3.0.0 [beta 3] (April 13th, 2010)* -- cgit v1.2.3 From afc102698672cdf546a15424471d3287a39dbb06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Wed, 2 Jun 2010 08:45:03 +0200 Subject: Still copy application configuration to generator even if they are required earlier. Also tidy up the guide a little bit. --- railties/guides/source/generators.textile | 12 ++++-------- railties/lib/rails/commands/destroy.rb | 1 + railties/lib/rails/commands/generate.rb | 1 + railties/lib/rails/generators.rb | 8 ++------ railties/test/generators/generators_test_helper.rb | 7 +++++-- 5 files changed, 13 insertions(+), 16 deletions(-) diff --git a/railties/guides/source/generators.textile b/railties/guides/source/generators.textile index d3757e9733..b77a2837c3 100644 --- a/railties/guides/source/generators.textile +++ b/railties/guides/source/generators.textile @@ -322,14 +322,10 @@ config.generators do |g| g.template_engine :erb g.test_framework :shoulda, :fixture => false g.stylesheets false -end - - -And at the end of the same file: - -require 'rails/generators' -Rails::Generators.fallbacks[:shoulda] = :test_unit + # Add a fallback! + g.fallbacks[:should] = :test_unit +end Now, if create a Comment scaffold, you will see that shoulda generators are being invoked, and at the end, they are just falling back to test unit generators: @@ -361,7 +357,7 @@ $ rails generate scaffold Comment body:text create test/unit/helpers/comments_helper_test.rb -Such tool allows your generators to have single responsibility, increasing the code reuse and reducing the amount of code duplication. +Such tool allows your generators to have single responsibility, increasing the code reuse and reducing the amount of duplication. h3. Changelog diff --git a/railties/lib/rails/commands/destroy.rb b/railties/lib/rails/commands/destroy.rb index 9023c61bf2..db59cd8ad9 100644 --- a/railties/lib/rails/commands/destroy.rb +++ b/railties/lib/rails/commands/destroy.rb @@ -1,4 +1,5 @@ require 'rails/generators' +Rails::Generators.configure! if [nil, "-h", "--help"].include?(ARGV.first) Rails::Generators.help 'destroy' diff --git a/railties/lib/rails/commands/generate.rb b/railties/lib/rails/commands/generate.rb index 7d05a30de8..1b3eef504a 100755 --- a/railties/lib/rails/commands/generate.rb +++ b/railties/lib/rails/commands/generate.rb @@ -1,4 +1,5 @@ require 'rails/generators' +Rails::Generators.configure! if [nil, "-h", "--help"].include?(ARGV.first) Rails::Generators.help 'generate' diff --git a/railties/lib/rails/generators.rb b/railties/lib/rails/generators.rb index fe8a6c0b94..af92757053 100644 --- a/railties/lib/rails/generators.rb +++ b/railties/lib/rails/generators.rb @@ -68,6 +68,7 @@ module Rails options.deep_merge! config.options fallbacks.merge! config.fallbacks templates_path.concat config.templates + templates_path.uniq! end def self.templates_path @@ -328,10 +329,5 @@ module Rails paths.uniq! paths end - end -end - -# If the application was already defined, configure generators, -# otherwise you have to configure it by hand. -Rails::Generators.configure! if Rails.respond_to?(:application) && Rails.application +end \ No newline at end of file diff --git a/railties/test/generators/generators_test_helper.rb b/railties/test/generators/generators_test_helper.rb index d8bdb344f2..4a5a9b2932 100644 --- a/railties/test/generators/generators_test_helper.rb +++ b/railties/test/generators/generators_test_helper.rb @@ -1,4 +1,6 @@ require 'abstract_unit' +require 'rails/generators' +require 'rails/generators/test_case' module Rails def self.root @@ -8,8 +10,9 @@ end Rails.application.config.root = Rails.root Rails.application.config.generators.templates = [File.join(Rails.root, "lib", "templates")] -require 'rails/generators' -require 'rails/generators/test_case' +# Call configure to load the settings from +# Rails.application.config.generators to Rails::Generators +Rails::Generators.configure! require 'active_record' require 'action_dispatch' -- cgit v1.2.3 From 0570720d667b5fe55aeebfba087064788d4d2ba4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Wed, 2 Jun 2010 09:37:38 +0200 Subject: Configure generators before invoking. --- activerecord/lib/active_record/railties/databases.rake | 1 + 1 file changed, 1 insertion(+) diff --git a/activerecord/lib/active_record/railties/databases.rake b/activerecord/lib/active_record/railties/databases.rake index cb7eade0ab..952f2a3070 100644 --- a/activerecord/lib/active_record/railties/databases.rake +++ b/activerecord/lib/active_record/railties/databases.rake @@ -435,6 +435,7 @@ namespace :db do task :create => :environment do raise "Task unavailable to this database (no migration support)" unless ActiveRecord::Base.connection.supports_migrations? require 'rails/generators' + Rails::Generators.configure! require 'rails/generators/rails/session_migration/session_migration_generator' Rails::Generators::SessionMigrationGenerator.start [ ENV["MIGRATION"] || "add_sessions_table" ] end -- cgit v1.2.3 From 4b91daff13be43ed913a97ffc8ad1b3f77fd9690 Mon Sep 17 00:00:00 2001 From: Pratik Naik Date: Wed, 2 Jun 2010 14:42:25 +0100 Subject: Special treatement for Relation#select { with block } --- activerecord/lib/active_record/relation/query_methods.rb | 15 ++++++++++++++- activerecord/test/cases/relations_test.rb | 5 +++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb index 6782554854..7a48a6596a 100644 --- a/activerecord/lib/active_record/relation/query_methods.rb +++ b/activerecord/lib/active_record/relation/query_methods.rb @@ -9,7 +9,7 @@ module ActiveRecord (ActiveRecord::Relation::ASSOCIATION_METHODS + ActiveRecord::Relation::MULTI_VALUE_METHODS).each do |query_method| attr_accessor :"#{query_method}_values" - next if [:where, :having].include?(query_method) + next if [:where, :having, :select].include?(query_method) class_eval <<-CEVAL, __FILE__, __LINE__ + 1 def #{query_method}(*args, &block) new_relation = clone @@ -21,6 +21,19 @@ module ActiveRecord CEVAL end + class_eval <<-CEVAL, __FILE__, __LINE__ + 1 + def select(*args, &block) + if block_given? + to_a.select(&block) + else + new_relation = clone + value = Array.wrap(args.flatten).reject {|x| x.blank? } + new_relation.select_values += value if value.present? + new_relation + end + end + CEVAL + [:where, :having].each do |query_method| class_eval <<-CEVAL, __FILE__, __LINE__ + 1 def #{query_method}(*args, &block) diff --git a/activerecord/test/cases/relations_test.rb b/activerecord/test/cases/relations_test.rb index b6815af67e..4097c5119e 100644 --- a/activerecord/test/cases/relations_test.rb +++ b/activerecord/test/cases/relations_test.rb @@ -112,6 +112,11 @@ class RelationTest < ActiveRecord::TestCase assert_equal 4, developers.map(&:salary).uniq.size end + def test_select_with_block + even_ids = Developer.scoped.select {|d| d.id % 2 == 0 }.map(&:id) + assert_equal [2, 4, 6, 8, 10], even_ids + end + def test_finding_with_hash_conditions_on_joined_table firms = DependentFirm.joins(:account).where({:name => 'RailsCore', :accounts => { :credit_limit => 55..60 }}).to_a assert_equal 1, firms.size -- cgit v1.2.3 From ffe001f19dbbd9e697f6300650779f5e1391ce1e Mon Sep 17 00:00:00 2001 From: wycats Date: Wed, 2 Jun 2010 22:56:41 +0200 Subject: Changes made while working on upgrading cells to Rails 3 --- actionpack/lib/abstract_controller/base.rb | 1 + actionpack/lib/abstract_controller/rendering.rb | 1 + actionpack/lib/action_controller/metal/rack_delegation.rb | 8 ++++---- .../lib/action_controller/metal/request_forgery_protection.rb | 1 + actionpack/lib/action_view/paths.rb | 1 + 5 files changed, 8 insertions(+), 4 deletions(-) diff --git a/actionpack/lib/abstract_controller/base.rb b/actionpack/lib/abstract_controller/base.rb index 8500cbd7f2..ff97a7e76a 100644 --- a/actionpack/lib/abstract_controller/base.rb +++ b/actionpack/lib/abstract_controller/base.rb @@ -1,4 +1,5 @@ require 'active_support/configurable' +require 'active_support/core_ext/module/anonymous' module AbstractController class Error < StandardError; end diff --git a/actionpack/lib/abstract_controller/rendering.rb b/actionpack/lib/abstract_controller/rendering.rb index d2db366140..6e3998aa21 100644 --- a/actionpack/lib/abstract_controller/rendering.rb +++ b/actionpack/lib/abstract_controller/rendering.rb @@ -1,4 +1,5 @@ require "abstract_controller/base" +require "action_view" module AbstractController class DoubleRenderError < Error diff --git a/actionpack/lib/action_controller/metal/rack_delegation.rb b/actionpack/lib/action_controller/metal/rack_delegation.rb index 060117756e..508ea6e2b7 100644 --- a/actionpack/lib/action_controller/metal/rack_delegation.rb +++ b/actionpack/lib/action_controller/metal/rack_delegation.rb @@ -8,10 +8,10 @@ module ActionController delegate :headers, :status=, :location=, :content_type=, :status, :location, :content_type, :to => "@_response" - def dispatch(action, request) - @_response = ActionDispatch::Response.new - @_response.request = request - super + def dispatch(action, request, response = ActionDispatch::Response.new) + @_response ||= response + @_response.request ||= request + super(action, request) end def params diff --git a/actionpack/lib/action_controller/metal/request_forgery_protection.rb b/actionpack/lib/action_controller/metal/request_forgery_protection.rb index 2ba0d6e5cd..8c25b147ef 100644 --- a/actionpack/lib/action_controller/metal/request_forgery_protection.rb +++ b/actionpack/lib/action_controller/metal/request_forgery_protection.rb @@ -47,6 +47,7 @@ module ActionController #:nodoc: extend ActiveSupport::Concern include AbstractController::Helpers + include AbstractController::Callbacks included do # Sets the token parameter name for RequestForgery. Calling +protect_from_forgery+ diff --git a/actionpack/lib/action_view/paths.rb b/actionpack/lib/action_view/paths.rb index 35927d09d1..7f5e5d11b8 100644 --- a/actionpack/lib/action_view/paths.rb +++ b/actionpack/lib/action_view/paths.rb @@ -31,6 +31,7 @@ module ActionView #:nodoc: def typecast! each_with_index do |path, i| + path = path.to_s if path.is_a?(Pathname) next unless path.is_a?(String) self[i] = FileSystemResolver.new(path) end -- cgit v1.2.3 From 02512914ae547eb664a78c0f6084b121d5283a61 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Wed, 2 Jun 2010 16:17:05 -0500 Subject: You need the ruby-debug available in your Gemfile for debugger to work --- activesupport/lib/active_support/core_ext/kernel/debugger.rb | 2 +- railties/lib/rails/generators/rails/app/templates/Gemfile | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/activesupport/lib/active_support/core_ext/kernel/debugger.rb b/activesupport/lib/active_support/core_ext/kernel/debugger.rb index 22fcc1910b..692340c7c7 100644 --- a/activesupport/lib/active_support/core_ext/kernel/debugger.rb +++ b/activesupport/lib/active_support/core_ext/kernel/debugger.rb @@ -2,7 +2,7 @@ module Kernel unless respond_to?(:debugger) # Starts a debugging session if ruby-debug has been loaded (call rails server --debugger to do load it). def debugger - message = "\n***** Debugger requested, but was not available: Start server with --debugger to enable *****\n" + message = "\n***** Debugger requested, but was not available (ensure ruby-debug is listed in Gemfile/installed as gem): Start server with --debugger to enable *****\n" defined?(Rails) ? Rails.logger.info(message) : $stderr.puts(message) end end diff --git a/railties/lib/rails/generators/rails/app/templates/Gemfile b/railties/lib/rails/generators/rails/app/templates/Gemfile index f751c4519d..0b922a89c0 100644 --- a/railties/lib/rails/generators/rails/app/templates/Gemfile +++ b/railties/lib/rails/generators/rails/app/templates/Gemfile @@ -23,6 +23,9 @@ gem '<%= gem_for_database %>'<% if require_for_database %>, :require => '<%= req # Deploy with Capistrano # gem 'capistrano' +# To use debugger +# gem 'ruby-debug' + # Bundle the extra gems: # gem 'bj' # gem 'nokogiri', '1.4.1' -- cgit v1.2.3 From 631dc618853325e5780237388224cb785a3a1b82 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Wed, 2 Jun 2010 16:49:02 -0500 Subject: Extract assets paths and make them available to Action Mailer as well --- actionmailer/lib/action_mailer/base.rb | 1 + actionmailer/lib/action_mailer/railtie.rb | 9 +++++++++ actionpack/lib/abstract_controller.rb | 1 + actionpack/lib/action_controller/base.rb | 3 ++- 4 files changed, 13 insertions(+), 1 deletion(-) diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb index 3c41691af7..3a82979d35 100644 --- a/actionmailer/lib/action_mailer/base.rb +++ b/actionmailer/lib/action_mailer/base.rb @@ -311,6 +311,7 @@ module ActionMailer #:nodoc: include AbstractController::Layouts include AbstractController::Helpers include AbstractController::Translation + include AbstractController::AssetPaths helper ActionMailer::MailHelper diff --git a/actionmailer/lib/action_mailer/railtie.rb b/actionmailer/lib/action_mailer/railtie.rb index 0730167a3e..43a4936013 100644 --- a/actionmailer/lib/action_mailer/railtie.rb +++ b/actionmailer/lib/action_mailer/railtie.rb @@ -13,7 +13,16 @@ module ActionMailer end initializer "action_mailer.set_configs" do |app| + paths = app.config.paths + am = app.config.action_mailer + + am.assets_dir ||= paths.public.to_a.first + am.javascripts_dir ||= paths.public.javascripts.to_a.first + am.stylesheets_dir ||= paths.public.stylesheets.to_a.first + ActiveSupport.on_load(:action_mailer) do + self.config.merge!(am) + include app.routes.url_helpers app.config.action_mailer.each do |k,v| diff --git a/actionpack/lib/abstract_controller.rb b/actionpack/lib/abstract_controller.rb index 2da4dc052c..5990a1bbd0 100644 --- a/actionpack/lib/abstract_controller.rb +++ b/actionpack/lib/abstract_controller.rb @@ -20,5 +20,6 @@ module AbstractController autoload :Logger autoload :Rendering autoload :Translation + autoload :AssetPaths autoload :ViewPaths end diff --git a/actionpack/lib/action_controller/base.rb b/actionpack/lib/action_controller/base.rb index 4297d9bbf6..73e07d59e3 100644 --- a/actionpack/lib/action_controller/base.rb +++ b/actionpack/lib/action_controller/base.rb @@ -13,6 +13,7 @@ module ActionController MODULES = [ AbstractController::Layouts, AbstractController::Translation, + AbstractController::AssetPaths, Helpers, HideActions, @@ -67,7 +68,7 @@ module ActionController end # TODO Move this to the appropriate module - config_accessor :assets_dir, :asset_path, :javascripts_dir, :stylesheets_dir + config_accessor :asset_path ActiveSupport.run_load_hooks(:action_controller, self) end -- cgit v1.2.3 From 71375c8936696d8d7404e02ff84e3dbde3d57029 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Wed, 2 Jun 2010 14:53:56 -0700 Subject: Add an asset_host accessor for consistency. --- actionpack/lib/action_controller/base.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/actionpack/lib/action_controller/base.rb b/actionpack/lib/action_controller/base.rb index 73e07d59e3..8611d0d3c3 100644 --- a/actionpack/lib/action_controller/base.rb +++ b/actionpack/lib/action_controller/base.rb @@ -67,8 +67,7 @@ module ActionController @subclasses ||= [] end - # TODO Move this to the appropriate module - config_accessor :asset_path + config_accessor :asset_host, :asset_path ActiveSupport.run_load_hooks(:action_controller, self) end -- cgit v1.2.3 From 4aac5fd7118065444cb860f1d76a0a76d3914364 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Wed, 2 Jun 2010 16:55:07 -0500 Subject: Missed the extraction --- actionpack/lib/abstract_controller/asset_paths.rb | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 actionpack/lib/abstract_controller/asset_paths.rb diff --git a/actionpack/lib/abstract_controller/asset_paths.rb b/actionpack/lib/abstract_controller/asset_paths.rb new file mode 100644 index 0000000000..6d6f6ac607 --- /dev/null +++ b/actionpack/lib/abstract_controller/asset_paths.rb @@ -0,0 +1,9 @@ +module AbstractController + module AssetPaths + extend ActiveSupport::Concern + + included do + config_accessor :assets_dir, :javascripts_dir, :stylesheets_dir + end + end +end \ No newline at end of file -- cgit v1.2.3 From 517f709b51d1d2766d1a783d9f02fa7a6a41abc4 Mon Sep 17 00:00:00 2001 From: Pratik Naik Date: Fri, 4 Jun 2010 00:53:45 +0100 Subject: Properly cache association_collection#scopes calls having arguments --- .../active_record/associations/association_collection.rb | 3 ++- activerecord/test/cases/named_scope_test.rb | 13 +++++++++++++ activerecord/test/models/comment.rb | 1 + 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/activerecord/lib/active_record/associations/association_collection.rb b/activerecord/lib/active_record/associations/association_collection.rb index 0dfd966466..d9903243ce 100644 --- a/activerecord/lib/active_record/associations/association_collection.rb +++ b/activerecord/lib/active_record/associations/association_collection.rb @@ -411,7 +411,8 @@ module ActiveRecord end elsif @reflection.klass.scopes[method] @_named_scopes_cache ||= {} - @_named_scopes_cache[method] ||= with_scope(construct_scope) { @reflection.klass.send(method, *args) } + @_named_scopes_cache[method] ||= {} + @_named_scopes_cache[method][args] ||= with_scope(construct_scope) { @reflection.klass.send(method, *args) } else with_scope(construct_scope) do if block_given? diff --git a/activerecord/test/cases/named_scope_test.rb b/activerecord/test/cases/named_scope_test.rb index 2007f5492f..91d8d2828b 100644 --- a/activerecord/test/cases/named_scope_test.rb +++ b/activerecord/test/cases/named_scope_test.rb @@ -428,6 +428,19 @@ class NamedScopeTest < ActiveRecord::TestCase assert_no_queries { post.comments.containing_the_letter_e.all } end + def test_named_scopes_with_arguments_are_cached_on_associations + post = posts(:welcome) + + one = post.comments.limit_by(1).all + assert_equal 1, one.size + + two = post.comments.limit_by(2).all + assert_equal 2, two.size + + assert_no_queries { post.comments.limit_by(1).all } + assert_no_queries { post.comments.limit_by(2).all } + end + def test_named_scopes_are_reset_on_association_reload post = posts(:welcome) diff --git a/activerecord/test/models/comment.rb b/activerecord/test/models/comment.rb index a8a99f6dce..9f6e2d3b71 100644 --- a/activerecord/test/models/comment.rb +++ b/activerecord/test/models/comment.rb @@ -1,4 +1,5 @@ class Comment < ActiveRecord::Base + scope :limit_by, lambda {|l| limit(l) } scope :containing_the_letter_e, :conditions => "comments.body LIKE '%e%'" scope :for_first_post, :conditions => { :post_id => 1 } scope :for_first_author, -- cgit v1.2.3 From 4774680405adcbf0d3f64abbc236c7653d137fa3 Mon Sep 17 00:00:00 2001 From: Mikel Lindsaar Date: Thu, 3 Jun 2010 23:47:44 +1000 Subject: Changing command line API from 'rails blog' to 'rails new blog'. Also removed the limitation of not being able to call your new server any of the rails commands (generate, server, dbconsole, console etc) as there is no longer any ambiguity here. http://rails.lighthouseapp.com/projects/8994/tickets/4665 Signed-off-by: David Heinemeier Hansson --- railties/lib/rails/commands.rb | 6 ++++++ railties/lib/rails/commands/application.rb | 7 ++++++- railties/lib/rails/generators/rails/app/USAGE | 4 ++-- railties/lib/rails/generators/rails/app/app_generator.rb | 3 +-- 4 files changed, 15 insertions(+), 5 deletions(-) diff --git a/railties/lib/rails/commands.rb b/railties/lib/rails/commands.rb index de93a87615..aad1170b56 100644 --- a/railties/lib/rails/commands.rb +++ b/railties/lib/rails/commands.rb @@ -38,6 +38,10 @@ when 'dbconsole' when 'application', 'runner' require "rails/commands/#{command}" +when 'new' + puts "Can't initialize a new Rails application within the directory of another, please change to a non-Rails directory first.\n" + puts "Type 'rails' for help." + when '--version', '-v' ARGV.unshift '--version' require 'rails/commands/application' @@ -53,6 +57,8 @@ The most common rails commands are: server Start the Rails server (short-cut alias: "s") dbconsole Start a console for the database specified in config/database.yml (short-cut alias: "db") + new Create a new Rails application. "rails new my_app" creates a + new application called MyApp in "./my_app" In addition to those, there are: application Generate the Rails application code diff --git a/railties/lib/rails/commands/application.rb b/railties/lib/rails/commands/application.rb index 8a8143e00e..a3a5aed399 100644 --- a/railties/lib/rails/commands/application.rb +++ b/railties/lib/rails/commands/application.rb @@ -4,7 +4,12 @@ if %w(--version -v).include? ARGV.first exit(0) end -ARGV << "--help" if ARGV.empty? +if ARGV.first != "new" || ARGV.empty? + ARGV[0] = "--help" +else + ARGV.shift +end + require 'rubygems' if ARGV.include?("--dev") require 'rails/generators' diff --git a/railties/lib/rails/generators/rails/app/USAGE b/railties/lib/rails/generators/rails/app/USAGE index 36d6061a59..9e7a78d132 100644 --- a/railties/lib/rails/generators/rails/app/USAGE +++ b/railties/lib/rails/generators/rails/app/USAGE @@ -1,9 +1,9 @@ Description: - The 'rails' command creates a new Rails application with a default + The 'rails new' command creates a new Rails application with a default directory structure and configuration at the path you specify. Example: - rails ~/Code/Ruby/weblog + rails new ~/Code/Ruby/weblog This generates a skeletal Rails installation in ~/Code/Ruby/weblog. See the README in the newly created application to get going. diff --git a/railties/lib/rails/generators/rails/app/app_generator.rb b/railties/lib/rails/generators/rails/app/app_generator.rb index cd4a3dce4e..fc971907df 100644 --- a/railties/lib/rails/generators/rails/app/app_generator.rb +++ b/railties/lib/rails/generators/rails/app/app_generator.rb @@ -149,8 +149,7 @@ module Rails # can change in Ruby 1.8.7 when we FileUtils.cd. RAILS_DEV_PATH = File.expand_path("../../../../../..", File.dirname(__FILE__)) - RESERVED_NAMES = %w[generate g console c server s dbconsole db - application destroy benchmarker profiler + RESERVED_NAMES = %w[application destroy benchmarker profiler plugin runner test] class AppGenerator < Base -- cgit v1.2.3 From bf83c5709f212b25575249bc7e1b6d7f7a0ad085 Mon Sep 17 00:00:00 2001 From: Mikel Lindsaar Date: Fri, 4 Jun 2010 00:03:42 +1000 Subject: Updating guides to new rails initialization process Signed-off-by: David Heinemeier Hansson --- railties/guides/source/3_0_release_notes.textile | 6 +++--- railties/guides/source/generators.textile | 2 +- railties/guides/source/getting_started.textile | 2 +- railties/guides/source/rails_application_templates.textile | 4 ++-- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/railties/guides/source/3_0_release_notes.textile b/railties/guides/source/3_0_release_notes.textile index b7f4fbf35c..2a8d7fbcdb 100644 --- a/railties/guides/source/3_0_release_notes.textile +++ b/railties/guides/source/3_0_release_notes.textile @@ -81,7 +81,7 @@ The new installing rails sequence (for the beta) is: $ gem install rails --prerelease -$ rails myapp +$ rails new myapp $ cd myapp @@ -98,13 +98,13 @@ h4. Living on the Edge If you want to bundle straight from the Git repository, you can pass the +--edge+ flag: -$ rails myapp --edge +$ rails new myapp --edge If you have a local checkout of the Rails repository and want to generate an application using that, you can pass the +--dev+ flag: -$ ruby /path/to/rails/bin/rails myapp --dev +$ ruby /path/to/rails/bin/rails new myapp --dev h3. Rails Architectural Changes diff --git a/railties/guides/source/generators.textile b/railties/guides/source/generators.textile index b77a2837c3..704a8793b2 100644 --- a/railties/guides/source/generators.textile +++ b/railties/guides/source/generators.textile @@ -20,7 +20,7 @@ h3. First contact When you create an application using the +rails+ command, you are in fact using a Rails generator. After that, you can get a list of all available generators by just invoking +rails generate+: -$ rails myapp +$ rails new myapp $ cd myapp $ rails generate diff --git a/railties/guides/source/getting_started.textile b/railties/guides/source/getting_started.textile index 46e709d0f5..89551a223d 100644 --- a/railties/guides/source/getting_started.textile +++ b/railties/guides/source/getting_started.textile @@ -160,7 +160,7 @@ The best way to use this guide is to follow each step as it happens, no code or To begin, open a terminal, navigate to a folder where you have rights to create files, and type: -$ rails blog +$ rails new blog This will create a Rails application called Blog in a directory called blog. diff --git a/railties/guides/source/rails_application_templates.textile b/railties/guides/source/rails_application_templates.textile index baaa3d6d66..1af6f56957 100644 --- a/railties/guides/source/rails_application_templates.textile +++ b/railties/guides/source/rails_application_templates.textile @@ -14,13 +14,13 @@ h3. Usage To apply a template, you need to provide the Rails generator with the location of the template you wish to apply, using -m option : -$ rails blog -m ~/template.rb +$ rails new blog -m ~/template.rb It's also possible to apply a template using a URL : -$ rails blog -m http://gist.github.com/31208.txt +$ rails new blog -m http://gist.github.com/31208.txt Alternatively, you can use the rake task +rails:template+ to apply a template to an existing Rails application : -- cgit v1.2.3 From 6401ab587021b78c3dc5e4a5ac831823a9258481 Mon Sep 17 00:00:00 2001 From: Mikel Lindsaar Date: Fri, 4 Jun 2010 16:36:38 +1000 Subject: Missed fixing the banner on the Usage output for Thor Signed-off-by: David Heinemeier Hansson --- railties/lib/rails/generators/rails/app/app_generator.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/railties/lib/rails/generators/rails/app/app_generator.rb b/railties/lib/rails/generators/rails/app/app_generator.rb index fc971907df..7d50e7da67 100644 --- a/railties/lib/rails/generators/rails/app/app_generator.rb +++ b/railties/lib/rails/generators/rails/app/app_generator.rb @@ -309,7 +309,7 @@ module Rails protected def self.banner - "rails #{self.arguments.map(&:usage).join(' ')} [options]" + "rails new #{self.arguments.map(&:usage).join(' ')} [options]" end def builder -- cgit v1.2.3 From 67a43554f153a3ddb97039b5fac305c0619dd372 Mon Sep 17 00:00:00 2001 From: Xavier Noria Date: Sat, 5 Jun 2010 01:13:37 +0200 Subject: removes Array#random_element and backports Array#sample from Ruby 1.9, thanks to Marc-Andre Lafortune --- .../associations/eager_load_nested_include_test.rb | 14 +++++----- activerecord/test/cases/named_scope_test.rb | 2 +- activesupport/CHANGELOG | 2 +- .../active_support/core_ext/array/random_access.rb | 22 +++++++++++++--- activesupport/test/core_ext/array_ext_test.rb | 30 +++++++++++++++++----- railties/guides/source/3_0_release_notes.textile | 1 + .../source/active_support_core_extensions.textile | 16 +++++++++--- .../application/initializers/frameworks_test.rb | 4 +-- 8 files changed, 66 insertions(+), 25 deletions(-) diff --git a/activerecord/test/cases/associations/eager_load_nested_include_test.rb b/activerecord/test/cases/associations/eager_load_nested_include_test.rb index 2beb3f8365..c7671a8c22 100644 --- a/activerecord/test/cases/associations/eager_load_nested_include_test.rb +++ b/activerecord/test/cases/associations/eager_load_nested_include_test.rb @@ -17,7 +17,7 @@ module Remembered module ClassMethods def remembered; @@remembered ||= []; end - def random_element; @@remembered.random_element; end + def sample; @@remembered.sample; end end end @@ -79,14 +79,14 @@ class EagerLoadPolyAssocsTest < ActiveRecord::TestCase [Circle, Square, Triangle, NonPolyOne, NonPolyTwo].map(&:create!) end 1.upto(NUM_SIMPLE_OBJS) do - PaintColor.create!(:non_poly_one_id => NonPolyOne.random_element.id) - PaintTexture.create!(:non_poly_two_id => NonPolyTwo.random_element.id) + PaintColor.create!(:non_poly_one_id => NonPolyOne.sample.id) + PaintTexture.create!(:non_poly_two_id => NonPolyTwo.sample.id) end 1.upto(NUM_SHAPE_EXPRESSIONS) do - shape_type = [Circle, Square, Triangle].random_element - paint_type = [PaintColor, PaintTexture].random_element - ShapeExpression.create!(:shape_type => shape_type.to_s, :shape_id => shape_type.random_element.id, - :paint_type => paint_type.to_s, :paint_id => paint_type.random_element.id) + shape_type = [Circle, Square, Triangle].sample + paint_type = [PaintColor, PaintTexture].sample + ShapeExpression.create!(:shape_type => shape_type.to_s, :shape_id => shape_type.sample.id, + :paint_type => paint_type.to_s, :paint_id => paint_type.sample.id) end end diff --git a/activerecord/test/cases/named_scope_test.rb b/activerecord/test/cases/named_scope_test.rb index 91d8d2828b..33ffb041c1 100644 --- a/activerecord/test/cases/named_scope_test.rb +++ b/activerecord/test/cases/named_scope_test.rb @@ -301,7 +301,7 @@ class NamedScopeTest < ActiveRecord::TestCase end def test_rand_should_select_a_random_object_from_proxy - assert_kind_of Topic, Topic.approved.random_element + assert_kind_of Topic, Topic.approved.sample end def test_should_use_where_in_query_for_named_scope diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG index d853788e00..0e24cc138a 100644 --- a/activesupport/CHANGELOG +++ b/activesupport/CHANGELOG @@ -4,7 +4,7 @@ * Ruby 1.9: support UTF-8 case folding. #4595 [Norman Clarke] -* Renames Array#rand -> Array#random_element. [Santiago Pastorino, Rizwan Reza] +* Removes Array#rand and backports Array#sample from Ruby 1.9, thanks to Marc-Andre Lafortune. [fxn] * Ruby 1.9: Renames last_(month|year) to prev_(month|year) in Date and Time. [fxn] diff --git a/activesupport/lib/active_support/core_ext/array/random_access.rb b/activesupport/lib/active_support/core_ext/array/random_access.rb index 67c322daea..7a4836cecd 100644 --- a/activesupport/lib/active_support/core_ext/array/random_access.rb +++ b/activesupport/lib/active_support/core_ext/array/random_access.rb @@ -1,6 +1,20 @@ class Array - # Returns a random element from the array. - def random_element - self[Kernel.rand(length)] - end + # Backport of Array#sample based on Marc-Andre Lafortune's http://github.com/marcandre/backports/ + def sample(n=nil) + return self[Kernel.rand(size)] if n.nil? + n = n.to_int + rescue Exception => e + raise TypeError, "Coercion error: #{n.inspect}.to_int => Integer failed:\n(#{e.message})" + else + raise TypeError, "Coercion error: obj.to_int did NOT return an Integer (was #{n.class})" unless n.kind_of? Integer + raise ArgumentError, "negative array size" if n < 0 + n = size if n > size + result = Array.new(self) + n.times do |i| + r = i + Kernel.rand(size - i) + result[i], result[r] = result[r], result[i] + end + result[n..size] = [] + result + end unless method_defined? :sample end \ No newline at end of file diff --git a/activesupport/test/core_ext/array_ext_test.rb b/activesupport/test/core_ext/array_ext_test.rb index 1f7cdb8ec1..54376deee5 100644 --- a/activesupport/test/core_ext/array_ext_test.rb +++ b/activesupport/test/core_ext/array_ext_test.rb @@ -359,14 +359,30 @@ class ArrayUniqByTests < Test::Unit::TestCase end class ArrayExtRandomTests < ActiveSupport::TestCase - def test_random_element_from_array - assert_nil [].random_element - - Kernel.expects(:rand).with(1).returns(0) - assert_equal 'x', ['x'].random_element + def test_sample_from_array + assert_nil [].sample + assert_equal [], [].sample(5) + assert_equal 42, [42].sample + assert_equal [42], [42].sample(5) + + a = [:foo, :bar, 42] + s = a.sample(2) + assert_equal 2, s.size + assert_equal 1, (a-s).size + assert_equal [], a-(0..20).sum{a.sample(2)} + + o = Object.new + def o.to_int; 1; end + assert_equal [0], [0].sample(o) + + o = Object.new + assert_raises(TypeError) { [0].sample(o) } + + o = Object.new + def o.to_int; ''; end + assert_raises(TypeError) { [0].sample(o) } - Kernel.expects(:rand).with(3).returns(1) - assert_equal 2, [1, 2, 3].random_element + assert_raises(ArgumentError) { [0].sample(-7) } end end diff --git a/railties/guides/source/3_0_release_notes.textile b/railties/guides/source/3_0_release_notes.textile index 2a8d7fbcdb..75c03be87c 100644 --- a/railties/guides/source/3_0_release_notes.textile +++ b/railties/guides/source/3_0_release_notes.textile @@ -512,6 +512,7 @@ These are the main changes in Active Support: * Active Support no longer provides vendored versions of "TZInfo":http://tzinfo.rubyforge.org/, "Memcache Client":http://deveiate.org/projects/RMemCache/ and "Builder":http://builder.rubyforge.org/, these are all included as dependencies and installed via the bundle install command. * Safe buffers are implemented in ActiveSupport::SafeBuffer. * Added Array.uniq_by and Array.uniq_by!. +* Removed Array#rand and backported Array#sample from Ruby 1.9. * Fixed bug on +TimeZone.seconds_to_utc_offset+ returning wrong value. * Added ActiveSupport::Notifications middleware. * ActiveSupport.use_standard_json_time_format now defaults to true. diff --git a/railties/guides/source/active_support_core_extensions.textile b/railties/guides/source/active_support_core_extensions.textile index de82e871a6..6c3a005daf 100644 --- a/railties/guides/source/active_support_core_extensions.textile +++ b/railties/guides/source/active_support_core_extensions.textile @@ -1927,13 +1927,23 @@ Similarly, +from+ returns the tail from the element at the passed index on: The methods +second+, +third+, +fourth+, and +fifth+ return the corresponding element (+first+ is builtin). Thanks to social wisdom and positive constructiveness all around, +forty_two+ is also available. -You can pick a random element with +random_element+: +NOTE: Defined in +active_support/core_ext/array/access.rb+. + +h4. Random Access + +Active Support backports +sample+ from Ruby 1.9: + +You can pick a random element with +sample+: -shape_type = [Circle, Square, Triangle].random_element +shape_type = [Circle, Square, Triangle].sample +# => Square, for example + +shape_types = [Circle, Square, Triangle].sample(2) +# => [Triangle, Circle], for example -NOTE: Defined in +active_support/core_ext/array/access.rb+. +NOTE: Defined in +active_support/core_ext/array/random_access.rb+. h4. Options Extraction diff --git a/railties/test/application/initializers/frameworks_test.rb b/railties/test/application/initializers/frameworks_test.rb index fadcc4c025..35ea2729d3 100644 --- a/railties/test/application/initializers/frameworks_test.rb +++ b/railties/test/application/initializers/frameworks_test.rb @@ -47,7 +47,7 @@ module ApplicationTests test "if there's no config.active_support.bare, all of ActiveSupport is required" do use_frameworks [] require "#{app_path}/config/environment" - assert_nothing_raised { [1,2,3].random_element } + assert_nothing_raised { [1,2,3].sample } end test "config.active_support.bare does not require all of ActiveSupport" do @@ -57,7 +57,7 @@ module ApplicationTests Dir.chdir("#{app_path}/app") do require "#{app_path}/config/environment" - assert_raises(NoMethodError) { [1,2,3].random_element } + assert_raises(NoMethodError) { [1,2,3].sample } end end -- cgit v1.2.3 From 8e8cb1769f3a78c53e5b79d0ce6a08589045cfca Mon Sep 17 00:00:00 2001 From: Xavier Noria Date: Sat, 5 Jun 2010 01:19:23 +0200 Subject: AS guide: removes your spurious line --- railties/guides/source/active_support_core_extensions.textile | 2 -- 1 file changed, 2 deletions(-) diff --git a/railties/guides/source/active_support_core_extensions.textile b/railties/guides/source/active_support_core_extensions.textile index 6c3a005daf..30b2099be4 100644 --- a/railties/guides/source/active_support_core_extensions.textile +++ b/railties/guides/source/active_support_core_extensions.textile @@ -1933,8 +1933,6 @@ h4. Random Access Active Support backports +sample+ from Ruby 1.9: -You can pick a random element with +sample+: - shape_type = [Circle, Square, Triangle].sample # => Square, for example -- cgit v1.2.3