Skip to content
Browse files

Merge commit 'rails/master'

  • Loading branch information...
2 parents cd424f0 + a4bdc00 commit 6d4b6a247ef4119f9657293c552ae71459a3665a @miloops miloops committed Jul 3, 2009
View
1 actionpack/lib/action_controller/routing/route_set.rb
@@ -305,6 +305,7 @@ def routes_changed_at
end
def add_route(path, options = {})
+ options.each { |k, v| options[k] = v.to_s if [:controller, :action].include?(k) && v.is_a?(Symbol) }
route = builder.build(path, options)
routes << route
route
View
6 actionpack/lib/action_view/helpers/asset_tag_helper.rb
@@ -505,16 +505,16 @@ def video_path(source)
def image_tag(source, options = {})
options.symbolize_keys!
- options[:src] = path_to_image(source)
- options[:alt] ||= File.basename(options[:src], '.*').split('.').first.to_s.capitalize
+ src = options[:src] = path_to_image(source)
+ options[:alt] ||= File.basename(src, '.*').split('.').first.to_s.capitalize
if size = options.delete(:size)
options[:width], options[:height] = size.split("x") if size =~ %r{^\d+x\d+$}
end
if mouseover = options.delete(:mouseover)
options[:onmouseover] = "this.src='#{image_path(mouseover)}'"
- options[:onmouseout] = "this.src='#{image_path(options[:src])}'"
+ options[:onmouseout] = "this.src='#{src}'"
end
tag("img", options)
View
10 actionpack/lib/action_view/helpers/form_helper.rb
@@ -926,6 +926,7 @@ class FormBuilder #:nodoc:
attr_accessor :object_name, :object, :options
def initialize(object_name, object, template, options, proc)
+ @nested_child_index = {}
@object_name, @object, @template, @options, @proc = object_name, object, template, options, proc
@default_options = @options ? @options.slice(:index) : {}
if @object_name.to_s.match(/\[\]$/)
@@ -1028,7 +1029,7 @@ def fields_for_with_nested_attributes(association_name, args, block)
explicit_child_index = args.last[:child_index] if args.last.is_a?(Hash)
children.map do |child|
- fields_for_nested_model("#{name}[#{explicit_child_index || nested_child_index}]", child, args, block)
+ fields_for_nested_model("#{name}[#{explicit_child_index || nested_child_index(name)}]", child, args, block)
end.join
else
fields_for_nested_model(name, explicit_object || association, args, block)
@@ -1046,15 +1047,16 @@ def fields_for_nested_model(name, object, args, block)
end
end
- def nested_child_index
- @nested_child_index ||= -1
- @nested_child_index += 1
+ def nested_child_index(name)
+ @nested_child_index[name] ||= -1
+ @nested_child_index[name] += 1
end
end
end
class << Base
attr_accessor :default_form_builder
end
+
Base.default_form_builder = ::ActionView::Helpers::FormBuilder
end
View
10 actionpack/test/controller/routing_test.rb
@@ -2492,6 +2492,16 @@ def test_route_requirement_recognize_with_xi_modifiers
end
assert_equal({:controller => 'pages', :action => 'show', :name => 'JAMIS'}, set.recognize_path('/page/JAMIS'))
end
+
+ def test_routes_with_symbols
+ set.draw do |map|
+ map.connect 'unnamed', :controller => :pages, :action => :show, :name => :as_symbol
+ map.named 'named', :controller => :pages, :action => :show, :name => :as_symbol
+ end
+ assert_equal({:controller => 'pages', :action => 'show', :name => :as_symbol}, set.recognize_path('/unnamed'))
+ assert_equal({:controller => 'pages', :action => 'show', :name => :as_symbol}, set.recognize_path('/named'))
+ end
+
end
class RouteLoadingTest < Test::Unit::TestCase
View
92 actionpack/test/template/form_helper_test.rb
@@ -21,6 +21,9 @@ def author_attributes=(attributes); end
attr_accessor :comments
def comments_attributes=(attributes); end
+
+ attr_accessor :tags
+ def tags_attributes=(attributes); end
end
class Comment
@@ -33,6 +36,50 @@ def to_param; @id; end
def name
@id.nil? ? "new #{self.class.name.downcase}" : "#{self.class.name.downcase} ##{@id}"
end
+
+ attr_accessor :relevances
+ def relevances_attributes=(attributes); end
+
+ end
+
+ class Tag
+ attr_reader :id
+ attr_reader :post_id
+ def initialize(id = nil, post_id = nil); @id, @post_id = id, post_id end
+ def save; @id = 1; @post_id = 1 end
+ def new_record?; @id.nil? end
+ def to_param; @id; end
+ def value
+ @id.nil? ? "new #{self.class.name.downcase}" : "#{self.class.name.downcase} ##{@id}"
+ end
+
+ attr_accessor :relevances
+ def relevances_attributes=(attributes); end
+
+ end
+
+ class CommentRelevance
+ attr_reader :id
+ attr_reader :comment_id
+ def initialize(id = nil, comment_id = nil); @id, @comment_id = id, comment_id end
+ def save; @id = 1; @comment_id = 1 end
+ def new_record?; @id.nil? end
+ def to_param; @id; end
+ def value
+ @id.nil? ? "new #{self.class.name.downcase}" : "#{self.class.name.downcase} ##{@id}"
+ end
+ end
+
+ class TagRelevance
+ attr_reader :id
+ attr_reader :tag_id
+ def initialize(id = nil, tag_id = nil); @id, @tag_id = id, tag_id end
+ def save; @id = 1; @tag_id = 1 end
+ def new_record?; @id.nil? end
+ def to_param; @id; end
+ def value
+ @id.nil? ? "new #{self.class.name.downcase}" : "#{self.class.name.downcase} ##{@id}"
+ end
end
class Author < Comment
@@ -740,6 +787,51 @@ def test_nested_fields_for_with_child_index_option_override_on_a_nested_attribut
assert_dom_equal expected, output_buffer
end
+ def test_nested_fields_uses_unique_indices_for_different_collection_associations
+ @post.comments = [Comment.new(321)]
+ @post.tags = [Tag.new(123), Tag.new(456)]
+ @post.comments[0].relevances = []
+ @post.tags[0].relevances = []
+ @post.tags[1].relevances = []
+ form_for(:post, @post) do |f|
+ f.fields_for(:comments, @post.comments[0]) do |cf|
+ concat cf.text_field(:name)
+ cf.fields_for(:relevances, CommentRelevance.new(314)) do |crf|
+ concat crf.text_field(:value)
+ end
+ end
+ f.fields_for(:tags, @post.tags[0]) do |tf|
+ concat tf.text_field(:value)
+ tf.fields_for(:relevances, TagRelevance.new(3141)) do |trf|
+ concat trf.text_field(:value)
+ end
+ end
+ f.fields_for('tags', @post.tags[1]) do |tf|
+ concat tf.text_field(:value)
+ tf.fields_for(:relevances, TagRelevance.new(31415)) do |trf|
+ concat trf.text_field(:value)
+ end
+ end
+ end
+
+ expected = '<form action="http://www.example.com" method="post">' +
+ '<input id="post_comments_attributes_0_id" name="post[comments_attributes][0][id]" type="hidden" value="321" />' +
+ '<input id="post_comments_attributes_0_name" name="post[comments_attributes][0][name]" size="30" type="text" value="comment #321" />' +
+ '<input id="post_comments_attributes_0_relevances_attributes_0_id" name="post[comments_attributes][0][relevances_attributes][0][id]" type="hidden" value="314" />' +
+ '<input id="post_comments_attributes_0_relevances_attributes_0_value" name="post[comments_attributes][0][relevances_attributes][0][value]" size="30" type="text" value="commentrelevance #314" />' +
+ '<input id="post_tags_attributes_0_id" name="post[tags_attributes][0][id]" type="hidden" value="123" />' +
+ '<input id="post_tags_attributes_0_value" name="post[tags_attributes][0][value]" size="30" type="text" value="tag #123" />' +
+ '<input id="post_tags_attributes_0_relevances_attributes_0_id" name="post[tags_attributes][0][relevances_attributes][0][id]" type="hidden" value="3141" />' +
+ '<input id="post_tags_attributes_0_relevances_attributes_0_value" name="post[tags_attributes][0][relevances_attributes][0][value]" size="30" type="text" value="tagrelevance #3141" />' +
+ '<input id="post_tags_attributes_1_id" name="post[tags_attributes][1][id]" type="hidden" value="456" />' +
+ '<input id="post_tags_attributes_1_value" name="post[tags_attributes][1][value]" size="30" type="text" value="tag #456" />' +
+ '<input id="post_tags_attributes_1_relevances_attributes_0_id" name="post[tags_attributes][1][relevances_attributes][0][id]" type="hidden" value="31415" />' +
+ '<input id="post_tags_attributes_1_relevances_attributes_0_value" name="post[tags_attributes][1][relevances_attributes][0][value]" size="30" type="text" value="tagrelevance #31415" />' +
+ '</form>'
+
+ assert_dom_equal expected, output_buffer
+ end
+
def test_fields_for
fields_for(:post, @post) do |f|
concat f.text_field(:title)
View
22 activesupport/lib/active_support/ordered_hash.rb
@@ -12,11 +12,25 @@ def initialize(*args, &block)
def self.[](*args)
ordered_hash = new
- args.each_with_index { |val,ind|
- # Only every second value is a key.
- next if ind % 2 != 0
+
+ if (args.length == 1 && args.first.is_a?(Array))
+ args.first.each do |key_value_pair|
+ next unless (key_value_pair.is_a?(Array))
+ ordered_hash[key_value_pair[0]] = key_value_pair[1]
+ end
+
+ return ordered_hash
+ end
+
+ unless (args.size % 2 == 0)
+ raise ArgumentError.new("odd number of arguments for Hash")
+ end
+
+ args.each_with_index do |val, ind|
+ next if (ind % 2 != 0)
ordered_hash[val] = args[ind + 1]
- }
+ end
+
ordered_hash
end
View
5 activesupport/lib/active_support/testing/isolation.rb
@@ -21,6 +21,11 @@ def self.forking_env?
end
def run(result)
+ unless defined?(@@ran_class_setup)
+ self.class.setup
+ @@ran_class_setup = true
+ end
+
yield(Test::Unit::TestCase::STARTED, name)
@_result = result
View
17 activesupport/test/isolation_test.rb
@@ -5,6 +5,12 @@
class ChildIsolationTest < ActiveSupport::TestCase
include ActiveSupport::Testing::Isolation
+ def self.setup
+ File.open(File.join(File.dirname(__FILE__), "fixtures", "isolation_test"), "a") do |f|
+ f.puts "hello"
+ end
+ end
+
def setup
@instance = "HELLO"
end
@@ -64,6 +70,8 @@ def teardown
else
class ParentIsolationTest < ActiveSupport::TestCase
+ File.open(File.join(File.dirname(__FILE__), "fixtures", "isolation_test"), "w") {}
+
ENV["CHILD"] = "1"
OUTPUT = `#{Gem.ruby} -I#{File.dirname(__FILE__)} #{File.expand_path(__FILE__)} -v`
ENV.delete("CHILD")
@@ -131,12 +139,17 @@ def assert_erroring(name)
test "backtrace is printed for errors" do
assert_equal 'Error', @backtraces["test_captures_errors"][:type]
- assert_match %{isolation_test.rb:21:in `test_captures_errors'}, @backtraces["test_captures_errors"][:output]
+ assert_match %r{isolation_test.rb:\d+:in `test_captures_errors'}, @backtraces["test_captures_errors"][:output]
end
test "backtrace is printed for failures" do
assert_equal 'Failure', @backtraces["test_captures_failures"][:type]
- assert_match %{isolation_test.rb:25:in `test_captures_failures'}, @backtraces["test_captures_failures"][:output]
+ assert_match %r{isolation_test.rb:\d+:in `test_captures_failures'}, @backtraces["test_captures_failures"][:output]
+ end
+
+ test "self.setup is run only once" do
+ text = File.read(File.join(File.dirname(__FILE__), "fixtures", "isolation_test"))
+ assert_equal "hello\n", text
end
end
View
25 activesupport/test/ordered_hash_test.rb
@@ -163,9 +163,32 @@ def test_inspect
assert @ordered_hash.inspect.include?(@hash.inspect)
end
- def test_alternate_initialization
+ def test_alternate_initialization_with_splat
alternate = ActiveSupport::OrderedHash[1,2,3,4]
assert_kind_of ActiveSupport::OrderedHash, alternate
assert_equal [1, 3], alternate.keys
end
+
+ def test_alternate_initialization_with_array
+ alternate = ActiveSupport::OrderedHash[ [
+ [1, 2],
+ [3, 4],
+ "bad key value pair",
+ [ 'missing value' ]
+ ]]
+
+ assert_kind_of ActiveSupport::OrderedHash, alternate
+ assert_equal [1, 3, 'missing value'], alternate.keys
+ assert_equal [2, 4, nil ], alternate.values
+ end
+
+ def test_alternate_initialization_raises_exception_on_odd_length_args
+ begin
+ alternate = ActiveSupport::OrderedHash[1,2,3,4,5]
+ flunk "Hash::[] should have raised an exception on initialization " +
+ "with an odd number of parameters"
+ rescue
+ assert_equal "odd number of arguments for Hash", $!.message
+ end
+ end
end
View
7 railties/lib/initializer.rb
@@ -125,11 +125,8 @@ def self.run(initializer = nil, config = nil)
if Rails.vendor_rails?
begin; require "rubygems"; rescue LoadError; return; end
- stubs = %w(rails activesupport activerecord actionpack actionmailer activeresource)
- stubs.reject! { |s| Gem.loaded_specs.key?(s) }
-
- stubs.each do |stub|
- Gem.loaded_specs[stub] = Gem::Specification.new do |s|
+ %w(rails activesupport activerecord actionpack actionmailer activeresource).each do |stub|
+ Gem.loaded_specs[stub] ||= Gem::Specification.new do |s|
s.name = stub
s.version = Rails::VERSION::STRING
s.loaded_from = ""
View
51 railties/test/initializer/check_ruby_version_test.rb
@@ -0,0 +1,51 @@
+require "initializer/test_helper"
+
+module InitializerTests
+ class PathsTest < ActiveSupport::TestCase
+ include ActiveSupport::Testing::Isolation
+
+ test "rails does not initialize with ruby version 1.8.1" do
+ assert_rails_does_not_boot "1.8.1"
+ end
+
+ test "rails initializes with ruby version 1.8.2" do
+ assert_rails_boots "1.8.2"
+ end
+
+ test "rails does not initialize with ruby version 1.8.3" do
+ assert_rails_does_not_boot "1.8.3"
+ end
+
+ test "rails initializes with ruby version 1.8.4" do
+ assert_rails_boots "1.8.4"
+ end
+
+ test "rails initializes with ruby version 1.8.5" do
+ assert_rails_boots "1.8.5"
+ end
+
+ test "rails initializes with ruby version 1.8.6" do
+ assert_rails_boots "1.8.6"
+ end
+
+ def set_ruby_version(version)
+ $-w = nil
+ Object.const_set(:RUBY_VERSION, version.freeze)
+ end
+
+ def assert_rails_boots(version)
+ set_ruby_version(version)
+ assert_nothing_raised "It appears that rails does not boot" do
+ Rails::Initializer.run { |c| c.frameworks = [] }
+ end
+ end
+
+ def assert_rails_does_not_boot(version)
+ set_ruby_version(version)
+ $stderr = File.open("/dev/null", "w")
+ assert_raises(SystemExit) do
+ Rails::Initializer.run { |c| c.frameworks = [] }
+ end
+ end
+ end
+end
View
85 railties/test/initializer/install_gem_spec_stubs_test.rb
@@ -0,0 +1,85 @@
+require "initializer/test_helper"
+
+module InitializerTests
+ class GemSpecStubsTest < ActiveSupport::TestCase
+ include ActiveSupport::Testing::Isolation
+
+ def setup
+ $stderr = StringIO.new
+ end
+
+ test "user has an old boot.rb (defined by having no Rails.vendor_rails?)" do
+ class << Rails
+ undef vendor_rails?
+ end
+
+ assert_stderr(/outdated/) do
+ assert_raises(SystemExit) do
+ Rails::Initializer.run { |c| c.frameworks = [] }
+ end
+ end
+ end
+
+ test "requires rubygems" do
+ Kernel.module_eval do
+ alias old_require require
+ def require(name)
+ $rubygems_required = true if name == "rubygems"
+ old_require(name)
+ end
+ end
+
+ Rails.vendor_rails = true
+ Rails::Initializer.run { |c| c.frameworks = [] }
+ assert $rubygems_required
+ end
+
+ test "does not fail if rubygems does not exist" do
+ Kernel.module_eval do
+ alias old_require require
+ def require(name)
+ raise LoadError if name == "rubygems"
+ old_require(name)
+ end
+ end
+
+ assert_nothing_raised do
+ Rails::Initializer.run { |c| c.frameworks = [] }
+ end
+ end
+
+ test "adds fake Rubygems stubs if a framework is not loaded in Rubygems and we've vendored" do
+ Rails.vendor_rails = true
+
+ Rails::Initializer.run { |c| c.frameworks = [] }
+
+ %w(rails activesupport activerecord actionpack actionmailer activeresource).each do |stub|
+ gem_spec = Gem.loaded_specs[stub]
+ assert_equal Gem::Version.new(Rails::VERSION::STRING), gem_spec.version
+ assert_equal stub, gem_spec.name
+ assert_equal "", gem_spec.loaded_from
+ end
+ end
+
+ test "doesn't replace gem specs that are already loaded" do
+ Rails.vendor_rails = true
+
+ Gem.loaded_specs["rails"] = Gem::Specification.new do |s|
+ s.name = "rails"
+ s.version = Rails::VERSION::STRING
+ s.loaded_from = "/foo/bar/baz"
+ end
+
+ Rails::Initializer.run { |c| c.frameworks = [] }
+
+ assert_equal "/foo/bar/baz", Gem.loaded_specs["rails"].loaded_from
+
+ %w(activesupport activerecord actionpack actionmailer activeresource).each do |stub|
+ gem_spec = Gem.loaded_specs[stub]
+ assert_equal Gem::Version.new(Rails::VERSION::STRING), gem_spec.version
+ assert_equal stub, gem_spec.name
+ assert_equal "", gem_spec.loaded_from
+ end
+ end
+ end
+end
View
21 railties/test/initializer/path_test.rb
@@ -1,21 +1,14 @@
-require 'abstract_unit'
-require 'active_support/ruby/shim'
-require 'initializer'
-
-RAILS_ROOT.replace File.join(File.dirname(__FILE__), "root")
-
-module Rails
- def self.vendor_rails? ; false ; end
-end
-
-# TODO: Can this be reset?
-Rails::Initializer.run do |config|
- config.frameworks = [:action_controller, :action_view, :action_mailer, :active_record]
-end
+require "initializer/test_helper"
class PathsTest < ActiveSupport::TestCase
include ActiveSupport::Testing::Isolation
+ def self.setup
+ Rails::Initializer.run do |config|
+ config.frameworks = [:action_controller, :action_view, :action_mailer, :active_record]
+ end
+ end
+
def setup
@paths = Rails::Initializer.default.config.paths
end
View
24 railties/test/initializer/test_helper.rb
@@ -0,0 +1,24 @@
+require 'abstract_unit'
+require 'active_support/ruby/shim'
+require 'initializer'
+
+RAILS_ROOT.replace File.join(File.dirname(__FILE__), "root")
+
+module Rails
+ class << self
+ attr_accessor :vendor_rails
+ def vendor_rails?() @vendor_rails end
+ end
+end
+
+class ActiveSupport::TestCase
+ def assert_stderr(match)
+ $stderr = StringIO.new
+ yield
+ $stderr.rewind
+ err = $stderr.read
+ assert_match match, err
+ ensure
+ $stderr = STDERR
+ end
+end
View
2 railties/test/initializer_test.rb
@@ -178,7 +178,7 @@ def assert_framework_path(path)
end
end
-require File.dirname(__FILE__) + '/plugin_test_helper'
+require 'plugin_test_helper'
class InitializerPluginLoadingTests < Test::Unit::TestCase
def setup
View
2 railties/test/plugin_test_helper.rb
@@ -4,7 +4,7 @@
require 'test/unit'
require 'active_support'
require 'initializer'
-require File.join(File.dirname(__FILE__), 'abstract_unit')
+require 'abstract_unit'
# We need to set RAILS_ROOT if it isn't already set
RAILS_ROOT = '.' unless defined?(RAILS_ROOT)

0 comments on commit 6d4b6a2

Please sign in to comment.
Something went wrong with that request. Please try again.