From 61036a60eb4677d0b1bc0f9c5ca47ad856301431 Mon Sep 17 00:00:00 2001 From: "John D. Hume" Date: Wed, 28 May 2008 23:35:56 -0400 Subject: [PATCH 001/200] Substitute value into validates_format_of message Signed-off-by: Michael Koziarski --- activerecord/lib/active_record/validations.rb | 2 +- activerecord/test/cases/validations_test.rb | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/activerecord/lib/active_record/validations.rb b/activerecord/lib/active_record/validations.rb index 0e150bddb26a0..c97aafb1261e1 100755 --- a/activerecord/lib/active_record/validations.rb +++ b/activerecord/lib/active_record/validations.rb @@ -692,7 +692,7 @@ def validates_format_of(*attr_names) raise(ArgumentError, "A regular expression must be supplied as the :with option of the configuration hash") unless configuration[:with].is_a?(Regexp) validates_each(attr_names, configuration) do |record, attr_name, value| - record.errors.add(attr_name, configuration[:message]) unless value.to_s =~ configuration[:with] + record.errors.add(attr_name, configuration[:message] % value) unless value.to_s =~ configuration[:with] end end diff --git a/activerecord/test/cases/validations_test.rb b/activerecord/test/cases/validations_test.rb index a4d9da480622f..7b71647d259ba 100755 --- a/activerecord/test/cases/validations_test.rb +++ b/activerecord/test/cases/validations_test.rb @@ -583,6 +583,12 @@ def test_validate_format_numeric assert_nil t.errors.on(:title) end + def test_validate_format_with_formatted_message + Topic.validates_format_of(:title, :with => /^Valid Title$/, :message => "can't be %s") + t = Topic.create(:title => 'Invalid title') + assert_equal "can't be Invalid title", t.errors.on(:title) + end + def test_validates_inclusion_of Topic.validates_inclusion_of( :title, :in => %w( a b c d e f g ) ) From b9a9b91a3e3b892ab72ff5c618181747d6b4be04 Mon Sep 17 00:00:00 2001 From: Michael Koziarski Date: Sat, 31 May 2008 16:39:32 -0700 Subject: [PATCH 002/200] Remove dead code, and the tests for it. --- .../lib/action_view/compiled_templates.rb | 69 ------- .../test/template/compiled_templates_test.rb | 192 ------------------ 2 files changed, 261 deletions(-) delete mode 100644 actionpack/lib/action_view/compiled_templates.rb delete mode 100644 actionpack/test/template/compiled_templates_test.rb diff --git a/actionpack/lib/action_view/compiled_templates.rb b/actionpack/lib/action_view/compiled_templates.rb deleted file mode 100644 index 5a286432e319b..0000000000000 --- a/actionpack/lib/action_view/compiled_templates.rb +++ /dev/null @@ -1,69 +0,0 @@ -module ActionView - - # CompiledTemplates modules hold methods that have been compiled. - # Templates are compiled into these methods so that they do not need to be - # read and parsed for each request. - # - # Each template may be compiled into one or more methods. Each method accepts a given - # set of parameters which is used to implement local assigns passing. - # - # To use a compiled template module, create a new instance and include it into the class - # in which you want the template to be rendered. - class CompiledTemplates < Module - attr_reader :method_names - - def initialize - @method_names = Hash.new do |hash, key| - hash[key] = "__compiled_method_#{(hash.length + 1)}" - end - @mtimes = {} - end - - # Return the full key for the given identifier and argument names - def full_key(identifier, arg_names) - [identifier, arg_names] - end - - # Return the selector for this method or nil if it has not been compiled - def selector(identifier, arg_names) - key = full_key(identifier, arg_names) - method_names.key?(key) ? method_names[key] : nil - end - alias :compiled? :selector - - # Return the time at which the method for the given identifier and argument names was compiled. - def mtime(identifier, arg_names) - @mtimes[full_key(identifier, arg_names)] - end - - # Compile the provided source code for the given argument names and with the given initial line number. - # The identifier should be unique to this source. - # - # The file_name, if provided will appear in backtraces. If not provided, the file_name defaults - # to the identifier. - # - # This method will return the selector for the compiled version of this method. - def compile_source(identifier, arg_names, source, initial_line_number = 0, file_name = nil) - file_name ||= identifier - name = method_names[full_key(identifier, arg_names)] - arg_desc = arg_names.empty? ? '' : "(#{arg_names * ', '})" - fake_file_name = "#{file_name}#{arg_desc}" # Include the arguments for this version (for now) - - method_def = wrap_source(name, arg_names, source) - - begin - module_eval(method_def, fake_file_name, initial_line_number) - @mtimes[full_key(identifier, arg_names)] = Time.now - rescue Exception => e # errors from compiled source - e.blame_file! identifier - raise - end - name - end - - # Wrap the provided source in a def ... end block. - def wrap_source(name, arg_names, source) - "def #{name}(#{arg_names * ', '})\n#{source}\nend" - end - end -end diff --git a/actionpack/test/template/compiled_templates_test.rb b/actionpack/test/template/compiled_templates_test.rb deleted file mode 100644 index 73e7ec1d76bca..0000000000000 --- a/actionpack/test/template/compiled_templates_test.rb +++ /dev/null @@ -1,192 +0,0 @@ -require 'abstract_unit' -require 'action_view/helpers/date_helper' -require 'action_view/compiled_templates' - -class CompiledTemplateTests < Test::Unit::TestCase - def setup - @ct = ActionView::CompiledTemplates.new - @v = Class.new - @v.send :include, @ct - @a = './test_compile_template_a.rhtml' - @b = './test_compile_template_b.rhtml' - @s = './test_compile_template_link.rhtml' - end - def teardown - [@a, @b, @s].each do |f| - FileUtils.rm(f) if File.exist?(f) || File.symlink?(f) - end - end - attr_reader :ct, :v - - def test_name_allocation - hi_world = ct.method_names['hi world'] - hi_sexy = ct.method_names['hi sexy'] - wish_upon_a_star = ct.method_names['I love seeing decent error messages'] - - assert_equal hi_world, ct.method_names['hi world'] - assert_equal hi_sexy, ct.method_names['hi sexy'] - assert_equal wish_upon_a_star, ct.method_names['I love seeing decent error messages'] - assert_equal 3, [hi_world, hi_sexy, wish_upon_a_star].uniq.length - end - - def test_wrap_source - assert_equal( - "def aliased_assignment(value)\nself.value = value\nend", - @ct.wrap_source(:aliased_assignment, [:value], 'self.value = value') - ) - - assert_equal( - "def simple()\nnil\nend", - @ct.wrap_source(:simple, [], 'nil') - ) - end - - def test_compile_source_single_method - selector = ct.compile_source('doubling method', [:a], 'a + a') - assert_equal 2, @v.new.send(selector, 1) - assert_equal 4, @v.new.send(selector, 2) - assert_equal -4, @v.new.send(selector, -2) - assert_equal 0, @v.new.send(selector, 0) - selector - end - - def test_compile_source_two_method - sel1 = test_compile_source_single_method # compile the method in the other test - sel2 = ct.compile_source('doubling method', [:a, :b], 'a + b + a + b') - assert_not_equal sel1, sel2 - - assert_equal 2, @v.new.send(sel1, 1) - assert_equal 4, @v.new.send(sel1, 2) - - assert_equal 6, @v.new.send(sel2, 1, 2) - assert_equal 32, @v.new.send(sel2, 15, 1) - end - - def test_mtime - t1 = Time.now - - test_compile_source_single_method - mtime = ct.mtime('doubling method', [:a]) - - assert mtime < Time.now - assert mtime > t1 - end - - uses_mocha 'test_compile_time' do - - def test_compile_time - t = Time.now - - File.open(@a, "w"){|f| f.puts @a} - File.open(@b, "w"){|f| f.puts @b} - # windows doesn't support symlinks (even under cygwin) - windows = (RUBY_PLATFORM =~ /win32/) - `ln -s #{@a} #{@s}` unless windows - - v = ActionView::Base.new - v.base_path = '.' - v.cache_template_loading = false - - ta = ActionView::Template.new(v, @a, false, {}) - tb = ActionView::Template.new(v, @b, false, {}) - ts = ActionView::Template.new(v, @s, false, {}) - - @handler_class = ActionView::Template.handler_class_for_extension(:rhtml) - @handler = @handler_class.new(v) - - # All templates were created at t+1 - File::Stat.any_instance.expects(:mtime).times(windows ? 2 : 3).returns(t + 1.second) - - # private methods template_changed_since? and compile_template? - # should report true for all since they have not been compiled - assert @handler.send(:template_changed_since?, @a, t) - assert @handler.send(:template_changed_since?, @b, t) - assert @handler.send(:template_changed_since?, @s, t) unless windows - - assert @handler.send(:compile_template?, ta) - assert @handler.send(:compile_template?, tb) - assert @handler.send(:compile_template?, ts) unless windows - - # All templates are rendered at t+2 - Time.expects(:now).times(windows ? 2 : 3).returns(t + 2.seconds) - v.send(:render_template, ta) - v.send(:render_template, tb) - v.send(:render_template, ts) unless windows - a_n = v.method_names[@a] - b_n = v.method_names[@b] - s_n = v.method_names[@s] unless windows - # all of the files have changed since last compile - assert @handler.compile_time[a_n] > t - assert @handler.compile_time[b_n] > t - assert @handler.compile_time[s_n] > t unless windows - - # private methods template_changed_since? and compile_template? - # should report false for all since none have changed since compile - File::Stat.any_instance.expects(:mtime).times(windows ? 6 : 12).returns(t + 1.second) - assert !@handler.send(:template_changed_since?, @a, @handler.compile_time[a_n]) - assert !@handler.send(:template_changed_since?, @b, @handler.compile_time[b_n]) - assert !@handler.send(:template_changed_since?, @s, @handler.compile_time[s_n]) unless windows - assert !@handler.send(:compile_template?, ta) - assert !@handler.send(:compile_template?, tb) - assert !@handler.send(:compile_template?, ts) unless windows - v.send(:render_template, ta) - v.send(:render_template, tb) - v.send(:render_template, ts) unless windows - # none of the files have changed since last compile - assert @handler.compile_time[a_n] < t + 3.seconds - assert @handler.compile_time[b_n] < t + 3.seconds - assert @handler.compile_time[s_n] < t + 3.seconds unless windows - - `rm #{@s}; ln -s #{@b} #{@s}` unless windows - # private methods template_changed_since? and compile_template? - # should report true for symlink since it has changed since compile - - # t + 3.seconds is for the symlink - File::Stat.any_instance.expects(:mtime).times(windows ? 6 : 9).returns( - *(windows ? [ t + 1.second, t + 1.second ] : - [ t + 1.second, t + 1.second, t + 3.second ]) * 3) - assert !@handler.send(:template_changed_since?, @a, @handler.compile_time[a_n]) - assert !@handler.send(:template_changed_since?, @b, @handler.compile_time[b_n]) - assert @handler.send(:template_changed_since?, @s, @handler.compile_time[s_n]) unless windows - assert !@handler.send(:compile_template?, ta) - assert !@handler.send(:compile_template?, tb) - assert @handler.send(:compile_template?, ts) unless windows - - # Only the symlink template gets rendered at t+3 - Time.stubs(:now).returns(t + 3.seconds) unless windows - v.send(:render_template, ta) - v.send(:render_template, tb) - v.send(:render_template, ts) unless windows - # the symlink has changed since last compile - assert @handler.compile_time[a_n] < t + 3.seconds - assert @handler.compile_time[b_n] < t + 3.seconds - assert_equal @handler.compile_time[s_n], t + 3.seconds unless windows - - FileUtils.touch @b - # private methods template_changed_since? and compile_template? - # should report true for symlink and file at end of symlink - # since it has changed since last compile - # - # t+4 is for @b and also for the file that @s points to, which is @b - File::Stat.any_instance.expects(:mtime).times(windows ? 6 : 12).returns( - *(windows ? [ t + 1.second, t + 4.seconds ] : - [ t + 1.second, t + 4.seconds, t + 3.second, t + 4.seconds ]) * 3) - assert !@handler.send(:template_changed_since?, @a, @handler.compile_time[a_n]) - assert @handler.send(:template_changed_since?, @b, @handler.compile_time[b_n]) - assert @handler.send(:template_changed_since?, @s, @handler.compile_time[s_n]) unless windows - assert !@handler.send(:compile_template?, ta) - assert @handler.send(:compile_template?, tb) - assert @handler.send(:compile_template?, ts) unless windows - - Time.expects(:now).times(windows ? 1 : 2).returns(t + 5.seconds) - v.send(:render_template, ta) - v.send(:render_template, tb) - v.send(:render_template, ts) unless windows - # the file at the end of the symlink has changed since last compile - # both the symlink and the file at the end of it should be recompiled - assert @handler.compile_time[a_n] < t + 5.seconds - assert_equal @handler.compile_time[b_n], t + 5.seconds - assert_equal @handler.compile_time[s_n], t + 5.seconds unless windows - end - end -end From 4210d85a3f3ce4e980f473e9ed2becb84b58363b Mon Sep 17 00:00:00 2001 From: Jonathan Viney Date: Mon, 2 Jun 2008 15:00:15 +1200 Subject: [PATCH 003/200] Ensure Associations#sum returns 0 when no rows are returned. [#295 state:resolved] Signed-off-by: Pratik Naik --- activerecord/lib/active_record/calculations.rb | 6 +++--- activerecord/test/cases/calculations_test.rb | 3 ++- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/activerecord/lib/active_record/calculations.rb b/activerecord/lib/active_record/calculations.rb index 10e8330d1c5dd..889b7845fb82e 100644 --- a/activerecord/lib/active_record/calculations.rb +++ b/activerecord/lib/active_record/calculations.rb @@ -71,7 +71,7 @@ def maximum(column_name, options = {}) # # Person.sum('age') def sum(column_name, options = {}) - calculate(:sum, column_name, options) || 0 + calculate(:sum, column_name, options) end # This calculates aggregate values in the given column. Methods for count, sum, average, minimum, and maximum have been added as shortcuts. @@ -265,8 +265,8 @@ def column_for(field) def type_cast_calculated_value(value, column, operation = nil) operation = operation.to_s.downcase case operation - when 'count' then value.to_i - when 'avg' then value && value.to_f + when 'count', 'sum' then value.to_i + when 'avg' then value && value.to_f else column ? column.type_cast(value) : value end end diff --git a/activerecord/test/cases/calculations_test.rb b/activerecord/test/cases/calculations_test.rb index a87fc26905f76..5cf655f7b72ea 100644 --- a/activerecord/test/cases/calculations_test.rb +++ b/activerecord/test/cases/calculations_test.rb @@ -99,6 +99,7 @@ def test_should_sum_field_with_conditions def test_should_return_zero_if_sum_conditions_return_nothing assert_equal 0, Account.sum(:credit_limit, :conditions => '1 = 2') + assert_equal 0, companies(:rails_core).companies.sum(:id, :conditions => '1 = 2') end def test_should_group_by_summed_field_with_conditions @@ -266,6 +267,6 @@ def test_count_with_too_many_parameters_raises end def test_should_sum_expression - assert_equal "636", Account.sum("2 * credit_limit") + assert_equal 636, Account.sum("2 * credit_limit") end end From 14a65cd982161364028da312f415c1ebe0bcd5ac Mon Sep 17 00:00:00 2001 From: Marcos Tapajos Date: Mon, 2 Jun 2008 09:47:53 -0500 Subject: [PATCH 004/200] Fixed changelog merge. Signed-off-by: Joshua Peek --- activesupport/CHANGELOG | 1 - 1 file changed, 1 deletion(-) diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG index 690bb987dcc77..15c25debf4e75 100644 --- a/activesupport/CHANGELOG +++ b/activesupport/CHANGELOG @@ -166,7 +166,6 @@ * Introduce ActiveSupport::TimeWithZone, for wrapping Time instances with a TimeZone. Introduce instance methods to Time for creating TimeWithZone instances, and class methods for managing a global time zone. [Geoff Buesing] ->>>>>>> .r8815 * Replace non-dst-aware TimeZone class with dst-aware class from tzinfo_timezone plugin. TimeZone#adjust and #unadjust are no longer available; tzinfo gem must now be present in order to perform time zone calculations, via #local_to_utc and #utc_to_local methods. [Geoff Buesing] * Extract ActiveSupport::Callbacks from Active Record, test case setup and teardown, and ActionController::Dispatcher. #10727 [Josh Peek] From 185fe2e9cce737d69d3b47a656f3651ce152c0c1 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Mon, 2 Jun 2008 09:54:36 -0500 Subject: [PATCH 005/200] In 9c4f003, gem installation quotes versions. Do the same for unpack and update tests to reflect the change. --- railties/lib/rails/gem_dependency.rb | 16 ++++++++-------- railties/test/gem_dependency_test.rb | 16 ++++++++-------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/railties/lib/rails/gem_dependency.rb b/railties/lib/rails/gem_dependency.rb index 9f088a18dd0d3..30bdf416fca15 100644 --- a/railties/lib/rails/gem_dependency.rb +++ b/railties/lib/rails/gem_dependency.rb @@ -39,7 +39,7 @@ def add_load_paths @load_paths_added = true rescue Gem::LoadError end - + def dependencies all_dependencies = specification.dependencies.map do |dependency| GemDependency.new(dependency.name, :requirement => dependency.version_requirements) @@ -47,7 +47,7 @@ def dependencies all_dependencies += all_dependencies.map(&:dependencies).flatten all_dependencies.uniq end - + def gem_dir(base_directory) File.join(base_directory, specification.full_name) end @@ -78,13 +78,13 @@ def install puts cmd puts %x(#{cmd}) end - + def unpack_to(directory) FileUtils.mkdir_p directory Dir.chdir directory do Gem::GemRunner.new.run(unpack_command) end - + # copy the gem's specification into GEMDIR/.specification so that # we can access information about the gem on deployment systems # without having the gem installed @@ -103,7 +103,7 @@ def ==(other) def specification @spec ||= Gem.source_index.search(Gem::Dependency.new(@name, @requirement)).sort_by { |s| s.version }.last end - + def gem_command RUBY_PLATFORM =~ /win32/ ? 'gem.bat' : 'gem' end @@ -114,11 +114,11 @@ def install_command cmd << "--source" << @source if @source cmd end - + def unpack_command cmd = %w(unpack) << @name - cmd << "--version" << "#{@requirement.to_s}" if @requirement + cmd << "--version" << %("#{@requirement.to_s}") if @requirement cmd end end -end \ No newline at end of file +end diff --git a/railties/test/gem_dependency_test.rb b/railties/test/gem_dependency_test.rb index 3ae01893277e6..b5946aa7b834b 100644 --- a/railties/test/gem_dependency_test.rb +++ b/railties/test/gem_dependency_test.rb @@ -10,13 +10,13 @@ def setup @gem = Rails::GemDependency.new "hpricot" @gem_with_source = Rails::GemDependency.new "hpricot", :source => "http://code.whytheluckystiff.net" @gem_with_version = Rails::GemDependency.new "hpricot", :version => "= 0.6" - @gem_with_lib = Rails::GemDependency.new "aws-s3", :lib => "aws/s3" + @gem_with_lib = Rails::GemDependency.new "aws-s3", :lib => "aws/s3" end def test_configuration_adds_gem_dependency config = Rails::Configuration.new config.gem "aws-s3", :lib => "aws/s3", :version => "0.4.0" - assert_equal [["install", "aws-s3", "--version", "= 0.4.0"]], config.gems.collect(&:install_command) + assert_equal [["install", "aws-s3", "--version", '"= 0.4.0"']], config.gems.collect(&:install_command) end def test_gem_creates_install_command @@ -28,7 +28,7 @@ def test_gem_with_source_creates_install_command end def test_gem_with_version_creates_install_command - assert_equal ["install", "hpricot", "--version", "= 0.6"], @gem_with_version.install_command + assert_equal ["install", "hpricot", "--version", '"= 0.6"'], @gem_with_version.install_command end def test_gem_creates_unpack_command @@ -36,26 +36,26 @@ def test_gem_creates_unpack_command end def test_gem_with_version_unpack_install_command - assert_equal ["unpack", "hpricot", "--version", "= 0.6"], @gem_with_version.unpack_command + assert_equal ["unpack", "hpricot", "--version", '"= 0.6"'], @gem_with_version.unpack_command end def test_gem_adds_load_paths @gem.expects(:gem).with(@gem.name) @gem.add_load_paths end - + def test_gem_with_version_adds_load_paths @gem_with_version.expects(:gem).with(@gem_with_version.name, @gem_with_version.requirement.to_s) @gem_with_version.add_load_paths end - + def test_gem_loading @gem.expects(:gem).with(@gem.name) @gem.expects(:require).with(@gem.name) @gem.add_load_paths @gem.load end - + def test_gem_with_lib_loading @gem_with_lib.expects(:gem).with(@gem_with_lib.name) @gem_with_lib.expects(:require).with(@gem_with_lib.lib) @@ -63,4 +63,4 @@ def test_gem_with_lib_loading @gem_with_lib.load end end -end \ No newline at end of file +end From 714d42d1a6140ac4f6fc478bf1766d2b0aedb251 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Mon, 2 Jun 2008 10:40:01 -0500 Subject: [PATCH 006/200] Fixed initializer tests by stubbing out gems dependencies check. --- railties/lib/initializer.rb | 11 ++++++---- railties/test/initializer_test.rb | 34 ++++++++++++++++--------------- 2 files changed, 25 insertions(+), 20 deletions(-) diff --git a/railties/lib/initializer.rb b/railties/lib/initializer.rb index bdadfeea8f3c8..9e6e02e8e01aa 100644 --- a/railties/lib/initializer.rb +++ b/railties/lib/initializer.rb @@ -79,7 +79,10 @@ class Initializer # The set of loaded plugins. attr_reader :loaded_plugins - + + # Whether or not all the gem dependencies have been met + attr_reader :gems_dependencies_loaded + # Runs the initializer. By default, this will invoke the #process method, # which simply executes all of the initialization routines. Alternately, # you can specify explicitly which initialization routine you want: @@ -307,7 +310,7 @@ def load_environment end def load_observers - if @gems_dependencies_loaded && configuration.frameworks.include?(:active_record) + if gems_dependencies_loaded && configuration.frameworks.include?(:active_record) ActiveRecord::Base.instantiate_observers end end @@ -463,7 +466,7 @@ def initialize_framework_settings # Fires the user-supplied after_initialize block (Configuration#after_initialize) def after_initialize - if @gems_dependencies_loaded + if gems_dependencies_loaded configuration.after_initialize_blocks.each do |block| block.call end @@ -471,7 +474,7 @@ def after_initialize end def load_application_initializers - if @gems_dependencies_loaded + if gems_dependencies_loaded Dir["#{configuration.root_path}/config/initializers/**/*.rb"].sort.each do |initializer| load(initializer) end diff --git a/railties/test/initializer_test.rb b/railties/test/initializer_test.rb index 0df0164ca673a..efce4f292d660 100644 --- a/railties/test/initializer_test.rb +++ b/railties/test/initializer_test.rb @@ -38,27 +38,28 @@ def setup end config.after_initialize do $test_after_initialize_block2 = "congratulations" - end + end assert_nil $test_after_initialize_block1 - assert_nil $test_after_initialize_block2 + assert_nil $test_after_initialize_block2 + Rails::Initializer.any_instance.expects(:gems_dependencies_loaded).returns(true) Rails::Initializer.run(:after_initialize, config) end - + def teardown $test_after_initialize_block1 = nil - $test_after_initialize_block2 = nil + $test_after_initialize_block2 = nil end def test_should_have_called_the_first_after_initialize_block assert_equal "success", $test_after_initialize_block1 end - + def test_should_have_called_the_second_after_initialize_block assert_equal "congratulations", $test_after_initialize_block2 end end - + class Initializer_after_initialize_with_no_block_environment_Test < Test::Unit::TestCase def setup @@ -69,15 +70,16 @@ def setup config.after_initialize # don't pass a block, this is what we're testing! config.after_initialize do $test_after_initialize_block2 = "congratulations" - end + end assert_nil $test_after_initialize_block1 + Rails::Initializer.any_instance.expects(:gems_dependencies_loaded).returns(true) Rails::Initializer.run(:after_initialize, config) end def teardown $test_after_initialize_block1 = nil - $test_after_initialize_block2 = nil + $test_after_initialize_block2 = nil end def test_should_have_called_the_first_after_initialize_block @@ -95,7 +97,7 @@ class ConfigurationFrameworkPathsTests < Test::Unit::TestCase def setup @config = Rails::Configuration.new @config.frameworks.clear - + File.stubs(:directory?).returns(true) @config.stubs(:framework_root_path).returns('') end @@ -112,7 +114,7 @@ def test_minimal def test_actioncontroller_or_actionview_add_actionpack @config.frameworks << :action_controller assert_framework_path '/actionpack/lib' - + @config.frameworks = [:action_view] assert_framework_path '/actionpack/lib' end @@ -204,22 +206,22 @@ def test_registering_a_plugin_name_that_does_not_exist_raises_a_load_error load_plugins! end end - + def test_should_ensure_all_loaded_plugins_load_paths_are_added_to_the_load_path only_load_the_following_plugins! [:stubby, :acts_as_chunky_bacon] @initializer.add_plugin_load_paths - + assert $LOAD_PATH.include?(File.join(plugin_fixture_path('default/stubby'), 'lib')) assert $LOAD_PATH.include?(File.join(plugin_fixture_path('default/acts/acts_as_chunky_bacon'), 'lib')) end - + private - + def load_plugins! @initializer.add_plugin_load_paths @initializer.load_plugins end end - -end + +end From 952ec79bec313e0001adfc8c86f7970448d32db9 Mon Sep 17 00:00:00 2001 From: Cheah Chu Yeow Date: Mon, 2 Jun 2008 01:42:38 -0700 Subject: [PATCH 007/200] Faster Hash#slice that doesn't use Enumerable#include?. Signed-off-by: Jeremy Kemper --- activesupport/lib/active_support/core_ext/hash/slice.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/activesupport/lib/active_support/core_ext/hash/slice.rb b/activesupport/lib/active_support/core_ext/hash/slice.rb index 6fe5e05330e28..44b6964c69a8e 100644 --- a/activesupport/lib/active_support/core_ext/hash/slice.rb +++ b/activesupport/lib/active_support/core_ext/hash/slice.rb @@ -15,7 +15,9 @@ module Slice # Returns a new hash with only the given keys. def slice(*keys) allowed = Set.new(respond_to?(:convert_key) ? keys.map { |key| convert_key(key) } : keys) - reject { |key,| !allowed.include?(key) } + hash = {} + allowed.each { |k| hash[k] = self[k] } + hash end # Replaces the hash with only the given keys. From bd75a722a2e9f979bfd1b1d89442e4dd6f3e3af7 Mon Sep 17 00:00:00 2001 From: Pratik Naik Date: Mon, 2 Jun 2008 20:40:25 +0100 Subject: [PATCH 008/200] Ensure AR#sum result is typecasted properly --- activerecord/lib/active_record/calculations.rb | 5 +++-- activerecord/test/cases/calculations_test.rb | 5 +++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/activerecord/lib/active_record/calculations.rb b/activerecord/lib/active_record/calculations.rb index 889b7845fb82e..caa8c539d5ff6 100644 --- a/activerecord/lib/active_record/calculations.rb +++ b/activerecord/lib/active_record/calculations.rb @@ -265,8 +265,9 @@ def column_for(field) def type_cast_calculated_value(value, column, operation = nil) operation = operation.to_s.downcase case operation - when 'count', 'sum' then value.to_i - when 'avg' then value && value.to_f + when 'count' then value.to_i + when 'sum' then value =~ /\./ ? value.to_f : value.to_i + when 'avg' then value && value.to_f else column ? column.type_cast(value) : value end end diff --git a/activerecord/test/cases/calculations_test.rb b/activerecord/test/cases/calculations_test.rb index 5cf655f7b72ea..aefb13ea9ab6b 100644 --- a/activerecord/test/cases/calculations_test.rb +++ b/activerecord/test/cases/calculations_test.rb @@ -102,6 +102,11 @@ def test_should_return_zero_if_sum_conditions_return_nothing assert_equal 0, companies(:rails_core).companies.sum(:id, :conditions => '1 = 2') end + def test_sum_should_return_valid_values_for_decimals + NumericData.create(:bank_balance => 19.83) + assert_equal 19.83, NumericData.sum(:bank_balance) + end + def test_should_group_by_summed_field_with_conditions c = Account.sum(:credit_limit, :conditions => 'firm_id > 1', :group => :firm_id) From a855ab89b7aae7cb451611301a1e83495f647f8a Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Mon, 2 Jun 2008 17:42:10 -0500 Subject: [PATCH 009/200] Added a test for Gzip --- activesupport/test/gzip_test.rb | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 activesupport/test/gzip_test.rb diff --git a/activesupport/test/gzip_test.rb b/activesupport/test/gzip_test.rb new file mode 100644 index 0000000000000..2a24c0bd0d046 --- /dev/null +++ b/activesupport/test/gzip_test.rb @@ -0,0 +1,7 @@ +require 'abstract_unit' + +class GzipTest < Test::Unit::TestCase + def test_compress_should_decompress_to_the_same_value + assert_equal "Hello World", ActiveSupport::Gzip.decompress(ActiveSupport::Gzip.compress("Hello World")) + end +end \ No newline at end of file From 4b4aa8f6e08ba2aa2ddce56f1d5b631a78eeef6c Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Mon, 2 Jun 2008 18:43:08 -0500 Subject: [PATCH 010/200] AR can be disabled, new_rails_defaults.rb should check [#303 state:resolved] (Jesper Hvirring Henriksen) --- railties/configs/initializers/new_rails_defaults.rb | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/railties/configs/initializers/new_rails_defaults.rb b/railties/configs/initializers/new_rails_defaults.rb index 5e60c0ade4fe6..78e0117cc47e1 100644 --- a/railties/configs/initializers/new_rails_defaults.rb +++ b/railties/configs/initializers/new_rails_defaults.rb @@ -1,11 +1,13 @@ # These settings change the behavior of Rails 2 apps and will be defaults # for Rails 3. You can remove this initializer when Rails 3 is released. -# Include Active Record class name as root for JSON serialized output. -ActiveRecord::Base.include_root_in_json = true +if defined?(ActiveRecord) + # Include Active Record class name as root for JSON serialized output. + ActiveRecord::Base.include_root_in_json = true -# Store the full class name (including module namespace) in STI type column. -ActiveRecord::Base.store_full_sti_class = true + # Store the full class name (including module namespace) in STI type column. + ActiveRecord::Base.store_full_sti_class = true +end # Use ISO 8601 format for JSON serialized times and dates. ActiveSupport.use_standard_json_time_format = true From 29641ff05ad1beb659582a3c4347c1395f940285 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Mon, 2 Jun 2008 19:00:25 -0500 Subject: [PATCH 011/200] Fixed the brokeness from 952ec79bec313e0001adfc8c86f7970448d32db9 --- activesupport/lib/active_support/core_ext/hash/slice.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/activesupport/lib/active_support/core_ext/hash/slice.rb b/activesupport/lib/active_support/core_ext/hash/slice.rb index 44b6964c69a8e..1b2c8f63e32d9 100644 --- a/activesupport/lib/active_support/core_ext/hash/slice.rb +++ b/activesupport/lib/active_support/core_ext/hash/slice.rb @@ -16,7 +16,7 @@ module Slice def slice(*keys) allowed = Set.new(respond_to?(:convert_key) ? keys.map { |key| convert_key(key) } : keys) hash = {} - allowed.each { |k| hash[k] = self[k] } + allowed.each { |k| hash[k] = self[k] if has_key?(k) } hash end From 92050f6c6f586b2a73aeb61da4f41b9822bbcf6d Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Mon, 2 Jun 2008 21:02:26 -0500 Subject: [PATCH 012/200] Ensure Rack processor reads CGI output_cookies for the session cookie. --- .../lib/action_controller/dispatcher.rb | 2 +- .../lib/action_controller/rack_process.rb | 10 +++- actionpack/test/controller/rack_test.rb | 56 +++++++++++++++++-- 3 files changed, 60 insertions(+), 8 deletions(-) diff --git a/actionpack/lib/action_controller/dispatcher.rb b/actionpack/lib/action_controller/dispatcher.rb index b40f1ba9beb43..7162fb8b1f274 100644 --- a/actionpack/lib/action_controller/dispatcher.rb +++ b/actionpack/lib/action_controller/dispatcher.rb @@ -125,7 +125,7 @@ def dispatch_cgi(cgi, session_options) def call(env) @request = RackRequest.new(env) - @response = RackResponse.new + @response = RackResponse.new(@request) dispatch end diff --git a/actionpack/lib/action_controller/rack_process.rb b/actionpack/lib/action_controller/rack_process.rb index 16625519b6a62..d5fb78c44d3ba 100644 --- a/actionpack/lib/action_controller/rack_process.rb +++ b/actionpack/lib/action_controller/rack_process.rb @@ -4,6 +4,7 @@ module ActionController #:nodoc: class RackRequest < AbstractRequest #:nodoc: attr_accessor :env, :session_options + attr_reader :cgi class SessionFixationAttempt < StandardError #:nodoc: end @@ -199,7 +200,8 @@ def unescape(s) class RackResponse < AbstractResponse #:nodoc: attr_accessor :status - def initialize + def initialize(request) + @request = request @writer = lambda { |x| @body << x } @block = nil super() @@ -270,9 +272,9 @@ def normalize_headers(options = "text/html") else cookies << cookie.to_s end - @output_cookies.each { |c| cookies << c.to_s } if @output_cookies + @request.cgi.output_cookies.each { |c| cookies << c.to_s } if @request.cgi.output_cookies - headers['Set-Cookie'] = [headers['Set-Cookie'], cookies].compact.join("\n") + headers['Set-Cookie'] = [headers['Set-Cookie'], cookies].flatten.compact end options.each { |k,v| headers[k] = v } @@ -283,6 +285,8 @@ def normalize_headers(options = "text/html") end class CGIWrapper < ::CGI + attr_reader :output_cookies + def initialize(request, *args) @request = request @args = *args diff --git a/actionpack/test/controller/rack_test.rb b/actionpack/test/controller/rack_test.rb index cd4151783ed9f..026b0195d1e20 100644 --- a/actionpack/test/controller/rack_test.rb +++ b/actionpack/test/controller/rack_test.rb @@ -3,7 +3,36 @@ class BaseRackTest < Test::Unit::TestCase def setup - @env = {"HTTP_MAX_FORWARDS"=>"10", "SERVER_NAME"=>"glu.ttono.us:8007", "FCGI_ROLE"=>"RESPONDER", "HTTP_X_FORWARDED_HOST"=>"glu.ttono.us", "HTTP_ACCEPT_ENCODING"=>"gzip, deflate", "HTTP_USER_AGENT"=>"Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/312.5.1 (KHTML, like Gecko) Safari/312.3.1", "PATH_INFO"=>"", "HTTP_ACCEPT_LANGUAGE"=>"en", "HTTP_HOST"=>"glu.ttono.us:8007", "SERVER_PROTOCOL"=>"HTTP/1.1", "REDIRECT_URI"=>"/dispatch.fcgi", "SCRIPT_NAME"=>"/dispatch.fcgi", "SERVER_ADDR"=>"207.7.108.53", "REMOTE_ADDR"=>"207.7.108.53", "SERVER_SOFTWARE"=>"lighttpd/1.4.5", "HTTP_COOKIE"=>"_session_id=c84ace84796670c052c6ceb2451fb0f2; is_admin=yes", "HTTP_X_FORWARDED_SERVER"=>"glu.ttono.us", "REQUEST_URI"=>"/admin", "DOCUMENT_ROOT"=>"/home/kevinc/sites/typo/public", "SERVER_PORT"=>"8007", "QUERY_STRING"=>"", "REMOTE_PORT"=>"63137", "GATEWAY_INTERFACE"=>"CGI/1.1", "HTTP_X_FORWARDED_FOR"=>"65.88.180.234", "HTTP_ACCEPT"=>"*/*", "SCRIPT_FILENAME"=>"/home/kevinc/sites/typo/public/dispatch.fcgi", "REDIRECT_STATUS"=>"200", "REQUEST_METHOD"=>"GET"} + @env = { + "HTTP_MAX_FORWARDS" => "10", + "SERVER_NAME" => "glu.ttono.us:8007", + "FCGI_ROLE" => "RESPONDER", + "HTTP_X_FORWARDED_HOST" => "glu.ttono.us", + "HTTP_ACCEPT_ENCODING" => "gzip, deflate", + "HTTP_USER_AGENT" => "Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en)", + "PATH_INFO" => "", + "HTTP_ACCEPT_LANGUAGE" => "en", + "HTTP_HOST" => "glu.ttono.us:8007", + "SERVER_PROTOCOL" => "HTTP/1.1", + "REDIRECT_URI" => "/dispatch.fcgi", + "SCRIPT_NAME" => "/dispatch.fcgi", + "SERVER_ADDR" => "207.7.108.53", + "REMOTE_ADDR" => "207.7.108.53", + "SERVER_SOFTWARE" => "lighttpd/1.4.5", + "HTTP_COOKIE" => "_session_id=c84ace84796670c052c6ceb2451fb0f2; is_admin=yes", + "HTTP_X_FORWARDED_SERVER" => "glu.ttono.us", + "REQUEST_URI" => "/admin", + "DOCUMENT_ROOT" => "/home/kevinc/sites/typo/public", + "SERVER_PORT" => "8007", + "QUERY_STRING" => "", + "REMOTE_PORT" => "63137", + "GATEWAY_INTERFACE" => "CGI/1.1", + "HTTP_X_FORWARDED_FOR" => "65.88.180.234", + "HTTP_ACCEPT" => "*/*", + "SCRIPT_FILENAME" => "/home/kevinc/sites/typo/public/dispatch.fcgi", + "REDIRECT_STATUS" => "200", + "REQUEST_METHOD" => "GET" + } # some Nokia phone browsers omit the space after the semicolon separator. # some developers have grown accustomed to using comma in cookie values. @alt_cookie_fmt_request_hash = {"HTTP_COOKIE"=>"_session_id=c84ace847,96670c052c6ceb2451fb0f2;is_admin=yes"} @@ -118,7 +147,7 @@ def test_body_should_be_rewound class RackResponseTest < BaseRackTest def setup super - @response = ActionController::RackResponse.new + @response = ActionController::RackResponse.new(@request) @output = StringIO.new('') end @@ -127,7 +156,7 @@ def test_simple_output status, headers, body = @response.out(@output) assert_equal 200, status - assert_equal({"Content-Type" => "text/html", "Cache-Control" => "no-cache", "Set-Cookie" => ""}, headers) + assert_equal({"Content-Type" => "text/html", "Cache-Control" => "no-cache", "Set-Cookie" => []}, headers) parts = [] body.each { |part| parts << part } @@ -141,10 +170,29 @@ def test_streaming_block status, headers, body = @response.out(@output) assert_equal 200, status - assert_equal({"Content-Type" => "text/html", "Cache-Control" => "no-cache", "Set-Cookie" => ""}, headers) + assert_equal({"Content-Type" => "text/html", "Cache-Control" => "no-cache", "Set-Cookie" => []}, headers) parts = [] body.each { |part| parts << part } assert_equal ["0", "1", "2", "3", "4"], parts end + + def test_set_session_cookie + cookie = CGI::Cookie.new({"name" => "name", "value" => "Josh"}) + @request.cgi.send :instance_variable_set, '@output_cookies', [cookie] + + @response.body = "Hello, World!" + + status, headers, body = @response.out(@output) + assert_equal 200, status + assert_equal({ + "Content-Type" => "text/html", + "Cache-Control" => "no-cache", + "Set-Cookie" => ["name=Josh; path="] + }, headers) + + parts = [] + body.each { |part| parts << part } + assert_equal ["Hello, World!"], parts + end end From 442d2f1032df6744c11f21b5ec6281c77e39c242 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Mon, 2 Jun 2008 21:55:42 -0500 Subject: [PATCH 013/200] Fixed that RailsInfoController wasnt considering all requests local in development mode (Edgard Castro) [#310 state:resolved] --- railties/CHANGELOG | 5 +++++ railties/builtin/rails_info/rails/info_controller.rb | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/railties/CHANGELOG b/railties/CHANGELOG index fe517755ef549..4d1c86e643b2e 100644 --- a/railties/CHANGELOG +++ b/railties/CHANGELOG @@ -1,3 +1,8 @@ +*Edge* + +* Fixed that RailsInfoController wasn't considering all requests local in development mode (Edgard Castro) [#310 state:resolved] + + *2.1.0 (May 31st, 2008)* * script/dbconsole fires up the command-line database client. #102 [Steve Purcell] diff --git a/railties/builtin/rails_info/rails/info_controller.rb b/railties/builtin/rails_info/rails/info_controller.rb index 39f8b1f120219..05745d606d2a9 100644 --- a/railties/builtin/rails_info/rails/info_controller.rb +++ b/railties/builtin/rails_info/rails/info_controller.rb @@ -1,6 +1,6 @@ class Rails::InfoController < ActionController::Base def properties - if local_request? + if consider_all_requests_local || local_request? render :inline => Rails::Info.to_html else render :text => '

For security purposes, this information is only available to local requests.

', :status => 500 From e7a305f08d8f72f81449e1d8934fba31f038ad88 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Mon, 2 Jun 2008 21:58:42 -0500 Subject: [PATCH 014/200] Fixed Base#exists? to check status code as integer [#299 state:resolved] (Wes Oldenbeuving) --- activeresource/CHANGELOG | 5 +++++ activeresource/lib/active_resource/base.rb | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/activeresource/CHANGELOG b/activeresource/CHANGELOG index e124e40dd1336..673e20de28658 100644 --- a/activeresource/CHANGELOG +++ b/activeresource/CHANGELOG @@ -1,3 +1,8 @@ +*Edge* + +* Fixed Base#exists? to check status code as integer [#299 state:resolved] (Wes Oldenbeuving) + + *2.1.0 (May 31st, 2008)* * Fixed response logging to use length instead of the entire thing (seangeo) [#27] diff --git a/activeresource/lib/active_resource/base.rb b/activeresource/lib/active_resource/base.rb index 463ee9f1e724e..55dacfdf06929 100644 --- a/activeresource/lib/active_resource/base.rb +++ b/activeresource/lib/active_resource/base.rb @@ -538,7 +538,7 @@ def exists?(id, options = {}) prefix_options, query_options = split_options(options[:params]) path = element_path(id, prefix_options, query_options) response = connection.head(path, headers) - response.code == 200 + response.code.to_i == 200 end # id && !find_single(id, options).nil? rescue ActiveResource::ResourceNotFound From 64fea9c45c515496bb60df4a1e141f44cac4d158 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Mon, 2 Jun 2008 22:02:43 -0500 Subject: [PATCH 015/200] Fixed Date#end_of_quarter to not blow up on May 31st [#289 state:resolved] (Danger) --- activesupport/CHANGELOG | 5 +++++ .../lib/active_support/core_ext/date/calculations.rb | 2 +- activesupport/test/core_ext/date_ext_test.rb | 1 + 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG index 15c25debf4e75..a1a3c7ac2ba2c 100644 --- a/activesupport/CHANGELOG +++ b/activesupport/CHANGELOG @@ -1,3 +1,8 @@ +*Edge* + +* Fixed Date#end_of_quarter to not blow up on May 31st [#289 state:resolved] (Danger) + + *2.1.0 (May 31st, 2008)* * TimeZone#to_s shows offset as GMT instead of UTC, because GMT will be more familiar to end users (see time zone selects used by Windows OS, google.com and yahoo.com.) Reverts [8370] [Geoff Buesing] diff --git a/activesupport/lib/active_support/core_ext/date/calculations.rb b/activesupport/lib/active_support/core_ext/date/calculations.rb index 1e2dbf118e3ab..b5180c9592597 100644 --- a/activesupport/lib/active_support/core_ext/date/calculations.rb +++ b/activesupport/lib/active_support/core_ext/date/calculations.rb @@ -184,7 +184,7 @@ def beginning_of_quarter # Returns a new Date/DateTime representing the end of the quarter (last day of march, june, september, december; DateTime objects will have time set to 23:59:59) def end_of_quarter - change(:month => [3, 6, 9, 12].detect { |m| m >= self.month }).end_of_month + beginning_of_month.change(:month => [3, 6, 9, 12].detect { |m| m >= self.month }).end_of_month end alias :at_end_of_quarter :end_of_quarter diff --git a/activesupport/test/core_ext/date_ext_test.rb b/activesupport/test/core_ext/date_ext_test.rb index 5925ae3a61f8e..e1d2f0e7f213d 100644 --- a/activesupport/test/core_ext/date_ext_test.rb +++ b/activesupport/test/core_ext/date_ext_test.rb @@ -76,6 +76,7 @@ def test_end_of_quarter assert_equal Date.new(2008,3,31), Date.new(2008,3,31).end_of_quarter assert_equal Date.new(2008,12,31), Date.new(2008,10,8).end_of_quarter assert_equal Date.new(2008,6,30), Date.new(2008,4,14).end_of_quarter + assert_equal Date.new(2008,6,30), Date.new(2008,5,31).end_of_quarter assert_equal Date.new(2008,9,30), Date.new(2008,8,21).end_of_quarter end From da91450e687fe9faa7b0575062c2b2aacc261f68 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Mon, 2 Jun 2008 22:05:27 -0500 Subject: [PATCH 016/200] Added tests [#279 state:resolved] (Nicholas Schlueter) --- activesupport/test/core_ext/array_ext_test.rb | 5 +++++ activesupport/test/core_ext/bigdecimal.rb | 1 + activesupport/test/core_ext/date_ext_test.rb | 4 ++++ 3 files changed, 10 insertions(+) diff --git a/activesupport/test/core_ext/array_ext_test.rb b/activesupport/test/core_ext/array_ext_test.rb index ccab0f70d0092..20bc5dfcb7568 100644 --- a/activesupport/test/core_ext/array_ext_test.rb +++ b/activesupport/test/core_ext/array_ext_test.rb @@ -222,6 +222,11 @@ def test_to_xml_with_block assert xml.include?(%(2)), xml end + + def test_to_xml_with_empty + xml = [].to_xml + assert_match /type="array"\/>/, xml + end end class ArrayExtractOptionsTests < Test::Unit::TestCase diff --git a/activesupport/test/core_ext/bigdecimal.rb b/activesupport/test/core_ext/bigdecimal.rb index 0417a2bca0816..9faad9146f292 100644 --- a/activesupport/test/core_ext/bigdecimal.rb +++ b/activesupport/test/core_ext/bigdecimal.rb @@ -5,5 +5,6 @@ def test_to_yaml assert_equal("--- 100000.30020320320000000000000000000000000000001\n", BigDecimal.new('100000.30020320320000000000000000000000000000001').to_yaml) assert_equal("--- .Inf\n", BigDecimal.new('Infinity').to_yaml) assert_equal("--- .NaN\n", BigDecimal.new('NaN').to_yaml) + assert_equal("--- -.Inf\n", BigDecimal.new('-Infinity').to_yaml) end end \ No newline at end of file diff --git a/activesupport/test/core_ext/date_ext_test.rb b/activesupport/test/core_ext/date_ext_test.rb index e1d2f0e7f213d..11b6056047fca 100644 --- a/activesupport/test/core_ext/date_ext_test.rb +++ b/activesupport/test/core_ext/date_ext_test.rb @@ -198,6 +198,10 @@ def test_end_of_day assert_equal Time.local(2005,2,21,23,59,59), Date.new(2005,2,21).end_of_day end + def test_date_acts_like_date + assert Date.new.acts_like_date? + end + def test_xmlschema with_env_tz 'US/Eastern' do assert_match(/^1980-02-28T00:00:00-05:?00$/, Date.new(1980, 2, 28).xmlschema) From 933697a5fc5f4c56c4fd7fbbd31b8973df9c1054 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Mon, 2 Jun 2008 09:12:00 -0700 Subject: [PATCH 017/200] Try replacing _erbout with @output_buffer --- actionpack/lib/action_view/base.rb | 5 +- .../lib/action_view/helpers/capture_helper.rb | 56 +++++-------------- .../action_view/helpers/javascript_helper.rb | 2 +- .../lib/action_view/helpers/tag_helper.rb | 2 +- .../lib/action_view/helpers/text_helper.rb | 8 ++- .../lib/action_view/template_handlers/erb.rb | 6 +- 6 files changed, 25 insertions(+), 54 deletions(-) diff --git a/actionpack/lib/action_view/base.rb b/actionpack/lib/action_view/base.rb index f398756550da4..5fae2d4dd6b8d 100644 --- a/actionpack/lib/action_view/base.rb +++ b/actionpack/lib/action_view/base.rb @@ -178,10 +178,7 @@ def self.cache_template_extensions=(*args) # that alert()s the caught exception (and then re-raises it). @@debug_rjs = false cattr_accessor :debug_rjs - - @@erb_variable = '_erbout' - cattr_accessor :erb_variable - + attr_internal :request delegate :request_forgery_protection_token, :template, :params, :session, :cookies, :response, :headers, diff --git a/actionpack/lib/action_view/helpers/capture_helper.rb b/actionpack/lib/action_view/helpers/capture_helper.rb index 9ea06568cf7da..f9a73a4c8d54a 100644 --- a/actionpack/lib/action_view/helpers/capture_helper.rb +++ b/actionpack/lib/action_view/helpers/capture_helper.rb @@ -31,20 +31,13 @@ module CaptureHelper # # def capture(*args, &block) - # execute the block - begin - buffer = eval(ActionView::Base.erb_variable, block.binding) - rescue - buffer = nil - end - - if buffer.nil? - capture_block(*args, &block).to_s - else - capture_erb_with_buffer(buffer, *args, &block).to_s - end + @output_buffer, old_buffer = '', @output_buffer + yield *args + @output_buffer + ensure + @output_buffer = old_buffer end - + # Calling content_for stores a block of markup in an identifier for later use. # You can make subsequent calls to the stored content in other templates or the layout # by passing the identifier as an argument to yield. @@ -121,40 +114,19 @@ def capture(*args, &block) # named @content_for_#{name_of_the_content_block}. The preferred usage is now # <%= yield :footer %>. def content_for(name, content = nil, &block) - existing_content_for = instance_variable_get("@content_for_#{name}").to_s - new_content_for = existing_content_for + (block_given? ? capture(&block) : content) - instance_variable_set("@content_for_#{name}", new_content_for) + ivar = "@content_for_#{name}" + instance_variable_set("@content_for_#{name}", "#{instance_variable_get(ivar)}#{block_given? ? capture(&block) : content}") end private - def capture_block(*args, &block) - block.call(*args) - end - - def capture_erb(*args, &block) - buffer = eval(ActionView::Base.erb_variable, block.binding) - capture_erb_with_buffer(buffer, *args, &block) - end - - def capture_erb_with_buffer(buffer, *args, &block) - pos = buffer.length - block.call(*args) - - # extract the block - data = buffer[pos..-1] - - # replace it in the original with empty string - buffer[pos..-1] = '' - - data - end - def erb_content_for(name, &block) - eval "@content_for_#{name} = (@content_for_#{name} || '') + capture_erb(&block)" + ivar = "@content_for_#{name}" + instance_variable_set(ivar, "#{instance_variable_get(ivar)}#{capture(&block)}") end - - def block_content_for(name, &block) - eval "@content_for_#{name} = (@content_for_#{name} || '') + capture_block(&block)" + + def block_content_for(name) + ivar = "@content_for_#{name}" + instance_variable_set(ivar, "#{instance_variable_get(ivar)}#{yield}") end end end diff --git a/actionpack/lib/action_view/helpers/javascript_helper.rb b/actionpack/lib/action_view/helpers/javascript_helper.rb index 1ea3cbd74eca4..8c880dee2f343 100644 --- a/actionpack/lib/action_view/helpers/javascript_helper.rb +++ b/actionpack/lib/action_view/helpers/javascript_helper.rb @@ -208,7 +208,7 @@ def array_or_string_for_javascript(option) private def block_is_within_action_view?(block) - eval("defined? _erbout", block.binding) + !@output_buffer.nil? end end diff --git a/actionpack/lib/action_view/helpers/tag_helper.rb b/actionpack/lib/action_view/helpers/tag_helper.rb index 999cbfb52a286..df3d69c0d8f5a 100644 --- a/actionpack/lib/action_view/helpers/tag_helper.rb +++ b/actionpack/lib/action_view/helpers/tag_helper.rb @@ -126,7 +126,7 @@ def tag_options(options, escape = true) end def block_is_within_action_view?(block) - eval("defined? _erbout", block.binding) + !@output_buffer.nil? end end end diff --git a/actionpack/lib/action_view/helpers/text_helper.rb b/actionpack/lib/action_view/helpers/text_helper.rb index 669a285424741..4711b84ae79da 100644 --- a/actionpack/lib/action_view/helpers/text_helper.rb +++ b/actionpack/lib/action_view/helpers/text_helper.rb @@ -25,8 +25,12 @@ module TextHelper # end # # will either display "Logged in!" or a login link # %> - def concat(string, binding) - eval(ActionView::Base.erb_variable, binding) << string + def concat(string, binding = nil) + if @output_buffer + @output_buffer << string + else + string + end end if RUBY_VERSION < '1.9' diff --git a/actionpack/lib/action_view/template_handlers/erb.rb b/actionpack/lib/action_view/template_handlers/erb.rb index 15a9064461f97..d1416b89d1549 100644 --- a/actionpack/lib/action_view/template_handlers/erb.rb +++ b/actionpack/lib/action_view/template_handlers/erb.rb @@ -43,13 +43,11 @@ class ERB < TemplateHandler include Compilable def compile(template) - ::ERB.new(template.source, nil, @view.erb_trim_mode).src + ::ERB.new(template.source, nil, @view.erb_trim_mode, '@output_buffer').src end def cache_fragment(block, name = {}, options = nil) #:nodoc: - @view.fragment_for(block, name, options) do - eval(ActionView::Base.erb_variable, block.binding) - end + @view.fragment_for(block, name, options) { @view.output_buffer } end end end From 0bdb7d353b4ac6f5470884360f9a480a16bd709c Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Mon, 2 Jun 2008 13:32:58 -0700 Subject: [PATCH 018/200] Work with @output_buffer instead of _erbout --- actionpack/lib/action_view/base.rb | 11 +- .../lib/action_view/helpers/capture_helper.rb | 18 +- .../action_view/template_handlers/builder.rb | 7 +- .../lib/action_view/template_handlers/erb.rb | 2 +- actionpack/test/controller/caching_test.rb | 4 +- actionpack/test/template/date_helper_test.rb | 12 +- actionpack/test/template/form_helper_test.rb | 260 +++++++++--------- .../test/template/form_options_helper_test.rb | 18 +- .../test/template/form_tag_helper_test.rb | 30 +- .../test/template/javascript_helper_test.rb | 12 +- .../test/template/prototype_helper_test.rb | 26 +- .../test/template/record_tag_helper_test.rb | 14 +- actionpack/test/template/tag_helper_test.rb | 12 +- 13 files changed, 220 insertions(+), 206 deletions(-) diff --git a/actionpack/lib/action_view/base.rb b/actionpack/lib/action_view/base.rb index 5fae2d4dd6b8d..7a603f150f2ce 100644 --- a/actionpack/lib/action_view/base.rb +++ b/actionpack/lib/action_view/base.rb @@ -156,10 +156,12 @@ class Base attr_reader :finder attr_accessor :base_path, :assigns, :template_extension, :first_render attr_accessor :controller - + attr_writer :template_format attr_accessor :current_render_extension + attr_accessor :output_buffer + # Specify trim mode for the ERB compiler. Defaults to '-'. # See ERb documentation for suitable values. @@erb_trim_mode = '-' @@ -313,9 +315,10 @@ def template_format private def wrap_content_for_layout(content) - original_content_for_layout = @content_for_layout - @content_for_layout = content - returning(yield) { @content_for_layout = original_content_for_layout } + original_content_for_layout, @content_for_layout = @content_for_layout, content + yield + ensure + @content_for_layout = original_content_for_layout end # Evaluate the local assigns and pushes them to the view. diff --git a/actionpack/lib/action_view/helpers/capture_helper.rb b/actionpack/lib/action_view/helpers/capture_helper.rb index f9a73a4c8d54a..837305d96c08e 100644 --- a/actionpack/lib/action_view/helpers/capture_helper.rb +++ b/actionpack/lib/action_view/helpers/capture_helper.rb @@ -31,11 +31,11 @@ module CaptureHelper # # def capture(*args, &block) - @output_buffer, old_buffer = '', @output_buffer - yield *args - @output_buffer - ensure - @output_buffer = old_buffer + if @output_buffer + with_temporary_output_buffer { yield *args } + else + block.call(*args) + end end # Calling content_for stores a block of markup in an identifier for later use. @@ -119,6 +119,14 @@ def content_for(name, content = nil, &block) end private + def with_temporary_output_buffer + @output_buffer, old_buffer = '', @output_buffer + yield + @output_buffer + ensure + @output_buffer = old_buffer + end + def erb_content_for(name, &block) ivar = "@content_for_#{name}" instance_variable_set(ivar, "#{instance_variable_get(ivar)}#{capture(&block)}") diff --git a/actionpack/lib/action_view/template_handlers/builder.rb b/actionpack/lib/action_view/template_handlers/builder.rb index f76d89777aba2..ee02ce1a6f841 100644 --- a/actionpack/lib/action_view/template_handlers/builder.rb +++ b/actionpack/lib/action_view/template_handlers/builder.rb @@ -11,10 +11,11 @@ def self.line_offset def compile(template) content_type_handler = (@view.send!(:controller).respond_to?(:response) ? "controller.response" : "controller") + "#{content_type_handler}.content_type ||= Mime::XML\n" + - "xml = ::Builder::XmlMarkup.new(:indent => 2)\n" + - template.source + - "\nxml.target!\n" + "xml = ::Builder::XmlMarkup.new(:indent => 2)\n" + + template.source + + "\nxml.target!\n" end def cache_fragment(block, name = {}, options = nil) diff --git a/actionpack/lib/action_view/template_handlers/erb.rb b/actionpack/lib/action_view/template_handlers/erb.rb index d1416b89d1549..ad4ccc7c42b24 100644 --- a/actionpack/lib/action_view/template_handlers/erb.rb +++ b/actionpack/lib/action_view/template_handlers/erb.rb @@ -47,7 +47,7 @@ def compile(template) end def cache_fragment(block, name = {}, options = nil) #:nodoc: - @view.fragment_for(block, name, options) { @view.output_buffer } + @view.fragment_for(block, name, options) { @view.response.template.output_buffer ||= '' } end end end diff --git a/actionpack/test/controller/caching_test.rb b/actionpack/test/controller/caching_test.rb index f9b6b87bc6160..d0ee93cf3fca4 100644 --- a/actionpack/test/controller/caching_test.rb +++ b/actionpack/test/controller/caching_test.rb @@ -421,6 +421,8 @@ def setup @controller.request = @request @controller.response = @response @controller.send(:initialize_current_url) + @controller.send(:initialize_template_class, @response) + @controller.send(:assign_shortcuts, @request, @response) end def test_fragment_cache_key @@ -510,7 +512,7 @@ def test_fragment_for def test_cache_erb_fragment @store.write('views/expensive', 'fragment content') - _erbout = 'generated till now -> ' + @controller.template.output_buffer = 'generated till now -> ' assert_equal( 'generated till now -> fragment content', ActionView::TemplateHandlers::ERB.new(@controller).cache_fragment(Proc.new{ }, 'expensive')) diff --git a/actionpack/test/template/date_helper_test.rb b/actionpack/test/template/date_helper_test.rb index 0a7b19ba96489..3399b03dd2a01 100755 --- a/actionpack/test/template/date_helper_test.rb +++ b/actionpack/test/template/date_helper_test.rb @@ -1002,17 +1002,17 @@ def test_date_select_within_fields_for @post = Post.new @post.written_on = Date.new(2004, 6, 15) - _erbout = '' + @output_buffer = '' fields_for :post, @post do |f| - _erbout.concat f.date_select(:written_on) + @output_buffer.concat f.date_select(:written_on) end expected = "\n" expected << "\n" expected << "\n" - assert_dom_equal(expected, _erbout) + assert_dom_equal(expected, @output_buffer) end def test_date_select_with_index @@ -1287,10 +1287,10 @@ def test_datetime_select_within_fields_for @post = Post.new @post.updated_at = Time.local(2004, 6, 15, 16, 35) - _erbout = '' + @output_buffer = '' fields_for :post, @post do |f| - _erbout.concat f.datetime_select(:updated_at) + @output_buffer.concat f.datetime_select(:updated_at) end expected = "\n" @@ -1299,7 +1299,7 @@ def test_datetime_select_within_fields_for expected << " — \n" expected << " : \n" - assert_dom_equal(expected, _erbout) + assert_dom_equal(expected, @output_buffer) end def test_date_select_with_zero_value_and_no_start_year diff --git a/actionpack/test/template/form_helper_test.rb b/actionpack/test/template/form_helper_test.rb index af99e6243d3d9..65984fac44f53 100644 --- a/actionpack/test/template/form_helper_test.rb +++ b/actionpack/test/template/form_helper_test.rb @@ -337,14 +337,14 @@ def test_auto_index end def test_form_for - _erbout = '' + @output_buffer = '' form_for(:post, @post, :html => { :id => 'create-post' }) do |f| - _erbout.concat f.label(:title) - _erbout.concat f.text_field(:title) - _erbout.concat f.text_area(:body) - _erbout.concat f.check_box(:secret) - _erbout.concat f.submit('Create post') + @output_buffer.concat f.label(:title) + @output_buffer.concat f.text_field(:title) + @output_buffer.concat f.text_area(:body) + @output_buffer.concat f.check_box(:secret) + @output_buffer.concat f.submit('Create post') end expected = @@ -357,16 +357,16 @@ def test_form_for "" + "" - assert_dom_equal expected, _erbout + assert_dom_equal expected, @output_buffer end def test_form_for_with_method - _erbout = '' + @output_buffer = '' form_for(:post, @post, :html => { :id => 'create-post', :method => :put }) do |f| - _erbout.concat f.text_field(:title) - _erbout.concat f.text_area(:body) - _erbout.concat f.check_box(:secret) + @output_buffer.concat f.text_field(:title) + @output_buffer.concat f.text_area(:body) + @output_buffer.concat f.check_box(:secret) end expected = @@ -378,16 +378,16 @@ def test_form_for_with_method "" + "" - assert_dom_equal expected, _erbout + assert_dom_equal expected, @output_buffer end def test_form_for_without_object - _erbout = '' + @output_buffer = '' form_for(:post, :html => { :id => 'create-post' }) do |f| - _erbout.concat f.text_field(:title) - _erbout.concat f.text_area(:body) - _erbout.concat f.check_box(:secret) + @output_buffer.concat f.text_field(:title) + @output_buffer.concat f.text_area(:body) + @output_buffer.concat f.check_box(:secret) end expected = @@ -398,17 +398,17 @@ def test_form_for_without_object "" + "" - assert_dom_equal expected, _erbout + assert_dom_equal expected, @output_buffer end def test_form_for_with_index - _erbout = '' + @output_buffer = '' form_for("post[]", @post) do |f| - _erbout.concat f.label(:title) - _erbout.concat f.text_field(:title) - _erbout.concat f.text_area(:body) - _erbout.concat f.check_box(:secret) + @output_buffer.concat f.label(:title) + @output_buffer.concat f.text_field(:title) + @output_buffer.concat f.text_area(:body) + @output_buffer.concat f.check_box(:secret) end expected = @@ -420,16 +420,16 @@ def test_form_for_with_index "" + "" - assert_dom_equal expected, _erbout + assert_dom_equal expected, @output_buffer end def test_form_for_with_nil_index_option_override - _erbout = '' + @output_buffer = '' form_for("post[]", @post, :index => nil) do |f| - _erbout.concat f.text_field(:title) - _erbout.concat f.text_area(:body) - _erbout.concat f.check_box(:secret) + @output_buffer.concat f.text_field(:title) + @output_buffer.concat f.text_area(:body) + @output_buffer.concat f.check_box(:secret) end expected = @@ -440,14 +440,14 @@ def test_form_for_with_nil_index_option_override "" + "" - assert_dom_equal expected, _erbout + assert_dom_equal expected, @output_buffer end def test_nested_fields_for - _erbout = '' + @output_buffer = '' form_for(:post, @post) do |f| f.fields_for(:comment, @post) do |c| - _erbout.concat c.text_field(:title) + @output_buffer.concat c.text_field(:title) end end @@ -455,16 +455,16 @@ def test_nested_fields_for "" + "" - assert_dom_equal expected, _erbout + assert_dom_equal expected, @output_buffer end def test_fields_for - _erbout = '' + @output_buffer = '' fields_for(:post, @post) do |f| - _erbout.concat f.text_field(:title) - _erbout.concat f.text_area(:body) - _erbout.concat f.check_box(:secret) + @output_buffer.concat f.text_field(:title) + @output_buffer.concat f.text_area(:body) + @output_buffer.concat f.check_box(:secret) end expected = @@ -473,16 +473,16 @@ def test_fields_for "" + "" - assert_dom_equal expected, _erbout + assert_dom_equal expected, @output_buffer end def test_fields_for_with_index - _erbout = '' + @output_buffer = '' fields_for("post[]", @post) do |f| - _erbout.concat f.text_field(:title) - _erbout.concat f.text_area(:body) - _erbout.concat f.check_box(:secret) + @output_buffer.concat f.text_field(:title) + @output_buffer.concat f.text_area(:body) + @output_buffer.concat f.check_box(:secret) end expected = @@ -491,16 +491,16 @@ def test_fields_for_with_index "" + "" - assert_dom_equal expected, _erbout + assert_dom_equal expected, @output_buffer end def test_fields_for_with_nil_index_option_override - _erbout = '' + @output_buffer = '' fields_for("post[]", @post, :index => nil) do |f| - _erbout.concat f.text_field(:title) - _erbout.concat f.text_area(:body) - _erbout.concat f.check_box(:secret) + @output_buffer.concat f.text_field(:title) + @output_buffer.concat f.text_area(:body) + @output_buffer.concat f.check_box(:secret) end expected = @@ -509,16 +509,16 @@ def test_fields_for_with_nil_index_option_override "" + "" - assert_dom_equal expected, _erbout + assert_dom_equal expected, @output_buffer end def test_fields_for_with_index_option_override - _erbout = '' + @output_buffer = '' fields_for("post[]", @post, :index => "abc") do |f| - _erbout.concat f.text_field(:title) - _erbout.concat f.text_area(:body) - _erbout.concat f.check_box(:secret) + @output_buffer.concat f.text_field(:title) + @output_buffer.concat f.text_area(:body) + @output_buffer.concat f.check_box(:secret) end expected = @@ -527,15 +527,15 @@ def test_fields_for_with_index_option_override "" + "" - assert_dom_equal expected, _erbout + assert_dom_equal expected, @output_buffer end def test_fields_for_without_object - _erbout = '' + @output_buffer = '' fields_for(:post) do |f| - _erbout.concat f.text_field(:title) - _erbout.concat f.text_area(:body) - _erbout.concat f.check_box(:secret) + @output_buffer.concat f.text_field(:title) + @output_buffer.concat f.text_area(:body) + @output_buffer.concat f.check_box(:secret) end expected = @@ -544,15 +544,15 @@ def test_fields_for_without_object "" + "" - assert_dom_equal expected, _erbout + assert_dom_equal expected, @output_buffer end def test_fields_for_with_only_object - _erbout = '' + @output_buffer = '' fields_for(@post) do |f| - _erbout.concat f.text_field(:title) - _erbout.concat f.text_area(:body) - _erbout.concat f.check_box(:secret) + @output_buffer.concat f.text_field(:title) + @output_buffer.concat f.text_area(:body) + @output_buffer.concat f.check_box(:secret) end expected = @@ -561,31 +561,31 @@ def test_fields_for_with_only_object "" + "" - assert_dom_equal expected, _erbout + assert_dom_equal expected, @output_buffer end def test_fields_for_object_with_bracketed_name - _erbout = '' + @output_buffer = '' fields_for("author[post]", @post) do |f| - _erbout.concat f.label(:title) - _erbout.concat f.text_field(:title) + @output_buffer.concat f.label(:title) + @output_buffer.concat f.text_field(:title) end assert_dom_equal "" + "", - _erbout + @output_buffer end def test_fields_for_object_with_bracketed_name_and_index - _erbout = '' + @output_buffer = '' fields_for("author[post]", @post, :index => 1) do |f| - _erbout.concat f.label(:title) - _erbout.concat f.text_field(:title) + @output_buffer.concat f.label(:title) + @output_buffer.concat f.text_field(:title) end assert_dom_equal "" + "", - _erbout + @output_buffer end def test_form_builder_does_not_have_form_for_method @@ -593,14 +593,14 @@ def test_form_builder_does_not_have_form_for_method end def test_form_for_and_fields_for - _erbout = '' + @output_buffer = '' form_for(:post, @post, :html => { :id => 'create-post' }) do |post_form| - _erbout.concat post_form.text_field(:title) - _erbout.concat post_form.text_area(:body) + @output_buffer.concat post_form.text_field(:title) + @output_buffer.concat post_form.text_area(:body) fields_for(:parent_post, @post) do |parent_fields| - _erbout.concat parent_fields.check_box(:secret) + @output_buffer.concat parent_fields.check_box(:secret) end end @@ -612,18 +612,18 @@ def test_form_for_and_fields_for "" + "" - assert_dom_equal expected, _erbout + assert_dom_equal expected, @output_buffer end def test_form_for_and_fields_for_with_object - _erbout = '' + @output_buffer = '' form_for(:post, @post, :html => { :id => 'create-post' }) do |post_form| - _erbout.concat post_form.text_field(:title) - _erbout.concat post_form.text_area(:body) + @output_buffer.concat post_form.text_field(:title) + @output_buffer.concat post_form.text_area(:body) post_form.fields_for(@comment) do |comment_fields| - _erbout.concat comment_fields.text_field(:name) + @output_buffer.concat comment_fields.text_field(:name) end end @@ -634,7 +634,7 @@ def test_form_for_and_fields_for_with_object "" + "" - assert_dom_equal expected, _erbout + assert_dom_equal expected, @output_buffer end class LabelledFormBuilder < ActionView::Helpers::FormBuilder @@ -649,12 +649,12 @@ def #{selector}(field, *args, &proc) end def test_form_for_with_labelled_builder - _erbout = '' + @output_buffer = '' form_for(:post, @post, :builder => LabelledFormBuilder) do |f| - _erbout.concat f.text_field(:title) - _erbout.concat f.text_area(:body) - _erbout.concat f.check_box(:secret) + @output_buffer.concat f.text_field(:title) + @output_buffer.concat f.text_area(:body) + @output_buffer.concat f.check_box(:secret) end expected = @@ -665,18 +665,18 @@ def test_form_for_with_labelled_builder "
" + "" - assert_dom_equal expected, _erbout + assert_dom_equal expected, @output_buffer end def test_default_form_builder old_default_form_builder, ActionView::Base.default_form_builder = ActionView::Base.default_form_builder, LabelledFormBuilder - _erbout = '' + @output_buffer = '' form_for(:post, @post) do |f| - _erbout.concat f.text_field(:title) - _erbout.concat f.text_area(:body) - _erbout.concat f.check_box(:secret) + @output_buffer.concat f.text_field(:title) + @output_buffer.concat f.text_area(:body) + @output_buffer.concat f.check_box(:secret) end expected = @@ -687,17 +687,17 @@ def test_default_form_builder "
" + "" - assert_dom_equal expected, _erbout + assert_dom_equal expected, @output_buffer ensure ActionView::Base.default_form_builder = old_default_form_builder end def test_default_form_builder_with_active_record_helpers - _erbout = '' + @output_buffer = '' form_for(:post, @post) do |f| - _erbout.concat f.error_message_on('author_name') - _erbout.concat f.error_messages + @output_buffer.concat f.error_message_on('author_name') + @output_buffer.concat f.error_messages end expected = %(
) + @@ -705,7 +705,7 @@ def test_default_form_builder_with_active_record_helpers %(

1 error prohibited this post from being saved

There were problems with the following fields:

  • Author name can't be empty
) + %(
) - assert_dom_equal expected, _erbout + assert_dom_equal expected, @output_buffer end @@ -713,10 +713,10 @@ def test_default_form_builder_no_instance_variable post = @post @post = nil - _erbout = '' + @output_buffer = '' form_for(:post, post) do |f| - _erbout.concat f.error_message_on('author_name') - _erbout.concat f.error_messages + @output_buffer.concat f.error_message_on('author_name') + @output_buffer.concat f.error_messages end expected = %(
) + @@ -724,19 +724,19 @@ def test_default_form_builder_no_instance_variable %(

1 error prohibited this post from being saved

There were problems with the following fields:

  • Author name can't be empty
) + %(
) - assert_dom_equal expected, _erbout + assert_dom_equal expected, @output_buffer end # Perhaps this test should be moved to prototype helper tests. def test_remote_form_for_with_labelled_builder self.extend ActionView::Helpers::PrototypeHelper - _erbout = '' + @output_buffer = '' remote_form_for(:post, @post, :builder => LabelledFormBuilder) do |f| - _erbout.concat f.text_field(:title) - _erbout.concat f.text_area(:body) - _erbout.concat f.check_box(:secret) + @output_buffer.concat f.text_field(:title) + @output_buffer.concat f.text_area(:body) + @output_buffer.concat f.check_box(:secret) end expected = @@ -747,16 +747,16 @@ def test_remote_form_for_with_labelled_builder "
" + "" - assert_dom_equal expected, _erbout + assert_dom_equal expected, @output_buffer end def test_fields_for_with_labelled_builder - _erbout = '' + @output_buffer = '' fields_for(:post, @post, :builder => LabelledFormBuilder) do |f| - _erbout.concat f.text_field(:title) - _erbout.concat f.text_area(:body) - _erbout.concat f.check_box(:secret) + @output_buffer.concat f.text_field(:title) + @output_buffer.concat f.text_area(:body) + @output_buffer.concat f.check_box(:secret) end expected = @@ -765,28 +765,28 @@ def test_fields_for_with_labelled_builder " " + "
" - assert_dom_equal expected, _erbout + assert_dom_equal expected, @output_buffer end def test_form_for_with_html_options_adds_options_to_form_tag - _erbout = '' + @output_buffer = '' form_for(:post, @post, :html => {:id => 'some_form', :class => 'some_class'}) do |f| end expected = "
" - assert_dom_equal expected, _erbout + assert_dom_equal expected, @output_buffer end def test_form_for_with_string_url_option - _erbout = '' + @output_buffer = '' form_for(:post, @post, :url => 'http://www.otherdomain.com') do |f| end - assert_equal '
', _erbout + assert_equal '
', @output_buffer end def test_form_for_with_hash_url_option - _erbout = '' + @output_buffer = '' form_for(:post, @post, :url => {:controller => 'controller', :action => 'action'}) do |f| end @@ -795,25 +795,25 @@ def test_form_for_with_hash_url_option end def test_form_for_with_record_url_option - _erbout = '' + @output_buffer = '' form_for(:post, @post, :url => @post) do |f| end expected = "
" - assert_equal expected, _erbout + assert_equal expected, @output_buffer end def test_form_for_with_existing_object - _erbout = '' + @output_buffer = '' form_for(@post) do |f| end expected = "
" - assert_equal expected, _erbout + assert_equal expected, @output_buffer end def test_form_for_with_new_object - _erbout = '' + @output_buffer = '' post = Post.new post.new_record = true @@ -822,64 +822,64 @@ def post.id() nil end form_for(post) do |f| end expected = "
" - assert_equal expected, _erbout + assert_equal expected, @output_buffer end def test_form_for_with_existing_object_in_list @post.new_record = false @comment.save - _erbout = '' + @output_buffer = '' form_for([@post, @comment]) {} expected = %(
) - assert_dom_equal expected, _erbout + assert_dom_equal expected, @output_buffer end def test_form_for_with_new_object_in_list @post.new_record = false - _erbout = '' + @output_buffer = '' form_for([@post, @comment]) {} expected = %(
) - assert_dom_equal expected, _erbout + assert_dom_equal expected, @output_buffer end def test_form_for_with_existing_object_and_namespace_in_list @post.new_record = false @comment.save - _erbout = '' + @output_buffer = '' form_for([:admin, @post, @comment]) {} expected = %(
) - assert_dom_equal expected, _erbout + assert_dom_equal expected, @output_buffer end def test_form_for_with_new_object_and_namespace_in_list @post.new_record = false - _erbout = '' + @output_buffer = '' form_for([:admin, @post, @comment]) {} expected = %(
) - assert_dom_equal expected, _erbout + assert_dom_equal expected, @output_buffer end def test_form_for_with_existing_object_and_custom_url - _erbout = '' + @output_buffer = '' form_for(@post, :url => "/super_posts") do |f| end expected = "
" - assert_equal expected, _erbout + assert_equal expected, @output_buffer end def test_remote_form_for_with_html_options_adds_options_to_form_tag self.extend ActionView::Helpers::PrototypeHelper - _erbout = '' + @output_buffer = '' remote_form_for(:post, @post, :html => {:id => 'some_form', :class => 'some_class'}) do |f| end expected = "
" - assert_dom_equal expected, _erbout + assert_dom_equal expected, @output_buffer end diff --git a/actionpack/test/template/form_options_helper_test.rb b/actionpack/test/template/form_options_helper_test.rb index 48a26deea91e1..3c9fb297c3a2d 100644 --- a/actionpack/test/template/form_options_helper_test.rb +++ b/actionpack/test/template/form_options_helper_test.rb @@ -231,15 +231,15 @@ def test_select_under_fields_for @post = Post.new @post.category = "" - _erbout = '' + @output_buffer = '' fields_for :post, @post do |f| - _erbout.concat f.select(:category, %w( abe hest)) + @output_buffer.concat f.select(:category, %w( abe hest)) end assert_dom_equal( "", - _erbout + @output_buffer ) end @@ -353,15 +353,15 @@ def test_collection_select_under_fields_for @post = Post.new @post.author_name = "Babe" - _erbout = '' + @output_buffer = '' fields_for :post, @post do |f| - _erbout.concat f.collection_select(:author_name, @posts, :author_name, :author_name) + @output_buffer.concat f.collection_select(:author_name, @posts, :author_name, :author_name) end assert_dom_equal( "", - _erbout + @output_buffer ) end @@ -1195,10 +1195,10 @@ def test_time_zone_select def test_time_zone_select_under_fields_for @firm = Firm.new("D") - _erbout = '' + @output_buffer = '' fields_for :firm, @firm do |f| - _erbout.concat f.time_zone_select(:time_zone) + @output_buffer.concat f.time_zone_select(:time_zone) end assert_dom_equal( @@ -1209,7 +1209,7 @@ def test_time_zone_select_under_fields_for "\n" + "" + "", - _erbout + @output_buffer ) end diff --git a/actionpack/test/template/form_tag_helper_test.rb b/actionpack/test/template/form_tag_helper_test.rb index 73a8bd4d87d69..b281db18bd48f 100644 --- a/actionpack/test/template/form_tag_helper_test.rb +++ b/actionpack/test/template/form_tag_helper_test.rb @@ -43,19 +43,19 @@ def test_form_tag_with_method_delete end def test_form_tag_with_block - _erbout = '' - form_tag("http://example.com") { _erbout.concat "Hello world!" } + @output_buffer = '' + form_tag("http://example.com") { @output_buffer.concat "Hello world!" } expected = %(
Hello world!
) - assert_dom_equal expected, _erbout + assert_dom_equal expected, @output_buffer end def test_form_tag_with_block_and_method - _erbout = '' - form_tag("http://example.com", :method => :put) { _erbout.concat "Hello world!" } + @output_buffer = '' + form_tag("http://example.com", :method => :put) { @output_buffer.concat "Hello world!" } expected = %(
Hello world!
) - assert_dom_equal expected, _erbout + assert_dom_equal expected, @output_buffer end def test_hidden_field_tag @@ -234,23 +234,23 @@ def test_pass end def test_field_set_tag - _erbout = '' - field_set_tag("Your details") { _erbout.concat "Hello world!" } + @output_buffer = '' + field_set_tag("Your details") { @output_buffer.concat "Hello world!" } expected = %(
Your detailsHello world!
) - assert_dom_equal expected, _erbout + assert_dom_equal expected, @output_buffer - _erbout = '' - field_set_tag { _erbout.concat "Hello world!" } + @output_buffer = '' + field_set_tag { @output_buffer.concat "Hello world!" } expected = %(
Hello world!
) - assert_dom_equal expected, _erbout + assert_dom_equal expected, @output_buffer - _erbout = '' - field_set_tag('') { _erbout.concat "Hello world!" } + @output_buffer = '' + field_set_tag('') { @output_buffer.concat "Hello world!" } expected = %(
Hello world!
) - assert_dom_equal expected, _erbout + assert_dom_equal expected, @output_buffer end def protect_against_forgery? diff --git a/actionpack/test/template/javascript_helper_test.rb b/actionpack/test/template/javascript_helper_test.rb index f18adb990c92e..f136f56777feb 100644 --- a/actionpack/test/template/javascript_helper_test.rb +++ b/actionpack/test/template/javascript_helper_test.rb @@ -92,15 +92,15 @@ def test_javascript_tag_with_options end def test_javascript_tag_with_block - _erbout = '' - javascript_tag { _erbout.concat "alert('hello')" } - assert_dom_equal "", _erbout + @output_buffer = '' + javascript_tag { @output_buffer.concat "alert('hello')" } + assert_dom_equal "", @output_buffer end def test_javascript_tag_with_block_and_options - _erbout = '' - javascript_tag(:id => "the_js_tag") { _erbout.concat "alert('hello')" } - assert_dom_equal "", _erbout + @output_buffer = '' + javascript_tag(:id => "the_js_tag") { @output_buffer.concat "alert('hello')" } + assert_dom_equal "", @output_buffer end def test_javascript_cdata_section diff --git a/actionpack/test/template/prototype_helper_test.rb b/actionpack/test/template/prototype_helper_test.rb index 53a250f9d5cea..dd6f6ab741ffa 100644 --- a/actionpack/test/template/prototype_helper_test.rb +++ b/actionpack/test/template/prototype_helper_test.rb @@ -118,52 +118,52 @@ def test_form_remote_tag_with_method end def test_form_remote_tag_with_block - _erbout = '' - form_remote_tag(:update => "glass_of_beer", :url => { :action => :fast }) { _erbout.concat "Hello world!" } - assert_dom_equal %(
Hello world!
), _erbout + @output_buffer = '' + form_remote_tag(:update => "glass_of_beer", :url => { :action => :fast }) { @output_buffer.concat "Hello world!" } + assert_dom_equal %(
Hello world!
), @output_buffer end def test_remote_form_for_with_record_identification_with_new_record - _erbout = '' + @output_buffer = '' remote_form_for(@record, {:html => { :id => 'create-author' }}) {} expected = %(
) - assert_dom_equal expected, _erbout + assert_dom_equal expected, @output_buffer end def test_remote_form_for_with_record_identification_without_html_options - _erbout = '' + @output_buffer = '' remote_form_for(@record) {} expected = %(
) - assert_dom_equal expected, _erbout + assert_dom_equal expected, @output_buffer end def test_remote_form_for_with_record_identification_with_existing_record @record.save - _erbout = '' + @output_buffer = '' remote_form_for(@record) {} expected = %(
) - assert_dom_equal expected, _erbout + assert_dom_equal expected, @output_buffer end def test_remote_form_for_with_new_object_in_list - _erbout = '' + @output_buffer = '' remote_form_for([@author, @article]) {} expected = %(
) - assert_dom_equal expected, _erbout + assert_dom_equal expected, @output_buffer end def test_remote_form_for_with_existing_object_in_list @author.save @article.save - _erbout = '' + @output_buffer = '' remote_form_for([@author, @article]) {} expected = %(
) - assert_dom_equal expected, _erbout + assert_dom_equal expected, @output_buffer end def test_on_callbacks diff --git a/actionpack/test/template/record_tag_helper_test.rb b/actionpack/test/template/record_tag_helper_test.rb index 0afbb54f57a30..c39e5c69bf8f9 100644 --- a/actionpack/test/template/record_tag_helper_test.rb +++ b/actionpack/test/template/record_tag_helper_test.rb @@ -17,37 +17,37 @@ def setup end def test_content_tag_for - _erbout = '' + @output_buffer = '' expected = %(
  • ) actual = content_tag_for(:li, @post, :class => 'bar') { } assert_dom_equal expected, actual end def test_content_tag_for_prefix - _erbout = '' + @output_buffer = '' expected = %(
      ) actual = content_tag_for(:ul, @post, :archived) { } assert_dom_equal expected, actual end def test_content_tag_for_with_extra_html_tags - _erbout = '' + @output_buffer = '' expected = %() actual = content_tag_for(:tr, @post, {:class => "bar", :style => "background-color: #f0f0f0"}) { } assert_dom_equal expected, actual end def test_block_works_with_content_tag_for - _erbout = '' + @output_buffer = '' expected = %(#{@post.body}) - actual = content_tag_for(:tr, @post) { _erbout.concat @post.body } + actual = content_tag_for(:tr, @post) { @output_buffer.concat @post.body } assert_dom_equal expected, actual end def test_div_for - _erbout = '' + @output_buffer = '' expected = %(
      #{@post.body}
      ) - actual = div_for(@post, :class => "bar") { _erbout.concat @post.body } + actual = div_for(@post, :class => "bar") { @output_buffer.concat @post.body } assert_dom_equal expected, actual end diff --git a/actionpack/test/template/tag_helper_test.rb b/actionpack/test/template/tag_helper_test.rb index 4da6116095324..7db04d178f34c 100644 --- a/actionpack/test/template/tag_helper_test.rb +++ b/actionpack/test/template/tag_helper_test.rb @@ -35,15 +35,15 @@ def test_content_tag end def test_content_tag_with_block - _erbout = '' - content_tag(:div) { _erbout.concat "Hello world!" } - assert_dom_equal "
      Hello world!
      ", _erbout + @output_buffer = '' + content_tag(:div) { @output_buffer.concat "Hello world!" } + assert_dom_equal "
      Hello world!
      ", @output_buffer end def test_content_tag_with_block_and_options - _erbout = '' - content_tag(:div, :class => "green") { _erbout.concat "Hello world!" } - assert_dom_equal %(
      Hello world!
      ), _erbout + @output_buffer = '' + content_tag(:div, :class => "green") { @output_buffer.concat "Hello world!" } + assert_dom_equal %(
      Hello world!
      ), @output_buffer end def test_content_tag_with_block_and_options_outside_of_action_view From 4d4c8e298f5396e6b8ace0a10d7f991594aace2d Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Mon, 2 Jun 2008 13:49:14 -0700 Subject: [PATCH 019/200] Don't pass block binding to concat --- actionpack/lib/action_view/base.rb | 2 +- actionpack/lib/action_view/helpers/form_helper.rb | 4 ++-- .../lib/action_view/helpers/form_tag_helper.rb | 14 +++++++------- .../lib/action_view/helpers/javascript_helper.rb | 8 +------- .../lib/action_view/helpers/prototype_helper.rb | 4 ++-- .../lib/action_view/helpers/record_tag_helper.rb | 5 ++--- actionpack/lib/action_view/helpers/tag_helper.rb | 7 ++----- actionpack/lib/action_view/helpers/text_helper.rb | 8 ++++---- 8 files changed, 21 insertions(+), 31 deletions(-) diff --git a/actionpack/lib/action_view/base.rb b/actionpack/lib/action_view/base.rb index 7a603f150f2ce..ad114acc6c6b0 100644 --- a/actionpack/lib/action_view/base.rb +++ b/actionpack/lib/action_view/base.rb @@ -257,7 +257,7 @@ def render(options = {}, local_assigns = {}, &block) #:nodoc: if partial_layout = options.delete(:layout) if block_given? wrap_content_for_layout capture(&block) do - concat(render(options.merge(:partial => partial_layout)), block.binding) + concat(render(options.merge(:partial => partial_layout))) end else wrap_content_for_layout render(options) do diff --git a/actionpack/lib/action_view/helpers/form_helper.rb b/actionpack/lib/action_view/helpers/form_helper.rb index 0791feb9ac278..63a932320e94f 100644 --- a/actionpack/lib/action_view/helpers/form_helper.rb +++ b/actionpack/lib/action_view/helpers/form_helper.rb @@ -249,9 +249,9 @@ def form_for(record_or_name_or_array, *args, &proc) args.unshift object end - concat(form_tag(options.delete(:url) || {}, options.delete(:html) || {}), proc.binding) + concat(form_tag(options.delete(:url) || {}, options.delete(:html) || {})) fields_for(object_name, *(args << options), &proc) - concat('', proc.binding) + concat('') end def apply_form_for_options!(object_or_array, options) #:nodoc: diff --git a/actionpack/lib/action_view/helpers/form_tag_helper.rb b/actionpack/lib/action_view/helpers/form_tag_helper.rb index ca58f4ba26cc1..3a97f1390ff66 100644 --- a/actionpack/lib/action_view/helpers/form_tag_helper.rb +++ b/actionpack/lib/action_view/helpers/form_tag_helper.rb @@ -407,10 +407,10 @@ def image_submit_tag(source, options = {}) # # =>
      Your details

      def field_set_tag(legend = nil, &block) content = capture(&block) - concat(tag(:fieldset, {}, true), block.binding) - concat(content_tag(:legend, legend), block.binding) unless legend.blank? - concat(content, block.binding) - concat("", block.binding) + concat(tag(:fieldset, {}, true)) + concat(content_tag(:legend, legend)) unless legend.blank? + concat(content) + concat("") end private @@ -442,9 +442,9 @@ def form_tag_html(html_options) def form_tag_in_block(html_options, &block) content = capture(&block) - concat(form_tag_html(html_options), block.binding) - concat(content, block.binding) - concat("", block.binding) + concat(form_tag_html(html_options)) + concat(content) + concat("") end def token_tag diff --git a/actionpack/lib/action_view/helpers/javascript_helper.rb b/actionpack/lib/action_view/helpers/javascript_helper.rb index 8c880dee2f343..a2f257763649e 100644 --- a/actionpack/lib/action_view/helpers/javascript_helper.rb +++ b/actionpack/lib/action_view/helpers/javascript_helper.rb @@ -179,13 +179,7 @@ def javascript_tag(content_or_options_with_block = nil, html_options = {}, &bloc content = content_or_options_with_block end - javascript_tag = content_tag("script", javascript_cdata_section(content), html_options.merge(:type => Mime::JS)) - - if block_given? && block_is_within_action_view?(block) - concat(javascript_tag, block.binding) - else - javascript_tag - end + concat(content_tag("script", javascript_cdata_section(content), html_options.merge(:type => Mime::JS))) end def javascript_cdata_section(content) #:nodoc: diff --git a/actionpack/lib/action_view/helpers/prototype_helper.rb b/actionpack/lib/action_view/helpers/prototype_helper.rb index 602832e47098a..ed2abba17a175 100644 --- a/actionpack/lib/action_view/helpers/prototype_helper.rb +++ b/actionpack/lib/action_view/helpers/prototype_helper.rb @@ -382,9 +382,9 @@ def remote_form_for(record_or_name_or_array, *args, &proc) args.unshift object end - concat(form_remote_tag(options), proc.binding) + concat(form_remote_tag(options)) fields_for(object_name, *(args << options), &proc) - concat('', proc.binding) + concat('') end alias_method :form_remote_for, :remote_form_for diff --git a/actionpack/lib/action_view/helpers/record_tag_helper.rb b/actionpack/lib/action_view/helpers/record_tag_helper.rb index 66c596f3a90a0..9bb235175e393 100644 --- a/actionpack/lib/action_view/helpers/record_tag_helper.rb +++ b/actionpack/lib/action_view/helpers/record_tag_helper.rb @@ -51,9 +51,8 @@ def content_tag_for(tag_name, record, *args, &block) prefix = args.first.is_a?(Hash) ? nil : args.shift options = args.first.is_a?(Hash) ? args.shift : {} concat content_tag(tag_name, capture(&block), - options.merge({ :class => "#{dom_class(record)} #{options[:class]}".strip, :id => dom_id(record, prefix) })), - block.binding + options.merge({ :class => "#{dom_class(record)} #{options[:class]}".strip, :id => dom_id(record, prefix) })) end end end -end \ No newline at end of file +end diff --git a/actionpack/lib/action_view/helpers/tag_helper.rb b/actionpack/lib/action_view/helpers/tag_helper.rb index df3d69c0d8f5a..e669620381f02 100644 --- a/actionpack/lib/action_view/helpers/tag_helper.rb +++ b/actionpack/lib/action_view/helpers/tag_helper.rb @@ -66,12 +66,9 @@ def tag(name, options = nil, open = false, escape = true) def content_tag(name, content_or_options_with_block = nil, options = nil, escape = true, &block) if block_given? options = content_or_options_with_block if content_or_options_with_block.is_a?(Hash) - content = capture(&block) - content_tag = content_tag_string(name, content, options, escape) - block_is_within_action_view?(block) ? concat(content_tag, block.binding) : content_tag + concat(content_tag_string(name, capture(&block), options, escape)) else - content = content_or_options_with_block - content_tag_string(name, content, options, escape) + content_tag_string(name, content_or_options_with_block, options, escape) end end diff --git a/actionpack/lib/action_view/helpers/text_helper.rb b/actionpack/lib/action_view/helpers/text_helper.rb index 4711b84ae79da..3be4843386664 100644 --- a/actionpack/lib/action_view/helpers/text_helper.rb +++ b/actionpack/lib/action_view/helpers/text_helper.rb @@ -15,17 +15,17 @@ module TextHelper # # ==== Examples # <% - # concat "hello", binding + # concat "hello" # # is the equivalent of <%= "hello" %> # # if (logged_in == true): - # concat "Logged in!", binding + # concat "Logged in!" # else - # concat link_to('login', :action => login), binding + # concat link_to('login', :action => login) # end # # will either display "Logged in!" or a login link # %> - def concat(string, binding = nil) + def concat(string) if @output_buffer @output_buffer << string else From f55ad960d22337d0d92a93724f1cc3ad45200836 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Tue, 3 Jun 2008 01:10:00 -0700 Subject: [PATCH 020/200] Stack @output_buffer for nested rendering --- actionpack/lib/action_view/template_handlers/compilable.rb | 4 ++-- actionpack/test/controller/caching_test.rb | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/actionpack/lib/action_view/template_handlers/compilable.rb b/actionpack/lib/action_view/template_handlers/compilable.rb index 25bd0fea7f5c8..28c72172a0af7 100644 --- a/actionpack/lib/action_view/template_handlers/compilable.rb +++ b/actionpack/lib/action_view/template_handlers/compilable.rb @@ -106,7 +106,7 @@ def create_template_source(template, render_symbol) locals_code << "#{key} = local_assigns[:#{key}]\n" end - "def #{render_symbol}(local_assigns)\n#{locals_code}#{body}\nend" + "def #{render_symbol}(local_assigns)\nold_output_buffer = @output_buffer;#{locals_code}#{body}\nensure\n@output_buffer = old_output_buffer\nend" end # Return true if the given template was compiled for a superset of the keys in local_assigns @@ -125,4 +125,4 @@ def template_changed_since?(file_name, compile_time) end end -end \ No newline at end of file +end diff --git a/actionpack/test/controller/caching_test.rb b/actionpack/test/controller/caching_test.rb index d0ee93cf3fca4..8b3ddc760304c 100644 --- a/actionpack/test/controller/caching_test.rb +++ b/actionpack/test/controller/caching_test.rb @@ -512,7 +512,7 @@ def test_fragment_for def test_cache_erb_fragment @store.write('views/expensive', 'fragment content') - @controller.template.output_buffer = 'generated till now -> ' + @controller.response.template.output_buffer = 'generated till now -> ' assert_equal( 'generated till now -> fragment content', ActionView::TemplateHandlers::ERB.new(@controller).cache_fragment(Proc.new{ }, 'expensive')) From c08547d2266c75f0a82d06dd91c6d0500740e12e Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Tue, 3 Jun 2008 13:32:53 -0500 Subject: [PATCH 021/200] Namespace Inflector, Dependencies, OrderedOptions, and TimeZone under ActiveSupport [#238 state:resolved] --- .../lib/action_controller/dispatcher.rb | 2 +- actionpack/lib/action_controller/routing.rb | 2 +- actionpack/test/controller/dispatcher_test.rb | 4 +- .../controller/integration_upload_test.rb | 2 +- actionpack/test/controller/routing_test.rb | 4 +- activerecord/lib/active_record/base.rb | 2 +- .../test/cases/attribute_methods_test.rb | 20 +- activerecord/test/cases/base_test.rb | 28 +- activerecord/test/cases/inheritance_test.rb | 4 +- activerecord/test/cases/multiple_db_test.rb | 2 +- activesupport/lib/active_support.rb | 4 + .../lib/active_support/dependencies.rb | 785 +++++++++--------- .../lib/active_support/deprecation.rb | 32 +- .../lib/active_support/inflections.rb | 100 +-- activesupport/lib/active_support/inflector.rb | 526 ++++++------ .../lib/active_support/ordered_options.rb | 26 +- .../lib/active_support/values/time_zone.rb | 694 ++++++++-------- activesupport/test/core_ext/date_ext_test.rb | 14 +- .../test/core_ext/date_time_ext_test.rb | 24 +- activesupport/test/core_ext/duration_test.rb | 8 +- activesupport/test/core_ext/time_ext_test.rb | 28 +- .../test/core_ext/time_with_zone_test.rb | 240 +++--- activesupport/test/dependencies_test.rb | 240 +++--- activesupport/test/deprecation_test.rb | 10 + activesupport/test/inflector_test.rb | 126 +-- activesupport/test/ordered_options_test.rb | 6 +- activesupport/test/time_zone_test.rb | 135 ++- 27 files changed, 1554 insertions(+), 1514 deletions(-) diff --git a/actionpack/lib/action_controller/dispatcher.rb b/actionpack/lib/action_controller/dispatcher.rb index 7162fb8b1f274..fe4f6b4a7ea29 100644 --- a/actionpack/lib/action_controller/dispatcher.rb +++ b/actionpack/lib/action_controller/dispatcher.rb @@ -141,7 +141,7 @@ def reload_application # be reloaded on the next request without restarting the server. def cleanup_application ActiveRecord::Base.reset_subclasses if defined?(ActiveRecord) - Dependencies.clear + ActiveSupport::Dependencies.clear ActiveRecord::Base.clear_reloadable_connections! if defined?(ActiveRecord) end diff --git a/actionpack/lib/action_controller/routing.rb b/actionpack/lib/action_controller/routing.rb index 6aa266513d865..8846dcc504c78 100644 --- a/actionpack/lib/action_controller/routing.rb +++ b/actionpack/lib/action_controller/routing.rb @@ -369,7 +369,7 @@ def controller_relative_to(controller, previous) Routes = RouteSet.new - ::Inflector.module_eval do + ActiveSupport::Inflector.module_eval do # Ensures that routes are reloaded when Rails inflections are updated. def inflections_with_route_reloading(&block) returning(inflections_without_route_reloading(&block)) { diff --git a/actionpack/test/controller/dispatcher_test.rb b/actionpack/test/controller/dispatcher_test.rb index eea0813ed5818..911fcab67b394 100644 --- a/actionpack/test/controller/dispatcher_test.rb +++ b/actionpack/test/controller/dispatcher_test.rb @@ -27,14 +27,14 @@ def teardown def test_clears_dependencies_after_dispatch_if_in_loading_mode ActionController::Routing::Routes.expects(:reload).once - Dependencies.expects(:clear).once + ActiveSupport::Dependencies.expects(:clear).once dispatch(@output, false) end def test_leaves_dependencies_after_dispatch_if_not_in_loading_mode ActionController::Routing::Routes.expects(:reload).never - Dependencies.expects(:clear).never + ActiveSupport::Dependencies.expects(:clear).never dispatch end diff --git a/actionpack/test/controller/integration_upload_test.rb b/actionpack/test/controller/integration_upload_test.rb index 33df1131cb0b6..4af9b7e6976e6 100644 --- a/actionpack/test/controller/integration_upload_test.rb +++ b/actionpack/test/controller/integration_upload_test.rb @@ -28,7 +28,7 @@ class << self # end def test_post_with_upload uses_mocha "test_post_with_upload" do - Dependencies.stubs(:load?).returns(false) + ActiveSupport::Dependencies.stubs(:load?).returns(false) with_routing do |set| set.draw do |map| map.update 'update', :controller => "upload_test", :action => "update", :method => :post diff --git a/actionpack/test/controller/routing_test.rb b/actionpack/test/controller/routing_test.rb index 5e5503fd52229..068d71a8f85d2 100644 --- a/actionpack/test/controller/routing_test.rb +++ b/actionpack/test/controller/routing_test.rb @@ -2392,10 +2392,10 @@ def test_bang_forces_reload end def test_adding_inflections_forces_reload - Inflector::Inflections.instance.expects(:uncountable).with('equipment') + ActiveSupport::Inflector::Inflections.instance.expects(:uncountable).with('equipment') routes.expects(:reload!) - Inflector.inflections { |inflect| inflect.uncountable('equipment') } + ActiveSupport::Inflector.inflections { |inflect| inflect.uncountable('equipment') } end def test_load_with_configuration diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 92a24ecc5f5cb..8dd07eb47884b 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -372,7 +372,7 @@ def self.inherited(child) #:nodoc: def self.reset_subclasses #:nodoc: nonreloadables = [] subclasses.each do |klass| - unless Dependencies.autoloaded? klass + unless ActiveSupport::Dependencies.autoloaded? klass nonreloadables << klass next end diff --git a/activerecord/test/cases/attribute_methods_test.rb b/activerecord/test/cases/attribute_methods_test.rb index c336fd9afbcb4..7999e292647b9 100755 --- a/activerecord/test/cases/attribute_methods_test.rb +++ b/activerecord/test/cases/attribute_methods_test.rb @@ -137,7 +137,7 @@ def test_accessing_cached_attributes_caches_the_converted_values_and_nothing_els end end end - + def test_time_attributes_are_retrieved_in_current_time_zone in_time_zone "Pacific Time (US & Canada)" do utc_time = Time.utc(2008, 1, 1) @@ -145,7 +145,7 @@ def test_time_attributes_are_retrieved_in_current_time_zone record[:written_on] = utc_time assert_equal utc_time, record.written_on # record.written on is equal to (i.e., simultaneous with) utc_time assert_kind_of ActiveSupport::TimeWithZone, record.written_on # but is a TimeWithZone - assert_equal TimeZone["Pacific Time (US & Canada)"], record.written_on.time_zone # and is in the current Time.zone + assert_equal ActiveSupport::TimeZone["Pacific Time (US & Canada)"], record.written_on.time_zone # and is in the current Time.zone assert_equal Time.utc(2007, 12, 31, 16), record.written_on.time # and represents time values adjusted accordingly end end @@ -156,7 +156,7 @@ def test_setting_time_zone_aware_attribute_to_utc record = @target.new record.written_on = utc_time assert_equal utc_time, record.written_on - assert_equal TimeZone["Pacific Time (US & Canada)"], record.written_on.time_zone + assert_equal ActiveSupport::TimeZone["Pacific Time (US & Canada)"], record.written_on.time_zone assert_equal Time.utc(2007, 12, 31, 16), record.written_on.time end end @@ -168,7 +168,7 @@ def test_setting_time_zone_aware_attribute_in_other_time_zone record = @target.new record.written_on = cst_time assert_equal utc_time, record.written_on - assert_equal TimeZone["Pacific Time (US & Canada)"], record.written_on.time_zone + assert_equal ActiveSupport::TimeZone["Pacific Time (US & Canada)"], record.written_on.time_zone assert_equal Time.utc(2007, 12, 31, 16), record.written_on.time end end @@ -181,12 +181,12 @@ def test_setting_time_zone_aware_attribute_with_string record = @target.new record.written_on = time_string assert_equal Time.zone.parse(time_string), record.written_on - assert_equal TimeZone["Pacific Time (US & Canada)"], record.written_on.time_zone + assert_equal ActiveSupport::TimeZone["Pacific Time (US & Canada)"], record.written_on.time_zone assert_equal Time.utc(2007, 12, 31, 16), record.written_on.time end end end - + def test_setting_time_zone_aware_attribute_to_blank_string_returns_nil in_time_zone "Pacific Time (US & Canada)" do record = @target.new @@ -202,7 +202,7 @@ def test_setting_time_zone_aware_attribute_interprets_time_zone_unaware_string_i record = @target.new record.written_on = time_string assert_equal Time.zone.parse(time_string), record.written_on - assert_equal TimeZone[timezone_offset], record.written_on.time_zone + assert_equal ActiveSupport::TimeZone[timezone_offset], record.written_on.time_zone assert_equal Time.utc(2008, 1, 1), record.written_on.time end end @@ -214,7 +214,7 @@ def test_setting_time_zone_aware_attribute_in_current_time_zone record = @target.new record.written_on = utc_time.in_time_zone assert_equal utc_time, record.written_on - assert_equal TimeZone["Pacific Time (US & Canada)"], record.written_on.time_zone + assert_equal ActiveSupport::TimeZone["Pacific Time (US & Canada)"], record.written_on.time_zone assert_equal Time.utc(2007, 12, 31, 16), record.written_on.time end end @@ -223,12 +223,12 @@ def test_setting_time_zone_aware_attribute_in_current_time_zone def time_related_columns_on_topic Topic.columns.select{|c| [:time, :date, :datetime, :timestamp].include?(c.type)}.map(&:name) end - + def in_time_zone(zone) old_zone = Time.zone old_tz = ActiveRecord::Base.time_zone_aware_attributes - Time.zone = zone ? TimeZone[zone] : nil + Time.zone = zone ? ActiveSupport::TimeZone[zone] : nil ActiveRecord::Base.time_zone_aware_attributes = !zone.nil? yield ensure diff --git a/activerecord/test/cases/base_test.rb b/activerecord/test/cases/base_test.rb index e07ec50c46a9d..a4be629fbdef2 100755 --- a/activerecord/test/cases/base_test.rb +++ b/activerecord/test/cases/base_test.rb @@ -133,19 +133,19 @@ def test_read_attributes_before_type_cast category_attrs = {"name"=>"Test categoty", "type" => nil} assert_equal category_attrs , category.attributes_before_type_cast end - + if current_adapter?(:MysqlAdapter) def test_read_attributes_before_type_cast_on_boolean bool = Booleantest.create({ "value" => false }) assert_equal 0, bool.attributes_before_type_cast["value"] end end - + def test_read_attributes_before_type_cast_on_datetime developer = Developer.find(:first) assert_equal developer.created_at.to_s(:db) , developer.attributes_before_type_cast["created_at"] end - + def test_hash_content topic = Topic.new topic.content = { "one" => 1, "two" => 2 } @@ -251,7 +251,7 @@ def test_create_through_factory topic = Topic.create("title" => "New Topic") topicReloaded = Topic.find(topic.id) assert_equal(topic, topicReloaded) - end + end def test_create_through_factory_with_block topic = Topic.create("title" => "New Topic") do |t| @@ -576,7 +576,7 @@ def test_table_name_guesses def test_destroy_all original_count = Topic.count topics_by_mary = Topic.count(:conditions => mary = "author_name = 'Mary'") - + Topic.destroy_all mary assert_equal original_count - topics_by_mary, Topic.count end @@ -665,7 +665,7 @@ def test_update_many def test_delete_all assert Topic.count > 0 - + assert_equal Topic.count, Topic.delete_all end @@ -970,7 +970,7 @@ def test_multiparameter_attributes_on_time topic.attributes = attributes assert_equal Time.local(2004, 6, 24, 16, 24, 0), topic.written_on end - + def test_multiparameter_attributes_on_time_with_old_date attributes = { "written_on(1i)" => "1850", "written_on(2i)" => "6", "written_on(3i)" => "24", @@ -998,7 +998,7 @@ def test_multiparameter_attributes_on_time_with_utc def test_multiparameter_attributes_on_time_with_time_zone_aware_attributes ActiveRecord::Base.time_zone_aware_attributes = true ActiveRecord::Base.default_timezone = :utc - Time.zone = TimeZone[-28800] + Time.zone = ActiveSupport::TimeZone[-28800] attributes = { "written_on(1i)" => "2004", "written_on(2i)" => "6", "written_on(3i)" => "24", "written_on(4i)" => "16", "written_on(5i)" => "24", "written_on(6i)" => "00" @@ -1016,7 +1016,7 @@ def test_multiparameter_attributes_on_time_with_time_zone_aware_attributes def test_multiparameter_attributes_on_time_with_time_zone_aware_attributes_false ActiveRecord::Base.time_zone_aware_attributes = false - Time.zone = TimeZone[-28800] + Time.zone = ActiveSupport::TimeZone[-28800] attributes = { "written_on(1i)" => "2004", "written_on(2i)" => "6", "written_on(3i)" => "24", "written_on(4i)" => "16", "written_on(5i)" => "24", "written_on(6i)" => "00" @@ -1032,7 +1032,7 @@ def test_multiparameter_attributes_on_time_with_time_zone_aware_attributes_false def test_multiparameter_attributes_on_time_with_skip_time_zone_conversion_for_attributes ActiveRecord::Base.time_zone_aware_attributes = true ActiveRecord::Base.default_timezone = :utc - Time.zone = TimeZone[-28800] + Time.zone = ActiveSupport::TimeZone[-28800] Topic.skip_time_zone_conversion_for_attributes = [:written_on] attributes = { "written_on(1i)" => "2004", "written_on(2i)" => "6", "written_on(3i)" => "24", @@ -1647,7 +1647,7 @@ def test_find_last last = Developer.find :last assert_equal last, Developer.find(:first, :order => 'id desc') end - + def test_last assert_equal Developer.find(:first, :order => 'id desc'), Developer.last end @@ -1655,7 +1655,7 @@ def test_last def test_all_with_conditions assert_equal Developer.find(:all, :order => 'id desc'), Developer.all(:order => 'id desc') end - + def test_find_ordered_last last = Developer.find :last, :order => 'developers.salary ASC' assert_equal last, Developer.find(:all, :order => 'developers.salary ASC').last @@ -1670,14 +1670,14 @@ def test_find_multiple_ordered_last last = Developer.find :last, :order => 'developers.name, developers.salary DESC' assert_equal last, Developer.find(:all, :order => 'developers.name, developers.salary DESC').last end - + def test_find_scoped_ordered_last last_developer = Developer.with_scope(:find => { :order => 'developers.salary ASC' }) do Developer.find(:last) end assert_equal last_developer, Developer.find(:all, :order => 'developers.salary ASC').last end - + def test_abstract_class assert !ActiveRecord::Base.abstract_class? assert LoosePerson.abstract_class? diff --git a/activerecord/test/cases/inheritance_test.rb b/activerecord/test/cases/inheritance_test.rb index f09b617e6d3bd..47fb5c608f27b 100755 --- a/activerecord/test/cases/inheritance_test.rb +++ b/activerecord/test/cases/inheritance_test.rb @@ -223,11 +223,11 @@ class InheritanceComputeTypeTest < ActiveRecord::TestCase fixtures :companies def setup - Dependencies.log_activity = true + ActiveSupport::Dependencies.log_activity = true end def teardown - Dependencies.log_activity = false + ActiveSupport::Dependencies.log_activity = false self.class.const_remove :FirmOnTheFly rescue nil Firm.const_remove :FirmOnTheFly rescue nil end diff --git a/activerecord/test/cases/multiple_db_test.rb b/activerecord/test/cases/multiple_db_test.rb index 236e4710a48ec..eb3e43c8ace49 100644 --- a/activerecord/test/cases/multiple_db_test.rb +++ b/activerecord/test/cases/multiple_db_test.rb @@ -51,7 +51,7 @@ def test_associations def test_course_connection_should_survive_dependency_reload assert Course.connection - Dependencies.clear + ActiveSupport::Dependencies.clear Object.send(:remove_const, :Course) require_dependency 'models/course' diff --git a/activesupport/lib/active_support.rb b/activesupport/lib/active_support.rb index e4cb145c98af5..982adfb0cf113 100644 --- a/activesupport/lib/active_support.rb +++ b/activesupport/lib/active_support.rb @@ -53,3 +53,7 @@ require 'active_support/base64' require 'active_support/time_with_zone' + +Inflector = ActiveSupport::Deprecation::DeprecatedConstantProxy.new('Inflector', 'ActiveSupport::Inflector') +Dependencies = ActiveSupport::Deprecation::DeprecatedConstantProxy.new('Dependencies', 'ActiveSupport::Dependencies') +TimeZone = ActiveSupport::Deprecation::DeprecatedConstantProxy.new('TimeZone', 'ActiveSupport::TimeZone') diff --git a/activesupport/lib/active_support/dependencies.rb b/activesupport/lib/active_support/dependencies.rb index da2ece610ac4e..7a8c4d0326731 100644 --- a/activesupport/lib/active_support/dependencies.rb +++ b/activesupport/lib/active_support/dependencies.rb @@ -3,458 +3,459 @@ require 'active_support/core_ext/load_error' require 'active_support/core_ext/kernel' -module Dependencies #:nodoc: - extend self - - # Should we turn on Ruby warnings on the first load of dependent files? - mattr_accessor :warnings_on_first_load - self.warnings_on_first_load = false - - # All files ever loaded. - mattr_accessor :history - self.history = Set.new - - # All files currently loaded. - mattr_accessor :loaded - self.loaded = Set.new - - # Should we load files or require them? - mattr_accessor :mechanism - self.mechanism = :load - - # The set of directories from which we may automatically load files. Files - # under these directories will be reloaded on each request in development mode, - # unless the directory also appears in load_once_paths. - mattr_accessor :load_paths - self.load_paths = [] - - # The set of directories from which automatically loaded constants are loaded - # only once. All directories in this set must also be present in +load_paths+. - mattr_accessor :load_once_paths - self.load_once_paths = [] - - # An array of qualified constant names that have been loaded. Adding a name to - # this array will cause it to be unloaded the next time Dependencies are cleared. - mattr_accessor :autoloaded_constants - self.autoloaded_constants = [] - - # An array of constant names that need to be unloaded on every request. Used - # to allow arbitrary constants to be marked for unloading. - mattr_accessor :explicitly_unloadable_constants - self.explicitly_unloadable_constants = [] - - # Set to true to enable logging of const_missing and file loads - mattr_accessor :log_activity - self.log_activity = false - - # An internal stack used to record which constants are loaded by any block. - mattr_accessor :constant_watch_stack - self.constant_watch_stack = [] - - def load? - mechanism == :load - end - - def depend_on(file_name, swallow_load_errors = false) - path = search_for_file(file_name) - require_or_load(path || file_name) - rescue LoadError - raise unless swallow_load_errors - end - - def associate_with(file_name) - depend_on(file_name, true) - end +module ActiveSupport #:nodoc: + module Dependencies #:nodoc: + extend self + + # Should we turn on Ruby warnings on the first load of dependent files? + mattr_accessor :warnings_on_first_load + self.warnings_on_first_load = false + + # All files ever loaded. + mattr_accessor :history + self.history = Set.new + + # All files currently loaded. + mattr_accessor :loaded + self.loaded = Set.new + + # Should we load files or require them? + mattr_accessor :mechanism + self.mechanism = :load + + # The set of directories from which we may automatically load files. Files + # under these directories will be reloaded on each request in development mode, + # unless the directory also appears in load_once_paths. + mattr_accessor :load_paths + self.load_paths = [] + + # The set of directories from which automatically loaded constants are loaded + # only once. All directories in this set must also be present in +load_paths+. + mattr_accessor :load_once_paths + self.load_once_paths = [] + + # An array of qualified constant names that have been loaded. Adding a name to + # this array will cause it to be unloaded the next time Dependencies are cleared. + mattr_accessor :autoloaded_constants + self.autoloaded_constants = [] + + # An array of constant names that need to be unloaded on every request. Used + # to allow arbitrary constants to be marked for unloading. + mattr_accessor :explicitly_unloadable_constants + self.explicitly_unloadable_constants = [] + + # Set to true to enable logging of const_missing and file loads + mattr_accessor :log_activity + self.log_activity = false + + # An internal stack used to record which constants are loaded by any block. + mattr_accessor :constant_watch_stack + self.constant_watch_stack = [] + + def load? + mechanism == :load + end - def clear - log_call - loaded.clear - remove_unloadable_constants! - end + def depend_on(file_name, swallow_load_errors = false) + path = search_for_file(file_name) + require_or_load(path || file_name) + rescue LoadError + raise unless swallow_load_errors + end - def require_or_load(file_name, const_path = nil) - log_call file_name, const_path - file_name = $1 if file_name =~ /^(.*)\.rb$/ - expanded = File.expand_path(file_name) - return if loaded.include?(expanded) + def associate_with(file_name) + depend_on(file_name, true) + end - # Record that we've seen this file *before* loading it to avoid an - # infinite loop with mutual dependencies. - loaded << expanded + def clear + log_call + loaded.clear + remove_unloadable_constants! + end - begin - if load? - log "loading #{file_name}" + def require_or_load(file_name, const_path = nil) + log_call file_name, const_path + file_name = $1 if file_name =~ /^(.*)\.rb$/ + expanded = File.expand_path(file_name) + return if loaded.include?(expanded) - # Enable warnings iff this file has not been loaded before and - # warnings_on_first_load is set. - load_args = ["#{file_name}.rb"] - load_args << const_path unless const_path.nil? + # Record that we've seen this file *before* loading it to avoid an + # infinite loop with mutual dependencies. + loaded << expanded - if !warnings_on_first_load or history.include?(expanded) - result = load_file(*load_args) + begin + if load? + log "loading #{file_name}" + + # Enable warnings iff this file has not been loaded before and + # warnings_on_first_load is set. + load_args = ["#{file_name}.rb"] + load_args << const_path unless const_path.nil? + + if !warnings_on_first_load or history.include?(expanded) + result = load_file(*load_args) + else + enable_warnings { result = load_file(*load_args) } + end else - enable_warnings { result = load_file(*load_args) } + log "requiring #{file_name}" + result = require file_name end - else - log "requiring #{file_name}" - result = require file_name + rescue Exception + loaded.delete expanded + raise end - rescue Exception - loaded.delete expanded - raise - end - # Record history *after* loading so first load gets warnings. - history << expanded - return result - end + # Record history *after* loading so first load gets warnings. + history << expanded + return result + end - # Is the provided constant path defined? - def qualified_const_defined?(path) - raise NameError, "#{path.inspect} is not a valid constant name!" unless - /^(::)?([A-Z]\w*)(::[A-Z]\w*)*$/ =~ path + # Is the provided constant path defined? + def qualified_const_defined?(path) + raise NameError, "#{path.inspect} is not a valid constant name!" unless + /^(::)?([A-Z]\w*)(::[A-Z]\w*)*$/ =~ path - names = path.to_s.split('::') - names.shift if names.first.empty? + names = path.to_s.split('::') + names.shift if names.first.empty? - # We can't use defined? because it will invoke const_missing for the parent - # of the name we are checking. - names.inject(Object) do |mod, name| - return false unless uninherited_const_defined?(mod, name) - mod.const_get name + # We can't use defined? because it will invoke const_missing for the parent + # of the name we are checking. + names.inject(Object) do |mod, name| + return false unless uninherited_const_defined?(mod, name) + mod.const_get name + end + return true end - return true - end - if Module.method(:const_defined?).arity == 1 - # Does this module define this constant? - # Wrapper to accomodate changing Module#const_defined? in Ruby 1.9 - def uninherited_const_defined?(mod, const) - mod.const_defined?(const) - end - else - def uninherited_const_defined?(mod, const) #:nodoc: - mod.const_defined?(const, false) + if Module.method(:const_defined?).arity == 1 + # Does this module define this constant? + # Wrapper to accomodate changing Module#const_defined? in Ruby 1.9 + def uninherited_const_defined?(mod, const) + mod.const_defined?(const) + end + else + def uninherited_const_defined?(mod, const) #:nodoc: + mod.const_defined?(const, false) + end end - end - - # Given +path+, a filesystem path to a ruby file, return an array of constant - # paths which would cause Dependencies to attempt to load this file. - def loadable_constants_for_path(path, bases = load_paths) - path = $1 if path =~ /\A(.*)\.rb\Z/ - expanded_path = File.expand_path(path) - - bases.collect do |root| - expanded_root = File.expand_path(root) - next unless %r{\A#{Regexp.escape(expanded_root)}(/|\\)} =~ expanded_path - - nesting = expanded_path[(expanded_root.size)..-1] - nesting = nesting[1..-1] if nesting && nesting[0] == ?/ - next if nesting.blank? - - [ - nesting.camelize, - # Special case: application.rb might define ApplicationControlller. - ('ApplicationController' if nesting == 'application') - ] - end.flatten.compact.uniq - end - # Search for a file in load_paths matching the provided suffix. - def search_for_file(path_suffix) - path_suffix = path_suffix + '.rb' unless path_suffix.ends_with? '.rb' - load_paths.each do |root| - path = File.join(root, path_suffix) - return path if File.file? path + # Given +path+, a filesystem path to a ruby file, return an array of constant + # paths which would cause Dependencies to attempt to load this file. + def loadable_constants_for_path(path, bases = load_paths) + path = $1 if path =~ /\A(.*)\.rb\Z/ + expanded_path = File.expand_path(path) + + bases.collect do |root| + expanded_root = File.expand_path(root) + next unless %r{\A#{Regexp.escape(expanded_root)}(/|\\)} =~ expanded_path + + nesting = expanded_path[(expanded_root.size)..-1] + nesting = nesting[1..-1] if nesting && nesting[0] == ?/ + next if nesting.blank? + + [ + nesting.camelize, + # Special case: application.rb might define ApplicationControlller. + ('ApplicationController' if nesting == 'application') + ] + end.flatten.compact.uniq end - nil # Gee, I sure wish we had first_match ;-) - end - # Does the provided path_suffix correspond to an autoloadable module? - # Instead of returning a boolean, the autoload base for this module is returned. - def autoloadable_module?(path_suffix) - load_paths.each do |load_path| - return load_path if File.directory? File.join(load_path, path_suffix) + # Search for a file in load_paths matching the provided suffix. + def search_for_file(path_suffix) + path_suffix = path_suffix + '.rb' unless path_suffix.ends_with? '.rb' + load_paths.each do |root| + path = File.join(root, path_suffix) + return path if File.file? path + end + nil # Gee, I sure wish we had first_match ;-) end - nil - end - - def load_once_path?(path) - load_once_paths.any? { |base| path.starts_with? base } - end - # Attempt to autoload the provided module name by searching for a directory - # matching the expect path suffix. If found, the module is created and assigned - # to +into+'s constants with the name +const_name+. Provided that the directory - # was loaded from a reloadable base path, it is added to the set of constants - # that are to be unloaded. - def autoload_module!(into, const_name, qualified_name, path_suffix) - return nil unless base_path = autoloadable_module?(path_suffix) - mod = Module.new - into.const_set const_name, mod - autoloaded_constants << qualified_name unless load_once_paths.include?(base_path) - return mod - end - - # Load the file at the provided path. +const_paths+ is a set of qualified - # constant names. When loading the file, Dependencies will watch for the - # addition of these constants. Each that is defined will be marked as - # autoloaded, and will be removed when Dependencies.clear is next called. - # - # If the second parameter is left off, then Dependencies will construct a set - # of names that the file at +path+ may define. See - # +loadable_constants_for_path+ for more details. - def load_file(path, const_paths = loadable_constants_for_path(path)) - log_call path, const_paths - const_paths = [const_paths].compact unless const_paths.is_a? Array - parent_paths = const_paths.collect { |const_path| /(.*)::[^:]+\Z/ =~ const_path ? $1 : :Object } - - result = nil - newly_defined_paths = new_constants_in(*parent_paths) do - result = load_without_new_constant_marking path + # Does the provided path_suffix correspond to an autoloadable module? + # Instead of returning a boolean, the autoload base for this module is returned. + def autoloadable_module?(path_suffix) + load_paths.each do |load_path| + return load_path if File.directory? File.join(load_path, path_suffix) + end + nil end - autoloaded_constants.concat newly_defined_paths unless load_once_path?(path) - autoloaded_constants.uniq! - log "loading #{path} defined #{newly_defined_paths * ', '}" unless newly_defined_paths.empty? - return result - end + def load_once_path?(path) + load_once_paths.any? { |base| path.starts_with? base } + end - # Return the constant path for the provided parent and constant name. - def qualified_name_for(mod, name) - mod_name = to_constant_name mod - (%w(Object Kernel).include? mod_name) ? name.to_s : "#{mod_name}::#{name}" - end + # Attempt to autoload the provided module name by searching for a directory + # matching the expect path suffix. If found, the module is created and assigned + # to +into+'s constants with the name +const_name+. Provided that the directory + # was loaded from a reloadable base path, it is added to the set of constants + # that are to be unloaded. + def autoload_module!(into, const_name, qualified_name, path_suffix) + return nil unless base_path = autoloadable_module?(path_suffix) + mod = Module.new + into.const_set const_name, mod + autoloaded_constants << qualified_name unless load_once_paths.include?(base_path) + return mod + end - # Load the constant named +const_name+ which is missing from +from_mod+. If - # it is not possible to load the constant into from_mod, try its parent module - # using const_missing. - def load_missing_constant(from_mod, const_name) - log_call from_mod, const_name - if from_mod == Kernel - if ::Object.const_defined?(const_name) - log "Returning Object::#{const_name} for Kernel::#{const_name}" - return ::Object.const_get(const_name) - else - log "Substituting Object for Kernel" - from_mod = Object + # Load the file at the provided path. +const_paths+ is a set of qualified + # constant names. When loading the file, Dependencies will watch for the + # addition of these constants. Each that is defined will be marked as + # autoloaded, and will be removed when Dependencies.clear is next called. + # + # If the second parameter is left off, then Dependencies will construct a set + # of names that the file at +path+ may define. See + # +loadable_constants_for_path+ for more details. + def load_file(path, const_paths = loadable_constants_for_path(path)) + log_call path, const_paths + const_paths = [const_paths].compact unless const_paths.is_a? Array + parent_paths = const_paths.collect { |const_path| /(.*)::[^:]+\Z/ =~ const_path ? $1 : :Object } + + result = nil + newly_defined_paths = new_constants_in(*parent_paths) do + result = load_without_new_constant_marking path end - end - # If we have an anonymous module, all we can do is attempt to load from Object. - from_mod = Object if from_mod.name.blank? + autoloaded_constants.concat newly_defined_paths unless load_once_path?(path) + autoloaded_constants.uniq! + log "loading #{path} defined #{newly_defined_paths * ', '}" unless newly_defined_paths.empty? + return result + end - unless qualified_const_defined?(from_mod.name) && from_mod.name.constantize.object_id == from_mod.object_id - raise ArgumentError, "A copy of #{from_mod} has been removed from the module tree but is still active!" + # Return the constant path for the provided parent and constant name. + def qualified_name_for(mod, name) + mod_name = to_constant_name mod + (%w(Object Kernel).include? mod_name) ? name.to_s : "#{mod_name}::#{name}" end - raise ArgumentError, "#{from_mod} is not missing constant #{const_name}!" if uninherited_const_defined?(from_mod, const_name) + # Load the constant named +const_name+ which is missing from +from_mod+. If + # it is not possible to load the constant into from_mod, try its parent module + # using const_missing. + def load_missing_constant(from_mod, const_name) + log_call from_mod, const_name + if from_mod == Kernel + if ::Object.const_defined?(const_name) + log "Returning Object::#{const_name} for Kernel::#{const_name}" + return ::Object.const_get(const_name) + else + log "Substituting Object for Kernel" + from_mod = Object + end + end - qualified_name = qualified_name_for from_mod, const_name - path_suffix = qualified_name.underscore - name_error = NameError.new("uninitialized constant #{qualified_name}") + # If we have an anonymous module, all we can do is attempt to load from Object. + from_mod = Object if from_mod.name.blank? - file_path = search_for_file(path_suffix) - if file_path && ! loaded.include?(File.expand_path(file_path)) # We found a matching file to load - require_or_load file_path - raise LoadError, "Expected #{file_path} to define #{qualified_name}" unless uninherited_const_defined?(from_mod, const_name) - return from_mod.const_get(const_name) - elsif mod = autoload_module!(from_mod, const_name, qualified_name, path_suffix) - return mod - elsif (parent = from_mod.parent) && parent != from_mod && - ! from_mod.parents.any? { |p| uninherited_const_defined?(p, const_name) } - # If our parents do not have a constant named +const_name+ then we are free - # to attempt to load upwards. If they do have such a constant, then this - # const_missing must be due to from_mod::const_name, which should not - # return constants from from_mod's parents. - begin - return parent.const_missing(const_name) - rescue NameError => e - raise unless e.missing_name? qualified_name_for(parent, const_name) + unless qualified_const_defined?(from_mod.name) && from_mod.name.constantize.object_id == from_mod.object_id + raise ArgumentError, "A copy of #{from_mod} has been removed from the module tree but is still active!" + end + + raise ArgumentError, "#{from_mod} is not missing constant #{const_name}!" if uninherited_const_defined?(from_mod, const_name) + + qualified_name = qualified_name_for from_mod, const_name + path_suffix = qualified_name.underscore + name_error = NameError.new("uninitialized constant #{qualified_name}") + + file_path = search_for_file(path_suffix) + if file_path && ! loaded.include?(File.expand_path(file_path)) # We found a matching file to load + require_or_load file_path + raise LoadError, "Expected #{file_path} to define #{qualified_name}" unless uninherited_const_defined?(from_mod, const_name) + return from_mod.const_get(const_name) + elsif mod = autoload_module!(from_mod, const_name, qualified_name, path_suffix) + return mod + elsif (parent = from_mod.parent) && parent != from_mod && + ! from_mod.parents.any? { |p| uninherited_const_defined?(p, const_name) } + # If our parents do not have a constant named +const_name+ then we are free + # to attempt to load upwards. If they do have such a constant, then this + # const_missing must be due to from_mod::const_name, which should not + # return constants from from_mod's parents. + begin + return parent.const_missing(const_name) + rescue NameError => e + raise unless e.missing_name? qualified_name_for(parent, const_name) + raise name_error + end + else raise name_error end - else - raise name_error end - end - # Remove the constants that have been autoloaded, and those that have been - # marked for unloading. - def remove_unloadable_constants! - autoloaded_constants.each { |const| remove_constant const } - autoloaded_constants.clear - explicitly_unloadable_constants.each { |const| remove_constant const } - end + # Remove the constants that have been autoloaded, and those that have been + # marked for unloading. + def remove_unloadable_constants! + autoloaded_constants.each { |const| remove_constant const } + autoloaded_constants.clear + explicitly_unloadable_constants.each { |const| remove_constant const } + end - # Determine if the given constant has been automatically loaded. - def autoloaded?(desc) - # No name => anonymous module. - return false if desc.is_a?(Module) && desc.name.blank? - name = to_constant_name desc - return false unless qualified_const_defined? name - return autoloaded_constants.include?(name) - end + # Determine if the given constant has been automatically loaded. + def autoloaded?(desc) + # No name => anonymous module. + return false if desc.is_a?(Module) && desc.name.blank? + name = to_constant_name desc + return false unless qualified_const_defined? name + return autoloaded_constants.include?(name) + end - # Will the provided constant descriptor be unloaded? - def will_unload?(const_desc) - autoloaded?(desc) || - explicitly_unloadable_constants.include?(to_constant_name(const_desc)) - end + # Will the provided constant descriptor be unloaded? + def will_unload?(const_desc) + autoloaded?(desc) || + explicitly_unloadable_constants.include?(to_constant_name(const_desc)) + end - # Mark the provided constant name for unloading. This constant will be - # unloaded on each request, not just the next one. - def mark_for_unload(const_desc) - name = to_constant_name const_desc - if explicitly_unloadable_constants.include? name - return false - else - explicitly_unloadable_constants << name - return true + # Mark the provided constant name for unloading. This constant will be + # unloaded on each request, not just the next one. + def mark_for_unload(const_desc) + name = to_constant_name const_desc + if explicitly_unloadable_constants.include? name + return false + else + explicitly_unloadable_constants << name + return true + end end - end - # Run the provided block and detect the new constants that were loaded during - # its execution. Constants may only be regarded as 'new' once -- so if the - # block calls +new_constants_in+ again, then the constants defined within the - # inner call will not be reported in this one. - # - # If the provided block does not run to completion, and instead raises an - # exception, any new constants are regarded as being only partially defined - # and will be removed immediately. - def new_constants_in(*descs) - log_call(*descs) - - # Build the watch frames. Each frame is a tuple of - # [module_name_as_string, constants_defined_elsewhere] - watch_frames = descs.collect do |desc| - if desc.is_a? Module - mod_name = desc.name - initial_constants = desc.local_constant_names - elsif desc.is_a?(String) || desc.is_a?(Symbol) - mod_name = desc.to_s - - # Handle the case where the module has yet to be defined. - initial_constants = if qualified_const_defined?(mod_name) - mod_name.constantize.local_constant_names + # Run the provided block and detect the new constants that were loaded during + # its execution. Constants may only be regarded as 'new' once -- so if the + # block calls +new_constants_in+ again, then the constants defined within the + # inner call will not be reported in this one. + # + # If the provided block does not run to completion, and instead raises an + # exception, any new constants are regarded as being only partially defined + # and will be removed immediately. + def new_constants_in(*descs) + log_call(*descs) + + # Build the watch frames. Each frame is a tuple of + # [module_name_as_string, constants_defined_elsewhere] + watch_frames = descs.collect do |desc| + if desc.is_a? Module + mod_name = desc.name + initial_constants = desc.local_constant_names + elsif desc.is_a?(String) || desc.is_a?(Symbol) + mod_name = desc.to_s + + # Handle the case where the module has yet to be defined. + initial_constants = if qualified_const_defined?(mod_name) + mod_name.constantize.local_constant_names + else + [] + end else - [] + raise Argument, "#{desc.inspect} does not describe a module!" end - else - raise Argument, "#{desc.inspect} does not describe a module!" - end - [mod_name, initial_constants] - end + [mod_name, initial_constants] + end - constant_watch_stack.concat watch_frames + constant_watch_stack.concat watch_frames - aborting = true - begin - yield # Now yield to the code that is to define new constants. - aborting = false - ensure - # Find the new constants. - new_constants = watch_frames.collect do |mod_name, prior_constants| - # Module still doesn't exist? Treat it as if it has no constants. - next [] unless qualified_const_defined?(mod_name) - - mod = mod_name.constantize - next [] unless mod.is_a? Module - new_constants = mod.local_constant_names - prior_constants - - # Make sure no other frames takes credit for these constants. - constant_watch_stack.each do |frame_name, constants| - constants.concat new_constants if frame_name == mod_name + aborting = true + begin + yield # Now yield to the code that is to define new constants. + aborting = false + ensure + # Find the new constants. + new_constants = watch_frames.collect do |mod_name, prior_constants| + # Module still doesn't exist? Treat it as if it has no constants. + next [] unless qualified_const_defined?(mod_name) + + mod = mod_name.constantize + next [] unless mod.is_a? Module + new_constants = mod.local_constant_names - prior_constants + + # Make sure no other frames takes credit for these constants. + constant_watch_stack.each do |frame_name, constants| + constants.concat new_constants if frame_name == mod_name + end + + new_constants.collect do |suffix| + mod_name == "Object" ? suffix : "#{mod_name}::#{suffix}" + end + end.flatten + + log "New constants: #{new_constants * ', '}" + + if aborting + log "Error during loading, removing partially loaded constants " + new_constants.each { |name| remove_constant name } + new_constants.clear end + end - new_constants.collect do |suffix| - mod_name == "Object" ? suffix : "#{mod_name}::#{suffix}" + return new_constants + ensure + # Remove the stack frames that we added. + if defined?(watch_frames) && ! watch_frames.blank? + frame_ids = watch_frames.collect(&:object_id) + constant_watch_stack.delete_if do |watch_frame| + frame_ids.include? watch_frame.object_id end - end.flatten - - log "New constants: #{new_constants * ', '}" - - if aborting - log "Error during loading, removing partially loaded constants " - new_constants.each { |name| remove_constant name } - new_constants.clear end end - return new_constants - ensure - # Remove the stack frames that we added. - if defined?(watch_frames) && ! watch_frames.blank? - frame_ids = watch_frames.collect(&:object_id) - constant_watch_stack.delete_if do |watch_frame| - frame_ids.include? watch_frame.object_id + class LoadingModule #:nodoc: + # Old style environment.rb referenced this method directly. Please note, it doesn't + # actually *do* anything any more. + def self.root(*args) + if defined?(RAILS_DEFAULT_LOGGER) + RAILS_DEFAULT_LOGGER.warn "Your environment.rb uses the old syntax, it may not continue to work in future releases." + RAILS_DEFAULT_LOGGER.warn "For upgrade instructions please see: http://manuals.rubyonrails.com/read/book/19" + end end end - end - class LoadingModule #:nodoc: - # Old style environment.rb referenced this method directly. Please note, it doesn't - # actually *do* anything any more. - def self.root(*args) - if defined?(RAILS_DEFAULT_LOGGER) - RAILS_DEFAULT_LOGGER.warn "Your environment.rb uses the old syntax, it may not continue to work in future releases." - RAILS_DEFAULT_LOGGER.warn "For upgrade instructions please see: http://manuals.rubyonrails.com/read/book/19" + # Convert the provided const desc to a qualified constant name (as a string). + # A module, class, symbol, or string may be provided. + def to_constant_name(desc) #:nodoc: + name = case desc + when String then desc.starts_with?('::') ? desc[2..-1] : desc + when Symbol then desc.to_s + when Module + raise ArgumentError, "Anonymous modules have no name to be referenced by" if desc.name.blank? + desc.name + else raise TypeError, "Not a valid constant descriptor: #{desc.inspect}" end end - end - # Convert the provided const desc to a qualified constant name (as a string). - # A module, class, symbol, or string may be provided. - def to_constant_name(desc) #:nodoc: - name = case desc - when String then desc.starts_with?('::') ? desc[2..-1] : desc - when Symbol then desc.to_s - when Module - raise ArgumentError, "Anonymous modules have no name to be referenced by" if desc.name.blank? - desc.name - else raise TypeError, "Not a valid constant descriptor: #{desc.inspect}" - end - end + def remove_constant(const) #:nodoc: + return false unless qualified_const_defined? const - def remove_constant(const) #:nodoc: - return false unless qualified_const_defined? const + const = $1 if /\A::(.*)\Z/ =~ const.to_s + names = const.to_s.split('::') + if names.size == 1 # It's under Object + parent = Object + else + parent = (names[0..-2] * '::').constantize + end - const = $1 if /\A::(.*)\Z/ =~ const.to_s - names = const.to_s.split('::') - if names.size == 1 # It's under Object - parent = Object - else - parent = (names[0..-2] * '::').constantize + log "removing constant #{const}" + parent.instance_eval { remove_const names.last } + return true end - log "removing constant #{const}" - parent.instance_eval { remove_const names.last } - return true - end - -protected - def log_call(*args) - if defined?(RAILS_DEFAULT_LOGGER) && RAILS_DEFAULT_LOGGER && log_activity - arg_str = args.collect(&:inspect) * ', ' - /in `([a-z_\?\!]+)'/ =~ caller(1).first - selector = $1 || '' - log "called #{selector}(#{arg_str})" - end - end + protected + def log_call(*args) + if defined?(RAILS_DEFAULT_LOGGER) && RAILS_DEFAULT_LOGGER && log_activity + arg_str = args.collect(&:inspect) * ', ' + /in `([a-z_\?\!]+)'/ =~ caller(1).first + selector = $1 || '' + log "called #{selector}(#{arg_str})" + end + end - def log(msg) - if defined?(RAILS_DEFAULT_LOGGER) && RAILS_DEFAULT_LOGGER && log_activity - RAILS_DEFAULT_LOGGER.debug "Dependencies: #{msg}" - end + def log(msg) + if defined?(RAILS_DEFAULT_LOGGER) && RAILS_DEFAULT_LOGGER && log_activity + RAILS_DEFAULT_LOGGER.debug "Dependencies: #{msg}" + end + end end - end Object.instance_eval do - define_method(:require_or_load) { |file_name| Dependencies.require_or_load(file_name) } unless Object.respond_to?(:require_or_load) - define_method(:require_dependency) { |file_name| Dependencies.depend_on(file_name) } unless Object.respond_to?(:require_dependency) - define_method(:require_association) { |file_name| Dependencies.associate_with(file_name) } unless Object.respond_to?(:require_association) + define_method(:require_or_load) { |file_name| ActiveSupport::Dependencies.require_or_load(file_name) } unless Object.respond_to?(:require_or_load) + define_method(:require_dependency) { |file_name| ActiveSupport::Dependencies.depend_on(file_name) } unless Object.respond_to?(:require_dependency) + define_method(:require_association) { |file_name| ActiveSupport::Dependencies.associate_with(file_name) } unless Object.respond_to?(:require_association) end class Module #:nodoc: @@ -464,7 +465,7 @@ class Module #:nodoc: # Use const_missing to autoload associations so we don't have to # require_association when using single-table inheritance. def const_missing(class_id) - Dependencies.load_missing_constant self, class_id + ActiveSupport::Dependencies.load_missing_constant self, class_id end def unloadable(const_desc = self) @@ -480,15 +481,15 @@ def const_missing(const_name) else begin begin - Dependencies.load_missing_constant self, const_name + ActiveSupport::Dependencies.load_missing_constant self, const_name rescue NameError parent.send :const_missing, const_name end rescue NameError => e # Make sure that the name we are missing is the one that caused the error - parent_qualified_name = Dependencies.qualified_name_for parent, const_name + parent_qualified_name = ActiveSupport::Dependencies.qualified_name_for parent, const_name raise unless e.missing_name? parent_qualified_name - qualified_name = Dependencies.qualified_name_for self, const_name + qualified_name = ActiveSupport::Dependencies.qualified_name_for self, const_name raise NameError.new("uninitialized constant #{qualified_name}").copy_blame!(e) end end @@ -499,14 +500,14 @@ class Object alias_method :load_without_new_constant_marking, :load def load(file, *extras) #:nodoc: - Dependencies.new_constants_in(Object) { super } + ActiveSupport::Dependencies.new_constants_in(Object) { super } rescue Exception => exception # errors from loading file exception.blame_file! file raise end def require(file, *extras) #:nodoc: - Dependencies.new_constants_in(Object) { super } + ActiveSupport::Dependencies.new_constants_in(Object) { super } rescue Exception => exception # errors from required file exception.blame_file! file raise @@ -526,7 +527,7 @@ def require(file, *extras) #:nodoc: # Returns true if the constant was not previously marked for unloading, false # otherwise. def unloadable(const_desc) - Dependencies.mark_for_unload const_desc + ActiveSupport::Dependencies.mark_for_unload const_desc end end diff --git a/activesupport/lib/active_support/deprecation.rb b/activesupport/lib/active_support/deprecation.rb index 758aef5445117..ebdaf86146f80 100644 --- a/activesupport/lib/active_support/deprecation.rb +++ b/activesupport/lib/active_support/deprecation.rb @@ -144,17 +144,11 @@ def collect_deprecations end end - # Stand-in for @request, @attributes, @params, etc. - # which emits deprecation warnings on any method call (except +inspect+). - class DeprecatedInstanceVariableProxy #:nodoc: + class DeprecationProxy #:nodoc: silence_warnings do instance_methods.each { |m| undef_method m unless m =~ /^__/ } end - def initialize(instance, method, var = "@#{method}") - @instance, @method, @var = instance, method, var - end - # Don't give a deprecation warning on inspect since test/unit and error # logs rely on it for diagnostics. def inspect @@ -166,7 +160,16 @@ def method_missing(called, *args, &block) warn caller, called, args target.__send__(called, *args, &block) end + end + # Stand-in for @request, @attributes, @params, etc. + # which emits deprecation warnings on any method call (except +inspect+). + class DeprecatedInstanceVariableProxy < DeprecationProxy #:nodoc: + def initialize(instance, method, var = "@#{method}") + @instance, @method, @var = instance, method, var + end + + private def target @instance.__send__(@method) end @@ -176,6 +179,21 @@ def warn(callstack, called, args) end end + class DeprecatedConstantProxy < DeprecationProxy #:nodoc: + def initialize(old_const, new_const) + @old_const = old_const + @new_const = new_const + end + + private + def target + @new_const.to_s.constantize + end + + def warn(callstack, called, args) + ActiveSupport::Deprecation.warn("#{@old_const} is deprecated! Use #{@new_const} instead.", callstack) + end + end end end diff --git a/activesupport/lib/active_support/inflections.rb b/activesupport/lib/active_support/inflections.rb index 967722c2bfa3f..b6d276953a71a 100644 --- a/activesupport/lib/active_support/inflections.rb +++ b/activesupport/lib/active_support/inflections.rb @@ -1,53 +1,55 @@ -Inflector.inflections do |inflect| - inflect.plural(/$/, 's') - inflect.plural(/s$/i, 's') - inflect.plural(/(ax|test)is$/i, '\1es') - inflect.plural(/(octop|vir)us$/i, '\1i') - inflect.plural(/(alias|status)$/i, '\1es') - inflect.plural(/(bu)s$/i, '\1ses') - inflect.plural(/(buffal|tomat)o$/i, '\1oes') - inflect.plural(/([ti])um$/i, '\1a') - inflect.plural(/sis$/i, 'ses') - inflect.plural(/(?:([^f])fe|([lr])f)$/i, '\1\2ves') - inflect.plural(/(hive)$/i, '\1s') - inflect.plural(/([^aeiouy]|qu)y$/i, '\1ies') - inflect.plural(/(x|ch|ss|sh)$/i, '\1es') - inflect.plural(/(matr|vert|ind)(?:ix|ex)$/i, '\1ices') - inflect.plural(/([m|l])ouse$/i, '\1ice') - inflect.plural(/^(ox)$/i, '\1en') - inflect.plural(/(quiz)$/i, '\1zes') +module ActiveSupport + Inflector.inflections do |inflect| + inflect.plural(/$/, 's') + inflect.plural(/s$/i, 's') + inflect.plural(/(ax|test)is$/i, '\1es') + inflect.plural(/(octop|vir)us$/i, '\1i') + inflect.plural(/(alias|status)$/i, '\1es') + inflect.plural(/(bu)s$/i, '\1ses') + inflect.plural(/(buffal|tomat)o$/i, '\1oes') + inflect.plural(/([ti])um$/i, '\1a') + inflect.plural(/sis$/i, 'ses') + inflect.plural(/(?:([^f])fe|([lr])f)$/i, '\1\2ves') + inflect.plural(/(hive)$/i, '\1s') + inflect.plural(/([^aeiouy]|qu)y$/i, '\1ies') + inflect.plural(/(x|ch|ss|sh)$/i, '\1es') + inflect.plural(/(matr|vert|ind)(?:ix|ex)$/i, '\1ices') + inflect.plural(/([m|l])ouse$/i, '\1ice') + inflect.plural(/^(ox)$/i, '\1en') + inflect.plural(/(quiz)$/i, '\1zes') - inflect.singular(/s$/i, '') - inflect.singular(/(n)ews$/i, '\1ews') - inflect.singular(/([ti])a$/i, '\1um') - inflect.singular(/((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/i, '\1\2sis') - inflect.singular(/(^analy)ses$/i, '\1sis') - inflect.singular(/([^f])ves$/i, '\1fe') - inflect.singular(/(hive)s$/i, '\1') - inflect.singular(/(tive)s$/i, '\1') - inflect.singular(/([lr])ves$/i, '\1f') - inflect.singular(/([^aeiouy]|qu)ies$/i, '\1y') - inflect.singular(/(s)eries$/i, '\1eries') - inflect.singular(/(m)ovies$/i, '\1ovie') - inflect.singular(/(x|ch|ss|sh)es$/i, '\1') - inflect.singular(/([m|l])ice$/i, '\1ouse') - inflect.singular(/(bus)es$/i, '\1') - inflect.singular(/(o)es$/i, '\1') - inflect.singular(/(shoe)s$/i, '\1') - inflect.singular(/(cris|ax|test)es$/i, '\1is') - inflect.singular(/(octop|vir)i$/i, '\1us') - inflect.singular(/(alias|status)es$/i, '\1') - inflect.singular(/^(ox)en/i, '\1') - inflect.singular(/(vert|ind)ices$/i, '\1ex') - inflect.singular(/(matr)ices$/i, '\1ix') - inflect.singular(/(quiz)zes$/i, '\1') + inflect.singular(/s$/i, '') + inflect.singular(/(n)ews$/i, '\1ews') + inflect.singular(/([ti])a$/i, '\1um') + inflect.singular(/((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/i, '\1\2sis') + inflect.singular(/(^analy)ses$/i, '\1sis') + inflect.singular(/([^f])ves$/i, '\1fe') + inflect.singular(/(hive)s$/i, '\1') + inflect.singular(/(tive)s$/i, '\1') + inflect.singular(/([lr])ves$/i, '\1f') + inflect.singular(/([^aeiouy]|qu)ies$/i, '\1y') + inflect.singular(/(s)eries$/i, '\1eries') + inflect.singular(/(m)ovies$/i, '\1ovie') + inflect.singular(/(x|ch|ss|sh)es$/i, '\1') + inflect.singular(/([m|l])ice$/i, '\1ouse') + inflect.singular(/(bus)es$/i, '\1') + inflect.singular(/(o)es$/i, '\1') + inflect.singular(/(shoe)s$/i, '\1') + inflect.singular(/(cris|ax|test)es$/i, '\1is') + inflect.singular(/(octop|vir)i$/i, '\1us') + inflect.singular(/(alias|status)es$/i, '\1') + inflect.singular(/^(ox)en/i, '\1') + inflect.singular(/(vert|ind)ices$/i, '\1ex') + inflect.singular(/(matr)ices$/i, '\1ix') + inflect.singular(/(quiz)zes$/i, '\1') - inflect.irregular('person', 'people') - inflect.irregular('man', 'men') - inflect.irregular('child', 'children') - inflect.irregular('sex', 'sexes') - inflect.irregular('move', 'moves') - inflect.irregular('cow', 'kine') + inflect.irregular('person', 'people') + inflect.irregular('man', 'men') + inflect.irregular('child', 'children') + inflect.irregular('sex', 'sexes') + inflect.irregular('move', 'moves') + inflect.irregular('cow', 'kine') - inflect.uncountable(%w(equipment information rice money species series fish sheep)) + inflect.uncountable(%w(equipment information rice money species series fish sheep)) + end end diff --git a/activesupport/lib/active_support/inflector.rb b/activesupport/lib/active_support/inflector.rb index a4fd6193178ec..fc88d80cdbcf5 100644 --- a/activesupport/lib/active_support/inflector.rb +++ b/activesupport/lib/active_support/inflector.rb @@ -1,305 +1,307 @@ require 'singleton' -# The Inflector transforms words from singular to plural, class names to table names, modularized class names to ones without, -# and class names to foreign keys. The default inflections for pluralization, singularization, and uncountable words are kept -# in inflections.rb. -# -# The Rails core team has stated patches for the inflections library will not be accepted -# in order to avoid breaking legacy applications which may be relying on errant inflections. -# If you discover an incorrect inflection and require it for your application, you'll need -# to correct it yourself (explained below). -module Inflector - # A singleton instance of this class is yielded by Inflector.inflections, which can then be used to specify additional - # inflection rules. Examples: +module ActiveSupport + # The Inflector transforms words from singular to plural, class names to table names, modularized class names to ones without, + # and class names to foreign keys. The default inflections for pluralization, singularization, and uncountable words are kept + # in inflections.rb. # - # Inflector.inflections do |inflect| - # inflect.plural /^(ox)$/i, '\1\2en' - # inflect.singular /^(ox)en/i, '\1' - # - # inflect.irregular 'octopus', 'octopi' - # - # inflect.uncountable "equipment" - # end - # - # New rules are added at the top. So in the example above, the irregular rule for octopus will now be the first of the - # pluralization and singularization rules that is runs. This guarantees that your rules run before any of the rules that may - # already have been loaded. - class Inflections - include Singleton + # The Rails core team has stated patches for the inflections library will not be accepted + # in order to avoid breaking legacy applications which may be relying on errant inflections. + # If you discover an incorrect inflection and require it for your application, you'll need + # to correct it yourself (explained below). + module Inflector + # A singleton instance of this class is yielded by Inflector.inflections, which can then be used to specify additional + # inflection rules. Examples: + # + # Inflector.inflections do |inflect| + # inflect.plural /^(ox)$/i, '\1\2en' + # inflect.singular /^(ox)en/i, '\1' + # + # inflect.irregular 'octopus', 'octopi' + # + # inflect.uncountable "equipment" + # end + # + # New rules are added at the top. So in the example above, the irregular rule for octopus will now be the first of the + # pluralization and singularization rules that is runs. This guarantees that your rules run before any of the rules that may + # already have been loaded. + class Inflections + include Singleton - attr_reader :plurals, :singulars, :uncountables + attr_reader :plurals, :singulars, :uncountables - def initialize - @plurals, @singulars, @uncountables = [], [], [] - end + def initialize + @plurals, @singulars, @uncountables = [], [], [] + end + + # Specifies a new pluralization rule and its replacement. The rule can either be a string or a regular expression. + # The replacement should always be a string that may include references to the matched data from the rule. + def plural(rule, replacement) + @plurals.insert(0, [rule, replacement]) + end + + # Specifies a new singularization rule and its replacement. The rule can either be a string or a regular expression. + # The replacement should always be a string that may include references to the matched data from the rule. + def singular(rule, replacement) + @singulars.insert(0, [rule, replacement]) + end - # Specifies a new pluralization rule and its replacement. The rule can either be a string or a regular expression. - # The replacement should always be a string that may include references to the matched data from the rule. - def plural(rule, replacement) - @plurals.insert(0, [rule, replacement]) + # Specifies a new irregular that applies to both pluralization and singularization at the same time. This can only be used + # for strings, not regular expressions. You simply pass the irregular in singular and plural form. + # + # Examples: + # irregular 'octopus', 'octopi' + # irregular 'person', 'people' + def irregular(singular, plural) + if singular[0,1].upcase == plural[0,1].upcase + plural(Regexp.new("(#{singular[0,1]})#{singular[1..-1]}$", "i"), '\1' + plural[1..-1]) + singular(Regexp.new("(#{plural[0,1]})#{plural[1..-1]}$", "i"), '\1' + singular[1..-1]) + else + plural(Regexp.new("#{singular[0,1].upcase}(?i)#{singular[1..-1]}$"), plural[0,1].upcase + plural[1..-1]) + plural(Regexp.new("#{singular[0,1].downcase}(?i)#{singular[1..-1]}$"), plural[0,1].downcase + plural[1..-1]) + singular(Regexp.new("#{plural[0,1].upcase}(?i)#{plural[1..-1]}$"), singular[0,1].upcase + singular[1..-1]) + singular(Regexp.new("#{plural[0,1].downcase}(?i)#{plural[1..-1]}$"), singular[0,1].downcase + singular[1..-1]) + end + end + + # Add uncountable words that shouldn't be attempted inflected. + # + # Examples: + # uncountable "money" + # uncountable "money", "information" + # uncountable %w( money information rice ) + def uncountable(*words) + (@uncountables << words).flatten! + end + + # Clears the loaded inflections within a given scope (default is :all). + # Give the scope as a symbol of the inflection type, the options are: :plurals, + # :singulars, :uncountables. + # + # Examples: + # clear :all + # clear :plurals + def clear(scope = :all) + case scope + when :all + @plurals, @singulars, @uncountables = [], [], [] + else + instance_variable_set "@#{scope}", [] + end + end end - # Specifies a new singularization rule and its replacement. The rule can either be a string or a regular expression. - # The replacement should always be a string that may include references to the matched data from the rule. - def singular(rule, replacement) - @singulars.insert(0, [rule, replacement]) + extend self + + # Yields a singleton instance of Inflector::Inflections so you can specify additional + # inflector rules. + # + # Example: + # Inflector.inflections do |inflect| + # inflect.uncountable "rails" + # end + def inflections + if block_given? + yield Inflections.instance + else + Inflections.instance + end end - # Specifies a new irregular that applies to both pluralization and singularization at the same time. This can only be used - # for strings, not regular expressions. You simply pass the irregular in singular and plural form. + # Returns the plural form of the word in the string. # # Examples: - # irregular 'octopus', 'octopi' - # irregular 'person', 'people' - def irregular(singular, plural) - if singular[0,1].upcase == plural[0,1].upcase - plural(Regexp.new("(#{singular[0,1]})#{singular[1..-1]}$", "i"), '\1' + plural[1..-1]) - singular(Regexp.new("(#{plural[0,1]})#{plural[1..-1]}$", "i"), '\1' + singular[1..-1]) + # "post".pluralize # => "posts" + # "octopus".pluralize # => "octopi" + # "sheep".pluralize # => "sheep" + # "words".pluralize # => "words" + # "the blue mailman".pluralize # => "the blue mailmen" + # "CamelOctopus".pluralize # => "CamelOctopi" + def pluralize(word) + result = word.to_s.dup + + if word.empty? || inflections.uncountables.include?(result.downcase) + result else - plural(Regexp.new("#{singular[0,1].upcase}(?i)#{singular[1..-1]}$"), plural[0,1].upcase + plural[1..-1]) - plural(Regexp.new("#{singular[0,1].downcase}(?i)#{singular[1..-1]}$"), plural[0,1].downcase + plural[1..-1]) - singular(Regexp.new("#{plural[0,1].upcase}(?i)#{plural[1..-1]}$"), singular[0,1].upcase + singular[1..-1]) - singular(Regexp.new("#{plural[0,1].downcase}(?i)#{plural[1..-1]}$"), singular[0,1].downcase + singular[1..-1]) + inflections.plurals.each { |(rule, replacement)| break if result.gsub!(rule, replacement) } + result end end - # Add uncountable words that shouldn't be attempted inflected. + # The reverse of +pluralize+, returns the singular form of a word in a string. # # Examples: - # uncountable "money" - # uncountable "money", "information" - # uncountable %w( money information rice ) - def uncountable(*words) - (@uncountables << words).flatten! + # "posts".singularize # => "post" + # "octopi".singularize # => "octopus" + # "sheep".singluarize # => "sheep" + # "word".singluarize # => "word" + # "the blue mailmen".singularize # => "the blue mailman" + # "CamelOctopi".singularize # => "CamelOctopus" + def singularize(word) + result = word.to_s.dup + + if inflections.uncountables.include?(result.downcase) + result + else + inflections.singulars.each { |(rule, replacement)| break if result.gsub!(rule, replacement) } + result + end end - # Clears the loaded inflections within a given scope (default is :all). - # Give the scope as a symbol of the inflection type, the options are: :plurals, - # :singulars, :uncountables. + # By default, +camelize+ converts strings to UpperCamelCase. If the argument to +camelize+ + # is set to :lower then +camelize+ produces lowerCamelCase. + # + # +camelize+ will also convert '/' to '::' which is useful for converting paths to namespaces. # # Examples: - # clear :all - # clear :plurals - def clear(scope = :all) - case scope - when :all - @plurals, @singulars, @uncountables = [], [], [] - else - instance_variable_set "@#{scope}", [] + # "active_record".camelize # => "ActiveRecord" + # "active_record".camelize(:lower) # => "activeRecord" + # "active_record/errors".camelize # => "ActiveRecord::Errors" + # "active_record/errors".camelize(:lower) # => "activeRecord::Errors" + def camelize(lower_case_and_underscored_word, first_letter_in_uppercase = true) + if first_letter_in_uppercase + lower_case_and_underscored_word.to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase } + else + lower_case_and_underscored_word.first + camelize(lower_case_and_underscored_word)[1..-1] end end - end - - extend self - # Yields a singleton instance of Inflector::Inflections so you can specify additional - # inflector rules. - # - # Example: - # Inflector.inflections do |inflect| - # inflect.uncountable "rails" - # end - def inflections - if block_given? - yield Inflections.instance - else - Inflections.instance + # Capitalizes all the words and replaces some characters in the string to create + # a nicer looking title. +titleize+ is meant for creating pretty output. It is not + # used in the Rails internals. + # + # +titleize+ is also aliased as as +titlecase+. + # + # Examples: + # "man from the boondocks".titleize # => "Man From The Boondocks" + # "x-men: the last stand".titleize # => "X Men: The Last Stand" + def titleize(word) + humanize(underscore(word)).gsub(/\b('?[a-z])/) { $1.capitalize } end - end - - # Returns the plural form of the word in the string. - # - # Examples: - # "post".pluralize # => "posts" - # "octopus".pluralize # => "octopi" - # "sheep".pluralize # => "sheep" - # "words".pluralize # => "words" - # "the blue mailman".pluralize # => "the blue mailmen" - # "CamelOctopus".pluralize # => "CamelOctopi" - def pluralize(word) - result = word.to_s.dup - if word.empty? || inflections.uncountables.include?(result.downcase) - result - else - inflections.plurals.each { |(rule, replacement)| break if result.gsub!(rule, replacement) } - result + # The reverse of +camelize+. Makes an underscored, lowercase form from the expression in the string. + # + # Changes '::' to '/' to convert namespaces to paths. + # + # Examples: + # "ActiveRecord".underscore # => "active_record" + # "ActiveRecord::Errors".underscore # => active_record/errors + def underscore(camel_cased_word) + camel_cased_word.to_s.gsub(/::/, '/'). + gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2'). + gsub(/([a-z\d])([A-Z])/,'\1_\2'). + tr("-", "_"). + downcase end - end - - # The reverse of +pluralize+, returns the singular form of a word in a string. - # - # Examples: - # "posts".singularize # => "post" - # "octopi".singularize # => "octopus" - # "sheep".singluarize # => "sheep" - # "word".singluarize # => "word" - # "the blue mailmen".singularize # => "the blue mailman" - # "CamelOctopi".singularize # => "CamelOctopus" - def singularize(word) - result = word.to_s.dup - if inflections.uncountables.include?(result.downcase) - result - else - inflections.singulars.each { |(rule, replacement)| break if result.gsub!(rule, replacement) } - result + # Replaces underscores with dashes in the string. + # + # Example: + # "puni_puni" # => "puni-puni" + def dasherize(underscored_word) + underscored_word.gsub(/_/, '-') end - end - # By default, +camelize+ converts strings to UpperCamelCase. If the argument to +camelize+ - # is set to :lower then +camelize+ produces lowerCamelCase. - # - # +camelize+ will also convert '/' to '::' which is useful for converting paths to namespaces. - # - # Examples: - # "active_record".camelize # => "ActiveRecord" - # "active_record".camelize(:lower) # => "activeRecord" - # "active_record/errors".camelize # => "ActiveRecord::Errors" - # "active_record/errors".camelize(:lower) # => "activeRecord::Errors" - def camelize(lower_case_and_underscored_word, first_letter_in_uppercase = true) - if first_letter_in_uppercase - lower_case_and_underscored_word.to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase } - else - lower_case_and_underscored_word.first + camelize(lower_case_and_underscored_word)[1..-1] + # Capitalizes the first word and turns underscores into spaces and strips a + # trailing "_id", if any. Like +titleize+, this is meant for creating pretty output. + # + # Examples: + # "employee_salary" # => "Employee salary" + # "author_id" # => "Author" + def humanize(lower_case_and_underscored_word) + lower_case_and_underscored_word.to_s.gsub(/_id$/, "").gsub(/_/, " ").capitalize end - end - # Capitalizes all the words and replaces some characters in the string to create - # a nicer looking title. +titleize+ is meant for creating pretty output. It is not - # used in the Rails internals. - # - # +titleize+ is also aliased as as +titlecase+. - # - # Examples: - # "man from the boondocks".titleize # => "Man From The Boondocks" - # "x-men: the last stand".titleize # => "X Men: The Last Stand" - def titleize(word) - humanize(underscore(word)).gsub(/\b('?[a-z])/) { $1.capitalize } - end - - # The reverse of +camelize+. Makes an underscored, lowercase form from the expression in the string. - # - # Changes '::' to '/' to convert namespaces to paths. - # - # Examples: - # "ActiveRecord".underscore # => "active_record" - # "ActiveRecord::Errors".underscore # => active_record/errors - def underscore(camel_cased_word) - camel_cased_word.to_s.gsub(/::/, '/'). - gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2'). - gsub(/([a-z\d])([A-Z])/,'\1_\2'). - tr("-", "_"). - downcase - end - - # Replaces underscores with dashes in the string. - # - # Example: - # "puni_puni" # => "puni-puni" - def dasherize(underscored_word) - underscored_word.gsub(/_/, '-') - end - - # Capitalizes the first word and turns underscores into spaces and strips a - # trailing "_id", if any. Like +titleize+, this is meant for creating pretty output. - # - # Examples: - # "employee_salary" # => "Employee salary" - # "author_id" # => "Author" - def humanize(lower_case_and_underscored_word) - lower_case_and_underscored_word.to_s.gsub(/_id$/, "").gsub(/_/, " ").capitalize - end + # Removes the module part from the expression in the string. + # + # Examples: + # "ActiveRecord::CoreExtensions::String::Inflections".demodulize # => "Inflections" + # "Inflections".demodulize # => "Inflections" + def demodulize(class_name_in_module) + class_name_in_module.to_s.gsub(/^.*::/, '') + end - # Removes the module part from the expression in the string. - # - # Examples: - # "ActiveRecord::CoreExtensions::String::Inflections".demodulize # => "Inflections" - # "Inflections".demodulize # => "Inflections" - def demodulize(class_name_in_module) - class_name_in_module.to_s.gsub(/^.*::/, '') - end + # Create the name of a table like Rails does for models to table names. This method + # uses the +pluralize+ method on the last word in the string. + # + # Examples + # "RawScaledScorer".tableize # => "raw_scaled_scorers" + # "egg_and_ham".tableize # => "egg_and_hams" + # "fancyCategory".tableize # => "fancy_categories" + def tableize(class_name) + pluralize(underscore(class_name)) + end - # Create the name of a table like Rails does for models to table names. This method - # uses the +pluralize+ method on the last word in the string. - # - # Examples - # "RawScaledScorer".tableize # => "raw_scaled_scorers" - # "egg_and_ham".tableize # => "egg_and_hams" - # "fancyCategory".tableize # => "fancy_categories" - def tableize(class_name) - pluralize(underscore(class_name)) - end + # Create a class name from a plural table name like Rails does for table names to models. + # Note that this returns a string and not a Class. (To convert to an actual class + # follow +classify+ with +constantize+.) + # + # Examples: + # "egg_and_hams".classify # => "EggAndHam" + # "posts".classify # => "Post" + # + # Singular names are not handled correctly: + # "business".classify # => "Busines" + def classify(table_name) + # strip out any leading schema name + camelize(singularize(table_name.to_s.sub(/.*\./, ''))) + end - # Create a class name from a plural table name like Rails does for table names to models. - # Note that this returns a string and not a Class. (To convert to an actual class - # follow +classify+ with +constantize+.) - # - # Examples: - # "egg_and_hams".classify # => "EggAndHam" - # "posts".classify # => "Post" - # - # Singular names are not handled correctly: - # "business".classify # => "Busines" - def classify(table_name) - # strip out any leading schema name - camelize(singularize(table_name.to_s.sub(/.*\./, ''))) - end + # Creates a foreign key name from a class name. + # +separate_class_name_and_id_with_underscore+ sets whether + # the method should put '_' between the name and 'id'. + # + # Examples: + # "Message".foreign_key # => "message_id" + # "Message".foreign_key(false) # => "messageid" + # "Admin::Post".foreign_key # => "post_id" + def foreign_key(class_name, separate_class_name_and_id_with_underscore = true) + underscore(demodulize(class_name)) + (separate_class_name_and_id_with_underscore ? "_id" : "id") + end - # Creates a foreign key name from a class name. - # +separate_class_name_and_id_with_underscore+ sets whether - # the method should put '_' between the name and 'id'. - # - # Examples: - # "Message".foreign_key # => "message_id" - # "Message".foreign_key(false) # => "messageid" - # "Admin::Post".foreign_key # => "post_id" - def foreign_key(class_name, separate_class_name_and_id_with_underscore = true) - underscore(demodulize(class_name)) + (separate_class_name_and_id_with_underscore ? "_id" : "id") - end + # Tries to find a constant with the name specified in the argument string: + # + # "Module".constantize # => Module + # "Test::Unit".constantize # => Test::Unit + # + # The name is assumed to be the one of a top-level constant, no matter whether + # it starts with "::" or not. No lexical context is taken into account: + # + # C = 'outside' + # module M + # C = 'inside' + # C # => 'inside' + # "C".constantize # => 'outside', same as ::C + # end + # + # NameError is raised when the name is not in CamelCase or the constant is + # unknown. + def constantize(camel_cased_word) + unless /\A(?:::)?([A-Z]\w*(?:::[A-Z]\w*)*)\z/ =~ camel_cased_word + raise NameError, "#{camel_cased_word.inspect} is not a valid constant name!" + end - # Tries to find a constant with the name specified in the argument string: - # - # "Module".constantize # => Module - # "Test::Unit".constantize # => Test::Unit - # - # The name is assumed to be the one of a top-level constant, no matter whether - # it starts with "::" or not. No lexical context is taken into account: - # - # C = 'outside' - # module M - # C = 'inside' - # C # => 'inside' - # "C".constantize # => 'outside', same as ::C - # end - # - # NameError is raised when the name is not in CamelCase or the constant is - # unknown. - def constantize(camel_cased_word) - unless /\A(?:::)?([A-Z]\w*(?:::[A-Z]\w*)*)\z/ =~ camel_cased_word - raise NameError, "#{camel_cased_word.inspect} is not a valid constant name!" + Object.module_eval("::#{$1}", __FILE__, __LINE__) end - Object.module_eval("::#{$1}", __FILE__, __LINE__) - end - - # Turns a number into an ordinal string used to denote the position in an - # ordered sequence such as 1st, 2nd, 3rd, 4th. - # - # Examples: - # ordinalize(1) # => "1st" - # ordinalize(2) # => "2nd" - # ordinalize(1002) # => "1002nd" - # ordinalize(1003) # => "1003rd" - def ordinalize(number) - if (11..13).include?(number.to_i % 100) - "#{number}th" - else - case number.to_i % 10 - when 1; "#{number}st" - when 2; "#{number}nd" - when 3; "#{number}rd" - else "#{number}th" + # Turns a number into an ordinal string used to denote the position in an + # ordered sequence such as 1st, 2nd, 3rd, 4th. + # + # Examples: + # ordinalize(1) # => "1st" + # ordinalize(2) # => "2nd" + # ordinalize(1002) # => "1002nd" + # ordinalize(1003) # => "1003rd" + def ordinalize(number) + if (11..13).include?(number.to_i % 100) + "#{number}th" + else + case number.to_i % 10 + when 1; "#{number}st" + when 2; "#{number}nd" + when 3; "#{number}rd" + else "#{number}th" + end end end end diff --git a/activesupport/lib/active_support/ordered_options.rb b/activesupport/lib/active_support/ordered_options.rb index 306376e9aeb89..642045186f56a 100644 --- a/activesupport/lib/active_support/ordered_options.rb +++ b/activesupport/lib/active_support/ordered_options.rb @@ -1,17 +1,19 @@ -class OrderedOptions < ActiveSupport::OrderedHash #:nodoc: - def []=(key, value) - super(key.to_sym, value) - end +module ActiveSupport #:nodoc: + class OrderedOptions < OrderedHash #:nodoc: + def []=(key, value) + super(key.to_sym, value) + end - def [](key) - super(key.to_sym) - end + def [](key) + super(key.to_sym) + end - def method_missing(name, *args) - if name.to_s =~ /(.*)=$/ - self[$1.to_sym] = args.first - else - self[name] + def method_missing(name, *args) + if name.to_s =~ /(.*)=$/ + self[$1.to_sym] = args.first + else + self[name] + end end end end diff --git a/activesupport/lib/active_support/values/time_zone.rb b/activesupport/lib/active_support/values/time_zone.rb index a71c819fba87b..5b2d42aa3cfbc 100644 --- a/activesupport/lib/active_support/values/time_zone.rb +++ b/activesupport/lib/active_support/values/time_zone.rb @@ -4,7 +4,7 @@ # * Retrieve and display zones with a friendlier name (e.g., "Eastern Time (US & Canada)" instead of "America/New_York"). # * Lazily load TZInfo::Timezone instances only when they're needed. # * Create ActiveSupport::TimeWithZone instances via TimeZone's +local+, +parse+, +at+ and +now+ methods. -# +# # If you set config.time_zone in the Rails Initializer, you can access this TimeZone object via Time.zone: # # # environment.rb: @@ -16,379 +16,381 @@ # Time.zone.name # => "Eastern Time (US & Canada)" # Time.zone.now # => Sun, 18 May 2008 14:30:44 EDT -04:00 # -# The version of TZInfo bundled with Active Support only includes the definitions necessary to support the zones +# The version of TZInfo bundled with Active Support only includes the definitions necessary to support the zones # defined by the TimeZone class. If you need to use zones that aren't defined by TimeZone, you'll need to install the TZInfo gem # (if a recent version of the gem is installed locally, this will be used instead of the bundled version.) -class TimeZone - unless const_defined?(:MAPPING) - # Keys are Rails TimeZone names, values are TZInfo identifiers - MAPPING = { - "International Date Line West" => "Pacific/Midway", - "Midway Island" => "Pacific/Midway", - "Samoa" => "Pacific/Pago_Pago", - "Hawaii" => "Pacific/Honolulu", - "Alaska" => "America/Juneau", - "Pacific Time (US & Canada)" => "America/Los_Angeles", - "Tijuana" => "America/Tijuana", - "Mountain Time (US & Canada)" => "America/Denver", - "Arizona" => "America/Phoenix", - "Chihuahua" => "America/Chihuahua", - "Mazatlan" => "America/Mazatlan", - "Central Time (US & Canada)" => "America/Chicago", - "Saskatchewan" => "America/Regina", - "Guadalajara" => "America/Mexico_City", - "Mexico City" => "America/Mexico_City", - "Monterrey" => "America/Monterrey", - "Central America" => "America/Guatemala", - "Eastern Time (US & Canada)" => "America/New_York", - "Indiana (East)" => "America/Indiana/Indianapolis", - "Bogota" => "America/Bogota", - "Lima" => "America/Lima", - "Quito" => "America/Lima", - "Atlantic Time (Canada)" => "America/Halifax", - "Caracas" => "America/Caracas", - "La Paz" => "America/La_Paz", - "Santiago" => "America/Santiago", - "Newfoundland" => "America/St_Johns", - "Brasilia" => "America/Argentina/Buenos_Aires", - "Buenos Aires" => "America/Argentina/Buenos_Aires", - "Georgetown" => "America/Argentina/San_Juan", - "Greenland" => "America/Godthab", - "Mid-Atlantic" => "Atlantic/South_Georgia", - "Azores" => "Atlantic/Azores", - "Cape Verde Is." => "Atlantic/Cape_Verde", - "Dublin" => "Europe/Dublin", - "Edinburgh" => "Europe/Dublin", - "Lisbon" => "Europe/Lisbon", - "London" => "Europe/London", - "Casablanca" => "Africa/Casablanca", - "Monrovia" => "Africa/Monrovia", - "UTC" => "Etc/UTC", - "Belgrade" => "Europe/Belgrade", - "Bratislava" => "Europe/Bratislava", - "Budapest" => "Europe/Budapest", - "Ljubljana" => "Europe/Ljubljana", - "Prague" => "Europe/Prague", - "Sarajevo" => "Europe/Sarajevo", - "Skopje" => "Europe/Skopje", - "Warsaw" => "Europe/Warsaw", - "Zagreb" => "Europe/Zagreb", - "Brussels" => "Europe/Brussels", - "Copenhagen" => "Europe/Copenhagen", - "Madrid" => "Europe/Madrid", - "Paris" => "Europe/Paris", - "Amsterdam" => "Europe/Amsterdam", - "Berlin" => "Europe/Berlin", - "Bern" => "Europe/Berlin", - "Rome" => "Europe/Rome", - "Stockholm" => "Europe/Stockholm", - "Vienna" => "Europe/Vienna", - "West Central Africa" => "Africa/Algiers", - "Bucharest" => "Europe/Bucharest", - "Cairo" => "Africa/Cairo", - "Helsinki" => "Europe/Helsinki", - "Kyev" => "Europe/Kiev", - "Riga" => "Europe/Riga", - "Sofia" => "Europe/Sofia", - "Tallinn" => "Europe/Tallinn", - "Vilnius" => "Europe/Vilnius", - "Athens" => "Europe/Athens", - "Istanbul" => "Europe/Istanbul", - "Minsk" => "Europe/Minsk", - "Jerusalem" => "Asia/Jerusalem", - "Harare" => "Africa/Harare", - "Pretoria" => "Africa/Johannesburg", - "Moscow" => "Europe/Moscow", - "St. Petersburg" => "Europe/Moscow", - "Volgograd" => "Europe/Moscow", - "Kuwait" => "Asia/Kuwait", - "Riyadh" => "Asia/Riyadh", - "Nairobi" => "Africa/Nairobi", - "Baghdad" => "Asia/Baghdad", - "Tehran" => "Asia/Tehran", - "Abu Dhabi" => "Asia/Muscat", - "Muscat" => "Asia/Muscat", - "Baku" => "Asia/Baku", - "Tbilisi" => "Asia/Tbilisi", - "Yerevan" => "Asia/Yerevan", - "Kabul" => "Asia/Kabul", - "Ekaterinburg" => "Asia/Yekaterinburg", - "Islamabad" => "Asia/Karachi", - "Karachi" => "Asia/Karachi", - "Tashkent" => "Asia/Tashkent", - "Chennai" => "Asia/Kolkata", - "Kolkata" => "Asia/Kolkata", - "Mumbai" => "Asia/Kolkata", - "New Delhi" => "Asia/Kolkata", - "Kathmandu" => "Asia/Katmandu", - "Astana" => "Asia/Dhaka", - "Dhaka" => "Asia/Dhaka", - "Sri Jayawardenepura" => "Asia/Dhaka", - "Almaty" => "Asia/Almaty", - "Novosibirsk" => "Asia/Novosibirsk", - "Rangoon" => "Asia/Rangoon", - "Bangkok" => "Asia/Bangkok", - "Hanoi" => "Asia/Bangkok", - "Jakarta" => "Asia/Jakarta", - "Krasnoyarsk" => "Asia/Krasnoyarsk", - "Beijing" => "Asia/Shanghai", - "Chongqing" => "Asia/Chongqing", - "Hong Kong" => "Asia/Hong_Kong", - "Urumqi" => "Asia/Urumqi", - "Kuala Lumpur" => "Asia/Kuala_Lumpur", - "Singapore" => "Asia/Singapore", - "Taipei" => "Asia/Taipei", - "Perth" => "Australia/Perth", - "Irkutsk" => "Asia/Irkutsk", - "Ulaan Bataar" => "Asia/Ulaanbaatar", - "Seoul" => "Asia/Seoul", - "Osaka" => "Asia/Tokyo", - "Sapporo" => "Asia/Tokyo", - "Tokyo" => "Asia/Tokyo", - "Yakutsk" => "Asia/Yakutsk", - "Darwin" => "Australia/Darwin", - "Adelaide" => "Australia/Adelaide", - "Canberra" => "Australia/Melbourne", - "Melbourne" => "Australia/Melbourne", - "Sydney" => "Australia/Sydney", - "Brisbane" => "Australia/Brisbane", - "Hobart" => "Australia/Hobart", - "Vladivostok" => "Asia/Vladivostok", - "Guam" => "Pacific/Guam", - "Port Moresby" => "Pacific/Port_Moresby", - "Magadan" => "Asia/Magadan", - "Solomon Is." => "Asia/Magadan", - "New Caledonia" => "Pacific/Noumea", - "Fiji" => "Pacific/Fiji", - "Kamchatka" => "Asia/Kamchatka", - "Marshall Is." => "Pacific/Majuro", - "Auckland" => "Pacific/Auckland", - "Wellington" => "Pacific/Auckland", - "Nuku'alofa" => "Pacific/Tongatapu" - }.each { |name, zone| name.freeze; zone.freeze } - MAPPING.freeze - end - - include Comparable - attr_reader :name +module ActiveSupport + class TimeZone + unless const_defined?(:MAPPING) + # Keys are Rails TimeZone names, values are TZInfo identifiers + MAPPING = { + "International Date Line West" => "Pacific/Midway", + "Midway Island" => "Pacific/Midway", + "Samoa" => "Pacific/Pago_Pago", + "Hawaii" => "Pacific/Honolulu", + "Alaska" => "America/Juneau", + "Pacific Time (US & Canada)" => "America/Los_Angeles", + "Tijuana" => "America/Tijuana", + "Mountain Time (US & Canada)" => "America/Denver", + "Arizona" => "America/Phoenix", + "Chihuahua" => "America/Chihuahua", + "Mazatlan" => "America/Mazatlan", + "Central Time (US & Canada)" => "America/Chicago", + "Saskatchewan" => "America/Regina", + "Guadalajara" => "America/Mexico_City", + "Mexico City" => "America/Mexico_City", + "Monterrey" => "America/Monterrey", + "Central America" => "America/Guatemala", + "Eastern Time (US & Canada)" => "America/New_York", + "Indiana (East)" => "America/Indiana/Indianapolis", + "Bogota" => "America/Bogota", + "Lima" => "America/Lima", + "Quito" => "America/Lima", + "Atlantic Time (Canada)" => "America/Halifax", + "Caracas" => "America/Caracas", + "La Paz" => "America/La_Paz", + "Santiago" => "America/Santiago", + "Newfoundland" => "America/St_Johns", + "Brasilia" => "America/Argentina/Buenos_Aires", + "Buenos Aires" => "America/Argentina/Buenos_Aires", + "Georgetown" => "America/Argentina/San_Juan", + "Greenland" => "America/Godthab", + "Mid-Atlantic" => "Atlantic/South_Georgia", + "Azores" => "Atlantic/Azores", + "Cape Verde Is." => "Atlantic/Cape_Verde", + "Dublin" => "Europe/Dublin", + "Edinburgh" => "Europe/Dublin", + "Lisbon" => "Europe/Lisbon", + "London" => "Europe/London", + "Casablanca" => "Africa/Casablanca", + "Monrovia" => "Africa/Monrovia", + "UTC" => "Etc/UTC", + "Belgrade" => "Europe/Belgrade", + "Bratislava" => "Europe/Bratislava", + "Budapest" => "Europe/Budapest", + "Ljubljana" => "Europe/Ljubljana", + "Prague" => "Europe/Prague", + "Sarajevo" => "Europe/Sarajevo", + "Skopje" => "Europe/Skopje", + "Warsaw" => "Europe/Warsaw", + "Zagreb" => "Europe/Zagreb", + "Brussels" => "Europe/Brussels", + "Copenhagen" => "Europe/Copenhagen", + "Madrid" => "Europe/Madrid", + "Paris" => "Europe/Paris", + "Amsterdam" => "Europe/Amsterdam", + "Berlin" => "Europe/Berlin", + "Bern" => "Europe/Berlin", + "Rome" => "Europe/Rome", + "Stockholm" => "Europe/Stockholm", + "Vienna" => "Europe/Vienna", + "West Central Africa" => "Africa/Algiers", + "Bucharest" => "Europe/Bucharest", + "Cairo" => "Africa/Cairo", + "Helsinki" => "Europe/Helsinki", + "Kyev" => "Europe/Kiev", + "Riga" => "Europe/Riga", + "Sofia" => "Europe/Sofia", + "Tallinn" => "Europe/Tallinn", + "Vilnius" => "Europe/Vilnius", + "Athens" => "Europe/Athens", + "Istanbul" => "Europe/Istanbul", + "Minsk" => "Europe/Minsk", + "Jerusalem" => "Asia/Jerusalem", + "Harare" => "Africa/Harare", + "Pretoria" => "Africa/Johannesburg", + "Moscow" => "Europe/Moscow", + "St. Petersburg" => "Europe/Moscow", + "Volgograd" => "Europe/Moscow", + "Kuwait" => "Asia/Kuwait", + "Riyadh" => "Asia/Riyadh", + "Nairobi" => "Africa/Nairobi", + "Baghdad" => "Asia/Baghdad", + "Tehran" => "Asia/Tehran", + "Abu Dhabi" => "Asia/Muscat", + "Muscat" => "Asia/Muscat", + "Baku" => "Asia/Baku", + "Tbilisi" => "Asia/Tbilisi", + "Yerevan" => "Asia/Yerevan", + "Kabul" => "Asia/Kabul", + "Ekaterinburg" => "Asia/Yekaterinburg", + "Islamabad" => "Asia/Karachi", + "Karachi" => "Asia/Karachi", + "Tashkent" => "Asia/Tashkent", + "Chennai" => "Asia/Kolkata", + "Kolkata" => "Asia/Kolkata", + "Mumbai" => "Asia/Kolkata", + "New Delhi" => "Asia/Kolkata", + "Kathmandu" => "Asia/Katmandu", + "Astana" => "Asia/Dhaka", + "Dhaka" => "Asia/Dhaka", + "Sri Jayawardenepura" => "Asia/Dhaka", + "Almaty" => "Asia/Almaty", + "Novosibirsk" => "Asia/Novosibirsk", + "Rangoon" => "Asia/Rangoon", + "Bangkok" => "Asia/Bangkok", + "Hanoi" => "Asia/Bangkok", + "Jakarta" => "Asia/Jakarta", + "Krasnoyarsk" => "Asia/Krasnoyarsk", + "Beijing" => "Asia/Shanghai", + "Chongqing" => "Asia/Chongqing", + "Hong Kong" => "Asia/Hong_Kong", + "Urumqi" => "Asia/Urumqi", + "Kuala Lumpur" => "Asia/Kuala_Lumpur", + "Singapore" => "Asia/Singapore", + "Taipei" => "Asia/Taipei", + "Perth" => "Australia/Perth", + "Irkutsk" => "Asia/Irkutsk", + "Ulaan Bataar" => "Asia/Ulaanbaatar", + "Seoul" => "Asia/Seoul", + "Osaka" => "Asia/Tokyo", + "Sapporo" => "Asia/Tokyo", + "Tokyo" => "Asia/Tokyo", + "Yakutsk" => "Asia/Yakutsk", + "Darwin" => "Australia/Darwin", + "Adelaide" => "Australia/Adelaide", + "Canberra" => "Australia/Melbourne", + "Melbourne" => "Australia/Melbourne", + "Sydney" => "Australia/Sydney", + "Brisbane" => "Australia/Brisbane", + "Hobart" => "Australia/Hobart", + "Vladivostok" => "Asia/Vladivostok", + "Guam" => "Pacific/Guam", + "Port Moresby" => "Pacific/Port_Moresby", + "Magadan" => "Asia/Magadan", + "Solomon Is." => "Asia/Magadan", + "New Caledonia" => "Pacific/Noumea", + "Fiji" => "Pacific/Fiji", + "Kamchatka" => "Asia/Kamchatka", + "Marshall Is." => "Pacific/Majuro", + "Auckland" => "Pacific/Auckland", + "Wellington" => "Pacific/Auckland", + "Nuku'alofa" => "Pacific/Tongatapu" + }.each { |name, zone| name.freeze; zone.freeze } + MAPPING.freeze + end - # Create a new TimeZone object with the given name and offset. The - # offset is the number of seconds that this time zone is offset from UTC - # (GMT). Seconds were chosen as the offset unit because that is the unit that - # Ruby uses to represent time zone offsets (see Time#utc_offset). - def initialize(name, utc_offset, tzinfo = nil) - @name = name - @utc_offset = utc_offset - @tzinfo = tzinfo - end + include Comparable + attr_reader :name - def utc_offset - @utc_offset ||= tzinfo.current_period.utc_offset - end - - # Returns the offset of this time zone as a formatted string, of the - # format "+HH:MM". - def formatted_offset(colon=true, alternate_utc_string = nil) - utc_offset == 0 && alternate_utc_string || utc_offset.to_utc_offset_s(colon) - end + # Create a new TimeZone object with the given name and offset. The + # offset is the number of seconds that this time zone is offset from UTC + # (GMT). Seconds were chosen as the offset unit because that is the unit that + # Ruby uses to represent time zone offsets (see Time#utc_offset). + def initialize(name, utc_offset, tzinfo = nil) + @name = name + @utc_offset = utc_offset + @tzinfo = tzinfo + end - # Compare this time zone to the parameter. The two are comapred first on - # their offsets, and then by name. - def <=>(zone) - result = (utc_offset <=> zone.utc_offset) - result = (name <=> zone.name) if result == 0 - result - end + def utc_offset + @utc_offset ||= tzinfo.current_period.utc_offset + end - # Returns a textual representation of this time zone. - def to_s - "(GMT#{formatted_offset}) #{name}" - end + # Returns the offset of this time zone as a formatted string, of the + # format "+HH:MM". + def formatted_offset(colon=true, alternate_utc_string = nil) + utc_offset == 0 && alternate_utc_string || utc_offset.to_utc_offset_s(colon) + end - # Method for creating new ActiveSupport::TimeWithZone instance in time zone of +self+ from given values. Example: - # - # Time.zone = "Hawaii" # => "Hawaii" - # Time.zone.local(2007, 2, 1, 15, 30, 45) # => Thu, 01 Feb 2007 15:30:45 HST -10:00 - def local(*args) - time = Time.utc_time(*args) - ActiveSupport::TimeWithZone.new(nil, self, time) - end + # Compare this time zone to the parameter. The two are comapred first on + # their offsets, and then by name. + def <=>(zone) + result = (utc_offset <=> zone.utc_offset) + result = (name <=> zone.name) if result == 0 + result + end - # Method for creating new ActiveSupport::TimeWithZone instance in time zone of +self+ from number of seconds since the Unix epoch. Example: - # - # Time.zone = "Hawaii" # => "Hawaii" - # Time.utc(2000).to_f # => 946684800.0 - # Time.zone.at(946684800.0) # => Fri, 31 Dec 1999 14:00:00 HST -10:00 - def at(secs) - utc = Time.at(secs).utc rescue DateTime.civil(1970).since(secs) - utc.in_time_zone(self) - end + # Returns a textual representation of this time zone. + def to_s + "(GMT#{formatted_offset}) #{name}" + end - # Method for creating new ActiveSupport::TimeWithZone instance in time zone of +self+ from parsed string. Example: - # - # Time.zone = "Hawaii" # => "Hawaii" - # Time.zone.parse('1999-12-31 14:00:00') # => Fri, 31 Dec 1999 14:00:00 HST -10:00 - # - # If upper components are missing from the string, they are supplied from TimeZone#now: - # - # Time.zone.now # => Fri, 31 Dec 1999 14:00:00 HST -10:00 - # Time.zone.parse('22:30:00') # => Fri, 31 Dec 1999 22:30:00 HST -10:00 - def parse(str, now=now) - date_parts = Date._parse(str) - return if date_parts.blank? - time = Time.parse(str, now) rescue DateTime.parse(str) - if date_parts[:offset].nil? + # Method for creating new ActiveSupport::TimeWithZone instance in time zone of +self+ from given values. Example: + # + # Time.zone = "Hawaii" # => "Hawaii" + # Time.zone.local(2007, 2, 1, 15, 30, 45) # => Thu, 01 Feb 2007 15:30:45 HST -10:00 + def local(*args) + time = Time.utc_time(*args) ActiveSupport::TimeWithZone.new(nil, self, time) - else - time.in_time_zone(self) end - end - # Returns an ActiveSupport::TimeWithZone instance representing the current time - # in the time zone represented by +self+. Example: - # - # Time.zone = 'Hawaii' # => "Hawaii" - # Time.zone.now # => Wed, 23 Jan 2008 20:24:27 HST -10:00 - def now - Time.now.utc.in_time_zone(self) - end + # Method for creating new ActiveSupport::TimeWithZone instance in time zone of +self+ from number of seconds since the Unix epoch. Example: + # + # Time.zone = "Hawaii" # => "Hawaii" + # Time.utc(2000).to_f # => 946684800.0 + # Time.zone.at(946684800.0) # => Fri, 31 Dec 1999 14:00:00 HST -10:00 + def at(secs) + utc = Time.at(secs).utc rescue DateTime.civil(1970).since(secs) + utc.in_time_zone(self) + end - # Return the current date in this time zone. - def today - tzinfo.now.to_date - end + # Method for creating new ActiveSupport::TimeWithZone instance in time zone of +self+ from parsed string. Example: + # + # Time.zone = "Hawaii" # => "Hawaii" + # Time.zone.parse('1999-12-31 14:00:00') # => Fri, 31 Dec 1999 14:00:00 HST -10:00 + # + # If upper components are missing from the string, they are supplied from TimeZone#now: + # + # Time.zone.now # => Fri, 31 Dec 1999 14:00:00 HST -10:00 + # Time.zone.parse('22:30:00') # => Fri, 31 Dec 1999 22:30:00 HST -10:00 + def parse(str, now=now) + date_parts = Date._parse(str) + return if date_parts.blank? + time = Time.parse(str, now) rescue DateTime.parse(str) + if date_parts[:offset].nil? + ActiveSupport::TimeWithZone.new(nil, self, time) + else + time.in_time_zone(self) + end + end - # Adjust the given time to the simultaneous time in the time zone represented by +self+. Returns a - # Time.utc() instance -- if you want an ActiveSupport::TimeWithZone instance, use Time#in_time_zone() instead. - def utc_to_local(time) - tzinfo.utc_to_local(time) - end + # Returns an ActiveSupport::TimeWithZone instance representing the current time + # in the time zone represented by +self+. Example: + # + # Time.zone = 'Hawaii' # => "Hawaii" + # Time.zone.now # => Wed, 23 Jan 2008 20:24:27 HST -10:00 + def now + Time.now.utc.in_time_zone(self) + end - # Adjust the given time to the simultaneous time in UTC. Returns a Time.utc() instance. - def local_to_utc(time, dst=true) - tzinfo.local_to_utc(time, dst) - end + # Return the current date in this time zone. + def today + tzinfo.now.to_date + end - # Available so that TimeZone instances respond like TZInfo::Timezone instances - def period_for_utc(time) - tzinfo.period_for_utc(time) - end + # Adjust the given time to the simultaneous time in the time zone represented by +self+. Returns a + # Time.utc() instance -- if you want an ActiveSupport::TimeWithZone instance, use Time#in_time_zone() instead. + def utc_to_local(time) + tzinfo.utc_to_local(time) + end - # Available so that TimeZone instances respond like TZInfo::Timezone instances - def period_for_local(time, dst=true) - tzinfo.period_for_local(time, dst) - end + # Adjust the given time to the simultaneous time in UTC. Returns a Time.utc() instance. + def local_to_utc(time, dst=true) + tzinfo.local_to_utc(time, dst) + end - # TODO: Preload instead of lazy load for thread safety - def tzinfo - @tzinfo ||= TZInfo::Timezone.get(MAPPING[name]) - end + # Available so that TimeZone instances respond like TZInfo::Timezone instances + def period_for_utc(time) + tzinfo.period_for_utc(time) + end - unless const_defined?(:ZONES) - ZONES = [] - ZONES_MAP = {} - [[-39_600, "International Date Line West", "Midway Island", "Samoa" ], - [-36_000, "Hawaii" ], - [-32_400, "Alaska" ], - [-28_800, "Pacific Time (US & Canada)", "Tijuana" ], - [-25_200, "Mountain Time (US & Canada)", "Chihuahua", "Mazatlan", - "Arizona" ], - [-21_600, "Central Time (US & Canada)", "Saskatchewan", "Guadalajara", - "Mexico City", "Monterrey", "Central America" ], - [-18_000, "Eastern Time (US & Canada)", "Indiana (East)", "Bogota", - "Lima", "Quito" ], - [-14_400, "Atlantic Time (Canada)", "Caracas", "La Paz", "Santiago" ], - [-12_600, "Newfoundland" ], - [-10_800, "Brasilia", "Buenos Aires", "Georgetown", "Greenland" ], - [ -7_200, "Mid-Atlantic" ], - [ -3_600, "Azores", "Cape Verde Is." ], - [ 0, "Dublin", "Edinburgh", "Lisbon", "London", "Casablanca", - "Monrovia", "UTC" ], - [ 3_600, "Belgrade", "Bratislava", "Budapest", "Ljubljana", "Prague", - "Sarajevo", "Skopje", "Warsaw", "Zagreb", "Brussels", - "Copenhagen", "Madrid", "Paris", "Amsterdam", "Berlin", - "Bern", "Rome", "Stockholm", "Vienna", - "West Central Africa" ], - [ 7_200, "Bucharest", "Cairo", "Helsinki", "Kyev", "Riga", "Sofia", - "Tallinn", "Vilnius", "Athens", "Istanbul", "Minsk", - "Jerusalem", "Harare", "Pretoria" ], - [ 10_800, "Moscow", "St. Petersburg", "Volgograd", "Kuwait", "Riyadh", - "Nairobi", "Baghdad" ], - [ 12_600, "Tehran" ], - [ 14_400, "Abu Dhabi", "Muscat", "Baku", "Tbilisi", "Yerevan" ], - [ 16_200, "Kabul" ], - [ 18_000, "Ekaterinburg", "Islamabad", "Karachi", "Tashkent" ], - [ 19_800, "Chennai", "Kolkata", "Mumbai", "New Delhi" ], - [ 20_700, "Kathmandu" ], - [ 21_600, "Astana", "Dhaka", "Sri Jayawardenepura", "Almaty", - "Novosibirsk" ], - [ 23_400, "Rangoon" ], - [ 25_200, "Bangkok", "Hanoi", "Jakarta", "Krasnoyarsk" ], - [ 28_800, "Beijing", "Chongqing", "Hong Kong", "Urumqi", - "Kuala Lumpur", "Singapore", "Taipei", "Perth", "Irkutsk", - "Ulaan Bataar" ], - [ 32_400, "Seoul", "Osaka", "Sapporo", "Tokyo", "Yakutsk" ], - [ 34_200, "Darwin", "Adelaide" ], - [ 36_000, "Canberra", "Melbourne", "Sydney", "Brisbane", "Hobart", - "Vladivostok", "Guam", "Port Moresby" ], - [ 39_600, "Magadan", "Solomon Is.", "New Caledonia" ], - [ 43_200, "Fiji", "Kamchatka", "Marshall Is.", "Auckland", - "Wellington" ], - [ 46_800, "Nuku'alofa" ]]. - each do |offset, *places| - places.each do |place| - place.freeze - zone = new(place, offset) - ZONES << zone - ZONES_MAP[place] = zone - end + # Available so that TimeZone instances respond like TZInfo::Timezone instances + def period_for_local(time, dst=true) + tzinfo.period_for_local(time, dst) end - ZONES.sort! - ZONES.freeze - ZONES_MAP.freeze - US_ZONES = ZONES.find_all { |z| z.name =~ /US|Arizona|Indiana|Hawaii|Alaska/ } - US_ZONES.freeze - end + # TODO: Preload instead of lazy load for thread safety + def tzinfo + @tzinfo ||= TZInfo::Timezone.get(MAPPING[name]) + end - class << self - alias_method :create, :new + unless const_defined?(:ZONES) + ZONES = [] + ZONES_MAP = {} + [[-39_600, "International Date Line West", "Midway Island", "Samoa" ], + [-36_000, "Hawaii" ], + [-32_400, "Alaska" ], + [-28_800, "Pacific Time (US & Canada)", "Tijuana" ], + [-25_200, "Mountain Time (US & Canada)", "Chihuahua", "Mazatlan", + "Arizona" ], + [-21_600, "Central Time (US & Canada)", "Saskatchewan", "Guadalajara", + "Mexico City", "Monterrey", "Central America" ], + [-18_000, "Eastern Time (US & Canada)", "Indiana (East)", "Bogota", + "Lima", "Quito" ], + [-14_400, "Atlantic Time (Canada)", "Caracas", "La Paz", "Santiago" ], + [-12_600, "Newfoundland" ], + [-10_800, "Brasilia", "Buenos Aires", "Georgetown", "Greenland" ], + [ -7_200, "Mid-Atlantic" ], + [ -3_600, "Azores", "Cape Verde Is." ], + [ 0, "Dublin", "Edinburgh", "Lisbon", "London", "Casablanca", + "Monrovia", "UTC" ], + [ 3_600, "Belgrade", "Bratislava", "Budapest", "Ljubljana", "Prague", + "Sarajevo", "Skopje", "Warsaw", "Zagreb", "Brussels", + "Copenhagen", "Madrid", "Paris", "Amsterdam", "Berlin", + "Bern", "Rome", "Stockholm", "Vienna", + "West Central Africa" ], + [ 7_200, "Bucharest", "Cairo", "Helsinki", "Kyev", "Riga", "Sofia", + "Tallinn", "Vilnius", "Athens", "Istanbul", "Minsk", + "Jerusalem", "Harare", "Pretoria" ], + [ 10_800, "Moscow", "St. Petersburg", "Volgograd", "Kuwait", "Riyadh", + "Nairobi", "Baghdad" ], + [ 12_600, "Tehran" ], + [ 14_400, "Abu Dhabi", "Muscat", "Baku", "Tbilisi", "Yerevan" ], + [ 16_200, "Kabul" ], + [ 18_000, "Ekaterinburg", "Islamabad", "Karachi", "Tashkent" ], + [ 19_800, "Chennai", "Kolkata", "Mumbai", "New Delhi" ], + [ 20_700, "Kathmandu" ], + [ 21_600, "Astana", "Dhaka", "Sri Jayawardenepura", "Almaty", + "Novosibirsk" ], + [ 23_400, "Rangoon" ], + [ 25_200, "Bangkok", "Hanoi", "Jakarta", "Krasnoyarsk" ], + [ 28_800, "Beijing", "Chongqing", "Hong Kong", "Urumqi", + "Kuala Lumpur", "Singapore", "Taipei", "Perth", "Irkutsk", + "Ulaan Bataar" ], + [ 32_400, "Seoul", "Osaka", "Sapporo", "Tokyo", "Yakutsk" ], + [ 34_200, "Darwin", "Adelaide" ], + [ 36_000, "Canberra", "Melbourne", "Sydney", "Brisbane", "Hobart", + "Vladivostok", "Guam", "Port Moresby" ], + [ 39_600, "Magadan", "Solomon Is.", "New Caledonia" ], + [ 43_200, "Fiji", "Kamchatka", "Marshall Is.", "Auckland", + "Wellington" ], + [ 46_800, "Nuku'alofa" ]]. + each do |offset, *places| + places.each do |place| + place.freeze + zone = new(place, offset) + ZONES << zone + ZONES_MAP[place] = zone + end + end + ZONES.sort! + ZONES.freeze + ZONES_MAP.freeze - # Return a TimeZone instance with the given name, or +nil+ if no - # such TimeZone instance exists. (This exists to support the use of - # this class with the +composed_of+ macro.) - def new(name) - self[name] + US_ZONES = ZONES.find_all { |z| z.name =~ /US|Arizona|Indiana|Hawaii|Alaska/ } + US_ZONES.freeze end - # Return an array of all TimeZone objects. There are multiple - # TimeZone objects per time zone, in many cases, to make it easier - # for users to find their own time zone. - def all - ZONES - end + class << self + alias_method :create, :new - # Locate a specific time zone object. If the argument is a string, it - # is interpreted to mean the name of the timezone to locate. If it is a - # numeric value it is either the hour offset, or the second offset, of the - # timezone to find. (The first one with that offset will be returned.) - # Returns +nil+ if no such time zone is known to the system. - def [](arg) - case arg - when String - ZONES_MAP[arg] - when Numeric, ActiveSupport::Duration - arg *= 3600 if arg.abs <= 13 - all.find { |z| z.utc_offset == arg.to_i } - else - raise ArgumentError, "invalid argument to TimeZone[]: #{arg.inspect}" + # Return a TimeZone instance with the given name, or +nil+ if no + # such TimeZone instance exists. (This exists to support the use of + # this class with the +composed_of+ macro.) + def new(name) + self[name] end - end - # A convenience method for returning a collection of TimeZone objects - # for time zones in the USA. - def us_zones - US_ZONES + # Return an array of all TimeZone objects. There are multiple + # TimeZone objects per time zone, in many cases, to make it easier + # for users to find their own time zone. + def all + ZONES + end + + # Locate a specific time zone object. If the argument is a string, it + # is interpreted to mean the name of the timezone to locate. If it is a + # numeric value it is either the hour offset, or the second offset, of the + # timezone to find. (The first one with that offset will be returned.) + # Returns +nil+ if no such time zone is known to the system. + def [](arg) + case arg + when String + ZONES_MAP[arg] + when Numeric, ActiveSupport::Duration + arg *= 3600 if arg.abs <= 13 + all.find { |z| z.utc_offset == arg.to_i } + else + raise ArgumentError, "invalid argument to TimeZone[]: #{arg.inspect}" + end + end + + # A convenience method for returning a collection of TimeZone objects + # for time zones in the USA. + def us_zones + US_ZONES + end end end end diff --git a/activesupport/test/core_ext/date_ext_test.rb b/activesupport/test/core_ext/date_ext_test.rb index 11b6056047fca..ddfe1f904fe52 100644 --- a/activesupport/test/core_ext/date_ext_test.rb +++ b/activesupport/test/core_ext/date_ext_test.rb @@ -172,7 +172,7 @@ def test_next_month_on_31st def test_last_month_on_31st assert_equal Date.new(2004, 2, 29), Date.new(2004, 3, 31).last_month - end + end def test_yesterday_constructor assert_equal Date.today - 1, Date.yesterday @@ -197,11 +197,11 @@ def test_beginning_of_day def test_end_of_day assert_equal Time.local(2005,2,21,23,59,59), Date.new(2005,2,21).end_of_day end - + def test_date_acts_like_date assert Date.new.acts_like_date? end - + def test_xmlschema with_env_tz 'US/Eastern' do assert_match(/^1980-02-28T00:00:00-05:?00$/, Date.new(1980, 2, 28).xmlschema) @@ -213,7 +213,7 @@ def test_xmlschema end end end - + uses_mocha 'TestDateCurrent' do def test_current_returns_date_today_when_zone_default_not_set with_env_tz 'US/Central' do @@ -222,10 +222,10 @@ def test_current_returns_date_today_when_zone_default_not_set assert_equal Date.new(1999, 12, 31), Date.current end end - + def test_current_returns_time_zone_today_when_zone_default_set silence_warnings do # silence warnings raised by tzinfo gem - Time.zone_default = TimeZone['Eastern Time (US & Canada)'] + Time.zone_default = ActiveSupport::TimeZone['Eastern Time (US & Canada)'] with_env_tz 'US/Central' do Time.stubs(:now).returns Time.local(1999, 12, 31, 23) assert_equal Date.new(1999, 12, 31), Date.today @@ -243,5 +243,5 @@ def with_env_tz(new_tz = 'US/Eastern') yield ensure old_tz ? ENV['TZ'] = old_tz : ENV.delete('TZ') - end + end end diff --git a/activesupport/test/core_ext/date_time_ext_test.rb b/activesupport/test/core_ext/date_time_ext_test.rb index ed45daf4033b1..854a3a05e1a10 100644 --- a/activesupport/test/core_ext/date_time_ext_test.rb +++ b/activesupport/test/core_ext/date_time_ext_test.rb @@ -219,14 +219,14 @@ def test_local_offset assert_equal Rational(-6, 24), DateTime.local_offset end end - + def test_utc? assert_equal true, DateTime.civil(2005, 2, 21, 10, 11, 12).utc? assert_equal true, DateTime.civil(2005, 2, 21, 10, 11, 12, 0).utc? assert_equal false, DateTime.civil(2005, 2, 21, 10, 11, 12, 0.25).utc? assert_equal false, DateTime.civil(2005, 2, 21, 10, 11, 12, -0.25).utc? end - + def test_utc_offset assert_equal 0, DateTime.civil(2005, 2, 21, 10, 11, 12).utc_offset assert_equal 0, DateTime.civil(2005, 2, 21, 10, 11, 12, 0).utc_offset @@ -234,7 +234,7 @@ def test_utc_offset assert_equal( -21600, DateTime.civil(2005, 2, 21, 10, 11, 12, -0.25).utc_offset ) assert_equal( -18000, DateTime.civil(2005, 2, 21, 10, 11, 12, Rational(-5, 24)).utc_offset ) end - + def test_utc assert_equal DateTime.civil(2005, 2, 21, 16, 11, 12, 0), DateTime.civil(2005, 2, 21, 10, 11, 12, Rational(-6, 24)).utc assert_equal DateTime.civil(2005, 2, 21, 15, 11, 12, 0), DateTime.civil(2005, 2, 21, 10, 11, 12, Rational(-5, 24)).utc @@ -242,37 +242,37 @@ def test_utc assert_equal DateTime.civil(2005, 2, 21, 9, 11, 12, 0), DateTime.civil(2005, 2, 21, 10, 11, 12, Rational(1, 24)).utc assert_equal DateTime.civil(2005, 2, 21, 9, 11, 12, 0), DateTime.civil(2005, 2, 21, 10, 11, 12, Rational(1, 24)).getutc end - + def test_formatted_offset_with_utc assert_equal '+00:00', DateTime.civil(2000).formatted_offset assert_equal '+0000', DateTime.civil(2000).formatted_offset(false) assert_equal 'UTC', DateTime.civil(2000).formatted_offset(true, 'UTC') end - + def test_formatted_offset_with_local dt = DateTime.civil(2005, 2, 21, 10, 11, 12, Rational(-5, 24)) assert_equal '-05:00', dt.formatted_offset assert_equal '-0500', dt.formatted_offset(false) end - + def test_compare_with_time assert_equal 1, DateTime.civil(2000) <=> Time.utc(1999, 12, 31, 23, 59, 59) assert_equal 0, DateTime.civil(2000) <=> Time.utc(2000, 1, 1, 0, 0, 0) assert_equal(-1, DateTime.civil(2000) <=> Time.utc(2000, 1, 1, 0, 0, 1)) end - + def test_compare_with_datetime assert_equal 1, DateTime.civil(2000) <=> DateTime.civil(1999, 12, 31, 23, 59, 59) assert_equal 0, DateTime.civil(2000) <=> DateTime.civil(2000, 1, 1, 0, 0, 0) assert_equal(-1, DateTime.civil(2000) <=> DateTime.civil(2000, 1, 1, 0, 0, 1)) end - + def test_compare_with_time_with_zone - assert_equal 1, DateTime.civil(2000) <=> ActiveSupport::TimeWithZone.new( Time.utc(1999, 12, 31, 23, 59, 59), TimeZone['UTC'] ) - assert_equal 0, DateTime.civil(2000) <=> ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1, 0, 0, 0), TimeZone['UTC'] ) - assert_equal(-1, DateTime.civil(2000) <=> ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1, 0, 0, 1), TimeZone['UTC'] )) + assert_equal 1, DateTime.civil(2000) <=> ActiveSupport::TimeWithZone.new( Time.utc(1999, 12, 31, 23, 59, 59), ActiveSupport::TimeZone['UTC'] ) + assert_equal 0, DateTime.civil(2000) <=> ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1, 0, 0, 0), ActiveSupport::TimeZone['UTC'] ) + assert_equal(-1, DateTime.civil(2000) <=> ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1, 0, 0, 1), ActiveSupport::TimeZone['UTC'] )) end - + def test_to_f assert_equal 946684800.0, DateTime.civil(2000).to_f assert_equal 946684800.0, DateTime.civil(1999,12,31,19,0,0,Rational(-5,24)).to_f diff --git a/activesupport/test/core_ext/duration_test.rb b/activesupport/test/core_ext/duration_test.rb index 7b17fe71db903..f802ed87601ab 100644 --- a/activesupport/test/core_ext/duration_test.rb +++ b/activesupport/test/core_ext/duration_test.rb @@ -29,7 +29,7 @@ def test_argument_error flunk("ArgumentError should be raised, but we got #{$!.class} instead") end end - + uses_mocha 'TestDurationSinceAndAgoWithCurrentTime' do def test_since_and_ago_anchored_to_time_now_when_time_zone_default_not_set Time.zone_default = nil @@ -43,10 +43,10 @@ def test_since_and_ago_anchored_to_time_now_when_time_zone_default_not_set assert_equal Time.local(1999,12,31,23,59,55), 5.seconds.ago end end - + def test_since_and_ago_anchored_to_time_zone_now_when_time_zone_default_set silence_warnings do # silence warnings raised by tzinfo gem - Time.zone_default = TimeZone['Eastern Time (US & Canada)'] + Time.zone_default = ActiveSupport::TimeZone['Eastern Time (US & Canada)'] with_env_tz 'US/Eastern' do Time.stubs(:now).returns Time.local(2000) # since @@ -63,7 +63,7 @@ def test_since_and_ago_anchored_to_time_zone_now_when_time_zone_default_set Time.zone_default = nil end end - + protected def with_env_tz(new_tz = 'US/Eastern') old_tz, ENV['TZ'] = ENV['TZ'], new_tz diff --git a/activesupport/test/core_ext/time_ext_test.rb b/activesupport/test/core_ext/time_ext_test.rb index e53b7193ea041..17a0968c0e519 100644 --- a/activesupport/test/core_ext/time_ext_test.rb +++ b/activesupport/test/core_ext/time_ext_test.rb @@ -449,7 +449,7 @@ def test_to_s assert_equal "17:44", time.to_s(:time) assert_equal "February 21, 2005 17:44", time.to_s(:long) assert_equal "February 21st, 2005 17:44", time.to_s(:long_ordinal) - with_env_tz "UTC" do + with_env_tz "UTC" do assert_equal "Mon, 21 Feb 2005 17:44:30 +0000", time.to_s(:rfc822) end end @@ -505,13 +505,13 @@ def test_days_in_month_with_year assert_equal 30, Time.days_in_month(11, 2005) assert_equal 31, Time.days_in_month(12, 2005) end - + uses_mocha 'TestTimeDaysInMonthWithoutYearArg' do def test_days_in_month_feb_in_common_year_without_year_arg Time.stubs(:now).returns(Time.utc(2007)) assert_equal 28, Time.days_in_month(2) end - + def test_days_in_month_feb_in_leap_year_without_year_arg Time.stubs(:now).returns(Time.utc(2008)) assert_equal 29, Time.days_in_month(2) @@ -559,13 +559,13 @@ def test_xmlschema_is_available def test_acts_like_time assert Time.new.acts_like_time? end - + def test_formatted_offset_with_utc assert_equal '+00:00', Time.utc(2000).formatted_offset assert_equal '+0000', Time.utc(2000).formatted_offset(false) assert_equal 'UTC', Time.utc(2000).formatted_offset(true, 'UTC') end - + def test_formatted_offset_with_local with_env_tz 'US/Eastern' do assert_equal '-05:00', Time.local(2000).formatted_offset @@ -574,27 +574,27 @@ def test_formatted_offset_with_local assert_equal '-0400', Time.local(2000, 7).formatted_offset(false) end end - + def test_compare_with_time assert_equal 1, Time.utc(2000) <=> Time.utc(1999, 12, 31, 23, 59, 59, 999) assert_equal 0, Time.utc(2000) <=> Time.utc(2000, 1, 1, 0, 0, 0) assert_equal(-1, Time.utc(2000) <=> Time.utc(2000, 1, 1, 0, 0, 0, 001)) end - + def test_compare_with_datetime assert_equal 1, Time.utc(2000) <=> DateTime.civil(1999, 12, 31, 23, 59, 59) assert_equal 0, Time.utc(2000) <=> DateTime.civil(2000, 1, 1, 0, 0, 0) assert_equal(-1, Time.utc(2000) <=> DateTime.civil(2000, 1, 1, 0, 0, 1)) end - + def test_compare_with_time_with_zone - assert_equal 1, Time.utc(2000) <=> ActiveSupport::TimeWithZone.new( Time.utc(1999, 12, 31, 23, 59, 59), TimeZone['UTC'] ) - assert_equal 0, Time.utc(2000) <=> ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1, 0, 0, 0), TimeZone['UTC'] ) - assert_equal(-1, Time.utc(2000) <=> ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1, 0, 0, 1), TimeZone['UTC'] )) + assert_equal 1, Time.utc(2000) <=> ActiveSupport::TimeWithZone.new( Time.utc(1999, 12, 31, 23, 59, 59), ActiveSupport::TimeZone['UTC'] ) + assert_equal 0, Time.utc(2000) <=> ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1, 0, 0, 0), ActiveSupport::TimeZone['UTC'] ) + assert_equal(-1, Time.utc(2000) <=> ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1, 0, 0, 1), ActiveSupport::TimeZone['UTC'] )) end - + def test_minus_with_time_with_zone - assert_equal 86_400.0, Time.utc(2000, 1, 2) - ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1), TimeZone['UTC'] ) + assert_equal 86_400.0, Time.utc(2000, 1, 2) - ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1), ActiveSupport::TimeZone['UTC'] ) end def test_time_created_with_local_constructor_cannot_represent_times_during_hour_skipped_by_dst @@ -608,7 +608,7 @@ def test_time_created_with_local_constructor_cannot_represent_times_during_hour_ def test_case_equality assert Time === Time.utc(2000) - assert Time === ActiveSupport::TimeWithZone.new(Time.utc(2000), TimeZone['UTC']) + assert Time === ActiveSupport::TimeWithZone.new(Time.utc(2000), ActiveSupport::TimeZone['UTC']) assert_equal false, Time === DateTime.civil(2000) end diff --git a/activesupport/test/core_ext/time_with_zone_test.rb b/activesupport/test/core_ext/time_with_zone_test.rb index c373bca88d2f1..012ec373c9966 100644 --- a/activesupport/test/core_ext/time_with_zone_test.rb +++ b/activesupport/test/core_ext/time_with_zone_test.rb @@ -1,10 +1,10 @@ require 'abstract_unit' - + class TimeWithZoneTest < Test::Unit::TestCase def setup @utc = Time.utc(2000, 1, 1, 0) - @time_zone = TimeZone['Eastern Time (US & Canada)'] + @time_zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)'] @twz = ActiveSupport::TimeWithZone.new(@utc, @time_zone) end @@ -21,114 +21,114 @@ def test_time def test_time_zone assert_equal @time_zone, @twz.time_zone end - + def test_in_time_zone Time.use_zone 'Alaska' do - assert_equal ActiveSupport::TimeWithZone.new(@utc, TimeZone['Alaska']), @twz.in_time_zone + assert_equal ActiveSupport::TimeWithZone.new(@utc, ActiveSupport::TimeZone['Alaska']), @twz.in_time_zone end end def test_in_time_zone_with_argument - assert_equal ActiveSupport::TimeWithZone.new(@utc, TimeZone['Alaska']), @twz.in_time_zone('Alaska') + assert_equal ActiveSupport::TimeWithZone.new(@utc, ActiveSupport::TimeZone['Alaska']), @twz.in_time_zone('Alaska') end - + def test_in_time_zone_with_new_zone_equal_to_old_zone_does_not_create_new_object - assert_equal @twz.object_id, @twz.in_time_zone(TimeZone['Eastern Time (US & Canada)']).object_id + assert_equal @twz.object_id, @twz.in_time_zone(ActiveSupport::TimeZone['Eastern Time (US & Canada)']).object_id end def test_utc? assert_equal false, @twz.utc? - assert_equal true, ActiveSupport::TimeWithZone.new(Time.utc(2000), TimeZone['UTC']).utc? + assert_equal true, ActiveSupport::TimeWithZone.new(Time.utc(2000), ActiveSupport::TimeZone['UTC']).utc? end - + def test_formatted_offset silence_warnings do # silence warnings raised by tzinfo gem assert_equal '-05:00', @twz.formatted_offset assert_equal '-04:00', ActiveSupport::TimeWithZone.new(Time.utc(2000, 6), @time_zone).formatted_offset #dst end end - + def test_dst? silence_warnings do # silence warnings raised by tzinfo gem assert_equal false, @twz.dst? assert_equal true, ActiveSupport::TimeWithZone.new(Time.utc(2000, 6), @time_zone).dst? end end - + def test_zone silence_warnings do # silence warnings raised by tzinfo gem assert_equal 'EST', @twz.zone assert_equal 'EDT', ActiveSupport::TimeWithZone.new(Time.utc(2000, 6), @time_zone).zone #dst end end - + def test_to_json silence_warnings do # silence warnings raised by tzinfo gem assert_equal "\"1999/12/31 19:00:00 -0500\"", @twz.to_json end end - + def test_to_json_with_use_standard_json_time_format_config_set_to_true old, ActiveSupport.use_standard_json_time_format = ActiveSupport.use_standard_json_time_format, true assert_equal "\"1999-12-31T19:00:00-05:00\"", @twz.to_json ensure ActiveSupport.use_standard_json_time_format = old end - + def test_strftime silence_warnings do # silence warnings raised by tzinfo gem assert_equal '1999-12-31 19:00:00 EST -0500', @twz.strftime('%Y-%m-%d %H:%M:%S %Z %z') end end - + def test_inspect silence_warnings do # silence warnings raised by tzinfo gem assert_equal 'Fri, 31 Dec 1999 19:00:00 EST -05:00', @twz.inspect end end - + def test_to_s silence_warnings do # silence warnings raised by tzinfo gem assert_equal '1999-12-31 19:00:00 -0500', @twz.to_s end end - + def test_to_s_db silence_warnings do # silence warnings raised by tzinfo gem assert_equal '2000-01-01 00:00:00', @twz.to_s(:db) end end - + def test_xmlschema silence_warnings do # silence warnings raised by tzinfo gem assert_equal "1999-12-31T19:00:00-05:00", @twz.xmlschema end end - + def test_to_yaml silence_warnings do # silence warnings raised by tzinfo gem assert_equal "--- 1999-12-31 19:00:00 -05:00\n", @twz.to_yaml end end - + def test_ruby_to_yaml silence_warnings do assert_equal "--- \n:twz: 2000-01-01 00:00:00 Z\n", {:twz => @twz}.to_yaml end end - + def test_httpdate silence_warnings do # silence warnings raised by tzinfo gem assert_equal 'Sat, 01 Jan 2000 00:00:00 GMT', @twz.httpdate end end - + def test_rfc2822 silence_warnings do # silence warnings raised by tzinfo gem assert_equal "Fri, 31 Dec 1999 19:00:00 -0500", @twz.rfc2822 end end - + def test_compare_with_time assert_equal 1, @twz <=> Time.utc(1999, 12, 31, 23, 59, 59) assert_equal 0, @twz <=> Time.utc(2000, 1, 1, 0, 0, 0) @@ -142,27 +142,27 @@ def test_compare_with_datetime end def test_compare_with_time_with_zone - assert_equal 1, @twz <=> ActiveSupport::TimeWithZone.new( Time.utc(1999, 12, 31, 23, 59, 59), TimeZone['UTC'] ) - assert_equal 0, @twz <=> ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1, 0, 0, 0), TimeZone['UTC'] ) - assert_equal(-1, @twz <=> ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1, 0, 0, 1), TimeZone['UTC'] )) + assert_equal 1, @twz <=> ActiveSupport::TimeWithZone.new( Time.utc(1999, 12, 31, 23, 59, 59), ActiveSupport::TimeZone['UTC'] ) + assert_equal 0, @twz <=> ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1, 0, 0, 0), ActiveSupport::TimeZone['UTC'] ) + assert_equal(-1, @twz <=> ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1, 0, 0, 1), ActiveSupport::TimeZone['UTC'] )) end - + def test_between? assert @twz.between?(Time.utc(1999,12,31,23,59,59), Time.utc(2000,1,1,0,0,1)) assert_equal false, @twz.between?(Time.utc(2000,1,1,0,0,1), Time.utc(2000,1,1,0,0,2)) end - + def test_eql? assert @twz.eql?(Time.utc(2000)) - assert @twz.eql?( ActiveSupport::TimeWithZone.new(Time.utc(2000), TimeZone["Hawaii"]) ) + assert @twz.eql?( ActiveSupport::TimeWithZone.new(Time.utc(2000), ActiveSupport::TimeZone["Hawaii"]) ) end - + def test_plus_with_integer silence_warnings do # silence warnings raised by tzinfo gem assert_equal Time.utc(1999, 12, 31, 19, 0 ,5), (@twz + 5).time end end - + def test_plus_with_integer_when_self_wraps_datetime silence_warnings do # silence warnings raised by tzinfo gem datetime = DateTime.civil(2000, 1, 1, 0) @@ -170,26 +170,26 @@ def test_plus_with_integer_when_self_wraps_datetime assert_equal DateTime.civil(1999, 12, 31, 19, 0 ,5), (twz + 5).time end end - + def test_plus_when_crossing_time_class_limit silence_warnings do # silence warnings raised by tzinfo gem twz = ActiveSupport::TimeWithZone.new(Time.utc(2038, 1, 19), @time_zone) assert_equal [0, 0, 19, 19, 1, 2038], (twz + 86_400).to_a[0,6] end end - + def test_plus_with_duration silence_warnings do # silence warnings raised by tzinfo gem assert_equal Time.utc(2000, 1, 5, 19, 0 ,0), (@twz + 5.days).time end end - + def test_minus_with_integer silence_warnings do # silence warnings raised by tzinfo gem assert_equal Time.utc(1999, 12, 31, 18, 59 ,55), (@twz - 5).time end end - + def test_minus_with_integer_when_self_wraps_datetime silence_warnings do # silence warnings raised by tzinfo gem datetime = DateTime.civil(2000, 1, 1, 0) @@ -197,24 +197,24 @@ def test_minus_with_integer_when_self_wraps_datetime assert_equal DateTime.civil(1999, 12, 31, 18, 59 ,55), (twz - 5).time end end - + def test_minus_with_duration silence_warnings do # silence warnings raised by tzinfo gem assert_equal Time.utc(1999, 12, 26, 19, 0 ,0), (@twz - 5.days).time end end - + def test_minus_with_time - assert_equal 86_400.0, ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 2), TimeZone['UTC'] ) - Time.utc(2000, 1, 1) - assert_equal 86_400.0, ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 2), TimeZone['Hawaii'] ) - Time.utc(2000, 1, 1) + assert_equal 86_400.0, ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 2), ActiveSupport::TimeZone['UTC'] ) - Time.utc(2000, 1, 1) + assert_equal 86_400.0, ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 2), ActiveSupport::TimeZone['Hawaii'] ) - Time.utc(2000, 1, 1) end - + def test_minus_with_time_with_zone - twz1 = ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1), TimeZone['UTC'] ) - twz2 = ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 2), TimeZone['UTC'] ) + twz1 = ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1), ActiveSupport::TimeZone['UTC'] ) + twz2 = ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 2), ActiveSupport::TimeZone['UTC'] ) assert_equal 86_400.0, twz2 - twz1 end - + def test_plus_and_minus_enforce_spring_dst_rules silence_warnings do # silence warnings raised by tzinfo gem utc = Time.utc(2006,4,2,6,59,59) # == Apr 2 2006 01:59:59 EST; i.e., 1 second before daylight savings start @@ -232,7 +232,7 @@ def test_plus_and_minus_enforce_spring_dst_rules assert_equal 'EST', twz.zone end end - + def test_plus_and_minus_enforce_fall_dst_rules silence_warnings do # silence warnings raised by tzinfo gem utc = Time.utc(2006,10,29,5,59,59) # == Oct 29 2006 01:59:59 EST; i.e., 1 second before daylight savings end @@ -250,25 +250,25 @@ def test_plus_and_minus_enforce_fall_dst_rules assert_equal 'EDT', twz.zone end end - + def test_to_a silence_warnings do # silence warnings raised by tzinfo gem - assert_equal [45, 30, 5, 1, 2, 2000, 2, 32, false, "HST"], ActiveSupport::TimeWithZone.new( Time.utc(2000, 2, 1, 15, 30, 45), TimeZone['Hawaii'] ).to_a + assert_equal [45, 30, 5, 1, 2, 2000, 2, 32, false, "HST"], ActiveSupport::TimeWithZone.new( Time.utc(2000, 2, 1, 15, 30, 45), ActiveSupport::TimeZone['Hawaii'] ).to_a end end - + def test_to_f - result = ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1), TimeZone['Hawaii'] ).to_f + result = ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1), ActiveSupport::TimeZone['Hawaii'] ).to_f assert_equal 946684800.0, result assert result.is_a?(Float) end - + def test_to_i - result = ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1), TimeZone['Hawaii'] ).to_i + result = ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1), ActiveSupport::TimeZone['Hawaii'] ).to_i assert_equal 946684800, result assert result.is_a?(Integer) end - + def test_to_time assert_equal @twz, @twz.to_time end @@ -276,55 +276,55 @@ def test_to_time def test_to_date silence_warnings do # silence warnings raised by tzinfo gem # 1 sec before midnight Jan 1 EST - assert_equal Date.new(1999, 12, 31), ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1, 4, 59, 59), TimeZone['Eastern Time (US & Canada)'] ).to_date + assert_equal Date.new(1999, 12, 31), ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1, 4, 59, 59), ActiveSupport::TimeZone['Eastern Time (US & Canada)'] ).to_date # midnight Jan 1 EST - assert_equal Date.new(2000, 1, 1), ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1, 5, 0, 0), TimeZone['Eastern Time (US & Canada)'] ).to_date + assert_equal Date.new(2000, 1, 1), ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1, 5, 0, 0), ActiveSupport::TimeZone['Eastern Time (US & Canada)'] ).to_date # 1 sec before midnight Jan 2 EST - assert_equal Date.new(2000, 1, 1), ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 2, 4, 59, 59), TimeZone['Eastern Time (US & Canada)'] ).to_date + assert_equal Date.new(2000, 1, 1), ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 2, 4, 59, 59), ActiveSupport::TimeZone['Eastern Time (US & Canada)'] ).to_date # midnight Jan 2 EST - assert_equal Date.new(2000, 1, 2), ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 2, 5, 0, 0), TimeZone['Eastern Time (US & Canada)'] ).to_date + assert_equal Date.new(2000, 1, 2), ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 2, 5, 0, 0), ActiveSupport::TimeZone['Eastern Time (US & Canada)'] ).to_date end end - + def test_to_datetime silence_warnings do # silence warnings raised by tzinfo gem assert_equal DateTime.civil(1999, 12, 31, 19, 0, 0, Rational(-18_000, 86_400)), @twz.to_datetime end end - + def test_acts_like_time assert @twz.acts_like?(:time) assert ActiveSupport::TimeWithZone.new(DateTime.civil(2000), @time_zone).acts_like?(:time) end - + def test_acts_like_date assert_equal false, @twz.acts_like?(:date) assert_equal false, ActiveSupport::TimeWithZone.new(DateTime.civil(2000), @time_zone).acts_like?(:date) end - + def test_is_a assert @twz.is_a?(Time) assert @twz.kind_of?(Time) assert @twz.is_a?(ActiveSupport::TimeWithZone) end - + def test_method_missing_with_time_return_value silence_warnings do # silence warnings raised by tzinfo gem assert_instance_of ActiveSupport::TimeWithZone, @twz.months_since(1) assert_equal Time.utc(2000, 1, 31, 19, 0 ,0), @twz.months_since(1).time end end - + def test_marshal_dump_and_load silence_warnings do # silence warnings raised by tzinfo gem marshal_str = Marshal.dump(@twz) mtime = Marshal.load(marshal_str) assert_equal Time.utc(2000, 1, 1, 0), mtime.utc - assert_equal TimeZone['Eastern Time (US & Canada)'], mtime.time_zone + assert_equal ActiveSupport::TimeZone['Eastern Time (US & Canada)'], mtime.time_zone assert_equal Time.utc(1999, 12, 31, 19), mtime.time end end - + def test_marshal_dump_and_load_with_tzinfo_identifier silence_warnings do # silence warnings raised by tzinfo gem twz = ActiveSupport::TimeWithZone.new(@utc, TZInfo::Timezone.get('America/New_York')) @@ -335,14 +335,14 @@ def test_marshal_dump_and_load_with_tzinfo_identifier assert_equal Time.utc(1999, 12, 31, 19), mtime.time end end - + def test_method_missing_with_non_time_return_value silence_warnings do # silence warnings raised by tzinfo gem @twz.time.expects(:foo).returns('bar') assert_equal 'bar', @twz.foo end end - + def test_date_part_value_methods silence_warnings do # silence warnings raised by tzinfo gem twz = ActiveSupport::TimeWithZone.new(Time.utc(1999,12,31,19,18,17,500), @time_zone) @@ -356,14 +356,14 @@ def test_date_part_value_methods assert_equal 500, twz.usec end end - + def test_usec_returns_0_when_datetime_is_wrapped silence_warnings do # silence warnings raised by tzinfo gem twz = ActiveSupport::TimeWithZone.new(DateTime.civil(2000), @time_zone) assert_equal 0, twz.usec end end - + def test_utc_to_local_conversion_saves_period_in_instance_variable silence_warnings do # silence warnings raised by tzinfo gem assert_nil @twz.instance_variable_get('@period') @@ -371,14 +371,14 @@ def test_utc_to_local_conversion_saves_period_in_instance_variable assert_kind_of TZInfo::TimezonePeriod, @twz.instance_variable_get('@period') end end - + def test_instance_created_with_local_time_returns_correct_utc_time silence_warnings do # silence warnings raised by tzinfo gem twz = ActiveSupport::TimeWithZone.new(nil, @time_zone, Time.utc(1999, 12, 31, 19)) assert_equal Time.utc(2000), twz.utc end end - + def test_instance_created_with_local_time_enforces_spring_dst_rules silence_warnings do # silence warnings raised by tzinfo gem twz = ActiveSupport::TimeWithZone.new(nil, @time_zone, Time.utc(2006,4,2,2)) # first second of DST @@ -387,7 +387,7 @@ def test_instance_created_with_local_time_enforces_spring_dst_rules assert_equal 'EDT', twz.zone end end - + def test_instance_created_with_local_time_enforces_fall_dst_rules silence_warnings do # silence warnings raised by tzinfo gem twz = ActiveSupport::TimeWithZone.new(nil, @time_zone, Time.utc(2006,10,29,1)) # 1AM can be either DST or non-DST; we'll pick DST @@ -396,14 +396,14 @@ def test_instance_created_with_local_time_enforces_fall_dst_rules assert_equal 'EDT', twz.zone end end - + def test_ruby_19_weekday_name_query_methods ruby_19_or_greater = RUBY_VERSION >= '1.9' %w(sunday? monday? tuesday? wednesday? thursday? friday? saturday?).each do |name| assert_equal ruby_19_or_greater, @twz.respond_to?(name) end end - + def test_utc_to_local_conversion_with_far_future_datetime silence_warnings do # silence warnings raised by tzinfo gem assert_equal [0,0,19,31,12,2049], ActiveSupport::TimeWithZone.new(DateTime.civil(2050), @time_zone).to_a[0,6] @@ -415,7 +415,7 @@ def test_local_to_utc_conversion_with_far_future_datetime assert_equal DateTime.civil(2050).to_f, ActiveSupport::TimeWithZone.new(nil, @time_zone, DateTime.civil(2049,12,31,19)).to_f end end - + def test_change assert_equal "Fri, 31 Dec 1999 19:00:00 EST -05:00", @twz.inspect assert_equal "Mon, 31 Dec 2001 19:00:00 EST -05:00", @twz.change(:year => 2001).inspect @@ -426,7 +426,7 @@ def test_change assert_equal "Fri, 31 Dec 1999 19:15:00 EST -05:00", @twz.change(:min => 15).inspect assert_equal "Fri, 31 Dec 1999 19:00:30 EST -05:00", @twz.change(:sec => 30).inspect end - + def test_advance assert_equal "Fri, 31 Dec 1999 19:00:00 EST -05:00", @twz.inspect assert_equal "Mon, 31 Dec 2001 19:00:00 EST -05:00", @twz.advance(:years => 2).inspect @@ -436,77 +436,77 @@ def test_advance assert_equal "Fri, 31 Dec 1999 19:15:00 EST -05:00", @twz.advance(:minutes => 15).inspect assert_equal "Fri, 31 Dec 1999 19:00:30 EST -05:00", @twz.advance(:seconds => 30).inspect end - + def beginning_of_year assert_equal "Fri, 31 Dec 1999 19:00:00 EST -05:00", @twz.inspect assert_equal "Fri, 01 Jan 1999 00:00:00 EST -05:00", @twz.beginning_of_year.inspect end - + def end_of_year assert_equal "Fri, 31 Dec 1999 19:00:00 EST -05:00", @twz.inspect assert_equal "Fri, 31 Dec 1999 23:59:59 EST -05:00", @twz.end_of_year.inspect end - + def beginning_of_month assert_equal "Fri, 31 Dec 1999 19:00:00 EST -05:00", @twz.inspect assert_equal "Fri, 01 Dec 1999 00:00:00 EST -05:00", @twz.beginning_of_month.inspect end - + def end_of_month assert_equal "Fri, 31 Dec 1999 19:00:00 EST -05:00", @twz.inspect assert_equal "Fri, 31 Dec 1999 23:59:59 EST -05:00", @twz.end_of_month.inspect end - + def beginning_of_day assert_equal "Fri, 31 Dec 1999 19:00:00 EST -05:00", @twz.inspect assert_equal "Fri, 31 Dec 1999 00:00:00 EST -05:00", @twz.beginning_of_day.inspect end - + def end_of_day assert_equal "Fri, 31 Dec 1999 19:00:00 EST -05:00", @twz.inspect assert_equal "Fri, 31 Dec 1999 23:59:59 EST -05:00", @twz.end_of_day.inspect end - + def test_since assert_equal "Fri, 31 Dec 1999 19:00:01 EST -05:00", @twz.since(1).inspect end - + def test_ago assert_equal "Fri, 31 Dec 1999 18:59:59 EST -05:00", @twz.ago(1).inspect end - + def test_seconds_since_midnight assert_equal 19 * 60 * 60, @twz.seconds_since_midnight end - + def test_advance_1_year_from_leap_day twz = ActiveSupport::TimeWithZone.new(nil, @time_zone, Time.utc(2004,2,29)) assert_equal "Mon, 28 Feb 2005 00:00:00 EST -05:00", twz.advance(:years => 1).inspect assert_equal "Mon, 28 Feb 2005 00:00:00 EST -05:00", twz.years_since(1).inspect assert_equal "Mon, 28 Feb 2005 00:00:00 EST -05:00", (twz + 1.year).inspect end - + def test_advance_1_month_from_last_day_of_january twz = ActiveSupport::TimeWithZone.new(nil, @time_zone, Time.utc(2005,1,31)) assert_equal "Mon, 28 Feb 2005 00:00:00 EST -05:00", twz.advance(:months => 1).inspect assert_equal "Mon, 28 Feb 2005 00:00:00 EST -05:00", twz.months_since(1).inspect assert_equal "Mon, 28 Feb 2005 00:00:00 EST -05:00", (twz + 1.month).inspect end - + def test_advance_1_month_from_last_day_of_january_during_leap_year twz = ActiveSupport::TimeWithZone.new(nil, @time_zone, Time.utc(2000,1,31)) assert_equal "Tue, 29 Feb 2000 00:00:00 EST -05:00", twz.advance(:months => 1).inspect assert_equal "Tue, 29 Feb 2000 00:00:00 EST -05:00", twz.months_since(1).inspect assert_equal "Tue, 29 Feb 2000 00:00:00 EST -05:00", (twz + 1.month).inspect end - + def test_advance_1_month_into_spring_dst_gap twz = ActiveSupport::TimeWithZone.new(nil, @time_zone, Time.utc(2006,3,2,2)) assert_equal "Sun, 02 Apr 2006 03:00:00 EDT -04:00", twz.advance(:months => 1).inspect assert_equal "Sun, 02 Apr 2006 03:00:00 EDT -04:00", twz.months_since(1).inspect assert_equal "Sun, 02 Apr 2006 03:00:00 EDT -04:00", (twz + 1.month).inspect end - + def test_advance_1_second_into_spring_dst_gap twz = ActiveSupport::TimeWithZone.new(nil, @time_zone, Time.utc(2006,4,2,1,59,59)) assert_equal "Sun, 02 Apr 2006 03:00:00 EDT -04:00", twz.advance(:seconds => 1).inspect @@ -519,11 +519,11 @@ class TimeWithZoneMethodsForTimeAndDateTimeTest < Test::Unit::TestCase def setup @t, @dt = Time.utc(2000), DateTime.civil(2000) end - + def teardown Time.zone = nil end - + def test_in_time_zone silence_warnings do # silence warnings raised by tzinfo gem Time.use_zone 'Alaska' do @@ -554,7 +554,7 @@ def test_in_time_zone_with_argument end end end - + def test_in_time_zone_with_time_local_instance silence_warnings do # silence warnings raised by tzinfo gem with_env_tz 'US/Eastern' do @@ -563,85 +563,85 @@ def test_in_time_zone_with_time_local_instance end end end - + def test_use_zone Time.zone = 'Alaska' Time.use_zone 'Hawaii' do - assert_equal TimeZone['Hawaii'], Time.zone + assert_equal ActiveSupport::TimeZone['Hawaii'], Time.zone end - assert_equal TimeZone['Alaska'], Time.zone + assert_equal ActiveSupport::TimeZone['Alaska'], Time.zone end - + def test_use_zone_with_exception_raised Time.zone = 'Alaska' assert_raises RuntimeError do Time.use_zone('Hawaii') { raise RuntimeError } end - assert_equal TimeZone['Alaska'], Time.zone + assert_equal ActiveSupport::TimeZone['Alaska'], Time.zone end - + def test_time_zone_getter_and_setter - Time.zone = TimeZone['Alaska'] - assert_equal TimeZone['Alaska'], Time.zone + Time.zone = ActiveSupport::TimeZone['Alaska'] + assert_equal ActiveSupport::TimeZone['Alaska'], Time.zone Time.zone = 'Alaska' - assert_equal TimeZone['Alaska'], Time.zone + assert_equal ActiveSupport::TimeZone['Alaska'], Time.zone Time.zone = -9.hours - assert_equal TimeZone['Alaska'], Time.zone + assert_equal ActiveSupport::TimeZone['Alaska'], Time.zone Time.zone = nil assert_equal nil, Time.zone end - + def test_time_zone_getter_and_setter_with_zone_default - Time.zone_default = TimeZone['Alaska'] - assert_equal TimeZone['Alaska'], Time.zone - Time.zone = TimeZone['Hawaii'] - assert_equal TimeZone['Hawaii'], Time.zone + Time.zone_default = ActiveSupport::TimeZone['Alaska'] + assert_equal ActiveSupport::TimeZone['Alaska'], Time.zone + Time.zone = ActiveSupport::TimeZone['Hawaii'] + assert_equal ActiveSupport::TimeZone['Hawaii'], Time.zone Time.zone = nil - assert_equal TimeZone['Alaska'], Time.zone + assert_equal ActiveSupport::TimeZone['Alaska'], Time.zone ensure Time.zone_default = nil end - + def test_time_zone_setter_is_thread_safe Time.use_zone 'Paris' do t1 = Thread.new { Time.zone = 'Alaska' }.join t2 = Thread.new { Time.zone = 'Hawaii' }.join assert t1.stop?, "Thread 1 did not finish running" assert t2.stop?, "Thread 2 did not finish running" - assert_equal TimeZone['Paris'], Time.zone - assert_equal TimeZone['Alaska'], t1[:time_zone] - assert_equal TimeZone['Hawaii'], t2[:time_zone] + assert_equal ActiveSupport::TimeZone['Paris'], Time.zone + assert_equal ActiveSupport::TimeZone['Alaska'], t1[:time_zone] + assert_equal ActiveSupport::TimeZone['Hawaii'], t2[:time_zone] end end - + def test_time_zone_setter_with_tzinfo_timezone_object_wraps_in_rails_time_zone silence_warnings do # silence warnings raised by tzinfo gem tzinfo = TZInfo::Timezone.get('America/New_York') Time.zone = tzinfo - assert_kind_of TimeZone, Time.zone + assert_kind_of ActiveSupport::TimeZone, Time.zone assert_equal tzinfo, Time.zone.tzinfo assert_equal 'America/New_York', Time.zone.name assert_equal(-18_000, Time.zone.utc_offset) end end - + def test_time_zone_setter_with_tzinfo_timezone_identifier_does_lookup_and_wraps_in_rails_time_zone silence_warnings do # silence warnings raised by tzinfo gem Time.zone = 'America/New_York' - assert_kind_of TimeZone, Time.zone + assert_kind_of ActiveSupport::TimeZone, Time.zone assert_equal 'America/New_York', Time.zone.tzinfo.name assert_equal 'America/New_York', Time.zone.name assert_equal(-18_000, Time.zone.utc_offset) end end - + def test_time_zone_setter_with_non_identifying_argument_returns_nil Time.zone = 'foo' assert_equal nil, Time.zone Time.zone = -15.hours assert_equal nil, Time.zone end - + uses_mocha 'TestTimeCurrent' do def test_current_returns_time_now_when_zone_default_not_set with_env_tz 'US/Eastern' do @@ -650,10 +650,10 @@ def test_current_returns_time_now_when_zone_default_not_set assert_equal Time.local(2000), Time.current end end - + def test_current_returns_time_zone_now_when_zone_default_set silence_warnings do # silence warnings raised by tzinfo gem - Time.zone_default = TimeZone['Eastern Time (US & Canada)'] + Time.zone_default = ActiveSupport::TimeZone['Eastern Time (US & Canada)'] with_env_tz 'US/Eastern' do Time.stubs(:now).returns Time.local(2000) assert_equal true, Time.current.is_a?(ActiveSupport::TimeWithZone) @@ -665,7 +665,7 @@ def test_current_returns_time_zone_now_when_zone_default_set Time.zone_default = nil end end - + protected def with_env_tz(new_tz = 'US/Eastern') old_tz, ENV['TZ'] = ENV['TZ'], new_tz diff --git a/activesupport/test/dependencies_test.rb b/activesupport/test/dependencies_test.rb index 0309ab6858498..038547a862f1e 100644 --- a/activesupport/test/dependencies_test.rb +++ b/activesupport/test/dependencies_test.rb @@ -15,31 +15,31 @@ module ModuleWithConstant class DependenciesTest < Test::Unit::TestCase def teardown - Dependencies.clear + ActiveSupport::Dependencies.clear end def with_loading(*from) - old_mechanism, Dependencies.mechanism = Dependencies.mechanism, :load + old_mechanism, ActiveSupport::Dependencies.mechanism = ActiveSupport::Dependencies.mechanism, :load dir = File.dirname(__FILE__) - prior_load_paths = Dependencies.load_paths - Dependencies.load_paths = from.collect { |f| "#{dir}/#{f}" } + prior_load_paths = ActiveSupport::Dependencies.load_paths + ActiveSupport::Dependencies.load_paths = from.collect { |f| "#{dir}/#{f}" } yield ensure - Dependencies.load_paths = prior_load_paths - Dependencies.mechanism = old_mechanism - Dependencies.explicitly_unloadable_constants = [] + ActiveSupport::Dependencies.load_paths = prior_load_paths + ActiveSupport::Dependencies.mechanism = old_mechanism + ActiveSupport::Dependencies.explicitly_unloadable_constants = [] end def test_tracking_loaded_files require_dependency 'dependencies/service_one' require_dependency 'dependencies/service_two' - assert_equal 2, Dependencies.loaded.size + assert_equal 2, ActiveSupport::Dependencies.loaded.size end def test_tracking_identical_loaded_files require_dependency 'dependencies/service_one' require_dependency 'dependencies/service_one' - assert_equal 1, Dependencies.loaded.size + assert_equal 1, ActiveSupport::Dependencies.loaded.size end def test_missing_dependency_raises_missing_source_file @@ -64,46 +64,46 @@ def test_dependency_which_raises_exception_isnt_added_to_loaded_set end assert_equal count + 1, $raises_exception_load_count - assert !Dependencies.loaded.include?(filename) - assert !Dependencies.history.include?(filename) + assert !ActiveSupport::Dependencies.loaded.include?(filename) + assert !ActiveSupport::Dependencies.history.include?(filename) end end end def test_warnings_should_be_enabled_on_first_load with_loading 'dependencies' do - old_warnings, Dependencies.warnings_on_first_load = Dependencies.warnings_on_first_load, true + old_warnings, ActiveSupport::Dependencies.warnings_on_first_load = ActiveSupport::Dependencies.warnings_on_first_load, true filename = "check_warnings" expanded = File.expand_path("test/dependencies/#{filename}") $check_warnings_load_count = 0 - assert !Dependencies.loaded.include?(expanded) - assert !Dependencies.history.include?(expanded) + assert !ActiveSupport::Dependencies.loaded.include?(expanded) + assert !ActiveSupport::Dependencies.history.include?(expanded) silence_warnings { require_dependency filename } assert_equal 1, $check_warnings_load_count assert_equal true, $checked_verbose, 'On first load warnings should be enabled.' - assert Dependencies.loaded.include?(expanded) - Dependencies.clear - assert !Dependencies.loaded.include?(expanded) - assert Dependencies.history.include?(expanded) + assert ActiveSupport::Dependencies.loaded.include?(expanded) + ActiveSupport::Dependencies.clear + assert !ActiveSupport::Dependencies.loaded.include?(expanded) + assert ActiveSupport::Dependencies.history.include?(expanded) silence_warnings { require_dependency filename } assert_equal 2, $check_warnings_load_count assert_equal nil, $checked_verbose, 'After first load warnings should be left alone.' - assert Dependencies.loaded.include?(expanded) - Dependencies.clear - assert !Dependencies.loaded.include?(expanded) - assert Dependencies.history.include?(expanded) + assert ActiveSupport::Dependencies.loaded.include?(expanded) + ActiveSupport::Dependencies.clear + assert !ActiveSupport::Dependencies.loaded.include?(expanded) + assert ActiveSupport::Dependencies.history.include?(expanded) enable_warnings { require_dependency filename } assert_equal 3, $check_warnings_load_count assert_equal true, $checked_verbose, 'After first load warnings should be left alone.' - assert Dependencies.loaded.include?(expanded) + assert ActiveSupport::Dependencies.loaded.include?(expanded) end end @@ -113,7 +113,7 @@ def test_mutual_dependencies_dont_infinite_loop assert_nothing_raised { require_dependency 'mutual_one' } assert_equal 2, $mutual_dependencies_count - Dependencies.clear + ActiveSupport::Dependencies.clear $mutual_dependencies_count = 0 assert_nothing_raised { require_dependency 'mutual_two' } @@ -239,7 +239,7 @@ def test_smart_name_error_strings end def test_loadable_constants_for_path_should_handle_empty_autoloads - assert_equal [], Dependencies.loadable_constants_for_path('hello') + assert_equal [], ActiveSupport::Dependencies.loadable_constants_for_path('hello') end def test_loadable_constants_for_path_should_handle_relative_paths @@ -247,7 +247,7 @@ def test_loadable_constants_for_path_should_handle_relative_paths relative_root = File.dirname(__FILE__) + '/dependencies' ['', '/'].each do |suffix| with_loading fake_root + suffix do - assert_equal ["A::B"], Dependencies.loadable_constants_for_path(relative_root + '/a/b') + assert_equal ["A::B"], ActiveSupport::Dependencies.loadable_constants_for_path(relative_root + '/a/b') end end end @@ -255,106 +255,106 @@ def test_loadable_constants_for_path_should_handle_relative_paths def test_loadable_constants_for_path_should_provide_all_results fake_root = '/usr/apps/backpack' with_loading fake_root, fake_root + '/lib' do - root = Dependencies.load_paths.first - assert_equal ["Lib::A::B", "A::B"], Dependencies.loadable_constants_for_path(root + '/lib/a/b') + root = ActiveSupport::Dependencies.load_paths.first + assert_equal ["Lib::A::B", "A::B"], ActiveSupport::Dependencies.loadable_constants_for_path(root + '/lib/a/b') end end def test_loadable_constants_for_path_should_uniq_results fake_root = '/usr/apps/backpack/lib' with_loading fake_root, fake_root + '/' do - root = Dependencies.load_paths.first - assert_equal ["A::B"], Dependencies.loadable_constants_for_path(root + '/a/b') + root = ActiveSupport::Dependencies.load_paths.first + assert_equal ["A::B"], ActiveSupport::Dependencies.loadable_constants_for_path(root + '/a/b') end end def test_loadable_constants_with_load_path_without_trailing_slash path = File.dirname(__FILE__) + '/autoloading_fixtures/class_folder/inline_class.rb' with_loading 'autoloading_fixtures/class/' do - assert_equal [], Dependencies.loadable_constants_for_path(path) + assert_equal [], ActiveSupport::Dependencies.loadable_constants_for_path(path) end end def test_qualified_const_defined - assert Dependencies.qualified_const_defined?("Object") - assert Dependencies.qualified_const_defined?("::Object") - assert Dependencies.qualified_const_defined?("::Object::Kernel") - assert Dependencies.qualified_const_defined?("::Object::Dependencies") - assert Dependencies.qualified_const_defined?("::Test::Unit::TestCase") + assert ActiveSupport::Dependencies.qualified_const_defined?("Object") + assert ActiveSupport::Dependencies.qualified_const_defined?("::Object") + assert ActiveSupport::Dependencies.qualified_const_defined?("::Object::Kernel") + assert ActiveSupport::Dependencies.qualified_const_defined?("::Object::Dependencies") + assert ActiveSupport::Dependencies.qualified_const_defined?("::Test::Unit::TestCase") end def test_qualified_const_defined_should_not_call_method_missing ModuleWithMissing.missing_count = 0 - assert ! Dependencies.qualified_const_defined?("ModuleWithMissing::A") + assert ! ActiveSupport::Dependencies.qualified_const_defined?("ModuleWithMissing::A") assert_equal 0, ModuleWithMissing.missing_count - assert ! Dependencies.qualified_const_defined?("ModuleWithMissing::A::B") + assert ! ActiveSupport::Dependencies.qualified_const_defined?("ModuleWithMissing::A::B") assert_equal 0, ModuleWithMissing.missing_count end def test_autoloaded? with_loading 'autoloading_fixtures' do - assert ! Dependencies.autoloaded?("ModuleFolder") - assert ! Dependencies.autoloaded?("ModuleFolder::NestedClass") + assert ! ActiveSupport::Dependencies.autoloaded?("ModuleFolder") + assert ! ActiveSupport::Dependencies.autoloaded?("ModuleFolder::NestedClass") - assert Dependencies.autoloaded?(ModuleFolder) + assert ActiveSupport::Dependencies.autoloaded?(ModuleFolder) - assert Dependencies.autoloaded?("ModuleFolder") - assert ! Dependencies.autoloaded?("ModuleFolder::NestedClass") + assert ActiveSupport::Dependencies.autoloaded?("ModuleFolder") + assert ! ActiveSupport::Dependencies.autoloaded?("ModuleFolder::NestedClass") - assert Dependencies.autoloaded?(ModuleFolder::NestedClass) + assert ActiveSupport::Dependencies.autoloaded?(ModuleFolder::NestedClass) - assert Dependencies.autoloaded?("ModuleFolder") - assert Dependencies.autoloaded?("ModuleFolder::NestedClass") + assert ActiveSupport::Dependencies.autoloaded?("ModuleFolder") + assert ActiveSupport::Dependencies.autoloaded?("ModuleFolder::NestedClass") - assert Dependencies.autoloaded?("::ModuleFolder") - assert Dependencies.autoloaded?(:ModuleFolder) + assert ActiveSupport::Dependencies.autoloaded?("::ModuleFolder") + assert ActiveSupport::Dependencies.autoloaded?(:ModuleFolder) # Anonymous modules aren't autoloaded. - assert !Dependencies.autoloaded?(Module.new) + assert !ActiveSupport::Dependencies.autoloaded?(Module.new) nil_name = Module.new def nil_name.name() nil end - assert !Dependencies.autoloaded?(nil_name) + assert !ActiveSupport::Dependencies.autoloaded?(nil_name) Object.class_eval { remove_const :ModuleFolder } end end def test_qualified_name_for - assert_equal "A", Dependencies.qualified_name_for(Object, :A) - assert_equal "A", Dependencies.qualified_name_for(:Object, :A) - assert_equal "A", Dependencies.qualified_name_for("Object", :A) - assert_equal "A", Dependencies.qualified_name_for("::Object", :A) - assert_equal "A", Dependencies.qualified_name_for("::Kernel", :A) + assert_equal "A", ActiveSupport::Dependencies.qualified_name_for(Object, :A) + assert_equal "A", ActiveSupport::Dependencies.qualified_name_for(:Object, :A) + assert_equal "A", ActiveSupport::Dependencies.qualified_name_for("Object", :A) + assert_equal "A", ActiveSupport::Dependencies.qualified_name_for("::Object", :A) + assert_equal "A", ActiveSupport::Dependencies.qualified_name_for("::Kernel", :A) - assert_equal "Dependencies::A", Dependencies.qualified_name_for(:Dependencies, :A) - assert_equal "Dependencies::A", Dependencies.qualified_name_for(Dependencies, :A) + assert_equal "ActiveSupport::Dependencies::A", ActiveSupport::Dependencies.qualified_name_for(:'ActiveSupport::Dependencies', :A) + assert_equal "ActiveSupport::Dependencies::A", ActiveSupport::Dependencies.qualified_name_for(ActiveSupport::Dependencies, :A) end def test_file_search with_loading 'dependencies' do - root = Dependencies.load_paths.first - assert_equal nil, Dependencies.search_for_file('service_three') - assert_equal nil, Dependencies.search_for_file('service_three.rb') - assert_equal root + '/service_one.rb', Dependencies.search_for_file('service_one') - assert_equal root + '/service_one.rb', Dependencies.search_for_file('service_one.rb') + root = ActiveSupport::Dependencies.load_paths.first + assert_equal nil, ActiveSupport::Dependencies.search_for_file('service_three') + assert_equal nil, ActiveSupport::Dependencies.search_for_file('service_three.rb') + assert_equal root + '/service_one.rb', ActiveSupport::Dependencies.search_for_file('service_one') + assert_equal root + '/service_one.rb', ActiveSupport::Dependencies.search_for_file('service_one.rb') end end def test_file_search_uses_first_in_load_path with_loading 'dependencies', 'autoloading_fixtures' do - deps, autoload = Dependencies.load_paths + deps, autoload = ActiveSupport::Dependencies.load_paths assert_match %r/dependencies/, deps assert_match %r/autoloading_fixtures/, autoload - assert_equal deps + '/conflict.rb', Dependencies.search_for_file('conflict') + assert_equal deps + '/conflict.rb', ActiveSupport::Dependencies.search_for_file('conflict') end with_loading 'autoloading_fixtures', 'dependencies' do - autoload, deps = Dependencies.load_paths + autoload, deps = ActiveSupport::Dependencies.load_paths assert_match %r/dependencies/, deps assert_match %r/autoloading_fixtures/, autoload - assert_equal autoload + '/conflict.rb', Dependencies.search_for_file('conflict') + assert_equal autoload + '/conflict.rb', ActiveSupport::Dependencies.search_for_file('conflict') end end @@ -383,7 +383,7 @@ def test_const_missing_should_not_double_load with_loading 'autoloading_fixtures' do require_dependency '././counting_loader' assert_equal 1, $counting_loaded_times - assert_raises(ArgumentError) { Dependencies.load_missing_constant Object, :CountingLoader } + assert_raises(ArgumentError) { ActiveSupport::Dependencies.load_missing_constant Object, :CountingLoader } assert_equal 1, $counting_loaded_times end end @@ -407,12 +407,12 @@ def test_const_missing_within_anonymous_module def test_removal_from_tree_should_be_detected with_loading 'dependencies' do - root = Dependencies.load_paths.first + root = ActiveSupport::Dependencies.load_paths.first c = ServiceOne - Dependencies.clear + ActiveSupport::Dependencies.clear assert ! defined?(ServiceOne) begin - Dependencies.load_missing_constant(c, :FakeMissing) + ActiveSupport::Dependencies.load_missing_constant(c, :FakeMissing) flunk "Expected exception" rescue ArgumentError => e assert_match %r{ServiceOne has been removed from the module tree}i, e.message @@ -430,25 +430,25 @@ def test_nested_load_error_isnt_rescued def test_load_once_paths_do_not_add_to_autoloaded_constants with_loading 'autoloading_fixtures' do - Dependencies.load_once_paths = Dependencies.load_paths.dup + ActiveSupport::Dependencies.load_once_paths = ActiveSupport::Dependencies.load_paths.dup - assert ! Dependencies.autoloaded?("ModuleFolder") - assert ! Dependencies.autoloaded?("ModuleFolder::NestedClass") - assert ! Dependencies.autoloaded?(ModuleFolder) + assert ! ActiveSupport::Dependencies.autoloaded?("ModuleFolder") + assert ! ActiveSupport::Dependencies.autoloaded?("ModuleFolder::NestedClass") + assert ! ActiveSupport::Dependencies.autoloaded?(ModuleFolder) 1 if ModuleFolder::NestedClass # 1 if to avoid warning - assert ! Dependencies.autoloaded?(ModuleFolder::NestedClass) + assert ! ActiveSupport::Dependencies.autoloaded?(ModuleFolder::NestedClass) end ensure Object.class_eval { remove_const :ModuleFolder } - Dependencies.load_once_paths = [] + ActiveSupport::Dependencies.load_once_paths = [] end def test_application_should_special_case_application_controller with_loading 'autoloading_fixtures' do require_dependency 'application' assert_equal 10, ApplicationController - assert Dependencies.autoloaded?(:ApplicationController) + assert ActiveSupport::Dependencies.autoloaded?(:ApplicationController) end end @@ -463,15 +463,15 @@ def test_const_missing_on_kernel_should_fallback_to_object def test_preexisting_constants_are_not_marked_as_autoloaded with_loading 'autoloading_fixtures' do require_dependency 'e' - assert Dependencies.autoloaded?(:E) - Dependencies.clear + assert ActiveSupport::Dependencies.autoloaded?(:E) + ActiveSupport::Dependencies.clear end Object.const_set :E, Class.new with_loading 'autoloading_fixtures' do require_dependency 'e' - assert ! Dependencies.autoloaded?(:E), "E shouldn't be marked autoloaded!" - Dependencies.clear + assert ! ActiveSupport::Dependencies.autoloaded?(:E), "E shouldn't be marked autoloaded!" + ActiveSupport::Dependencies.clear end ensure @@ -483,11 +483,11 @@ def test_unloadable Object.const_set :M, Module.new M.unloadable - Dependencies.clear + ActiveSupport::Dependencies.clear assert ! defined?(M) Object.const_set :M, Module.new - Dependencies.clear + ActiveSupport::Dependencies.clear assert ! defined?(M), "Dependencies should unload unloadable constants each time" end end @@ -508,24 +508,24 @@ def test_unloadable_should_return_change_flag end def test_new_contants_in_without_constants - assert_equal [], (Dependencies.new_constants_in(Object) { }) - assert Dependencies.constant_watch_stack.empty? + assert_equal [], (ActiveSupport::Dependencies.new_constants_in(Object) { }) + assert ActiveSupport::Dependencies.constant_watch_stack.empty? end def test_new_constants_in_with_a_single_constant - assert_equal ["Hello"], Dependencies.new_constants_in(Object) { + assert_equal ["Hello"], ActiveSupport::Dependencies.new_constants_in(Object) { Object.const_set :Hello, 10 }.map(&:to_s) - assert Dependencies.constant_watch_stack.empty? + assert ActiveSupport::Dependencies.constant_watch_stack.empty? ensure Object.class_eval { remove_const :Hello } end def test_new_constants_in_with_nesting - outer = Dependencies.new_constants_in(Object) do + outer = ActiveSupport::Dependencies.new_constants_in(Object) do Object.const_set :OuterBefore, 10 - assert_equal ["Inner"], Dependencies.new_constants_in(Object) { + assert_equal ["Inner"], ActiveSupport::Dependencies.new_constants_in(Object) { Object.const_set :Inner, 20 }.map(&:to_s) @@ -533,7 +533,7 @@ def test_new_constants_in_with_nesting end assert_equal ["OuterAfter", "OuterBefore"], outer.sort.map(&:to_s) - assert Dependencies.constant_watch_stack.empty? + assert ActiveSupport::Dependencies.constant_watch_stack.empty? ensure %w(OuterBefore Inner OuterAfter).each do |name| Object.class_eval { remove_const name if const_defined?(name) } @@ -543,10 +543,10 @@ def test_new_constants_in_with_nesting def test_new_constants_in_module Object.const_set :M, Module.new - outer = Dependencies.new_constants_in(M) do + outer = ActiveSupport::Dependencies.new_constants_in(M) do M.const_set :OuterBefore, 10 - inner = Dependencies.new_constants_in(M) do + inner = ActiveSupport::Dependencies.new_constants_in(M) do M.const_set :Inner, 20 end assert_equal ["M::Inner"], inner @@ -554,17 +554,17 @@ def test_new_constants_in_module M.const_set :OuterAfter, 30 end assert_equal ["M::OuterAfter", "M::OuterBefore"], outer.sort - assert Dependencies.constant_watch_stack.empty? + assert ActiveSupport::Dependencies.constant_watch_stack.empty? ensure Object.class_eval { remove_const :M } end def test_new_constants_in_module_using_name - outer = Dependencies.new_constants_in(:M) do + outer = ActiveSupport::Dependencies.new_constants_in(:M) do Object.const_set :M, Module.new M.const_set :OuterBefore, 10 - inner = Dependencies.new_constants_in(:M) do + inner = ActiveSupport::Dependencies.new_constants_in(:M) do M.const_set :Inner, 20 end assert_equal ["M::Inner"], inner @@ -572,13 +572,13 @@ def test_new_constants_in_module_using_name M.const_set :OuterAfter, 30 end assert_equal ["M::OuterAfter", "M::OuterBefore"], outer.sort - assert Dependencies.constant_watch_stack.empty? + assert ActiveSupport::Dependencies.constant_watch_stack.empty? ensure Object.class_eval { remove_const :M } end def test_new_constants_in_with_inherited_constants - m = Dependencies.new_constants_in(:Object) do + m = ActiveSupport::Dependencies.new_constants_in(:Object) do Object.class_eval { include ModuleWithConstant } end assert_equal [], m @@ -586,7 +586,7 @@ def test_new_constants_in_with_inherited_constants def test_new_constants_in_with_illegal_module_name_raises_correct_error assert_raises(NameError) do - Dependencies.new_constants_in("Illegal-Name") {} + ActiveSupport::Dependencies.new_constants_in("Illegal-Name") {} end end @@ -598,10 +598,10 @@ def test_file_with_multiple_constants_and_require_dependency require_dependency 'multiple_constant_file' assert defined?(MultipleConstantFile) assert defined?(SiblingConstant) - assert Dependencies.autoloaded?(:MultipleConstantFile) - assert Dependencies.autoloaded?(:SiblingConstant) + assert ActiveSupport::Dependencies.autoloaded?(:MultipleConstantFile) + assert ActiveSupport::Dependencies.autoloaded?(:SiblingConstant) - Dependencies.clear + ActiveSupport::Dependencies.clear assert ! defined?(MultipleConstantFile) assert ! defined?(SiblingConstant) @@ -617,10 +617,10 @@ def test_file_with_multiple_constants_and_auto_loading assert defined?(MultipleConstantFile) assert defined?(SiblingConstant) - assert Dependencies.autoloaded?(:MultipleConstantFile) - assert Dependencies.autoloaded?(:SiblingConstant) + assert ActiveSupport::Dependencies.autoloaded?(:MultipleConstantFile) + assert ActiveSupport::Dependencies.autoloaded?(:SiblingConstant) - Dependencies.clear + ActiveSupport::Dependencies.clear assert ! defined?(MultipleConstantFile) assert ! defined?(SiblingConstant) @@ -636,10 +636,10 @@ def test_nested_file_with_multiple_constants_and_require_dependency assert defined?(ClassFolder::NestedClass) assert defined?(ClassFolder::SiblingClass) - assert Dependencies.autoloaded?("ClassFolder::NestedClass") - assert Dependencies.autoloaded?("ClassFolder::SiblingClass") + assert ActiveSupport::Dependencies.autoloaded?("ClassFolder::NestedClass") + assert ActiveSupport::Dependencies.autoloaded?("ClassFolder::SiblingClass") - Dependencies.clear + ActiveSupport::Dependencies.clear assert ! defined?(ClassFolder::NestedClass) assert ! defined?(ClassFolder::SiblingClass) @@ -655,10 +655,10 @@ def test_nested_file_with_multiple_constants_and_auto_loading assert defined?(ClassFolder::NestedClass) assert defined?(ClassFolder::SiblingClass) - assert Dependencies.autoloaded?("ClassFolder::NestedClass") - assert Dependencies.autoloaded?("ClassFolder::SiblingClass") + assert ActiveSupport::Dependencies.autoloaded?("ClassFolder::NestedClass") + assert ActiveSupport::Dependencies.autoloaded?("ClassFolder::SiblingClass") - Dependencies.clear + ActiveSupport::Dependencies.clear assert ! defined?(ClassFolder::NestedClass) assert ! defined?(ClassFolder::SiblingClass) @@ -693,7 +693,7 @@ def test_autoload_doesnt_shadow_no_method_error_with_absolute_constant def test_autoload_doesnt_shadow_error_when_mechanism_not_set_to_load with_loading 'autoloading_fixtures' do - Dependencies.mechanism = :require + ActiveSupport::Dependencies.mechanism = :require 2.times do assert_raise(NameError) {"RaisesNameError".constantize} end @@ -727,39 +727,39 @@ def test_autoload_doesnt_shadow_name_error def test_remove_constant_handles_double_colon_at_start Object.const_set 'DeleteMe', Module.new DeleteMe.const_set 'OrMe', Module.new - Dependencies.remove_constant "::DeleteMe::OrMe" + ActiveSupport::Dependencies.remove_constant "::DeleteMe::OrMe" assert ! defined?(DeleteMe::OrMe) assert defined?(DeleteMe) - Dependencies.remove_constant "::DeleteMe" + ActiveSupport::Dependencies.remove_constant "::DeleteMe" assert ! defined?(DeleteMe) end def test_load_once_constants_should_not_be_unloaded with_loading 'autoloading_fixtures' do - Dependencies.load_once_paths = Dependencies.load_paths + ActiveSupport::Dependencies.load_once_paths = ActiveSupport::Dependencies.load_paths ::A.to_s assert defined?(A) - Dependencies.clear + ActiveSupport::Dependencies.clear assert defined?(A) end ensure - Dependencies.load_once_paths = [] + ActiveSupport::Dependencies.load_once_paths = [] Object.class_eval { remove_const :A if const_defined?(:A) } end def test_load_once_paths_should_behave_when_recursively_loading with_loading 'dependencies', 'autoloading_fixtures' do - Dependencies.load_once_paths = [Dependencies.load_paths.last] + ActiveSupport::Dependencies.load_once_paths = [ActiveSupport::Dependencies.load_paths.last] assert !defined?(CrossSiteDependency) assert_nothing_raised { CrossSiteDepender.nil? } assert defined?(CrossSiteDependency) - assert !Dependencies.autoloaded?(CrossSiteDependency), + assert !ActiveSupport::Dependencies.autoloaded?(CrossSiteDependency), "CrossSiteDependency shouldn't be marked as autoloaded!" - Dependencies.clear + ActiveSupport::Dependencies.clear assert defined?(CrossSiteDependency), "CrossSiteDependency shouldn't have been unloaded!" end ensure - Dependencies.load_once_paths = [] + ActiveSupport::Dependencies.load_once_paths = [] end end diff --git a/activesupport/test/deprecation_test.rb b/activesupport/test/deprecation_test.rb index ebfa405947a36..27e9573ce21c8 100644 --- a/activesupport/test/deprecation_test.rb +++ b/activesupport/test/deprecation_test.rb @@ -24,6 +24,11 @@ def c; end def d; end def e; end deprecate :a, :b, :c => :e, :d => "you now need to do something extra for this one" + + module B + C = 1 + end + A = ActiveSupport::Deprecation::DeprecatedConstantProxy.new('Deprecatee::A', 'Deprecatee::B::C') end @@ -83,6 +88,11 @@ def test_deprecated_instance_variable_proxy_shouldnt_warn_on_inspect assert_not_deprecated { assert_equal @dtc.request.inspect, @dtc.old_request.inspect } end + def test_deprecated_constant_proxy + assert_not_deprecated { Deprecatee::B::C } + assert_deprecated('Deprecatee::A') { assert_equal Deprecatee::B::C, Deprecatee::A } + end + def test_assert_deprecation_without_match assert_deprecated do @dtc.partially diff --git a/activesupport/test/inflector_test.rb b/activesupport/test/inflector_test.rb index 26af245ff709f..4ce9cbb705af3 100644 --- a/activesupport/test/inflector_test.rb +++ b/activesupport/test/inflector_test.rb @@ -12,188 +12,188 @@ class InflectorTest < Test::Unit::TestCase include InflectorTestCases def test_pluralize_plurals - assert_equal "plurals", Inflector.pluralize("plurals") - assert_equal "Plurals", Inflector.pluralize("Plurals") + assert_equal "plurals", ActiveSupport::Inflector.pluralize("plurals") + assert_equal "Plurals", ActiveSupport::Inflector.pluralize("Plurals") end def test_pluralize_empty_string - assert_equal "", Inflector.pluralize("") + assert_equal "", ActiveSupport::Inflector.pluralize("") end SingularToPlural.each do |singular, plural| define_method "test_pluralize_#{singular}" do - assert_equal(plural, Inflector.pluralize(singular)) - assert_equal(plural.capitalize, Inflector.pluralize(singular.capitalize)) + assert_equal(plural, ActiveSupport::Inflector.pluralize(singular)) + assert_equal(plural.capitalize, ActiveSupport::Inflector.pluralize(singular.capitalize)) end end SingularToPlural.each do |singular, plural| define_method "test_singularize_#{plural}" do - assert_equal(singular, Inflector.singularize(plural)) - assert_equal(singular.capitalize, Inflector.singularize(plural.capitalize)) + assert_equal(singular, ActiveSupport::Inflector.singularize(plural)) + assert_equal(singular.capitalize, ActiveSupport::Inflector.singularize(plural.capitalize)) end end MixtureToTitleCase.each do |before, titleized| define_method "test_titleize_#{before}" do - assert_equal(titleized, Inflector.titleize(before)) + assert_equal(titleized, ActiveSupport::Inflector.titleize(before)) end end def test_camelize CamelToUnderscore.each do |camel, underscore| - assert_equal(camel, Inflector.camelize(underscore)) + assert_equal(camel, ActiveSupport::Inflector.camelize(underscore)) end end def test_underscore CamelToUnderscore.each do |camel, underscore| - assert_equal(underscore, Inflector.underscore(camel)) + assert_equal(underscore, ActiveSupport::Inflector.underscore(camel)) end CamelToUnderscoreWithoutReverse.each do |camel, underscore| - assert_equal(underscore, Inflector.underscore(camel)) + assert_equal(underscore, ActiveSupport::Inflector.underscore(camel)) end end def test_camelize_with_module CamelWithModuleToUnderscoreWithSlash.each do |camel, underscore| - assert_equal(camel, Inflector.camelize(underscore)) + assert_equal(camel, ActiveSupport::Inflector.camelize(underscore)) end end def test_underscore_with_slashes CamelWithModuleToUnderscoreWithSlash.each do |camel, underscore| - assert_equal(underscore, Inflector.underscore(camel)) + assert_equal(underscore, ActiveSupport::Inflector.underscore(camel)) end end def test_demodulize - assert_equal "Account", Inflector.demodulize("MyApplication::Billing::Account") + assert_equal "Account", ActiveSupport::Inflector.demodulize("MyApplication::Billing::Account") end def test_foreign_key ClassNameToForeignKeyWithUnderscore.each do |klass, foreign_key| - assert_equal(foreign_key, Inflector.foreign_key(klass)) + assert_equal(foreign_key, ActiveSupport::Inflector.foreign_key(klass)) end ClassNameToForeignKeyWithoutUnderscore.each do |klass, foreign_key| - assert_equal(foreign_key, Inflector.foreign_key(klass, false)) + assert_equal(foreign_key, ActiveSupport::Inflector.foreign_key(klass, false)) end end def test_tableize ClassNameToTableName.each do |class_name, table_name| - assert_equal(table_name, Inflector.tableize(class_name)) + assert_equal(table_name, ActiveSupport::Inflector.tableize(class_name)) end end def test_classify ClassNameToTableName.each do |class_name, table_name| - assert_equal(class_name, Inflector.classify(table_name)) - assert_equal(class_name, Inflector.classify("table_prefix." + table_name)) + assert_equal(class_name, ActiveSupport::Inflector.classify(table_name)) + assert_equal(class_name, ActiveSupport::Inflector.classify("table_prefix." + table_name)) end end def test_classify_with_symbol assert_nothing_raised do - assert_equal 'FooBar', Inflector.classify(:foo_bars) + assert_equal 'FooBar', ActiveSupport::Inflector.classify(:foo_bars) end end def test_classify_with_leading_schema_name - assert_equal 'FooBar', Inflector.classify('schema.foo_bar') + assert_equal 'FooBar', ActiveSupport::Inflector.classify('schema.foo_bar') end def test_humanize UnderscoreToHuman.each do |underscore, human| - assert_equal(human, Inflector.humanize(underscore)) + assert_equal(human, ActiveSupport::Inflector.humanize(underscore)) end end def test_constantize - assert_nothing_raised { assert_equal Ace::Base::Case, Inflector.constantize("Ace::Base::Case") } - assert_nothing_raised { assert_equal Ace::Base::Case, Inflector.constantize("::Ace::Base::Case") } - assert_nothing_raised { assert_equal InflectorTest, Inflector.constantize("InflectorTest") } - assert_nothing_raised { assert_equal InflectorTest, Inflector.constantize("::InflectorTest") } - assert_raises(NameError) { Inflector.constantize("UnknownClass") } - assert_raises(NameError) { Inflector.constantize("An invalid string") } - assert_raises(NameError) { Inflector.constantize("InvalidClass\n") } + assert_nothing_raised { assert_equal Ace::Base::Case, ActiveSupport::Inflector.constantize("Ace::Base::Case") } + assert_nothing_raised { assert_equal Ace::Base::Case, ActiveSupport::Inflector.constantize("::Ace::Base::Case") } + assert_nothing_raised { assert_equal InflectorTest, ActiveSupport::Inflector.constantize("InflectorTest") } + assert_nothing_raised { assert_equal InflectorTest, ActiveSupport::Inflector.constantize("::InflectorTest") } + assert_raises(NameError) { ActiveSupport::Inflector.constantize("UnknownClass") } + assert_raises(NameError) { ActiveSupport::Inflector.constantize("An invalid string") } + assert_raises(NameError) { ActiveSupport::Inflector.constantize("InvalidClass\n") } end def test_constantize_does_lexical_lookup - assert_raises(NameError) { Inflector.constantize("Ace::Base::InflectorTest") } + assert_raises(NameError) { ActiveSupport::Inflector.constantize("Ace::Base::InflectorTest") } end def test_ordinal OrdinalNumbers.each do |number, ordinalized| - assert_equal(ordinalized, Inflector.ordinalize(number)) + assert_equal(ordinalized, ActiveSupport::Inflector.ordinalize(number)) end end def test_dasherize UnderscoresToDashes.each do |underscored, dasherized| - assert_equal(dasherized, Inflector.dasherize(underscored)) + assert_equal(dasherized, ActiveSupport::Inflector.dasherize(underscored)) end end def test_underscore_as_reverse_of_dasherize UnderscoresToDashes.each do |underscored, dasherized| - assert_equal(underscored, Inflector.underscore(Inflector.dasherize(underscored))) + assert_equal(underscored, ActiveSupport::Inflector.underscore(ActiveSupport::Inflector.dasherize(underscored))) end end def test_underscore_to_lower_camel UnderscoreToLowerCamel.each do |underscored, lower_camel| - assert_equal(lower_camel, Inflector.camelize(underscored, false)) + assert_equal(lower_camel, ActiveSupport::Inflector.camelize(underscored, false)) end end - + %w{plurals singulars uncountables}.each do |inflection_type| class_eval " def test_clear_#{inflection_type} - cached_values = Inflector.inflections.#{inflection_type} - Inflector.inflections.clear :#{inflection_type} - assert Inflector.inflections.#{inflection_type}.empty?, \"#{inflection_type} inflections should be empty after clear :#{inflection_type}\" - Inflector.inflections.instance_variable_set :@#{inflection_type}, cached_values + cached_values = ActiveSupport::Inflector.inflections.#{inflection_type} + ActiveSupport::Inflector.inflections.clear :#{inflection_type} + assert ActiveSupport::Inflector.inflections.#{inflection_type}.empty?, \"#{inflection_type} inflections should be empty after clear :#{inflection_type}\" + ActiveSupport::Inflector.inflections.instance_variable_set :@#{inflection_type}, cached_values end " end - + def test_clear_all - cached_values = Inflector.inflections.plurals, Inflector.inflections.singulars, Inflector.inflections.uncountables - Inflector.inflections.clear :all - assert Inflector.inflections.plurals.empty? - assert Inflector.inflections.singulars.empty? - assert Inflector.inflections.uncountables.empty? - Inflector.inflections.instance_variable_set :@plurals, cached_values[0] - Inflector.inflections.instance_variable_set :@singulars, cached_values[1] - Inflector.inflections.instance_variable_set :@uncountables, cached_values[2] - end - + cached_values = ActiveSupport::Inflector.inflections.plurals, ActiveSupport::Inflector.inflections.singulars, ActiveSupport::Inflector.inflections.uncountables + ActiveSupport::Inflector.inflections.clear :all + assert ActiveSupport::Inflector.inflections.plurals.empty? + assert ActiveSupport::Inflector.inflections.singulars.empty? + assert ActiveSupport::Inflector.inflections.uncountables.empty? + ActiveSupport::Inflector.inflections.instance_variable_set :@plurals, cached_values[0] + ActiveSupport::Inflector.inflections.instance_variable_set :@singulars, cached_values[1] + ActiveSupport::Inflector.inflections.instance_variable_set :@uncountables, cached_values[2] + end + def test_clear_with_default - cached_values = Inflector.inflections.plurals, Inflector.inflections.singulars, Inflector.inflections.uncountables - Inflector.inflections.clear - assert Inflector.inflections.plurals.empty? - assert Inflector.inflections.singulars.empty? - assert Inflector.inflections.uncountables.empty? - Inflector.inflections.instance_variable_set :@plurals, cached_values[0] - Inflector.inflections.instance_variable_set :@singulars, cached_values[1] - Inflector.inflections.instance_variable_set :@uncountables, cached_values[2] + cached_values = ActiveSupport::Inflector.inflections.plurals, ActiveSupport::Inflector.inflections.singulars, ActiveSupport::Inflector.inflections.uncountables + ActiveSupport::Inflector.inflections.clear + assert ActiveSupport::Inflector.inflections.plurals.empty? + assert ActiveSupport::Inflector.inflections.singulars.empty? + assert ActiveSupport::Inflector.inflections.uncountables.empty? + ActiveSupport::Inflector.inflections.instance_variable_set :@plurals, cached_values[0] + ActiveSupport::Inflector.inflections.instance_variable_set :@singulars, cached_values[1] + ActiveSupport::Inflector.inflections.instance_variable_set :@uncountables, cached_values[2] end Irregularities.each do |irregularity| singular, plural = *irregularity - Inflector.inflections do |inflect| + ActiveSupport::Inflector.inflections do |inflect| define_method("test_irregularity_between_#{singular}_and_#{plural}") do inflect.irregular(singular, plural) - assert_equal singular, Inflector.singularize(plural) - assert_equal plural, Inflector.pluralize(singular) + assert_equal singular, ActiveSupport::Inflector.singularize(plural) + assert_equal plural, ActiveSupport::Inflector.pluralize(singular) end end end [ :all, [] ].each do |scope| - Inflector.inflections do |inflect| + ActiveSupport::Inflector.inflections do |inflect| define_method("test_clear_inflections_with_#{scope.kind_of?(Array) ? "no_arguments" : scope}") do # save all the inflections singulars, plurals, uncountables = inflect.singulars, inflect.plurals, inflect.uncountables @@ -218,7 +218,7 @@ def test_clear_with_default end { :singulars => :singular, :plurals => :plural, :uncountables => :uncountable }.each do |scope, method| - Inflector.inflections do |inflect| + ActiveSupport::Inflector.inflections do |inflect| define_method("test_clear_inflections_with_#{scope}") do # save the inflections values = inflect.send(scope) diff --git a/activesupport/test/ordered_options_test.rb b/activesupport/test/ordered_options_test.rb index fb7a58d0b0f8d..e48425ca25b9a 100644 --- a/activesupport/test/ordered_options_test.rb +++ b/activesupport/test/ordered_options_test.rb @@ -2,7 +2,7 @@ class OrderedOptionsTest < Test::Unit::TestCase def test_usage - a = OrderedOptions.new + a = ActiveSupport::OrderedOptions.new assert_nil a[:not_set] @@ -20,7 +20,7 @@ def test_usage end def test_looping - a = OrderedOptions.new + a = ActiveSupport::OrderedOptions.new a[:allow_concurreny] = true a["else_where"] = 56 @@ -34,7 +34,7 @@ def test_looping end def test_method_access - a = OrderedOptions.new + a = ActiveSupport::OrderedOptions.new assert_nil a.not_set diff --git a/activesupport/test/time_zone_test.rb b/activesupport/test/time_zone_test.rb index f3069b589b1bf..b42dcd17f8aae 100644 --- a/activesupport/test/time_zone_test.rb +++ b/activesupport/test/time_zone_test.rb @@ -1,10 +1,9 @@ require 'abstract_unit' class TimeZoneTest < Test::Unit::TestCase - def test_utc_to_local silence_warnings do # silence warnings raised by tzinfo gem - zone = TimeZone['Eastern Time (US & Canada)'] + zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)'] assert_equal Time.utc(1999, 12, 31, 19), zone.utc_to_local(Time.utc(2000, 1)) # standard offset -0500 assert_equal Time.utc(2000, 6, 30, 20), zone.utc_to_local(Time.utc(2000, 7)) # dst offset -0400 end @@ -12,41 +11,41 @@ def test_utc_to_local def test_local_to_utc silence_warnings do # silence warnings raised by tzinfo gem - zone = TimeZone['Eastern Time (US & Canada)'] + zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)'] assert_equal Time.utc(2000, 1, 1, 5), zone.local_to_utc(Time.utc(2000, 1)) # standard offset -0500 assert_equal Time.utc(2000, 7, 1, 4), zone.local_to_utc(Time.utc(2000, 7)) # dst offset -0400 end end - + def test_period_for_local silence_warnings do # silence warnings raised by tzinfo gem - zone = TimeZone['Eastern Time (US & Canada)'] + zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)'] assert_instance_of TZInfo::TimezonePeriod, zone.period_for_local(Time.utc(2000)) end end - - TimeZone::MAPPING.keys.each do |name| + + ActiveSupport::TimeZone::MAPPING.keys.each do |name| define_method("test_map_#{name.downcase.gsub(/[^a-z]/, '_')}_to_tzinfo") do silence_warnings do # silence warnings raised by tzinfo gem - zone = TimeZone[name] + zone = ActiveSupport::TimeZone[name] assert zone.tzinfo.respond_to?(:period_for_local) end end end def test_from_integer_to_map - assert_instance_of TimeZone, TimeZone[-28800] # PST + assert_instance_of ActiveSupport::TimeZone, ActiveSupport::TimeZone[-28800] # PST end def test_from_duration_to_map - assert_instance_of TimeZone, TimeZone[-480.minutes] # PST + assert_instance_of ActiveSupport::TimeZone, ActiveSupport::TimeZone[-480.minutes] # PST end - TimeZone.all.each do |zone| + ActiveSupport::TimeZone.all.each do |zone| name = zone.name.downcase.gsub(/[^a-z]/, '_') define_method("test_from_#{name}_to_map") do silence_warnings do # silence warnings raised by tzinfo gem - assert_instance_of TimeZone, TimeZone[zone.name] + assert_instance_of ActiveSupport::TimeZone, ActiveSupport::TimeZone[zone.name] end end @@ -62,62 +61,62 @@ def test_from_duration_to_map def test_now with_env_tz 'US/Eastern' do Time.stubs(:now).returns(Time.local(2000)) - zone = TimeZone['Eastern Time (US & Canada)'] + zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)'] assert_instance_of ActiveSupport::TimeWithZone, zone.now assert_equal Time.utc(2000,1,1,5), zone.now.utc assert_equal Time.utc(2000), zone.now.time assert_equal zone, zone.now.time_zone end end - + def test_now_enforces_spring_dst_rules with_env_tz 'US/Eastern' do Time.stubs(:now).returns(Time.local(2006,4,2,2)) # 2AM springs forward to 3AM - zone = TimeZone['Eastern Time (US & Canada)'] + zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)'] assert_equal Time.utc(2006,4,2,3), zone.now.time assert_equal true, zone.now.dst? end end - + def test_now_enforces_fall_dst_rules with_env_tz 'US/Eastern' do Time.stubs(:now).returns(Time.at(1162098000)) # equivalent to 1AM DST - zone = TimeZone['Eastern Time (US & Canada)'] + zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)'] assert_equal Time.utc(2006,10,29,1), zone.now.time assert_equal true, zone.now.dst? end end - + def test_today Time.stubs(:now).returns(Time.utc(2000, 1, 1, 4, 59, 59)) # 1 sec before midnight Jan 1 EST - assert_equal Date.new(1999, 12, 31), TimeZone['Eastern Time (US & Canada)'].today + assert_equal Date.new(1999, 12, 31), ActiveSupport::TimeZone['Eastern Time (US & Canada)'].today Time.stubs(:now).returns(Time.utc(2000, 1, 1, 5)) # midnight Jan 1 EST - assert_equal Date.new(2000, 1, 1), TimeZone['Eastern Time (US & Canada)'].today + assert_equal Date.new(2000, 1, 1), ActiveSupport::TimeZone['Eastern Time (US & Canada)'].today Time.stubs(:now).returns(Time.utc(2000, 1, 2, 4, 59, 59)) # 1 sec before midnight Jan 2 EST - assert_equal Date.new(2000, 1, 1), TimeZone['Eastern Time (US & Canada)'].today + assert_equal Date.new(2000, 1, 1), ActiveSupport::TimeZone['Eastern Time (US & Canada)'].today Time.stubs(:now).returns(Time.utc(2000, 1, 2, 5)) # midnight Jan 2 EST - assert_equal Date.new(2000, 1, 2), TimeZone['Eastern Time (US & Canada)'].today + assert_equal Date.new(2000, 1, 2), ActiveSupport::TimeZone['Eastern Time (US & Canada)'].today end end - + def test_local silence_warnings do # silence warnings raised by tzinfo gem - time = TimeZone["Hawaii"].local(2007, 2, 5, 15, 30, 45) + time = ActiveSupport::TimeZone["Hawaii"].local(2007, 2, 5, 15, 30, 45) assert_equal Time.utc(2007, 2, 5, 15, 30, 45), time.time - assert_equal TimeZone["Hawaii"], time.time_zone + assert_equal ActiveSupport::TimeZone["Hawaii"], time.time_zone end end def test_local_with_old_date silence_warnings do # silence warnings raised by tzinfo gem - time = TimeZone["Hawaii"].local(1850, 2, 5, 15, 30, 45) + time = ActiveSupport::TimeZone["Hawaii"].local(1850, 2, 5, 15, 30, 45) assert_equal [45,30,15,5,2,1850], time.to_a[0,6] - assert_equal TimeZone["Hawaii"], time.time_zone + assert_equal ActiveSupport::TimeZone["Hawaii"], time.time_zone end end def test_local_enforces_spring_dst_rules - zone = TimeZone['Eastern Time (US & Canada)'] + zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)'] twz = zone.local(2006,4,2,1,59,59) # 1 second before DST start assert_equal Time.utc(2006,4,2,1,59,59), twz.time assert_equal Time.utc(2006,4,2,6,59,59), twz.utc @@ -138,16 +137,16 @@ def test_local_enforces_spring_dst_rules def test_local_enforces_fall_dst_rules # 1AM during fall DST transition is ambiguous, it could be either DST or non-DST 1AM # Mirroring Time.local behavior, this method selects the DST time - zone = TimeZone['Eastern Time (US & Canada)'] + zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)'] twz = zone.local(2006,10,29,1) assert_equal Time.utc(2006,10,29,1), twz.time assert_equal Time.utc(2006,10,29,5), twz.utc - assert_equal true, twz.dst? + assert_equal true, twz.dst? assert_equal 'EDT', twz.zone end def test_at - zone = TimeZone['Eastern Time (US & Canada)'] + zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)'] secs = 946684800.0 twz = zone.at(secs) assert_equal Time.utc(1999,12,31,19), twz.time @@ -157,7 +156,7 @@ def test_at end def test_at_with_old_date - zone = TimeZone['Eastern Time (US & Canada)'] + zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)'] secs = DateTime.civil(1850).to_f twz = zone.at(secs) assert_equal [1850, 1, 1, 0], [twz.utc.year, twz.utc.mon, twz.utc.day, twz.utc.hour] @@ -166,7 +165,7 @@ def test_at_with_old_date end def test_parse - zone = TimeZone['Eastern Time (US & Canada)'] + zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)'] twz = zone.parse('1999-12-31 19:00:00') assert_equal Time.utc(1999,12,31,19), twz.time assert_equal Time.utc(2000), twz.utc @@ -175,7 +174,7 @@ def test_parse def test_parse_string_with_timezone (-11..13).each do |timezone_offset| - zone = TimeZone[timezone_offset] + zone = ActiveSupport::TimeZone[timezone_offset] twz = zone.parse('1999-12-31 19:00:00') assert_equal twz, zone.parse(twz.to_s) end @@ -183,25 +182,25 @@ def test_parse_string_with_timezone def test_parse_with_old_date silence_warnings do # silence warnings raised by tzinfo gem - zone = TimeZone['Eastern Time (US & Canada)'] + zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)'] twz = zone.parse('1850-12-31 19:00:00') assert_equal [0,0,19,31,12,1850], twz.to_a[0,6] assert_equal zone, twz.time_zone end end - + def test_parse_far_future_date_with_time_zone_offset_in_string silence_warnings do # silence warnings raised by tzinfo gem - zone = TimeZone['Eastern Time (US & Canada)'] + zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)'] twz = zone.parse('2050-12-31 19:00:00 -10:00') # i.e., 2050-01-01 05:00:00 UTC assert_equal [0,0,0,1,1,2051], twz.to_a[0,6] assert_equal zone, twz.time_zone end end - + def test_parse_returns_nil_when_string_without_date_information_is_passed_in silence_warnings do # silence warnings raised by tzinfo gem - zone = TimeZone['Eastern Time (US & Canada)'] + zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)'] assert_nil zone.parse('foobar') assert_nil zone.parse(' ') end @@ -209,80 +208,80 @@ def test_parse_returns_nil_when_string_without_date_information_is_passed_in uses_mocha 'TestParseWithIncompleteDate' do def test_parse_with_incomplete_date - zone = TimeZone['Eastern Time (US & Canada)'] + zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)'] zone.stubs(:now).returns zone.local(1999,12,31) twz = zone.parse('19:00:00') assert_equal Time.utc(1999,12,31,19), twz.time end end - + def test_utc_offset_lazy_loaded_from_tzinfo_when_not_passed_in_to_initialize silence_warnings do # silence warnings raised by tzinfo gem tzinfo = TZInfo::Timezone.get('America/New_York') - zone = TimeZone.create(tzinfo.name, nil, tzinfo) + zone = ActiveSupport::TimeZone.create(tzinfo.name, nil, tzinfo) assert_equal nil, zone.instance_variable_get('@utc_offset') assert_equal(-18_000, zone.utc_offset) end end - + def test_formatted_offset_positive - zone = TimeZone['Moscow'] + zone = ActiveSupport::TimeZone['Moscow'] assert_equal "+03:00", zone.formatted_offset assert_equal "+0300", zone.formatted_offset(false) end - + def test_formatted_offset_negative - zone = TimeZone['Eastern Time (US & Canada)'] + zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)'] assert_equal "-05:00", zone.formatted_offset assert_equal "-0500", zone.formatted_offset(false) end - + def test_formatted_offset_zero - zone = TimeZone['London'] + zone = ActiveSupport::TimeZone['London'] assert_equal "+00:00", zone.formatted_offset assert_equal "UTC", zone.formatted_offset(true, 'UTC') end - + def test_zone_compare - zone1 = TimeZone['Central Time (US & Canada)'] # offset -0600 - zone2 = TimeZone['Eastern Time (US & Canada)'] # offset -0500 + zone1 = ActiveSupport::TimeZone['Central Time (US & Canada)'] # offset -0600 + zone2 = ActiveSupport::TimeZone['Eastern Time (US & Canada)'] # offset -0500 assert zone1 < zone2 assert zone2 > zone1 assert zone1 == zone1 end - + def test_to_s - assert_equal "(GMT+03:00) Moscow", TimeZone['Moscow'].to_s + assert_equal "(GMT+03:00) Moscow", ActiveSupport::TimeZone['Moscow'].to_s end - + def test_all_sorted - all = TimeZone.all + all = ActiveSupport::TimeZone.all 1.upto( all.length-1 ) do |i| assert all[i-1] < all[i] end end - + def test_index - assert_nil TimeZone["bogus"] - assert_instance_of TimeZone, TimeZone["Central Time (US & Canada)"] - assert_instance_of TimeZone, TimeZone[8] - assert_raises(ArgumentError) { TimeZone[false] } + assert_nil ActiveSupport::TimeZone["bogus"] + assert_instance_of ActiveSupport::TimeZone, ActiveSupport::TimeZone["Central Time (US & Canada)"] + assert_instance_of ActiveSupport::TimeZone, ActiveSupport::TimeZone[8] + assert_raises(ArgumentError) { ActiveSupport::TimeZone[false] } end - + def test_new - assert_equal TimeZone["Central Time (US & Canada)"], TimeZone.new("Central Time (US & Canada)") + assert_equal ActiveSupport::TimeZone["Central Time (US & Canada)"], ActiveSupport::TimeZone.new("Central Time (US & Canada)") end - + def test_us_zones - assert TimeZone.us_zones.include?(TimeZone["Hawaii"]) - assert !TimeZone.us_zones.include?(TimeZone["Kuala Lumpur"]) - end - + assert ActiveSupport::TimeZone.us_zones.include?(ActiveSupport::TimeZone["Hawaii"]) + assert !ActiveSupport::TimeZone.us_zones.include?(ActiveSupport::TimeZone["Kuala Lumpur"]) + end + protected def with_env_tz(new_tz = 'US/Eastern') old_tz, ENV['TZ'] = ENV['TZ'], new_tz yield ensure old_tz ? ENV['TZ'] = old_tz : ENV.delete('TZ') - end + end end From a977f3e88e989cd7d795c46504451b4b1b4db79d Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Tue, 3 Jun 2008 13:35:03 -0500 Subject: [PATCH 022/200] Fixed ambiguous first argument warning in ArrayExtTest. --- activesupport/test/core_ext/array_ext_test.rb | 26 +++++++++---------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/activesupport/test/core_ext/array_ext_test.rb b/activesupport/test/core_ext/array_ext_test.rb index 20bc5dfcb7568..73fbeb8b6b4c5 100644 --- a/activesupport/test/core_ext/array_ext_test.rb +++ b/activesupport/test/core_ext/array_ext_test.rb @@ -21,7 +21,7 @@ def to_param "#{self}1" end end - + def test_string_array assert_equal '', %w().to_param assert_equal 'hello/world', %w(hello world).to_param @@ -31,7 +31,7 @@ def test_string_array def test_number_array assert_equal '10/20', [10, 20].to_param end - + def test_to_param_array assert_equal 'custom1/param1', [ToParam.new('custom'), ToParam.new('param')].to_param end @@ -222,10 +222,10 @@ def test_to_xml_with_block assert xml.include?(%(2)), xml end - + def test_to_xml_with_empty xml = [].to_xml - assert_match /type="array"\/>/, xml + assert_match(/type="array"\/>/, xml) end end @@ -239,17 +239,15 @@ def test_extract_options end uses_mocha "ArrayExtRandomTests" do + class ArrayExtRandomTests < Test::Unit::TestCase + def test_random_element_from_array + assert_nil [].rand -class ArrayExtRandomTests < Test::Unit::TestCase - def test_random_element_from_array - assert_nil [].rand - - Kernel.expects(:rand).with(1).returns(0) - assert_equal 'x', ['x'].rand + Kernel.expects(:rand).with(1).returns(0) + assert_equal 'x', ['x'].rand - Kernel.expects(:rand).with(3).returns(1) - assert_equal 2, [1, 2, 3].rand + Kernel.expects(:rand).with(3).returns(1) + assert_equal 2, [1, 2, 3].rand + end end end - -end From aa1771668877f20ca044e8f45a9736fbb7c8402e Mon Sep 17 00:00:00 2001 From: Craig Demyanovich Date: Tue, 3 Jun 2008 13:38:00 -0500 Subject: [PATCH 023/200] Callbacks fire before notifying observers [#230 state:resolved] Signed-off-by: Joshua Peek --- activerecord/lib/active_record/callbacks.rb | 4 +- .../test/cases/callbacks_observers_test.rb | 38 +++++++++++++++++++ 2 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 activerecord/test/cases/callbacks_observers_test.rb diff --git a/activerecord/lib/active_record/callbacks.rb b/activerecord/lib/active_record/callbacks.rb index 41ec5c5e61d21..4edc209c65b24 100755 --- a/activerecord/lib/active_record/callbacks.rb +++ b/activerecord/lib/active_record/callbacks.rb @@ -293,14 +293,14 @@ def destroy_with_callbacks #:nodoc: private def callback(method) - notify(method) - result = run_callbacks(method) { |result, object| result == false } if result != false && respond_to_without_attributes?(method) result = send(method) end + notify(method) + return result end diff --git a/activerecord/test/cases/callbacks_observers_test.rb b/activerecord/test/cases/callbacks_observers_test.rb new file mode 100644 index 0000000000000..87de5249236b7 --- /dev/null +++ b/activerecord/test/cases/callbacks_observers_test.rb @@ -0,0 +1,38 @@ +require "cases/helper" + +class Comment < ActiveRecord::Base + attr_accessor :callers + + before_validation :record_callers + + def after_validation + record_callers + end + + def record_callers + callers << self.class if callers + end +end + +class CommentObserver < ActiveRecord::Observer + attr_accessor :callers + + def after_validation(model) + callers << self.class if callers + end +end + +class CallbacksObserversTest < ActiveRecord::TestCase + def test_model_callbacks_fire_before_observers_are_notified + callers = [] + + comment = Comment.new + comment.callers = callers + + CommentObserver.instance.callers = callers + + comment.valid? + + assert_equal [Comment, Comment, CommentObserver], callers, "model callbacks did not fire before observers were notified" + end +end From d54d90f2b590c763fe710482a9b993923fe03ec0 Mon Sep 17 00:00:00 2001 From: josevalim Date: Tue, 3 Jun 2008 14:02:51 -0500 Subject: [PATCH 024/200] Allow caches_action to accept a layout option [#198 state:resolved] Signed-off-by: Joshua Peek --- actionpack/CHANGELOG | 2 ++ .../lib/action_controller/caching/actions.rb | 20 ++++++++++++++++--- actionpack/test/controller/caching_test.rb | 15 ++++++++++++++ 3 files changed, 34 insertions(+), 3 deletions(-) diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG index 9622029362865..ba2b16849c4dc 100644 --- a/actionpack/CHANGELOG +++ b/actionpack/CHANGELOG @@ -1,3 +1,5 @@ +* Allow caches_action to accept a layout option [José Valim] + * Added Rack processor [Ezra Zygmuntowicz, Josh Peek] diff --git a/actionpack/lib/action_controller/caching/actions.rb b/actionpack/lib/action_controller/caching/actions.rb index 1ef9e60a21af1..c4b0a97a33d75 100644 --- a/actionpack/lib/action_controller/caching/actions.rb +++ b/actionpack/lib/action_controller/caching/actions.rb @@ -40,6 +40,8 @@ module Caching # controller.send(:list_url, c.params[:id]) } # end # + # If you pass :layout => false, it will only cache your action content. It is useful when your layout has dynamic information. + # module Actions def self.included(base) #:nodoc: base.extend(ClassMethods) @@ -54,7 +56,8 @@ module ClassMethods def caches_action(*actions) return unless cache_configured? options = actions.extract_options! - around_filter(ActionCacheFilter.new(:cache_path => options.delete(:cache_path)), {:only => actions}.merge(options)) + cache_filter = ActionCacheFilter.new(:layout => options.delete(:layout), :cache_path => options.delete(:cache_path)) + around_filter(cache_filter, {:only => actions}.merge(options)) end end @@ -81,7 +84,9 @@ def before(controller) if cache = controller.read_fragment(cache_path.path) controller.rendered_action_cache = true set_content_type!(controller, cache_path.extension) - controller.send!(:render_for_text, cache) + options = { :text => cache } + options.merge!(:layout => true) if cache_layout? + controller.send!(:render, options) false else controller.action_cache_path = cache_path @@ -90,7 +95,8 @@ def before(controller) def after(controller) return if controller.rendered_action_cache || !caching_allowed(controller) - controller.write_fragment(controller.action_cache_path.path, controller.response.body) + action_content = cache_layout? ? content_for_layout(controller) : controller.response.body + controller.write_fragment(controller.action_cache_path.path, action_content) end private @@ -105,6 +111,14 @@ def path_options_for(controller, options) def caching_allowed(controller) controller.request.get? && controller.response.headers['Status'].to_i == 200 end + + def cache_layout? + @options[:layout] == false + end + + def content_for_layout(controller) + controller.response.layout && controller.response.template.instance_variable_get('@content_for_layout') + end end class ActionCachePath diff --git a/actionpack/test/controller/caching_test.rb b/actionpack/test/controller/caching_test.rb index f9b6b87bc6160..c6d61bb50473a 100644 --- a/actionpack/test/controller/caching_test.rb +++ b/actionpack/test/controller/caching_test.rb @@ -156,6 +156,7 @@ class ActionCachingTestController < ActionController::Base caches_action :show, :cache_path => 'http://test.host/custom/show' caches_action :edit, :cache_path => Proc.new { |c| c.params[:id] ? "http://test.host/#{c.params[:id]};edit" : "http://test.host/edit" } caches_action :with_layout + caches_action :layout_false, :layout => false layout 'talk_from_action.erb' @@ -181,6 +182,7 @@ def with_layout alias_method :show, :index alias_method :edit, :index alias_method :destroy, :index + alias_method :layout_false, :with_layout def expire expire_action :controller => 'action_caching_test', :action => 'index' @@ -263,6 +265,19 @@ def test_action_cache_with_layout assert_equal @response.body, read_fragment('hostname.com/action_caching_test/with_layout') end + def test_action_cache_with_layout_and_layout_cache_false + get :layout_false + cached_time = content_to_cache + assert_not_equal cached_time, @response.body + assert fragment_exist?('hostname.com/action_caching_test/layout_false') + reset! + + get :layout_false + assert_not_equal cached_time, @response.body + + assert_equal cached_time, read_fragment('hostname.com/action_caching_test/layout_false') + end + def test_action_cache_conditional_options @request.env['HTTP_ACCEPT'] = 'application/json' get :index From 7cfa6ec8a37b70ec302f09929df5160ac42971e7 Mon Sep 17 00:00:00 2001 From: Steve Purcell Date: Tue, 3 Jun 2008 14:15:33 -0500 Subject: [PATCH 025/200] Add more standard Hash methods to ActiveSupport::OrderedHash [#314 state:resolved] Signed-off-by: Joshua Peek --- activesupport/CHANGELOG | 4 ++++ .../lib/active_support/ordered_hash.rb | 14 ++++++++++++++ activesupport/test/ordered_hash_test.rb | 19 +++++++++++++++++++ 3 files changed, 37 insertions(+) diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG index a1a3c7ac2ba2c..a6ce37ac14925 100644 --- a/activesupport/CHANGELOG +++ b/activesupport/CHANGELOG @@ -1,5 +1,9 @@ *Edge* +* Add more standard Hash methods to ActiveSupport::OrderedHash [Steve Purcell] + +* Namespace Inflector, Dependencies, OrderedOptions, and TimeZone under ActiveSupport [Josh Peek] + * Fixed Date#end_of_quarter to not blow up on May 31st [#289 state:resolved] (Danger) diff --git a/activesupport/lib/active_support/ordered_hash.rb b/activesupport/lib/active_support/ordered_hash.rb index 6993621ef9a6a..59ceaec696697 100644 --- a/activesupport/lib/active_support/ordered_hash.rb +++ b/activesupport/lib/active_support/ordered_hash.rb @@ -38,6 +38,20 @@ def to_hash each { |array| hash[array[0]] = array[1] } end end + + def has_key?(k) + !assoc(k).nil? + end + + alias_method :key?, :has_key? + alias_method :include?, :has_key? + alias_method :member?, :has_key? + + def has_value?(v) + any? { |key, value| value == v } + end + + alias_method :value?, :has_value? end end end diff --git a/activesupport/test/ordered_hash_test.rb b/activesupport/test/ordered_hash_test.rb index 14be48724e284..98a6ad6b26c41 100644 --- a/activesupport/test/ordered_hash_test.rb +++ b/activesupport/test/ordered_hash_test.rb @@ -42,4 +42,23 @@ def test_delete assert_nil @ordered_hash.delete(bad_key) end + + def test_has_key + assert_equal true, @ordered_hash.has_key?('blue') + assert_equal true, @ordered_hash.key?('blue') + assert_equal true, @ordered_hash.include?('blue') + assert_equal true, @ordered_hash.member?('blue') + + assert_equal false, @ordered_hash.has_key?('indigo') + assert_equal false, @ordered_hash.key?('indigo') + assert_equal false, @ordered_hash.include?('indigo') + assert_equal false, @ordered_hash.member?('indigo') + end + + def test_has_value + assert_equal true, @ordered_hash.has_value?('000099') + assert_equal true, @ordered_hash.value?('000099') + assert_equal false, @ordered_hash.has_value?('ABCABC') + assert_equal false, @ordered_hash.value?('ABCABC') + end end From 8afa725f4b98a6e0ceee4792e8ebaebb6189e5f6 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Tue, 3 Jun 2008 17:44:56 -0500 Subject: [PATCH 026/200] Wrapped Rails.env in StringQuestioneer so you can do Rails.env.development? [DHH] --- activesupport/CHANGELOG | 2 ++ activesupport/lib/active_support.rb | 2 ++ .../lib/active_support/string_questioneer.rb | 9 +++++++++ activesupport/test/string_questioneer_test.rb | 15 +++++++++++++++ railties/CHANGELOG | 2 ++ railties/lib/initializer.rb | 2 +- 6 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 activesupport/lib/active_support/string_questioneer.rb create mode 100644 activesupport/test/string_questioneer_test.rb diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG index a6ce37ac14925..894b43928f01a 100644 --- a/activesupport/CHANGELOG +++ b/activesupport/CHANGELOG @@ -4,6 +4,8 @@ * Namespace Inflector, Dependencies, OrderedOptions, and TimeZone under ActiveSupport [Josh Peek] +* Added StringQuestioneer for doing things like StringQuestioneer.new("production").production? # => true and StringQuestioneer.new("production").development? # => false [DHH] + * Fixed Date#end_of_quarter to not blow up on May 31st [#289 state:resolved] (Danger) diff --git a/activesupport/lib/active_support.rb b/activesupport/lib/active_support.rb index 982adfb0cf113..2b418a1bb7cf7 100644 --- a/activesupport/lib/active_support.rb +++ b/activesupport/lib/active_support.rb @@ -43,6 +43,8 @@ require 'active_support/ordered_options' require 'active_support/option_merger' +require 'active_support/string_questioneer' + require 'active_support/values/time_zone' require 'active_support/duration' diff --git a/activesupport/lib/active_support/string_questioneer.rb b/activesupport/lib/active_support/string_questioneer.rb new file mode 100644 index 0000000000000..7732f8b401585 --- /dev/null +++ b/activesupport/lib/active_support/string_questioneer.rb @@ -0,0 +1,9 @@ +class StringQuestioneer < String + def method_missing(method_name, *arguments) + if method_name.to_s.ends_with?("?") + self == method_name.to_s[0..-2] + else + super + end + end +end \ No newline at end of file diff --git a/activesupport/test/string_questioneer_test.rb b/activesupport/test/string_questioneer_test.rb new file mode 100644 index 0000000000000..ff9d2c17f936b --- /dev/null +++ b/activesupport/test/string_questioneer_test.rb @@ -0,0 +1,15 @@ +require 'abstract_unit' + +class StringQuestioneerTest < Test::Unit::TestCase + def test_match + assert StringQuestioneer.new("production").production? + end + + def test_miss + assert !StringQuestioneer.new("production").development? + end + + def test_missing_question_mark + assert_raises(NoMethodError) { StringQuestioneer.new("production").production } + end +end \ No newline at end of file diff --git a/railties/CHANGELOG b/railties/CHANGELOG index 4d1c86e643b2e..abadeb693fd0f 100644 --- a/railties/CHANGELOG +++ b/railties/CHANGELOG @@ -1,5 +1,7 @@ *Edge* +* Wrapped Rails.env in StringQuestioneer so you can do Rails.env.development? [DHH] + * Fixed that RailsInfoController wasn't considering all requests local in development mode (Edgard Castro) [#310 state:resolved] diff --git a/railties/lib/initializer.rb b/railties/lib/initializer.rb index 9e6e02e8e01aa..d80d014527f57 100644 --- a/railties/lib/initializer.rb +++ b/railties/lib/initializer.rb @@ -37,7 +37,7 @@ def root end def env - RAILS_ENV + StringQuestioneer.new(RAILS_ENV) end def cache From 025515b234d8380f195e3de3818d076302605b12 Mon Sep 17 00:00:00 2001 From: Gabe da Silveira Date: Mon, 2 Jun 2008 19:57:35 -0300 Subject: [PATCH 027/200] Fix assert_redirected_to for nested controllers and named routes [#308 state:resolved] Signed-off-by: Michael Koziarski --- .../assertions/response_assertions.rb | 2 +- .../controller/action_pack_assertions_test.rb | 23 ++++++++++++++++--- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/actionpack/lib/action_controller/assertions/response_assertions.rb b/actionpack/lib/action_controller/assertions/response_assertions.rb index c5fc6c7966710..3deda0b45a4a1 100644 --- a/actionpack/lib/action_controller/assertions/response_assertions.rb +++ b/actionpack/lib/action_controller/assertions/response_assertions.rb @@ -97,7 +97,7 @@ def assert_redirected_to(options = {}, message=nil) value['controller'] = value['controller'].to_s if key == :actual && value['controller'].first != '/' && !value['controller'].include?('/') new_controller_path = ActionController::Routing.controller_relative_to(value['controller'], @controller.class.controller_path) - value['controller'] = new_controller_path if value['controller'] != new_controller_path && ActionController::Routing.possible_controllers.include?(new_controller_path) + value['controller'] = new_controller_path if value['controller'] != new_controller_path && ActionController::Routing.possible_controllers.include?(new_controller_path) && @response.redirected_to.is_a?(Hash) end value['controller'] = value['controller'][1..-1] if value['controller'].first == '/' # strip leading hash end diff --git a/actionpack/test/controller/action_pack_assertions_test.rb b/actionpack/test/controller/action_pack_assertions_test.rb index f152b1d19cf09..c25e9e1df64fa 100644 --- a/actionpack/test/controller/action_pack_assertions_test.rb +++ b/actionpack/test/controller/action_pack_assertions_test.rb @@ -137,6 +137,9 @@ def show end end +class UserController < ActionController::Base +end + module Admin class InnerModuleController < ActionController::Base def index @@ -174,7 +177,7 @@ class ActionPackAssertionsControllerTest < Test::Unit::TestCase # let's get this party started def setup ActionController::Routing::Routes.reload - ActionController::Routing.use_controllers!(%w(action_pack_assertions admin/inner_module content admin/user)) + ActionController::Routing.use_controllers!(%w(action_pack_assertions admin/inner_module user content admin/user)) @controller = ActionPackAssertionsController.new @request, @response = ActionController::TestRequest.new, ActionController::TestResponse.new end @@ -268,7 +271,7 @@ def test_assert_redirect_to_nested_named_route assert_redirected_to admin_inner_module_path end end - + def test_assert_redirected_to_top_level_named_route_from_nested_controller with_routing do |set| set.draw do |map| @@ -277,11 +280,25 @@ def test_assert_redirected_to_top_level_named_route_from_nested_controller end @controller = Admin::InnerModuleController.new process :redirect_to_top_level_named_route - # passes -> assert_redirected_to "http://test.host/action_pack_assertions/foo" + # assert_redirected_to "http://test.host/action_pack_assertions/foo" would pass because of exact match early return assert_redirected_to "/action_pack_assertions/foo" end end + def test_assert_redirected_to_top_level_named_route_with_same_controller_name_in_both_namespaces + with_routing do |set| + set.draw do |map| + # this controller exists in the admin namespace as well which is the only difference from previous test + map.top_level '/user/:id', :controller => 'user', :action => 'index' + map.connect ':controller/:action/:id' + end + @controller = Admin::InnerModuleController.new + process :redirect_to_top_level_named_route + # assert_redirected_to top_level_url('foo') would pass because of exact match early return + assert_redirected_to top_level_path('foo') + end + end + # -- standard request/response object testing -------------------------------- # make sure that the template objects exist From edfa195e2ace7b4fb8195333c6e44e6bf8986c11 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Tue, 3 Jun 2008 18:11:47 -0500 Subject: [PATCH 028/200] Fixed Request#remote_ip to only raise hell if the HTTP_CLIENT_IP and HTTP_X_FORWARDED_FOR doesnt match (not just if theyre both present) [Mark Imbriaco, Bradford Folkens] --- actionpack/CHANGELOG | 4 ++++ actionpack/lib/action_controller/request.rb | 9 +++++---- actionpack/test/controller/request_test.rb | 3 +++ 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG index ba2b16849c4dc..861597701c249 100644 --- a/actionpack/CHANGELOG +++ b/actionpack/CHANGELOG @@ -1,3 +1,7 @@ +*Edge* + +* Fixed Request#remote_ip to only raise hell if the HTTP_CLIENT_IP and HTTP_X_FORWARDED_FOR doesn't match (not just if they're both present) [Mark Imbriaco, Bradford Folkens] + * Allow caches_action to accept a layout option [José Valim] * Added Rack processor [Ezra Zygmuntowicz, Josh Peek] diff --git a/actionpack/lib/action_controller/request.rb b/actionpack/lib/action_controller/request.rb index a35b904194ca8..9b02f2c8a13da 100755 --- a/actionpack/lib/action_controller/request.rb +++ b/actionpack/lib/action_controller/request.rb @@ -134,14 +134,15 @@ def xml_http_request? # REMOTE_ADDR is a proxy. HTTP_X_FORWARDED_FOR may be a comma- # delimited list in the case of multiple chained proxies; the last # address which is not trusted is the originating IP. - def remote_ip if TRUSTED_PROXIES !~ @env['REMOTE_ADDR'] return @env['REMOTE_ADDR'] end + remote_ips = @env['HTTP_X_FORWARDED_FOR'] && @env['HTTP_X_FORWARDED_FOR'].split(',') + if @env.include? 'HTTP_CLIENT_IP' - if @env.include? 'HTTP_X_FORWARDED_FOR' + if remote_ips && !remote_ips.include?(@env['HTTP_CLIENT_IP']) # We don't know which came from the proxy, and which from the user raise ActionControllerError.new(< 1 && TRUSTED_PROXIES =~ remote_ips.last.strip remote_ips.pop end diff --git a/actionpack/test/controller/request_test.rb b/actionpack/test/controller/request_test.rb index 82ddfec8e8f87..2bd489b2c7bcb 100644 --- a/actionpack/test/controller/request_test.rb +++ b/actionpack/test/controller/request_test.rb @@ -59,6 +59,9 @@ def test_remote_ip assert_match /HTTP_X_FORWARDED_FOR="9.9.9.9, 3.4.5.6, 10.0.0.1, 172.31.4.4"/, e.message assert_match /HTTP_CLIENT_IP="8.8.8.8"/, e.message + @request.env['HTTP_X_FORWARDED_FOR'] = '8.8.8.8, 9.9.9.9' + assert_equal '8.8.8.8', @request.remote_ip + @request.env.delete 'HTTP_CLIENT_IP' @request.env.delete 'HTTP_X_FORWARDED_FOR' end From 82e96eb294ae21528c3e05e91c05c7ee5222afbd Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Tue, 3 Jun 2008 19:19:08 -0500 Subject: [PATCH 029/200] Dependencies move to ActiveSupport::Dependencies missed a few spots --- railties/lib/initializer.rb | 14 +++++++------- railties/lib/rails/plugin/loader.rb | 4 ++-- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/railties/lib/initializer.rb b/railties/lib/initializer.rb index d80d014527f57..dd6a0c66f54fc 100644 --- a/railties/lib/initializer.rb +++ b/railties/lib/initializer.rb @@ -204,10 +204,10 @@ def set_load_path # Set the paths from which Rails will automatically load source files, and # the load_once paths. def set_autoload_paths - Dependencies.load_paths = configuration.load_paths.uniq - Dependencies.load_once_paths = configuration.load_once_paths.uniq + ActiveSupport::Dependencies.load_paths = configuration.load_paths.uniq + ActiveSupport::Dependencies.load_once_paths = configuration.load_once_paths.uniq - extra = Dependencies.load_once_paths - Dependencies.load_paths + extra = ActiveSupport::Dependencies.load_once_paths - ActiveSupport::Dependencies.load_paths unless extra.empty? abort <<-end_error load_once_paths must be a subset of the load_paths. @@ -234,7 +234,7 @@ def add_support_load_paths end # Adds all load paths from plugins to the global set of load paths, so that - # code from plugins can be required (explicitly or automatically via Dependencies). + # code from plugins can be required (explicitly or automatically via ActiveSupport::Dependencies). def add_plugin_load_paths plugin_loader.add_plugin_load_paths end @@ -416,7 +416,7 @@ def initialize_routing # Sets the dependency loading mechanism based on the value of # Configuration#cache_classes. def initialize_dependency_mechanism - Dependencies.mechanism = configuration.cache_classes ? :require : :load + ActiveSupport::Dependencies.mechanism = configuration.cache_classes ? :require : :load end # Loads support for "whiny nil" (noisy warnings when methods are invoked @@ -602,12 +602,12 @@ def plugins=(plugins) # If reload_plugins? is false, add this to your plugin's init.rb # to make it reloadable: # - # Dependencies.load_once_paths.delete lib_path + # ActiveSupport::Dependencies.load_once_paths.delete lib_path # # If reload_plugins? is true, add this to your plugin's init.rb # to only load it once: # - # Dependencies.load_once_paths << lib_path + # ActiveSupport::Dependencies.load_once_paths << lib_path # attr_accessor :reload_plugins diff --git a/railties/lib/rails/plugin/loader.rb b/railties/lib/rails/plugin/loader.rb index 1e542dd038b28..948d497007359 100644 --- a/railties/lib/rails/plugin/loader.rb +++ b/railties/lib/rails/plugin/loader.rb @@ -45,9 +45,9 @@ def add_plugin_load_paths plugins.each do |plugin| plugin.load_paths.each do |path| $LOAD_PATH.insert(application_lib_index + 1, path) - Dependencies.load_paths << path + ActiveSupport::Dependencies.load_paths << path unless Rails.configuration.reload_plugins? - Dependencies.load_once_paths << path + ActiveSupport::Dependencies.load_once_paths << path end end end From 30a0ebb3eb9fd4c8e4401bcaa4887eb1ce95defa Mon Sep 17 00:00:00 2001 From: Sean Huber Date: Thu, 29 May 2008 10:49:38 -0700 Subject: [PATCH 030/200] Add RJS#page.reload. [#277 state:resolved] Signed-off-by: Pratik Naik --- actionpack/CHANGELOG | 2 ++ actionpack/lib/action_view/helpers/prototype_helper.rb | 10 ++++++++++ actionpack/test/template/prototype_helper_test.rb | 5 +++++ 3 files changed, 17 insertions(+) diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG index 861597701c249..66af5fd745180 100644 --- a/actionpack/CHANGELOG +++ b/actionpack/CHANGELOG @@ -1,5 +1,7 @@ *Edge* +* Added page.reload functionality. Resolves #277. [Sean Huber] + * Fixed Request#remote_ip to only raise hell if the HTTP_CLIENT_IP and HTTP_X_FORWARDED_FOR doesn't match (not just if they're both present) [Mark Imbriaco, Bradford Folkens] * Allow caches_action to accept a layout option [José Valim] diff --git a/actionpack/lib/action_view/helpers/prototype_helper.rb b/actionpack/lib/action_view/helpers/prototype_helper.rb index 602832e47098a..5a1012954eef0 100644 --- a/actionpack/lib/action_view/helpers/prototype_helper.rb +++ b/actionpack/lib/action_view/helpers/prototype_helper.rb @@ -868,6 +868,16 @@ def redirect_to(location) record "window.location.href = #{url.inspect}" end + # Reloads the browser's current +location+ using JavaScript + # + # Examples: + # + # # Generates: window.location.reload(); + # page.reload + def reload + record 'window.location.reload()' + end + # Calls the JavaScript +function+, optionally with the given +arguments+. # # If a block is given, the block will be passed to a new JavaScriptGenerator; diff --git a/actionpack/test/template/prototype_helper_test.rb b/actionpack/test/template/prototype_helper_test.rb index 53a250f9d5cea..b63d8a368ccb2 100644 --- a/actionpack/test/template/prototype_helper_test.rb +++ b/actionpack/test/template/prototype_helper_test.rb @@ -347,6 +347,11 @@ def test_redirect_to @generator.redirect_to("http://www.example.com/welcome?a=b&c=d") end + def test_reload + assert_equal 'window.location.reload();', + @generator.reload + end + def test_delay @generator.delay(20) do @generator.hide('foo') From e66005547223d11365f249a87ab8fcc1deeb5def Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Wed, 4 Jun 2008 12:06:55 -0700 Subject: [PATCH 031/200] Give a nice message if there are duplicate migrations instead of raising a strange insert error --- .../abstract/schema_statements.rb | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb b/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb index 67d70b38860fb..55f67995d1382 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb @@ -331,15 +331,26 @@ def initialize_schema_migrations_table end def assume_migrated_upto_version(version) + version = version.to_i sm_table = quote_table_name(ActiveRecord::Migrator.schema_migrations_table_name) + migrated = select_values("SELECT version FROM #{sm_table}").map(&:to_i) versions = Dir['db/migrate/[0-9]*_*.rb'].map do |filename| filename.split('/').last.split('_').first.to_i end - execute "INSERT INTO #{sm_table} (version) VALUES ('#{version}')" unless migrated.include?(version.to_i) - (versions - migrated).select { |v| v < version.to_i }.each do |v| - execute "INSERT INTO #{sm_table} (version) VALUES ('#{v}')" + unless migrated.include?(version) + execute "INSERT INTO #{sm_table} (version) VALUES ('#{version}')" + end + + inserted = Set.new + (versions - migrated).each do |v| + if inserted.include?(v) + raise "Duplicate migration #{v}. Please renumber your migrations to resolve the conflict." + elsif v < version + execute "INSERT INTO #{sm_table} (version) VALUES ('#{v}')" + inserted << v + end end end From 1e4fae42d49256c925c43ea109a16a86961d47bd Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Wed, 4 Jun 2008 15:00:21 -0500 Subject: [PATCH 032/200] Fixed deprecated call to Dependencies in plugin loader test. --- railties/test/plugin_loader_test.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/railties/test/plugin_loader_test.rb b/railties/test/plugin_loader_test.rb index 41bd6ec7eabae..bce4446f79ed2 100644 --- a/railties/test/plugin_loader_test.rb +++ b/railties/test/plugin_loader_test.rb @@ -108,8 +108,8 @@ def test_should_add_plugin_load_paths_to_Dependencies_load_paths @loader.add_plugin_load_paths - assert Dependencies.load_paths.include?(File.join(plugin_fixture_path('default/stubby'), 'lib')) - assert Dependencies.load_paths.include?(File.join(plugin_fixture_path('default/acts/acts_as_chunky_bacon'), 'lib')) + assert ActiveSupport::Dependencies.load_paths.include?(File.join(plugin_fixture_path('default/stubby'), 'lib')) + assert ActiveSupport::Dependencies.load_paths.include?(File.join(plugin_fixture_path('default/acts/acts_as_chunky_bacon'), 'lib')) end def test_should_add_plugin_load_paths_to_Dependencies_load_once_paths @@ -117,8 +117,8 @@ def test_should_add_plugin_load_paths_to_Dependencies_load_once_paths @loader.add_plugin_load_paths - assert Dependencies.load_once_paths.include?(File.join(plugin_fixture_path('default/stubby'), 'lib')) - assert Dependencies.load_once_paths.include?(File.join(plugin_fixture_path('default/acts/acts_as_chunky_bacon'), 'lib')) + assert ActiveSupport::Dependencies.load_once_paths.include?(File.join(plugin_fixture_path('default/stubby'), 'lib')) + assert ActiveSupport::Dependencies.load_once_paths.include?(File.join(plugin_fixture_path('default/acts/acts_as_chunky_bacon'), 'lib')) end def test_should_add_all_load_paths_from_a_plugin_to_LOAD_PATH_array From 6e85f14817cddb53875e572770bf3739f82e155f Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Wed, 4 Jun 2008 15:02:51 -0500 Subject: [PATCH 033/200] Namespaced StringQuestioneer under ActiveSupport. --- .../lib/active_support/string_questioneer.rb | 16 +++++++++------- activesupport/test/string_questioneer_test.rb | 6 +++--- railties/lib/initializer.rb | 2 +- 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/activesupport/lib/active_support/string_questioneer.rb b/activesupport/lib/active_support/string_questioneer.rb index 7732f8b401585..666f3c60180d9 100644 --- a/activesupport/lib/active_support/string_questioneer.rb +++ b/activesupport/lib/active_support/string_questioneer.rb @@ -1,9 +1,11 @@ -class StringQuestioneer < String - def method_missing(method_name, *arguments) - if method_name.to_s.ends_with?("?") - self == method_name.to_s[0..-2] - else - super +module ActiveSupport + class StringQuestioneer < String + def method_missing(method_name, *arguments) + if method_name.to_s.ends_with?("?") + self == method_name.to_s[0..-2] + else + super + end end end -end \ No newline at end of file +end diff --git a/activesupport/test/string_questioneer_test.rb b/activesupport/test/string_questioneer_test.rb index ff9d2c17f936b..51a7e399e7db6 100644 --- a/activesupport/test/string_questioneer_test.rb +++ b/activesupport/test/string_questioneer_test.rb @@ -2,14 +2,14 @@ class StringQuestioneerTest < Test::Unit::TestCase def test_match - assert StringQuestioneer.new("production").production? + assert ActiveSupport::StringQuestioneer.new("production").production? end def test_miss - assert !StringQuestioneer.new("production").development? + assert !ActiveSupport::StringQuestioneer.new("production").development? end def test_missing_question_mark - assert_raises(NoMethodError) { StringQuestioneer.new("production").production } + assert_raises(NoMethodError) { ActiveSupport::StringQuestioneer.new("production").production } end end \ No newline at end of file diff --git a/railties/lib/initializer.rb b/railties/lib/initializer.rb index dd6a0c66f54fc..5c927ceddf0af 100644 --- a/railties/lib/initializer.rb +++ b/railties/lib/initializer.rb @@ -37,7 +37,7 @@ def root end def env - StringQuestioneer.new(RAILS_ENV) + ActiveSupport::StringQuestioneer.new(RAILS_ENV) end def cache From 5fe28789731fd521d5a250ac7be21da45dae147d Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Wed, 4 Jun 2008 15:06:32 -0500 Subject: [PATCH 034/200] Renamed StringQuestioneer to StringInquirer. --- activesupport/CHANGELOG | 2 +- activesupport/lib/active_support.rb | 2 +- .../{string_questioneer.rb => string_inquirer.rb} | 2 +- activesupport/test/string_inquirer_test.rb | 15 +++++++++++++++ activesupport/test/string_questioneer_test.rb | 15 --------------- railties/CHANGELOG | 2 +- railties/lib/initializer.rb | 2 +- 7 files changed, 20 insertions(+), 20 deletions(-) rename activesupport/lib/active_support/{string_questioneer.rb => string_inquirer.rb} (85%) create mode 100644 activesupport/test/string_inquirer_test.rb delete mode 100644 activesupport/test/string_questioneer_test.rb diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG index 894b43928f01a..c7739fd7e0f1b 100644 --- a/activesupport/CHANGELOG +++ b/activesupport/CHANGELOG @@ -4,7 +4,7 @@ * Namespace Inflector, Dependencies, OrderedOptions, and TimeZone under ActiveSupport [Josh Peek] -* Added StringQuestioneer for doing things like StringQuestioneer.new("production").production? # => true and StringQuestioneer.new("production").development? # => false [DHH] +* Added StringInquirer for doing things like StringInquirer.new("production").production? # => true and StringInquirer.new("production").development? # => false [DHH] * Fixed Date#end_of_quarter to not blow up on May 31st [#289 state:resolved] (Danger) diff --git a/activesupport/lib/active_support.rb b/activesupport/lib/active_support.rb index 2b418a1bb7cf7..1a8603e892eef 100644 --- a/activesupport/lib/active_support.rb +++ b/activesupport/lib/active_support.rb @@ -43,7 +43,7 @@ require 'active_support/ordered_options' require 'active_support/option_merger' -require 'active_support/string_questioneer' +require 'active_support/string_inquirer' require 'active_support/values/time_zone' require 'active_support/duration' diff --git a/activesupport/lib/active_support/string_questioneer.rb b/activesupport/lib/active_support/string_inquirer.rb similarity index 85% rename from activesupport/lib/active_support/string_questioneer.rb rename to activesupport/lib/active_support/string_inquirer.rb index 666f3c60180d9..65545748df58d 100644 --- a/activesupport/lib/active_support/string_questioneer.rb +++ b/activesupport/lib/active_support/string_inquirer.rb @@ -1,5 +1,5 @@ module ActiveSupport - class StringQuestioneer < String + class StringInquirer < String def method_missing(method_name, *arguments) if method_name.to_s.ends_with?("?") self == method_name.to_s[0..-2] diff --git a/activesupport/test/string_inquirer_test.rb b/activesupport/test/string_inquirer_test.rb new file mode 100644 index 0000000000000..dda7850e6bdbd --- /dev/null +++ b/activesupport/test/string_inquirer_test.rb @@ -0,0 +1,15 @@ +require 'abstract_unit' + +class StringInquirerTest < Test::Unit::TestCase + def test_match + assert ActiveSupport::StringInquirer.new("production").production? + end + + def test_miss + assert !ActiveSupport::StringInquirer.new("production").development? + end + + def test_missing_question_mark + assert_raises(NoMethodError) { ActiveSupport::StringInquirer.new("production").production } + end +end diff --git a/activesupport/test/string_questioneer_test.rb b/activesupport/test/string_questioneer_test.rb deleted file mode 100644 index 51a7e399e7db6..0000000000000 --- a/activesupport/test/string_questioneer_test.rb +++ /dev/null @@ -1,15 +0,0 @@ -require 'abstract_unit' - -class StringQuestioneerTest < Test::Unit::TestCase - def test_match - assert ActiveSupport::StringQuestioneer.new("production").production? - end - - def test_miss - assert !ActiveSupport::StringQuestioneer.new("production").development? - end - - def test_missing_question_mark - assert_raises(NoMethodError) { ActiveSupport::StringQuestioneer.new("production").production } - end -end \ No newline at end of file diff --git a/railties/CHANGELOG b/railties/CHANGELOG index abadeb693fd0f..39edc511da22e 100644 --- a/railties/CHANGELOG +++ b/railties/CHANGELOG @@ -1,6 +1,6 @@ *Edge* -* Wrapped Rails.env in StringQuestioneer so you can do Rails.env.development? [DHH] +* Wrapped Rails.env in StringInquirer so you can do Rails.env.development? [DHH] * Fixed that RailsInfoController wasn't considering all requests local in development mode (Edgard Castro) [#310 state:resolved] diff --git a/railties/lib/initializer.rb b/railties/lib/initializer.rb index 5c927ceddf0af..f0b5e3f257bf9 100644 --- a/railties/lib/initializer.rb +++ b/railties/lib/initializer.rb @@ -37,7 +37,7 @@ def root end def env - ActiveSupport::StringQuestioneer.new(RAILS_ENV) + ActiveSupport::StringInquirer.new(RAILS_ENV) end def cache From c4d570c2eb6b7bd0a529e20a1055754183d50c23 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Wed, 4 Jun 2008 22:32:09 -0500 Subject: [PATCH 035/200] Use CGI::Cookie::parse for request cookies until we officially deprecated CGI. --- .../lib/action_controller/rack_process.rb | 42 ++----------------- actionpack/test/controller/rack_test.rb | 8 ++-- 2 files changed, 8 insertions(+), 42 deletions(-) diff --git a/actionpack/lib/action_controller/rack_process.rb b/actionpack/lib/action_controller/rack_process.rb index d5fb78c44d3ba..37b56dabca649 100644 --- a/actionpack/lib/action_controller/rack_process.rb +++ b/actionpack/lib/action_controller/rack_process.rb @@ -49,21 +49,12 @@ def request_parameters def cookies return {} unless @env["HTTP_COOKIE"] - if @env["rack.request.cookie_string"] == @env["HTTP_COOKIE"] - @env["rack.request.cookie_hash"] - else + unless @env["rack.request.cookie_string"] == @env["HTTP_COOKIE"] @env["rack.request.cookie_string"] = @env["HTTP_COOKIE"] - # According to RFC 2109: - # If multiple cookies satisfy the criteria above, they are ordered in - # the Cookie header such that those with more specific Path attributes - # precede those with less specific. Ordering with respect to other - # attributes (e.g., Domain) is unspecified. - @env["rack.request.cookie_hash"] = - parse_query(@env["rack.request.cookie_string"], ';,').inject({}) { |h, (k,v)| - h[k] = Array === v ? v.first : v - h - } + @env["rack.request.cookie_hash"] = CGI::Cookie::parse(@env["rack.request.cookie_string"]) end + + @env["rack.request.cookie_hash"] end def host_with_port_without_standard_port_handling @@ -170,31 +161,6 @@ def stale_session_check! def session_options_with_string_keys @session_options_with_string_keys ||= DEFAULT_SESSION_OPTIONS.merge(@session_options).stringify_keys end - - # From Rack::Utils - def parse_query(qs, d = '&;') - params = {} - (qs || '').split(/[#{d}] */n).inject(params) { |h,p| - k, v = unescape(p).split('=',2) - if cur = params[k] - if cur.class == Array - params[k] << v - else - params[k] = [cur, v] - end - else - params[k] = v - end - } - - return params - end - - def unescape(s) - s.tr('+', ' ').gsub(/((?:%[0-9a-fA-F]{2})+)/n){ - [$1.delete('%')].pack('H*') - } - end end class RackResponse < AbstractResponse #:nodoc: diff --git a/actionpack/test/controller/rack_test.rb b/actionpack/test/controller/rack_test.rb index 026b0195d1e20..856f24bbdb1a8 100644 --- a/actionpack/test/controller/rack_test.rb +++ b/actionpack/test/controller/rack_test.rb @@ -33,10 +33,10 @@ def setup "REDIRECT_STATUS" => "200", "REQUEST_METHOD" => "GET" } + @request = ActionController::RackRequest.new(@env) # some Nokia phone browsers omit the space after the semicolon separator. # some developers have grown accustomed to using comma in cookie values. - @alt_cookie_fmt_request_hash = {"HTTP_COOKIE"=>"_session_id=c84ace847,96670c052c6ceb2451fb0f2;is_admin=yes"} - @request = ActionController::RackRequest.new(@env) + @alt_cookie_fmt_request = ActionController::RackRequest.new(@env.merge({"HTTP_COOKIE"=>"_session_id=c84ace847,96670c052c6ceb2451fb0f2;is_admin=yes"})) end def default_test; end @@ -100,11 +100,11 @@ def test_host_if_ipv6_reference_with_port end def test_cookie_syntax_resilience - cookies = CGI::Cookie::parse(@env["HTTP_COOKIE"]); + cookies = @request.cookies assert_equal ["c84ace84796670c052c6ceb2451fb0f2"], cookies["_session_id"], cookies.inspect assert_equal ["yes"], cookies["is_admin"], cookies.inspect - alt_cookies = CGI::Cookie::parse(@alt_cookie_fmt_request_hash["HTTP_COOKIE"]); + alt_cookies = @alt_cookie_fmt_request.cookies assert_equal ["c84ace847,96670c052c6ceb2451fb0f2"], alt_cookies["_session_id"], alt_cookies.inspect assert_equal ["yes"], alt_cookies["is_admin"], alt_cookies.inspect end From ed0cb91a830f317e3a8192a90294c1005f6156c2 Mon Sep 17 00:00:00 2001 From: Ryan Kinderman Date: Mon, 26 May 2008 22:15:57 -0500 Subject: [PATCH 036/200] Ensure plugins' rake tasks are loaded before application's rake tasks. [#259 state:resolved] Signed-off-by: Pratik Naik --- railties/lib/tasks/rails.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/railties/lib/tasks/rails.rb b/railties/lib/tasks/rails.rb index bfcf5bc4935e2..8c2b7f9bdedb7 100644 --- a/railties/lib/tasks/rails.rb +++ b/railties/lib/tasks/rails.rb @@ -4,5 +4,5 @@ Dir["#{File.dirname(__FILE__)}/*.rake"].each { |ext| load ext } # Load any custom rakefile extensions -Dir["#{RAILS_ROOT}/lib/tasks/**/*.rake"].sort.each { |ext| load ext } Dir["#{RAILS_ROOT}/vendor/plugins/*/**/tasks/**/*.rake"].sort.each { |ext| load ext } +Dir["#{RAILS_ROOT}/lib/tasks/**/*.rake"].sort.each { |ext| load ext } From df8154c845f8fb251c58f1fd882cc221cfdcbbc2 Mon Sep 17 00:00:00 2001 From: Pratik Naik Date: Thu, 5 Jun 2008 20:41:22 +0100 Subject: [PATCH 037/200] Fix that Rails::InfoController tests --- railties/test/rails_info_controller_test.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/railties/test/rails_info_controller_test.rb b/railties/test/rails_info_controller_test.rb index 17c7d9deea568..e1872ebf33afb 100644 --- a/railties/test/rails_info_controller_test.rb +++ b/railties/test/rails_info_controller_test.rb @@ -30,6 +30,8 @@ def setup @controller = Rails::InfoController.new @request = ActionController::TestRequest.new @response = ActionController::TestResponse.new + + ActionController::Base.consider_all_requests_local = true end def test_rails_info_properties_table_rendered_for_local_request @@ -41,6 +43,8 @@ def test_rails_info_properties_table_rendered_for_local_request def test_rails_info_properties_error_rendered_for_non_local_request Rails::InfoController.local_request = false + ActionController::Base.consider_all_requests_local = false + get :properties assert_tag :tag => 'p' assert_response 500 From 2e0765a00361781fb9bff2a7ca8996eab1f01bd4 Mon Sep 17 00:00:00 2001 From: Frederick Cheung Date: Thu, 5 Jun 2008 20:48:42 +0100 Subject: [PATCH 038/200] Make partial counter start from 0. Signed-off-by: Pratik Naik --- actionpack/lib/action_view/partial_template.rb | 2 +- actionpack/test/controller/new_render_test.rb | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/actionpack/lib/action_view/partial_template.rb b/actionpack/lib/action_view/partial_template.rb index 1fb3aaee02eba..0b374db888db6 100644 --- a/actionpack/lib/action_view/partial_template.rb +++ b/actionpack/lib/action_view/partial_template.rb @@ -22,10 +22,10 @@ def render end def render_member(object) - @locals[@counter_name] += 1 @locals[:object] = @locals[@variable_name] = object template = render_template + @locals[@counter_name] += 1 @locals.delete(@variable_name) @locals.delete(:object) diff --git a/actionpack/test/controller/new_render_test.rb b/actionpack/test/controller/new_render_test.rb index 6e2c6d90c6a21..3b439a3b185ac 100644 --- a/actionpack/test/controller/new_render_test.rb +++ b/actionpack/test/controller/new_render_test.rb @@ -742,7 +742,7 @@ def test_partial_collection def test_partial_collection_with_counter get :partial_collection_with_counter - assert_equal "david1mary2", @response.body + assert_equal "david0mary1", @response.body end def test_partial_collection_with_locals @@ -762,7 +762,7 @@ def test_partial_collection_shorthand_with_locals def test_partial_collection_shorthand_with_different_types_of_records get :partial_collection_shorthand_with_different_types_of_records - assert_equal "Bonjour bad customer: mark1Bonjour good customer: craig2Bonjour bad customer: john3Bonjour good customer: zach4Bonjour good customer: brandon5Bonjour bad customer: dan6", @response.body + assert_equal "Bonjour bad customer: mark0Bonjour good customer: craig1Bonjour bad customer: john2Bonjour good customer: zach3Bonjour good customer: brandon4Bonjour bad customer: dan5", @response.body end def test_empty_partial_collection From 1dbfe9766e00282c56523f6969550494bbffbbf4 Mon Sep 17 00:00:00 2001 From: Pratik Naik Date: Thu, 5 Jun 2008 23:27:27 +0100 Subject: [PATCH 039/200] Ensure render :file works inside templates --- actionmailer/lib/action_mailer/base.rb | 3 ++- actionpack/lib/action_view/base.rb | 3 ++- actionpack/test/controller/new_render_test.rb | 10 ++++++++++ .../fixtures/test/render_file_from_template.html.erb | 1 + 4 files changed, 15 insertions(+), 2 deletions(-) create mode 100644 actionpack/test/fixtures/test/render_file_from_template.html.erb diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb index e065132107482..51fc6032cc5dc 100644 --- a/actionmailer/lib/action_mailer/base.rb +++ b/actionmailer/lib/action_mailer/base.rb @@ -530,7 +530,7 @@ def initialize_defaults(method_name) end def render_message(method_name, body) - render :file => method_name, :body => body + render :file => method_name, :body => body, :use_full_path => true end def render(opts) @@ -538,6 +538,7 @@ def render(opts) if opts[:file] && opts[:file] !~ /\// opts[:file] = "#{mailer_name}/#{opts[:file]}" end + opts[:use_full_path] = true initialize_template_class(body).render(opts) end diff --git a/actionpack/lib/action_view/base.rb b/actionpack/lib/action_view/base.rb index f398756550da4..b2fcbfa55437f 100644 --- a/actionpack/lib/action_view/base.rb +++ b/actionpack/lib/action_view/base.rb @@ -253,6 +253,7 @@ def render(options = {}, local_assigns = {}, &block) #:nodoc: elsif options == :update update_page(&block) elsif options.is_a?(Hash) + use_full_path = options[:use_full_path] options = options.reverse_merge(:locals => {}, :use_full_path => true) if partial_layout = options.delete(:layout) @@ -266,7 +267,7 @@ def render(options = {}, local_assigns = {}, &block) #:nodoc: end end elsif options[:file] - render_file(options[:file], options[:use_full_path], options[:locals]) + render_file(options[:file], use_full_path || false, options[:locals]) elsif options[:partial] && options[:collection] render_partial_collection(options[:partial], options[:collection], options[:spacer_template], options[:locals]) elsif options[:partial] diff --git a/actionpack/test/controller/new_render_test.rb b/actionpack/test/controller/new_render_test.rb index 3b439a3b185ac..6b83b8337eb9b 100644 --- a/actionpack/test/controller/new_render_test.rb +++ b/actionpack/test/controller/new_render_test.rb @@ -68,6 +68,11 @@ def render_file_with_instance_variables path = File.join(File.dirname(__FILE__), '../fixtures/test/render_file_with_ivar.erb') render :file => path end + + def render_file_from_template + @secret = 'in the sauce' + @path = File.expand_path(File.join(File.dirname(__FILE__), '../fixtures/test/render_file_with_ivar.erb')) + end def render_file_with_locals path = File.join(File.dirname(__FILE__), '../fixtures/test/render_file_with_locals.erb') @@ -531,6 +536,11 @@ def test_render_file_with_locals get :render_file_with_locals assert_equal "The secret is in the sauce\n", @response.body end + + def test_render_file_from_template + get :render_file_from_template + assert_equal "The secret is in the sauce\n", @response.body + end def test_attempt_to_access_object_method assert_raises(ActionController::UnknownAction, "No action responded to [clone]") { get :clone } diff --git a/actionpack/test/fixtures/test/render_file_from_template.html.erb b/actionpack/test/fixtures/test/render_file_from_template.html.erb new file mode 100644 index 0000000000000..fde9f4bb646cb --- /dev/null +++ b/actionpack/test/fixtures/test/render_file_from_template.html.erb @@ -0,0 +1 @@ +<%= render :file => @path %> \ No newline at end of file From 0ccd0b569ad4df7acc37a14531606ba6f74c69f4 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Fri, 6 Jun 2008 03:02:22 -0700 Subject: [PATCH 040/200] Fix doc typo. Move extend self so it's more immediately obvious. Require inflections from load path. --- activesupport/lib/active_support/inflector.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/activesupport/lib/active_support/inflector.rb b/activesupport/lib/active_support/inflector.rb index fc88d80cdbcf5..9d549eaae1c60 100644 --- a/activesupport/lib/active_support/inflector.rb +++ b/activesupport/lib/active_support/inflector.rb @@ -10,6 +10,8 @@ module ActiveSupport # If you discover an incorrect inflection and require it for your application, you'll need # to correct it yourself (explained below). module Inflector + extend self + # A singleton instance of this class is yielded by Inflector.inflections, which can then be used to specify additional # inflection rules. Examples: # @@ -91,8 +93,6 @@ def clear(scope = :all) end end - extend self - # Yields a singleton instance of Inflector::Inflections so you can specify additional # inflector rules. # @@ -134,7 +134,7 @@ def pluralize(word) # "posts".singularize # => "post" # "octopi".singularize # => "octopus" # "sheep".singluarize # => "sheep" - # "word".singluarize # => "word" + # "word".singularize # => "word" # "the blue mailmen".singularize # => "the blue mailman" # "CamelOctopi".singularize # => "CamelOctopus" def singularize(word) @@ -307,4 +307,4 @@ def ordinalize(number) end end -require File.dirname(__FILE__) + '/inflections' +require 'active_support/inflections' From c1a98209da7422965f5dd4f475603b8a3cc887e4 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Fri, 6 Jun 2008 03:03:30 -0700 Subject: [PATCH 041/200] Cache RecordIdentifier methods in Class#model_name wrapper --- .../action_controller/record_identifier.rb | 45 ++++++++++++++----- 1 file changed, 34 insertions(+), 11 deletions(-) diff --git a/actionpack/lib/action_controller/record_identifier.rb b/actionpack/lib/action_controller/record_identifier.rb index 643ff7e5f4350..77fe5cbe2a8c6 100644 --- a/actionpack/lib/action_controller/record_identifier.rb +++ b/actionpack/lib/action_controller/record_identifier.rb @@ -1,3 +1,19 @@ +class Class + def model_name + @model_name ||= ModelName.new(name) + end + + class ModelName + attr_reader :singular, :plural, :path + + def initialize(name) + @singular = name.underscore.tr('/', '_').freeze + @plural = @singular.pluralize.freeze + @path = "#{name.tableize}/#{name.demodulize.underscore}".freeze + end + end +end + module ActionController # The record identifier encapsulates a number of naming conventions for dealing with records, like Active Records or # Active Resources or pretty much any other model type that has an id. These patterns are then used to try elevate @@ -31,18 +47,21 @@ module ActionController module RecordIdentifier extend self + JOIN = '_'.freeze + NEW = 'new'.freeze + # Returns plural/singular for a record or class. Example: # # partial_path(post) # => "posts/post" # partial_path(Person) # => "people/person" # partial_path(Person, "admin/games") # => "admin/people/person" def partial_path(record_or_class, controller_path = nil) - klass = class_from_record_or_class(record_or_class) + name = model_name_from_record_or_class(record_or_class) if controller_path && controller_path.include?("/") - "#{File.dirname(controller_path)}/#{klass.name.tableize}/#{klass.name.demodulize.underscore}" + "#{File.dirname(controller_path)}/#{name.path}" else - "#{klass.name.tableize}/#{klass.name.demodulize.underscore}" + name.path end end @@ -56,7 +75,8 @@ def partial_path(record_or_class, controller_path = nil) # dom_class(post, :edit) # => "edit_post" # dom_class(Person, :edit) # => "edit_person" def dom_class(record_or_class, prefix = nil) - [ prefix, singular_class_name(record_or_class) ].compact * '_' + singular = singular_class_name(record_or_class) + prefix ? "#{prefix}#{JOIN}#{singular}" : singular end # The DOM id convention is to use the singular form of an object or class with the id following an underscore. @@ -69,8 +89,11 @@ def dom_class(record_or_class, prefix = nil) # # dom_id(Post.new(:id => 45), :edit) # => "edit_post_45" def dom_id(record, prefix = nil) - prefix ||= 'new' unless record.id - [ prefix, singular_class_name(record), record.id ].compact * '_' + if record_id = record.id + "#{dom_class(record, prefix)}#{JOIN}#{record_id}" + else + dom_class(record, prefix || NEW) + end end # Returns the plural class name of a record or class. Examples: @@ -78,7 +101,7 @@ def dom_id(record, prefix = nil) # plural_class_name(post) # => "posts" # plural_class_name(Highrise::Person) # => "highrise_people" def plural_class_name(record_or_class) - singular_class_name(record_or_class).pluralize + model_name_from_record_or_class(record_or_class).plural end # Returns the singular class name of a record or class. Examples: @@ -86,12 +109,12 @@ def plural_class_name(record_or_class) # singular_class_name(post) # => "post" # singular_class_name(Highrise::Person) # => "highrise_person" def singular_class_name(record_or_class) - class_from_record_or_class(record_or_class).name.underscore.tr('/', '_') + model_name_from_record_or_class(record_or_class).singular end private - def class_from_record_or_class(record_or_class) - record_or_class.is_a?(Class) ? record_or_class : record_or_class.class + def model_name_from_record_or_class(record_or_class) + (record_or_class.is_a?(Class) ? record_or_class : record_or_class.class).model_name end end -end \ No newline at end of file +end From 566d717d783f56563cd602198be2177c972c9a81 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Fri, 6 Jun 2008 03:38:05 -0700 Subject: [PATCH 042/200] Move Class::ModelName to Active Support module core_ext --- .../action_controller/record_identifier.rb | 20 ++--------------- .../lib/active_support/core_ext/module.rb | 5 +++++ .../core_ext/module/model_naming.rb | 22 +++++++++++++++++++ .../test/core_ext/module/model_naming_test.rb | 19 ++++++++++++++++ 4 files changed, 48 insertions(+), 18 deletions(-) create mode 100644 activesupport/lib/active_support/core_ext/module/model_naming.rb create mode 100644 activesupport/test/core_ext/module/model_naming_test.rb diff --git a/actionpack/lib/action_controller/record_identifier.rb b/actionpack/lib/action_controller/record_identifier.rb index 77fe5cbe2a8c6..f69c3d6163e7f 100644 --- a/actionpack/lib/action_controller/record_identifier.rb +++ b/actionpack/lib/action_controller/record_identifier.rb @@ -1,19 +1,3 @@ -class Class - def model_name - @model_name ||= ModelName.new(name) - end - - class ModelName - attr_reader :singular, :plural, :path - - def initialize(name) - @singular = name.underscore.tr('/', '_').freeze - @plural = @singular.pluralize.freeze - @path = "#{name.tableize}/#{name.demodulize.underscore}".freeze - end - end -end - module ActionController # The record identifier encapsulates a number of naming conventions for dealing with records, like Active Records or # Active Resources or pretty much any other model type that has an id. These patterns are then used to try elevate @@ -59,9 +43,9 @@ def partial_path(record_or_class, controller_path = nil) name = model_name_from_record_or_class(record_or_class) if controller_path && controller_path.include?("/") - "#{File.dirname(controller_path)}/#{name.path}" + "#{File.dirname(controller_path)}/#{name.partial_path}" else - name.path + name.partial_path end end diff --git a/activesupport/lib/active_support/core_ext/module.rb b/activesupport/lib/active_support/core_ext/module.rb index da8d5b376291c..34fcbd124bb54 100644 --- a/activesupport/lib/active_support/core_ext/module.rb +++ b/activesupport/lib/active_support/core_ext/module.rb @@ -6,3 +6,8 @@ require 'active_support/core_ext/module/introspection' require 'active_support/core_ext/module/loading' require 'active_support/core_ext/module/aliasing' +require 'active_support/core_ext/module/model_naming' + +class Module + include ActiveSupport::CoreExt::Module::ModelNaming +end diff --git a/activesupport/lib/active_support/core_ext/module/model_naming.rb b/activesupport/lib/active_support/core_ext/module/model_naming.rb new file mode 100644 index 0000000000000..26e76ab556755 --- /dev/null +++ b/activesupport/lib/active_support/core_ext/module/model_naming.rb @@ -0,0 +1,22 @@ +module ActiveSupport + class ModelName < String + attr_reader :singular, :plural, :partial_path + + def initialize(name) + super + @singular = underscore.tr('/', '_').freeze + @plural = @singular.pluralize.freeze + @partial_path = "#{tableize}/#{demodulize.underscore}".freeze + end + end + + module CoreExt + module Module + module ModelNaming + def model_name + @model_name ||= ModelName.new(name) + end + end + end + end +end diff --git a/activesupport/test/core_ext/module/model_naming_test.rb b/activesupport/test/core_ext/module/model_naming_test.rb new file mode 100644 index 0000000000000..fc73fa5c36ddc --- /dev/null +++ b/activesupport/test/core_ext/module/model_naming_test.rb @@ -0,0 +1,19 @@ +require 'abstract_unit' + +class ModelNamingTest < Test::Unit::TestCase + def setup + @name = ActiveSupport::ModelName.new('Post::TrackBack') + end + + def test_singular + assert_equal 'post_track_back', @name.singular + end + + def test_plural + assert_equal 'post_track_backs', @name.plural + end + + def test_partial_path + assert_equal 'post/track_backs/track_back', @name.partial_path + end +end From fd40fbc1983a96587cc25729985191573a03e04c Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Fri, 6 Jun 2008 03:54:16 -0700 Subject: [PATCH 043/200] Generate less garbage when expanding range bind variables in conditions --- activerecord/lib/active_record/base.rb | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 8dd07eb47884b..110902b590591 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -2064,13 +2064,18 @@ def replace_named_bind_variables(statement, bind_vars) #:nodoc: end def expand_range_bind_variables(bind_vars) #:nodoc: - bind_vars.sum do |var| + expanded = [] + + bind_vars.each do |var| if var.is_a?(Range) - [var.first, var.last] + expanded << var.first + expanded << var.last else - [var] + expanded << var end end + + expanded end def quote_bound_value(value) #:nodoc: From e2c49e6a59f1b969a526586a3dd4dfd9c6f12b10 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Fri, 6 Jun 2008 04:13:09 -0700 Subject: [PATCH 044/200] Drop a string conversion from the often-called tag_options helper --- actionpack/lib/action_view/helpers/tag_helper.rb | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/actionpack/lib/action_view/helpers/tag_helper.rb b/actionpack/lib/action_view/helpers/tag_helper.rb index 999cbfb52a286..ba43b5e8efec2 100644 --- a/actionpack/lib/action_view/helpers/tag_helper.rb +++ b/actionpack/lib/action_view/helpers/tag_helper.rb @@ -1,5 +1,6 @@ require 'cgi' require 'erb' +require 'set' module ActionView module Helpers #:nodoc: @@ -8,7 +9,8 @@ module Helpers #:nodoc: module TagHelper include ERB::Util - BOOLEAN_ATTRIBUTES = Set.new(%w(disabled readonly multiple)) + BOOLEAN_ATTRIBUTES = %w(disabled readonly multiple).to_set + BOOLEAN_ATTRIBUTES.merge(BOOLEAN_ATTRIBUTES.map(&:to_sym)) # Returns an empty HTML tag of type +name+ which by default is XHTML # compliant. Set +open+ to true to create an open tag compatible @@ -37,7 +39,7 @@ module TagHelper # tag("img", { :src => "open & shut.png" }, false, false) # # => def tag(name, options = nil, open = false, escape = true) - "<#{name}#{tag_options(options, escape) if options}" + (open ? ">" : " />") + "<#{name}#{tag_options(options, escape) if options}#{open ? ">" : " />"}" end # Returns an HTML block tag of type +name+ surrounding the +content+. Add @@ -114,7 +116,6 @@ def tag_options(options, escape = true) if escape options.each do |key, value| next unless value - key = key.to_s value = BOOLEAN_ATTRIBUTES.include?(key) ? key : escape_once(value) attrs << %(#{key}="#{value}") end From e79d47847a75d63a66a3ed2296271fd28d41f24c Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Fri, 6 Jun 2008 13:03:59 -0700 Subject: [PATCH 045/200] Qualify Inflector in rdoc examples also. [#356 state:resolved] --- activesupport/lib/active_support/inflector.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/activesupport/lib/active_support/inflector.rb b/activesupport/lib/active_support/inflector.rb index 9d549eaae1c60..47bd6e1767caf 100644 --- a/activesupport/lib/active_support/inflector.rb +++ b/activesupport/lib/active_support/inflector.rb @@ -15,7 +15,7 @@ module Inflector # A singleton instance of this class is yielded by Inflector.inflections, which can then be used to specify additional # inflection rules. Examples: # - # Inflector.inflections do |inflect| + # ActiveSupport::Inflector.inflections do |inflect| # inflect.plural /^(ox)$/i, '\1\2en' # inflect.singular /^(ox)en/i, '\1' # @@ -97,7 +97,7 @@ def clear(scope = :all) # inflector rules. # # Example: - # Inflector.inflections do |inflect| + # ActiveSupport::Inflector.inflections do |inflect| # inflect.uncountable "rails" # end def inflections From 89ea7bee3609e513b21151eb4a7991b074fc0e8f Mon Sep 17 00:00:00 2001 From: Pratik Naik Date: Sat, 7 Jun 2008 01:25:27 +0100 Subject: [PATCH 046/200] Simplify ActiveRecord::Base#update_attribute --- activerecord/lib/active_record/base.rb | 8 ++++---- activerecord/lib/active_record/validations.rb | 9 --------- 2 files changed, 4 insertions(+), 13 deletions(-) diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 110902b590591..450ea5cb33664 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -2252,12 +2252,12 @@ def becomes(klass) end end - # Updates a single attribute and saves the record. This is especially useful for boolean flags on existing records. - # Note: This method is overwritten by the Validation module that'll make sure that updates made with this method - # aren't subjected to validation checks. Hence, attributes can be updated even if the full object isn't valid. + # Updates a single attribute and saves the record without going through the normal validation procedure. + # This is especially useful for boolean flags on existing records. The regular +update_attribute+ method + # in Base is replaced with this when the validations module is mixed in, which it is by default. def update_attribute(name, value) send(name.to_s + '=', value) - save + save(false) end # Updates all the attributes from the passed-in Hash and saves the record. If the object is invalid, the saving will diff --git a/activerecord/lib/active_record/validations.rb b/activerecord/lib/active_record/validations.rb index c97aafb1261e1..c4e370d01709d 100755 --- a/activerecord/lib/active_record/validations.rb +++ b/activerecord/lib/active_record/validations.rb @@ -277,7 +277,6 @@ def self.included(base) # :nodoc: base.class_eval do alias_method_chain :save, :validation alias_method_chain :save!, :validation - alias_method_chain :update_attribute, :validation_skipping end base.send :include, ActiveSupport::Callbacks @@ -914,14 +913,6 @@ def save_with_validation! end end - # Updates a single attribute and saves the record without going through the normal validation procedure. - # This is especially useful for boolean flags on existing records. The regular +update_attribute+ method - # in Base is replaced with this when the validations module is mixed in, which it is by default. - def update_attribute_with_validation_skipping(name, value) - send(name.to_s + '=', value) - save(false) - end - # Runs +validate+ and +validate_on_create+ or +validate_on_update+ and returns true if no errors were added otherwise false. def valid? errors.clear From e732a405ab7d0d04f8a254764970c9dcda988f01 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Fri, 6 Jun 2008 17:59:41 -0700 Subject: [PATCH 047/200] javascript_tag should only concat when block_given? --- .../action_view/helpers/javascript_helper.rb | 17 ++++++++++------- .../test/template/javascript_helper_test.rb | 4 ++++ 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/actionpack/lib/action_view/helpers/javascript_helper.rb b/actionpack/lib/action_view/helpers/javascript_helper.rb index a2f257763649e..85b205c264cf4 100644 --- a/actionpack/lib/action_view/helpers/javascript_helper.rb +++ b/actionpack/lib/action_view/helpers/javascript_helper.rb @@ -172,14 +172,17 @@ def escape_javascript(javascript) # alert('All is good') # <% end -%> def javascript_tag(content_or_options_with_block = nil, html_options = {}, &block) - if block_given? - html_options = content_or_options_with_block if content_or_options_with_block.is_a?(Hash) - content = capture(&block) - else - content = content_or_options_with_block - end + content = + if block_given? + html_options = content_or_options_with_block if content_or_options_with_block.is_a?(Hash) + capture(&block) + else + content_or_options_with_block + end + + tag = content_tag("script", javascript_cdata_section(content), html_options.merge(:type => Mime::JS)) - concat(content_tag("script", javascript_cdata_section(content), html_options.merge(:type => Mime::JS))) + block_given? ? concat(tag) : tag end def javascript_cdata_section(content) #:nodoc: diff --git a/actionpack/test/template/javascript_helper_test.rb b/actionpack/test/template/javascript_helper_test.rb index f136f56777feb..4924074c3e4e1 100644 --- a/actionpack/test/template/javascript_helper_test.rb +++ b/actionpack/test/template/javascript_helper_test.rb @@ -82,8 +82,12 @@ def test_button_to_function_without_function end def test_javascript_tag + @output_buffer = 'foo' + assert_dom_equal "", javascript_tag("alert('hello')") + + assert_equal 'foo', @output_buffer, 'javascript_tag without a block should not concat to @output_buffer' end def test_javascript_tag_with_options From 26ec1be24a820327d00e22fb65764a3dc06977e2 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Fri, 6 Jun 2008 18:00:01 -0700 Subject: [PATCH 048/200] concat should ignore nil --- actionpack/lib/action_view/helpers/text_helper.rb | 2 +- actionpack/test/template/text_helper_test.rb | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/actionpack/lib/action_view/helpers/text_helper.rb b/actionpack/lib/action_view/helpers/text_helper.rb index 3be4843386664..f81f7eded6f8a 100644 --- a/actionpack/lib/action_view/helpers/text_helper.rb +++ b/actionpack/lib/action_view/helpers/text_helper.rb @@ -26,7 +26,7 @@ module TextHelper # # will either display "Logged in!" or a login link # %> def concat(string) - if @output_buffer + if @output_buffer && string @output_buffer << string else string diff --git a/actionpack/test/template/text_helper_test.rb b/actionpack/test/template/text_helper_test.rb index 62cdca03d176f..0f5c62acad2e2 100644 --- a/actionpack/test/template/text_helper_test.rb +++ b/actionpack/test/template/text_helper_test.rb @@ -11,6 +11,14 @@ def setup @_cycles = nil if (defined? @_cycles) end + def test_concat + @output_buffer = 'foo' + concat 'bar' + assert_equal 'foobar', @output_buffer + assert_nothing_raised { concat nil } + assert_equal 'foobar', @output_buffer + end + def test_simple_format assert_equal "

      ", simple_format(nil) From fe9d2ad6e84626d34f1850ef4668a87787d5c6b0 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Fri, 6 Jun 2008 18:01:14 -0700 Subject: [PATCH 049/200] Remove some internal dead code that supported content_for --- .../lib/action_view/helpers/capture_helper.rb | 20 +++++-------------- actionpack/test/controller/capture_test.rb | 20 +------------------ .../test/fixtures/test/block_content_for.erb | 2 -- .../test/fixtures/test/erb_content_for.erb | 2 -- 4 files changed, 6 insertions(+), 38 deletions(-) delete mode 100644 actionpack/test/fixtures/test/block_content_for.erb delete mode 100644 actionpack/test/fixtures/test/erb_content_for.erb diff --git a/actionpack/lib/action_view/helpers/capture_helper.rb b/actionpack/lib/action_view/helpers/capture_helper.rb index 837305d96c08e..25e62f78fb831 100644 --- a/actionpack/lib/action_view/helpers/capture_helper.rb +++ b/actionpack/lib/action_view/helpers/capture_helper.rb @@ -32,7 +32,7 @@ module CaptureHelper # def capture(*args, &block) if @output_buffer - with_temporary_output_buffer { yield *args } + with_output_buffer { block.call(*args) } else block.call(*args) end @@ -115,27 +115,17 @@ def capture(*args, &block) # <%= yield :footer %>. def content_for(name, content = nil, &block) ivar = "@content_for_#{name}" - instance_variable_set("@content_for_#{name}", "#{instance_variable_get(ivar)}#{block_given? ? capture(&block) : content}") + content = capture(&block) if block_given? + instance_variable_set(ivar, "#{instance_variable_get(ivar)}#{content}") end private - def with_temporary_output_buffer - @output_buffer, old_buffer = '', @output_buffer + def with_output_buffer(buf = '') + @output_buffer, old_buffer = buf, @output_buffer yield - @output_buffer ensure @output_buffer = old_buffer end - - def erb_content_for(name, &block) - ivar = "@content_for_#{name}" - instance_variable_set(ivar, "#{instance_variable_get(ivar)}#{capture(&block)}") - end - - def block_content_for(name) - ivar = "@content_for_#{name}" - instance_variable_set(ivar, "#{instance_variable_get(ivar)}#{yield}") - end end end end diff --git a/actionpack/test/controller/capture_test.rb b/actionpack/test/controller/capture_test.rb index aaafea3920de4..2604844b84b18 100644 --- a/actionpack/test/controller/capture_test.rb +++ b/actionpack/test/controller/capture_test.rb @@ -11,16 +11,8 @@ def content_for def content_for_with_parameter render :layout => "talk_from_action" end - - def content_for_concatenated - render :layout => "talk_from_action" - end - def erb_content_for - render :layout => "talk_from_action" - end - - def block_content_for + def content_for_concatenated render :layout => "talk_from_action" end @@ -62,21 +54,11 @@ def test_should_concatentate_content_for assert_equal expected_content_for_output, @response.body end - def test_erb_content_for - get :erb_content_for - assert_equal expected_content_for_output, @response.body - end - def test_should_set_content_for_with_parameter get :content_for_with_parameter assert_equal expected_content_for_output, @response.body end - def test_block_content_for - get :block_content_for - assert_equal expected_content_for_output, @response.body - end - def test_non_erb_block_content_for get :non_erb_block_content_for assert_equal expected_content_for_output, @response.body diff --git a/actionpack/test/fixtures/test/block_content_for.erb b/actionpack/test/fixtures/test/block_content_for.erb deleted file mode 100644 index 9510337365bc0..0000000000000 --- a/actionpack/test/fixtures/test/block_content_for.erb +++ /dev/null @@ -1,2 +0,0 @@ -<% block_content_for :title do 'Putting stuff in the title!' end %> -Great stuff! \ No newline at end of file diff --git a/actionpack/test/fixtures/test/erb_content_for.erb b/actionpack/test/fixtures/test/erb_content_for.erb deleted file mode 100644 index c3bdd13643575..0000000000000 --- a/actionpack/test/fixtures/test/erb_content_for.erb +++ /dev/null @@ -1,2 +0,0 @@ -<% erb_content_for :title do %>Putting stuff in the title!<% end %> -Great stuff! \ No newline at end of file From 1d1ea92f405ab6f47942b21233da04aa21498cb7 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Fri, 6 Jun 2008 20:41:22 -0700 Subject: [PATCH 050/200] GemDependency#specification should be public --- railties/lib/rails/gem_dependency.rb | 31 ++++++++++++++-------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/railties/lib/rails/gem_dependency.rb b/railties/lib/rails/gem_dependency.rb index 30bdf416fca15..4cac7f725a231 100644 --- a/railties/lib/rails/gem_dependency.rb +++ b/railties/lib/rails/gem_dependency.rb @@ -98,27 +98,26 @@ def ==(other) self.name == other.name && self.requirement == other.requirement end -private ################################################################### - def specification @spec ||= Gem.source_index.search(Gem::Dependency.new(@name, @requirement)).sort_by { |s| s.version }.last end - def gem_command - RUBY_PLATFORM =~ /win32/ ? 'gem.bat' : 'gem' - end + private + def gem_command + RUBY_PLATFORM =~ /win32/ ? 'gem.bat' : 'gem' + end - def install_command - cmd = %w(install) << @name - cmd << "--version" << %("#{@requirement.to_s}") if @requirement - cmd << "--source" << @source if @source - cmd - end + def install_command + cmd = %w(install) << @name + cmd << "--version" << %("#{@requirement.to_s}") if @requirement + cmd << "--source" << @source if @source + cmd + end - def unpack_command - cmd = %w(unpack) << @name - cmd << "--version" << %("#{@requirement.to_s}") if @requirement - cmd - end + def unpack_command + cmd = %w(unpack) << @name + cmd << "--version" << %("#{@requirement.to_s}") if @requirement + cmd + end end end From 84fb971c2f3f26452fbc73467c13d039fe4c2024 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Fri, 6 Jun 2008 21:34:36 -0700 Subject: [PATCH 051/200] Remove 1.9's String#chars also --- .../active_support/core_ext/string/unicode.rb | 24 +++++++++---------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/activesupport/lib/active_support/core_ext/string/unicode.rb b/activesupport/lib/active_support/core_ext/string/unicode.rb index 5e20534d1d5c7..666f7bcb65490 100644 --- a/activesupport/lib/active_support/core_ext/string/unicode.rb +++ b/activesupport/lib/active_support/core_ext/string/unicode.rb @@ -1,16 +1,16 @@ module ActiveSupport #:nodoc: module CoreExtensions #:nodoc: module String #:nodoc: - unless '1.9'.respond_to?(:force_encoding) - # Define methods for handling unicode data. - module Unicode - def self.append_features(base) - if '1.8.7'.respond_to?(:chars) - base.class_eval { remove_method :chars } - end - super + # Define methods for handling unicode data. + module Unicode + def self.append_features(base) + if '1.8.7 and later'.respond_to?(:chars) + base.class_eval { remove_method :chars } end + super + end + unless '1.9'.respond_to?(:force_encoding) # +chars+ is a Unicode safe proxy for string methods. It creates and returns an instance of the # ActiveSupport::Multibyte::Chars class which encapsulates the original string. A Unicode safe version of all # the String methods are defined on this proxy class. Undefined methods are forwarded to String, so all of the @@ -44,14 +44,12 @@ def chars def is_utf8? ActiveSupport::Multibyte::Handlers::UTF8Handler.consumes?(self) end - end - else - module Unicode #:nodoc: - def chars + else + def chars #:nodoc: self end - def is_utf8? + def is_utf8? #:nodoc: case encoding when Encoding::UTF_8 valid_encoding? From 5eb893f72d189a7ad82c9a6f45873e2317f202d5 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Fri, 6 Jun 2008 22:02:23 -0700 Subject: [PATCH 052/200] Don't use deprecated String#each --- actionpack/lib/action_controller/rack_process.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/actionpack/lib/action_controller/rack_process.rb b/actionpack/lib/action_controller/rack_process.rb index 37b56dabca649..9b4aa9b7cfe0a 100644 --- a/actionpack/lib/action_controller/rack_process.rb +++ b/actionpack/lib/action_controller/rack_process.rb @@ -189,6 +189,8 @@ def each(&callback) if @body.respond_to?(:call) @writer = lambda { |x| callback.call(x) } @body.call(self, self) + elsif @body.is_a?(String) + @body.each_line(&callback) else @body.each(&callback) end From 5b53a0695904afb6a576fe5a9badbf14261909b5 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Fri, 6 Jun 2008 22:02:48 -0700 Subject: [PATCH 053/200] Ensure we have an array to collect --- actionpack/lib/action_controller/routing/segments.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/actionpack/lib/action_controller/routing/segments.rb b/actionpack/lib/action_controller/routing/segments.rb index 864e06800428d..f0ad066badf60 100644 --- a/actionpack/lib/action_controller/routing/segments.rb +++ b/actionpack/lib/action_controller/routing/segments.rb @@ -249,7 +249,7 @@ def interpolation_chunk(value_code = "#{local_name}") end def extract_value - "#{local_name} = hash[:#{key}] && hash[:#{key}].collect { |path_component| URI.escape(path_component.to_param, ActionController::Routing::Segment::UNSAFE_PCHAR) }.to_param #{"|| #{default.inspect}" if default}" + "#{local_name} = hash[:#{key}] && Array(hash[:#{key}]).collect { |path_component| URI.escape(path_component.to_param, ActionController::Routing::Segment::UNSAFE_PCHAR) }.to_param #{"|| #{default.inspect}" if default}" end def default From 21bb0f40b075e7b878e7ae0fd6c40c9a9ad5b00a Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Sat, 7 Jun 2008 13:39:03 -0700 Subject: [PATCH 054/200] PostgreSQL: quote bare table names --- .../active_record/connection_adapters/postgresql_adapter.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb index 7dbfbb41f6680..049e6f61def2e 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb @@ -506,7 +506,7 @@ def create_database(name, options = {}) end end - execute "CREATE DATABASE #{name}#{option_string}" + execute "CREATE DATABASE #{quote_table_name(name)}#{option_string}" end # Drops a PostgreSQL database @@ -514,7 +514,7 @@ def create_database(name, options = {}) # Example: # drop_database 'matt_development' def drop_database(name) #:nodoc: - execute "DROP DATABASE IF EXISTS #{name}" + execute "DROP DATABASE IF EXISTS #{quote_table_name(name)}" end @@ -676,7 +676,7 @@ def pk_and_sequence_for(table) #:nodoc: # Renames a table. def rename_table(name, new_name) - execute "ALTER TABLE #{name} RENAME TO #{new_name}" + execute "ALTER TABLE #{quote_table_name(name)} RENAME TO #{quote_table_name(new_name)}" end # Adds a new column to the named table. From d0956335a677aeb3b78393aa6ecd05cb7e4384f4 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Sat, 7 Jun 2008 13:43:52 -0700 Subject: [PATCH 055/200] PostgreSQL: update create_database_with_encoding test also --- activerecord/test/cases/active_schema_test_postgresql.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/activerecord/test/cases/active_schema_test_postgresql.rb b/activerecord/test/cases/active_schema_test_postgresql.rb index db325e3fb4c48..af80f724f29fa 100644 --- a/activerecord/test/cases/active_schema_test_postgresql.rb +++ b/activerecord/test/cases/active_schema_test_postgresql.rb @@ -13,8 +13,8 @@ def teardown end def test_create_database_with_encoding - assert_equal "CREATE DATABASE matt ENCODING = 'utf8'", create_database(:matt) - assert_equal "CREATE DATABASE aimonetti ENCODING = 'latin1'", create_database(:aimonetti, :encoding => :latin1) + assert_equal %(CREATE DATABASE "matt" ENCODING = 'utf8'), create_database(:matt) + assert_equal %(CREATE DATABASE "aimonetti" ENCODING = 'latin1'), create_database(:aimonetti, :encoding => :latin1) end private From 63679733d8217b69e242a00083abfc4cd8a9dca0 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Sat, 7 Jun 2008 17:48:44 -0700 Subject: [PATCH 056/200] Changelog: More efficient concat and capture helpers. Remove ActionView::Base.erb_variable. --- actionpack/CHANGELOG | 2 ++ 1 file changed, 2 insertions(+) diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG index 66af5fd745180..8265b5b04f7ad 100644 --- a/actionpack/CHANGELOG +++ b/actionpack/CHANGELOG @@ -1,5 +1,7 @@ *Edge* +* More efficient concat and capture helpers. Remove ActionView::Base.erb_variable. [Jeremy Kemper] + * Added page.reload functionality. Resolves #277. [Sean Huber] * Fixed Request#remote_ip to only raise hell if the HTTP_CLIENT_IP and HTTP_X_FORWARDED_FOR doesn't match (not just if they're both present) [Mark Imbriaco, Bradford Folkens] From 67e4f16fc5303ae35bdfe9ef4ef016127751ce35 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Sat, 7 Jun 2008 18:06:45 -0700 Subject: [PATCH 057/200] No need to build a Set since we're iterating instead of checking for inclusion now --- activesupport/lib/active_support/core_ext/hash/slice.rb | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/activesupport/lib/active_support/core_ext/hash/slice.rb b/activesupport/lib/active_support/core_ext/hash/slice.rb index 1b2c8f63e32d9..be4dec6e534e4 100644 --- a/activesupport/lib/active_support/core_ext/hash/slice.rb +++ b/activesupport/lib/active_support/core_ext/hash/slice.rb @@ -1,5 +1,3 @@ -require 'set' - module ActiveSupport #:nodoc: module CoreExtensions #:nodoc: module Hash #:nodoc: @@ -14,9 +12,9 @@ module Hash #:nodoc: module Slice # Returns a new hash with only the given keys. def slice(*keys) - allowed = Set.new(respond_to?(:convert_key) ? keys.map { |key| convert_key(key) } : keys) + keys = keys.map! { |key| convert_key(key) } if respond_to?(:convert_key) hash = {} - allowed.each { |k| hash[k] = self[k] if has_key?(k) } + keys.each { |k| hash[k] = self[k] if has_key?(k) } hash end From ceb5b6dbb141888ae56788a3d40ccb7017c92f7d Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Sat, 7 Jun 2008 23:25:34 -0500 Subject: [PATCH 058/200] Wrap date part value method tests inside a uses mocha block. --- .../test/core_ext/time_with_zone_test.rb | 34 ++++++++++--------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/activesupport/test/core_ext/time_with_zone_test.rb b/activesupport/test/core_ext/time_with_zone_test.rb index 012ec373c9966..0977cd8e505b8 100644 --- a/activesupport/test/core_ext/time_with_zone_test.rb +++ b/activesupport/test/core_ext/time_with_zone_test.rb @@ -336,24 +336,26 @@ def test_marshal_dump_and_load_with_tzinfo_identifier end end - def test_method_missing_with_non_time_return_value - silence_warnings do # silence warnings raised by tzinfo gem - @twz.time.expects(:foo).returns('bar') - assert_equal 'bar', @twz.foo + uses_mocha 'TestDatePartValueMethods' do + def test_method_missing_with_non_time_return_value + silence_warnings do # silence warnings raised by tzinfo gem + @twz.time.expects(:foo).returns('bar') + assert_equal 'bar', @twz.foo + end end - end - def test_date_part_value_methods - silence_warnings do # silence warnings raised by tzinfo gem - twz = ActiveSupport::TimeWithZone.new(Time.utc(1999,12,31,19,18,17,500), @time_zone) - twz.stubs(:method_missing).returns(nil) #ensure these methods are defined directly on class - assert_equal 1999, twz.year - assert_equal 12, twz.month - assert_equal 31, twz.day - assert_equal 14, twz.hour - assert_equal 18, twz.min - assert_equal 17, twz.sec - assert_equal 500, twz.usec + def test_date_part_value_methods + silence_warnings do # silence warnings raised by tzinfo gem + twz = ActiveSupport::TimeWithZone.new(Time.utc(1999,12,31,19,18,17,500), @time_zone) + twz.stubs(:method_missing).returns(nil) #ensure these methods are defined directly on class + assert_equal 1999, twz.year + assert_equal 12, twz.month + assert_equal 31, twz.day + assert_equal 14, twz.hour + assert_equal 18, twz.min + assert_equal 17, twz.sec + assert_equal 500, twz.usec + end end end From d5539958a892ece562ba3dcb36df12e046bd4879 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Sat, 7 Jun 2008 23:42:05 -0500 Subject: [PATCH 059/200] Wrap CGIResponse, LegacyRouteSet, Route, RouteSet and RouteLoading tests inside mocha block. --- actionpack/test/controller/cgi_test.rb | 50 +- actionpack/test/controller/routing_test.rb | 3315 ++++++++++---------- 2 files changed, 1669 insertions(+), 1696 deletions(-) diff --git a/actionpack/test/controller/cgi_test.rb b/actionpack/test/controller/cgi_test.rb index f0f3a4b826b87..1b1ded4615964 100755 --- a/actionpack/test/controller/cgi_test.rb +++ b/actionpack/test/controller/cgi_test.rb @@ -115,35 +115,37 @@ def test_body_should_be_rewound end end -class CgiResponseTest < BaseCgiTest - def setup - super - @fake_cgi.expects(:header).returns("HTTP/1.0 200 OK\nContent-Type: text/html\n") - @response = ActionController::CgiResponse.new(@fake_cgi) - @output = StringIO.new('') - end - - def test_simple_output - @response.body = "Hello, World!" +uses_mocha 'CGI Response' do + class CgiResponseTest < BaseCgiTest + def setup + super + @fake_cgi.expects(:header).returns("HTTP/1.0 200 OK\nContent-Type: text/html\n") + @response = ActionController::CgiResponse.new(@fake_cgi) + @output = StringIO.new('') + end - @response.out(@output) - assert_equal "HTTP/1.0 200 OK\nContent-Type: text/html\nHello, World!", @output.string - end + def test_simple_output + @response.body = "Hello, World!" - def test_head_request - @fake_cgi.env_table['REQUEST_METHOD'] = 'HEAD' - @response.body = "Hello, World!" + @response.out(@output) + assert_equal "HTTP/1.0 200 OK\nContent-Type: text/html\nHello, World!", @output.string + end - @response.out(@output) - assert_equal "HTTP/1.0 200 OK\nContent-Type: text/html\n", @output.string - end + def test_head_request + @fake_cgi.env_table['REQUEST_METHOD'] = 'HEAD' + @response.body = "Hello, World!" - def test_streaming_block - @response.body = Proc.new do |response, output| - 5.times { |n| output.write(n) } + @response.out(@output) + assert_equal "HTTP/1.0 200 OK\nContent-Type: text/html\n", @output.string end - @response.out(@output) - assert_equal "HTTP/1.0 200 OK\nContent-Type: text/html\n01234", @output.string + def test_streaming_block + @response.body = Proc.new do |response, output| + 5.times { |n| output.write(n) } + end + + @response.out(@output) + assert_equal "HTTP/1.0 200 OK\nContent-Type: text/html\n01234", @output.string + end end end diff --git a/actionpack/test/controller/routing_test.rb b/actionpack/test/controller/routing_test.rb index 068d71a8f85d2..07c13ebbf70fe 100644 --- a/actionpack/test/controller/routing_test.rb +++ b/actionpack/test/controller/routing_test.rb @@ -59,647 +59,30 @@ def test_route_generation_allows_passing_non_string_values_to_generated_helper end end -class LegacyRouteSetTests < Test::Unit::TestCase - attr_reader :rs - def setup - # These tests assume optimisation is on, so re-enable it. - ActionController::Base.optimise_named_routes = true - - @rs = ::ActionController::Routing::RouteSet.new - @rs.draw {|m| m.connect ':controller/:action/:id' } - - ActionController::Routing.use_controllers! %w(content admin/user admin/news_feed) - end - - def test_default_setup - assert_equal({:controller => "content", :action => 'index'}, rs.recognize_path("/content")) - assert_equal({:controller => "content", :action => 'list'}, rs.recognize_path("/content/list")) - assert_equal({:controller => "content", :action => 'show', :id => '10'}, rs.recognize_path("/content/show/10")) - - assert_equal({:controller => "admin/user", :action => 'show', :id => '10'}, rs.recognize_path("/admin/user/show/10")) - - assert_equal '/admin/user/show/10', rs.generate(:controller => 'admin/user', :action => 'show', :id => 10) - - assert_equal '/admin/user/show', rs.generate({:action => 'show'}, {:controller => 'admin/user', :action => 'list', :id => '10'}) - assert_equal '/admin/user/list/10', rs.generate({}, {:controller => 'admin/user', :action => 'list', :id => '10'}) - - assert_equal '/admin/stuff', rs.generate({:controller => 'stuff'}, {:controller => 'admin/user', :action => 'list', :id => '10'}) - assert_equal '/stuff', rs.generate({:controller => '/stuff'}, {:controller => 'admin/user', :action => 'list', :id => '10'}) - end - - def test_ignores_leading_slash - @rs.draw {|m| m.connect '/:controller/:action/:id'} - test_default_setup - end - - def test_time_recognition - # We create many routes to make situation more realistic - @rs = ::ActionController::Routing::RouteSet.new - @rs.draw { |map| - map.frontpage '', :controller => 'search', :action => 'new' - map.resources :videos do |video| - video.resources :comments - video.resource :file, :controller => 'video_file' - video.resource :share, :controller => 'video_shares' - video.resource :abuse, :controller => 'video_abuses' - end - map.resources :abuses, :controller => 'video_abuses' - map.resources :video_uploads - map.resources :video_visits - - map.resources :users do |user| - user.resource :settings - user.resources :videos - end - map.resources :channels do |channel| - channel.resources :videos, :controller => 'channel_videos' - end - map.resource :session - map.resource :lost_password - map.search 'search', :controller => 'search' - map.resources :pages - map.connect ':controller/:action/:id' - } - n = 1000 - if RunTimeTests - GC.start - rectime = Benchmark.realtime do - n.times do - rs.recognize_path("/videos/1234567", {:method => :get}) - rs.recognize_path("/videos/1234567/abuse", {:method => :get}) - rs.recognize_path("/users/1234567/settings", {:method => :get}) - rs.recognize_path("/channels/1234567", {:method => :get}) - rs.recognize_path("/session/new", {:method => :get}) - rs.recognize_path("/admin/user/show/10", {:method => :get}) - end - end - puts "\n\nRecognition (#{rs.routes.size} routes):" - per_url = rectime / (n * 6) - puts "#{per_url * 1000} ms/url" - puts "#{1 / per_url} url/s\n\n" - end - end - def test_time_generation - n = 5000 - if RunTimeTests - GC.start - pairs = [ - [{:controller => 'content', :action => 'index'}, {:controller => 'content', :action => 'show'}], - [{:controller => 'content'}, {:controller => 'content', :action => 'index'}], - [{:controller => 'content', :action => 'list'}, {:controller => 'content', :action => 'index'}], - [{:controller => 'content', :action => 'show', :id => '10'}, {:controller => 'content', :action => 'list'}], - [{:controller => 'admin/user', :action => 'index'}, {:controller => 'admin/user', :action => 'show'}], - [{:controller => 'admin/user'}, {:controller => 'admin/user', :action => 'index'}], - [{:controller => 'admin/user', :action => 'list'}, {:controller => 'admin/user', :action => 'index'}], - [{:controller => 'admin/user', :action => 'show', :id => '10'}, {:controller => 'admin/user', :action => 'list'}], - ] - p = nil - gentime = Benchmark.realtime do - n.times do - pairs.each {|(a, b)| rs.generate(a, b)} - end - end - - puts "\n\nGeneration (RouteSet): (#{(n * 8)} urls)" - per_url = gentime / (n * 8) - puts "#{per_url * 1000} ms/url" - puts "#{1 / per_url} url/s\n\n" - end - end - - def test_route_with_colon_first - rs.draw do |map| - map.connect '/:controller/:action/:id', :action => 'index', :id => nil - map.connect ':url', :controller => 'tiny_url', :action => 'translate' - end - end - - def test_route_with_regexp_for_controller - rs.draw do |map| - map.connect ':controller/:admintoken/:action/:id', :controller => /admin\/.+/ - map.connect ':controller/:action/:id' - end - assert_equal({:controller => "admin/user", :admintoken => "foo", :action => "index"}, - rs.recognize_path("/admin/user/foo")) - assert_equal({:controller => "content", :action => "foo"}, rs.recognize_path("/content/foo")) - assert_equal '/admin/user/foo', rs.generate(:controller => "admin/user", :admintoken => "foo", :action => "index") - assert_equal '/content/foo', rs.generate(:controller => "content", :action => "foo") - end - - def test_route_with_regexp_and_dot - rs.draw do |map| - map.connect ':controller/:action/:file', - :controller => /admin|user/, - :action => /upload|download/, - :defaults => {:file => nil}, - :requirements => {:file => %r{[^/]+(\.[^/]+)?}} - end - # Without a file extension - assert_equal '/user/download/file', - rs.generate(:controller => "user", :action => "download", :file => "file") - assert_equal( - {:controller => "user", :action => "download", :file => "file"}, - rs.recognize_path("/user/download/file")) - - # Now, let's try a file with an extension, really a dot (.) - assert_equal '/user/download/file.jpg', - rs.generate( - :controller => "user", :action => "download", :file => "file.jpg") - assert_equal( - {:controller => "user", :action => "download", :file => "file.jpg"}, - rs.recognize_path("/user/download/file.jpg")) - end - - def test_basic_named_route - rs.add_named_route :home, '', :controller => 'content', :action => 'list' - x = setup_for_named_route - assert_equal("http://named.route.test/", - x.send(:home_url)) - end - - def test_basic_named_route_with_relative_url_root - rs.add_named_route :home, '', :controller => 'content', :action => 'list' - x = setup_for_named_route - x.relative_url_root="/foo" - assert_equal("http://named.route.test/foo/", - x.send(:home_url)) - assert_equal "/foo/", x.send(:home_path) - end - - def test_named_route_with_option - rs.add_named_route :page, 'page/:title', :controller => 'content', :action => 'show_page' - x = setup_for_named_route - assert_equal("http://named.route.test/page/new%20stuff", - x.send(:page_url, :title => 'new stuff')) - end - - def test_named_route_with_default - rs.add_named_route :page, 'page/:title', :controller => 'content', :action => 'show_page', :title => 'AboutPage' - x = setup_for_named_route - assert_equal("http://named.route.test/page/AboutRails", - x.send(:page_url, :title => "AboutRails")) - - end - - def test_named_route_with_nested_controller - rs.add_named_route :users, 'admin/user', :controller => 'admin/user', :action => 'index' - x = setup_for_named_route - assert_equal("http://named.route.test/admin/user", - x.send(:users_url)) - end - - uses_mocha "named route optimisation" do - def test_optimised_named_route_call_never_uses_url_for - rs.add_named_route :users, 'admin/user', :controller => '/admin/user', :action => 'index' - rs.add_named_route :user, 'admin/user/:id', :controller=>'/admin/user', :action=>'show' - x = setup_for_named_route - x.expects(:url_for).never - x.send(:users_url) - x.send(:users_path) - x.send(:user_url, 2, :foo=>"bar") - x.send(:user_path, 3, :bar=>"foo") - end - - def test_optimised_named_route_with_host - rs.add_named_route :pages, 'pages', :controller => 'content', :action => 'show_page', :host => 'foo.com' - x = setup_for_named_route - x.expects(:url_for).with(:host => 'foo.com', :only_path => false, :controller => 'content', :action => 'show_page', :use_route => :pages).once - x.send(:pages_url) - end - end - - def setup_for_named_route - klass = Class.new(MockController) - rs.install_helpers(klass) - klass.new(rs) - end - - def test_named_route_without_hash - rs.draw do |map| - map.normal ':controller/:action/:id' - end - end - - def test_named_route_root - rs.draw do |map| - map.root :controller => "hello" - end - x = setup_for_named_route - assert_equal("http://named.route.test/", x.send(:root_url)) - assert_equal("/", x.send(:root_path)) - end - - def test_named_route_with_regexps - rs.draw do |map| - map.article 'page/:year/:month/:day/:title', :controller => 'page', :action => 'show', - :year => /\d+/, :month => /\d+/, :day => /\d+/ - map.connect ':controller/:action/:id' - end - x = setup_for_named_route - # assert_equal( - # {:controller => 'page', :action => 'show', :title => 'hi', :use_route => :article, :only_path => false}, - # x.send(:article_url, :title => 'hi') - # ) - assert_equal( - "http://named.route.test/page/2005/6/10/hi", - x.send(:article_url, :title => 'hi', :day => 10, :year => 2005, :month => 6) - ) - end - - def test_changing_controller - assert_equal '/admin/stuff/show/10', rs.generate( - {:controller => 'stuff', :action => 'show', :id => 10}, - {:controller => 'admin/user', :action => 'index'} - ) - end - - def test_paths_escaped - rs.draw do |map| - map.path 'file/*path', :controller => 'content', :action => 'show_file' - map.connect ':controller/:action/:id' - end - - # No + to space in URI escaping, only for query params. - results = rs.recognize_path "/file/hello+world/how+are+you%3F" - assert results, "Recognition should have succeeded" - assert_equal ['hello+world', 'how+are+you?'], results[:path] - - # Use %20 for space instead. - results = rs.recognize_path "/file/hello%20world/how%20are%20you%3F" - assert results, "Recognition should have succeeded" - assert_equal ['hello world', 'how are you?'], results[:path] - - results = rs.recognize_path "/file" - assert results, "Recognition should have succeeded" - assert_equal [], results[:path] - end - - def test_paths_slashes_unescaped_with_ordered_parameters - rs.add_named_route :path, '/file/*path', :controller => 'content' - - # No / to %2F in URI, only for query params. - x = setup_for_named_route - assert_equal("/file/hello/world", x.send(:path_path, 'hello/world')) - end - - def test_non_controllers_cannot_be_matched - rs.draw do |map| - map.connect ':controller/:action/:id' - end - assert_raises(ActionController::RoutingError) { rs.recognize_path("/not_a/show/10") } - end - - def test_paths_do_not_accept_defaults - assert_raises(ActionController::RoutingError) do - rs.draw do |map| - map.path 'file/*path', :controller => 'content', :action => 'show_file', :path => %w(fake default) - map.connect ':controller/:action/:id' - end - end - - rs.draw do |map| - map.path 'file/*path', :controller => 'content', :action => 'show_file', :path => [] - map.connect ':controller/:action/:id' - end - end - - def test_should_list_options_diff_when_routing_requirements_dont_match - rs.draw do |map| - map.post 'post/:id', :controller=> 'post', :action=> 'show', :requirements => {:id => /\d+/} - end - exception = assert_raise(ActionController::RoutingError) { rs.generate(:controller => 'post', :action => 'show', :bad_param => "foo", :use_route => "post") } - assert_match /^post_url failed to generate/, exception.message - from_match = exception.message.match(/from \{[^\}]+\}/).to_s - assert_match /:bad_param=>"foo"/, from_match - assert_match /:action=>"show"/, from_match - assert_match /:controller=>"post"/, from_match - - expected_match = exception.message.match(/expected: \{[^\}]+\}/).to_s - assert_no_match /:bad_param=>"foo"/, expected_match - assert_match /:action=>"show"/, expected_match - assert_match /:controller=>"post"/, expected_match - - diff_match = exception.message.match(/diff: \{[^\}]+\}/).to_s - assert_match /:bad_param=>"foo"/, diff_match - assert_no_match /:action=>"show"/, diff_match - assert_no_match /:controller=>"post"/, diff_match - end - - # this specifies the case where your formerly would get a very confusing error message with an empty diff - def test_should_have_better_error_message_when_options_diff_is_empty - rs.draw do |map| - map.content '/content/:query', :controller => 'content', :action => 'show' - end - - exception = assert_raise(ActionController::RoutingError) { rs.generate(:controller => 'content', :action => 'show', :use_route => "content") } - assert_match %r[:action=>"show"], exception.message - assert_match %r[:controller=>"content"], exception.message - assert_match %r[you may have ambiguous routes, or you may need to supply additional parameters for this route], exception.message - assert_match %r[content_url has the following required parameters: \["content", :query\] - are they all satisfied?], exception.message - end - - def test_dynamic_path_allowed - rs.draw do |map| - map.connect '*path', :controller => 'content', :action => 'show_file' - end - - assert_equal '/pages/boo', rs.generate(:controller => 'content', :action => 'show_file', :path => %w(pages boo)) - end - - def test_dynamic_recall_paths_allowed - rs.draw do |map| - map.connect '*path', :controller => 'content', :action => 'show_file' - end - - recall_path = ActionController::Routing::PathSegment::Result.new(%w(pages boo)) - assert_equal '/pages/boo', rs.generate({}, :controller => 'content', :action => 'show_file', :path => recall_path) - end - - def test_backwards - rs.draw do |map| - map.connect 'page/:id/:action', :controller => 'pages', :action => 'show' - map.connect ':controller/:action/:id' - end - - assert_equal '/page/20', rs.generate({:id => 20}, {:controller => 'pages', :action => 'show'}) - assert_equal '/page/20', rs.generate(:controller => 'pages', :id => 20, :action => 'show') - assert_equal '/pages/boo', rs.generate(:controller => 'pages', :action => 'boo') - end - - def test_route_with_fixnum_default - rs.draw do |map| - map.connect 'page/:id', :controller => 'content', :action => 'show_page', :id => 1 - map.connect ':controller/:action/:id' - end - - assert_equal '/page', rs.generate(:controller => 'content', :action => 'show_page') - assert_equal '/page', rs.generate(:controller => 'content', :action => 'show_page', :id => 1) - assert_equal '/page', rs.generate(:controller => 'content', :action => 'show_page', :id => '1') - assert_equal '/page/10', rs.generate(:controller => 'content', :action => 'show_page', :id => 10) - - assert_equal({:controller => "content", :action => 'show_page', :id => '1'}, rs.recognize_path("/page")) - assert_equal({:controller => "content", :action => 'show_page', :id => '1'}, rs.recognize_path("/page/1")) - assert_equal({:controller => "content", :action => 'show_page', :id => '10'}, rs.recognize_path("/page/10")) - end - - # For newer revision - def test_route_with_text_default - rs.draw do |map| - map.connect 'page/:id', :controller => 'content', :action => 'show_page', :id => 1 - map.connect ':controller/:action/:id' - end - - assert_equal '/page/foo', rs.generate(:controller => 'content', :action => 'show_page', :id => 'foo') - assert_equal({:controller => "content", :action => 'show_page', :id => 'foo'}, rs.recognize_path("/page/foo")) - - token = "\321\202\320\265\320\272\321\201\321\202" # 'text' in russian - escaped_token = CGI::escape(token) - - assert_equal '/page/' + escaped_token, rs.generate(:controller => 'content', :action => 'show_page', :id => token) - assert_equal({:controller => "content", :action => 'show_page', :id => token}, rs.recognize_path("/page/#{escaped_token}")) - end - - def test_action_expiry - assert_equal '/content', rs.generate({:controller => 'content'}, {:controller => 'content', :action => 'show'}) - end - - def test_recognition_with_uppercase_controller_name - assert_equal({:controller => "content", :action => 'index'}, rs.recognize_path("/Content")) - assert_equal({:controller => "content", :action => 'list'}, rs.recognize_path("/ConTent/list")) - assert_equal({:controller => "content", :action => 'show', :id => '10'}, rs.recognize_path("/CONTENT/show/10")) - - # these used to work, before the routes rewrite, but support for this was pulled in the new version... - #assert_equal({'controller' => "admin/news_feed", 'action' => 'index'}, rs.recognize_path("Admin/NewsFeed")) - #assert_equal({'controller' => "admin/news_feed", 'action' => 'index'}, rs.recognize_path("Admin/News_Feed")) - end - - def test_requirement_should_prevent_optional_id - rs.draw do |map| - map.post 'post/:id', :controller=> 'post', :action=> 'show', :requirements => {:id => /\d+/} - end - - assert_equal '/post/10', rs.generate(:controller => 'post', :action => 'show', :id => 10) - - assert_raises ActionController::RoutingError do - rs.generate(:controller => 'post', :action => 'show') - end - end - - def test_both_requirement_and_optional - rs.draw do |map| - map.blog('test/:year', :controller => 'post', :action => 'show', - :defaults => { :year => nil }, - :requirements => { :year => /\d{4}/ } - ) - map.connect ':controller/:action/:id' - end - - assert_equal '/test', rs.generate(:controller => 'post', :action => 'show') - assert_equal '/test', rs.generate(:controller => 'post', :action => 'show', :year => nil) - - x = setup_for_named_route - assert_equal("http://named.route.test/test", - x.send(:blog_url)) - end - - def test_set_to_nil_forgets - rs.draw do |map| - map.connect 'pages/:year/:month/:day', :controller => 'content', :action => 'list_pages', :month => nil, :day => nil - map.connect ':controller/:action/:id' - end - - assert_equal '/pages/2005', - rs.generate(:controller => 'content', :action => 'list_pages', :year => 2005) - assert_equal '/pages/2005/6', - rs.generate(:controller => 'content', :action => 'list_pages', :year => 2005, :month => 6) - assert_equal '/pages/2005/6/12', - rs.generate(:controller => 'content', :action => 'list_pages', :year => 2005, :month => 6, :day => 12) - - assert_equal '/pages/2005/6/4', - rs.generate({:day => 4}, {:controller => 'content', :action => 'list_pages', :year => '2005', :month => '6', :day => '12'}) - - assert_equal '/pages/2005/6', - rs.generate({:day => nil}, {:controller => 'content', :action => 'list_pages', :year => '2005', :month => '6', :day => '12'}) - - assert_equal '/pages/2005', - rs.generate({:day => nil, :month => nil}, {:controller => 'content', :action => 'list_pages', :year => '2005', :month => '6', :day => '12'}) - end - - def test_url_with_no_action_specified - rs.draw do |map| - map.connect '', :controller => 'content' - map.connect ':controller/:action/:id' - end - - assert_equal '/', rs.generate(:controller => 'content', :action => 'index') - assert_equal '/', rs.generate(:controller => 'content') - end - - def test_named_url_with_no_action_specified - rs.draw do |map| - map.home '', :controller => 'content' - map.connect ':controller/:action/:id' - end - - assert_equal '/', rs.generate(:controller => 'content', :action => 'index') - assert_equal '/', rs.generate(:controller => 'content') - - x = setup_for_named_route - assert_equal("http://named.route.test/", - x.send(:home_url)) - end - - def test_url_generated_when_forgetting_action - [{:controller => 'content', :action => 'index'}, {:controller => 'content'}].each do |hash| - rs.draw do |map| - map.home '', hash - map.connect ':controller/:action/:id' - end - assert_equal '/', rs.generate({:action => nil}, {:controller => 'content', :action => 'hello'}) - assert_equal '/', rs.generate({:controller => 'content'}) - assert_equal '/content/hi', rs.generate({:controller => 'content', :action => 'hi'}) - end - end - - def test_named_route_method - rs.draw do |map| - map.categories 'categories', :controller => 'content', :action => 'categories' - map.connect ':controller/:action/:id' - end - - assert_equal '/categories', rs.generate(:controller => 'content', :action => 'categories') - assert_equal '/content/hi', rs.generate({:controller => 'content', :action => 'hi'}) - end - - def test_named_routes_array - test_named_route_method - assert_equal [:categories], rs.named_routes.names - end - - def test_nil_defaults - rs.draw do |map| - map.connect 'journal', - :controller => 'content', - :action => 'list_journal', - :date => nil, :user_id => nil - map.connect ':controller/:action/:id' - end - - assert_equal '/journal', rs.generate(:controller => 'content', :action => 'list_journal', :date => nil, :user_id => nil) - end - - def setup_request_method_routes_for(method) - @request = ActionController::TestRequest.new - @request.env["REQUEST_METHOD"] = method - @request.request_uri = "/match" - - rs.draw do |r| - r.connect '/match', :controller => 'books', :action => 'get', :conditions => { :method => :get } - r.connect '/match', :controller => 'books', :action => 'post', :conditions => { :method => :post } - r.connect '/match', :controller => 'books', :action => 'put', :conditions => { :method => :put } - r.connect '/match', :controller => 'books', :action => 'delete', :conditions => { :method => :delete } - end - end - - %w(GET POST PUT DELETE).each do |request_method| - define_method("test_request_method_recognized_with_#{request_method}") do - begin - Object.const_set(:BooksController, Class.new(ActionController::Base)) - - setup_request_method_routes_for(request_method) - - assert_nothing_raised { rs.recognize(@request) } - assert_equal request_method.downcase, @request.path_parameters[:action] - ensure - Object.send(:remove_const, :BooksController) rescue nil - end - end - end - - def test_subpath_recognized - Object.const_set(:SubpathBooksController, Class.new(ActionController::Base)) - - rs.draw do |r| - r.connect '/books/:id/edit', :controller => 'subpath_books', :action => 'edit' - r.connect '/items/:id/:action', :controller => 'subpath_books' - r.connect '/posts/new/:action', :controller => 'subpath_books' - r.connect '/posts/:id', :controller => 'subpath_books', :action => "show" - end - - hash = rs.recognize_path "/books/17/edit" - assert_not_nil hash - assert_equal %w(subpath_books 17 edit), [hash[:controller], hash[:id], hash[:action]] - - hash = rs.recognize_path "/items/3/complete" - assert_not_nil hash - assert_equal %w(subpath_books 3 complete), [hash[:controller], hash[:id], hash[:action]] - - hash = rs.recognize_path "/posts/new/preview" - assert_not_nil hash - assert_equal %w(subpath_books preview), [hash[:controller], hash[:action]] - - hash = rs.recognize_path "/posts/7" - assert_not_nil hash - assert_equal %w(subpath_books show 7), [hash[:controller], hash[:action], hash[:id]] - ensure - Object.send(:remove_const, :SubpathBooksController) rescue nil - end - - def test_subpath_generated - Object.const_set(:SubpathBooksController, Class.new(ActionController::Base)) - - rs.draw do |r| - r.connect '/books/:id/edit', :controller => 'subpath_books', :action => 'edit' - r.connect '/items/:id/:action', :controller => 'subpath_books' - r.connect '/posts/new/:action', :controller => 'subpath_books' - end - - assert_equal "/books/7/edit", rs.generate(:controller => "subpath_books", :id => 7, :action => "edit") - assert_equal "/items/15/complete", rs.generate(:controller => "subpath_books", :id => 15, :action => "complete") - assert_equal "/posts/new/preview", rs.generate(:controller => "subpath_books", :action => "preview") - ensure - Object.send(:remove_const, :SubpathBooksController) rescue nil - end - - def test_failed_requirements_raises_exception_with_violated_requirements - rs.draw do |r| - r.foo_with_requirement 'foos/:id', :controller=>'foos', :requirements=>{:id=>/\d+/} - end - - x = setup_for_named_route - assert_raises(ActionController::RoutingError) do - x.send(:foo_with_requirement_url, "I am Against the requirements") - end - end -end - class SegmentTest < Test::Unit::TestCase - def test_first_segment_should_interpolate_for_structure s = ROUTING::Segment.new def s.interpolation_statement(array) 'hello' end assert_equal 'hello', s.continue_string_structure([]) end - + def test_interpolation_statement s = ROUTING::StaticSegment.new s.value = "Hello" assert_equal "Hello", eval(s.interpolation_statement([])) assert_equal "HelloHello", eval(s.interpolation_statement([s])) - + s2 = ROUTING::StaticSegment.new s2.value = "-" assert_equal "Hello-Hello", eval(s.interpolation_statement([s, s2])) - + s3 = ROUTING::StaticSegment.new s3.value = "World" assert_equal "Hello-World", eval(s3.interpolation_statement([s, s2])) end - end class StaticSegmentTest < Test::Unit::TestCase - def test_interpolation_chunk_should_respect_raw s = ROUTING::StaticSegment.new s.value = 'Hello World' @@ -713,28 +96,26 @@ def test_interpolation_chunk_should_respect_raw def test_regexp_chunk_should_escape_specials s = ROUTING::StaticSegment.new - + s.value = 'Hello*World' assert_equal 'Hello\*World', s.regexp_chunk - + s.value = 'HelloWorld' assert_equal 'HelloWorld', s.regexp_chunk end - + def test_regexp_chunk_should_add_question_mark_for_optionals s = ROUTING::StaticSegment.new s.value = "/" s.is_optional = true assert_equal "/?", s.regexp_chunk - + s.value = "hello" assert_equal "(?:hello)?", s.regexp_chunk end - end class DynamicSegmentTest < Test::Unit::TestCase - def segment unless @segment @segment = ROUTING::DynamicSegment.new @@ -742,126 +123,126 @@ def segment end @segment end - + def test_extract_value s = ROUTING::DynamicSegment.new s.key = :a - + hash = {:a => '10', :b => '20'} assert_equal '10', eval(s.extract_value) - + hash = {:b => '20'} assert_equal nil, eval(s.extract_value) - + s.default = '20' assert_equal '20', eval(s.extract_value) end - + def test_default_local_name assert_equal 'a_value', segment.local_name, "Unexpected name -- all value_check tests will fail!" end - + def test_presence_value_check a_value = 10 assert eval(segment.value_check) end - + def test_regexp_value_check_rejects_nil segment.regexp = /\d+/ a_value = nil assert ! eval(segment.value_check) end - + def test_optional_regexp_value_check_should_accept_nil segment.regexp = /\d+/ segment.is_optional = true a_value = nil assert eval(segment.value_check) end - + def test_regexp_value_check_rejects_no_match segment.regexp = /\d+/ - + a_value = "Hello20World" assert ! eval(segment.value_check) - + a_value = "20Hi" assert ! eval(segment.value_check) end - + def test_regexp_value_check_accepts_match segment.regexp = /\d+/ - + a_value = "30" assert eval(segment.value_check) end - + def test_value_check_fails_on_nil a_value = nil assert ! eval(segment.value_check) end - + def test_optional_value_needs_no_check segment.is_optional = true a_value = nil assert_equal nil, segment.value_check end - + def test_regexp_value_check_should_accept_match_with_default segment.regexp = /\d+/ segment.default = '200' - + a_value = '100' assert eval(segment.value_check) end - + def test_expiry_should_not_trigger_once_expired expired = true hash = merged = {:a => 2, :b => 3} options = {:b => 3} expire_on = Hash.new { raise 'No!!!' } - + eval(segment.expiry_statement) rescue RuntimeError flunk "Expiry check should not have occurred!" end - + def test_expiry_should_occur_according_to_expire_on expired = false hash = merged = {:a => 2, :b => 3} options = {:b => 3} - + expire_on = {:b => true, :a => false} eval(segment.expiry_statement) assert !expired assert_equal({:a => 2, :b => 3}, hash) - + expire_on = {:b => true, :a => true} eval(segment.expiry_statement) assert expired assert_equal({:b => 3}, hash) end - + def test_extraction_code_should_return_on_nil hash = merged = {:b => 3} options = {:b => 3} a_value = nil - + # Local jump because of return inside eval. assert_raises(LocalJumpError) { eval(segment.extraction_code) } end - + def test_extraction_code_should_return_on_mismatch segment.regexp = /\d+/ hash = merged = {:a => 'Hi', :b => '3'} options = {:b => '3'} a_value = nil - + # Local jump because of return inside eval. assert_raises(LocalJumpError) { eval(segment.extraction_code) } end - + def test_extraction_code_should_accept_value_and_set_local hash = merged = {:a => 'Hi', :b => '3'} options = {:b => '3'} @@ -871,45 +252,45 @@ def test_extraction_code_should_accept_value_and_set_local eval(segment.extraction_code) assert_equal 'Hi', a_value end - + def test_extraction_should_work_without_value_check segment.default = 'hi' hash = merged = {:b => '3'} options = {:b => '3'} a_value = nil expired = true - + eval(segment.extraction_code) assert_equal 'hi', a_value end - + def test_extraction_code_should_perform_expiry expired = false hash = merged = {:a => 'Hi', :b => '3'} options = {:b => '3'} expire_on = {:a => true} a_value = nil - + eval(segment.extraction_code) assert_equal 'Hi', a_value assert expired assert_equal options, hash end - + def test_interpolation_chunk_should_replace_value a_value = 'Hi' assert_equal a_value, eval(%("#{segment.interpolation_chunk}")) end - + def test_interpolation_chunk_should_accept_nil a_value = nil assert_equal '', eval(%("#{segment.interpolation_chunk('a_value')}")) end - + def test_value_regexp_should_be_nil_without_regexp assert_equal nil, segment.value_regexp end - + def test_value_regexp_should_match_exacly segment.regexp = /\d+/ assert_no_match segment.value_regexp, "Hello 10 World" @@ -917,12 +298,12 @@ def test_value_regexp_should_match_exacly assert_no_match segment.value_regexp, "10 World" assert_match segment.value_regexp, "10" end - + def test_regexp_chunk_should_return_string segment.regexp = /\d+/ assert_kind_of String, segment.regexp_chunk end - + def test_build_pattern_non_optional_with_no_captures # Non optional a_segment = ROUTING::DynamicSegment.new @@ -958,284 +339,43 @@ def test_modifiers_must_be_handled_sensibly end class ControllerSegmentTest < Test::Unit::TestCase - def test_regexp_should_only_match_possible_controllers ActionController::Routing.with_controllers %w(admin/accounts admin/users account pages) do cs = ROUTING::ControllerSegment.new :controller regexp = %r{\A#{cs.regexp_chunk}\Z} - + ActionController::Routing.possible_controllers.each do |name| assert_match regexp, name assert_no_match regexp, "#{name}_fake" - + match = regexp.match name assert_equal name, match[1] end end end - end -uses_mocha 'RouteTest' do - - class MockController - attr_accessor :routes - - def initialize(routes) - self.routes = routes - end - - def url_for(options) - only_path = options.delete(:only_path) - - port = options.delete(:port) || 80 - port_string = port == 80 ? '' : ":#{port}" - - host = options.delete(:host) || "named.route.test" - anchor = "##{options.delete(:anchor)}" if options.key?(:anchor) - - path = routes.generate(options) - - only_path ? "#{path}#{anchor}" : "http://#{host}#{port_string}#{path}#{anchor}" - end - - def request - @request ||= MockRequest.new(:host => "named.route.test", :method => :get) - end - - def relative_url_root=(value) - request.relative_url_root=value - end +class RouteBuilderTest < Test::Unit::TestCase + def builder + @builder ||= ROUTING::RouteBuilder.new end - class MockRequest - attr_accessor :path, :path_parameters, :host, :subdomains, :domain, - :method, :relative_url_root - - def initialize(values={}) - values.each { |key, value| send("#{key}=", value) } - if values[:host] - subdomain, self.domain = values[:host].split(/\./, 2) - self.subdomains = [subdomain] - end - end - - def protocol - "http://" - end - - def host_with_port - (subdomains * '.') + '.' + domain - end + def build(path, options) + builder.build(path, options) end -class RouteTest < Test::Unit::TestCase + def test_options_should_not_be_modified + requirements1 = { :id => /\w+/, :controller => /(?:[a-z](?:-?[a-z]+)*)/ } + requirements2 = requirements1.dup - def setup - @route = ROUTING::Route.new - end + assert_equal requirements1, requirements2 - def slash_segment(is_optional = false) - returning ROUTING::DividerSegment.new('/') do |s| - s.is_optional = is_optional + with_options(:controller => 'folder', + :requirements => requirements2) do |m| + m.build 'folders/new', :action => 'new' end - end - - def default_route - unless defined?(@default_route) - @default_route = ROUTING::Route.new - - @default_route.segments << (s = ROUTING::StaticSegment.new) - s.value = '/' - s.raw = true - - @default_route.segments << (s = ROUTING::DynamicSegment.new) - s.key = :controller - - @default_route.segments << slash_segment(:optional) - @default_route.segments << (s = ROUTING::DynamicSegment.new) - s.key = :action - s.default = 'index' - s.is_optional = true - - @default_route.segments << slash_segment(:optional) - @default_route.segments << (s = ROUTING::DynamicSegment.new) - s.key = :id - s.is_optional = true - - @default_route.segments << slash_segment(:optional) - end - @default_route - end - - def test_default_route_recognition - expected = {:controller => 'accounts', :action => 'show', :id => '10'} - assert_equal expected, default_route.recognize('/accounts/show/10') - assert_equal expected, default_route.recognize('/accounts/show/10/') - - expected[:id] = 'jamis' - assert_equal expected, default_route.recognize('/accounts/show/jamis/') - - expected.delete :id - assert_equal expected, default_route.recognize('/accounts/show') - assert_equal expected, default_route.recognize('/accounts/show/') - - expected[:action] = 'index' - assert_equal expected, default_route.recognize('/accounts/') - assert_equal expected, default_route.recognize('/accounts') - - assert_equal nil, default_route.recognize('/') - assert_equal nil, default_route.recognize('/accounts/how/goood/it/is/to/be/free') - end - - def test_default_route_should_omit_default_action - o = {:controller => 'accounts', :action => 'index'} - assert_equal '/accounts', default_route.generate(o, o, {}) - end - - def test_default_route_should_include_default_action_when_id_present - o = {:controller => 'accounts', :action => 'index', :id => '20'} - assert_equal '/accounts/index/20', default_route.generate(o, o, {}) - end - - def test_default_route_should_work_with_action_but_no_id - o = {:controller => 'accounts', :action => 'list_all'} - assert_equal '/accounts/list_all', default_route.generate(o, o, {}) - end - - def test_default_route_should_uri_escape_pluses - expected = { :controller => 'accounts', :action => 'show', :id => 'hello world' } - assert_equal expected, default_route.recognize('/accounts/show/hello world') - assert_equal expected, default_route.recognize('/accounts/show/hello%20world') - assert_equal '/accounts/show/hello%20world', default_route.generate(expected, expected, {}) - - expected[:id] = 'hello+world' - assert_equal expected, default_route.recognize('/accounts/show/hello+world') - assert_equal expected, default_route.recognize('/accounts/show/hello%2Bworld') - assert_equal '/accounts/show/hello+world', default_route.generate(expected, expected, {}) - end - - def test_matches_controller_and_action - # requirement_for should only be called for the action and controller _once_ - @route.expects(:requirement_for).with(:controller).times(1).returns('pages') - @route.expects(:requirement_for).with(:action).times(1).returns('show') - - @route.requirements = {:controller => 'pages', :action => 'show'} - assert @route.matches_controller_and_action?('pages', 'show') - assert !@route.matches_controller_and_action?('not_pages', 'show') - assert !@route.matches_controller_and_action?('pages', 'not_show') - end - - def test_parameter_shell - page_url = ROUTING::Route.new - page_url.requirements = {:controller => 'pages', :action => 'show', :id => /\d+/} - assert_equal({:controller => 'pages', :action => 'show'}, page_url.parameter_shell) - end - - def test_defaults - route = ROUTING::RouteBuilder.new.build '/users/:id.:format', :controller => "users", :action => "show", :format => "html" - assert_equal( - { :controller => "users", :action => "show", :format => "html" }, - route.defaults) - end - - def test_builder_complains_without_controller - assert_raises(ArgumentError) do - ROUTING::RouteBuilder.new.build '/contact', :contoller => "contact", :action => "index" - end - end - - def test_significant_keys_for_default_route - keys = default_route.significant_keys.sort_by {|k| k.to_s } - assert_equal [:action, :controller, :id], keys - end - - def test_significant_keys - user_url = ROUTING::Route.new - user_url.segments << (s = ROUTING::StaticSegment.new) - s.value = '/' - s.raw = true - - user_url.segments << (s = ROUTING::StaticSegment.new) - s.value = 'user' - - user_url.segments << (s = ROUTING::StaticSegment.new) - s.value = '/' - s.raw = true - s.is_optional = true - - user_url.segments << (s = ROUTING::DynamicSegment.new) - s.key = :user - - user_url.segments << (s = ROUTING::StaticSegment.new) - s.value = '/' - s.raw = true - s.is_optional = true - - user_url.requirements = {:controller => 'users', :action => 'show'} - - keys = user_url.significant_keys.sort_by { |k| k.to_s } - assert_equal [:action, :controller, :user], keys - end - - def test_build_empty_query_string - assert_equal '', @route.build_query_string({}) - end - - def test_build_query_string_with_nil_value - assert_equal '', @route.build_query_string({:x => nil}) - end - def test_simple_build_query_string - assert_equal '?x=1&y=2', order_query_string(@route.build_query_string(:x => '1', :y => '2')) - end - - def test_convert_ints_build_query_string - assert_equal '?x=1&y=2', order_query_string(@route.build_query_string(:x => 1, :y => 2)) - end - - def test_escape_spaces_build_query_string - assert_equal '?x=hello+world&y=goodbye+world', order_query_string(@route.build_query_string(:x => 'hello world', :y => 'goodbye world')) - end - - def test_expand_array_build_query_string - assert_equal '?x%5B%5D=1&x%5B%5D=2', order_query_string(@route.build_query_string(:x => [1, 2])) - end - - def test_escape_spaces_build_query_string_selected_keys - assert_equal '?x=hello+world', order_query_string(@route.build_query_string({:x => 'hello world', :y => 'goodbye world'}, [:x])) - end - - private - def order_query_string(qs) - '?' + qs[1..-1].split('&').sort.join('&') - end -end - -end # uses_mocha - -class RouteBuilderTest < Test::Unit::TestCase - - def builder - @builder ||= ROUTING::RouteBuilder.new - end - - def build(path, options) - builder.build(path, options) - end - - def test_options_should_not_be_modified - requirements1 = { :id => /\w+/, :controller => /(?:[a-z](?:-?[a-z]+)*)/ } - requirements2 = requirements1.dup - - assert_equal requirements1, requirements2 - - with_options(:controller => 'folder', - :requirements => requirements2) do |m| - m.build 'folders/new', :action => 'new' - end - - assert_equal requirements1, requirements2 + assert_equal requirements1, requirements2 end def test_segment_for_static @@ -1244,7 +384,7 @@ def test_segment_for_static assert_kind_of ROUTING::StaticSegment, segment assert_equal 'ulysses', segment.value end - + def test_segment_for_action segment, rest = builder.segment_for ':action' assert_equal '', rest @@ -1252,7 +392,7 @@ def test_segment_for_action assert_equal :action, segment.key assert_equal 'index', segment.default end - + def test_segment_for_dynamic segment, rest = builder.segment_for ':login' assert_equal '', rest @@ -1261,7 +401,7 @@ def test_segment_for_dynamic assert_equal nil, segment.default assert ! segment.optional? end - + def test_segment_for_with_rest segment, rest = builder.segment_for ':login/:action' assert_equal :login, segment.key @@ -1273,105 +413,104 @@ def test_segment_for_with_rest assert_equal :action, segment.key assert_equal '', rest end - + def test_segments_for segments = builder.segments_for_route_path '/:controller/:action/:id' - + assert_kind_of ROUTING::DividerSegment, segments[0] assert_equal '/', segments[2].value - + assert_kind_of ROUTING::DynamicSegment, segments[1] assert_equal :controller, segments[1].key - + assert_kind_of ROUTING::DividerSegment, segments[2] assert_equal '/', segments[2].value - + assert_kind_of ROUTING::DynamicSegment, segments[3] assert_equal :action, segments[3].key - + assert_kind_of ROUTING::DividerSegment, segments[4] assert_equal '/', segments[4].value - + assert_kind_of ROUTING::DynamicSegment, segments[5] assert_equal :id, segments[5].key end - + def test_segment_for_action s, r = builder.segment_for(':action/something/else') assert_equal '/something/else', r assert_equal :action, s.key end - + def test_action_default_should_not_trigger_on_prefix s, r = builder.segment_for ':action_name/something/else' assert_equal '/something/else', r assert_equal :action_name, s.key assert_equal nil, s.default end - + def test_divide_route_options segments = builder.segments_for_route_path '/cars/:action/:person/:car/' defaults, requirements = builder.divide_route_options(segments, :action => 'buy', :person => /\w+/, :car => /\w+/, :defaults => {:person => nil, :car => nil} ) - + assert_equal({:action => 'buy', :person => nil, :car => nil}, defaults) assert_equal({:person => /\w+/, :car => /\w+/}, requirements) end - + def test_assign_route_options segments = builder.segments_for_route_path '/cars/:action/:person/:car/' defaults = {:action => 'buy', :person => nil, :car => nil} requirements = {:person => /\w+/, :car => /\w+/} - + route_requirements = builder.assign_route_options(segments, defaults, requirements) assert_equal({}, route_requirements) - + assert_equal :action, segments[3].key assert_equal 'buy', segments[3].default - + assert_equal :person, segments[5].key assert_equal %r/\w+/, segments[5].regexp assert segments[5].optional? - + assert_equal :car, segments[7].key assert_equal %r/\w+/, segments[7].regexp assert segments[7].optional? end - + def test_assign_route_options_with_anchor_chars segments = builder.segments_for_route_path '/cars/:action/:person/:car/' defaults = {:action => 'buy', :person => nil, :car => nil} requirements = {:person => /\w+/, :car => /^\w+$/} - + assert_raises ArgumentError do route_requirements = builder.assign_route_options(segments, defaults, requirements) end - + requirements[:car] = /[^\/]+/ route_requirements = builder.assign_route_options(segments, defaults, requirements) end - def test_optional_segments_preceding_required_segments segments = builder.segments_for_route_path '/cars/:action/:person/:car/' defaults = {:action => 'buy', :person => nil, :car => "model-t"} assert builder.assign_route_options(segments, defaults, {}).empty? - + 0.upto(1) { |i| assert !segments[i].optional?, "segment #{i} is optional and it shouldn't be" } assert segments[2].optional? - + assert_equal nil, builder.warn_output # should only warn on the :person segment end - + def test_segmentation_of_dot_path segments = builder.segments_for_route_path '/books/:action.rss' assert builder.assign_route_options(segments, {}, {}).empty? assert_equal 6, segments.length # "/", "books", "/", ":action", ".", "rss" assert !segments.any? { |seg| seg.optional? } end - + def test_segmentation_of_dynamic_dot_path segments = builder.segments_for_route_path '/books/:action.:format' assert builder.assign_route_options(segments, {}, {}).empty? @@ -1379,56 +518,56 @@ def test_segmentation_of_dynamic_dot_path assert !segments.any? { |seg| seg.optional? } assert_kind_of ROUTING::DynamicSegment, segments.last end - + def test_assignment_of_default_options segments = builder.segments_for_route_path '/:controller/:action/:id/' action, id = segments[-4], segments[-2] - + assert_equal :action, action.key assert_equal :id, id.key assert ! action.optional? assert ! id.optional? - + builder.assign_default_route_options(segments) - + assert_equal 'index', action.default assert action.optional? assert id.optional? end - + def test_assignment_of_default_options_respects_existing_defaults segments = builder.segments_for_route_path '/:controller/:action/:id/' action, id = segments[-4], segments[-2] - + assert_equal :action, action.key assert_equal :id, id.key action.default = 'show' action.is_optional = true - + id.default = 'Welcome' id.is_optional = true - + builder.assign_default_route_options(segments) - + assert_equal 'show', action.default assert action.optional? assert_equal 'Welcome', id.default assert id.optional? end - + def test_assignment_of_default_options_respects_regexps segments = builder.segments_for_route_path '/:controller/:action/:id/' action = segments[-4] - + assert_equal :action, action.key action.regexp = /show|in/ # Use 'in' to check partial matches - + builder.assign_default_route_options(segments) - + assert_equal nil, action.default assert ! action.optional? end - + def test_assignment_of_is_optional_when_default segments = builder.segments_for_route_path '/books/:action.rss' assert_equal segments[3].key, :action @@ -1436,44 +575,44 @@ def test_assignment_of_is_optional_when_default builder.ensure_required_segments(segments) assert ! segments[3].optional? end - + def test_is_optional_is_assigned_to_default_segments segments = builder.segments_for_route_path '/books/:action' builder.assign_route_options(segments, {:action => 'index'}, {}) - + assert_equal segments[3].key, :action assert segments[3].optional? assert_kind_of ROUTING::DividerSegment, segments[2] assert segments[2].optional? end - + # XXX is optional not being set right? # /blah/:defaulted_segment <-- is the second slash optional? it should be. - + def test_route_build ActionController::Routing.with_controllers %w(users pages) do r = builder.build '/:controller/:action/:id/', :action => nil - + [0, 2, 4].each do |i| assert_kind_of ROUTING::DividerSegment, r.segments[i] assert_equal '/', r.segments[i].value assert r.segments[i].optional? if i > 1 end - + assert_kind_of ROUTING::DynamicSegment, r.segments[1] assert_equal :controller, r.segments[1].key assert_equal nil, r.segments[1].default - + assert_kind_of ROUTING::DynamicSegment, r.segments[3] assert_equal :action, r.segments[3].key assert_equal 'index', r.segments[3].default - + assert_kind_of ROUTING::DynamicSegment, r.segments[5] assert_equal :id, r.segments[5].key assert r.segments[5].optional? end end - + def test_slashes_are_implied routes = [ builder.build('/:controller/:action/:id/', :action => nil), @@ -1487,668 +626,1627 @@ def test_slashes_are_implied assert_equal expected, found, "Route #{i + 1} has #{found} segments, expected #{expected}" end end - end +class RoutingTest < Test::Unit::TestCase + def test_possible_controllers + true_controller_paths = ActionController::Routing.controller_paths + ActionController::Routing.use_controllers! nil + silence_warnings do + Object.send(:const_set, :RAILS_ROOT, File.dirname(__FILE__) + '/controller_fixtures') + end -class RouteSetTest < Test::Unit::TestCase + ActionController::Routing.controller_paths = [ + RAILS_ROOT, RAILS_ROOT + '/app/controllers', RAILS_ROOT + '/vendor/plugins/bad_plugin/lib' + ] - def set - @set ||= ROUTING::RouteSet.new + assert_equal ["admin/user", "plugin", "user"], ActionController::Routing.possible_controllers.sort + ensure + if true_controller_paths + ActionController::Routing.controller_paths = true_controller_paths + end + ActionController::Routing.use_controllers! nil + Object.send(:remove_const, :RAILS_ROOT) rescue nil end - def request - @request ||= MockRequest.new(:host => "named.routes.test", :method => :get) - end + def test_possible_controllers_are_reset_on_each_load + true_possible_controllers = ActionController::Routing.possible_controllers + true_controller_paths = ActionController::Routing.controller_paths - def test_generate_extras - set.draw { |m| m.connect ':controller/:action/:id' } - path, extras = set.generate_extras(:controller => "foo", :action => "bar", :id => 15, :this => "hello", :that => "world") - assert_equal "/foo/bar/15", path - assert_equal %w(that this), extras.map(&:to_s).sort - end + ActionController::Routing.use_controllers! nil + root = File.dirname(__FILE__) + '/controller_fixtures' - def test_extra_keys - set.draw { |m| m.connect ':controller/:action/:id' } - extras = set.extra_keys(:controller => "foo", :action => "bar", :id => 15, :this => "hello", :that => "world") - assert_equal %w(that this), extras.map(&:to_s).sort - end - - def test_generate_extras_not_first - set.draw do |map| - map.connect ':controller/:action/:id.:format' - map.connect ':controller/:action/:id' - end - path, extras = set.generate_extras(:controller => "foo", :action => "bar", :id => 15, :this => "hello", :that => "world") - assert_equal "/foo/bar/15", path - assert_equal %w(that this), extras.map(&:to_s).sort - end - - def test_generate_not_first - set.draw do |map| - map.connect ':controller/:action/:id.:format' - map.connect ':controller/:action/:id' - end - assert_equal "/foo/bar/15?this=hello", set.generate(:controller => "foo", :action => "bar", :id => 15, :this => "hello") - end - - def test_extra_keys_not_first - set.draw do |map| - map.connect ':controller/:action/:id.:format' - map.connect ':controller/:action/:id' - end - extras = set.extra_keys(:controller => "foo", :action => "bar", :id => 15, :this => "hello", :that => "world") - assert_equal %w(that this), extras.map(&:to_s).sort - end + ActionController::Routing.controller_paths = [] + assert_equal [], ActionController::Routing.possible_controllers - def test_draw - assert_equal 0, set.routes.size - set.draw do |map| - map.connect '/hello/world', :controller => 'a', :action => 'b' - end - assert_equal 1, set.routes.size - end - - def test_named_draw - assert_equal 0, set.routes.size - set.draw do |map| - map.hello '/hello/world', :controller => 'a', :action => 'b' - end - assert_equal 1, set.routes.size - assert_equal set.routes.first, set.named_routes[:hello] - end - - def test_later_named_routes_take_precedence - set.draw do |map| - map.hello '/hello/world', :controller => 'a', :action => 'b' - map.hello '/hello', :controller => 'a', :action => 'b' - end - assert_equal set.routes.last, set.named_routes[:hello] - end + ActionController::Routing::Routes.load! + ActionController::Routing.controller_paths = [ + root, root + '/app/controllers', root + '/vendor/plugins/bad_plugin/lib' + ] - def setup_named_route_test - set.draw do |map| - map.show '/people/:id', :controller => 'people', :action => 'show' - map.index '/people', :controller => 'people', :action => 'index' - map.multi '/people/go/:foo/:bar/joe/:id', :controller => 'people', :action => 'multi' - map.users '/admin/users', :controller => 'admin/users', :action => 'index' - end + assert_equal ["admin/user", "plugin", "user"], ActionController::Routing.possible_controllers.sort + ensure + ActionController::Routing.controller_paths = true_controller_paths + ActionController::Routing.use_controllers! true_possible_controllers + Object.send(:remove_const, :RAILS_ROOT) rescue nil - klass = Class.new(MockController) - set.install_helpers(klass) - klass.new(set) + ActionController::Routing::Routes.clear! + ActionController::Routing::Routes.load_routes! end - def test_named_route_hash_access_method - controller = setup_named_route_test + def test_with_controllers + c = %w(admin/accounts admin/users account pages) + ActionController::Routing.with_controllers c do + assert_equal c, ActionController::Routing.possible_controllers + end + end - assert_equal( - { :controller => 'people', :action => 'show', :id => 5, :use_route => :show, :only_path => false }, - controller.send(:hash_for_show_url, :id => 5)) + def test_normalize_unix_paths + load_paths = %w(. config/../app/controllers config/../app//helpers script/../config/../vendor/rails/actionpack/lib vendor/rails/railties/builtin/rails_info app/models lib script/../config/../foo/bar/../../app/models .foo/../.bar foo.bar/../config) + paths = ActionController::Routing.normalize_paths(load_paths) + assert_equal %w(vendor/rails/railties/builtin/rails_info vendor/rails/actionpack/lib app/controllers app/helpers app/models config .bar lib .), paths + end - assert_equal( - { :controller => 'people', :action => 'index', :use_route => :index, :only_path => false }, - controller.send(:hash_for_index_url)) - - assert_equal( - { :controller => 'people', :action => 'show', :id => 5, :use_route => :show, :only_path => true }, - controller.send(:hash_for_show_path, :id => 5) - ) + def test_normalize_windows_paths + load_paths = %w(. config\\..\\app\\controllers config\\..\\app\\\\helpers script\\..\\config\\..\\vendor\\rails\\actionpack\\lib vendor\\rails\\railties\\builtin\\rails_info app\\models lib script\\..\\config\\..\\foo\\bar\\..\\..\\app\\models .foo\\..\\.bar foo.bar\\..\\config) + paths = ActionController::Routing.normalize_paths(load_paths) + assert_equal %w(vendor\\rails\\railties\\builtin\\rails_info vendor\\rails\\actionpack\\lib app\\controllers app\\helpers app\\models config .bar lib .), paths end - def test_named_route_url_method - controller = setup_named_route_test - - assert_equal "http://named.route.test/people/5", controller.send(:show_url, :id => 5) - assert_equal "/people/5", controller.send(:show_path, :id => 5) - - assert_equal "http://named.route.test/people", controller.send(:index_url) - assert_equal "/people", controller.send(:index_path) + def test_routing_helper_module + assert_kind_of Module, ActionController::Routing::Helpers - assert_equal "http://named.route.test/admin/users", controller.send(:users_url) - assert_equal '/admin/users', controller.send(:users_path) - assert_equal '/admin/users', set.generate(controller.send(:hash_for_users_url), {:controller => 'users', :action => 'index'}) + h = ActionController::Routing::Helpers + c = Class.new + assert ! c.ancestors.include?(h) + ActionController::Routing::Routes.install_helpers c + assert c.ancestors.include?(h) end +end - def test_named_route_url_method_with_anchor - controller = setup_named_route_test +uses_mocha 'LegacyRouteSet, Route, RouteSet and RouteLoading' do + class MockController + attr_accessor :routes - assert_equal "http://named.route.test/people/5#location", controller.send(:show_url, :id => 5, :anchor => 'location') - assert_equal "/people/5#location", controller.send(:show_path, :id => 5, :anchor => 'location') + def initialize(routes) + self.routes = routes + end - assert_equal "http://named.route.test/people#location", controller.send(:index_url, :anchor => 'location') - assert_equal "/people#location", controller.send(:index_path, :anchor => 'location') + def url_for(options) + only_path = options.delete(:only_path) - assert_equal "http://named.route.test/admin/users#location", controller.send(:users_url, :anchor => 'location') - assert_equal '/admin/users#location', controller.send(:users_path, :anchor => 'location') + port = options.delete(:port) || 80 + port_string = port == 80 ? '' : ":#{port}" - assert_equal "http://named.route.test/people/go/7/hello/joe/5#location", - controller.send(:multi_url, 7, "hello", 5, :anchor => 'location') + host = options.delete(:host) || "named.route.test" + anchor = "##{options.delete(:anchor)}" if options.key?(:anchor) - assert_equal "http://named.route.test/people/go/7/hello/joe/5?baz=bar#location", - controller.send(:multi_url, 7, "hello", 5, :baz => "bar", :anchor => 'location') + path = routes.generate(options) - assert_equal "http://named.route.test/people?baz=bar#location", - controller.send(:index_url, :baz => "bar", :anchor => 'location') - end - - def test_named_route_url_method_with_port - controller = setup_named_route_test - assert_equal "http://named.route.test:8080/people/5", controller.send(:show_url, 5, :port=>8080) - end - - def test_named_route_url_method_with_host - controller = setup_named_route_test - assert_equal "http://some.example.com/people/5", controller.send(:show_url, 5, :host=>"some.example.com") - end - + only_path ? "#{path}#{anchor}" : "http://#{host}#{port_string}#{path}#{anchor}" + end - def test_named_route_url_method_with_ordered_parameters - controller = setup_named_route_test - assert_equal "http://named.route.test/people/go/7/hello/joe/5", - controller.send(:multi_url, 7, "hello", 5) - end + def request + @request ||= MockRequest.new(:host => "named.route.test", :method => :get) + end - def test_named_route_url_method_with_ordered_parameters_and_hash - controller = setup_named_route_test - assert_equal "http://named.route.test/people/go/7/hello/joe/5?baz=bar", - controller.send(:multi_url, 7, "hello", 5, :baz => "bar") - end - - def test_named_route_url_method_with_no_positional_arguments - controller = setup_named_route_test - assert_equal "http://named.route.test/people?baz=bar", - controller.send(:index_url, :baz => "bar") + def relative_url_root=(value) + request.relative_url_root=value + end end - - def test_draw_default_route - ActionController::Routing.with_controllers(['users']) do - set.draw do |map| - map.connect '/:controller/:action/:id' - end - assert_equal 1, set.routes.size - route = set.routes.first + class MockRequest + attr_accessor :path, :path_parameters, :host, :subdomains, :domain, + :method, :relative_url_root - assert route.segments.last.optional? + def initialize(values={}) + values.each { |key, value| send("#{key}=", value) } + if values[:host] + subdomain, self.domain = values[:host].split(/\./, 2) + self.subdomains = [subdomain] + end + end - assert_equal '/users/show/10', set.generate(:controller => 'users', :action => 'show', :id => 10) - assert_equal '/users/index/10', set.generate(:controller => 'users', :id => 10) + def protocol + "http://" + end - assert_equal({:controller => 'users', :action => 'index', :id => '10'}, set.recognize_path('/users/index/10')) - assert_equal({:controller => 'users', :action => 'index', :id => '10'}, set.recognize_path('/users/index/10/')) + def host_with_port + (subdomains * '.') + '.' + domain end end - def test_draw_default_route_with_default_controller - ActionController::Routing.with_controllers(['users']) do - set.draw do |map| - map.connect '/:controller/:action/:id', :controller => 'users' - end - assert_equal({:controller => 'users', :action => 'index'}, set.recognize_path('/')) + class LegacyRouteSetTests < Test::Unit::TestCase + attr_reader :rs + + def setup + # These tests assume optimisation is on, so re-enable it. + ActionController::Base.optimise_named_routes = true + + @rs = ::ActionController::Routing::RouteSet.new + @rs.draw {|m| m.connect ':controller/:action/:id' } + + ActionController::Routing.use_controllers! %w(content admin/user admin/news_feed) end - end - def test_route_with_parameter_shell - ActionController::Routing.with_controllers(['users', 'pages']) do - set.draw do |map| - map.connect 'page/:id', :controller => 'pages', :action => 'show', :id => /\d+/ - map.connect '/:controller/:action/:id' - end + def test_default_setup + assert_equal({:controller => "content", :action => 'index'}, rs.recognize_path("/content")) + assert_equal({:controller => "content", :action => 'list'}, rs.recognize_path("/content/list")) + assert_equal({:controller => "content", :action => 'show', :id => '10'}, rs.recognize_path("/content/show/10")) - assert_equal({:controller => 'pages', :action => 'index'}, set.recognize_path('/pages')) - assert_equal({:controller => 'pages', :action => 'index'}, set.recognize_path('/pages/index')) - assert_equal({:controller => 'pages', :action => 'list'}, set.recognize_path('/pages/list')) + assert_equal({:controller => "admin/user", :action => 'show', :id => '10'}, rs.recognize_path("/admin/user/show/10")) - assert_equal({:controller => 'pages', :action => 'show', :id => '10'}, set.recognize_path('/pages/show/10')) - assert_equal({:controller => 'pages', :action => 'show', :id => '10'}, set.recognize_path('/page/10')) + assert_equal '/admin/user/show/10', rs.generate(:controller => 'admin/user', :action => 'show', :id => 10) + + assert_equal '/admin/user/show', rs.generate({:action => 'show'}, {:controller => 'admin/user', :action => 'list', :id => '10'}) + assert_equal '/admin/user/list/10', rs.generate({}, {:controller => 'admin/user', :action => 'list', :id => '10'}) + + assert_equal '/admin/stuff', rs.generate({:controller => 'stuff'}, {:controller => 'admin/user', :action => 'list', :id => '10'}) + assert_equal '/stuff', rs.generate({:controller => '/stuff'}, {:controller => 'admin/user', :action => 'list', :id => '10'}) end - end - def test_route_requirements_with_anchor_chars_are_invalid - assert_raises ArgumentError do - set.draw do |map| - map.connect 'page/:id', :controller => 'pages', :action => 'show', :id => /^\d+/ - end + def test_ignores_leading_slash + @rs.draw {|m| m.connect '/:controller/:action/:id'} + test_default_setup end - assert_raises ArgumentError do - set.draw do |map| - map.connect 'page/:id', :controller => 'pages', :action => 'show', :id => /\A\d+/ + + def test_time_recognition + # We create many routes to make situation more realistic + @rs = ::ActionController::Routing::RouteSet.new + @rs.draw { |map| + map.frontpage '', :controller => 'search', :action => 'new' + map.resources :videos do |video| + video.resources :comments + video.resource :file, :controller => 'video_file' + video.resource :share, :controller => 'video_shares' + video.resource :abuse, :controller => 'video_abuses' + end + map.resources :abuses, :controller => 'video_abuses' + map.resources :video_uploads + map.resources :video_visits + + map.resources :users do |user| + user.resource :settings + user.resources :videos + end + map.resources :channels do |channel| + channel.resources :videos, :controller => 'channel_videos' + end + map.resource :session + map.resource :lost_password + map.search 'search', :controller => 'search' + map.resources :pages + map.connect ':controller/:action/:id' + } + n = 1000 + if RunTimeTests + GC.start + rectime = Benchmark.realtime do + n.times do + rs.recognize_path("/videos/1234567", {:method => :get}) + rs.recognize_path("/videos/1234567/abuse", {:method => :get}) + rs.recognize_path("/users/1234567/settings", {:method => :get}) + rs.recognize_path("/channels/1234567", {:method => :get}) + rs.recognize_path("/session/new", {:method => :get}) + rs.recognize_path("/admin/user/show/10", {:method => :get}) + end + end + puts "\n\nRecognition (#{rs.routes.size} routes):" + per_url = rectime / (n * 6) + puts "#{per_url * 1000} ms/url" + puts "#{1 / per_url} url/s\n\n" end end - assert_raises ArgumentError do - set.draw do |map| - map.connect 'page/:id', :controller => 'pages', :action => 'show', :id => /\d+$/ + def test_time_generation + n = 5000 + if RunTimeTests + GC.start + pairs = [ + [{:controller => 'content', :action => 'index'}, {:controller => 'content', :action => 'show'}], + [{:controller => 'content'}, {:controller => 'content', :action => 'index'}], + [{:controller => 'content', :action => 'list'}, {:controller => 'content', :action => 'index'}], + [{:controller => 'content', :action => 'show', :id => '10'}, {:controller => 'content', :action => 'list'}], + [{:controller => 'admin/user', :action => 'index'}, {:controller => 'admin/user', :action => 'show'}], + [{:controller => 'admin/user'}, {:controller => 'admin/user', :action => 'index'}], + [{:controller => 'admin/user', :action => 'list'}, {:controller => 'admin/user', :action => 'index'}], + [{:controller => 'admin/user', :action => 'show', :id => '10'}, {:controller => 'admin/user', :action => 'list'}], + ] + p = nil + gentime = Benchmark.realtime do + n.times do + pairs.each {|(a, b)| rs.generate(a, b)} + end + end + + puts "\n\nGeneration (RouteSet): (#{(n * 8)} urls)" + per_url = gentime / (n * 8) + puts "#{per_url * 1000} ms/url" + puts "#{1 / per_url} url/s\n\n" end end - assert_raises ArgumentError do - set.draw do |map| - map.connect 'page/:id', :controller => 'pages', :action => 'show', :id => /\d+\Z/ + + def test_route_with_colon_first + rs.draw do |map| + map.connect '/:controller/:action/:id', :action => 'index', :id => nil + map.connect ':url', :controller => 'tiny_url', :action => 'translate' end end - assert_raises ArgumentError do - set.draw do |map| - map.connect 'page/:id', :controller => 'pages', :action => 'show', :id => /\d+\z/ + + def test_route_with_regexp_for_controller + rs.draw do |map| + map.connect ':controller/:admintoken/:action/:id', :controller => /admin\/.+/ + map.connect ':controller/:action/:id' end + assert_equal({:controller => "admin/user", :admintoken => "foo", :action => "index"}, + rs.recognize_path("/admin/user/foo")) + assert_equal({:controller => "content", :action => "foo"}, rs.recognize_path("/content/foo")) + assert_equal '/admin/user/foo', rs.generate(:controller => "admin/user", :admintoken => "foo", :action => "index") + assert_equal '/content/foo', rs.generate(:controller => "content", :action => "foo") end - assert_nothing_raised do - set.draw do |map| - map.connect 'page/:id', :controller => 'pages', :action => 'show', :id => /\d+/, :name => /^(david|jamis)/ - end - assert_raises ActionController::RoutingError do - set.generate :controller => 'pages', :action => 'show', :id => 10 + + def test_route_with_regexp_and_dot + rs.draw do |map| + map.connect ':controller/:action/:file', + :controller => /admin|user/, + :action => /upload|download/, + :defaults => {:file => nil}, + :requirements => {:file => %r{[^/]+(\.[^/]+)?}} end + # Without a file extension + assert_equal '/user/download/file', + rs.generate(:controller => "user", :action => "download", :file => "file") + assert_equal( + {:controller => "user", :action => "download", :file => "file"}, + rs.recognize_path("/user/download/file")) + + # Now, let's try a file with an extension, really a dot (.) + assert_equal '/user/download/file.jpg', + rs.generate( + :controller => "user", :action => "download", :file => "file.jpg") + assert_equal( + {:controller => "user", :action => "download", :file => "file.jpg"}, + rs.recognize_path("/user/download/file.jpg")) + end + + def test_basic_named_route + rs.add_named_route :home, '', :controller => 'content', :action => 'list' + x = setup_for_named_route + assert_equal("http://named.route.test/", + x.send(:home_url)) end - end - def test_non_path_route_requirements_match_all - set.draw do |map| - map.connect 'page/37s', :controller => 'pages', :action => 'show', :name => /(jamis|david)/ - end - assert_equal '/page/37s', set.generate(:controller => 'pages', :action => 'show', :name => 'jamis') - assert_raises ActionController::RoutingError do - set.generate(:controller => 'pages', :action => 'show', :name => 'not_jamis') - end - assert_raises ActionController::RoutingError do - set.generate(:controller => 'pages', :action => 'show', :name => 'nor_jamis_and_david') - end - end - - def test_recognize_with_encoded_id_and_regex - set.draw do |map| - map.connect 'page/:id', :controller => 'pages', :action => 'show', :id => /[a-zA-Z0-9\+]+/ + def test_basic_named_route_with_relative_url_root + rs.add_named_route :home, '', :controller => 'content', :action => 'list' + x = setup_for_named_route + x.relative_url_root="/foo" + assert_equal("http://named.route.test/foo/", + x.send(:home_url)) + assert_equal "/foo/", x.send(:home_path) end - assert_equal({:controller => 'pages', :action => 'show', :id => '10'}, set.recognize_path('/page/10')) - assert_equal({:controller => 'pages', :action => 'show', :id => 'hello+world'}, set.recognize_path('/page/hello+world')) - end - - def test_recognize_with_conditions - Object.const_set(:PeopleController, Class.new) - - set.draw do |map| - map.with_options(:controller => "people") do |people| - people.people "/people", :action => "index", :conditions => { :method => :get } - people.connect "/people", :action => "create", :conditions => { :method => :post } - people.person "/people/:id", :action => "show", :conditions => { :method => :get } - people.connect "/people/:id", :action => "update", :conditions => { :method => :put } - people.connect "/people/:id", :action => "destroy", :conditions => { :method => :delete } - end + def test_named_route_with_option + rs.add_named_route :page, 'page/:title', :controller => 'content', :action => 'show_page' + x = setup_for_named_route + assert_equal("http://named.route.test/page/new%20stuff", + x.send(:page_url, :title => 'new stuff')) end - request.path = "/people" - request.method = :get - assert_nothing_raised { set.recognize(request) } - assert_equal("index", request.path_parameters[:action]) - - request.method = :post - assert_nothing_raised { set.recognize(request) } - assert_equal("create", request.path_parameters[:action]) - - request.method = :put - assert_nothing_raised { set.recognize(request) } - assert_equal("update", request.path_parameters[:action]) + def test_named_route_with_default + rs.add_named_route :page, 'page/:title', :controller => 'content', :action => 'show_page', :title => 'AboutPage' + x = setup_for_named_route + assert_equal("http://named.route.test/page/AboutRails", + x.send(:page_url, :title => "AboutRails")) - begin - request.method = :bacon - set.recognize(request) - flunk 'Should have raised NotImplemented' - rescue ActionController::NotImplemented => e - assert_equal [:get, :post, :put, :delete], e.allowed_methods end - request.path = "/people/5" - request.method = :get - assert_nothing_raised { set.recognize(request) } - assert_equal("show", request.path_parameters[:action]) - assert_equal("5", request.path_parameters[:id]) + def test_named_route_with_nested_controller + rs.add_named_route :users, 'admin/user', :controller => 'admin/user', :action => 'index' + x = setup_for_named_route + assert_equal("http://named.route.test/admin/user", + x.send(:users_url)) + end - request.method = :put - assert_nothing_raised { set.recognize(request) } - assert_equal("update", request.path_parameters[:action]) - assert_equal("5", request.path_parameters[:id]) + def test_optimised_named_route_call_never_uses_url_for + rs.add_named_route :users, 'admin/user', :controller => '/admin/user', :action => 'index' + rs.add_named_route :user, 'admin/user/:id', :controller=>'/admin/user', :action=>'show' + x = setup_for_named_route + x.expects(:url_for).never + x.send(:users_url) + x.send(:users_path) + x.send(:user_url, 2, :foo=>"bar") + x.send(:user_path, 3, :bar=>"foo") + end + + def test_optimised_named_route_with_host + rs.add_named_route :pages, 'pages', :controller => 'content', :action => 'show_page', :host => 'foo.com' + x = setup_for_named_route + x.expects(:url_for).with(:host => 'foo.com', :only_path => false, :controller => 'content', :action => 'show_page', :use_route => :pages).once + x.send(:pages_url) + end + + def setup_for_named_route + klass = Class.new(MockController) + rs.install_helpers(klass) + klass.new(rs) + end + + def test_named_route_without_hash + rs.draw do |map| + map.normal ':controller/:action/:id' + end + end + + def test_named_route_root + rs.draw do |map| + map.root :controller => "hello" + end + x = setup_for_named_route + assert_equal("http://named.route.test/", x.send(:root_url)) + assert_equal("/", x.send(:root_path)) + end + + def test_named_route_with_regexps + rs.draw do |map| + map.article 'page/:year/:month/:day/:title', :controller => 'page', :action => 'show', + :year => /\d+/, :month => /\d+/, :day => /\d+/ + map.connect ':controller/:action/:id' + end + x = setup_for_named_route + # assert_equal( + # {:controller => 'page', :action => 'show', :title => 'hi', :use_route => :article, :only_path => false}, + # x.send(:article_url, :title => 'hi') + # ) + assert_equal( + "http://named.route.test/page/2005/6/10/hi", + x.send(:article_url, :title => 'hi', :day => 10, :year => 2005, :month => 6) + ) + end + + def test_changing_controller + assert_equal '/admin/stuff/show/10', rs.generate( + {:controller => 'stuff', :action => 'show', :id => 10}, + {:controller => 'admin/user', :action => 'index'} + ) + end + + def test_paths_escaped + rs.draw do |map| + map.path 'file/*path', :controller => 'content', :action => 'show_file' + map.connect ':controller/:action/:id' + end + + # No + to space in URI escaping, only for query params. + results = rs.recognize_path "/file/hello+world/how+are+you%3F" + assert results, "Recognition should have succeeded" + assert_equal ['hello+world', 'how+are+you?'], results[:path] + + # Use %20 for space instead. + results = rs.recognize_path "/file/hello%20world/how%20are%20you%3F" + assert results, "Recognition should have succeeded" + assert_equal ['hello world', 'how are you?'], results[:path] + + results = rs.recognize_path "/file" + assert results, "Recognition should have succeeded" + assert_equal [], results[:path] + end + + def test_paths_slashes_unescaped_with_ordered_parameters + rs.add_named_route :path, '/file/*path', :controller => 'content' + + # No / to %2F in URI, only for query params. + x = setup_for_named_route + assert_equal("/file/hello/world", x.send(:path_path, 'hello/world')) + end + + def test_non_controllers_cannot_be_matched + rs.draw do |map| + map.connect ':controller/:action/:id' + end + assert_raises(ActionController::RoutingError) { rs.recognize_path("/not_a/show/10") } + end + + def test_paths_do_not_accept_defaults + assert_raises(ActionController::RoutingError) do + rs.draw do |map| + map.path 'file/*path', :controller => 'content', :action => 'show_file', :path => %w(fake default) + map.connect ':controller/:action/:id' + end + end + + rs.draw do |map| + map.path 'file/*path', :controller => 'content', :action => 'show_file', :path => [] + map.connect ':controller/:action/:id' + end + end + + def test_should_list_options_diff_when_routing_requirements_dont_match + rs.draw do |map| + map.post 'post/:id', :controller=> 'post', :action=> 'show', :requirements => {:id => /\d+/} + end + exception = assert_raise(ActionController::RoutingError) { rs.generate(:controller => 'post', :action => 'show', :bad_param => "foo", :use_route => "post") } + assert_match /^post_url failed to generate/, exception.message + from_match = exception.message.match(/from \{[^\}]+\}/).to_s + assert_match /:bad_param=>"foo"/, from_match + assert_match /:action=>"show"/, from_match + assert_match /:controller=>"post"/, from_match + + expected_match = exception.message.match(/expected: \{[^\}]+\}/).to_s + assert_no_match /:bad_param=>"foo"/, expected_match + assert_match /:action=>"show"/, expected_match + assert_match /:controller=>"post"/, expected_match + + diff_match = exception.message.match(/diff: \{[^\}]+\}/).to_s + assert_match /:bad_param=>"foo"/, diff_match + assert_no_match /:action=>"show"/, diff_match + assert_no_match /:controller=>"post"/, diff_match + end + + # this specifies the case where your formerly would get a very confusing error message with an empty diff + def test_should_have_better_error_message_when_options_diff_is_empty + rs.draw do |map| + map.content '/content/:query', :controller => 'content', :action => 'show' + end + + exception = assert_raise(ActionController::RoutingError) { rs.generate(:controller => 'content', :action => 'show', :use_route => "content") } + assert_match %r[:action=>"show"], exception.message + assert_match %r[:controller=>"content"], exception.message + assert_match %r[you may have ambiguous routes, or you may need to supply additional parameters for this route], exception.message + assert_match %r[content_url has the following required parameters: \["content", :query\] - are they all satisfied?], exception.message + end + + def test_dynamic_path_allowed + rs.draw do |map| + map.connect '*path', :controller => 'content', :action => 'show_file' + end + + assert_equal '/pages/boo', rs.generate(:controller => 'content', :action => 'show_file', :path => %w(pages boo)) + end + + def test_dynamic_recall_paths_allowed + rs.draw do |map| + map.connect '*path', :controller => 'content', :action => 'show_file' + end + + recall_path = ActionController::Routing::PathSegment::Result.new(%w(pages boo)) + assert_equal '/pages/boo', rs.generate({}, :controller => 'content', :action => 'show_file', :path => recall_path) + end + + def test_backwards + rs.draw do |map| + map.connect 'page/:id/:action', :controller => 'pages', :action => 'show' + map.connect ':controller/:action/:id' + end + + assert_equal '/page/20', rs.generate({:id => 20}, {:controller => 'pages', :action => 'show'}) + assert_equal '/page/20', rs.generate(:controller => 'pages', :id => 20, :action => 'show') + assert_equal '/pages/boo', rs.generate(:controller => 'pages', :action => 'boo') + end + + def test_route_with_fixnum_default + rs.draw do |map| + map.connect 'page/:id', :controller => 'content', :action => 'show_page', :id => 1 + map.connect ':controller/:action/:id' + end + + assert_equal '/page', rs.generate(:controller => 'content', :action => 'show_page') + assert_equal '/page', rs.generate(:controller => 'content', :action => 'show_page', :id => 1) + assert_equal '/page', rs.generate(:controller => 'content', :action => 'show_page', :id => '1') + assert_equal '/page/10', rs.generate(:controller => 'content', :action => 'show_page', :id => 10) + + assert_equal({:controller => "content", :action => 'show_page', :id => '1'}, rs.recognize_path("/page")) + assert_equal({:controller => "content", :action => 'show_page', :id => '1'}, rs.recognize_path("/page/1")) + assert_equal({:controller => "content", :action => 'show_page', :id => '10'}, rs.recognize_path("/page/10")) + end + + # For newer revision + def test_route_with_text_default + rs.draw do |map| + map.connect 'page/:id', :controller => 'content', :action => 'show_page', :id => 1 + map.connect ':controller/:action/:id' + end + + assert_equal '/page/foo', rs.generate(:controller => 'content', :action => 'show_page', :id => 'foo') + assert_equal({:controller => "content", :action => 'show_page', :id => 'foo'}, rs.recognize_path("/page/foo")) + + token = "\321\202\320\265\320\272\321\201\321\202" # 'text' in russian + escaped_token = CGI::escape(token) + + assert_equal '/page/' + escaped_token, rs.generate(:controller => 'content', :action => 'show_page', :id => token) + assert_equal({:controller => "content", :action => 'show_page', :id => token}, rs.recognize_path("/page/#{escaped_token}")) + end + + def test_action_expiry + assert_equal '/content', rs.generate({:controller => 'content'}, {:controller => 'content', :action => 'show'}) + end + + def test_recognition_with_uppercase_controller_name + assert_equal({:controller => "content", :action => 'index'}, rs.recognize_path("/Content")) + assert_equal({:controller => "content", :action => 'list'}, rs.recognize_path("/ConTent/list")) + assert_equal({:controller => "content", :action => 'show', :id => '10'}, rs.recognize_path("/CONTENT/show/10")) + + # these used to work, before the routes rewrite, but support for this was pulled in the new version... + #assert_equal({'controller' => "admin/news_feed", 'action' => 'index'}, rs.recognize_path("Admin/NewsFeed")) + #assert_equal({'controller' => "admin/news_feed", 'action' => 'index'}, rs.recognize_path("Admin/News_Feed")) + end + + def test_requirement_should_prevent_optional_id + rs.draw do |map| + map.post 'post/:id', :controller=> 'post', :action=> 'show', :requirements => {:id => /\d+/} + end + + assert_equal '/post/10', rs.generate(:controller => 'post', :action => 'show', :id => 10) + + assert_raises ActionController::RoutingError do + rs.generate(:controller => 'post', :action => 'show') + end + end + + def test_both_requirement_and_optional + rs.draw do |map| + map.blog('test/:year', :controller => 'post', :action => 'show', + :defaults => { :year => nil }, + :requirements => { :year => /\d{4}/ } + ) + map.connect ':controller/:action/:id' + end + + assert_equal '/test', rs.generate(:controller => 'post', :action => 'show') + assert_equal '/test', rs.generate(:controller => 'post', :action => 'show', :year => nil) + + x = setup_for_named_route + assert_equal("http://named.route.test/test", + x.send(:blog_url)) + end + + def test_set_to_nil_forgets + rs.draw do |map| + map.connect 'pages/:year/:month/:day', :controller => 'content', :action => 'list_pages', :month => nil, :day => nil + map.connect ':controller/:action/:id' + end + + assert_equal '/pages/2005', + rs.generate(:controller => 'content', :action => 'list_pages', :year => 2005) + assert_equal '/pages/2005/6', + rs.generate(:controller => 'content', :action => 'list_pages', :year => 2005, :month => 6) + assert_equal '/pages/2005/6/12', + rs.generate(:controller => 'content', :action => 'list_pages', :year => 2005, :month => 6, :day => 12) + + assert_equal '/pages/2005/6/4', + rs.generate({:day => 4}, {:controller => 'content', :action => 'list_pages', :year => '2005', :month => '6', :day => '12'}) + + assert_equal '/pages/2005/6', + rs.generate({:day => nil}, {:controller => 'content', :action => 'list_pages', :year => '2005', :month => '6', :day => '12'}) + + assert_equal '/pages/2005', + rs.generate({:day => nil, :month => nil}, {:controller => 'content', :action => 'list_pages', :year => '2005', :month => '6', :day => '12'}) + end + + def test_url_with_no_action_specified + rs.draw do |map| + map.connect '', :controller => 'content' + map.connect ':controller/:action/:id' + end + + assert_equal '/', rs.generate(:controller => 'content', :action => 'index') + assert_equal '/', rs.generate(:controller => 'content') + end + + def test_named_url_with_no_action_specified + rs.draw do |map| + map.home '', :controller => 'content' + map.connect ':controller/:action/:id' + end + + assert_equal '/', rs.generate(:controller => 'content', :action => 'index') + assert_equal '/', rs.generate(:controller => 'content') + + x = setup_for_named_route + assert_equal("http://named.route.test/", + x.send(:home_url)) + end + + def test_url_generated_when_forgetting_action + [{:controller => 'content', :action => 'index'}, {:controller => 'content'}].each do |hash| + rs.draw do |map| + map.home '', hash + map.connect ':controller/:action/:id' + end + assert_equal '/', rs.generate({:action => nil}, {:controller => 'content', :action => 'hello'}) + assert_equal '/', rs.generate({:controller => 'content'}) + assert_equal '/content/hi', rs.generate({:controller => 'content', :action => 'hi'}) + end + end + + def test_named_route_method + rs.draw do |map| + map.categories 'categories', :controller => 'content', :action => 'categories' + map.connect ':controller/:action/:id' + end + + assert_equal '/categories', rs.generate(:controller => 'content', :action => 'categories') + assert_equal '/content/hi', rs.generate({:controller => 'content', :action => 'hi'}) + end + + def test_named_routes_array + test_named_route_method + assert_equal [:categories], rs.named_routes.names + end + + def test_nil_defaults + rs.draw do |map| + map.connect 'journal', + :controller => 'content', + :action => 'list_journal', + :date => nil, :user_id => nil + map.connect ':controller/:action/:id' + end + + assert_equal '/journal', rs.generate(:controller => 'content', :action => 'list_journal', :date => nil, :user_id => nil) + end + + def setup_request_method_routes_for(method) + @request = ActionController::TestRequest.new + @request.env["REQUEST_METHOD"] = method + @request.request_uri = "/match" + + rs.draw do |r| + r.connect '/match', :controller => 'books', :action => 'get', :conditions => { :method => :get } + r.connect '/match', :controller => 'books', :action => 'post', :conditions => { :method => :post } + r.connect '/match', :controller => 'books', :action => 'put', :conditions => { :method => :put } + r.connect '/match', :controller => 'books', :action => 'delete', :conditions => { :method => :delete } + end + end + + %w(GET POST PUT DELETE).each do |request_method| + define_method("test_request_method_recognized_with_#{request_method}") do + begin + Object.const_set(:BooksController, Class.new(ActionController::Base)) + + setup_request_method_routes_for(request_method) + + assert_nothing_raised { rs.recognize(@request) } + assert_equal request_method.downcase, @request.path_parameters[:action] + ensure + Object.send(:remove_const, :BooksController) rescue nil + end + end + end + + def test_subpath_recognized + Object.const_set(:SubpathBooksController, Class.new(ActionController::Base)) + + rs.draw do |r| + r.connect '/books/:id/edit', :controller => 'subpath_books', :action => 'edit' + r.connect '/items/:id/:action', :controller => 'subpath_books' + r.connect '/posts/new/:action', :controller => 'subpath_books' + r.connect '/posts/:id', :controller => 'subpath_books', :action => "show" + end + + hash = rs.recognize_path "/books/17/edit" + assert_not_nil hash + assert_equal %w(subpath_books 17 edit), [hash[:controller], hash[:id], hash[:action]] + + hash = rs.recognize_path "/items/3/complete" + assert_not_nil hash + assert_equal %w(subpath_books 3 complete), [hash[:controller], hash[:id], hash[:action]] + + hash = rs.recognize_path "/posts/new/preview" + assert_not_nil hash + assert_equal %w(subpath_books preview), [hash[:controller], hash[:action]] + + hash = rs.recognize_path "/posts/7" + assert_not_nil hash + assert_equal %w(subpath_books show 7), [hash[:controller], hash[:action], hash[:id]] + ensure + Object.send(:remove_const, :SubpathBooksController) rescue nil + end + + def test_subpath_generated + Object.const_set(:SubpathBooksController, Class.new(ActionController::Base)) + + rs.draw do |r| + r.connect '/books/:id/edit', :controller => 'subpath_books', :action => 'edit' + r.connect '/items/:id/:action', :controller => 'subpath_books' + r.connect '/posts/new/:action', :controller => 'subpath_books' + end + + assert_equal "/books/7/edit", rs.generate(:controller => "subpath_books", :id => 7, :action => "edit") + assert_equal "/items/15/complete", rs.generate(:controller => "subpath_books", :id => 15, :action => "complete") + assert_equal "/posts/new/preview", rs.generate(:controller => "subpath_books", :action => "preview") + ensure + Object.send(:remove_const, :SubpathBooksController) rescue nil + end + + def test_failed_requirements_raises_exception_with_violated_requirements + rs.draw do |r| + r.foo_with_requirement 'foos/:id', :controller=>'foos', :requirements=>{:id=>/\d+/} + end + + x = setup_for_named_route + assert_raises(ActionController::RoutingError) do + x.send(:foo_with_requirement_url, "I am Against the requirements") + end + end + end + + class RouteTest < Test::Unit::TestCase + def setup + @route = ROUTING::Route.new + end + + def slash_segment(is_optional = false) + returning ROUTING::DividerSegment.new('/') do |s| + s.is_optional = is_optional + end + end + + def default_route + unless defined?(@default_route) + @default_route = ROUTING::Route.new + + @default_route.segments << (s = ROUTING::StaticSegment.new) + s.value = '/' + s.raw = true + + @default_route.segments << (s = ROUTING::DynamicSegment.new) + s.key = :controller + + @default_route.segments << slash_segment(:optional) + @default_route.segments << (s = ROUTING::DynamicSegment.new) + s.key = :action + s.default = 'index' + s.is_optional = true + + @default_route.segments << slash_segment(:optional) + @default_route.segments << (s = ROUTING::DynamicSegment.new) + s.key = :id + s.is_optional = true + + @default_route.segments << slash_segment(:optional) + end + @default_route + end + + def test_default_route_recognition + expected = {:controller => 'accounts', :action => 'show', :id => '10'} + assert_equal expected, default_route.recognize('/accounts/show/10') + assert_equal expected, default_route.recognize('/accounts/show/10/') + + expected[:id] = 'jamis' + assert_equal expected, default_route.recognize('/accounts/show/jamis/') + + expected.delete :id + assert_equal expected, default_route.recognize('/accounts/show') + assert_equal expected, default_route.recognize('/accounts/show/') + + expected[:action] = 'index' + assert_equal expected, default_route.recognize('/accounts/') + assert_equal expected, default_route.recognize('/accounts') + + assert_equal nil, default_route.recognize('/') + assert_equal nil, default_route.recognize('/accounts/how/goood/it/is/to/be/free') + end + + def test_default_route_should_omit_default_action + o = {:controller => 'accounts', :action => 'index'} + assert_equal '/accounts', default_route.generate(o, o, {}) + end + + def test_default_route_should_include_default_action_when_id_present + o = {:controller => 'accounts', :action => 'index', :id => '20'} + assert_equal '/accounts/index/20', default_route.generate(o, o, {}) + end + + def test_default_route_should_work_with_action_but_no_id + o = {:controller => 'accounts', :action => 'list_all'} + assert_equal '/accounts/list_all', default_route.generate(o, o, {}) + end + + def test_default_route_should_uri_escape_pluses + expected = { :controller => 'accounts', :action => 'show', :id => 'hello world' } + assert_equal expected, default_route.recognize('/accounts/show/hello world') + assert_equal expected, default_route.recognize('/accounts/show/hello%20world') + assert_equal '/accounts/show/hello%20world', default_route.generate(expected, expected, {}) + + expected[:id] = 'hello+world' + assert_equal expected, default_route.recognize('/accounts/show/hello+world') + assert_equal expected, default_route.recognize('/accounts/show/hello%2Bworld') + assert_equal '/accounts/show/hello+world', default_route.generate(expected, expected, {}) + end + + def test_matches_controller_and_action + # requirement_for should only be called for the action and controller _once_ + @route.expects(:requirement_for).with(:controller).times(1).returns('pages') + @route.expects(:requirement_for).with(:action).times(1).returns('show') + + @route.requirements = {:controller => 'pages', :action => 'show'} + assert @route.matches_controller_and_action?('pages', 'show') + assert !@route.matches_controller_and_action?('not_pages', 'show') + assert !@route.matches_controller_and_action?('pages', 'not_show') + end + + def test_parameter_shell + page_url = ROUTING::Route.new + page_url.requirements = {:controller => 'pages', :action => 'show', :id => /\d+/} + assert_equal({:controller => 'pages', :action => 'show'}, page_url.parameter_shell) + end + + def test_defaults + route = ROUTING::RouteBuilder.new.build '/users/:id.:format', :controller => "users", :action => "show", :format => "html" + assert_equal( + { :controller => "users", :action => "show", :format => "html" }, + route.defaults) + end + + def test_builder_complains_without_controller + assert_raises(ArgumentError) do + ROUTING::RouteBuilder.new.build '/contact', :contoller => "contact", :action => "index" + end + end + + def test_significant_keys_for_default_route + keys = default_route.significant_keys.sort_by {|k| k.to_s } + assert_equal [:action, :controller, :id], keys + end + + def test_significant_keys + user_url = ROUTING::Route.new + user_url.segments << (s = ROUTING::StaticSegment.new) + s.value = '/' + s.raw = true + + user_url.segments << (s = ROUTING::StaticSegment.new) + s.value = 'user' + + user_url.segments << (s = ROUTING::StaticSegment.new) + s.value = '/' + s.raw = true + s.is_optional = true + + user_url.segments << (s = ROUTING::DynamicSegment.new) + s.key = :user + + user_url.segments << (s = ROUTING::StaticSegment.new) + s.value = '/' + s.raw = true + s.is_optional = true + + user_url.requirements = {:controller => 'users', :action => 'show'} + + keys = user_url.significant_keys.sort_by { |k| k.to_s } + assert_equal [:action, :controller, :user], keys + end + + def test_build_empty_query_string + assert_equal '', @route.build_query_string({}) + end + + def test_build_query_string_with_nil_value + assert_equal '', @route.build_query_string({:x => nil}) + end + + def test_simple_build_query_string + assert_equal '?x=1&y=2', order_query_string(@route.build_query_string(:x => '1', :y => '2')) + end + + def test_convert_ints_build_query_string + assert_equal '?x=1&y=2', order_query_string(@route.build_query_string(:x => 1, :y => 2)) + end + + def test_escape_spaces_build_query_string + assert_equal '?x=hello+world&y=goodbye+world', order_query_string(@route.build_query_string(:x => 'hello world', :y => 'goodbye world')) + end + + def test_expand_array_build_query_string + assert_equal '?x%5B%5D=1&x%5B%5D=2', order_query_string(@route.build_query_string(:x => [1, 2])) + end + + def test_escape_spaces_build_query_string_selected_keys + assert_equal '?x=hello+world', order_query_string(@route.build_query_string({:x => 'hello world', :y => 'goodbye world'}, [:x])) + end + + private + def order_query_string(qs) + '?' + qs[1..-1].split('&').sort.join('&') + end + end + + class RouteSetTest < Test::Unit::TestCase + def set + @set ||= ROUTING::RouteSet.new + end + + def request + @request ||= MockRequest.new(:host => "named.routes.test", :method => :get) + end + + def test_generate_extras + set.draw { |m| m.connect ':controller/:action/:id' } + path, extras = set.generate_extras(:controller => "foo", :action => "bar", :id => 15, :this => "hello", :that => "world") + assert_equal "/foo/bar/15", path + assert_equal %w(that this), extras.map(&:to_s).sort + end + + def test_extra_keys + set.draw { |m| m.connect ':controller/:action/:id' } + extras = set.extra_keys(:controller => "foo", :action => "bar", :id => 15, :this => "hello", :that => "world") + assert_equal %w(that this), extras.map(&:to_s).sort + end + + def test_generate_extras_not_first + set.draw do |map| + map.connect ':controller/:action/:id.:format' + map.connect ':controller/:action/:id' + end + path, extras = set.generate_extras(:controller => "foo", :action => "bar", :id => 15, :this => "hello", :that => "world") + assert_equal "/foo/bar/15", path + assert_equal %w(that this), extras.map(&:to_s).sort + end + + def test_generate_not_first + set.draw do |map| + map.connect ':controller/:action/:id.:format' + map.connect ':controller/:action/:id' + end + assert_equal "/foo/bar/15?this=hello", set.generate(:controller => "foo", :action => "bar", :id => 15, :this => "hello") + end + + def test_extra_keys_not_first + set.draw do |map| + map.connect ':controller/:action/:id.:format' + map.connect ':controller/:action/:id' + end + extras = set.extra_keys(:controller => "foo", :action => "bar", :id => 15, :this => "hello", :that => "world") + assert_equal %w(that this), extras.map(&:to_s).sort + end + + def test_draw + assert_equal 0, set.routes.size + set.draw do |map| + map.connect '/hello/world', :controller => 'a', :action => 'b' + end + assert_equal 1, set.routes.size + end + + def test_named_draw + assert_equal 0, set.routes.size + set.draw do |map| + map.hello '/hello/world', :controller => 'a', :action => 'b' + end + assert_equal 1, set.routes.size + assert_equal set.routes.first, set.named_routes[:hello] + end + + def test_later_named_routes_take_precedence + set.draw do |map| + map.hello '/hello/world', :controller => 'a', :action => 'b' + map.hello '/hello', :controller => 'a', :action => 'b' + end + assert_equal set.routes.last, set.named_routes[:hello] + end + + def setup_named_route_test + set.draw do |map| + map.show '/people/:id', :controller => 'people', :action => 'show' + map.index '/people', :controller => 'people', :action => 'index' + map.multi '/people/go/:foo/:bar/joe/:id', :controller => 'people', :action => 'multi' + map.users '/admin/users', :controller => 'admin/users', :action => 'index' + end + + klass = Class.new(MockController) + set.install_helpers(klass) + klass.new(set) + end + + def test_named_route_hash_access_method + controller = setup_named_route_test + + assert_equal( + { :controller => 'people', :action => 'show', :id => 5, :use_route => :show, :only_path => false }, + controller.send(:hash_for_show_url, :id => 5)) + + assert_equal( + { :controller => 'people', :action => 'index', :use_route => :index, :only_path => false }, + controller.send(:hash_for_index_url)) + + assert_equal( + { :controller => 'people', :action => 'show', :id => 5, :use_route => :show, :only_path => true }, + controller.send(:hash_for_show_path, :id => 5) + ) + end + + def test_named_route_url_method + controller = setup_named_route_test + + assert_equal "http://named.route.test/people/5", controller.send(:show_url, :id => 5) + assert_equal "/people/5", controller.send(:show_path, :id => 5) + + assert_equal "http://named.route.test/people", controller.send(:index_url) + assert_equal "/people", controller.send(:index_path) + + assert_equal "http://named.route.test/admin/users", controller.send(:users_url) + assert_equal '/admin/users', controller.send(:users_path) + assert_equal '/admin/users', set.generate(controller.send(:hash_for_users_url), {:controller => 'users', :action => 'index'}) + end + + def test_named_route_url_method_with_anchor + controller = setup_named_route_test + + assert_equal "http://named.route.test/people/5#location", controller.send(:show_url, :id => 5, :anchor => 'location') + assert_equal "/people/5#location", controller.send(:show_path, :id => 5, :anchor => 'location') + + assert_equal "http://named.route.test/people#location", controller.send(:index_url, :anchor => 'location') + assert_equal "/people#location", controller.send(:index_path, :anchor => 'location') + + assert_equal "http://named.route.test/admin/users#location", controller.send(:users_url, :anchor => 'location') + assert_equal '/admin/users#location', controller.send(:users_path, :anchor => 'location') + + assert_equal "http://named.route.test/people/go/7/hello/joe/5#location", + controller.send(:multi_url, 7, "hello", 5, :anchor => 'location') + + assert_equal "http://named.route.test/people/go/7/hello/joe/5?baz=bar#location", + controller.send(:multi_url, 7, "hello", 5, :baz => "bar", :anchor => 'location') + + assert_equal "http://named.route.test/people?baz=bar#location", + controller.send(:index_url, :baz => "bar", :anchor => 'location') + end + + def test_named_route_url_method_with_port + controller = setup_named_route_test + assert_equal "http://named.route.test:8080/people/5", controller.send(:show_url, 5, :port=>8080) + end + + def test_named_route_url_method_with_host + controller = setup_named_route_test + assert_equal "http://some.example.com/people/5", controller.send(:show_url, 5, :host=>"some.example.com") + end + + def test_named_route_url_method_with_ordered_parameters + controller = setup_named_route_test + assert_equal "http://named.route.test/people/go/7/hello/joe/5", + controller.send(:multi_url, 7, "hello", 5) + end + + def test_named_route_url_method_with_ordered_parameters_and_hash + controller = setup_named_route_test + assert_equal "http://named.route.test/people/go/7/hello/joe/5?baz=bar", + controller.send(:multi_url, 7, "hello", 5, :baz => "bar") + end + + def test_named_route_url_method_with_no_positional_arguments + controller = setup_named_route_test + assert_equal "http://named.route.test/people?baz=bar", + controller.send(:index_url, :baz => "bar") + end + + def test_draw_default_route + ActionController::Routing.with_controllers(['users']) do + set.draw do |map| + map.connect '/:controller/:action/:id' + end + + assert_equal 1, set.routes.size + route = set.routes.first + + assert route.segments.last.optional? + + assert_equal '/users/show/10', set.generate(:controller => 'users', :action => 'show', :id => 10) + assert_equal '/users/index/10', set.generate(:controller => 'users', :id => 10) + + assert_equal({:controller => 'users', :action => 'index', :id => '10'}, set.recognize_path('/users/index/10')) + assert_equal({:controller => 'users', :action => 'index', :id => '10'}, set.recognize_path('/users/index/10/')) + end + end + + def test_draw_default_route_with_default_controller + ActionController::Routing.with_controllers(['users']) do + set.draw do |map| + map.connect '/:controller/:action/:id', :controller => 'users' + end + assert_equal({:controller => 'users', :action => 'index'}, set.recognize_path('/')) + end + end + + def test_route_with_parameter_shell + ActionController::Routing.with_controllers(['users', 'pages']) do + set.draw do |map| + map.connect 'page/:id', :controller => 'pages', :action => 'show', :id => /\d+/ + map.connect '/:controller/:action/:id' + end + + assert_equal({:controller => 'pages', :action => 'index'}, set.recognize_path('/pages')) + assert_equal({:controller => 'pages', :action => 'index'}, set.recognize_path('/pages/index')) + assert_equal({:controller => 'pages', :action => 'list'}, set.recognize_path('/pages/list')) + + assert_equal({:controller => 'pages', :action => 'show', :id => '10'}, set.recognize_path('/pages/show/10')) + assert_equal({:controller => 'pages', :action => 'show', :id => '10'}, set.recognize_path('/page/10')) + end + end + + def test_route_requirements_with_anchor_chars_are_invalid + assert_raises ArgumentError do + set.draw do |map| + map.connect 'page/:id', :controller => 'pages', :action => 'show', :id => /^\d+/ + end + end + assert_raises ArgumentError do + set.draw do |map| + map.connect 'page/:id', :controller => 'pages', :action => 'show', :id => /\A\d+/ + end + end + assert_raises ArgumentError do + set.draw do |map| + map.connect 'page/:id', :controller => 'pages', :action => 'show', :id => /\d+$/ + end + end + assert_raises ArgumentError do + set.draw do |map| + map.connect 'page/:id', :controller => 'pages', :action => 'show', :id => /\d+\Z/ + end + end + assert_raises ArgumentError do + set.draw do |map| + map.connect 'page/:id', :controller => 'pages', :action => 'show', :id => /\d+\z/ + end + end + assert_nothing_raised do + set.draw do |map| + map.connect 'page/:id', :controller => 'pages', :action => 'show', :id => /\d+/, :name => /^(david|jamis)/ + end + assert_raises ActionController::RoutingError do + set.generate :controller => 'pages', :action => 'show', :id => 10 + end + end + end + + def test_non_path_route_requirements_match_all + set.draw do |map| + map.connect 'page/37s', :controller => 'pages', :action => 'show', :name => /(jamis|david)/ + end + assert_equal '/page/37s', set.generate(:controller => 'pages', :action => 'show', :name => 'jamis') + assert_raises ActionController::RoutingError do + set.generate(:controller => 'pages', :action => 'show', :name => 'not_jamis') + end + assert_raises ActionController::RoutingError do + set.generate(:controller => 'pages', :action => 'show', :name => 'nor_jamis_and_david') + end + end + + def test_recognize_with_encoded_id_and_regex + set.draw do |map| + map.connect 'page/:id', :controller => 'pages', :action => 'show', :id => /[a-zA-Z0-9\+]+/ + end + + assert_equal({:controller => 'pages', :action => 'show', :id => '10'}, set.recognize_path('/page/10')) + assert_equal({:controller => 'pages', :action => 'show', :id => 'hello+world'}, set.recognize_path('/page/hello+world')) + end + + def test_recognize_with_conditions + Object.const_set(:PeopleController, Class.new) + + set.draw do |map| + map.with_options(:controller => "people") do |people| + people.people "/people", :action => "index", :conditions => { :method => :get } + people.connect "/people", :action => "create", :conditions => { :method => :post } + people.person "/people/:id", :action => "show", :conditions => { :method => :get } + people.connect "/people/:id", :action => "update", :conditions => { :method => :put } + people.connect "/people/:id", :action => "destroy", :conditions => { :method => :delete } + end + end - request.method = :delete - assert_nothing_raised { set.recognize(request) } - assert_equal("destroy", request.path_parameters[:action]) - assert_equal("5", request.path_parameters[:id]) + request.path = "/people" + request.method = :get + assert_nothing_raised { set.recognize(request) } + assert_equal("index", request.path_parameters[:action]) - begin request.method = :post - set.recognize(request) - flunk 'Should have raised MethodNotAllowed' - rescue ActionController::MethodNotAllowed => e - assert_equal [:get, :put, :delete], e.allowed_methods + assert_nothing_raised { set.recognize(request) } + assert_equal("create", request.path_parameters[:action]) + + request.method = :put + assert_nothing_raised { set.recognize(request) } + assert_equal("update", request.path_parameters[:action]) + + begin + request.method = :bacon + set.recognize(request) + flunk 'Should have raised NotImplemented' + rescue ActionController::NotImplemented => e + assert_equal [:get, :post, :put, :delete], e.allowed_methods + end + + request.path = "/people/5" + request.method = :get + assert_nothing_raised { set.recognize(request) } + assert_equal("show", request.path_parameters[:action]) + assert_equal("5", request.path_parameters[:id]) + + request.method = :put + assert_nothing_raised { set.recognize(request) } + assert_equal("update", request.path_parameters[:action]) + assert_equal("5", request.path_parameters[:id]) + + request.method = :delete + assert_nothing_raised { set.recognize(request) } + assert_equal("destroy", request.path_parameters[:action]) + assert_equal("5", request.path_parameters[:id]) + + begin + request.method = :post + set.recognize(request) + flunk 'Should have raised MethodNotAllowed' + rescue ActionController::MethodNotAllowed => e + assert_equal [:get, :put, :delete], e.allowed_methods + end + + ensure + Object.send(:remove_const, :PeopleController) end - ensure - Object.send(:remove_const, :PeopleController) - end - - def test_recognize_with_alias_in_conditions - Object.const_set(:PeopleController, Class.new) + def test_recognize_with_alias_in_conditions + Object.const_set(:PeopleController, Class.new) + + set.draw do |map| + map.people "/people", :controller => 'people', :action => "index", + :conditions => { :method => :get } + map.root :people + end - set.draw do |map| - map.people "/people", :controller => 'people', :action => "index", - :conditions => { :method => :get } - map.root :people + request.path = "/people" + request.method = :get + assert_nothing_raised { set.recognize(request) } + assert_equal("people", request.path_parameters[:controller]) + assert_equal("index", request.path_parameters[:action]) + + request.path = "/" + request.method = :get + assert_nothing_raised { set.recognize(request) } + assert_equal("people", request.path_parameters[:controller]) + assert_equal("index", request.path_parameters[:action]) + ensure + Object.send(:remove_const, :PeopleController) end - request.path = "/people" - request.method = :get - assert_nothing_raised { set.recognize(request) } - assert_equal("people", request.path_parameters[:controller]) - assert_equal("index", request.path_parameters[:action]) + def test_typo_recognition + Object.const_set(:ArticlesController, Class.new) - request.path = "/" - request.method = :get - assert_nothing_raised { set.recognize(request) } - assert_equal("people", request.path_parameters[:controller]) - assert_equal("index", request.path_parameters[:action]) - ensure - Object.send(:remove_const, :PeopleController) - end - - def test_typo_recognition - Object.const_set(:ArticlesController, Class.new) - - set.draw do |map| - map.connect 'articles/:year/:month/:day/:title', - :controller => 'articles', :action => 'permalink', - :year => /\d{4}/, :day => /\d{1,2}/, :month => /\d{1,2}/ - end - - request.path = "/articles/2005/11/05/a-very-interesting-article" - request.method = :get - assert_nothing_raised { set.recognize(request) } - assert_equal("permalink", request.path_parameters[:action]) - assert_equal("2005", request.path_parameters[:year]) - assert_equal("11", request.path_parameters[:month]) - assert_equal("05", request.path_parameters[:day]) - assert_equal("a-very-interesting-article", request.path_parameters[:title]) - - ensure - Object.send(:remove_const, :ArticlesController) - end + set.draw do |map| + map.connect 'articles/:year/:month/:day/:title', + :controller => 'articles', :action => 'permalink', + :year => /\d{4}/, :day => /\d{1,2}/, :month => /\d{1,2}/ + end - def test_routing_traversal_does_not_load_extra_classes - assert !Object.const_defined?("Profiler__"), "Profiler should not be loaded" - set.draw do |map| - map.connect '/profile', :controller => 'profile' + request.path = "/articles/2005/11/05/a-very-interesting-article" + request.method = :get + assert_nothing_raised { set.recognize(request) } + assert_equal("permalink", request.path_parameters[:action]) + assert_equal("2005", request.path_parameters[:year]) + assert_equal("11", request.path_parameters[:month]) + assert_equal("05", request.path_parameters[:day]) + assert_equal("a-very-interesting-article", request.path_parameters[:title]) + + ensure + Object.send(:remove_const, :ArticlesController) end - request.path = '/profile' + def test_routing_traversal_does_not_load_extra_classes + assert !Object.const_defined?("Profiler__"), "Profiler should not be loaded" + set.draw do |map| + map.connect '/profile', :controller => 'profile' + end + + request.path = '/profile' - set.recognize(request) rescue nil - - assert !Object.const_defined?("Profiler__"), "Profiler should not be loaded" - end + set.recognize(request) rescue nil + + assert !Object.const_defined?("Profiler__"), "Profiler should not be loaded" + end - def test_recognize_with_conditions_and_format - Object.const_set(:PeopleController, Class.new) + def test_recognize_with_conditions_and_format + Object.const_set(:PeopleController, Class.new) - set.draw do |map| - map.with_options(:controller => "people") do |people| - people.person "/people/:id", :action => "show", :conditions => { :method => :get } - people.connect "/people/:id", :action => "update", :conditions => { :method => :put } - people.connect "/people/:id.:_format", :action => "show", :conditions => { :method => :get } + set.draw do |map| + map.with_options(:controller => "people") do |people| + people.person "/people/:id", :action => "show", :conditions => { :method => :get } + people.connect "/people/:id", :action => "update", :conditions => { :method => :put } + people.connect "/people/:id.:_format", :action => "show", :conditions => { :method => :get } + end end + + request.path = "/people/5" + request.method = :get + assert_nothing_raised { set.recognize(request) } + assert_equal("show", request.path_parameters[:action]) + assert_equal("5", request.path_parameters[:id]) + + request.method = :put + assert_nothing_raised { set.recognize(request) } + assert_equal("update", request.path_parameters[:action]) + + request.path = "/people/5.png" + request.method = :get + assert_nothing_raised { set.recognize(request) } + assert_equal("show", request.path_parameters[:action]) + assert_equal("5", request.path_parameters[:id]) + assert_equal("png", request.path_parameters[:_format]) + ensure + Object.send(:remove_const, :PeopleController) end - request.path = "/people/5" - request.method = :get - assert_nothing_raised { set.recognize(request) } - assert_equal("show", request.path_parameters[:action]) - assert_equal("5", request.path_parameters[:id]) + def test_generate_with_default_action + set.draw do |map| + map.connect "/people", :controller => "people" + map.connect "/people/list", :controller => "people", :action => "list" + end + + url = set.generate(:controller => "people", :action => "list") + assert_equal "/people/list", url + end - request.method = :put - assert_nothing_raised { set.recognize(request) } - assert_equal("update", request.path_parameters[:action]) + def test_root_map + Object.const_set(:PeopleController, Class.new) - request.path = "/people/5.png" - request.method = :get - assert_nothing_raised { set.recognize(request) } - assert_equal("show", request.path_parameters[:action]) - assert_equal("5", request.path_parameters[:id]) - assert_equal("png", request.path_parameters[:_format]) - ensure - Object.send(:remove_const, :PeopleController) - end + set.draw { |map| map.root :controller => "people" } - def test_generate_with_default_action - set.draw do |map| - map.connect "/people", :controller => "people" - map.connect "/people/list", :controller => "people", :action => "list" + request.path = "" + request.method = :get + assert_nothing_raised { set.recognize(request) } + assert_equal("people", request.path_parameters[:controller]) + assert_equal("index", request.path_parameters[:action]) + ensure + Object.send(:remove_const, :PeopleController) end - url = set.generate(:controller => "people", :action => "list") - assert_equal "/people/list", url - end - - def test_root_map - Object.const_set(:PeopleController, Class.new) + def test_namespace + Object.const_set(:Api, Module.new { |m| m.const_set(:ProductsController, Class.new) }) - set.draw { |map| map.root :controller => "people" } + set.draw do |map| - request.path = "" - request.method = :get - assert_nothing_raised { set.recognize(request) } - assert_equal("people", request.path_parameters[:controller]) - assert_equal("index", request.path_parameters[:action]) - ensure - Object.send(:remove_const, :PeopleController) - end - - - def test_namespace - Object.const_set(:Api, Module.new { |m| m.const_set(:ProductsController, Class.new) }) + map.namespace 'api' do |api| + api.route 'inventory', :controller => "products", :action => 'inventory' + end - set.draw do |map| - - map.namespace 'api' do |api| - api.route 'inventory', :controller => "products", :action => 'inventory' end - + + request.path = "/api/inventory" + request.method = :get + assert_nothing_raised { set.recognize(request) } + assert_equal("api/products", request.path_parameters[:controller]) + assert_equal("inventory", request.path_parameters[:action]) + ensure + Object.send(:remove_const, :Api) end - request.path = "/api/inventory" - request.method = :get - assert_nothing_raised { set.recognize(request) } - assert_equal("api/products", request.path_parameters[:controller]) - assert_equal("inventory", request.path_parameters[:action]) - ensure - Object.send(:remove_const, :Api) - end - + def test_namespaced_root_map + Object.const_set(:Api, Module.new { |m| m.const_set(:ProductsController, Class.new) }) - def test_namespaced_root_map - Object.const_set(:Api, Module.new { |m| m.const_set(:ProductsController, Class.new) }) + set.draw do |map| + + map.namespace 'api' do |api| + api.root :controller => "products" + end - set.draw do |map| - - map.namespace 'api' do |api| - api.root :controller => "products" end - + + request.path = "/api" + request.method = :get + assert_nothing_raised { set.recognize(request) } + assert_equal("api/products", request.path_parameters[:controller]) + assert_equal("index", request.path_parameters[:action]) + ensure + Object.send(:remove_const, :Api) end - request.path = "/api" - request.method = :get - assert_nothing_raised { set.recognize(request) } - assert_equal("api/products", request.path_parameters[:controller]) - assert_equal("index", request.path_parameters[:action]) - ensure - Object.send(:remove_const, :Api) - end + def test_generate_finds_best_fit + set.draw do |map| + map.connect "/people", :controller => "people", :action => "index" + map.connect "/ws/people", :controller => "people", :action => "index", :ws => true + end - def test_generate_finds_best_fit - set.draw do |map| - map.connect "/people", :controller => "people", :action => "index" - map.connect "/ws/people", :controller => "people", :action => "index", :ws => true + url = set.generate(:controller => "people", :action => "index", :ws => true) + assert_equal "/ws/people", url end - url = set.generate(:controller => "people", :action => "index", :ws => true) - assert_equal "/ws/people", url - end + def test_generate_changes_controller_module + set.draw { |map| map.connect ':controller/:action/:id' } + current = { :controller => "bling/bloop", :action => "bap", :id => 9 } + url = set.generate({:controller => "foo/bar", :action => "baz", :id => 7}, current) + assert_equal "/foo/bar/baz/7", url + end - def test_generate_changes_controller_module - set.draw { |map| map.connect ':controller/:action/:id' } - current = { :controller => "bling/bloop", :action => "bap", :id => 9 } - url = set.generate({:controller => "foo/bar", :action => "baz", :id => 7}, current) - assert_equal "/foo/bar/baz/7", url - end + def test_id_is_not_impossibly_sticky + set.draw do |map| + map.connect 'foo/:number', :controller => "people", :action => "index" + map.connect ':controller/:action/:id' + end - def test_id_is_not_impossibly_sticky - set.draw do |map| - map.connect 'foo/:number', :controller => "people", :action => "index" - map.connect ':controller/:action/:id' + url = set.generate({:controller => "people", :action => "index", :number => 3}, + {:controller => "people", :action => "index", :id => "21"}) + assert_equal "/foo/3", url end - url = set.generate({:controller => "people", :action => "index", :number => 3}, - {:controller => "people", :action => "index", :id => "21"}) - assert_equal "/foo/3", url - end + def test_id_is_sticky_when_it_ought_to_be + set.draw do |map| + map.connect ':controller/:id/:action' + end - def test_id_is_sticky_when_it_ought_to_be - set.draw do |map| - map.connect ':controller/:id/:action' + url = set.generate({:action => "destroy"}, {:controller => "people", :action => "show", :id => "7"}) + assert_equal "/people/7/destroy", url end - url = set.generate({:action => "destroy"}, {:controller => "people", :action => "show", :id => "7"}) - assert_equal "/people/7/destroy", url - end + def test_use_static_path_when_possible + set.draw do |map| + map.connect 'about', :controller => "welcome", :action => "about" + map.connect ':controller/:action/:id' + end - def test_use_static_path_when_possible - set.draw do |map| - map.connect 'about', :controller => "welcome", :action => "about" - map.connect ':controller/:action/:id' + url = set.generate({:controller => "welcome", :action => "about"}, + {:controller => "welcome", :action => "get", :id => "7"}) + assert_equal "/about", url end - url = set.generate({:controller => "welcome", :action => "about"}, - {:controller => "welcome", :action => "get", :id => "7"}) - assert_equal "/about", url - end + def test_generate + set.draw { |map| map.connect ':controller/:action/:id' } - def test_generate - set.draw { |map| map.connect ':controller/:action/:id' } + args = { :controller => "foo", :action => "bar", :id => "7", :x => "y" } + assert_equal "/foo/bar/7?x=y", set.generate(args) + assert_equal ["/foo/bar/7", [:x]], set.generate_extras(args) + assert_equal [:x], set.extra_keys(args) + end - args = { :controller => "foo", :action => "bar", :id => "7", :x => "y" } - assert_equal "/foo/bar/7?x=y", set.generate(args) - assert_equal ["/foo/bar/7", [:x]], set.generate_extras(args) - assert_equal [:x], set.extra_keys(args) - end + def test_named_routes_are_never_relative_to_modules + set.draw do |map| + map.connect "/connection/manage/:action", :controller => 'connection/manage' + map.connect "/connection/connection", :controller => "connection/connection" + map.family_connection "/connection", :controller => "connection" + end - def test_named_routes_are_never_relative_to_modules - set.draw do |map| - map.connect "/connection/manage/:action", :controller => 'connection/manage' - map.connect "/connection/connection", :controller => "connection/connection" - map.family_connection "/connection", :controller => "connection" - end + url = set.generate({:controller => "connection"}, {:controller => 'connection/manage'}) + assert_equal "/connection/connection", url - url = set.generate({:controller => "connection"}, {:controller => 'connection/manage'}) - assert_equal "/connection/connection", url + url = set.generate({:use_route => :family_connection, :controller => "connection"}, {:controller => 'connection/manage'}) + assert_equal "/connection", url + end - url = set.generate({:use_route => :family_connection, :controller => "connection"}, {:controller => 'connection/manage'}) - assert_equal "/connection", url - end - - def test_action_left_off_when_id_is_recalled - set.draw do |map| - map.connect ':controller/:action/:id' + def test_action_left_off_when_id_is_recalled + set.draw do |map| + map.connect ':controller/:action/:id' + end + assert_equal '/post', set.generate( + {:controller => 'post', :action => 'index'}, + {:controller => 'post', :action => 'show', :id => '10'} + ) end - assert_equal '/post', set.generate( - {:controller => 'post', :action => 'index'}, - {:controller => 'post', :action => 'show', :id => '10'} - ) - end - - def test_query_params_will_be_shown_when_recalled - set.draw do |map| - map.connect 'show_post/:parameter', :controller => 'post', :action => 'show' - map.connect ':controller/:action/:id' + + def test_query_params_will_be_shown_when_recalled + set.draw do |map| + map.connect 'show_post/:parameter', :controller => 'post', :action => 'show' + map.connect ':controller/:action/:id' + end + assert_equal '/post/edit?parameter=1', set.generate( + {:action => 'edit', :parameter => 1}, + {:controller => 'post', :action => 'show', :parameter => 1} + ) end - assert_equal '/post/edit?parameter=1', set.generate( - {:action => 'edit', :parameter => 1}, - {:controller => 'post', :action => 'show', :parameter => 1} - ) - end - def test_expiry_determination_should_consider_values_with_to_param - set.draw { |map| map.connect 'projects/:project_id/:controller/:action' } - assert_equal '/projects/1/post/show', set.generate( - {:action => 'show', :project_id => 1}, - {:controller => 'post', :action => 'show', :project_id => '1'}) - end + def test_expiry_determination_should_consider_values_with_to_param + set.draw { |map| map.connect 'projects/:project_id/:controller/:action' } + assert_equal '/projects/1/post/show', set.generate( + {:action => 'show', :project_id => 1}, + {:controller => 'post', :action => 'show', :project_id => '1'}) + end - def test_generate_all - set.draw do |map| - map.connect 'show_post/:id', :controller => 'post', :action => 'show' - map.connect ':controller/:action/:id' + def test_generate_all + set.draw do |map| + map.connect 'show_post/:id', :controller => 'post', :action => 'show' + map.connect ':controller/:action/:id' + end + all = set.generate( + {:action => 'show', :id => 10, :generate_all => true}, + {:controller => 'post', :action => 'show'} + ) + assert_equal 2, all.length + assert_equal '/show_post/10', all.first + assert_equal '/post/show/10', all.last end - all = set.generate( - {:action => 'show', :id => 10, :generate_all => true}, - {:controller => 'post', :action => 'show'} - ) - assert_equal 2, all.length - assert_equal '/show_post/10', all.first - assert_equal '/post/show/10', all.last - end - - def test_named_route_in_nested_resource - set.draw do |map| - map.resources :projects do |project| - project.milestones 'milestones', :controller => 'milestones', :action => 'index' - end - end - - request.path = "/projects/1/milestones" - request.method = :get - assert_nothing_raised { set.recognize(request) } - assert_equal("milestones", request.path_parameters[:controller]) - assert_equal("index", request.path_parameters[:action]) - end - - def test_setting_root_in_namespace_using_symbol - assert_nothing_raised do + + def test_named_route_in_nested_resource set.draw do |map| - map.namespace :admin do |admin| - admin.root :controller => 'home' + map.resources :projects do |project| + project.milestones 'milestones', :controller => 'milestones', :action => 'index' end end + + request.path = "/projects/1/milestones" + request.method = :get + assert_nothing_raised { set.recognize(request) } + assert_equal("milestones", request.path_parameters[:controller]) + assert_equal("index", request.path_parameters[:action]) end - end - - def test_setting_root_in_namespace_using_string - assert_nothing_raised do - set.draw do |map| - map.namespace 'admin' do |admin| - admin.root :controller => 'home' + + def test_setting_root_in_namespace_using_symbol + assert_nothing_raised do + set.draw do |map| + map.namespace :admin do |admin| + admin.root :controller => 'home' + end end end end - end - def test_route_requirements_with_unsupported_regexp_options_must_error - assert_raises ArgumentError do + def test_setting_root_in_namespace_using_string + assert_nothing_raised do + set.draw do |map| + map.namespace 'admin' do |admin| + admin.root :controller => 'home' + end + end + end + end + + def test_route_requirements_with_unsupported_regexp_options_must_error + assert_raises ArgumentError do + set.draw do |map| + map.connect 'page/:name', :controller => 'pages', + :action => 'show', + :requirements => {:name => /(david|jamis)/m} + end + end + end + + def test_route_requirements_with_supported_options_must_not_error + assert_nothing_raised do + set.draw do |map| + map.connect 'page/:name', :controller => 'pages', + :action => 'show', + :requirements => {:name => /(david|jamis)/i} + end + end + assert_nothing_raised do + set.draw do |map| + map.connect 'page/:name', :controller => 'pages', + :action => 'show', + :requirements => {:name => / # Desperately overcommented regexp + ( #Either + david #The Creator + | #Or + jamis #The Deployer + )/x} + end + end + end + + def test_route_requirement_recognize_with_ignore_case set.draw do |map| map.connect 'page/:name', :controller => 'pages', :action => 'show', - :requirements => {:name => /(david|jamis)/m} + :requirements => {:name => /(david|jamis)/i} + end + assert_equal({:controller => 'pages', :action => 'show', :name => 'jamis'}, set.recognize_path('/page/jamis')) + assert_raises ActionController::RoutingError do + set.recognize_path('/page/davidjamis') end + assert_equal({:controller => 'pages', :action => 'show', :name => 'DAVID'}, set.recognize_path('/page/DAVID')) end - end - def test_route_requirements_with_supported_options_must_not_error - assert_nothing_raised do + def test_route_requirement_generate_with_ignore_case set.draw do |map| map.connect 'page/:name', :controller => 'pages', :action => 'show', :requirements => {:name => /(david|jamis)/i} end + url = set.generate({:controller => 'pages', :action => 'show', :name => 'david'}) + assert_equal "/page/david", url + assert_raises ActionController::RoutingError do + url = set.generate({:controller => 'pages', :action => 'show', :name => 'davidjamis'}) + end + url = set.generate({:controller => 'pages', :action => 'show', :name => 'JAMIS'}) + assert_equal "/page/JAMIS", url end - assert_nothing_raised do + + def test_route_requirement_recognize_with_extended_syntax set.draw do |map| map.connect 'page/:name', :controller => 'pages', :action => 'show', @@ -2159,194 +2257,68 @@ def test_route_requirements_with_supported_options_must_not_error jamis #The Deployer )/x} end + assert_equal({:controller => 'pages', :action => 'show', :name => 'jamis'}, set.recognize_path('/page/jamis')) + assert_equal({:controller => 'pages', :action => 'show', :name => 'david'}, set.recognize_path('/page/david')) + assert_raises ActionController::RoutingError do + set.recognize_path('/page/david #The Creator') + end + assert_raises ActionController::RoutingError do + set.recognize_path('/page/David') + end end - end - - def test_route_requirement_recognize_with_ignore_case - set.draw do |map| - map.connect 'page/:name', :controller => 'pages', - :action => 'show', - :requirements => {:name => /(david|jamis)/i} - end - assert_equal({:controller => 'pages', :action => 'show', :name => 'jamis'}, set.recognize_path('/page/jamis')) - assert_raises ActionController::RoutingError do - set.recognize_path('/page/davidjamis') - end - assert_equal({:controller => 'pages', :action => 'show', :name => 'DAVID'}, set.recognize_path('/page/DAVID')) - end - - def test_route_requirement_generate_with_ignore_case - set.draw do |map| - map.connect 'page/:name', :controller => 'pages', - :action => 'show', - :requirements => {:name => /(david|jamis)/i} - end - url = set.generate({:controller => 'pages', :action => 'show', :name => 'david'}) - assert_equal "/page/david", url - assert_raises ActionController::RoutingError do - url = set.generate({:controller => 'pages', :action => 'show', :name => 'davidjamis'}) - end - url = set.generate({:controller => 'pages', :action => 'show', :name => 'JAMIS'}) - assert_equal "/page/JAMIS", url - end - def test_route_requirement_recognize_with_extended_syntax - set.draw do |map| - map.connect 'page/:name', :controller => 'pages', - :action => 'show', - :requirements => {:name => / # Desperately overcommented regexp - ( #Either - david #The Creator - | #Or - jamis #The Deployer - )/x} - end - assert_equal({:controller => 'pages', :action => 'show', :name => 'jamis'}, set.recognize_path('/page/jamis')) - assert_equal({:controller => 'pages', :action => 'show', :name => 'david'}, set.recognize_path('/page/david')) - assert_raises ActionController::RoutingError do - set.recognize_path('/page/david #The Creator') - end - assert_raises ActionController::RoutingError do - set.recognize_path('/page/David') + def test_route_requirement_generate_with_extended_syntax + set.draw do |map| + map.connect 'page/:name', :controller => 'pages', + :action => 'show', + :requirements => {:name => / # Desperately overcommented regexp + ( #Either + david #The Creator + | #Or + jamis #The Deployer + )/x} + end + url = set.generate({:controller => 'pages', :action => 'show', :name => 'david'}) + assert_equal "/page/david", url + assert_raises ActionController::RoutingError do + url = set.generate({:controller => 'pages', :action => 'show', :name => 'davidjamis'}) + end + assert_raises ActionController::RoutingError do + url = set.generate({:controller => 'pages', :action => 'show', :name => 'JAMIS'}) + end end - end - def test_route_requirement_generate_with_extended_syntax - set.draw do |map| - map.connect 'page/:name', :controller => 'pages', - :action => 'show', - :requirements => {:name => / # Desperately overcommented regexp - ( #Either - david #The Creator - | #Or - jamis #The Deployer - )/x} - end - url = set.generate({:controller => 'pages', :action => 'show', :name => 'david'}) - assert_equal "/page/david", url - assert_raises ActionController::RoutingError do - url = set.generate({:controller => 'pages', :action => 'show', :name => 'davidjamis'}) - end - assert_raises ActionController::RoutingError do + def test_route_requirement_generate_with_xi_modifiers + set.draw do |map| + map.connect 'page/:name', :controller => 'pages', + :action => 'show', + :requirements => {:name => / # Desperately overcommented regexp + ( #Either + david #The Creator + | #Or + jamis #The Deployer + )/xi} + end url = set.generate({:controller => 'pages', :action => 'show', :name => 'JAMIS'}) - end - end - - def test_route_requirement_generate_with_xi_modifiers - set.draw do |map| - map.connect 'page/:name', :controller => 'pages', - :action => 'show', - :requirements => {:name => / # Desperately overcommented regexp - ( #Either - david #The Creator - | #Or - jamis #The Deployer - )/xi} - end - url = set.generate({:controller => 'pages', :action => 'show', :name => 'JAMIS'}) - assert_equal "/page/JAMIS", url - end - - def test_route_requirement_recognize_with_xi_modifiers - set.draw do |map| - map.connect 'page/:name', :controller => 'pages', - :action => 'show', - :requirements => {:name => / # Desperately overcommented regexp - ( #Either - david #The Creator - | #Or - jamis #The Deployer - )/xi} - end - assert_equal({:controller => 'pages', :action => 'show', :name => 'JAMIS'}, set.recognize_path('/page/JAMIS')) - end - - -end - -class RoutingTest < Test::Unit::TestCase - - def test_possible_controllers - true_controller_paths = ActionController::Routing.controller_paths - - ActionController::Routing.use_controllers! nil - - silence_warnings do - Object.send(:const_set, :RAILS_ROOT, File.dirname(__FILE__) + '/controller_fixtures') + assert_equal "/page/JAMIS", url end - ActionController::Routing.controller_paths = [ - RAILS_ROOT, RAILS_ROOT + '/app/controllers', RAILS_ROOT + '/vendor/plugins/bad_plugin/lib' - ] - - assert_equal ["admin/user", "plugin", "user"], ActionController::Routing.possible_controllers.sort - ensure - if true_controller_paths - ActionController::Routing.controller_paths = true_controller_paths - end - ActionController::Routing.use_controllers! nil - Object.send(:remove_const, :RAILS_ROOT) rescue nil - end - - def test_possible_controllers_are_reset_on_each_load - true_possible_controllers = ActionController::Routing.possible_controllers - true_controller_paths = ActionController::Routing.controller_paths - - ActionController::Routing.use_controllers! nil - root = File.dirname(__FILE__) + '/controller_fixtures' - - ActionController::Routing.controller_paths = [] - assert_equal [], ActionController::Routing.possible_controllers - - ActionController::Routing::Routes.load! - ActionController::Routing.controller_paths = [ - root, root + '/app/controllers', root + '/vendor/plugins/bad_plugin/lib' - ] - - assert_equal ["admin/user", "plugin", "user"], ActionController::Routing.possible_controllers.sort - ensure - ActionController::Routing.controller_paths = true_controller_paths - ActionController::Routing.use_controllers! true_possible_controllers - Object.send(:remove_const, :RAILS_ROOT) rescue nil - - ActionController::Routing::Routes.clear! - ActionController::Routing::Routes.load_routes! - end - - def test_with_controllers - c = %w(admin/accounts admin/users account pages) - ActionController::Routing.with_controllers c do - assert_equal c, ActionController::Routing.possible_controllers + def test_route_requirement_recognize_with_xi_modifiers + set.draw do |map| + map.connect 'page/:name', :controller => 'pages', + :action => 'show', + :requirements => {:name => / # Desperately overcommented regexp + ( #Either + david #The Creator + | #Or + jamis #The Deployer + )/xi} + end + assert_equal({:controller => 'pages', :action => 'show', :name => 'JAMIS'}, set.recognize_path('/page/JAMIS')) end end - def test_normalize_unix_paths - load_paths = %w(. config/../app/controllers config/../app//helpers script/../config/../vendor/rails/actionpack/lib vendor/rails/railties/builtin/rails_info app/models lib script/../config/../foo/bar/../../app/models .foo/../.bar foo.bar/../config) - paths = ActionController::Routing.normalize_paths(load_paths) - assert_equal %w(vendor/rails/railties/builtin/rails_info vendor/rails/actionpack/lib app/controllers app/helpers app/models config .bar lib .), paths - end - - def test_normalize_windows_paths - load_paths = %w(. config\\..\\app\\controllers config\\..\\app\\\\helpers script\\..\\config\\..\\vendor\\rails\\actionpack\\lib vendor\\rails\\railties\\builtin\\rails_info app\\models lib script\\..\\config\\..\\foo\\bar\\..\\..\\app\\models .foo\\..\\.bar foo.bar\\..\\config) - paths = ActionController::Routing.normalize_paths(load_paths) - assert_equal %w(vendor\\rails\\railties\\builtin\\rails_info vendor\\rails\\actionpack\\lib app\\controllers app\\helpers app\\models config .bar lib .), paths - end - - def test_routing_helper_module - assert_kind_of Module, ActionController::Routing::Helpers - - h = ActionController::Routing::Helpers - c = Class.new - assert ! c.ancestors.include?(h) - ActionController::Routing::Routes.install_helpers c - assert c.ancestors.include?(h) - end - -end - -uses_mocha 'route loading' do class RouteLoadingTest < Test::Unit::TestCase - def setup routes.instance_variable_set '@routes_last_modified', nil silence_warnings { Object.const_set :RAILS_ROOT, '.' } @@ -2397,7 +2369,7 @@ def test_adding_inflections_forces_reload ActiveSupport::Inflector.inflections { |inflect| inflect.uncountable('equipment') } end - + def test_load_with_configuration routes.configuration_file = "foobarbaz" File.expects(:stat).returns(@stat) @@ -2407,9 +2379,8 @@ def test_load_with_configuration end private - def routes - ActionController::Routing::Routes - end - + def routes + ActionController::Routing::Routes + end end end From 06b6f435cb7ed4b171eb600d3a9286829b8b2482 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Sat, 7 Jun 2008 23:46:06 -0500 Subject: [PATCH 060/200] Wrap Initializer after_initialize inside mocha block. --- railties/test/initializer_test.rb | 96 +++++++++++++++---------------- 1 file changed, 48 insertions(+), 48 deletions(-) diff --git a/railties/test/initializer_test.rb b/railties/test/initializer_test.rb index efce4f292d660..e5da4919473ba 100644 --- a/railties/test/initializer_test.rb +++ b/railties/test/initializer_test.rb @@ -30,66 +30,66 @@ def test_load_environment_with_constant end -class Initializer_after_initialize_with_blocks_environment_Test < Test::Unit::TestCase - def setup - config = ConfigurationMock.new("") - config.after_initialize do - $test_after_initialize_block1 = "success" - end - config.after_initialize do - $test_after_initialize_block2 = "congratulations" - end - assert_nil $test_after_initialize_block1 - assert_nil $test_after_initialize_block2 +uses_mocha 'Initializer after_initialize' do + class Initializer_after_initialize_with_blocks_environment_Test < Test::Unit::TestCase + def setup + config = ConfigurationMock.new("") + config.after_initialize do + $test_after_initialize_block1 = "success" + end + config.after_initialize do + $test_after_initialize_block2 = "congratulations" + end + assert_nil $test_after_initialize_block1 + assert_nil $test_after_initialize_block2 - Rails::Initializer.any_instance.expects(:gems_dependencies_loaded).returns(true) - Rails::Initializer.run(:after_initialize, config) - end + Rails::Initializer.any_instance.expects(:gems_dependencies_loaded).returns(true) + Rails::Initializer.run(:after_initialize, config) + end - def teardown - $test_after_initialize_block1 = nil - $test_after_initialize_block2 = nil - end + def teardown + $test_after_initialize_block1 = nil + $test_after_initialize_block2 = nil + end - def test_should_have_called_the_first_after_initialize_block - assert_equal "success", $test_after_initialize_block1 - end + def test_should_have_called_the_first_after_initialize_block + assert_equal "success", $test_after_initialize_block1 + end - def test_should_have_called_the_second_after_initialize_block - assert_equal "congratulations", $test_after_initialize_block2 + def test_should_have_called_the_second_after_initialize_block + assert_equal "congratulations", $test_after_initialize_block2 + end end -end -class Initializer_after_initialize_with_no_block_environment_Test < Test::Unit::TestCase + class Initializer_after_initialize_with_no_block_environment_Test < Test::Unit::TestCase + def setup + config = ConfigurationMock.new("") + config.after_initialize do + $test_after_initialize_block1 = "success" + end + config.after_initialize # don't pass a block, this is what we're testing! + config.after_initialize do + $test_after_initialize_block2 = "congratulations" + end + assert_nil $test_after_initialize_block1 - def setup - config = ConfigurationMock.new("") - config.after_initialize do - $test_after_initialize_block1 = "success" + Rails::Initializer.any_instance.expects(:gems_dependencies_loaded).returns(true) + Rails::Initializer.run(:after_initialize, config) end - config.after_initialize # don't pass a block, this is what we're testing! - config.after_initialize do - $test_after_initialize_block2 = "congratulations" - end - assert_nil $test_after_initialize_block1 - - Rails::Initializer.any_instance.expects(:gems_dependencies_loaded).returns(true) - Rails::Initializer.run(:after_initialize, config) - end - def teardown - $test_after_initialize_block1 = nil - $test_after_initialize_block2 = nil - end + def teardown + $test_after_initialize_block1 = nil + $test_after_initialize_block2 = nil + end - def test_should_have_called_the_first_after_initialize_block - assert_equal "success", $test_after_initialize_block1, "should still get set" - end + def test_should_have_called_the_first_after_initialize_block + assert_equal "success", $test_after_initialize_block1, "should still get set" + end - def test_should_have_called_the_second_after_initialize_block - assert_equal "congratulations", $test_after_initialize_block2 + def test_should_have_called_the_second_after_initialize_block + assert_equal "congratulations", $test_after_initialize_block2 + end end - end uses_mocha 'framework paths' do From 6c970d79a064b953d3d9555a362a1ad1e0058d1c Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Sat, 7 Jun 2008 17:23:25 -0700 Subject: [PATCH 061/200] Performance: faster Object.subclasses_of --- .../active_support/core_ext/object/extending.rb | 17 +++++++++-------- activesupport/test/core_ext/class_test.rb | 2 +- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/activesupport/lib/active_support/core_ext/object/extending.rb b/activesupport/lib/active_support/core_ext/object/extending.rb index 43a2be916e9d3..082e98a297bfc 100644 --- a/activesupport/lib/active_support/core_ext/object/extending.rb +++ b/activesupport/lib/active_support/core_ext/object/extending.rb @@ -3,17 +3,18 @@ def remove_subclasses_of(*superclasses) #:nodoc: Class.remove_class(*subclasses_of(*superclasses)) end + # Exclude this class unless it's a subclass of our supers and is defined. + # We check defined? in case we find a removed class that has yet to be + # garbage collected. This also fails for anonymous classes -- please + # submit a patch if you have a workaround. def subclasses_of(*superclasses) #:nodoc: subclasses = [] - # Exclude this class unless it's a subclass of our supers and is defined. - # We check defined? in case we find a removed class that has yet to be - # garbage collected. This also fails for anonymous classes -- please - # submit a patch if you have a workaround. - ObjectSpace.each_object(Class) do |k| - if superclasses.any? { |superclass| k < superclass } && - (k.name.blank? || eval("defined?(::#{k}) && ::#{k}.object_id == k.object_id")) - subclasses << k + superclasses.each do |sup| + ObjectSpace.each_object(class << sup; self; end) do |k| + if k != sup && (k.name.blank? || eval("defined?(::#{k}) && ::#{k}.object_id == k.object_id")) + subclasses << k + end end end diff --git a/activesupport/test/core_ext/class_test.rb b/activesupport/test/core_ext/class_test.rb index 0346fad1907d5..9c6071d47895d 100644 --- a/activesupport/test/core_ext/class_test.rb +++ b/activesupport/test/core_ext/class_test.rb @@ -38,9 +38,9 @@ def test_retrieving_subclasses @parent = eval("class D; end; D") @sub = eval("class E < D; end; E") @subofsub = eval("class F < E; end; F") - assert @parent.subclasses.all? { |i| [@sub.to_s, @subofsub.to_s].include?(i) } assert_equal 2, @parent.subclasses.size assert_equal [@subofsub.to_s], @sub.subclasses assert_equal [], @subofsub.subclasses + assert_equal [@sub.to_s, @subofsub.to_s].sort, @parent.subclasses.sort end end From 56f5c5582c97b60a89ce44f5dfccba0b5aed4357 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Sun, 8 Jun 2008 01:32:57 -0700 Subject: [PATCH 062/200] Missed add: deprecated erb_variable test --- actionpack/test/template/deprecated_erb_variable_test.rb | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 actionpack/test/template/deprecated_erb_variable_test.rb diff --git a/actionpack/test/template/deprecated_erb_variable_test.rb b/actionpack/test/template/deprecated_erb_variable_test.rb new file mode 100644 index 0000000000000..b07c590164a0b --- /dev/null +++ b/actionpack/test/template/deprecated_erb_variable_test.rb @@ -0,0 +1,9 @@ +require 'abstract_unit' + +class DeprecatedErbVariableTest < ActionView::TestCase + def test_setting_erb_variable_warns + assert_deprecated 'erb_variable' do + ActionView::Base.erb_variable = '_erbout' + end + end +end From f42dab8221a2cfc13b49f6ac713e0845f25b65c3 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Sun, 8 Jun 2008 01:36:33 -0700 Subject: [PATCH 063/200] Revert "Missed add: deprecated erb_variable test" This reverts commit 56f5c5582c97b60a89ce44f5dfccba0b5aed4357. --- actionpack/test/template/deprecated_erb_variable_test.rb | 9 --------- 1 file changed, 9 deletions(-) delete mode 100644 actionpack/test/template/deprecated_erb_variable_test.rb diff --git a/actionpack/test/template/deprecated_erb_variable_test.rb b/actionpack/test/template/deprecated_erb_variable_test.rb deleted file mode 100644 index b07c590164a0b..0000000000000 --- a/actionpack/test/template/deprecated_erb_variable_test.rb +++ /dev/null @@ -1,9 +0,0 @@ -require 'abstract_unit' - -class DeprecatedErbVariableTest < ActionView::TestCase - def test_setting_erb_variable_warns - assert_deprecated 'erb_variable' do - ActionView::Base.erb_variable = '_erbout' - end - end -end From b336ce9e062e4de0d20aa359b08e86fe83b6dd89 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Sun, 8 Jun 2008 01:36:33 -0700 Subject: [PATCH 064/200] Revert "Missed add: deprecated erb_variable test" This reverts commit 56f5c5582c97b60a89ce44f5dfccba0b5aed4357. --- actionpack/test/template/deprecated_erb_variable_test.rb | 9 --------- 1 file changed, 9 deletions(-) delete mode 100644 actionpack/test/template/deprecated_erb_variable_test.rb diff --git a/actionpack/test/template/deprecated_erb_variable_test.rb b/actionpack/test/template/deprecated_erb_variable_test.rb deleted file mode 100644 index b07c590164a0b..0000000000000 --- a/actionpack/test/template/deprecated_erb_variable_test.rb +++ /dev/null @@ -1,9 +0,0 @@ -require 'abstract_unit' - -class DeprecatedErbVariableTest < ActionView::TestCase - def test_setting_erb_variable_warns - assert_deprecated 'erb_variable' do - ActionView::Base.erb_variable = '_erbout' - end - end -end From 86a042ddd9dba8f62e7328c7258a798aef73d57f Mon Sep 17 00:00:00 2001 From: Jacek Becela Date: Wed, 28 May 2008 21:30:44 +0200 Subject: [PATCH 065/200] Make plugins initialize also from rails/init.rb to ensure consistency with gems used as plugins [#272 state:resolved] --- railties/lib/rails/plugin.rb | 10 +++++++++- .../fixtures/plugins/default/gemlike/init.rb | 1 + .../plugins/default/gemlike/lib/gemlike.rb | 2 ++ .../plugins/default/gemlike/rails/init.rb | 7 +++++++ railties/test/initializer_test.rb | 6 +++--- railties/test/plugin_loader_test.rb | 12 ++++++------ railties/test/plugin_locator_test.rb | 4 ++-- railties/test/plugin_test.rb | 16 ++++++++++++---- 8 files changed, 42 insertions(+), 16 deletions(-) create mode 100644 railties/test/fixtures/plugins/default/gemlike/init.rb create mode 100644 railties/test/fixtures/plugins/default/gemlike/lib/gemlike.rb create mode 100644 railties/test/fixtures/plugins/default/gemlike/rails/init.rb diff --git a/railties/lib/rails/plugin.rb b/railties/lib/rails/plugin.rb index 256f4b0132563..a54ab85dbe56f 100644 --- a/railties/lib/rails/plugin.rb +++ b/railties/lib/rails/plugin.rb @@ -74,10 +74,18 @@ def lib_path File.join(directory, 'lib') end - def init_path + def classic_init_path File.join(directory, 'init.rb') end + def gem_init_path + File.join(directory, 'rails', 'init.rb') + end + + def init_path + File.file?(gem_init_path) ? gem_init_path : classic_init_path + end + def has_lib_directory? File.directory?(lib_path) end diff --git a/railties/test/fixtures/plugins/default/gemlike/init.rb b/railties/test/fixtures/plugins/default/gemlike/init.rb new file mode 100644 index 0000000000000..6a771b5b68529 --- /dev/null +++ b/railties/test/fixtures/plugins/default/gemlike/init.rb @@ -0,0 +1 @@ +raise 'This init.rb should not be evaluated because rails/init.rb exists' diff --git a/railties/test/fixtures/plugins/default/gemlike/lib/gemlike.rb b/railties/test/fixtures/plugins/default/gemlike/lib/gemlike.rb new file mode 100644 index 0000000000000..2088103e459f1 --- /dev/null +++ b/railties/test/fixtures/plugins/default/gemlike/lib/gemlike.rb @@ -0,0 +1,2 @@ +module Gemlike +end \ No newline at end of file diff --git a/railties/test/fixtures/plugins/default/gemlike/rails/init.rb b/railties/test/fixtures/plugins/default/gemlike/rails/init.rb new file mode 100644 index 0000000000000..171a293eb3bfa --- /dev/null +++ b/railties/test/fixtures/plugins/default/gemlike/rails/init.rb @@ -0,0 +1,7 @@ +# I have access to my directory and the Rails config. +raise 'directory expected but undefined in init.rb' unless defined? directory +raise 'config expected but undefined in init.rb' unless defined? config + +# My lib/ dir must be in the load path. +require 'gemlike' +raise 'missing mixin from my lib/ dir' unless defined? Gemlike diff --git a/railties/test/initializer_test.rb b/railties/test/initializer_test.rb index e5da4919473ba..dee7abe05f1f5 100644 --- a/railties/test/initializer_test.rb +++ b/railties/test/initializer_test.rb @@ -173,7 +173,7 @@ def test_only_the_specified_plugins_are_located_in_the_order_listed def test_all_plugins_are_loaded_when_registered_plugin_list_is_untouched failure_tip = "It's likely someone has added a new plugin fixture without updating this list" load_plugins! - assert_plugins [:a, :acts_as_chunky_bacon, :plugin_with_no_lib_dir, :stubby], @initializer.loaded_plugins, failure_tip + assert_plugins [:a, :acts_as_chunky_bacon, :gemlike, :plugin_with_no_lib_dir, :stubby], @initializer.loaded_plugins, failure_tip end def test_all_plugins_loaded_when_all_is_used @@ -181,7 +181,7 @@ def test_all_plugins_loaded_when_all_is_used only_load_the_following_plugins! plugin_names load_plugins! failure_tip = "It's likely someone has added a new plugin fixture without updating this list" - assert_plugins [:stubby, :acts_as_chunky_bacon, :a, :plugin_with_no_lib_dir], @initializer.loaded_plugins, failure_tip + assert_plugins [:stubby, :acts_as_chunky_bacon, :a, :gemlike, :plugin_with_no_lib_dir], @initializer.loaded_plugins, failure_tip end def test_all_plugins_loaded_after_all @@ -189,7 +189,7 @@ def test_all_plugins_loaded_after_all only_load_the_following_plugins! plugin_names load_plugins! failure_tip = "It's likely someone has added a new plugin fixture without updating this list" - assert_plugins [:stubby, :a, :plugin_with_no_lib_dir, :acts_as_chunky_bacon], @initializer.loaded_plugins, failure_tip + assert_plugins [:stubby, :a, :gemlike, :plugin_with_no_lib_dir, :acts_as_chunky_bacon], @initializer.loaded_plugins, failure_tip end def test_plugin_names_may_be_strings diff --git a/railties/test/plugin_loader_test.rb b/railties/test/plugin_loader_test.rb index bce4446f79ed2..e5fc0926eb71a 100644 --- a/railties/test/plugin_loader_test.rb +++ b/railties/test/plugin_loader_test.rb @@ -48,16 +48,16 @@ def test_should_return_empty_array_if_configuration_plugins_is_empty end def test_should_find_all_availble_plugins_and_return_as_all_plugins - assert_plugins [:stubby, :plugin_with_no_lib_dir, :acts_as_chunky_bacon, :a], @loader.all_plugins.reverse, @failure_tip + assert_plugins [:stubby, :plugin_with_no_lib_dir, :gemlike, :acts_as_chunky_bacon, :a], @loader.all_plugins.reverse, @failure_tip end def test_should_return_all_plugins_as_plugins_when_registered_plugin_list_is_untouched - assert_plugins [:a, :acts_as_chunky_bacon, :plugin_with_no_lib_dir, :stubby], @loader.plugins, @failure_tip + assert_plugins [:a, :acts_as_chunky_bacon, :gemlike, :plugin_with_no_lib_dir, :stubby], @loader.plugins, @failure_tip end def test_should_return_all_plugins_as_plugins_when_registered_plugin_list_is_nil @configuration.plugins = nil - assert_plugins [:a, :acts_as_chunky_bacon, :plugin_with_no_lib_dir, :stubby], @loader.plugins, @failure_tip + assert_plugins [:a, :acts_as_chunky_bacon, :gemlike, :plugin_with_no_lib_dir, :stubby], @loader.plugins, @failure_tip end def test_should_return_specific_plugins_named_in_config_plugins_array_if_set @@ -74,17 +74,17 @@ def test_should_respect_the_order_of_plugins_given_in_configuration def test_should_load_all_plugins_in_natural_order_when_all_is_used only_load_the_following_plugins! [:all] - assert_plugins [:a, :acts_as_chunky_bacon, :plugin_with_no_lib_dir, :stubby], @loader.plugins, @failure_tip + assert_plugins [:a, :acts_as_chunky_bacon, :gemlike, :plugin_with_no_lib_dir, :stubby], @loader.plugins, @failure_tip end def test_should_load_specified_plugins_in_order_and_then_all_remaining_plugins_when_all_is_used only_load_the_following_plugins! [:stubby, :acts_as_chunky_bacon, :all] - assert_plugins [:stubby, :acts_as_chunky_bacon, :a, :plugin_with_no_lib_dir], @loader.plugins, @failure_tip + assert_plugins [:stubby, :acts_as_chunky_bacon, :a, :gemlike, :plugin_with_no_lib_dir], @loader.plugins, @failure_tip end def test_should_be_able_to_specify_loading_of_plugins_loaded_after_all only_load_the_following_plugins! [:stubby, :all, :acts_as_chunky_bacon] - assert_plugins [:stubby, :a, :plugin_with_no_lib_dir, :acts_as_chunky_bacon], @loader.plugins, @failure_tip + assert_plugins [:stubby, :a, :gemlike, :plugin_with_no_lib_dir, :acts_as_chunky_bacon], @loader.plugins, @failure_tip end def test_should_accept_plugin_names_given_as_strings diff --git a/railties/test/plugin_locator_test.rb b/railties/test/plugin_locator_test.rb index 5f1dd991eabe9..363fa27f15465 100644 --- a/railties/test/plugin_locator_test.rb +++ b/railties/test/plugin_locator_test.rb @@ -47,12 +47,12 @@ def test_should_return_nil_when_calling_create_plugin_with_an_invalid_plugin_dir end def test_should_return_all_plugins_found_under_the_set_plugin_paths - assert_equal ["a", "acts_as_chunky_bacon", "plugin_with_no_lib_dir", "stubby"].sort, @locator.plugins.map(&:name).sort + assert_equal ["a", "acts_as_chunky_bacon", "gemlike", "plugin_with_no_lib_dir", "stubby"].sort, @locator.plugins.map(&:name).sort end def test_should_find_plugins_only_under_the_plugin_paths_set_in_configuration @configuration.plugin_paths = [File.join(plugin_fixture_root_path, "default")] - assert_equal ["acts_as_chunky_bacon", "plugin_with_no_lib_dir", "stubby"].sort, @locator.plugins.map(&:name).sort + assert_equal ["acts_as_chunky_bacon", "gemlike", "plugin_with_no_lib_dir", "stubby"].sort, @locator.plugins.map(&:name).sort @configuration.plugin_paths = [File.join(plugin_fixture_root_path, "alternate")] assert_equal ["a"], @locator.plugins.map(&:name) diff --git a/railties/test/plugin_test.rb b/railties/test/plugin_test.rb index 1445338f14054..50124240a5652 100644 --- a/railties/test/plugin_test.rb +++ b/railties/test/plugin_test.rb @@ -5,9 +5,10 @@ class PluginTest < Test::Unit::TestCase def setup - @initializer = Rails::Initializer.new(Rails::Configuration.new) - @valid_plugin_path = plugin_fixture_path('default/stubby') - @empty_plugin_path = plugin_fixture_path('default/empty') + @initializer = Rails::Initializer.new(Rails::Configuration.new) + @valid_plugin_path = plugin_fixture_path('default/stubby') + @empty_plugin_path = plugin_fixture_path('default/empty') + @gemlike_plugin_path = plugin_fixture_path('default/gemlike') end def test_should_determine_plugin_name_from_the_directory_of_the_plugin @@ -70,7 +71,14 @@ def test_should_raise_a_load_error_when_trying_to_load_an_invalid_plugin plugin.stubs(:evaluate_init_rb) plugin.send(:load, @initializer) end - + + # This path is fine so nothing is raised + assert_nothing_raised do + plugin = plugin_for(@gemlike_plugin_path) + plugin.stubs(:evaluate_init_rb) + plugin.send(:load, @initializer) + end + # This is an empty path so it raises assert_raises(LoadError) do plugin = plugin_for(@empty_plugin_path) From faad1e32a8ab81890018ba89d191607778830cf0 Mon Sep 17 00:00:00 2001 From: rick Date: Sun, 8 Jun 2008 14:04:04 -0400 Subject: [PATCH 066/200] Fix discrepancies with loading rails/init.rb from gems. [#324 state:resolved] --- railties/CHANGELOG | 4 ++++ railties/lib/rails/gem_dependency.rb | 6 +++++- railties/lib/rails/plugin.rb | 21 +++++++++++---------- railties/lib/rails/plugin/locator.rb | 7 ++++--- railties/test/plugin_loader_test.rb | 2 +- 5 files changed, 25 insertions(+), 15 deletions(-) diff --git a/railties/CHANGELOG b/railties/CHANGELOG index 39edc511da22e..a43455ae2c006 100644 --- a/railties/CHANGELOG +++ b/railties/CHANGELOG @@ -1,5 +1,9 @@ *Edge* +* Fix discrepancies with loading rails/init.rb from gems. + +* Plugins check for the gem init path (rails/init.rb) before the standard plugin init path (init.rb) [Jacek Becela] + * Wrapped Rails.env in StringInquirer so you can do Rails.env.development? [DHH] * Fixed that RailsInfoController wasn't considering all requests local in development mode (Edgard Castro) [#310 state:resolved] diff --git a/railties/lib/rails/gem_dependency.rb b/railties/lib/rails/gem_dependency.rb index 4cac7f725a231..f8d97840c1f72 100644 --- a/railties/lib/rails/gem_dependency.rb +++ b/railties/lib/rails/gem_dependency.rb @@ -23,9 +23,13 @@ def initialize(name, options = {}) @unpack_directory = nil end + def unpacked_paths + Dir[File.join(self.class.unpacked_path, "#{@name}-#{@version || "*"}")] + end + def add_load_paths return if @loaded || @load_paths_added - unpacked_paths = Dir[File.join(self.class.unpacked_path, "#{@name}-#{@version || "*"}")] + unpacked_paths = self.unpacked_paths if unpacked_paths.empty? args = [@name] args << @requirement.to_s if @requirement diff --git a/railties/lib/rails/plugin.rb b/railties/lib/rails/plugin.rb index a54ab85dbe56f..b8b2b57038b16 100644 --- a/railties/lib/rails/plugin.rb +++ b/railties/lib/rails/plugin.rb @@ -95,14 +95,14 @@ def has_init_file? end def evaluate_init_rb(initializer) - if has_init_file? - silence_warnings do - # Allow plugins to reference the current configuration object - config = initializer.configuration - - eval(IO.read(init_path), binding, init_path) - end - end + if has_init_file? + silence_warnings do + # Allow plugins to reference the current configuration object + config = initializer.configuration + + eval(IO.read(init_path), binding, init_path) + end + end end end @@ -111,8 +111,9 @@ def evaluate_init_rb(initializer) # to Dependencies.load_paths. class GemPlugin < Plugin # Initialize this plugin from a Gem::Specification. - def initialize(spec) - super(File.join(spec.full_gem_path)) + def initialize(spec, gem) + directory = (gem.frozen? && gem.unpacked_paths.first) || File.join(spec.full_gem_path) + super(directory) @name = spec.name end diff --git a/railties/lib/rails/plugin/locator.rb b/railties/lib/rails/plugin/locator.rb index f06a51a572730..79c07fccd1d7a 100644 --- a/railties/lib/rails/plugin/locator.rb +++ b/railties/lib/rails/plugin/locator.rb @@ -78,8 +78,9 @@ def locate_plugins_under(base_path) # a rails/init.rb file. class GemLocator < Locator def plugins - specs = initializer.configuration.gems.map(&:specification) - specs += Gem.loaded_specs.values.select do |spec| + gem_index = initializer.configuration.gems.inject({}) { |memo, gem| memo.update gem.specification => gem } + specs = gem_index.keys + specs += Gem.loaded_specs.values.select do |spec| spec.loaded_from && # prune stubs File.exist?(File.join(spec.full_gem_path, "rails", "init.rb")) end @@ -91,7 +92,7 @@ def plugins deps.add(*specs) unless specs.empty? deps.dependency_order.collect do |spec| - Rails::GemPlugin.new(spec) + Rails::GemPlugin.new(spec, gem_index[spec]) end end end diff --git a/railties/test/plugin_loader_test.rb b/railties/test/plugin_loader_test.rb index e5fc0926eb71a..f429bae15c05b 100644 --- a/railties/test/plugin_loader_test.rb +++ b/railties/test/plugin_loader_test.rb @@ -94,7 +94,7 @@ def test_should_accept_plugin_names_given_as_strings def test_should_add_plugin_load_paths_to_global_LOAD_PATH_array only_load_the_following_plugins! [:stubby, :acts_as_chunky_bacon] - stubbed_application_lib_index_in_LOAD_PATHS = 5 + stubbed_application_lib_index_in_LOAD_PATHS = 4 @loader.stubs(:application_lib_index).returns(stubbed_application_lib_index_in_LOAD_PATHS) @loader.add_plugin_load_paths From 51e4106dcc58e5218e8b297ad870a063b7bb1ab8 Mon Sep 17 00:00:00 2001 From: rick Date: Sun, 8 Jun 2008 14:30:14 -0400 Subject: [PATCH 067/200] Add the gem load paths before the framework is loaded, so certain gems like RedCloth and BlueCloth can be frozen. [#320 state:resolved] --- railties/CHANGELOG | 2 ++ railties/lib/initializer.rb | 8 ++++---- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/railties/CHANGELOG b/railties/CHANGELOG index a43455ae2c006..93f4f8fa286f3 100644 --- a/railties/CHANGELOG +++ b/railties/CHANGELOG @@ -1,5 +1,7 @@ *Edge* +* Add the gem load paths before the framework is loaded, so certain gems like RedCloth and BlueCloth can be frozen. + * Fix discrepancies with loading rails/init.rb from gems. * Plugins check for the gem init path (rails/init.rb) before the standard plugin init path (init.rb) [Jacek Becela] diff --git a/railties/lib/initializer.rb b/railties/lib/initializer.rb index f0b5e3f257bf9..3d94cedb47c98 100644 --- a/railties/lib/initializer.rb +++ b/railties/lib/initializer.rb @@ -113,10 +113,10 @@ def process check_ruby_version install_gem_spec_stubs set_load_path - + add_gem_load_paths + require_frameworks set_autoload_paths - add_gem_load_paths add_plugin_load_paths load_environment @@ -242,12 +242,12 @@ def add_plugin_load_paths def add_gem_load_paths unless @configuration.gems.empty? require "rubygems" - @configuration.gems.each &:add_load_paths + @configuration.gems.each { |gem| gem.add_load_paths } end end def load_gems - @configuration.gems.each(&:load) + @configuration.gems.each { |gem| gem.load } end def check_gem_dependencies From a2f6ded73209eeb9c6843b16c0253bbe56236b29 Mon Sep 17 00:00:00 2001 From: Tiago Macedo Date: Sun, 8 Jun 2008 17:00:56 +0100 Subject: [PATCH 068/200] Fix conditions and order on join tables with limited eager loading. [#372 state:resolved] --- activerecord/lib/active_record/associations.rb | 4 +++- activerecord/test/cases/associations/eager_test.rb | 13 ++++++++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb index a3d1bbbada68b..8ddcc24daa7a6 100755 --- a/activerecord/lib/active_record/associations.rb +++ b/activerecord/lib/active_record/associations.rb @@ -1638,7 +1638,9 @@ def remove_duplicate_results!(base, records, associations) end def join_for_table_name(table_name) - @joins.select{|j|j.aliased_table_name == table_name.gsub(/^\"(.*)\"$/){$1} }.first rescue nil + join = (@joins.select{|j|j.aliased_table_name == table_name.gsub(/^\"(.*)\"$/){$1} }.first) rescue nil + return join unless join.nil? + @joins.select{|j|j.is_a?(JoinAssociation) && j.aliased_join_table_name == table_name.gsub(/^\"(.*)\"$/){$1} }.first rescue nil end def joins_for_table_name(table_name) diff --git a/activerecord/test/cases/associations/eager_test.rb b/activerecord/test/cases/associations/eager_test.rb index 3a3358e39bcba..f65ada550b149 100644 --- a/activerecord/test/cases/associations/eager_test.rb +++ b/activerecord/test/cases/associations/eager_test.rb @@ -14,11 +14,14 @@ require 'models/subscriber' require 'models/subscription' require 'models/book' +require 'models/developer' +require 'models/project' class EagerAssociationTest < ActiveRecord::TestCase fixtures :posts, :comments, :authors, :categories, :categories_posts, :companies, :accounts, :tags, :taggings, :people, :readers, - :owners, :pets, :author_favorites, :jobs, :references, :subscribers, :subscriptions, :books + :owners, :pets, :author_favorites, :jobs, :references, :subscribers, :subscriptions, :books, + :developers, :projects def test_loading_with_one_association posts = Post.find(:all, :include => :comments) @@ -609,4 +612,12 @@ def test_load_with_sti_sharing_association Comment.find :all, :include => :post end end + + def test_conditions_on_join_table_with_include_and_limit + assert_equal 3, Developer.find(:all, :include => 'projects', :conditions => 'developers_projects.access_level = 1', :limit => 5).size + end + + def test_order_on_join_table_with_include_and_limit + assert_equal 5, Developer.find(:all, :include => 'projects', :order => 'developers_projects.joined_on DESC', :limit => 5).size + end end From 68af8c54af8294a8cf079dfaae0c1c04f5c23e59 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Sun, 8 Jun 2008 16:02:25 -0700 Subject: [PATCH 069/200] Remove vendor/mysql.rb. Deprecated in 2.1 stable, gone in 2.2. --- .../connection_adapters/mysql_adapter.rb | 29 +- .../lib/active_record/vendor/mysql.rb | 1214 ----------------- 2 files changed, 4 insertions(+), 1239 deletions(-) delete mode 100644 activerecord/lib/active_record/vendor/mysql.rb diff --git a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb index 653b45021d0de..b052c0328e4c6 100755 --- a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb @@ -42,30 +42,6 @@ def all_hashes module ActiveRecord class Base - def self.require_mysql - # Include the MySQL driver if one hasn't already been loaded - unless defined? Mysql - begin - require_library_or_gem 'mysql' - rescue LoadError => cannot_require_mysql - # Use the bundled Ruby/MySQL driver if no driver is already in place - begin - ActiveRecord::Base.logger.info( - "WARNING: You're using the Ruby-based MySQL library that ships with Rails. This library is not suited for production. " + - "Please install the C-based MySQL library instead (gem install mysql)." - ) if ActiveRecord::Base.logger - - require 'active_record/vendor/mysql' - rescue LoadError - raise cannot_require_mysql - end - end - end - - # Define Mysql::Result.all_hashes - MysqlCompat.define_all_hashes_method! - end - # Establishes a connection to the database that's used by all Active Record objects. def self.mysql_connection(config) # :nodoc: config = config.symbolize_keys @@ -81,7 +57,10 @@ def self.mysql_connection(config) # :nodoc: raise ArgumentError, "No database specified. Missing argument: database." end - require_mysql + # Require the MySQL driver and define Mysql::Result.all_hashes + require_library_or_gem('mysql') unless defined? Mysql + MysqlCompat.define_all_hashes_method! + mysql = Mysql.init mysql.ssl_set(config[:sslkey], config[:sslcert], config[:sslca], config[:sslcapath], config[:sslcipher]) if config[:sslkey] diff --git a/activerecord/lib/active_record/vendor/mysql.rb b/activerecord/lib/active_record/vendor/mysql.rb deleted file mode 100644 index 1c3294c719502..0000000000000 --- a/activerecord/lib/active_record/vendor/mysql.rb +++ /dev/null @@ -1,1214 +0,0 @@ -# $Id: mysql.rb,v 1.24 2005/02/12 11:37:15 tommy Exp $ -# -# Copyright (C) 2003-2005 TOMITA Masahiro -# tommy@tmtm.org -# - -class Mysql - - VERSION = "4.0-ruby-0.2.6-plus-changes" - - require "socket" - require "digest/sha1" - - MAX_PACKET_LENGTH = 256*256*256-1 - MAX_ALLOWED_PACKET = 1024*1024*1024 - - MYSQL_UNIX_ADDR = "/tmp/mysql.sock" - MYSQL_PORT = 3306 - PROTOCOL_VERSION = 10 - - SCRAMBLE_LENGTH = 20 - SCRAMBLE_LENGTH_323 = 8 - - # Command - COM_SLEEP = 0 - COM_QUIT = 1 - COM_INIT_DB = 2 - COM_QUERY = 3 - COM_FIELD_LIST = 4 - COM_CREATE_DB = 5 - COM_DROP_DB = 6 - COM_REFRESH = 7 - COM_SHUTDOWN = 8 - COM_STATISTICS = 9 - COM_PROCESS_INFO = 10 - COM_CONNECT = 11 - COM_PROCESS_KILL = 12 - COM_DEBUG = 13 - COM_PING = 14 - COM_TIME = 15 - COM_DELAYED_INSERT = 16 - COM_CHANGE_USER = 17 - COM_BINLOG_DUMP = 18 - COM_TABLE_DUMP = 19 - COM_CONNECT_OUT = 20 - COM_REGISTER_SLAVE = 21 - - # Client flag - CLIENT_LONG_PASSWORD = 1 - CLIENT_FOUND_ROWS = 1 << 1 - CLIENT_LONG_FLAG = 1 << 2 - CLIENT_CONNECT_WITH_DB= 1 << 3 - CLIENT_NO_SCHEMA = 1 << 4 - CLIENT_COMPRESS = 1 << 5 - CLIENT_ODBC = 1 << 6 - CLIENT_LOCAL_FILES = 1 << 7 - CLIENT_IGNORE_SPACE = 1 << 8 - CLIENT_PROTOCOL_41 = 1 << 9 - CLIENT_INTERACTIVE = 1 << 10 - CLIENT_SSL = 1 << 11 - CLIENT_IGNORE_SIGPIPE = 1 << 12 - CLIENT_TRANSACTIONS = 1 << 13 - CLIENT_RESERVED = 1 << 14 - CLIENT_SECURE_CONNECTION = 1 << 15 - CLIENT_CAPABILITIES = CLIENT_LONG_PASSWORD|CLIENT_LONG_FLAG|CLIENT_TRANSACTIONS - PROTO_AUTH41 = CLIENT_PROTOCOL_41 | CLIENT_SECURE_CONNECTION - - # Connection Option - OPT_CONNECT_TIMEOUT = 0 - OPT_COMPRESS = 1 - OPT_NAMED_PIPE = 2 - INIT_COMMAND = 3 - READ_DEFAULT_FILE = 4 - READ_DEFAULT_GROUP = 5 - SET_CHARSET_DIR = 6 - SET_CHARSET_NAME = 7 - OPT_LOCAL_INFILE = 8 - - # Server Status - SERVER_STATUS_IN_TRANS = 1 - SERVER_STATUS_AUTOCOMMIT = 2 - - # Refresh parameter - REFRESH_GRANT = 1 - REFRESH_LOG = 2 - REFRESH_TABLES = 4 - REFRESH_HOSTS = 8 - REFRESH_STATUS = 16 - REFRESH_THREADS = 32 - REFRESH_SLAVE = 64 - REFRESH_MASTER = 128 - - def initialize(*args) - @client_flag = 0 - @max_allowed_packet = MAX_ALLOWED_PACKET - @query_with_result = true - @status = :STATUS_READY - if args[0] != :INIT then - real_connect(*args) - end - end - - def real_connect(host=nil, user=nil, passwd=nil, db=nil, port=nil, socket=nil, flag=nil) - @server_status = SERVER_STATUS_AUTOCOMMIT - if (host == nil or host == "localhost") and defined? UNIXSocket then - unix_socket = socket || ENV["MYSQL_UNIX_PORT"] || MYSQL_UNIX_ADDR - sock = UNIXSocket::new(unix_socket) - @host_info = Error::err(Error::CR_LOCALHOST_CONNECTION) - @unix_socket = unix_socket - else - sock = TCPSocket::new(host, port||ENV["MYSQL_TCP_PORT"]||(Socket::getservbyname("mysql","tcp") rescue MYSQL_PORT)) - @host_info = sprintf Error::err(Error::CR_TCP_CONNECTION), host - end - @host = host ? host.dup : nil - sock.setsockopt Socket::SOL_SOCKET, Socket::SO_KEEPALIVE, true - @net = Net::new sock - - a = read - @protocol_version = a.slice!(0) - @server_version, a = a.split(/\0/,2) - @thread_id, @scramble_buff = a.slice!(0,13).unpack("La8") - if a.size >= 2 then - @server_capabilities, = a.slice!(0,2).unpack("v") - end - if a.size >= 16 then - @server_language, @server_status = a.slice!(0,3).unpack("cv") - end - - flag = 0 if flag == nil - flag |= @client_flag | CLIENT_CAPABILITIES - flag |= CLIENT_CONNECT_WITH_DB if db - - @pre_411 = (0 == @server_capabilities & PROTO_AUTH41) - if @pre_411 - data = Net::int2str(flag)+Net::int3str(@max_allowed_packet)+ - (user||"")+"\0"+ - scramble(passwd, @scramble_buff, @protocol_version==9) - else - dummy, @salt2 = a.unpack("a13a12") - @scramble_buff += @salt2 - flag |= PROTO_AUTH41 - data = Net::int4str(flag) + Net::int4str(@max_allowed_packet) + - ([8] + Array.new(23, 0)).pack("c24") + (user||"")+"\0"+ - scramble41(passwd, @scramble_buff) - end - - if db and @server_capabilities & CLIENT_CONNECT_WITH_DB != 0 - data << "\0" if @pre_411 - data << db - @db = db.dup - end - write data - pkt = read - handle_auth_fallback(pkt, passwd) - ObjectSpace.define_finalizer(self, Mysql.finalizer(@net)) - self - end - alias :connect :real_connect - - def handle_auth_fallback(pkt, passwd) - # A packet like this means that we need to send an old-format password - if pkt.size == 1 and pkt[0] == 254 and - @server_capabilities & CLIENT_SECURE_CONNECTION != 0 then - data = scramble(passwd, @scramble_buff, @protocol_version == 9) - write data + "\0" - read - end - end - - def escape_string(str) - Mysql::escape_string str - end - alias :quote :escape_string - - def get_client_info() - VERSION - end - alias :client_info :get_client_info - - def options(option, arg=nil) - if option == OPT_LOCAL_INFILE then - if arg == false or arg == 0 then - @client_flag &= ~CLIENT_LOCAL_FILES - else - @client_flag |= CLIENT_LOCAL_FILES - end - else - raise "not implemented" - end - end - - def real_query(query) - command COM_QUERY, query, true - read_query_result - self - end - - def use_result() - if @status != :STATUS_GET_RESULT then - error Error::CR_COMMANDS_OUT_OF_SYNC - end - res = Result::new self, @fields, @field_count - @status = :STATUS_USE_RESULT - res - end - - def store_result() - if @status != :STATUS_GET_RESULT then - error Error::CR_COMMANDS_OUT_OF_SYNC - end - @status = :STATUS_READY - data = read_rows @field_count - res = Result::new self, @fields, @field_count, data - @fields = nil - @affected_rows = data.length - res - end - - def change_user(user="", passwd="", db="") - if @pre_411 - data = user+"\0"+scramble(passwd, @scramble_buff, @protocol_version==9)+"\0"+db - else - data = user+"\0"+scramble41(passwd, @scramble_buff)+db - end - pkt = command COM_CHANGE_USER, data - handle_auth_fallback(pkt, passwd) - @user = user - @passwd = passwd - @db = db - end - - def character_set_name() - raise "not implemented" - end - - def close() - @status = :STATUS_READY - command COM_QUIT, nil, true - @net.close - self - end - - def create_db(db) - command COM_CREATE_DB, db - self - end - - def drop_db(db) - command COM_DROP_DB, db - self - end - - def dump_debug_info() - command COM_DEBUG - self - end - - def get_host_info() - @host_info - end - alias :host_info :get_host_info - - def get_proto_info() - @protocol_version - end - alias :proto_info :get_proto_info - - def get_server_info() - @server_version - end - alias :server_info :get_server_info - - def kill(id) - command COM_PROCESS_KILL, Net::int4str(id) - self - end - - def list_dbs(db=nil) - real_query "show databases #{db}" - @status = :STATUS_READY - read_rows(1).flatten - end - - def list_fields(table, field=nil) - command COM_FIELD_LIST, "#{table}\0#{field}", true - if @pre_411 - f = read_rows 6 - else - f = read_rows 7 - end - fields = unpack_fields(f, @server_capabilities & CLIENT_LONG_FLAG != 0) - res = Result::new self, fields, f.length - res.eof = true - res - end - - def list_processes() - data = command COM_PROCESS_INFO - @field_count = get_length data - if @pre_411 - fields = read_rows 5 - else - fields = read_rows 7 - end - @fields = unpack_fields(fields, @server_capabilities & CLIENT_LONG_FLAG != 0) - @status = :STATUS_GET_RESULT - store_result - end - - def list_tables(table=nil) - real_query "show tables #{table}" - @status = :STATUS_READY - read_rows(1).flatten - end - - def ping() - command COM_PING - self - end - - def query(query) - real_query query - if not @query_with_result then - return self - end - if @field_count == 0 then - return nil - end - store_result - end - - def refresh(r) - command COM_REFRESH, r.chr - self - end - - def reload() - refresh REFRESH_GRANT - self - end - - def select_db(db) - command COM_INIT_DB, db - @db = db - self - end - - def shutdown() - command COM_SHUTDOWN - self - end - - def stat() - command COM_STATISTICS - end - - attr_reader :info, :insert_id, :affected_rows, :field_count, :thread_id - attr_accessor :query_with_result, :status - - def read_one_row(field_count) - data = read - if data[0] == 254 and data.length == 1 ## EOF - return - elsif data[0] == 254 and data.length == 5 - return - end - rec = [] - field_count.times do - len = get_length data - if len == nil then - rec << len - else - rec << data.slice!(0,len) - end - end - rec - end - - def skip_result() - if @status == :STATUS_USE_RESULT then - loop do - data = read - break if data[0] == 254 and data.length == 1 - end - @status = :STATUS_READY - end - end - - def inspect() - "#<#{self.class}>" - end - - private - - def read_query_result() - data = read - @field_count = get_length(data) - if @field_count == nil then # LOAD DATA LOCAL INFILE - File::open(data) do |f| - write f.read - end - write "" # mark EOF - data = read - @field_count = get_length(data) - end - if @field_count == 0 then - @affected_rows = get_length(data, true) - @insert_id = get_length(data, true) - if @server_capabilities & CLIENT_TRANSACTIONS != 0 then - a = data.slice!(0,2) - @server_status = a[0]+a[1]*256 - end - if data.size > 0 and get_length(data) then - @info = data - end - else - @extra_info = get_length(data, true) - if @pre_411 - fields = read_rows(5) - else - fields = read_rows(7) - end - @fields = unpack_fields(fields, @server_capabilities & CLIENT_LONG_FLAG != 0) - @status = :STATUS_GET_RESULT - end - self - end - - def unpack_fields(data, long_flag_protocol) - ret = [] - data.each do |f| - if @pre_411 - table = org_table = f[0] - name = f[1] - length = f[2][0]+f[2][1]*256+f[2][2]*256*256 - type = f[3][0] - if long_flag_protocol then - flags = f[4][0]+f[4][1]*256 - decimals = f[4][2] - else - flags = f[4][0] - decimals = f[4][1] - end - def_value = f[5] - max_length = 0 - else - catalog = f[0] - db = f[1] - table = f[2] - org_table = f[3] - name = f[4] - org_name = f[5] - length = f[6][2]+f[6][3]*256+f[6][4]*256*256 - type = f[6][6] - flags = f[6][7]+f[6][8]*256 - decimals = f[6][9] - def_value = "" - max_length = 0 - end - ret << Field::new(table, org_table, name, length, type, flags, decimals, def_value, max_length) - end - ret - end - - def read_rows(field_count) - ret = [] - while rec = read_one_row(field_count) do - ret << rec - end - ret - end - - def get_length(data, longlong=nil) - return if data.length == 0 - c = data.slice!(0) - case c - when 251 - return nil - when 252 - a = data.slice!(0,2) - return a[0]+a[1]*256 - when 253 - a = data.slice!(0,3) - return a[0]+a[1]*256+a[2]*256**2 - when 254 - a = data.slice!(0,8) - if longlong then - return a[0]+a[1]*256+a[2]*256**2+a[3]*256**3+ - a[4]*256**4+a[5]*256**5+a[6]*256**6+a[7]*256**7 - else - return a[0]+a[1]*256+a[2]*256**2+a[3]*256**3 - end - else - c - end - end - - def command(cmd, arg=nil, skip_check=nil) - unless @net then - error Error::CR_SERVER_GONE_ERROR - end - if @status != :STATUS_READY then - error Error::CR_COMMANDS_OUT_OF_SYNC - end - @net.clear - write cmd.chr+(arg||"") - read unless skip_check - end - - def read() - unless @net then - error Error::CR_SERVER_GONE_ERROR - end - a = @net.read - if a[0] == 255 then - if a.length > 3 then - @errno = a[1]+a[2]*256 - @error = a[3 .. -1] - else - @errno = Error::CR_UNKNOWN_ERROR - @error = Error::err @errno - end - raise Error::new(@errno, @error) - end - a - end - - def write(arg) - unless @net then - error Error::CR_SERVER_GONE_ERROR - end - @net.write arg - end - - def hash_password(password) - nr = 1345345333 - add = 7 - nr2 = 0x12345671 - password.each_byte do |i| - next if i == 0x20 or i == 9 - nr ^= (((nr & 63) + add) * i) + (nr << 8) - nr2 += (nr2 << 8) ^ nr - add += i - end - [nr & ((1 << 31) - 1), nr2 & ((1 << 31) - 1)] - end - - def scramble(password, message, old_ver) - return "" if password == nil or password == "" - raise "old version password is not implemented" if old_ver - hash_pass = hash_password password - hash_message = hash_password message.slice(0,SCRAMBLE_LENGTH_323) - rnd = Random::new hash_pass[0] ^ hash_message[0], hash_pass[1] ^ hash_message[1] - to = [] - 1.upto(SCRAMBLE_LENGTH_323) do - to << ((rnd.rnd*31)+64).floor - end - extra = (rnd.rnd*31).floor - to.map! do |t| (t ^ extra).chr end - to.join - end - - def scramble41(password, message) - return 0x00.chr if password.nil? or password.empty? - buf = [0x14] - s1 = Digest::SHA1.digest(password) - s2 = Digest::SHA1.digest(s1) - x = Digest::SHA1.digest(message + s2) - (0..s1.length - 1).each {|i| buf.push(s1[i] ^ x[i])} - buf.pack("C*") - end - - def error(errno) - @errno = errno - @error = Error::err errno - raise Error::new(@errno, @error) - end - - class Result - def initialize(mysql, fields, field_count, data=nil) - @handle = mysql - @fields = fields - @field_count = field_count - @data = data - @current_field = 0 - @current_row = 0 - @eof = false - @row_count = 0 - end - attr_accessor :eof - - def data_seek(n) - @current_row = n - end - - def fetch_field() - return if @current_field >= @field_count - f = @fields[@current_field] - @current_field += 1 - f - end - - def fetch_fields() - @fields - end - - def fetch_field_direct(n) - @fields[n] - end - - def fetch_lengths() - @data ? @data[@current_row].map{|i| i ? i.length : 0} : @lengths - end - - def fetch_row() - if @data then - if @current_row >= @data.length then - @handle.status = :STATUS_READY - return - end - ret = @data[@current_row] - @current_row += 1 - else - return if @eof - ret = @handle.read_one_row @field_count - if ret == nil then - @eof = true - return - end - @lengths = ret.map{|i| i ? i.length : 0} - @row_count += 1 - end - ret - end - - def fetch_hash(with_table=nil) - row = fetch_row - return if row == nil - hash = {} - @fields.each_index do |i| - f = with_table ? @fields[i].table+"."+@fields[i].name : @fields[i].name - hash[f] = row[i] - end - hash - end - - def field_seek(n) - @current_field = n - end - - def field_tell() - @current_field - end - - def free() - @handle.skip_result - @handle = @fields = @data = nil - end - - def num_fields() - @field_count - end - - def num_rows() - @data ? @data.length : @row_count - end - - def row_seek(n) - @current_row = n - end - - def row_tell() - @current_row - end - - def each() - while row = fetch_row do - yield row - end - end - - def each_hash(with_table=nil) - while hash = fetch_hash(with_table) do - yield hash - end - end - - def inspect() - "#<#{self.class}>" - end - - end - - class Field - # Field type - TYPE_DECIMAL = 0 - TYPE_TINY = 1 - TYPE_SHORT = 2 - TYPE_LONG = 3 - TYPE_FLOAT = 4 - TYPE_DOUBLE = 5 - TYPE_NULL = 6 - TYPE_TIMESTAMP = 7 - TYPE_LONGLONG = 8 - TYPE_INT24 = 9 - TYPE_DATE = 10 - TYPE_TIME = 11 - TYPE_DATETIME = 12 - TYPE_YEAR = 13 - TYPE_NEWDATE = 14 - TYPE_ENUM = 247 - TYPE_SET = 248 - TYPE_TINY_BLOB = 249 - TYPE_MEDIUM_BLOB = 250 - TYPE_LONG_BLOB = 251 - TYPE_BLOB = 252 - TYPE_VAR_STRING = 253 - TYPE_STRING = 254 - TYPE_GEOMETRY = 255 - TYPE_CHAR = TYPE_TINY - TYPE_INTERVAL = TYPE_ENUM - - # Flag - NOT_NULL_FLAG = 1 - PRI_KEY_FLAG = 2 - UNIQUE_KEY_FLAG = 4 - MULTIPLE_KEY_FLAG = 8 - BLOB_FLAG = 16 - UNSIGNED_FLAG = 32 - ZEROFILL_FLAG = 64 - BINARY_FLAG = 128 - ENUM_FLAG = 256 - AUTO_INCREMENT_FLAG = 512 - TIMESTAMP_FLAG = 1024 - SET_FLAG = 2048 - NUM_FLAG = 32768 - PART_KEY_FLAG = 16384 - GROUP_FLAG = 32768 - UNIQUE_FLAG = 65536 - - def initialize(table, org_table, name, length, type, flags, decimals, def_value, max_length) - @table = table - @org_table = org_table - @name = name - @length = length - @type = type - @flags = flags - @decimals = decimals - @def = def_value - @max_length = max_length - if (type <= TYPE_INT24 and (type != TYPE_TIMESTAMP or length == 14 or length == 8)) or type == TYPE_YEAR then - @flags |= NUM_FLAG - end - end - attr_reader :table, :org_table, :name, :length, :type, :flags, :decimals, :def, :max_length - - def inspect() - "#<#{self.class}:#{@name}>" - end - end - - class Error < StandardError - # Server Error - ER_HASHCHK = 1000 - ER_NISAMCHK = 1001 - ER_NO = 1002 - ER_YES = 1003 - ER_CANT_CREATE_FILE = 1004 - ER_CANT_CREATE_TABLE = 1005 - ER_CANT_CREATE_DB = 1006 - ER_DB_CREATE_EXISTS = 1007 - ER_DB_DROP_EXISTS = 1008 - ER_DB_DROP_DELETE = 1009 - ER_DB_DROP_RMDIR = 1010 - ER_CANT_DELETE_FILE = 1011 - ER_CANT_FIND_SYSTEM_REC = 1012 - ER_CANT_GET_STAT = 1013 - ER_CANT_GET_WD = 1014 - ER_CANT_LOCK = 1015 - ER_CANT_OPEN_FILE = 1016 - ER_FILE_NOT_FOUND = 1017 - ER_CANT_READ_DIR = 1018 - ER_CANT_SET_WD = 1019 - ER_CHECKREAD = 1020 - ER_DISK_FULL = 1021 - ER_DUP_KEY = 1022 - ER_ERROR_ON_CLOSE = 1023 - ER_ERROR_ON_READ = 1024 - ER_ERROR_ON_RENAME = 1025 - ER_ERROR_ON_WRITE = 1026 - ER_FILE_USED = 1027 - ER_FILSORT_ABORT = 1028 - ER_FORM_NOT_FOUND = 1029 - ER_GET_ERRNO = 1030 - ER_ILLEGAL_HA = 1031 - ER_KEY_NOT_FOUND = 1032 - ER_NOT_FORM_FILE = 1033 - ER_NOT_KEYFILE = 1034 - ER_OLD_KEYFILE = 1035 - ER_OPEN_AS_READONLY = 1036 - ER_OUTOFMEMORY = 1037 - ER_OUT_OF_SORTMEMORY = 1038 - ER_UNEXPECTED_EOF = 1039 - ER_CON_COUNT_ERROR = 1040 - ER_OUT_OF_RESOURCES = 1041 - ER_BAD_HOST_ERROR = 1042 - ER_HANDSHAKE_ERROR = 1043 - ER_DBACCESS_DENIED_ERROR = 1044 - ER_ACCESS_DENIED_ERROR = 1045 - ER_NO_DB_ERROR = 1046 - ER_UNKNOWN_COM_ERROR = 1047 - ER_BAD_NULL_ERROR = 1048 - ER_BAD_DB_ERROR = 1049 - ER_TABLE_EXISTS_ERROR = 1050 - ER_BAD_TABLE_ERROR = 1051 - ER_NON_UNIQ_ERROR = 1052 - ER_SERVER_SHUTDOWN = 1053 - ER_BAD_FIELD_ERROR = 1054 - ER_WRONG_FIELD_WITH_GROUP = 1055 - ER_WRONG_GROUP_FIELD = 1056 - ER_WRONG_SUM_SELECT = 1057 - ER_WRONG_VALUE_COUNT = 1058 - ER_TOO_LONG_IDENT = 1059 - ER_DUP_FIELDNAME = 1060 - ER_DUP_KEYNAME = 1061 - ER_DUP_ENTRY = 1062 - ER_WRONG_FIELD_SPEC = 1063 - ER_PARSE_ERROR = 1064 - ER_EMPTY_QUERY = 1065 - ER_NONUNIQ_TABLE = 1066 - ER_INVALID_DEFAULT = 1067 - ER_MULTIPLE_PRI_KEY = 1068 - ER_TOO_MANY_KEYS = 1069 - ER_TOO_MANY_KEY_PARTS = 1070 - ER_TOO_LONG_KEY = 1071 - ER_KEY_COLUMN_DOES_NOT_EXITS = 1072 - ER_BLOB_USED_AS_KEY = 1073 - ER_TOO_BIG_FIELDLENGTH = 1074 - ER_WRONG_AUTO_KEY = 1075 - ER_READY = 1076 - ER_NORMAL_SHUTDOWN = 1077 - ER_GOT_SIGNAL = 1078 - ER_SHUTDOWN_COMPLETE = 1079 - ER_FORCING_CLOSE = 1080 - ER_IPSOCK_ERROR = 1081 - ER_NO_SUCH_INDEX = 1082 - ER_WRONG_FIELD_TERMINATORS = 1083 - ER_BLOBS_AND_NO_TERMINATED = 1084 - ER_TEXTFILE_NOT_READABLE = 1085 - ER_FILE_EXISTS_ERROR = 1086 - ER_LOAD_INFO = 1087 - ER_ALTER_INFO = 1088 - ER_WRONG_SUB_KEY = 1089 - ER_CANT_REMOVE_ALL_FIELDS = 1090 - ER_CANT_DROP_FIELD_OR_KEY = 1091 - ER_INSERT_INFO = 1092 - ER_INSERT_TABLE_USED = 1093 - ER_NO_SUCH_THREAD = 1094 - ER_KILL_DENIED_ERROR = 1095 - ER_NO_TABLES_USED = 1096 - ER_TOO_BIG_SET = 1097 - ER_NO_UNIQUE_LOGFILE = 1098 - ER_TABLE_NOT_LOCKED_FOR_WRITE = 1099 - ER_TABLE_NOT_LOCKED = 1100 - ER_BLOB_CANT_HAVE_DEFAULT = 1101 - ER_WRONG_DB_NAME = 1102 - ER_WRONG_TABLE_NAME = 1103 - ER_TOO_BIG_SELECT = 1104 - ER_UNKNOWN_ERROR = 1105 - ER_UNKNOWN_PROCEDURE = 1106 - ER_WRONG_PARAMCOUNT_TO_PROCEDURE = 1107 - ER_WRONG_PARAMETERS_TO_PROCEDURE = 1108 - ER_UNKNOWN_TABLE = 1109 - ER_FIELD_SPECIFIED_TWICE = 1110 - ER_INVALID_GROUP_FUNC_USE = 1111 - ER_UNSUPPORTED_EXTENSION = 1112 - ER_TABLE_MUST_HAVE_COLUMNS = 1113 - ER_RECORD_FILE_FULL = 1114 - ER_UNKNOWN_CHARACTER_SET = 1115 - ER_TOO_MANY_TABLES = 1116 - ER_TOO_MANY_FIELDS = 1117 - ER_TOO_BIG_ROWSIZE = 1118 - ER_STACK_OVERRUN = 1119 - ER_WRONG_OUTER_JOIN = 1120 - ER_NULL_COLUMN_IN_INDEX = 1121 - ER_CANT_FIND_UDF = 1122 - ER_CANT_INITIALIZE_UDF = 1123 - ER_UDF_NO_PATHS = 1124 - ER_UDF_EXISTS = 1125 - ER_CANT_OPEN_LIBRARY = 1126 - ER_CANT_FIND_DL_ENTRY = 1127 - ER_FUNCTION_NOT_DEFINED = 1128 - ER_HOST_IS_BLOCKED = 1129 - ER_HOST_NOT_PRIVILEGED = 1130 - ER_PASSWORD_ANONYMOUS_USER = 1131 - ER_PASSWORD_NOT_ALLOWED = 1132 - ER_PASSWORD_NO_MATCH = 1133 - ER_UPDATE_INFO = 1134 - ER_CANT_CREATE_THREAD = 1135 - ER_WRONG_VALUE_COUNT_ON_ROW = 1136 - ER_CANT_REOPEN_TABLE = 1137 - ER_INVALID_USE_OF_NULL = 1138 - ER_REGEXP_ERROR = 1139 - ER_MIX_OF_GROUP_FUNC_AND_FIELDS = 1140 - ER_NONEXISTING_GRANT = 1141 - ER_TABLEACCESS_DENIED_ERROR = 1142 - ER_COLUMNACCESS_DENIED_ERROR = 1143 - ER_ILLEGAL_GRANT_FOR_TABLE = 1144 - ER_GRANT_WRONG_HOST_OR_USER = 1145 - ER_NO_SUCH_TABLE = 1146 - ER_NONEXISTING_TABLE_GRANT = 1147 - ER_NOT_ALLOWED_COMMAND = 1148 - ER_SYNTAX_ERROR = 1149 - ER_DELAYED_CANT_CHANGE_LOCK = 1150 - ER_TOO_MANY_DELAYED_THREADS = 1151 - ER_ABORTING_CONNECTION = 1152 - ER_NET_PACKET_TOO_LARGE = 1153 - ER_NET_READ_ERROR_FROM_PIPE = 1154 - ER_NET_FCNTL_ERROR = 1155 - ER_NET_PACKETS_OUT_OF_ORDER = 1156 - ER_NET_UNCOMPRESS_ERROR = 1157 - ER_NET_READ_ERROR = 1158 - ER_NET_READ_INTERRUPTED = 1159 - ER_NET_ERROR_ON_WRITE = 1160 - ER_NET_WRITE_INTERRUPTED = 1161 - ER_TOO_LONG_STRING = 1162 - ER_TABLE_CANT_HANDLE_BLOB = 1163 - ER_TABLE_CANT_HANDLE_AUTO_INCREMENT = 1164 - ER_DELAYED_INSERT_TABLE_LOCKED = 1165 - ER_WRONG_COLUMN_NAME = 1166 - ER_WRONG_KEY_COLUMN = 1167 - ER_WRONG_MRG_TABLE = 1168 - ER_DUP_UNIQUE = 1169 - ER_BLOB_KEY_WITHOUT_LENGTH = 1170 - ER_PRIMARY_CANT_HAVE_NULL = 1171 - ER_TOO_MANY_ROWS = 1172 - ER_REQUIRES_PRIMARY_KEY = 1173 - ER_NO_RAID_COMPILED = 1174 - ER_UPDATE_WITHOUT_KEY_IN_SAFE_MODE = 1175 - ER_KEY_DOES_NOT_EXITS = 1176 - ER_CHECK_NO_SUCH_TABLE = 1177 - ER_CHECK_NOT_IMPLEMENTED = 1178 - ER_CANT_DO_THIS_DURING_AN_TRANSACTION = 1179 - ER_ERROR_DURING_COMMIT = 1180 - ER_ERROR_DURING_ROLLBACK = 1181 - ER_ERROR_DURING_FLUSH_LOGS = 1182 - ER_ERROR_DURING_CHECKPOINT = 1183 - ER_NEW_ABORTING_CONNECTION = 1184 - ER_DUMP_NOT_IMPLEMENTED = 1185 - ER_FLUSH_MASTER_BINLOG_CLOSED = 1186 - ER_INDEX_REBUILD = 1187 - ER_MASTER = 1188 - ER_MASTER_NET_READ = 1189 - ER_MASTER_NET_WRITE = 1190 - ER_FT_MATCHING_KEY_NOT_FOUND = 1191 - ER_LOCK_OR_ACTIVE_TRANSACTION = 1192 - ER_UNKNOWN_SYSTEM_VARIABLE = 1193 - ER_CRASHED_ON_USAGE = 1194 - ER_CRASHED_ON_REPAIR = 1195 - ER_WARNING_NOT_COMPLETE_ROLLBACK = 1196 - ER_TRANS_CACHE_FULL = 1197 - ER_SLAVE_MUST_STOP = 1198 - ER_SLAVE_NOT_RUNNING = 1199 - ER_BAD_SLAVE = 1200 - ER_MASTER_INFO = 1201 - ER_SLAVE_THREAD = 1202 - ER_TOO_MANY_USER_CONNECTIONS = 1203 - ER_SET_CONSTANTS_ONLY = 1204 - ER_LOCK_WAIT_TIMEOUT = 1205 - ER_LOCK_TABLE_FULL = 1206 - ER_READ_ONLY_TRANSACTION = 1207 - ER_DROP_DB_WITH_READ_LOCK = 1208 - ER_CREATE_DB_WITH_READ_LOCK = 1209 - ER_WRONG_ARGUMENTS = 1210 - ER_NO_PERMISSION_TO_CREATE_USER = 1211 - ER_UNION_TABLES_IN_DIFFERENT_DIR = 1212 - ER_LOCK_DEADLOCK = 1213 - ER_TABLE_CANT_HANDLE_FULLTEXT = 1214 - ER_CANNOT_ADD_FOREIGN = 1215 - ER_NO_REFERENCED_ROW = 1216 - ER_ROW_IS_REFERENCED = 1217 - ER_CONNECT_TO_MASTER = 1218 - ER_QUERY_ON_MASTER = 1219 - ER_ERROR_WHEN_EXECUTING_COMMAND = 1220 - ER_WRONG_USAGE = 1221 - ER_WRONG_NUMBER_OF_COLUMNS_IN_SELECT = 1222 - ER_CANT_UPDATE_WITH_READLOCK = 1223 - ER_MIXING_NOT_ALLOWED = 1224 - ER_DUP_ARGUMENT = 1225 - ER_USER_LIMIT_REACHED = 1226 - ER_SPECIFIC_ACCESS_DENIED_ERROR = 1227 - ER_LOCAL_VARIABLE = 1228 - ER_GLOBAL_VARIABLE = 1229 - ER_NO_DEFAULT = 1230 - ER_WRONG_VALUE_FOR_VAR = 1231 - ER_WRONG_TYPE_FOR_VAR = 1232 - ER_VAR_CANT_BE_READ = 1233 - ER_CANT_USE_OPTION_HERE = 1234 - ER_NOT_SUPPORTED_YET = 1235 - ER_MASTER_FATAL_ERROR_READING_BINLOG = 1236 - ER_SLAVE_IGNORED_TABLE = 1237 - ER_ERROR_MESSAGES = 238 - - # Client Error - CR_MIN_ERROR = 2000 - CR_MAX_ERROR = 2999 - CR_UNKNOWN_ERROR = 2000 - CR_SOCKET_CREATE_ERROR = 2001 - CR_CONNECTION_ERROR = 2002 - CR_CONN_HOST_ERROR = 2003 - CR_IPSOCK_ERROR = 2004 - CR_UNKNOWN_HOST = 2005 - CR_SERVER_GONE_ERROR = 2006 - CR_VERSION_ERROR = 2007 - CR_OUT_OF_MEMORY = 2008 - CR_WRONG_HOST_INFO = 2009 - CR_LOCALHOST_CONNECTION = 2010 - CR_TCP_CONNECTION = 2011 - CR_SERVER_HANDSHAKE_ERR = 2012 - CR_SERVER_LOST = 2013 - CR_COMMANDS_OUT_OF_SYNC = 2014 - CR_NAMEDPIPE_CONNECTION = 2015 - CR_NAMEDPIPEWAIT_ERROR = 2016 - CR_NAMEDPIPEOPEN_ERROR = 2017 - CR_NAMEDPIPESETSTATE_ERROR = 2018 - CR_CANT_READ_CHARSET = 2019 - CR_NET_PACKET_TOO_LARGE = 2020 - CR_EMBEDDED_CONNECTION = 2021 - CR_PROBE_SLAVE_STATUS = 2022 - CR_PROBE_SLAVE_HOSTS = 2023 - CR_PROBE_SLAVE_CONNECT = 2024 - CR_PROBE_MASTER_CONNECT = 2025 - CR_SSL_CONNECTION_ERROR = 2026 - CR_MALFORMED_PACKET = 2027 - - CLIENT_ERRORS = [ - "Unknown MySQL error", - "Can't create UNIX socket (%d)", - "Can't connect to local MySQL server through socket '%-.64s' (%d)", - "Can't connect to MySQL server on '%-.64s' (%d)", - "Can't create TCP/IP socket (%d)", - "Unknown MySQL Server Host '%-.64s' (%d)", - "MySQL server has gone away", - "Protocol mismatch. Server Version = %d Client Version = %d", - "MySQL client run out of memory", - "Wrong host info", - "Localhost via UNIX socket", - "%-.64s via TCP/IP", - "Error in server handshake", - "Lost connection to MySQL server during query", - "Commands out of sync; You can't run this command now", - "%-.64s via named pipe", - "Can't wait for named pipe to host: %-.64s pipe: %-.32s (%lu)", - "Can't open named pipe to host: %-.64s pipe: %-.32s (%lu)", - "Can't set state of named pipe to host: %-.64s pipe: %-.32s (%lu)", - "Can't initialize character set %-.64s (path: %-.64s)", - "Got packet bigger than 'max_allowed_packet'", - "Embedded server", - "Error on SHOW SLAVE STATUS:", - "Error on SHOW SLAVE HOSTS:", - "Error connecting to slave:", - "Error connecting to master:", - "SSL connection error", - "Malformed packet" - ] - - def initialize(errno, error) - @errno = errno - @error = error - super error - end - attr_reader :errno, :error - - def Error::err(errno) - CLIENT_ERRORS[errno - Error::CR_MIN_ERROR] - end - end - - class Net - def initialize(sock) - @sock = sock - @pkt_nr = 0 - end - - def clear() - @pkt_nr = 0 - end - - def read() - buf = [] - len = nil - @sock.sync = false - while len == nil or len == MAX_PACKET_LENGTH do - a = @sock.read(4) - len = a[0]+a[1]*256+a[2]*256*256 - pkt_nr = a[3] - if @pkt_nr != pkt_nr then - raise "Packets out of order: #{@pkt_nr}<>#{pkt_nr}" - end - @pkt_nr = @pkt_nr + 1 & 0xff - buf << @sock.read(len) - end - @sock.sync = true - buf.join - rescue - errno = Error::CR_SERVER_LOST - raise Error::new(errno, Error::err(errno)) - end - - def write(data) - if data.is_a? Array then - data = data.join - end - @sock.sync = false - ptr = 0 - while data.length >= MAX_PACKET_LENGTH do - @sock.write Net::int3str(MAX_PACKET_LENGTH)+@pkt_nr.chr+data[ptr, MAX_PACKET_LENGTH] - @pkt_nr = @pkt_nr + 1 & 0xff - ptr += MAX_PACKET_LENGTH - end - @sock.write Net::int3str(data.length-ptr)+@pkt_nr.chr+data[ptr .. -1] - @pkt_nr = @pkt_nr + 1 & 0xff - @sock.sync = true - @sock.flush - rescue - errno = Error::CR_SERVER_LOST - raise Error::new(errno, Error::err(errno)) - end - - def close() - @sock.close - end - - def Net::int2str(n) - [n].pack("v") - end - - def Net::int3str(n) - [n%256, n>>8].pack("cv") - end - - def Net::int4str(n) - [n].pack("V") - end - - end - - class Random - def initialize(seed1, seed2) - @max_value = 0x3FFFFFFF - @seed1 = seed1 % @max_value - @seed2 = seed2 % @max_value - end - - def rnd() - @seed1 = (@seed1*3+@seed2) % @max_value - @seed2 = (@seed1+@seed2+33) % @max_value - @seed1.to_f / @max_value - end - end - -end - -class << Mysql - def init() - Mysql::new :INIT - end - - def real_connect(*args) - Mysql::new(*args) - end - alias :connect :real_connect - - def finalizer(net) - proc { - net.clear - begin - net.write(Mysql::COM_QUIT.chr) - net.close - rescue # Ignore IOError if socket is already closed. - end - } - end - - def escape_string(str) - str.gsub(/([\0\n\r\032\'\"\\])/) do - case $1 - when "\0" then "\\0" - when "\n" then "\\n" - when "\r" then "\\r" - when "\032" then "\\Z" - else "\\"+$1 - end - end - end - alias :quote :escape_string - - def get_client_info() - Mysql::VERSION - end - alias :client_info :get_client_info - - def debug(str) - raise "not implemented" - end -end - -# -# for compatibility -# - -MysqlRes = Mysql::Result -MysqlField = Mysql::Field -MysqlError = Mysql::Error From d9fb021845c0481c5119eebdc534aec427072f7d Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Sun, 8 Jun 2008 16:02:49 -0700 Subject: [PATCH 070/200] Remove dead, unused vendor/db2.rb --- activerecord/lib/active_record/vendor/db2.rb | 362 ------------------- 1 file changed, 362 deletions(-) delete mode 100644 activerecord/lib/active_record/vendor/db2.rb diff --git a/activerecord/lib/active_record/vendor/db2.rb b/activerecord/lib/active_record/vendor/db2.rb deleted file mode 100644 index 812c8cc5179b6..0000000000000 --- a/activerecord/lib/active_record/vendor/db2.rb +++ /dev/null @@ -1,362 +0,0 @@ -require 'db2/db2cli.rb' - -module DB2 - module DB2Util - include DB2CLI - - def free() SQLFreeHandle(@handle_type, @handle); end - def handle() @handle; end - - def check_rc(rc) - if ![SQL_SUCCESS, SQL_SUCCESS_WITH_INFO, SQL_NO_DATA_FOUND].include?(rc) - rec = 1 - msg = '' - loop do - a = SQLGetDiagRec(@handle_type, @handle, rec, 500) - break if a[0] != SQL_SUCCESS - msg << a[3] if !a[3].nil? and a[3] != '' # Create message. - rec += 1 - end - raise "DB2 error: #{msg}" - end - end - end - - class Environment - include DB2Util - - def initialize - @handle_type = SQL_HANDLE_ENV - rc, @handle = SQLAllocHandle(@handle_type, SQL_NULL_HANDLE) - check_rc(rc) - end - - def data_sources(buffer_length = 1024) - retval = [] - max_buffer_length = buffer_length - - a = SQLDataSources(@handle, SQL_FETCH_FIRST, SQL_MAX_DSN_LENGTH + 1, buffer_length) - retval << [a[1], a[3]] - max_buffer_length = [max_buffer_length, a[4]].max - - loop do - a = SQLDataSources(@handle, SQL_FETCH_NEXT, SQL_MAX_DSN_LENGTH + 1, buffer_length) - break if a[0] == SQL_NO_DATA_FOUND - - retval << [a[1], a[3]] - max_buffer_length = [max_buffer_length, a[4]].max - end - - if max_buffer_length > buffer_length - get_data_sources(max_buffer_length) - else - retval - end - end - end - - class Connection - include DB2Util - - def initialize(environment) - @env = environment - @handle_type = SQL_HANDLE_DBC - rc, @handle = SQLAllocHandle(@handle_type, @env.handle) - check_rc(rc) - end - - def connect(server_name, user_name = '', auth = '') - check_rc(SQLConnect(@handle, server_name, user_name.to_s, auth.to_s)) - end - - def set_connect_attr(attr, value) - value += "\0" if value.class == String - check_rc(SQLSetConnectAttr(@handle, attr, value)) - end - - def set_auto_commit_on - set_connect_attr(SQL_ATTR_AUTOCOMMIT, SQL_AUTOCOMMIT_ON) - end - - def set_auto_commit_off - set_connect_attr(SQL_ATTR_AUTOCOMMIT, SQL_AUTOCOMMIT_OFF) - end - - def disconnect - check_rc(SQLDisconnect(@handle)) - end - - def rollback - check_rc(SQLEndTran(@handle_type, @handle, SQL_ROLLBACK)) - end - - def commit - check_rc(SQLEndTran(@handle_type, @handle, SQL_COMMIT)) - end - end - - class Statement - include DB2Util - - def initialize(connection) - @conn = connection - @handle_type = SQL_HANDLE_STMT - @parms = [] #yun - @sql = '' #yun - @numParms = 0 #yun - @prepared = false #yun - @parmArray = [] #yun. attributes of the parameter markers - rc, @handle = SQLAllocHandle(@handle_type, @conn.handle) - check_rc(rc) - end - - def columns(table_name, schema_name = '%') - check_rc(SQLColumns(@handle, '', schema_name.upcase, table_name.upcase, '%')) - fetch_all - end - - def tables(schema_name = '%') - check_rc(SQLTables(@handle, '', schema_name.upcase, '%', 'TABLE')) - fetch_all - end - - def indexes(table_name, schema_name = '') - check_rc(SQLStatistics(@handle, '', schema_name.upcase, table_name.upcase, SQL_INDEX_ALL, SQL_ENSURE)) - fetch_all - end - - def prepare(sql) - @sql = sql - check_rc(SQLPrepare(@handle, sql)) - rc, @numParms = SQLNumParams(@handle) #number of question marks - check_rc(rc) - #-------------------------------------------------------------------------- - # parameter attributes are stored in instance variable @parmArray so that - # they are available when execute method is called. - #-------------------------------------------------------------------------- - if @numParms > 0 # get parameter marker attributes - 1.upto(@numParms) do |i| # parameter number starts from 1 - rc, type, size, decimalDigits = SQLDescribeParam(@handle, i) - check_rc(rc) - @parmArray << Parameter.new(type, size, decimalDigits) - end - end - @prepared = true - self - end - - def execute(*parms) - raise "The statement was not prepared" if @prepared == false - - if parms.size == 1 and parms[0].class == Array - parms = parms[0] - end - - if @numParms != parms.size - raise "Number of parameters supplied does not match with the SQL statement" - end - - if @numParms > 0 #need to bind parameters - #-------------------------------------------------------------------- - #calling bindParms may not be safe. Look comment below. - #-------------------------------------------------------------------- - #bindParms(parms) - - valueArray = [] - 1.upto(@numParms) do |i| # parameter number starts from 1 - type = @parmArray[i - 1].class - size = @parmArray[i - 1].size - decimalDigits = @parmArray[i - 1].decimalDigits - - if parms[i - 1].class == String - valueArray << parms[i - 1] - else - valueArray << parms[i - 1].to_s - end - - rc = SQLBindParameter(@handle, i, type, size, decimalDigits, valueArray[i - 1]) - check_rc(rc) - end - end - - check_rc(SQLExecute(@handle)) - - if @numParms != 0 - check_rc(SQLFreeStmt(@handle, SQL_RESET_PARAMS)) # Reset parameters - end - - self - end - - #------------------------------------------------------------------------------- - # The last argument(value) to SQLBindParameter is a deferred argument, that is, - # it should be available when SQLExecute is called. Even though "value" is - # local to bindParms method, it seems that it is available when SQLExecute - # is called. I am not sure whether it would still work if garbage collection - # is done between bindParms call and SQLExecute call inside the execute method - # above. - #------------------------------------------------------------------------------- - def bindParms(parms) # This is the real thing. It uses SQLBindParms - 1.upto(@numParms) do |i| # parameter number starts from 1 - rc, dataType, parmSize, decimalDigits = SQLDescribeParam(@handle, i) - check_rc(rc) - if parms[i - 1].class == String - value = parms[i - 1] - else - value = parms[i - 1].to_s - end - rc = SQLBindParameter(@handle, i, dataType, parmSize, decimalDigits, value) - check_rc(rc) - end - end - - #------------------------------------------------------------------------------ - # bind method does not use DB2's SQLBindParams, but replaces "?" in the - # SQL statement with the value before passing the SQL statement to DB2. - # It is not efficient and can handle only strings since it puts everything in - # quotes. - #------------------------------------------------------------------------------ - def bind(sql, args) #does not use SQLBindParams - arg_index = 0 - result = "" - tokens(sql).each do |part| - case part - when '?' - result << "'" + (args[arg_index]) + "'" #put it into quotes - arg_index += 1 - when '??' - result << "?" - else - result << part - end - end - if arg_index < args.size - raise "Too many SQL parameters" - elsif arg_index > args.size - raise "Not enough SQL parameters" - end - result - end - - ## Break the sql string into parts. - # - # This is NOT a full lexer for SQL. It just breaks up the SQL - # string enough so that question marks, double question marks and - # quoted strings are separated. This is used when binding - # arguments to "?" in the SQL string. Note: comments are not - # handled. - # - def tokens(sql) - toks = sql.scan(/('([^'\\]|''|\\.)*'|"([^"\\]|""|\\.)*"|\?\??|[^'"?]+)/) - toks.collect { |t| t[0] } - end - - def exec_direct(sql) - check_rc(SQLExecDirect(@handle, sql)) - self - end - - def set_cursor_name(name) - check_rc(SQLSetCursorName(@handle, name)) - self - end - - def get_cursor_name - rc, name = SQLGetCursorName(@handle) - check_rc(rc) - name - end - - def row_count - rc, rowcount = SQLRowCount(@handle) - check_rc(rc) - rowcount - end - - def num_result_cols - rc, cols = SQLNumResultCols(@handle) - check_rc(rc) - cols - end - - def fetch_all - if block_given? - while row = fetch do - yield row - end - else - res = [] - while row = fetch do - res << row - end - res - end - end - - def fetch - cols = get_col_desc - rc = SQLFetch(@handle) - if rc == SQL_NO_DATA_FOUND - SQLFreeStmt(@handle, SQL_CLOSE) # Close cursor - SQLFreeStmt(@handle, SQL_RESET_PARAMS) # Reset parameters - return nil - end - raise "ERROR" unless rc == SQL_SUCCESS - - retval = [] - cols.each_with_index do |c, i| - rc, content = SQLGetData(@handle, i + 1, c[1], c[2] + 1) #yun added 1 to c[2] - retval << adjust_content(content) - end - retval - end - - def fetch_as_hash - cols = get_col_desc - rc = SQLFetch(@handle) - if rc == SQL_NO_DATA_FOUND - SQLFreeStmt(@handle, SQL_CLOSE) # Close cursor - SQLFreeStmt(@handle, SQL_RESET_PARAMS) # Reset parameters - return nil - end - raise "ERROR" unless rc == SQL_SUCCESS - - retval = {} - cols.each_with_index do |c, i| - rc, content = SQLGetData(@handle, i + 1, c[1], c[2] + 1) #yun added 1 to c[2] - retval[c[0]] = adjust_content(content) - end - retval - end - - def get_col_desc - rc, nr_cols = SQLNumResultCols(@handle) - cols = (1..nr_cols).collect do |c| - rc, name, bl, type, col_sz = SQLDescribeCol(@handle, c, 1024) - [name.downcase, type, col_sz] - end - end - - def adjust_content(c) - case c.class.to_s - when 'DB2CLI::NullClass' - return nil - when 'DB2CLI::Time' - "%02d:%02d:%02d" % [c.hour, c.minute, c.second] - when 'DB2CLI::Date' - "%04d-%02d-%02d" % [c.year, c.month, c.day] - when 'DB2CLI::Timestamp' - "%04d-%02d-%02d %02d:%02d:%02d" % [c.year, c.month, c.day, c.hour, c.minute, c.second] - else - return c - end - end - end - - class Parameter - attr_reader :type, :size, :decimalDigits - def initialize(type, size, decimalDigits) - @type, @size, @decimalDigits = type, size, decimalDigits - end - end -end From 8bf74c30fe276606214850ae2de76fe0efb08d94 Mon Sep 17 00:00:00 2001 From: rick Date: Sun, 8 Jun 2008 22:26:30 -0400 Subject: [PATCH 071/200] change some more TimeZone references to ActiveSupport::TimeZone --- actionpack/lib/action_view/helpers/form_options_helper.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/actionpack/lib/action_view/helpers/form_options_helper.rb b/actionpack/lib/action_view/helpers/form_options_helper.rb index e0a097e367b9d..90bd99a861419 100644 --- a/actionpack/lib/action_view/helpers/form_options_helper.rb +++ b/actionpack/lib/action_view/helpers/form_options_helper.rb @@ -304,7 +304,7 @@ def country_options_for_select(selected = nil, priority_countries = nil) # # NOTE: Only the option tags are returned, you have to wrap this call in # a regular HTML select tag. - def time_zone_options_for_select(selected = nil, priority_zones = nil, model = TimeZone) + def time_zone_options_for_select(selected = nil, priority_zones = nil, model = ActiveSupport::TimeZone) zone_options = "" zones = model.all @@ -417,7 +417,7 @@ def to_time_zone_select_tag(priority_zones, options, html_options) value = value(object) content_tag("select", add_options( - time_zone_options_for_select(value || options[:default], priority_zones, options[:model] || TimeZone), + time_zone_options_for_select(value || options[:default], priority_zones, options[:model] || ActiveSupport::TimeZone), options, value ), html_options ) From ff5f155f8dc1d2ba363718c3e17f99719399eab5 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Sun, 8 Jun 2008 20:05:39 -0700 Subject: [PATCH 072/200] Use output_buffer reader and writer methods exclusively instead of hitting the instance variable so others can override the methods. --- .../lib/action_view/helpers/capture_helper.rb | 6 +- .../action_view/helpers/javascript_helper.rb | 5 - .../lib/action_view/helpers/tag_helper.rb | 4 - .../lib/action_view/helpers/text_helper.rb | 4 +- .../template_handlers/compilable.rb | 2 +- actionpack/lib/action_view/test_case.rb | 5 + actionpack/test/template/date_helper_test.rb | 12 +- actionpack/test/template/form_helper_test.rb | 251 +++++++----------- .../test/template/form_options_helper_test.rb | 24 +- .../test/template/form_tag_helper_test.rb | 29 +- .../test/template/javascript_helper_test.rb | 14 +- .../test/template/prototype_helper_test.rb | 20 +- .../test/template/record_tag_helper_test.rb | 9 +- actionpack/test/template/tag_helper_test.rb | 10 +- actionpack/test/template/text_helper_test.rb | 6 +- 15 files changed, 159 insertions(+), 242 deletions(-) diff --git a/actionpack/lib/action_view/helpers/capture_helper.rb b/actionpack/lib/action_view/helpers/capture_helper.rb index 25e62f78fb831..ab453fadf2f40 100644 --- a/actionpack/lib/action_view/helpers/capture_helper.rb +++ b/actionpack/lib/action_view/helpers/capture_helper.rb @@ -31,7 +31,7 @@ module CaptureHelper # # def capture(*args, &block) - if @output_buffer + if output_buffer with_output_buffer { block.call(*args) } else block.call(*args) @@ -121,10 +121,10 @@ def content_for(name, content = nil, &block) private def with_output_buffer(buf = '') - @output_buffer, old_buffer = buf, @output_buffer + self.output_buffer, old_buffer = buf, output_buffer yield ensure - @output_buffer = old_buffer + self.output_buffer = old_buffer end end end diff --git a/actionpack/lib/action_view/helpers/javascript_helper.rb b/actionpack/lib/action_view/helpers/javascript_helper.rb index 85b205c264cf4..7404a251e492d 100644 --- a/actionpack/lib/action_view/helpers/javascript_helper.rb +++ b/actionpack/lib/action_view/helpers/javascript_helper.rb @@ -202,11 +202,6 @@ def array_or_string_for_javascript(option) end js_option end - - private - def block_is_within_action_view?(block) - !@output_buffer.nil? - end end JavascriptHelper = JavaScriptHelper unless const_defined? :JavascriptHelper diff --git a/actionpack/lib/action_view/helpers/tag_helper.rb b/actionpack/lib/action_view/helpers/tag_helper.rb index a8a5987b1f318..e1abec1847ed4 100644 --- a/actionpack/lib/action_view/helpers/tag_helper.rb +++ b/actionpack/lib/action_view/helpers/tag_helper.rb @@ -122,10 +122,6 @@ def tag_options(options, escape = true) " #{attrs.sort * ' '}" unless attrs.empty? end end - - def block_is_within_action_view?(block) - !@output_buffer.nil? - end end end end diff --git a/actionpack/lib/action_view/helpers/text_helper.rb b/actionpack/lib/action_view/helpers/text_helper.rb index f81f7eded6f8a..85a36726781f3 100644 --- a/actionpack/lib/action_view/helpers/text_helper.rb +++ b/actionpack/lib/action_view/helpers/text_helper.rb @@ -26,8 +26,8 @@ module TextHelper # # will either display "Logged in!" or a login link # %> def concat(string) - if @output_buffer && string - @output_buffer << string + if output_buffer && string + output_buffer << string else string end diff --git a/actionpack/lib/action_view/template_handlers/compilable.rb b/actionpack/lib/action_view/template_handlers/compilable.rb index 28c72172a0af7..1aef81ba1a410 100644 --- a/actionpack/lib/action_view/template_handlers/compilable.rb +++ b/actionpack/lib/action_view/template_handlers/compilable.rb @@ -106,7 +106,7 @@ def create_template_source(template, render_symbol) locals_code << "#{key} = local_assigns[:#{key}]\n" end - "def #{render_symbol}(local_assigns)\nold_output_buffer = @output_buffer;#{locals_code}#{body}\nensure\n@output_buffer = old_output_buffer\nend" + "def #{render_symbol}(local_assigns)\nold_output_buffer = output_buffer;#{locals_code}#{body}\nensure\nself.output_buffer = old_output_buffer\nend" end # Return true if the given template was compiled for a superset of the keys in local_assigns diff --git a/actionpack/lib/action_view/test_case.rb b/actionpack/lib/action_view/test_case.rb index 16fedd9732758..1a3c93c283812 100644 --- a/actionpack/lib/action_view/test_case.rb +++ b/actionpack/lib/action_view/test_case.rb @@ -37,6 +37,8 @@ def setup_with_helper_class if helper_class && !self.class.ancestors.include?(helper_class) self.class.send(:include, helper_class) end + + self.output_buffer = '' end class TestController < ActionController::Base @@ -48,6 +50,9 @@ def initialize end end + protected + attr_accessor :output_buffer + private def method_missing(selector, *args) controller = TestController.new diff --git a/actionpack/test/template/date_helper_test.rb b/actionpack/test/template/date_helper_test.rb index 3399b03dd2a01..11b3bdb3fa1e0 100755 --- a/actionpack/test/template/date_helper_test.rb +++ b/actionpack/test/template/date_helper_test.rb @@ -1002,17 +1002,15 @@ def test_date_select_within_fields_for @post = Post.new @post.written_on = Date.new(2004, 6, 15) - @output_buffer = '' - fields_for :post, @post do |f| - @output_buffer.concat f.date_select(:written_on) + concat f.date_select(:written_on) end expected = "\n" expected << "\n" expected << "\n" - assert_dom_equal(expected, @output_buffer) + assert_dom_equal(expected, output_buffer) end def test_date_select_with_index @@ -1287,10 +1285,8 @@ def test_datetime_select_within_fields_for @post = Post.new @post.updated_at = Time.local(2004, 6, 15, 16, 35) - @output_buffer = '' - fields_for :post, @post do |f| - @output_buffer.concat f.datetime_select(:updated_at) + concat f.datetime_select(:updated_at) end expected = "\n" @@ -1299,7 +1295,7 @@ def test_datetime_select_within_fields_for expected << " — \n" expected << " : \n" - assert_dom_equal(expected, @output_buffer) + assert_dom_equal(expected, output_buffer) end def test_date_select_with_zero_value_and_no_start_year diff --git a/actionpack/test/template/form_helper_test.rb b/actionpack/test/template/form_helper_test.rb index 65984fac44f53..39649c3622641 100644 --- a/actionpack/test/template/form_helper_test.rb +++ b/actionpack/test/template/form_helper_test.rb @@ -337,14 +337,12 @@ def test_auto_index end def test_form_for - @output_buffer = '' - form_for(:post, @post, :html => { :id => 'create-post' }) do |f| - @output_buffer.concat f.label(:title) - @output_buffer.concat f.text_field(:title) - @output_buffer.concat f.text_area(:body) - @output_buffer.concat f.check_box(:secret) - @output_buffer.concat f.submit('Create post') + concat f.label(:title) + concat f.text_field(:title) + concat f.text_area(:body) + concat f.check_box(:secret) + concat f.submit('Create post') end expected = @@ -357,16 +355,14 @@ def test_form_for "" + "" - assert_dom_equal expected, @output_buffer + assert_dom_equal expected, output_buffer end def test_form_for_with_method - @output_buffer = '' - form_for(:post, @post, :html => { :id => 'create-post', :method => :put }) do |f| - @output_buffer.concat f.text_field(:title) - @output_buffer.concat f.text_area(:body) - @output_buffer.concat f.check_box(:secret) + concat f.text_field(:title) + concat f.text_area(:body) + concat f.check_box(:secret) end expected = @@ -378,16 +374,14 @@ def test_form_for_with_method "" + "" - assert_dom_equal expected, @output_buffer + assert_dom_equal expected, output_buffer end def test_form_for_without_object - @output_buffer = '' - form_for(:post, :html => { :id => 'create-post' }) do |f| - @output_buffer.concat f.text_field(:title) - @output_buffer.concat f.text_area(:body) - @output_buffer.concat f.check_box(:secret) + concat f.text_field(:title) + concat f.text_area(:body) + concat f.check_box(:secret) end expected = @@ -398,17 +392,15 @@ def test_form_for_without_object "" + "" - assert_dom_equal expected, @output_buffer + assert_dom_equal expected, output_buffer end def test_form_for_with_index - @output_buffer = '' - form_for("post[]", @post) do |f| - @output_buffer.concat f.label(:title) - @output_buffer.concat f.text_field(:title) - @output_buffer.concat f.text_area(:body) - @output_buffer.concat f.check_box(:secret) + concat f.label(:title) + concat f.text_field(:title) + concat f.text_area(:body) + concat f.check_box(:secret) end expected = @@ -420,16 +412,14 @@ def test_form_for_with_index "" + "" - assert_dom_equal expected, @output_buffer + assert_dom_equal expected, output_buffer end def test_form_for_with_nil_index_option_override - @output_buffer = '' - form_for("post[]", @post, :index => nil) do |f| - @output_buffer.concat f.text_field(:title) - @output_buffer.concat f.text_area(:body) - @output_buffer.concat f.check_box(:secret) + concat f.text_field(:title) + concat f.text_area(:body) + concat f.check_box(:secret) end expected = @@ -440,14 +430,13 @@ def test_form_for_with_nil_index_option_override "" + "" - assert_dom_equal expected, @output_buffer + assert_dom_equal expected, output_buffer end def test_nested_fields_for - @output_buffer = '' form_for(:post, @post) do |f| f.fields_for(:comment, @post) do |c| - @output_buffer.concat c.text_field(:title) + concat c.text_field(:title) end end @@ -455,16 +444,14 @@ def test_nested_fields_for "" + "" - assert_dom_equal expected, @output_buffer + assert_dom_equal expected, output_buffer end def test_fields_for - @output_buffer = '' - fields_for(:post, @post) do |f| - @output_buffer.concat f.text_field(:title) - @output_buffer.concat f.text_area(:body) - @output_buffer.concat f.check_box(:secret) + concat f.text_field(:title) + concat f.text_area(:body) + concat f.check_box(:secret) end expected = @@ -473,16 +460,14 @@ def test_fields_for "" + "" - assert_dom_equal expected, @output_buffer + assert_dom_equal expected, output_buffer end def test_fields_for_with_index - @output_buffer = '' - fields_for("post[]", @post) do |f| - @output_buffer.concat f.text_field(:title) - @output_buffer.concat f.text_area(:body) - @output_buffer.concat f.check_box(:secret) + concat f.text_field(:title) + concat f.text_area(:body) + concat f.check_box(:secret) end expected = @@ -491,16 +476,14 @@ def test_fields_for_with_index "" + "" - assert_dom_equal expected, @output_buffer + assert_dom_equal expected, output_buffer end def test_fields_for_with_nil_index_option_override - @output_buffer = '' - fields_for("post[]", @post, :index => nil) do |f| - @output_buffer.concat f.text_field(:title) - @output_buffer.concat f.text_area(:body) - @output_buffer.concat f.check_box(:secret) + concat f.text_field(:title) + concat f.text_area(:body) + concat f.check_box(:secret) end expected = @@ -509,16 +492,14 @@ def test_fields_for_with_nil_index_option_override "" + "" - assert_dom_equal expected, @output_buffer + assert_dom_equal expected, output_buffer end def test_fields_for_with_index_option_override - @output_buffer = '' - fields_for("post[]", @post, :index => "abc") do |f| - @output_buffer.concat f.text_field(:title) - @output_buffer.concat f.text_area(:body) - @output_buffer.concat f.check_box(:secret) + concat f.text_field(:title) + concat f.text_area(:body) + concat f.check_box(:secret) end expected = @@ -527,15 +508,14 @@ def test_fields_for_with_index_option_override "" + "" - assert_dom_equal expected, @output_buffer + assert_dom_equal expected, output_buffer end def test_fields_for_without_object - @output_buffer = '' fields_for(:post) do |f| - @output_buffer.concat f.text_field(:title) - @output_buffer.concat f.text_area(:body) - @output_buffer.concat f.check_box(:secret) + concat f.text_field(:title) + concat f.text_area(:body) + concat f.check_box(:secret) end expected = @@ -544,15 +524,14 @@ def test_fields_for_without_object "" + "" - assert_dom_equal expected, @output_buffer + assert_dom_equal expected, output_buffer end def test_fields_for_with_only_object - @output_buffer = '' fields_for(@post) do |f| - @output_buffer.concat f.text_field(:title) - @output_buffer.concat f.text_area(:body) - @output_buffer.concat f.check_box(:secret) + concat f.text_field(:title) + concat f.text_area(:body) + concat f.check_box(:secret) end expected = @@ -561,31 +540,29 @@ def test_fields_for_with_only_object "" + "" - assert_dom_equal expected, @output_buffer + assert_dom_equal expected, output_buffer end def test_fields_for_object_with_bracketed_name - @output_buffer = '' fields_for("author[post]", @post) do |f| - @output_buffer.concat f.label(:title) - @output_buffer.concat f.text_field(:title) + concat f.label(:title) + concat f.text_field(:title) end assert_dom_equal "" + "", - @output_buffer + output_buffer end def test_fields_for_object_with_bracketed_name_and_index - @output_buffer = '' fields_for("author[post]", @post, :index => 1) do |f| - @output_buffer.concat f.label(:title) - @output_buffer.concat f.text_field(:title) + concat f.label(:title) + concat f.text_field(:title) end assert_dom_equal "" + "", - @output_buffer + output_buffer end def test_form_builder_does_not_have_form_for_method @@ -593,14 +570,12 @@ def test_form_builder_does_not_have_form_for_method end def test_form_for_and_fields_for - @output_buffer = '' - form_for(:post, @post, :html => { :id => 'create-post' }) do |post_form| - @output_buffer.concat post_form.text_field(:title) - @output_buffer.concat post_form.text_area(:body) + concat post_form.text_field(:title) + concat post_form.text_area(:body) fields_for(:parent_post, @post) do |parent_fields| - @output_buffer.concat parent_fields.check_box(:secret) + concat parent_fields.check_box(:secret) end end @@ -612,18 +587,16 @@ def test_form_for_and_fields_for "" + "" - assert_dom_equal expected, @output_buffer + assert_dom_equal expected, output_buffer end def test_form_for_and_fields_for_with_object - @output_buffer = '' - form_for(:post, @post, :html => { :id => 'create-post' }) do |post_form| - @output_buffer.concat post_form.text_field(:title) - @output_buffer.concat post_form.text_area(:body) + concat post_form.text_field(:title) + concat post_form.text_area(:body) post_form.fields_for(@comment) do |comment_fields| - @output_buffer.concat comment_fields.text_field(:name) + concat comment_fields.text_field(:name) end end @@ -634,7 +607,7 @@ def test_form_for_and_fields_for_with_object "" + "" - assert_dom_equal expected, @output_buffer + assert_dom_equal expected, output_buffer end class LabelledFormBuilder < ActionView::Helpers::FormBuilder @@ -649,12 +622,10 @@ def #{selector}(field, *args, &proc) end def test_form_for_with_labelled_builder - @output_buffer = '' - form_for(:post, @post, :builder => LabelledFormBuilder) do |f| - @output_buffer.concat f.text_field(:title) - @output_buffer.concat f.text_area(:body) - @output_buffer.concat f.check_box(:secret) + concat f.text_field(:title) + concat f.text_area(:body) + concat f.check_box(:secret) end expected = @@ -665,18 +636,17 @@ def test_form_for_with_labelled_builder "
      " + "" - assert_dom_equal expected, @output_buffer + assert_dom_equal expected, output_buffer end def test_default_form_builder old_default_form_builder, ActionView::Base.default_form_builder = ActionView::Base.default_form_builder, LabelledFormBuilder - @output_buffer = '' form_for(:post, @post) do |f| - @output_buffer.concat f.text_field(:title) - @output_buffer.concat f.text_area(:body) - @output_buffer.concat f.check_box(:secret) + concat f.text_field(:title) + concat f.text_area(:body) + concat f.check_box(:secret) end expected = @@ -687,17 +657,15 @@ def test_default_form_builder "
      " + "" - assert_dom_equal expected, @output_buffer + assert_dom_equal expected, output_buffer ensure ActionView::Base.default_form_builder = old_default_form_builder end def test_default_form_builder_with_active_record_helpers - - @output_buffer = '' form_for(:post, @post) do |f| - @output_buffer.concat f.error_message_on('author_name') - @output_buffer.concat f.error_messages + concat f.error_message_on('author_name') + concat f.error_messages end expected = %(
      ) + @@ -705,7 +673,7 @@ def test_default_form_builder_with_active_record_helpers %(

      1 error prohibited this post from being saved

      There were problems with the following fields:

      • Author name can't be empty
      ) + %(
      ) - assert_dom_equal expected, @output_buffer + assert_dom_equal expected, output_buffer end @@ -713,10 +681,9 @@ def test_default_form_builder_no_instance_variable post = @post @post = nil - @output_buffer = '' form_for(:post, post) do |f| - @output_buffer.concat f.error_message_on('author_name') - @output_buffer.concat f.error_messages + concat f.error_message_on('author_name') + concat f.error_messages end expected = %(
      ) + @@ -724,19 +691,18 @@ def test_default_form_builder_no_instance_variable %(

      1 error prohibited this post from being saved

      There were problems with the following fields:

      • Author name can't be empty
      ) + %(
      ) - assert_dom_equal expected, @output_buffer + assert_dom_equal expected, output_buffer end # Perhaps this test should be moved to prototype helper tests. def test_remote_form_for_with_labelled_builder self.extend ActionView::Helpers::PrototypeHelper - @output_buffer = '' remote_form_for(:post, @post, :builder => LabelledFormBuilder) do |f| - @output_buffer.concat f.text_field(:title) - @output_buffer.concat f.text_area(:body) - @output_buffer.concat f.check_box(:secret) + concat f.text_field(:title) + concat f.text_area(:body) + concat f.check_box(:secret) end expected = @@ -747,16 +713,14 @@ def test_remote_form_for_with_labelled_builder "
      " + "" - assert_dom_equal expected, @output_buffer + assert_dom_equal expected, output_buffer end def test_fields_for_with_labelled_builder - @output_buffer = '' - fields_for(:post, @post, :builder => LabelledFormBuilder) do |f| - @output_buffer.concat f.text_field(:title) - @output_buffer.concat f.text_area(:body) - @output_buffer.concat f.check_box(:secret) + concat f.text_field(:title) + concat f.text_area(:body) + concat f.check_box(:secret) end expected = @@ -765,29 +729,23 @@ def test_fields_for_with_labelled_builder " " + "
      " - assert_dom_equal expected, @output_buffer + assert_dom_equal expected, output_buffer end def test_form_for_with_html_options_adds_options_to_form_tag - @output_buffer = '' - form_for(:post, @post, :html => {:id => 'some_form', :class => 'some_class'}) do |f| end expected = "
      " - assert_dom_equal expected, @output_buffer + assert_dom_equal expected, output_buffer end def test_form_for_with_string_url_option - @output_buffer = '' - form_for(:post, @post, :url => 'http://www.otherdomain.com') do |f| end - assert_equal '
      ', @output_buffer + assert_equal '
      ', output_buffer end def test_form_for_with_hash_url_option - @output_buffer = '' - form_for(:post, @post, :url => {:controller => 'controller', :action => 'action'}) do |f| end assert_equal 'controller', @controller.url_for_options[:controller] @@ -795,26 +753,20 @@ def test_form_for_with_hash_url_option end def test_form_for_with_record_url_option - @output_buffer = '' - form_for(:post, @post, :url => @post) do |f| end expected = "
      " - assert_equal expected, @output_buffer + assert_equal expected, output_buffer end def test_form_for_with_existing_object - @output_buffer = '' - form_for(@post) do |f| end expected = "
      " - assert_equal expected, @output_buffer + assert_equal expected, output_buffer end def test_form_for_with_new_object - @output_buffer = '' - post = Post.new post.new_record = true def post.id() nil end @@ -822,64 +774,61 @@ def post.id() nil end form_for(post) do |f| end expected = "
      " - assert_equal expected, @output_buffer + assert_equal expected, output_buffer end def test_form_for_with_existing_object_in_list @post.new_record = false @comment.save - @output_buffer = '' + form_for([@post, @comment]) {} expected = %(
      ) - assert_dom_equal expected, @output_buffer + assert_dom_equal expected, output_buffer end def test_form_for_with_new_object_in_list @post.new_record = false - @output_buffer = '' + form_for([@post, @comment]) {} expected = %(
      ) - assert_dom_equal expected, @output_buffer + assert_dom_equal expected, output_buffer end def test_form_for_with_existing_object_and_namespace_in_list @post.new_record = false @comment.save - @output_buffer = '' + form_for([:admin, @post, @comment]) {} expected = %(
      ) - assert_dom_equal expected, @output_buffer + assert_dom_equal expected, output_buffer end def test_form_for_with_new_object_and_namespace_in_list @post.new_record = false - @output_buffer = '' + form_for([:admin, @post, @comment]) {} expected = %(
      ) - assert_dom_equal expected, @output_buffer + assert_dom_equal expected, output_buffer end def test_form_for_with_existing_object_and_custom_url - @output_buffer = '' - form_for(@post, :url => "/super_posts") do |f| end expected = "
      " - assert_equal expected, @output_buffer + assert_equal expected, output_buffer end def test_remote_form_for_with_html_options_adds_options_to_form_tag self.extend ActionView::Helpers::PrototypeHelper - @output_buffer = '' remote_form_for(:post, @post, :html => {:id => 'some_form', :class => 'some_class'}) do |f| end expected = "
      " - assert_dom_equal expected, @output_buffer + assert_dom_equal expected, output_buffer end diff --git a/actionpack/test/template/form_options_helper_test.rb b/actionpack/test/template/form_options_helper_test.rb index 3c9fb297c3a2d..4fb2be9d3f182 100644 --- a/actionpack/test/template/form_options_helper_test.rb +++ b/actionpack/test/template/form_options_helper_test.rb @@ -230,16 +230,14 @@ def test_select def test_select_under_fields_for @post = Post.new @post.category = "" - - @output_buffer = '' - + fields_for :post, @post do |f| - @output_buffer.concat f.select(:category, %w( abe hest)) + concat f.select(:category, %w( abe hest)) end assert_dom_equal( "", - @output_buffer + output_buffer ) end @@ -352,16 +350,14 @@ def test_collection_select_under_fields_for @post = Post.new @post.author_name = "Babe" - - @output_buffer = '' - + fields_for :post, @post do |f| - @output_buffer.concat f.collection_select(:author_name, @posts, :author_name, :author_name) + concat f.collection_select(:author_name, @posts, :author_name, :author_name) end assert_dom_equal( "", - @output_buffer + output_buffer ) end @@ -1194,11 +1190,9 @@ def test_time_zone_select def test_time_zone_select_under_fields_for @firm = Firm.new("D") - - @output_buffer = '' - + fields_for :firm, @firm do |f| - @output_buffer.concat f.time_zone_select(:time_zone) + concat f.time_zone_select(:time_zone) end assert_dom_equal( @@ -1209,7 +1203,7 @@ def test_time_zone_select_under_fields_for "\n" + "" + "", - @output_buffer + output_buffer ) end diff --git a/actionpack/test/template/form_tag_helper_test.rb b/actionpack/test/template/form_tag_helper_test.rb index b281db18bd48f..47b3605849e2c 100644 --- a/actionpack/test/template/form_tag_helper_test.rb +++ b/actionpack/test/template/form_tag_helper_test.rb @@ -43,19 +43,17 @@ def test_form_tag_with_method_delete end def test_form_tag_with_block - @output_buffer = '' - form_tag("http://example.com") { @output_buffer.concat "Hello world!" } + form_tag("http://example.com") { concat "Hello world!" } expected = %(
      Hello world!
      ) - assert_dom_equal expected, @output_buffer + assert_dom_equal expected, output_buffer end def test_form_tag_with_block_and_method - @output_buffer = '' - form_tag("http://example.com", :method => :put) { @output_buffer.concat "Hello world!" } + form_tag("http://example.com", :method => :put) { concat "Hello world!" } expected = %(
      Hello world!
      ) - assert_dom_equal expected, @output_buffer + assert_dom_equal expected, output_buffer end def test_hidden_field_tag @@ -234,23 +232,22 @@ def test_pass end def test_field_set_tag - @output_buffer = '' - field_set_tag("Your details") { @output_buffer.concat "Hello world!" } + field_set_tag("Your details") { concat "Hello world!" } expected = %(
      Your detailsHello world!
      ) - assert_dom_equal expected, @output_buffer + assert_dom_equal expected, output_buffer - @output_buffer = '' - field_set_tag { @output_buffer.concat "Hello world!" } + self.output_buffer = '' + field_set_tag { concat "Hello world!" } expected = %(
      Hello world!
      ) - assert_dom_equal expected, @output_buffer - - @output_buffer = '' - field_set_tag('') { @output_buffer.concat "Hello world!" } + assert_dom_equal expected, output_buffer + + self.output_buffer = '' + field_set_tag('') { concat "Hello world!" } expected = %(
      Hello world!
      ) - assert_dom_equal expected, @output_buffer + assert_dom_equal expected, output_buffer end def protect_against_forgery? diff --git a/actionpack/test/template/javascript_helper_test.rb b/actionpack/test/template/javascript_helper_test.rb index 4924074c3e4e1..8c649ea544251 100644 --- a/actionpack/test/template/javascript_helper_test.rb +++ b/actionpack/test/template/javascript_helper_test.rb @@ -82,12 +82,12 @@ def test_button_to_function_without_function end def test_javascript_tag - @output_buffer = 'foo' + self.output_buffer = 'foo' assert_dom_equal "", javascript_tag("alert('hello')") - assert_equal 'foo', @output_buffer, 'javascript_tag without a block should not concat to @output_buffer' + assert_equal 'foo', output_buffer, 'javascript_tag without a block should not concat to output_buffer' end def test_javascript_tag_with_options @@ -96,15 +96,13 @@ def test_javascript_tag_with_options end def test_javascript_tag_with_block - @output_buffer = '' - javascript_tag { @output_buffer.concat "alert('hello')" } - assert_dom_equal "", @output_buffer + javascript_tag { concat "alert('hello')" } + assert_dom_equal "", output_buffer end def test_javascript_tag_with_block_and_options - @output_buffer = '' - javascript_tag(:id => "the_js_tag") { @output_buffer.concat "alert('hello')" } - assert_dom_equal "", @output_buffer + javascript_tag(:id => "the_js_tag") { concat "alert('hello')" } + assert_dom_equal "", output_buffer end def test_javascript_cdata_section diff --git a/actionpack/test/template/prototype_helper_test.rb b/actionpack/test/template/prototype_helper_test.rb index 3f5ec0721404c..a5be0d2789721 100644 --- a/actionpack/test/template/prototype_helper_test.rb +++ b/actionpack/test/template/prototype_helper_test.rb @@ -118,52 +118,46 @@ def test_form_remote_tag_with_method end def test_form_remote_tag_with_block - @output_buffer = '' - form_remote_tag(:update => "glass_of_beer", :url => { :action => :fast }) { @output_buffer.concat "Hello world!" } - assert_dom_equal %(
      Hello world!
      ), @output_buffer + form_remote_tag(:update => "glass_of_beer", :url => { :action => :fast }) { concat "Hello world!" } + assert_dom_equal %(
      Hello world!
      ), output_buffer end def test_remote_form_for_with_record_identification_with_new_record - @output_buffer = '' remote_form_for(@record, {:html => { :id => 'create-author' }}) {} expected = %(
      ) - assert_dom_equal expected, @output_buffer + assert_dom_equal expected, output_buffer end def test_remote_form_for_with_record_identification_without_html_options - @output_buffer = '' remote_form_for(@record) {} expected = %(
      ) - assert_dom_equal expected, @output_buffer + assert_dom_equal expected, output_buffer end def test_remote_form_for_with_record_identification_with_existing_record @record.save - @output_buffer = '' remote_form_for(@record) {} expected = %(
      ) - assert_dom_equal expected, @output_buffer + assert_dom_equal expected, output_buffer end def test_remote_form_for_with_new_object_in_list - @output_buffer = '' remote_form_for([@author, @article]) {} expected = %(
      ) - assert_dom_equal expected, @output_buffer + assert_dom_equal expected, output_buffer end def test_remote_form_for_with_existing_object_in_list @author.save @article.save - @output_buffer = '' remote_form_for([@author, @article]) {} expected = %(
      ) - assert_dom_equal expected, @output_buffer + assert_dom_equal expected, output_buffer end def test_on_callbacks diff --git a/actionpack/test/template/record_tag_helper_test.rb b/actionpack/test/template/record_tag_helper_test.rb index c39e5c69bf8f9..441dc6b7205b8 100644 --- a/actionpack/test/template/record_tag_helper_test.rb +++ b/actionpack/test/template/record_tag_helper_test.rb @@ -17,37 +17,32 @@ def setup end def test_content_tag_for - @output_buffer = '' expected = %(
    • ) actual = content_tag_for(:li, @post, :class => 'bar') { } assert_dom_equal expected, actual end def test_content_tag_for_prefix - @output_buffer = '' expected = %(
        ) actual = content_tag_for(:ul, @post, :archived) { } assert_dom_equal expected, actual end def test_content_tag_for_with_extra_html_tags - @output_buffer = '' expected = %() actual = content_tag_for(:tr, @post, {:class => "bar", :style => "background-color: #f0f0f0"}) { } assert_dom_equal expected, actual end def test_block_works_with_content_tag_for - @output_buffer = '' expected = %(#{@post.body}) - actual = content_tag_for(:tr, @post) { @output_buffer.concat @post.body } + actual = content_tag_for(:tr, @post) { concat @post.body } assert_dom_equal expected, actual end def test_div_for - @output_buffer = '' expected = %(
        #{@post.body}
        ) - actual = div_for(@post, :class => "bar") { @output_buffer.concat @post.body } + actual = div_for(@post, :class => "bar") { concat @post.body } assert_dom_equal expected, actual end diff --git a/actionpack/test/template/tag_helper_test.rb b/actionpack/test/template/tag_helper_test.rb index 7db04d178f34c..bfdaf0d639fb9 100644 --- a/actionpack/test/template/tag_helper_test.rb +++ b/actionpack/test/template/tag_helper_test.rb @@ -35,15 +35,13 @@ def test_content_tag end def test_content_tag_with_block - @output_buffer = '' - content_tag(:div) { @output_buffer.concat "Hello world!" } - assert_dom_equal "
        Hello world!
        ", @output_buffer + content_tag(:div) { concat "Hello world!" } + assert_dom_equal "
        Hello world!
        ", output_buffer end def test_content_tag_with_block_and_options - @output_buffer = '' - content_tag(:div, :class => "green") { @output_buffer.concat "Hello world!" } - assert_dom_equal %(
        Hello world!
        ), @output_buffer + content_tag(:div, :class => "green") { concat "Hello world!" } + assert_dom_equal %(
        Hello world!
        ), output_buffer end def test_content_tag_with_block_and_options_outside_of_action_view diff --git a/actionpack/test/template/text_helper_test.rb b/actionpack/test/template/text_helper_test.rb index 0f5c62acad2e2..cbb5c7ee7431b 100644 --- a/actionpack/test/template/text_helper_test.rb +++ b/actionpack/test/template/text_helper_test.rb @@ -12,11 +12,11 @@ def setup end def test_concat - @output_buffer = 'foo' + self.output_buffer = 'foo' concat 'bar' - assert_equal 'foobar', @output_buffer + assert_equal 'foobar', output_buffer assert_nothing_raised { concat nil } - assert_equal 'foobar', @output_buffer + assert_equal 'foobar', output_buffer end def test_simple_format From 0c9281e82140f3a69e4473b3bcefd5ccebd79e2d Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Sun, 8 Jun 2008 22:11:08 -0500 Subject: [PATCH 073/200] Drop ActionController::Base.allow_concurrency flag --- actionpack/CHANGELOG | 2 ++ actionpack/lib/action_controller/base.rb | 7 ------- railties/lib/webrick_server.rb | 13 ++----------- 3 files changed, 4 insertions(+), 18 deletions(-) diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG index 8265b5b04f7ad..3096716dba547 100644 --- a/actionpack/CHANGELOG +++ b/actionpack/CHANGELOG @@ -1,5 +1,7 @@ *Edge* +* Drop ActionController::Base.allow_concurrency flag [Josh Peek] + * More efficient concat and capture helpers. Remove ActionView::Base.erb_variable. [Jeremy Kemper] * Added page.reload functionality. Resolves #277. [Sean Huber] diff --git a/actionpack/lib/action_controller/base.rb b/actionpack/lib/action_controller/base.rb index a036600c2bbcc..44269fc73584e 100755 --- a/actionpack/lib/action_controller/base.rb +++ b/actionpack/lib/action_controller/base.rb @@ -283,13 +283,6 @@ class Base @@debug_routes = true cattr_accessor :debug_routes - # Indicates to Mongrel or Webrick whether to allow concurrent action - # processing. Your controller actions and any other code they call must - # also behave well when called from concurrent threads. Turned off by - # default. - @@allow_concurrency = false - cattr_accessor :allow_concurrency - # Modern REST web services often need to submit complex data to the web application. # The @@param_parsers hash lets you register handlers which will process the HTTP body and add parameters to the # params hash. These handlers are invoked for POST and PUT requests. diff --git a/railties/lib/webrick_server.rb b/railties/lib/webrick_server.rb index ad4ca926ba4b4..2f60151b22275 100644 --- a/railties/lib/webrick_server.rb +++ b/railties/lib/webrick_server.rb @@ -43,8 +43,6 @@ def initialize(type = "query", table = nil, stdin = nil) # can change this behavior by setting ActionController::Base.allow_concurrency # to true. class DispatchServlet < WEBrick::HTTPServlet::AbstractServlet - REQUEST_MUTEX = Mutex.new - # Start the WEBrick server with the given options, mounting the # DispatchServlet at /. def self.dispatch(options = {}) @@ -73,15 +71,8 @@ def initialize(server, options) #:nodoc: def service(req, res) #:nodoc: unless handle_file(req, res) - begin - REQUEST_MUTEX.lock unless ActionController::Base.allow_concurrency - unless handle_dispatch(req, res) - raise WEBrick::HTTPStatus::NotFound, "`#{req.path}' not found." - end - ensure - unless ActionController::Base.allow_concurrency - REQUEST_MUTEX.unlock if REQUEST_MUTEX.locked? - end + unless handle_dispatch(req, res) + raise WEBrick::HTTPStatus::NotFound, "`#{req.path}' not found." end end end From df44df945d6315238e7d94d9bdef82e435dc9b24 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Sun, 8 Jun 2008 22:31:54 -0500 Subject: [PATCH 074/200] Ensure ActionView::TemplateFinder view cache is rebuilt on initialize. --- actionpack/CHANGELOG | 2 ++ actionpack/lib/action_controller/dispatcher.rb | 5 ++++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG index 3096716dba547..f9cb7154002ad 100644 --- a/actionpack/CHANGELOG +++ b/actionpack/CHANGELOG @@ -1,5 +1,7 @@ *Edge* +* Ensure ActionView::TemplateFinder view cache is rebuilt on initialize [Josh Peek] + * Drop ActionController::Base.allow_concurrency flag [Josh Peek] * More efficient concat and capture helpers. Remove ActionView::Base.erb_variable. [Jeremy Kemper] diff --git a/actionpack/lib/action_controller/dispatcher.rb b/actionpack/lib/action_controller/dispatcher.rb index fe4f6b4a7ea29..64553fb25df6d 100644 --- a/actionpack/lib/action_controller/dispatcher.rb +++ b/actionpack/lib/action_controller/dispatcher.rb @@ -10,6 +10,10 @@ def define_dispatcher_callbacks(cache_classes) # Development mode callbacks before_dispatch :reload_application after_dispatch :cleanup_application + + to_prepare :reload_view_path_cache do + ActionView::TemplateFinder.reload! unless ActionView::Base.cache_template_loading + end end # Common callbacks @@ -134,7 +138,6 @@ def reload_application run_callbacks :prepare_dispatch Routing::Routes.reload - ActionView::TemplateFinder.reload! unless ActionView::Base.cache_template_loading end # Cleanup the application by clearing out loaded classes so they can From c88f2b5e23b0cb6c1a3b3687958f45d518414041 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Sun, 8 Jun 2008 20:35:14 -0700 Subject: [PATCH 075/200] with_output_buffer returns the temporary buffer instead of the result of the block --- actionpack/lib/action_view/helpers/capture_helper.rb | 1 + actionpack/test/template/tag_helper_test.rb | 1 + 2 files changed, 2 insertions(+) diff --git a/actionpack/lib/action_view/helpers/capture_helper.rb b/actionpack/lib/action_view/helpers/capture_helper.rb index ab453fadf2f40..9cd9d3d06a4e2 100644 --- a/actionpack/lib/action_view/helpers/capture_helper.rb +++ b/actionpack/lib/action_view/helpers/capture_helper.rb @@ -123,6 +123,7 @@ def content_for(name, content = nil, &block) def with_output_buffer(buf = '') self.output_buffer, old_buffer = buf, output_buffer yield + output_buffer ensure self.output_buffer = old_buffer end diff --git a/actionpack/test/template/tag_helper_test.rb b/actionpack/test/template/tag_helper_test.rb index bfdaf0d639fb9..2941dfe217770 100644 --- a/actionpack/test/template/tag_helper_test.rb +++ b/actionpack/test/template/tag_helper_test.rb @@ -45,6 +45,7 @@ def test_content_tag_with_block_and_options end def test_content_tag_with_block_and_options_outside_of_action_view + self.output_buffer = nil assert_equal content_tag("a", "Create", :href => "create"), content_tag("a", "href" => "create") { "Create" } end From 057768cd2c8541f9c466131cb6c77f13ce12204d Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Sun, 8 Jun 2008 21:21:54 -0700 Subject: [PATCH 076/200] Process view paths passed to AV::Base#initialize instead of raising. --- actionpack/lib/action_view/template_finder.rb | 11 +---------- actionpack/test/template/template_finder_test.rb | 6 ------ 2 files changed, 1 insertion(+), 16 deletions(-) diff --git a/actionpack/lib/action_view/template_finder.rb b/actionpack/lib/action_view/template_finder.rb index 83b7e27c093c3..fc2a07b30c35a 100644 --- a/actionpack/lib/action_view/template_finder.rb +++ b/actionpack/lib/action_view/template_finder.rb @@ -1,14 +1,5 @@ module ActionView #:nodoc: class TemplateFinder #:nodoc: - - class InvalidViewPath < StandardError #:nodoc: - attr_reader :unprocessed_path - def initialize(path) - @unprocessed_path = path - super("Unprocessed view path found: #{@unprocessed_path.inspect}. Set your view paths with #append_view_path, #prepend_view_path, or #view_paths=.") - end - end - cattr_reader :processed_view_paths @@processed_view_paths = Hash.new {|hash, key| hash[key] = []} @@ -76,7 +67,7 @@ def initialize(*args) @view_paths = args.flatten @view_paths = @view_paths.respond_to?(:find) ? @view_paths.dup : [*@view_paths].compact - check_view_paths(@view_paths) + self.class.process_view_paths(@view_paths) end def prepend_view_path(path) diff --git a/actionpack/test/template/template_finder_test.rb b/actionpack/test/template/template_finder_test.rb index 3d6baff5fb6a9..d6aac87b13689 100644 --- a/actionpack/test/template/template_finder_test.rb +++ b/actionpack/test/template/template_finder_test.rb @@ -11,12 +11,6 @@ def setup @finder = ActionView::TemplateFinder.new(@template, LOAD_PATH_ROOT) end - def test_should_raise_exception_for_unprocessed_view_path - assert_raises ActionView::TemplateFinder::InvalidViewPath do - ActionView::TemplateFinder.new(@template, File.dirname(__FILE__)) - end - end - def test_should_cache_file_extension_properly assert_equal ["builder", "erb", "rhtml", "rjs", "rxml", "mab"].sort, ActionView::TemplateFinder.file_extension_cache[LOAD_PATH_ROOT].values.flatten.uniq.sort From def594b92d1e68db68a27d53dec7ae4e6246f5a5 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Sun, 8 Jun 2008 22:08:59 -0700 Subject: [PATCH 077/200] Don't append limit to primary key column definition. Freeze some constants. --- .../abstract/schema_statements.rb | 4 +-- .../connection_adapters/mysql_adapter.rb | 34 +++++++++++-------- .../connection_adapters/postgresql_adapter.rb | 34 +++++++++++-------- activerecord/lib/active_record/fixtures.rb | 2 +- 4 files changed, 41 insertions(+), 33 deletions(-) diff --git a/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb b/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb index 55f67995d1382..7d8530ebef92a 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb @@ -356,7 +356,7 @@ def assume_migrated_upto_version(version) def type_to_sql(type, limit = nil, precision = nil, scale = nil) #:nodoc: if native = native_database_types[type] - column_type_sql = native.is_a?(Hash) ? native[:name] : native + column_type_sql = (native.is_a?(Hash) ? native[:name] : native).dup if type == :decimal # ignore limit, use precision and scale scale ||= native[:scale] @@ -371,7 +371,7 @@ def type_to_sql(type, limit = nil, precision = nil, scale = nil) #:nodoc: raise ArgumentError, "Error adding decimal column: precision cannot be empty if scale if specified" end - elsif limit ||= native.is_a?(Hash) && native[:limit] + elsif (type != :primary_key) && (limit ||= native.is_a?(Hash) && native[:limit]) column_type_sql << "(#{limit})" end diff --git a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb index b052c0328e4c6..cfd2402d9d38a 100755 --- a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb @@ -147,6 +147,8 @@ class MysqlAdapter < AbstractAdapter @@emulate_booleans = true cattr_accessor :emulate_booleans + ADAPTER_NAME = 'MySQL'.freeze + LOST_CONNECTION_ERROR_MESSAGES = [ "Server shutdown in progress", "Broken pipe", @@ -155,6 +157,21 @@ class MysqlAdapter < AbstractAdapter QUOTED_TRUE, QUOTED_FALSE = '1', '0' + NATIVE_DATABASE_TYPES = { + :primary_key => "int(11) DEFAULT NULL auto_increment PRIMARY KEY".freeze, + :string => { :name => "varchar", :limit => 255 }, + :text => { :name => "text" }, + :integer => { :name => "int"}, + :float => { :name => "float" }, + :decimal => { :name => "decimal" }, + :datetime => { :name => "datetime" }, + :timestamp => { :name => "datetime" }, + :time => { :name => "time" }, + :date => { :name => "date" }, + :binary => { :name => "blob" }, + :boolean => { :name => "tinyint", :limit => 1 } + } + def initialize(connection, logger, connection_options, config) super(connection, logger) @connection_options, @config = connection_options, config @@ -163,7 +180,7 @@ def initialize(connection, logger, connection_options, config) end def adapter_name #:nodoc: - 'MySQL' + ADAPTER_NAME end def supports_migrations? #:nodoc: @@ -171,20 +188,7 @@ def supports_migrations? #:nodoc: end def native_database_types #:nodoc: - { - :primary_key => "int(11) DEFAULT NULL auto_increment PRIMARY KEY", - :string => { :name => "varchar", :limit => 255 }, - :text => { :name => "text" }, - :integer => { :name => "int"}, - :float => { :name => "float" }, - :decimal => { :name => "decimal" }, - :datetime => { :name => "datetime" }, - :timestamp => { :name => "datetime" }, - :time => { :name => "time" }, - :date => { :name => "date" }, - :binary => { :name => "blob" }, - :boolean => { :name => "tinyint", :limit => 1 } - } + NATIVE_DATABASE_TYPES end diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb index 049e6f61def2e..ae070421f180c 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb @@ -238,9 +238,26 @@ module ConnectionAdapters # * :min_messages - An optional client min messages that is used in a SET client_min_messages TO call on the connection. # * :allow_concurrency - If true, use async query methods so Ruby threads don't deadlock; otherwise, use blocking query methods. class PostgreSQLAdapter < AbstractAdapter + ADAPTER_NAME = 'PostgreSQL'.freeze + + NATIVE_DATABASE_TYPES = { + :primary_key => "serial primary key".freeze, + :string => { :name => "character varying", :limit => 255 }, + :text => { :name => "text" }, + :integer => { :name => "integer" }, + :float => { :name => "float" }, + :decimal => { :name => "decimal" }, + :datetime => { :name => "timestamp" }, + :timestamp => { :name => "timestamp" }, + :time => { :name => "time" }, + :date => { :name => "date" }, + :binary => { :name => "bytea" }, + :boolean => { :name => "boolean" } + } + # Returns 'PostgreSQL' as adapter name for identification purposes. def adapter_name - 'PostgreSQL' + ADAPTER_NAME end # Initializes and connects a PostgreSQL adapter. @@ -282,20 +299,7 @@ def disconnect! end def native_database_types #:nodoc: - { - :primary_key => "serial primary key", - :string => { :name => "character varying", :limit => 255 }, - :text => { :name => "text" }, - :integer => { :name => "integer" }, - :float => { :name => "float" }, - :decimal => { :name => "decimal" }, - :datetime => { :name => "timestamp" }, - :timestamp => { :name => "timestamp" }, - :time => { :name => "time" }, - :date => { :name => "date" }, - :binary => { :name => "bytea" }, - :boolean => { :name => "boolean" } - } + NATIVE_DATABASE_TYPES end # Does PostgreSQL support migrations? diff --git a/activerecord/lib/active_record/fixtures.rb b/activerecord/lib/active_record/fixtures.rb index c4cbe5d52f747..e19614e31f176 100755 --- a/activerecord/lib/active_record/fixtures.rb +++ b/activerecord/lib/active_record/fixtures.rb @@ -547,7 +547,7 @@ def initialize(connection, table_name, class_name, fixture_path, file_filter = D @connection, @table_name, @fixture_path, @file_filter = connection, table_name, fixture_path, file_filter @class_name = class_name || (ActiveRecord::Base.pluralize_table_names ? @table_name.singularize.camelize : @table_name.camelize) - @table_name = ActiveRecord::Base.table_name_prefix + @table_name + ActiveRecord::Base.table_name_suffix + @table_name = "#{ActiveRecord::Base.table_name_prefix}#{@table_name}#{ActiveRecord::Base.table_name_suffix}" @table_name = class_name.table_name if class_name.respond_to?(:table_name) @connection = class_name.connection if class_name.respond_to?(:connection) read_fixture_files From d20035910c186401c1d957462f4ad7894347c398 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Sun, 8 Jun 2008 22:34:09 -0700 Subject: [PATCH 078/200] Give a more informative error message instead of just raising a load error when mysql gem isn't installed --- .../active_record/connection_adapters/mysql_adapter.rb | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb index cfd2402d9d38a..076b7f63c2a04 100755 --- a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb @@ -58,7 +58,14 @@ def self.mysql_connection(config) # :nodoc: end # Require the MySQL driver and define Mysql::Result.all_hashes - require_library_or_gem('mysql') unless defined? Mysql + unless defined? Mysql + begin + require_library_or_gem('mysql') + rescue LoadError + $stderr.puts '!!! The bundled mysql.rb driver has been removed from Rails 2.2. Please install the mysql gem and try again: gem install mysql.' + raise + end + end MysqlCompat.define_all_hashes_method! mysql = Mysql.init From 9051da90e4da2ab0db16530a7f7568e24a0ccaed Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Sun, 8 Jun 2008 19:40:50 -0700 Subject: [PATCH 079/200] Enable autoreconnect if available. Freeze constants. --- .../active_record/connection_adapters/mysql_adapter.rb | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb index 076b7f63c2a04..93aafaaad1da6 100755 --- a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb @@ -151,8 +151,8 @@ def missing_default_forged_as_empty_string?(default) # # ActiveRecord::ConnectionAdapters::MysqlAdapter.emulate_booleans = false class MysqlAdapter < AbstractAdapter - @@emulate_booleans = true cattr_accessor :emulate_booleans + self.emulate_booleans = true ADAPTER_NAME = 'MySQL'.freeze @@ -162,7 +162,7 @@ class MysqlAdapter < AbstractAdapter "Lost connection to MySQL server during query", "MySQL server has gone away" ] - QUOTED_TRUE, QUOTED_FALSE = '1', '0' + QUOTED_TRUE, QUOTED_FALSE = '1'.freeze, '0'.freeze NATIVE_DATABASE_TYPES = { :primary_key => "int(11) DEFAULT NULL auto_increment PRIMARY KEY".freeze, @@ -488,12 +488,17 @@ def pk_and_sequence_for(table) #:nodoc: private def connect + @connection.reconnect = true if @connection.respond_to?(:reconnect=) + encoding = @config[:encoding] if encoding @connection.options(Mysql::SET_CHARSET_NAME, encoding) rescue nil end + @connection.ssl_set(@config[:sslkey], @config[:sslcert], @config[:sslca], @config[:sslcapath], @config[:sslcipher]) if @config[:sslkey] + @connection.real_connect(*@connection_options) + execute("SET NAMES '#{encoding}'") if encoding # By default, MySQL 'where id is null' selects the last inserted id. From 55791c6c0012d0daea2a75b6a5927f459be25c54 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Mon, 9 Jun 2008 10:05:02 -0500 Subject: [PATCH 080/200] Removed used check_view_paths after 057768c --- actionpack/lib/action_view/template_finder.rb | 7 ------- 1 file changed, 7 deletions(-) diff --git a/actionpack/lib/action_view/template_finder.rb b/actionpack/lib/action_view/template_finder.rb index fc2a07b30c35a..cd5e290644273 100644 --- a/actionpack/lib/action_view/template_finder.rb +++ b/actionpack/lib/action_view/template_finder.rb @@ -157,12 +157,5 @@ def path_and_extension(template_path) def find_template_extension_from_first_render File.basename(@template.first_render.to_s)[/^[^.]+\.(.+)$/, 1] end - - private - def check_view_paths(view_paths) - view_paths.each do |path| - raise InvalidViewPath.new(path) unless @@processed_view_paths.has_key?(path) - end - end end end From 233643047104131565467787d0bbc0841bbc77cb Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Mon, 9 Jun 2008 10:21:30 -0500 Subject: [PATCH 081/200] Removed TemplateFinder.update_extension_cache_for since view path cache will be updated on boot. --- actionpack/lib/action_view/template.rb | 1 - actionpack/lib/action_view/template_finder.rb | 16 +--------------- actionpack/test/template/template_finder_test.rb | 7 ------- 3 files changed, 1 insertion(+), 23 deletions(-) diff --git a/actionpack/lib/action_view/template.rb b/actionpack/lib/action_view/template.rb index 369526188f5ff..a878ac66d976d 100644 --- a/actionpack/lib/action_view/template.rb +++ b/actionpack/lib/action_view/template.rb @@ -99,7 +99,6 @@ def raise_missing_template_exception # return the rendered template as a string. def self.register_template_handler(extension, klass) @@template_handlers[extension.to_sym] = klass - TemplateFinder.update_extension_cache_for(extension.to_s) end def self.template_handler_extensions diff --git a/actionpack/lib/action_view/template_finder.rb b/actionpack/lib/action_view/template_finder.rb index cd5e290644273..7e9a310810dbb 100644 --- a/actionpack/lib/action_view/template_finder.rb +++ b/actionpack/lib/action_view/template_finder.rb @@ -9,7 +9,6 @@ class TemplateFinder #:nodoc: } class << self #:nodoc: - # This method is not thread safe. Mutex should be used whenever this is accessed from an instance method def process_view_paths(*view_paths) view_paths.flatten.compact.each do |dir| @@ -26,7 +25,7 @@ def process_view_paths(*view_paths) # Build extension cache extension = file.split(".").last - if template_handler_extensions.include?(extension) + if ActionView::Template.template_handler_extensions.include?(extension) key = file.split(dir).last.sub(/^\//, '').sub(/\.(\w+)$/, '') @@file_extension_cache[dir][key] << extension end @@ -35,19 +34,6 @@ def process_view_paths(*view_paths) end end - def update_extension_cache_for(extension) - @@processed_view_paths.keys.each do |dir| - Dir.glob("#{dir}/**/*.#{extension}").each do |file| - key = file.split(dir).last.sub(/^\//, '').sub(/\.(\w+)$/, '') - @@file_extension_cache[dir][key] << extension - end - end - end - - def template_handler_extensions - ActionView::Template.template_handler_extensions - end - def reload! view_paths = @@processed_view_paths.keys diff --git a/actionpack/test/template/template_finder_test.rb b/actionpack/test/template/template_finder_test.rb index d6aac87b13689..07fc4b8c56535 100644 --- a/actionpack/test/template/template_finder_test.rb +++ b/actionpack/test/template/template_finder_test.rb @@ -57,11 +57,4 @@ def test_should_report_file_exists_correctly assert_equal false, @finder.send(:file_exists?, 'baz') assert_equal false, @finder.send(:file_exists?, 'baz.rb') end - - uses_mocha 'Template finder tests' do - def test_should_update_extension_cache_when_template_handler_is_registered - ActionView::TemplateFinder.expects(:update_extension_cache_for).with("funky") - ActionView::Template::register_template_handler :funky, Class.new(ActionView::TemplateHandler) - end - end end From e94e53f9cd70bee69759661e9771da3fe0ee9554 Mon Sep 17 00:00:00 2001 From: Brandon Keepers Date: Mon, 9 Jun 2008 11:30:48 -0400 Subject: [PATCH 082/200] fix eager loading with dynamic finders --- activerecord/lib/active_record/associations.rb | 2 +- .../associations/has_and_belongs_to_many_association.rb | 1 + .../lib/active_record/associations/has_many_association.rb | 2 +- .../has_and_belongs_to_many_associations_test.rb | 6 ++++++ .../associations/has_many_through_associations_test.rb | 6 ++++++ activerecord/test/models/author.rb | 1 + activerecord/test/models/category.rb | 1 + activerecord/test/models/person.rb | 2 +- 8 files changed, 18 insertions(+), 3 deletions(-) diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb index a3d1bbbada68b..f32b217326a2a 100755 --- a/activerecord/lib/active_record/associations.rb +++ b/activerecord/lib/active_record/associations.rb @@ -1506,7 +1506,7 @@ def conditions_tables(options) end def order_tables(options) - order = options[:order] + order = [options[:order], scope(:find, :order) ].join(", ") return [] unless order && order.is_a?(String) order.scan(/([\.\w]+).?\./).flatten end diff --git a/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb b/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb index 4fa8e9d0a84ee..918404eac6e0d 100644 --- a/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb +++ b/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb @@ -87,6 +87,7 @@ def construct_scope :joins => @join_sql, :readonly => false, :order => @reflection.options[:order], + :include => @reflection.options[:include], :limit => @reflection.options[:limit] } } end diff --git a/activerecord/lib/active_record/associations/has_many_association.rb b/activerecord/lib/active_record/associations/has_many_association.rb index f584a97cbba57..295beb2966694 100644 --- a/activerecord/lib/active_record/associations/has_many_association.rb +++ b/activerecord/lib/active_record/associations/has_many_association.rb @@ -100,7 +100,7 @@ def construct_scope create_scoping = {} set_belongs_to_association_for(create_scoping) { - :find => { :conditions => @finder_sql, :readonly => false, :order => @reflection.options[:order], :limit => @reflection.options[:limit] }, + :find => { :conditions => @finder_sql, :readonly => false, :order => @reflection.options[:order], :limit => @reflection.options[:limit], :include => @reflection.options[:include]}, :create => create_scoping } end diff --git a/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb b/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb index 294b993c55833..b29df68d22e50 100644 --- a/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb @@ -681,4 +681,10 @@ def test_symbols_as_keys assert_equal developer, project.developers.find(:first) assert_equal project, developer.projects.find(:first) end + + def test_dynamic_find_should_respect_association_include + # SQL error in sort clause if :include is not included + # due to Unknown column 'authors.id' + assert Category.find(1).posts_with_authors_sorted_by_author_id.find_by_title('Welcome to the weblog') + end end diff --git a/activerecord/test/cases/associations/has_many_through_associations_test.rb b/activerecord/test/cases/associations/has_many_through_associations_test.rb index 05155f63039bf..be5170f830394 100644 --- a/activerecord/test/cases/associations/has_many_through_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_through_associations_test.rb @@ -187,4 +187,10 @@ def test_association_callback_ordering post.people_with_callbacks.clear assert_equal (%w(Michael David Julian Roger) * 2).sort, log.last(8).collect(&:last).sort end + + def test_dynamic_find_should_respect_association_include + # SQL error in sort clause if :include is not included + # due to Unknown column 'comments.id' + assert Person.find(1).posts_with_comments_sorted_by_comment_id.find_by_title('Welcome to the weblog') + end end diff --git a/activerecord/test/models/author.rb b/activerecord/test/models/author.rb index f63af27403716..82e069005ab0b 100644 --- a/activerecord/test/models/author.rb +++ b/activerecord/test/models/author.rb @@ -1,6 +1,7 @@ class Author < ActiveRecord::Base has_many :posts has_many :posts_with_comments, :include => :comments, :class_name => "Post" + has_many :posts_with_comments_sorted_by_comment_id, :include => :comments, :class_name => "Post", :order => 'comments.id' has_many :posts_with_categories, :include => :categories, :class_name => "Post" has_many :posts_with_comments_and_categories, :include => [ :comments, :categories ], :order => "posts.id", :class_name => "Post" has_many :posts_containing_the_letter_a, :class_name => "Post" diff --git a/activerecord/test/models/category.rb b/activerecord/test/models/category.rb index f1d2e4805a6f6..1660c61682799 100644 --- a/activerecord/test/models/category.rb +++ b/activerecord/test/models/category.rb @@ -2,6 +2,7 @@ class Category < ActiveRecord::Base has_and_belongs_to_many :posts has_and_belongs_to_many :special_posts, :class_name => "Post" has_and_belongs_to_many :other_posts, :class_name => "Post" + has_and_belongs_to_many :posts_with_authors_sorted_by_author_id, :class_name => "Post", :include => :authors, :order => "authors.id" has_and_belongs_to_many(:select_testing_posts, :class_name => 'Post', diff --git a/activerecord/test/models/person.rb b/activerecord/test/models/person.rb index 4f4d695b24d02..430d0b38f7365 100644 --- a/activerecord/test/models/person.rb +++ b/activerecord/test/models/person.rb @@ -6,5 +6,5 @@ class Person < ActiveRecord::Base has_many :references has_many :jobs, :through => :references has_one :favourite_reference, :class_name => 'Reference', :conditions => ['favourite=?', true] - + has_many :posts_with_comments_sorted_by_comment_id, :through => :readers, :source => :post, :include => :comments, :order => 'comments.id' end From 64637da284ed4685591c178202ee103e6bee71cf Mon Sep 17 00:00:00 2001 From: rick Date: Mon, 9 Jun 2008 12:06:23 -0400 Subject: [PATCH 083/200] add deprecation for the #concat helper's 2nd argument, which is no longer needed --- actionpack/lib/action_view/helpers/text_helper.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/actionpack/lib/action_view/helpers/text_helper.rb b/actionpack/lib/action_view/helpers/text_helper.rb index f81f7eded6f8a..3077c8f970e4b 100644 --- a/actionpack/lib/action_view/helpers/text_helper.rb +++ b/actionpack/lib/action_view/helpers/text_helper.rb @@ -25,7 +25,10 @@ module TextHelper # end # # will either display "Logged in!" or a login link # %> - def concat(string) + def concat(string, unused_binding = nil) + if unused_binding + ActiveSupport::Deprecation.warn("The binding argument of #concat is no longer needed. Please remove it from your views and helpers.") + end if @output_buffer && string @output_buffer << string else From f545e19692c84eeaa8de38319766b5ceed1768c1 Mon Sep 17 00:00:00 2001 From: rick Date: Mon, 9 Jun 2008 12:06:23 -0400 Subject: [PATCH 084/200] add deprecation for the #concat helper's 2nd argument, which is no longer needed --- actionpack/lib/action_view/helpers/text_helper.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/actionpack/lib/action_view/helpers/text_helper.rb b/actionpack/lib/action_view/helpers/text_helper.rb index 85a36726781f3..a1a91f6b3d0ea 100644 --- a/actionpack/lib/action_view/helpers/text_helper.rb +++ b/actionpack/lib/action_view/helpers/text_helper.rb @@ -25,7 +25,11 @@ module TextHelper # end # # will either display "Logged in!" or a login link # %> - def concat(string) + def concat(string, unused_binding = nil) + if unused_binding + ActiveSupport::Deprecation.warn("The binding argument of #concat is no longer needed. Please remove it from your views and helpers.") + end + if output_buffer && string output_buffer << string else From 16a9787bf034a4de36a35b647c456ef142f814e1 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Mon, 9 Jun 2008 23:04:51 -0700 Subject: [PATCH 085/200] Add empty setup and teardown methods to rule out default setup behavior in base class --- activesupport/test/test_test.rb | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/activesupport/test/test_test.rb b/activesupport/test/test_test.rb index 1e75e18602f72..26a45af255c54 100644 --- a/activesupport/test/test_test.rb +++ b/activesupport/test/test_test.rb @@ -84,6 +84,12 @@ def test_inherited_setup_callbacks assert_equal [:foo, :sentinel, :foo], self.class.teardown_callback_chain.map(&:method) end + def setup + end + + def teardown + end + protected def reset_callback_record @called_back = [] From 225065709c43dacd57e0904aef2075024ccf2744 Mon Sep 17 00:00:00 2001 From: Luis Hurtado Date: Mon, 9 Jun 2008 21:39:39 -0500 Subject: [PATCH 086/200] Fixes parsing deep nested resources from XML. [#380 state:resolved] --- activeresource/lib/active_resource/base.rb | 6 ++- activeresource/test/base_test.rb | 48 ++++++++++++++++++++++ activeresource/test/fixtures/customer.rb | 3 ++ 3 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 activeresource/test/fixtures/customer.rb diff --git a/activeresource/lib/active_resource/base.rb b/activeresource/lib/active_resource/base.rb index 55dacfdf06929..347dbb82aa633 100644 --- a/activeresource/lib/active_resource/base.rb +++ b/activeresource/lib/active_resource/base.rb @@ -988,7 +988,11 @@ def find_or_create_resource_for(name) self.class.const_get(resource_name) end rescue NameError - resource = self.class.const_set(resource_name, Class.new(ActiveResource::Base)) + if self.class.const_defined?(resource_name) + resource = self.class.const_get(resource_name) + else + resource = self.class.const_set(resource_name, Class.new(ActiveResource::Base)) + end resource.prefix = self.class.prefix resource.site = self.class.site resource diff --git a/activeresource/test/base_test.rb b/activeresource/test/base_test.rb index 9e2f6c1831f5d..4addd52636533 100644 --- a/activeresource/test/base_test.rb +++ b/activeresource/test/base_test.rb @@ -1,5 +1,6 @@ require 'abstract_unit' require "fixtures/person" +require "fixtures/customer" require "fixtures/street_address" require "fixtures/beast" @@ -15,6 +16,37 @@ def setup @people_david = [{ :id => 2, :name => 'David' }].to_xml(:root => 'people') @addresses = [{ :id => 1, :street => '12345 Street' }].to_xml(:root => 'addresses') + # - deep nested resource - + # - Luis (Customer) + # - JK (Customer::Friend) + # - Mateo (Customer::Friend::Brother) + # - Edith (Customer::Friend::Brother::Child) + # - Martha (Customer::Friend::Brother::Child) + # - Felipe (Customer::Friend::Brother) + # - Bryan (Customer::Friend::Brother::Child) + # - Luke (Customer::Friend::Brother::Child) + # - Eduardo (Customer::Friend) + # - Sebas (Customer::Friend::Brother) + # - Andres (Customer::Friend::Brother::Child) + # - Jorge (Customer::Friend::Brother::Child) + # - Elsa (Customer::Friend::Brother) + # - Natacha (Customer::Friend::Brother::Child) + # - Milena (Customer::Friend::Brother) + # + @luis = {:id => 1, :name => 'Luis', + :friends => [{:name => 'JK', + :brothers => [{:name => 'Mateo', + :children => [{:name => 'Edith'},{:name => 'Martha'}]}, + {:name => 'Felipe', + :children => [{:name => 'Bryan'},{:name => 'Luke'}]}]}, + {:name => 'Eduardo', + :brothers => [{:name => 'Sebas', + :children => [{:name => 'Andres'},{:name => 'Jorge'}]}, + {:name => 'Elsa', + :children => [{:name => 'Natacha'}]}, + {:name => 'Milena', + :children => []}]}]}.to_xml(:root => 'customer') + ActiveResource::HttpMock.respond_to do |mock| mock.get "/people/1.xml", {}, @matz mock.get "/people/2.xml", {}, @david @@ -46,6 +78,8 @@ def setup mock.head "/people/1/addresses/2.xml", {}, nil, 404 mock.head "/people/2/addresses/1.xml", {}, nil, 404 mock.head "/people/Greg/addresses/1.xml", {}, nil, 200 + # customer + mock.get "/customers/1.xml", {}, @luis end Person.user = nil @@ -788,4 +822,18 @@ def test_to_param_quacks_like_active_record matz = Person.find(1) assert_equal '1', matz.to_param end + + def test_parse_deep_nested_resources + luis = Customer.find(1) + assert_kind_of Customer, luis + luis.friends.each do |friend| + assert_kind_of Customer::Friend, friend + friend.brothers.each do |brother| + assert_kind_of Customer::Friend::Brother, brother + brother.children.each do |child| + assert_kind_of Customer::Friend::Brother::Child, child + end + end + end + end end diff --git a/activeresource/test/fixtures/customer.rb b/activeresource/test/fixtures/customer.rb new file mode 100644 index 0000000000000..845d5d11cbfc3 --- /dev/null +++ b/activeresource/test/fixtures/customer.rb @@ -0,0 +1,3 @@ +class Customer < ActiveResource::Base + self.site = "http://37s.sunrise.i:3000" +end From 19895f087c338d8385dff9d272d30fb87cb10330 Mon Sep 17 00:00:00 2001 From: Pratik Naik Date: Tue, 10 Jun 2008 10:29:25 +0100 Subject: [PATCH 087/200] Lazy load cache and session stores --- actionpack/lib/action_controller/session_management.rb | 9 ++------- actionpack/test/controller/caching_test.rb | 1 + .../test/controller/session/mem_cache_store_test.rb | 2 +- activesupport/lib/active_support/cache.rb | 9 +++------ activesupport/lib/active_support/cache/drb_store.rb | 1 + 5 files changed, 8 insertions(+), 14 deletions(-) diff --git a/actionpack/lib/action_controller/session_management.rb b/actionpack/lib/action_controller/session_management.rb index 80a3ddd2c5741..ad1013b379253 100644 --- a/actionpack/lib/action_controller/session_management.rb +++ b/actionpack/lib/action_controller/session_management.rb @@ -1,10 +1,3 @@ -require 'action_controller/session/cookie_store' -require 'action_controller/session/drb_store' -require 'action_controller/session/mem_cache_store' -if Object.const_defined?(:ActiveRecord) - require 'action_controller/session/active_record_store' -end - module ActionController #:nodoc: module SessionManagement #:nodoc: def self.included(base) @@ -22,6 +15,8 @@ module ClassMethods # :p_store, :drb_store, :mem_cache_store, or # :memory_store) or your own custom class. def session_store=(store) + require "action_controller/session/#{store.to_s}" if [:active_record_store, :drb_store, :mem_cache_store].include?(store) + ActionController::CgiRequest::DEFAULT_SESSION_OPTIONS[:database_manager] = store.is_a?(Symbol) ? CGI::Session.const_get(store == :drb_store ? "DRbStore" : store.to_s.camelize) : store end diff --git a/actionpack/test/controller/caching_test.rb b/actionpack/test/controller/caching_test.rb index aee60b1c6f9bb..4fb2397e5bc5d 100644 --- a/actionpack/test/controller/caching_test.rb +++ b/actionpack/test/controller/caching_test.rb @@ -1,5 +1,6 @@ require 'fileutils' require 'abstract_unit' +require "active_support/cache/memory_store" CACHE_DIR = 'test_cache' # Don't change '/../temp/' cavalierly or you might hose something you don't want hosed diff --git a/actionpack/test/controller/session/mem_cache_store_test.rb b/actionpack/test/controller/session/mem_cache_store_test.rb index a7d48431f8e93..079a870ece02b 100644 --- a/actionpack/test/controller/session/mem_cache_store_test.rb +++ b/actionpack/test/controller/session/mem_cache_store_test.rb @@ -1,7 +1,7 @@ require 'abstract_unit' require 'action_controller/cgi_process' require 'action_controller/cgi_ext' - +require 'action_controller/session/mem_cache_store' class CGI::Session def cache diff --git a/activesupport/lib/active_support/cache.rb b/activesupport/lib/active_support/cache.rb index 2f1143e61052c..07c83774dfe0f 100644 --- a/activesupport/lib/active_support/cache.rb +++ b/activesupport/lib/active_support/cache.rb @@ -7,10 +7,13 @@ def self.lookup_store(*store_option) case store when Symbol + require "active_support/cache/#{store.to_s}" + store_class_name = (store == :drb_store ? "DRbStore" : store.to_s.camelize) store_class = ActiveSupport::Cache.const_get(store_class_name) store_class.new(*parameters) when nil + require "active_support/cache/memory_store" ActiveSupport::Cache::MemoryStore.new else store @@ -137,9 +140,3 @@ def delete_matched(matcher, options = nil) #:nodoc: end end end - -require 'active_support/cache/file_store' -require 'active_support/cache/memory_store' -require 'active_support/cache/drb_store' -require 'active_support/cache/mem_cache_store' -require 'active_support/cache/compressed_mem_cache_store' diff --git a/activesupport/lib/active_support/cache/drb_store.rb b/activesupport/lib/active_support/cache/drb_store.rb index b80c2ee4d53ae..f06f08f566bf7 100644 --- a/activesupport/lib/active_support/cache/drb_store.rb +++ b/activesupport/lib/active_support/cache/drb_store.rb @@ -1,4 +1,5 @@ require 'drb' +require 'active_support/cache/memory_store' module ActiveSupport module Cache From f5cbad21ac09822a61d6326cbadea16bbe86b623 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Tue, 10 Jun 2008 03:42:43 -0700 Subject: [PATCH 088/200] Rubinious: work around h[k] ||= v returning []= result instead of v --- .../lib/active_support/core_ext/enumerable.rb | 17 ++++++++++++++--- .../lib/active_support/ordered_hash.rb | 1 + 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/activesupport/lib/active_support/core_ext/enumerable.rb b/activesupport/lib/active_support/core_ext/enumerable.rb index f1469aa0e35c7..e7f537d045353 100644 --- a/activesupport/lib/active_support/core_ext/enumerable.rb +++ b/activesupport/lib/active_support/core_ext/enumerable.rb @@ -1,3 +1,5 @@ +require 'active_support/ordered_hash' + module Enumerable # Ruby 1.8.7 introduces group_by, but the result isn't ordered. Override it. remove_method(:group_by) if [].respond_to?(:group_by) && RUBY_VERSION < '1.9' @@ -18,10 +20,19 @@ module Enumerable # "2006-02-24 -> Transcript, Transcript" # "2006-02-23 -> Transcript" def group_by - inject ActiveSupport::OrderedHash.new do |grouped, element| - (grouped[yield(element)] ||= []) << element - grouped + assoc = ActiveSupport::OrderedHash.new + + each do |element| + key = yield(element) + + if assoc.has_key?(key) + assoc[key] << element + else + assoc[key] = [element] + end end + + assoc end unless [].respond_to?(:group_by) # Calculates a sum from the elements. Examples: diff --git a/activesupport/lib/active_support/ordered_hash.rb b/activesupport/lib/active_support/ordered_hash.rb index 59ceaec696697..9757054e43b85 100644 --- a/activesupport/lib/active_support/ordered_hash.rb +++ b/activesupport/lib/active_support/ordered_hash.rb @@ -12,6 +12,7 @@ def []=(key, value) else self << [key, value] end + value end def [](key) From 34c51c9e8f758a5fc892e654b6ca1ad8b86d1b3a Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Tue, 10 Jun 2008 03:46:23 -0700 Subject: [PATCH 089/200] Rubinious: setup/teardown override for miniunit --- .../testing/setup_and_teardown.rb | 38 +++++++++++++++---- 1 file changed, 31 insertions(+), 7 deletions(-) diff --git a/activesupport/lib/active_support/testing/setup_and_teardown.rb b/activesupport/lib/active_support/testing/setup_and_teardown.rb index ed8e34510a88f..21d71eb92a576 100644 --- a/activesupport/lib/active_support/testing/setup_and_teardown.rb +++ b/activesupport/lib/active_support/testing/setup_and_teardown.rb @@ -10,19 +10,43 @@ module SetupAndTeardown end def self.included(base) - base.send :include, ActiveSupport::Callbacks - base.define_callbacks :setup, :teardown + base.class_eval do + include ActiveSupport::Callbacks + define_callbacks :setup, :teardown + if defined?(::Mini) + alias_method :run, :run_with_callbacks_and_miniunit + else + begin + require 'mocha' + alias_method :run, :run_with_callbacks_and_mocha + rescue LoadError + alias_method :run, :run_with_callbacks_and_testunit + end + end + end + end + + def run_with_callbacks_and_miniunit(runner) + result = '.' begin - require 'mocha' - base.alias_method_chain :run, :callbacks_and_mocha - rescue LoadError - base.alias_method_chain :run, :callbacks + run_callbacks :setup + result = super + rescue Exception => e + result = runner.puke(self.class, self.name, e) + ensure + begin + teardown + run_callbacks :teardown, :enumerator => :reverse_each + rescue Exception => e + result = runner.puke(self.class, self.name, e) + end end + result end # This redefinition is unfortunate but test/unit shows us no alternative. - def run_with_callbacks(result) #:nodoc: + def run_with_callbacks_and_testunit(result) #:nodoc: return if @method_name.to_s == "default_test" yield(Test::Unit::TestCase::STARTED, name) From fb14c88e39cd28f1e9bf54ef79b85cc085a9f034 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Tue, 10 Jun 2008 14:02:38 -0700 Subject: [PATCH 090/200] Inflector -> ActiveSupport::Inflector --- railties/configs/initializers/inflections.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/railties/configs/initializers/inflections.rb b/railties/configs/initializers/inflections.rb index 09158b865ce15..d531b8bb8258b 100644 --- a/railties/configs/initializers/inflections.rb +++ b/railties/configs/initializers/inflections.rb @@ -2,7 +2,7 @@ # Add new inflection rules using the following format # (all these examples are active by default): -# Inflector.inflections do |inflect| +# ActiveSupport::Inflector.inflections do |inflect| # inflect.plural /^(ox)$/i, '\1en' # inflect.singular /^(ox)en/i, '\1' # inflect.irregular 'person', 'people' From b440aeb54a969ec25228881dd02eb019bbfd7c1e Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Tue, 10 Jun 2008 15:48:16 -0700 Subject: [PATCH 091/200] PostgreSQL: insert looks up pk and sequence name if not given. [#384 state:resolved] --- .../connection_adapters/postgresql_adapter.rb | 19 +++++++++++++++++-- .../test/cases/database_statements_test.rb | 12 ++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 activerecord/test/cases/database_statements_test.rb diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb index ae070421f180c..8a66ec82f9b59 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb @@ -415,8 +415,23 @@ def select_rows(sql, name = nil) # Executes an INSERT query and returns the new record's ID def insert(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil) - table = sql.split(" ", 4)[2].gsub('"', '') - super || pk && last_insert_id(table, sequence_name || default_sequence_name(table, pk)) + if insert_id = super + insert_id + else + # Extract the table from the insert sql. Yuck. + table = sql.split(" ", 4)[2].gsub('"', '') + + # If neither pk nor sequence name is given, look them up. + unless pk || sequence_name + pk, sequence_name = *pk_and_sequence_for(table) + end + + # If a pk is given, fallback to default sequence name. + # Don't fetch last insert id for a table without a pk. + if pk && sequence_name ||= default_sequence_name(table, pk) + last_insert_id(table, sequence_name) + end + end end # create a 2D array representing the result set diff --git a/activerecord/test/cases/database_statements_test.rb b/activerecord/test/cases/database_statements_test.rb new file mode 100644 index 0000000000000..6274d5250f4e1 --- /dev/null +++ b/activerecord/test/cases/database_statements_test.rb @@ -0,0 +1,12 @@ +require "cases/helper" + +class DatabaseStatementsTest < ActiveRecord::TestCase + def setup + @connection = ActiveRecord::Base.connection + end + + def test_insert_should_return_the_inserted_id + id = @connection.insert("INSERT INTO accounts (firm_id,credit_limit) VALUES (42,5000)") + assert_not_nil id + end +end From 0ad0bdc01c4fe2d66dd2b405433186824e7ce136 Mon Sep 17 00:00:00 2001 From: Pratik Naik Date: Tue, 10 Jun 2008 23:53:30 +0100 Subject: [PATCH 092/200] Delegate ActionView::Base#controller_name to controller --- actionpack/lib/action_view/base.rb | 2 +- actionpack/test/controller/new_render_test.rb | 15 ++++++++++++--- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/actionpack/lib/action_view/base.rb b/actionpack/lib/action_view/base.rb index eeebf335dc0b7..c417cc07acbf6 100644 --- a/actionpack/lib/action_view/base.rb +++ b/actionpack/lib/action_view/base.rb @@ -184,7 +184,7 @@ def self.cache_template_extensions=(*args) attr_internal :request delegate :request_forgery_protection_token, :template, :params, :session, :cookies, :response, :headers, - :flash, :logger, :action_name, :to => :controller + :flash, :logger, :action_name, :controller_name, :to => :controller module CompiledTemplates #:nodoc: # holds compiled template code diff --git a/actionpack/test/controller/new_render_test.rb b/actionpack/test/controller/new_render_test.rb index 6b83b8337eb9b..b77b3ceffa218 100644 --- a/actionpack/test/controller/new_render_test.rb +++ b/actionpack/test/controller/new_render_test.rb @@ -220,7 +220,7 @@ def render_to_string_with_assigns render :action => "test/hello_world" end - def render_to_string_with_partial + def render_to_string_with_partial @partial_only = render_to_string :partial => "partial_only" @partial_with_locals = render_to_string :partial => "customer", :locals => { :customer => Customer.new("david") } render :action => "test/hello_world" @@ -251,11 +251,15 @@ def accessing_request_in_template def accessing_logger_in_template render :inline => "<%= logger.class %>" end - + def accessing_action_name_in_template render :inline => "<%= action_name %>" end + def accessing_controller_name_in_template + render :inline => "<%= controller_name %>" + end + def accessing_params_in_template_with_layout render :layout => nil, :inline => "Hello: <%= params[:name] %>" end @@ -559,12 +563,17 @@ def test_access_to_logger_in_view get :accessing_logger_in_template assert_equal "Logger", @response.body end - + def test_access_to_action_name_in_view get :accessing_action_name_in_template assert_equal "accessing_action_name_in_template", @response.body end + def test_access_to_controller_name_in_view + get :accessing_controller_name_in_template + assert_equal "test", @response.body # name is explicitly set to 'test' inside the controller. + end + def test_render_xml get :render_xml_hello assert_equal "\n

        Hello David

        \n

        This is grand!

        \n\n", @response.body From 03bf72721900049ce18750767a22cf8091886c07 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Tue, 10 Jun 2008 18:31:37 -0700 Subject: [PATCH 093/200] PostgreSQL: use 'INSERT ... RETURNING id' for 8.2 and later. --- activerecord/CHANGELOG | 2 ++ .../connection_adapters/postgresql_adapter.rb | 23 ++++++++++++++++--- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index a65771648e7f2..a899bfbd52a75 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,5 +1,7 @@ *Edge* +* PostgreSQL: use 'INSERT ... RETURNING id' for 8.2 and later. [Jeremy Kemper] + * Added SQL escaping for :limit and :offset in MySQL [Jonathan Wiess] diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb index 8a66ec82f9b59..e759a74faf5c8 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb @@ -323,6 +323,12 @@ def supports_standard_conforming_strings? has_support end + def supports_insert_with_returning? + @supports_insert_with_returning ||= + @connection.respond_to?(:server_version) && + @connection.server_version >= 80200 + end + # Returns the configured supported identifier length supported by PostgreSQL, # or report the default of 63 on PostgreSQL 7.x. def table_alias_length @@ -415,12 +421,23 @@ def select_rows(sql, name = nil) # Executes an INSERT query and returns the new record's ID def insert(sql, name = nil, pk = nil, id_value = nil, sequence_name = nil) + # Extract the table from the insert sql. Yuck. + table = sql.split(" ", 4)[2].gsub('"', '') + + # Try an insert with 'returning id' if available (PG >= 8.2) + if supports_insert_with_returning? + pk, sequence_name = *pk_and_sequence_for(table) unless pk + if pk + id = select_value("#{sql} RETURNING #{quote_column_name(pk)}") + clear_query_cache + return id + end + end + + # Otherwise, insert then grab last_insert_id. if insert_id = super insert_id else - # Extract the table from the insert sql. Yuck. - table = sql.split(" ", 4)[2].gsub('"', '') - # If neither pk nor sequence name is given, look them up. unless pk || sequence_name pk, sequence_name = *pk_and_sequence_for(table) From e8a0ba4c93e2f0f811675bc6a6720725c866d1a5 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Tue, 10 Jun 2008 20:38:18 -0500 Subject: [PATCH 094/200] Ensure view path cache is rebuilt in production mode which was broke by df44df9. --- actionpack/lib/action_controller/dispatcher.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/actionpack/lib/action_controller/dispatcher.rb b/actionpack/lib/action_controller/dispatcher.rb index 64553fb25df6d..f20d9cc40fc91 100644 --- a/actionpack/lib/action_controller/dispatcher.rb +++ b/actionpack/lib/action_controller/dispatcher.rb @@ -10,10 +10,6 @@ def define_dispatcher_callbacks(cache_classes) # Development mode callbacks before_dispatch :reload_application after_dispatch :cleanup_application - - to_prepare :reload_view_path_cache do - ActionView::TemplateFinder.reload! unless ActionView::Base.cache_template_loading - end end # Common callbacks @@ -25,6 +21,10 @@ def define_dispatcher_callbacks(cache_classes) end end + to_prepare :reload_view_path_cache do + ActionView::TemplateFinder.reload! + end + if defined?(ActiveRecord) before_dispatch { ActiveRecord::Base.verify_active_connections! } to_prepare(:activerecord_instantiate_observers) { ActiveRecord::Base.instantiate_observers } From 634e462a0b70ddae2f21dbddddd07e7b340bb69c Mon Sep 17 00:00:00 2001 From: Grant Hollingworth Date: Tue, 10 Jun 2008 11:54:58 -0400 Subject: [PATCH 095/200] Performance: speed up Hash#except. [#382 state:resolved] --- activesupport/lib/active_support/core_ext/hash/except.rb | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/activesupport/lib/active_support/core_ext/hash/except.rb b/activesupport/lib/active_support/core_ext/hash/except.rb index 8362cd880e4fc..bc97fa35a6d80 100644 --- a/activesupport/lib/active_support/core_ext/hash/except.rb +++ b/activesupport/lib/active_support/core_ext/hash/except.rb @@ -10,13 +10,14 @@ module Hash #:nodoc: module Except # Returns a new hash without the given keys. def except(*keys) - rejected = Set.new(respond_to?(:convert_key) ? keys.map { |key| convert_key(key) } : keys) - reject { |key,| rejected.include?(key) } + clone.except!(*keys) end # Replaces the hash without only the given keys. def except!(*keys) - replace(except(*keys)) + keys.map! { |key| convert_key(key) } if respond_to?(:convert_key) + keys.each { |key| delete(key) } + self end end end From 3f594299c85ef111ac479b845fa8c7e37563ff66 Mon Sep 17 00:00:00 2001 From: Ruy Asan Date: Tue, 10 Jun 2008 21:13:27 -0700 Subject: [PATCH 096/200] TimeZone -> ActiveSupport::TimeZone. [#387 state:resolved] --- actionpack/lib/action_view/helpers/form_options_helper.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/actionpack/lib/action_view/helpers/form_options_helper.rb b/actionpack/lib/action_view/helpers/form_options_helper.rb index e0a097e367b9d..b3f8e63c1bb22 100644 --- a/actionpack/lib/action_view/helpers/form_options_helper.rb +++ b/actionpack/lib/action_view/helpers/form_options_helper.rb @@ -304,7 +304,7 @@ def country_options_for_select(selected = nil, priority_countries = nil) # # NOTE: Only the option tags are returned, you have to wrap this call in # a regular HTML select tag. - def time_zone_options_for_select(selected = nil, priority_zones = nil, model = TimeZone) + def time_zone_options_for_select(selected = nil, priority_zones = nil, model = ::ActiveSupport::TimeZone) zone_options = "" zones = model.all @@ -417,7 +417,7 @@ def to_time_zone_select_tag(priority_zones, options, html_options) value = value(object) content_tag("select", add_options( - time_zone_options_for_select(value || options[:default], priority_zones, options[:model] || TimeZone), + time_zone_options_for_select(value || options[:default], priority_zones, options[:model] || ActiveSupport::TimeZone), options, value ), html_options ) From f728e57d2204a429f5282856ec89d4e047e72957 Mon Sep 17 00:00:00 2001 From: Pratik Naik Date: Wed, 11 Jun 2008 09:11:08 +0100 Subject: [PATCH 097/200] Make sure cache_template_loading works and don't use to_prepare callback --- actionpack/CHANGELOG | 2 -- actionpack/lib/action_controller/dispatcher.rb | 5 +---- actionpack/lib/action_view/template.rb | 1 + 3 files changed, 2 insertions(+), 6 deletions(-) diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG index f9cb7154002ad..3096716dba547 100644 --- a/actionpack/CHANGELOG +++ b/actionpack/CHANGELOG @@ -1,7 +1,5 @@ *Edge* -* Ensure ActionView::TemplateFinder view cache is rebuilt on initialize [Josh Peek] - * Drop ActionController::Base.allow_concurrency flag [Josh Peek] * More efficient concat and capture helpers. Remove ActionView::Base.erb_variable. [Jeremy Kemper] diff --git a/actionpack/lib/action_controller/dispatcher.rb b/actionpack/lib/action_controller/dispatcher.rb index f20d9cc40fc91..fe4f6b4a7ea29 100644 --- a/actionpack/lib/action_controller/dispatcher.rb +++ b/actionpack/lib/action_controller/dispatcher.rb @@ -21,10 +21,6 @@ def define_dispatcher_callbacks(cache_classes) end end - to_prepare :reload_view_path_cache do - ActionView::TemplateFinder.reload! - end - if defined?(ActiveRecord) before_dispatch { ActiveRecord::Base.verify_active_connections! } to_prepare(:activerecord_instantiate_observers) { ActiveRecord::Base.instantiate_observers } @@ -138,6 +134,7 @@ def reload_application run_callbacks :prepare_dispatch Routing::Routes.reload + ActionView::TemplateFinder.reload! unless ActionView::Base.cache_template_loading end # Cleanup the application by clearing out loaded classes so they can diff --git a/actionpack/lib/action_view/template.rb b/actionpack/lib/action_view/template.rb index a878ac66d976d..25d5819af9fef 100644 --- a/actionpack/lib/action_view/template.rb +++ b/actionpack/lib/action_view/template.rb @@ -99,6 +99,7 @@ def raise_missing_template_exception # return the rendered template as a string. def self.register_template_handler(extension, klass) @@template_handlers[extension.to_sym] = klass + ActionView::TemplateFinder.reload! end def self.template_handler_extensions From 7f140bbddaf70abc61570f6cfdcbfba5771ffc78 Mon Sep 17 00:00:00 2001 From: Jan De Poorter Date: Wed, 11 Jun 2008 13:08:35 +0200 Subject: [PATCH 098/200] Add :validate option to associations. [#301 state:resolved] Signed-off-by: Pratik Naik --- activerecord/CHANGELOG | 2 ++ .../lib/active_record/associations.rb | 24 ++++++++++++------- .../belongs_to_associations_test.rb | 19 +++++++++++++++ .../has_many_associations_test.rb | 11 +++++++++ .../associations/has_one_associations_test.rb | 12 ++++++++++ activerecord/test/cases/reflection_test.rb | 6 ++--- activerecord/test/models/company.rb | 4 +++- activerecord/test/models/developer.rb | 1 + 8 files changed, 67 insertions(+), 12 deletions(-) diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index a899bfbd52a75..b726962872a6b 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,5 +1,7 @@ *Edge* +* Add :validate option to associations to enable/disable the automatic validation of associated models. Resolves #301. [Jan De Poorter] + * PostgreSQL: use 'INSERT ... RETURNING id' for 8.2 and later. [Jeremy Kemper] * Added SQL escaping for :limit and :offset in MySQL [Jonathan Wiess] diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb index 8ddcc24daa7a6..cbdb145078a09 100755 --- a/activerecord/lib/active_record/associations.rb +++ b/activerecord/lib/active_record/associations.rb @@ -690,6 +690,7 @@ module ClassMethods # association is a polymorphic +belongs_to+. # * :uniq - If true, duplicates will be omitted from the collection. Useful in conjunction with :through. # * :readonly - If true, all the associated objects are readonly through the association. + # * :validate - If false, don't validate the associated objects when saving the parent object. true by default. # # Option examples: # has_many :comments, :order => "posted_on" @@ -710,7 +711,7 @@ def has_many(association_id, options = {}, &extension) configure_dependency_for_has_many(reflection) - add_multiple_associated_save_callbacks(reflection.name) + add_multiple_associated_save_callbacks(reflection.name) unless options[:validate] == false add_association_callbacks(reflection.name, reflection.options) if options[:through] @@ -769,6 +770,7 @@ def has_many(association_id, options = {}, &extension) # * :source_type - Specifies type of the source association used by has_one :through queries where the source # association is a polymorphic +belongs_to+. # * :readonly - If true, the associated object is readonly through the association. + # * :validate - If false, don't validate the associated object when saving the parent object. +false+ by default. # # Option examples: # has_one :credit_card, :dependent => :destroy # destroys the associated credit card @@ -799,7 +801,7 @@ def has_one(association_id, options = {}) end after_save method_name - add_single_associated_save_callbacks(reflection.name) + add_single_associated_save_callbacks(reflection.name) if options[:validate] == true association_accessor_methods(reflection, HasOneAssociation) association_constructor_method(:build, reflection, HasOneAssociation) association_constructor_method(:create, reflection, HasOneAssociation) @@ -857,6 +859,7 @@ def has_one(association_id, options = {}) # Note: If you've enabled the counter cache, then you may want to add the counter cache attribute # to the +attr_readonly+ list in the associated classes (e.g. class Post; attr_readonly :comments_count; end). # * :readonly - If true, the associated object is readonly through the association. + # * :validate - If false, don't validate the associated objects when saving the parent object. +true+ by default. # # Option examples: # belongs_to :firm, :foreign_key => "client_of" @@ -937,6 +940,8 @@ def belongs_to(association_id, options = {}) ) end + add_single_associated_save_callbacks(reflection.name) unless options[:validate] == false + configure_dependency_for_belongs_to(reflection) end @@ -1025,6 +1030,7 @@ def belongs_to(association_id, options = {}) # * :select - By default, this is * as in SELECT * FROM, but can be changed if, for example, you want to do a join # but not include the joined columns. Do not forget to include the primary and foreign keys, otherwise it will raise an error. # * :readonly - If true, all the associated objects are readonly through the association. + # * :validate - If false, don't validate the associated objects when saving the parent object. +true+ by default. # # Option examples: # has_and_belongs_to_many :projects @@ -1037,7 +1043,7 @@ def belongs_to(association_id, options = {}) def has_and_belongs_to_many(association_id, options = {}, &extension) reflection = create_has_and_belongs_to_many_reflection(association_id, options, &extension) - add_multiple_associated_save_callbacks(reflection.name) + add_multiple_associated_save_callbacks(reflection.name) unless options[:validate] == false collection_accessor_methods(reflection, HasAndBelongsToManyAssociation) # Don't use a before_destroy callback since users' before_destroy @@ -1343,7 +1349,8 @@ def create_has_many_reflection(association_id, options, &extension) :uniq, :finder_sql, :counter_sql, :before_add, :after_add, :before_remove, :after_remove, - :extend, :readonly + :extend, :readonly, + :validate ) options[:extend] = create_extension_modules(association_id, extension, options[:extend]) @@ -1353,7 +1360,7 @@ def create_has_many_reflection(association_id, options, &extension) def create_has_one_reflection(association_id, options) options.assert_valid_keys( - :class_name, :foreign_key, :remote, :select, :conditions, :order, :include, :dependent, :counter_cache, :extend, :as, :readonly + :class_name, :foreign_key, :remote, :select, :conditions, :order, :include, :dependent, :counter_cache, :extend, :as, :readonly, :validate ) create_reflection(:has_one, association_id, options, self) @@ -1361,7 +1368,7 @@ def create_has_one_reflection(association_id, options) def create_has_one_through_reflection(association_id, options) options.assert_valid_keys( - :class_name, :foreign_key, :remote, :select, :conditions, :order, :include, :dependent, :counter_cache, :extend, :as, :through, :source, :source_type + :class_name, :foreign_key, :remote, :select, :conditions, :order, :include, :dependent, :counter_cache, :extend, :as, :through, :source, :source_type, :validate ) create_reflection(:has_one, association_id, options, self) end @@ -1369,7 +1376,7 @@ def create_has_one_through_reflection(association_id, options) def create_belongs_to_reflection(association_id, options) options.assert_valid_keys( :class_name, :foreign_key, :foreign_type, :remote, :select, :conditions, :include, :dependent, - :counter_cache, :extend, :polymorphic, :readonly + :counter_cache, :extend, :polymorphic, :readonly, :validate ) reflection = create_reflection(:belongs_to, association_id, options, self) @@ -1388,7 +1395,8 @@ def create_has_and_belongs_to_many_reflection(association_id, options, &extensio :uniq, :finder_sql, :delete_sql, :insert_sql, :before_add, :after_add, :before_remove, :after_remove, - :extend, :readonly + :extend, :readonly, + :validate ) options[:extend] = create_extension_modules(association_id, extension, options[:extend]) diff --git a/activerecord/test/cases/associations/belongs_to_associations_test.rb b/activerecord/test/cases/associations/belongs_to_associations_test.rb index e0da8bfb7abb8..9c718c4fefb15 100755 --- a/activerecord/test/cases/associations/belongs_to_associations_test.rb +++ b/activerecord/test/cases/associations/belongs_to_associations_test.rb @@ -409,4 +409,23 @@ def test_polymorphic_assignment_updates_foreign_id_field_for_new_and_saved_recor sponsor.sponsorable = new_member assert_equal nil, sponsor.sponsorable_id end + + def test_save_fails_for_invalid_belongs_to + assert log = AuditLog.create(:developer_id=>0,:message=>"") + + log.developer = Developer.new + assert !log.developer.valid? + assert !log.valid? + assert !log.save + assert_equal "is invalid", log.errors.on("developer") + end + + def test_save_succeeds_for_invalid_belongs_to_with_validate_false + assert log = AuditLog.create(:developer_id=>0,:message=>"") + + log.unvalidated_developer = Developer.new + assert !log.unvalidated_developer.valid? + assert log.valid? + assert log.save + end end diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb index dbfa025efbd8a..1f23ff256cbf1 100644 --- a/activerecord/test/cases/associations/has_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_associations_test.rb @@ -342,6 +342,17 @@ def test_invalid_adding_before_save assert new_firm.new_record? end + def test_invalid_adding_with_validate_false + firm = Firm.find(:first) + client = Client.new + firm.unvalidated_clients_of_firm << Client.new + + assert firm.valid? + assert !client.valid? + assert firm.save + assert client.new_record? + end + def test_build company = companies(:first_firm) new_client = assert_no_queries { company.clients_of_firm.build("name" => "Another Client") } diff --git a/activerecord/test/cases/associations/has_one_associations_test.rb b/activerecord/test/cases/associations/has_one_associations_test.rb index abc7ee7e9d721..d3ca0cae414b5 100755 --- a/activerecord/test/cases/associations/has_one_associations_test.rb +++ b/activerecord/test/cases/associations/has_one_associations_test.rb @@ -275,6 +275,18 @@ def test_save_fails_for_invalid_has_one assert_equal "is invalid", firm.errors.on("account") end + + def test_save_succeeds_for_invalid_has_one_with_validate_false + firm = Firm.find(:first) + assert firm.valid? + + firm.unvalidated_account = Account.new + + assert !firm.unvalidated_account.valid? + assert firm.valid? + assert firm.save + end + def test_assignment_before_either_saved firm = Firm.new("name" => "GlobalMegaCorp") firm.account = a = Account.new("credit_limit" => 1000) diff --git a/activerecord/test/cases/reflection_test.rb b/activerecord/test/cases/reflection_test.rb index 8b4d2325543ad..0c57b79401b0c 100644 --- a/activerecord/test/cases/reflection_test.rb +++ b/activerecord/test/cases/reflection_test.rb @@ -160,9 +160,9 @@ def test_association_reflection_in_modules def test_reflection_of_all_associations # FIXME these assertions bust a lot - assert_equal 20, Firm.reflect_on_all_associations.size - assert_equal 16, Firm.reflect_on_all_associations(:has_many).size - assert_equal 4, Firm.reflect_on_all_associations(:has_one).size + assert_equal 22, Firm.reflect_on_all_associations.size + assert_equal 17, Firm.reflect_on_all_associations(:has_many).size + assert_equal 5, Firm.reflect_on_all_associations(:has_one).size assert_equal 0, Firm.reflect_on_all_associations(:belongs_to).size end diff --git a/activerecord/test/models/company.rb b/activerecord/test/models/company.rb index 70f83fa8e654f..9fa810ac6897a 100755 --- a/activerecord/test/models/company.rb +++ b/activerecord/test/models/company.rb @@ -26,6 +26,7 @@ class Firm < Company "AND (#{QUOTED_TYPE} = 'Client' OR #{QUOTED_TYPE} = 'SpecialClient' OR #{QUOTED_TYPE} = 'VerySpecialClient' )" has_many :clients_sorted_desc, :class_name => "Client", :order => "id DESC" has_many :clients_of_firm, :foreign_key => "client_of", :class_name => "Client", :order => "id" + has_many :unvalidated_clients_of_firm, :foreign_key => "client_of", :class_name => "Client", :validate => false has_many :dependent_clients_of_firm, :foreign_key => "client_of", :class_name => "Client", :order => "id", :dependent => :destroy has_many :exclusively_dependent_clients_of_firm, :foreign_key => "client_of", :class_name => "Client", :order => "id", :dependent => :delete_all has_many :limited_clients, :class_name => "Client", :order => "id", :limit => 1 @@ -46,7 +47,8 @@ class Firm < Company has_many :plain_clients, :class_name => 'Client' has_many :readonly_clients, :class_name => 'Client', :readonly => true - has_one :account, :foreign_key => "firm_id", :dependent => :destroy + has_one :account, :foreign_key => "firm_id", :dependent => :destroy, :validate => true + has_one :unvalidated_account, :foreign_key => "firm_id", :class_name => 'Account', :validate => false has_one :account_with_select, :foreign_key => "firm_id", :select => "id, firm_id", :class_name=>'Account' has_one :readonly_account, :foreign_key => "firm_id", :class_name => "Account", :readonly => true end diff --git a/activerecord/test/models/developer.rb b/activerecord/test/models/developer.rb index f77fd0e96dadf..7a64bed5ff0f7 100644 --- a/activerecord/test/models/developer.rb +++ b/activerecord/test/models/developer.rb @@ -57,6 +57,7 @@ def log=(message) class AuditLog < ActiveRecord::Base belongs_to :developer + belongs_to :unvalidated_developer, :class_name => 'Developer', :validate => false end DeveloperSalary = Struct.new(:amount) From 71bf756ea21f338771b53d02951d6654bd295649 Mon Sep 17 00:00:00 2001 From: Pratik Naik Date: Wed, 11 Jun 2008 12:39:56 +0100 Subject: [PATCH 099/200] Disable validations for associated belongs_to record by default --- activerecord/lib/active_record/associations.rb | 2 +- activerecord/test/models/developer.rb | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb index cbdb145078a09..fc186f806c208 100755 --- a/activerecord/lib/active_record/associations.rb +++ b/activerecord/lib/active_record/associations.rb @@ -940,7 +940,7 @@ def belongs_to(association_id, options = {}) ) end - add_single_associated_save_callbacks(reflection.name) unless options[:validate] == false + add_single_associated_save_callbacks(reflection.name) if options[:validate] == true configure_dependency_for_belongs_to(reflection) end diff --git a/activerecord/test/models/developer.rb b/activerecord/test/models/developer.rb index 7a64bed5ff0f7..9f26cacdecb70 100644 --- a/activerecord/test/models/developer.rb +++ b/activerecord/test/models/developer.rb @@ -56,8 +56,8 @@ def log=(message) end class AuditLog < ActiveRecord::Base - belongs_to :developer - belongs_to :unvalidated_developer, :class_name => 'Developer', :validate => false + belongs_to :developer, :validate => true + belongs_to :unvalidated_developer, :class_name => 'Developer' end DeveloperSalary = Struct.new(:amount) From 6fd73442d83ee162af622b722b1e1b7356fa9fcb Mon Sep 17 00:00:00 2001 From: Pratik Naik Date: Wed, 11 Jun 2008 12:57:19 +0100 Subject: [PATCH 100/200] Update docs to reflect 71bf75 --- activerecord/lib/active_record/associations.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb index fc186f806c208..5f42b5a4594dd 100755 --- a/activerecord/lib/active_record/associations.rb +++ b/activerecord/lib/active_record/associations.rb @@ -859,7 +859,7 @@ def has_one(association_id, options = {}) # Note: If you've enabled the counter cache, then you may want to add the counter cache attribute # to the +attr_readonly+ list in the associated classes (e.g. class Post; attr_readonly :comments_count; end). # * :readonly - If true, the associated object is readonly through the association. - # * :validate - If false, don't validate the associated objects when saving the parent object. +true+ by default. + # * :validate - If false, don't validate the associated objects when saving the parent object. +false+ by default. # # Option examples: # belongs_to :firm, :foreign_key => "client_of" From 3e07f320c10b53ec21b947d949d0063e724c5fd8 Mon Sep 17 00:00:00 2001 From: Jonathan del Strother Date: Thu, 6 Mar 2008 11:50:44 +0000 Subject: [PATCH 101/200] Improve ActionCaching's format-handling Make ActionCaching more aware of different mimetype formats. It will now use request.format to look up the cache type, in addition to the path extension. When expiring caches, the request format no longer affects which cache is expired. Signed-off-by: Pratik Naik --- actionpack/CHANGELOG | 2 + .../lib/action_controller/caching/actions.rb | 41 +++++++++----- actionpack/test/controller/caching_test.rb | 53 +++++++++++++++++-- 3 files changed, 79 insertions(+), 17 deletions(-) diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG index 3096716dba547..8d1acb265f918 100644 --- a/actionpack/CHANGELOG +++ b/actionpack/CHANGELOG @@ -1,5 +1,7 @@ *Edge* +* Make caching more aware of mime types. Ensure request format is not considered while expiring cache. [Jonathan del Strother] + * Drop ActionController::Base.allow_concurrency flag [Josh Peek] * More efficient concat and capture helpers. Remove ActionView::Base.erb_variable. [Jeremy Kemper] diff --git a/actionpack/lib/action_controller/caching/actions.rb b/actionpack/lib/action_controller/caching/actions.rb index c4b0a97a33d75..65a36f7f98bdb 100644 --- a/actionpack/lib/action_controller/caching/actions.rb +++ b/actionpack/lib/action_controller/caching/actions.rb @@ -67,10 +67,10 @@ def expire_action(options = {}) if options[:action].is_a?(Array) options[:action].dup.each do |action| - expire_fragment(ActionCachePath.path_for(self, options.merge({ :action => action }))) + expire_fragment(ActionCachePath.path_for(self, options.merge({ :action => action }), false)) end else - expire_fragment(ActionCachePath.path_for(self, options)) + expire_fragment(ActionCachePath.path_for(self, options, false)) end end @@ -125,16 +125,24 @@ class ActionCachePath attr_reader :path, :extension class << self - def path_for(controller, options) - new(controller, options).path + def path_for(controller, options, infer_extension=true) + new(controller, options, infer_extension).path end end - - def initialize(controller, options = {}) - @extension = extract_extension(controller.request.path) + + # When true, infer_extension will look up the cache path extension from the request's path & format. + # This is desirable when reading and writing the cache, but not when expiring the cache - expire_action should expire the same files regardless of the request format. + def initialize(controller, options = {}, infer_extension=true) + if infer_extension and options.is_a? Hash + request_extension = extract_extension(controller.request) + options = options.reverse_merge(:format => request_extension) + end path = controller.url_for(options).split('://').last normalize!(path) - add_extension!(path, @extension) + if infer_extension + @extension = request_extension + add_extension!(path, @extension) + end @path = URI.unescape(path) end @@ -144,13 +152,22 @@ def normalize!(path) end def add_extension!(path, extension) - path << ".#{extension}" if extension + path << ".#{extension}" if extension and !path.ends_with?(extension) end - - def extract_extension(file_path) + + def extract_extension(request) # Don't want just what comes after the last '.' to accommodate multi part extensions # such as tar.gz. - file_path[/^[^.]+\.(.+)$/, 1] + extension = request.path[/^[^.]+\.(.+)$/, 1] + + # If there's no extension in the path, check request.format + if extension.nil? + extension = request.format.to_sym.to_s + if extension=='all' + extension = nil + end + end + extension end end end diff --git a/actionpack/test/controller/caching_test.rb b/actionpack/test/controller/caching_test.rb index 4fb2397e5bc5d..89e12ddae3809 100644 --- a/actionpack/test/controller/caching_test.rb +++ b/actionpack/test/controller/caching_test.rb @@ -189,6 +189,10 @@ def expire expire_action :controller => 'action_caching_test', :action => 'index' render :nothing => true end + def expire_xml + expire_action :controller => 'action_caching_test', :action => 'index', :format => 'xml' + render :nothing => true + end end class MockTime < Time @@ -214,6 +218,7 @@ def request mocked_path = @mock_path Object.new.instance_eval(<<-EVAL) def path; '#{@mock_path}' end + def format; 'all' end self EVAL end @@ -327,6 +332,20 @@ def test_cache_expiration assert_equal new_cached_time, @response.body end + def test_cache_expiration_isnt_affected_by_request_format + get :index + cached_time = content_to_cache + reset! + + @request.set_REQUEST_URI "/action_caching_test/expire.xml" + get :expire, :format => :xml + reset! + + get :index + new_cached_time = content_to_cache + assert_not_equal cached_time, @response.body + end + def test_cache_is_scoped_by_subdomain @request.host = 'jamis.hostname.com' get :index @@ -371,11 +390,35 @@ def test_forbidden_is_not_cached end def test_xml_version_of_resource_is_treated_as_different_cache - @mock_controller.mock_url_for = 'http://example.org/posts/' - @mock_controller.mock_path = '/posts/index.xml' - path_object = @path_class.new(@mock_controller, {}) - assert_equal 'xml', path_object.extension - assert_equal 'example.org/posts/index.xml', path_object.path + with_routing do |set| + ActionController::Routing::Routes.draw do |map| + map.connect ':controller/:action.:format' + map.connect ':controller/:action' + end + + get :index, :format => 'xml' + cached_time = content_to_cache + assert_equal cached_time, @response.body + assert fragment_exist?('hostname.com/action_caching_test/index.xml') + reset! + + get :index, :format => 'xml' + assert_equal cached_time, @response.body + assert_equal 'application/xml', @response.content_type + reset! + + @request.env['HTTP_ACCEPT'] = "application/xml" + get :index + assert_equal cached_time, @response.body + assert_equal 'application/xml', @response.content_type + reset! + + get :expire_xml + reset! + + get :index, :format => 'xml' + assert_not_equal cached_time, @response.body + end end def test_correct_content_type_is_returned_for_cache_hit From ca9641f8a7ca1142d0ea99405a079c8699bd443c Mon Sep 17 00:00:00 2001 From: Jan De Poorter Date: Wed, 11 Jun 2008 13:17:40 +0100 Subject: [PATCH 102/200] Fix FormOptionsHelper tests. Signed-off-by: Pratik Naik --- actionpack/test/template/form_options_helper_test.rb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/actionpack/test/template/form_options_helper_test.rb b/actionpack/test/template/form_options_helper_test.rb index 4fb2be9d3f182..d5aeb4939e5a7 100644 --- a/actionpack/test/template/form_options_helper_test.rb +++ b/actionpack/test/template/form_options_helper_test.rb @@ -20,7 +20,7 @@ def to_s end end -ActionView::Helpers::FormOptionsHelper::TimeZone = MockTimeZone +ActiveSupport::TimeZone = MockTimeZone class FormOptionsHelperTest < ActionView::TestCase tests ActionView::Helpers::FormOptionsHelper @@ -183,7 +183,7 @@ def test_time_zone_options_with_unknown_selected end def test_time_zone_options_with_priority_zones - zones = [ TimeZone.new( "B" ), TimeZone.new( "E" ) ] + zones = [ ActiveSupport::TimeZone.new( "B" ), ActiveSupport::TimeZone.new( "E" ) ] opts = time_zone_options_for_select( nil, zones ) assert_dom_equal "\n" + "" + @@ -195,7 +195,7 @@ def test_time_zone_options_with_priority_zones end def test_time_zone_options_with_selected_priority_zones - zones = [ TimeZone.new( "B" ), TimeZone.new( "E" ) ] + zones = [ ActiveSupport::TimeZone.new( "B" ), ActiveSupport::TimeZone.new( "E" ) ] opts = time_zone_options_for_select( "E", zones ) assert_dom_equal "\n" + "" + @@ -207,7 +207,7 @@ def test_time_zone_options_with_selected_priority_zones end def test_time_zone_options_with_unselected_priority_zones - zones = [ TimeZone.new( "B" ), TimeZone.new( "E" ) ] + zones = [ ActiveSupport::TimeZone.new( "B" ), ActiveSupport::TimeZone.new( "E" ) ] opts = time_zone_options_for_select( "C", zones ) assert_dom_equal "\n" + "" + @@ -1287,7 +1287,7 @@ def test_time_zone_select_with_blank_as_string_and_style def test_time_zone_select_with_priority_zones @firm = Firm.new("D") - zones = [ TimeZone.new("A"), TimeZone.new("D") ] + zones = [ ActiveSupport::TimeZone.new("A"), ActiveSupport::TimeZone.new("D") ] html = time_zone_select("firm", "time_zone", zones ) assert_dom_equal " @@ -146,13 +146,13 @@ def label_tag(name, text = nil, options = {}) # # => # # hidden_field_tag 'collected_input', '', :onchange => "alert('Input collected!')" - # # => def hidden_field_tag(name, value = nil, options = {}) text_field_tag(name, value, options.stringify_keys.update("type" => "hidden")) end - # Creates a file upload field. If you are using file uploads then you will also need + # Creates a file upload field. If you are using file uploads then you will also need # to set the multipart option for the form tag: # # <%= form_tag { :action => "post" }, { :multipart => true } %> @@ -160,7 +160,7 @@ def hidden_field_tag(name, value = nil, options = {}) # <%= submit_tag %> # <%= end_form_tag %> # - # The specified URL will then be passed a File object containing the selected file, or if the field + # The specified URL will then be passed a File object containing the selected file, or if the field # was left blank, a StringIO object. # # ==== Options @@ -181,7 +181,7 @@ def hidden_field_tag(name, value = nil, options = {}) # # => # # file_field_tag 'user_pic', :accept => 'image/png,image/gif,image/jpeg' - # # => + # # => # # file_field_tag 'file', :accept => 'text/html', :class => 'upload', :value => 'index.html' # # => @@ -286,7 +286,7 @@ def check_box_tag(name, value = "1", checked = false, options = {}) tag :input, html_options end - # Creates a radio button; use groups of radio buttons named the same to allow users to + # Creates a radio button; use groups of radio buttons named the same to allow users to # select from a group of options. # # ==== Options @@ -313,14 +313,14 @@ def radio_button_tag(name, value, checked = false, options = {}) tag :input, html_options end - # Creates a submit button with the text value as the caption. + # Creates a submit button with the text value as the caption. # # ==== Options # * :confirm => 'question?' - This will add a JavaScript confirm # prompt with the question specified. If the user accepts, the form is # processed normally, otherwise no action is taken. # * :disabled - If true, the user will not be able to use this input. - # * :disable_with - Value of this parameter will be used as the value for a disabled version + # * :disable_with - Value of this parameter will be used as the value for a disabled version # of the submit button when the form is submitted. # * Any other key creates standard HTML options for the tag. # @@ -335,7 +335,7 @@ def radio_button_tag(name, value, checked = false, options = {}) # # => # # submit_tag "Complete sale", :disable_with => "Please wait..." - # # => # # submit_tag nil, :class => "form_submit" @@ -346,7 +346,7 @@ def radio_button_tag(name, value, checked = false, options = {}) # # name="commit" type="submit" value="Edit" /> def submit_tag(value = "Save changes", options = {}) options.stringify_keys! - + if disable_with = options.delete("disable_with") options["onclick"] = [ "this.setAttribute('originalValue', this.value)", @@ -358,15 +358,15 @@ def submit_tag(value = "Save changes", options = {}) "return result;", ].join(";") end - + if confirm = options.delete("confirm") options["onclick"] ||= '' options["onclick"] += "return #{confirm_javascript_function(confirm)};" end - + tag :input, { "type" => "submit", "name" => "commit", "value" => value }.update(options.stringify_keys) end - + # Displays an image which when clicked will submit the form. # # source is passed to AssetTagHelper#image_path @@ -412,7 +412,7 @@ def field_set_tag(legend = nil, &block) concat(content) concat("") end - + private def html_options_for_form(url_for_options, options, *parameters_for_url) returning options.stringify_keys do |html_options| @@ -420,7 +420,7 @@ def html_options_for_form(url_for_options, options, *parameters_for_url) html_options["action"] = url_for(url_for_options, *parameters_for_url) end end - + def extra_tags_for_form(html_options) case method = html_options.delete("method").to_s when /^get$/i # must be case-insentive, but can't use downcase as might be nil @@ -434,12 +434,12 @@ def extra_tags_for_form(html_options) content_tag(:div, tag(:input, :type => "hidden", :name => "_method", :value => method) + token_tag, :style => 'margin:0;padding:0') end end - + def form_tag_html(html_options) extra_tags = extra_tags_for_form(html_options) tag(:form, html_options, true) + extra_tags end - + def form_tag_in_block(html_options, &block) content = capture(&block) concat(form_tag_html(html_options)) diff --git a/actionpack/lib/action_view/helpers/tag_helper.rb b/actionpack/lib/action_view/helpers/tag_helper.rb index e1abec1847ed4..649ec87a2486d 100644 --- a/actionpack/lib/action_view/helpers/tag_helper.rb +++ b/actionpack/lib/action_view/helpers/tag_helper.rb @@ -12,14 +12,14 @@ module TagHelper BOOLEAN_ATTRIBUTES = %w(disabled readonly multiple).to_set BOOLEAN_ATTRIBUTES.merge(BOOLEAN_ATTRIBUTES.map(&:to_sym)) - # Returns an empty HTML tag of type +name+ which by default is XHTML - # compliant. Set +open+ to true to create an open tag compatible - # with HTML 4.0 and below. Add HTML attributes by passing an attributes + # Returns an empty HTML tag of type +name+ which by default is XHTML + # compliant. Set +open+ to true to create an open tag compatible + # with HTML 4.0 and below. Add HTML attributes by passing an attributes # hash to +options+. Set +escape+ to false to disable attribute value # escaping. # # ==== Options - # The +options+ hash is used with attributes with no value like (disabled and + # The +options+ hash is used with attributes with no value like (disabled and # readonly), which you can give a value of true in the +options+ hash. You can use # symbols or strings for the attribute names. # @@ -30,7 +30,7 @@ module TagHelper # tag("br", nil, true) # # =>
        # - # tag("input", { :type => 'text', :disabled => true }) + # tag("input", { :type => 'text', :disabled => true }) # # => # # tag("img", { :src => "open & shut.png" }) @@ -43,13 +43,13 @@ def tag(name, options = nil, open = false, escape = true) end # Returns an HTML block tag of type +name+ surrounding the +content+. Add - # HTML attributes by passing an attributes hash to +options+. + # HTML attributes by passing an attributes hash to +options+. # Instead of passing the content as an argument, you can also use a block # in which case, you pass your +options+ as the second parameter. # Set escape to false to disable attribute value escaping. # # ==== Options - # The +options+ hash is used with attributes with no value like (disabled and + # The +options+ hash is used with attributes with no value like (disabled and # readonly), which you can give a value of true in the +options+ hash. You can use # symbols or strings for the attribute names. # @@ -68,7 +68,13 @@ def tag(name, options = nil, open = false, escape = true) def content_tag(name, content_or_options_with_block = nil, options = nil, escape = true, &block) if block_given? options = content_or_options_with_block if content_or_options_with_block.is_a?(Hash) - concat(content_tag_string(name, capture(&block), options, escape)) + content_tag = content_tag_string(name, capture(&block), options, escape) + + if block_called_from_erb?(block) + concat(content_tag) + else + content_tag + end else content_tag_string(name, content_or_options_with_block, options, escape) end @@ -102,6 +108,16 @@ def escape_once(html) end private + BLOCK_CALLED_FROM_ERB = 'defined? __in_erb_template' + + # Check whether we're called from an erb template. + # We'd return a string in any other case, but erb <%= ... %> + # can't take an <% end %> later on, so we have to use <% ... %> + # and implicitly concat. + def block_called_from_erb?(block) + eval(BLOCK_CALLED_FROM_ERB, block) + end + def content_tag_string(name, content, options, escape = true) tag_options = tag_options(options, escape) if options "<#{name}#{tag_options}>#{content}" diff --git a/actionpack/lib/action_view/helpers/text_helper.rb b/actionpack/lib/action_view/helpers/text_helper.rb index a1a91f6b3d0ea..d98c5bd0d5c34 100644 --- a/actionpack/lib/action_view/helpers/text_helper.rb +++ b/actionpack/lib/action_view/helpers/text_helper.rb @@ -30,11 +30,7 @@ def concat(string, unused_binding = nil) ActiveSupport::Deprecation.warn("The binding argument of #concat is no longer needed. Please remove it from your views and helpers.") end - if output_buffer && string - output_buffer << string - else - string - end + output_buffer << string end if RUBY_VERSION < '1.9' diff --git a/actionpack/lib/action_view/template_handlers/compilable.rb b/actionpack/lib/action_view/template_handlers/compilable.rb index 1aef81ba1a410..783456ab53824 100644 --- a/actionpack/lib/action_view/template_handlers/compilable.rb +++ b/actionpack/lib/action_view/template_handlers/compilable.rb @@ -106,7 +106,13 @@ def create_template_source(template, render_symbol) locals_code << "#{key} = local_assigns[:#{key}]\n" end - "def #{render_symbol}(local_assigns)\nold_output_buffer = output_buffer;#{locals_code}#{body}\nensure\nself.output_buffer = old_output_buffer\nend" + <<-end_src + def #{render_symbol}(local_assigns) + old_output_buffer = output_buffer;#{locals_code}#{body} + ensure + self.output_buffer = old_output_buffer + end + end_src end # Return true if the given template was compiled for a superset of the keys in local_assigns diff --git a/actionpack/lib/action_view/template_handlers/erb.rb b/actionpack/lib/action_view/template_handlers/erb.rb index 0733b3e3c2582..458416b6cbabd 100644 --- a/actionpack/lib/action_view/template_handlers/erb.rb +++ b/actionpack/lib/action_view/template_handlers/erb.rb @@ -48,7 +48,8 @@ class ERB < TemplateHandler self.erb_trim_mode = '-' def compile(template) - ::ERB.new(template.source, nil, erb_trim_mode, '@output_buffer').src + src = ::ERB.new(template.source, nil, erb_trim_mode, '@output_buffer').src + "__in_erb_template=true;#{src}" end def cache_fragment(block, name = {}, options = nil) #:nodoc: diff --git a/actionpack/test/fixtures/test/non_erb_block_content_for.builder b/actionpack/test/fixtures/test/non_erb_block_content_for.builder index 6ff6db0f953e9..a94643561c09d 100644 --- a/actionpack/test/fixtures/test/non_erb_block_content_for.builder +++ b/actionpack/test/fixtures/test/non_erb_block_content_for.builder @@ -1,4 +1,4 @@ content_for :title do 'Putting stuff in the title!' end -xml << "\nGreat stuff!" \ No newline at end of file +xml << "\nGreat stuff!" diff --git a/actionpack/test/template/form_tag_helper_test.rb b/actionpack/test/template/form_tag_helper_test.rb index 47b3605849e2c..eabbe9c8a03d9 100644 --- a/actionpack/test/template/form_tag_helper_test.rb +++ b/actionpack/test/template/form_tag_helper_test.rb @@ -35,21 +35,23 @@ def test_form_tag_with_method_put expected = %(
        ) assert_dom_equal expected, actual end - + def test_form_tag_with_method_delete actual = form_tag({}, { :method => :delete }) expected = %(
        ) assert_dom_equal expected, actual end - def test_form_tag_with_block + def test_form_tag_with_block_in_erb + __in_erb_template = '' form_tag("http://example.com") { concat "Hello world!" } expected = %(Hello world!
        ) assert_dom_equal expected, output_buffer end - def test_form_tag_with_block_and_method + def test_form_tag_with_block_and_method_in_erb + __in_erb_template = '' form_tag("http://example.com", :method => :put) { concat "Hello world!" } expected = %(
        Hello world!
        ) @@ -88,11 +90,11 @@ def test_radio_button_tag actual = radio_button_tag("gender", "m") + radio_button_tag("gender", "f") expected = %() assert_dom_equal expected, actual - + actual = radio_button_tag("opinion", "-1") + radio_button_tag("opinion", "1") expected = %() assert_dom_equal expected, actual - + actual = radio_button_tag("person[gender]", "m") expected = %() assert_dom_equal expected, actual @@ -103,13 +105,13 @@ def test_select_tag expected = %() assert_dom_equal expected, actual end - + def test_select_tag_with_multiple actual = select_tag "colors", "", :multiple => :true expected = %() assert_dom_equal expected, actual end - + def test_select_tag_disabled actual = select_tag "places", "", :disabled => :true expected = %() @@ -145,37 +147,37 @@ def test_text_field_tag_class_string expected = %() assert_dom_equal expected, actual end - + def test_text_field_tag_size_symbol actual = text_field_tag "title", "Hello!", :size => 75 expected = %() assert_dom_equal expected, actual end - + def test_text_field_tag_size_string actual = text_field_tag "title", "Hello!", "size" => "75" expected = %() assert_dom_equal expected, actual end - + def test_text_field_tag_maxlength_symbol actual = text_field_tag "title", "Hello!", :maxlength => 75 expected = %() assert_dom_equal expected, actual end - + def test_text_field_tag_maxlength_string actual = text_field_tag "title", "Hello!", "maxlength" => "75" expected = %() assert_dom_equal expected, actual end - + def test_text_field_disabled actual = text_field_tag "title", "Hello!", :disabled => :true expected = %() assert_dom_equal expected, actual end - + def test_text_field_tag_with_multiple_options actual = text_field_tag "title", "Hello!", :size => 70, :maxlength => 80 expected = %() @@ -226,12 +228,13 @@ def test_submit_tag_with_confirmation submit_tag("Save", :confirm => "Are you sure?") ) end - + def test_pass assert_equal 1, 1 end - def test_field_set_tag + def test_field_set_tag_in_erb + __in_erb_template = '' field_set_tag("Your details") { concat "Hello world!" } expected = %(
        Your detailsHello world!
        ) diff --git a/actionpack/test/template/javascript_helper_test.rb b/actionpack/test/template/javascript_helper_test.rb index 8c649ea544251..d6d398d22411f 100644 --- a/actionpack/test/template/javascript_helper_test.rb +++ b/actionpack/test/template/javascript_helper_test.rb @@ -48,7 +48,7 @@ def test_link_to_function_with_href end def test_link_to_function_with_href - assert_dom_equal %(Greeting), + assert_dom_equal %(Greeting), link_to_function("Greeting", "alert('Hello world!')", :href => 'http://example.com/') end @@ -95,12 +95,14 @@ def test_javascript_tag_with_options javascript_tag("alert('hello')", :id => "the_js_tag") end - def test_javascript_tag_with_block + def test_javascript_tag_with_block_in_erb + __in_erb_template = '' javascript_tag { concat "alert('hello')" } assert_dom_equal "", output_buffer end - def test_javascript_tag_with_block_and_options + def test_javascript_tag_with_block_and_options_in_erb + __in_erb_template = '' javascript_tag(:id => "the_js_tag") { concat "alert('hello')" } assert_dom_equal "", output_buffer end diff --git a/actionpack/test/template/prototype_helper_test.rb b/actionpack/test/template/prototype_helper_test.rb index a5be0d2789721..60b83b476d6d1 100644 --- a/actionpack/test/template/prototype_helper_test.rb +++ b/actionpack/test/template/prototype_helper_test.rb @@ -54,7 +54,7 @@ def protect_against_forgery? end def create_generator - block = Proc.new { |*args| yield *args if block_given? } + block = Proc.new { |*args| yield *args if block_given? } JavaScriptGenerator.new self, &block end end @@ -70,7 +70,7 @@ def test_link_to_remote assert_dom_equal %(Remote outauthor), link_to_remote("Remote outauthor", { :url => { :action => "whatnot" }}, { :class => "fine" }) assert_dom_equal %(Remote outauthor), - link_to_remote("Remote outauthor", :complete => "alert(request.responseText)", :url => { :action => "whatnot" }) + link_to_remote("Remote outauthor", :complete => "alert(request.responseText)", :url => { :action => "whatnot" }) assert_dom_equal %(Remote outauthor), link_to_remote("Remote outauthor", :success => "alert(request.responseText)", :url => { :action => "whatnot" }) assert_dom_equal %(Remote outauthor), @@ -78,12 +78,12 @@ def test_link_to_remote assert_dom_equal %(Remote outauthor), link_to_remote("Remote outauthor", :failure => "alert(request.responseText)", :url => { :action => "whatnot", :a => '10', :b => '20' }) end - + def test_link_to_remote_html_options assert_dom_equal %(Remote outauthor), link_to_remote("Remote outauthor", { :url => { :action => "whatnot" }, :html => { :class => "fine" } }) end - + def test_link_to_remote_url_quote_escaping assert_dom_equal %(Remote), link_to_remote("Remote", { :url => { :action => "whatnot's" } }) @@ -93,14 +93,14 @@ def test_periodically_call_remote assert_dom_equal %(), periodically_call_remote(:update => "schremser_bier", :url => { :action => "mehr_bier" }) end - + def test_periodically_call_remote_with_frequency assert_dom_equal( "", periodically_call_remote(:frequency => 2) ) end - + def test_form_remote_tag assert_dom_equal %(
        ), form_remote_tag(:update => "glass_of_beer", :url => { :action => :fast }) @@ -117,21 +117,22 @@ def test_form_remote_tag_with_method form_remote_tag(:update => "glass_of_beer", :url => { :action => :fast }, :html => { :method => :put }) end - def test_form_remote_tag_with_block + def test_form_remote_tag_with_block_in_erb + __in_erb_template = '' form_remote_tag(:update => "glass_of_beer", :url => { :action => :fast }) { concat "Hello world!" } assert_dom_equal %(Hello world!
        ), output_buffer end def test_remote_form_for_with_record_identification_with_new_record remote_form_for(@record, {:html => { :id => 'create-author' }}) {} - + expected = %(
        ) assert_dom_equal expected, output_buffer end def test_remote_form_for_with_record_identification_without_html_options remote_form_for(@record) {} - + expected = %(
        ) assert_dom_equal expected, output_buffer end @@ -139,23 +140,23 @@ def test_remote_form_for_with_record_identification_without_html_options def test_remote_form_for_with_record_identification_with_existing_record @record.save remote_form_for(@record) {} - + expected = %(
        ) assert_dom_equal expected, output_buffer end def test_remote_form_for_with_new_object_in_list remote_form_for([@author, @article]) {} - + expected = %(
        ) assert_dom_equal expected, output_buffer end - + def test_remote_form_for_with_existing_object_in_list @author.save @article.save remote_form_for([@author, @article]) {} - + expected = %(
        ) assert_dom_equal expected, output_buffer end @@ -172,18 +173,18 @@ def test_on_callbacks assert_dom_equal %(
        ), form_remote_tag(:update => { :success => "glass_of_beer", :failure => "glass_of_water" }, :url => { :action => :fast }, callback=>"monkeys();") end - + #HTTP status codes 200 up to 599 have callbacks #these should work 100.upto(599) do |callback| assert_dom_equal %(), form_remote_tag(:update => "glass_of_beer", :url => { :action => :fast }, callback=>"monkeys();") end - + #test 200 and 404 assert_dom_equal %(), form_remote_tag(:update => "glass_of_beer", :url => { :action => :fast }, 200=>"monkeys();", 404=>"bananas();") - + #these shouldn't 1.upto(99) do |callback| assert_dom_equal %(), @@ -193,44 +194,44 @@ def test_on_callbacks assert_dom_equal %(), form_remote_tag(:update => "glass_of_beer", :url => { :action => :fast }, callback=>"monkeys();") end - + #test ultimate combo assert_dom_equal %(), form_remote_tag(:update => "glass_of_beer", :url => { :action => :fast }, :loading => "c1()", :success => "s()", :failure => "f();", :complete => "c();", 200=>"monkeys();", 404=>"bananas();") - + end - + def test_submit_to_remote assert_dom_equal %(), submit_to_remote("More beer!", 1_000_000, :update => "empty_bottle") end - + def test_observe_field assert_dom_equal %(), observe_field("glass", :frequency => 5.minutes, :url => { :action => "reorder_if_empty" }) end - + def test_observe_field_using_with_option expected = %() assert_dom_equal expected, observe_field("glass", :frequency => 5.minutes, :url => { :action => "check_value" }, :with => 'id') assert_dom_equal expected, observe_field("glass", :frequency => 5.minutes, :url => { :action => "check_value" }, :with => "'id=' + encodeURIComponent(value)") end - + def test_observe_field_using_json_in_with_option expected = %() - assert_dom_equal expected, observe_field("glass", :frequency => 5.minutes, :url => { :action => "check_value" }, :with => "{'id':value}") + assert_dom_equal expected, observe_field("glass", :frequency => 5.minutes, :url => { :action => "check_value" }, :with => "{'id':value}") end - + def test_observe_field_using_function_for_callback assert_dom_equal %(), observe_field("glass", :frequency => 5.minutes, :function => "alert('Element changed')") end - + def test_observe_form assert_dom_equal %(), observe_form("cart", :frequency => 2, :url => { :action => "cart_changed" }) end - + def test_observe_form_using_function_for_callback assert_dom_equal %(), observe_form("cart", :frequency => 2, :function => "alert('Form changed')") @@ -245,7 +246,7 @@ def test_update_page block = Proc.new { |page| page.replace_html('foo', 'bar') } assert_equal create_generator(&block).to_s, update_page(&block) end - + def test_update_page_tag block = Proc.new { |page| page.replace_html('foo', 'bar') } assert_equal javascript_tag(create_generator(&block).to_s), update_page_tag(&block) @@ -261,15 +262,15 @@ def test_update_page_tag_with_html_options def author_path(record) "/authors/#{record.id}" end - + def authors_path "/authors" end - + def author_articles_path(author) "/authors/#{author.id}/articles" end - + def author_article_path(author, article) "/authors/#{author.id}/articles/#{article.id}" end @@ -280,7 +281,7 @@ def setup super @generator = create_generator end - + def test_insert_html_with_string assert_equal 'new Insertion.Top("element", "\\u003Cp\\u003EThis is a test\\u003C/p\\u003E");', @generator.insert_html(:top, 'element', '

        This is a test

        ') @@ -291,56 +292,56 @@ def test_insert_html_with_string assert_equal 'new Insertion.After("element", "\\u003Cp\u003EThis is a test\\u003C/p\u003E");', @generator.insert_html(:after, 'element', '

        This is a test

        ') end - + def test_replace_html_with_string assert_equal 'Element.update("element", "\\u003Cp\\u003EThis is a test\\u003C/p\\u003E");', @generator.replace_html('element', '

        This is a test

        ') end - + def test_replace_element_with_string assert_equal 'Element.replace("element", "\\u003Cdiv id=\"element\"\\u003E\\u003Cp\\u003EThis is a test\\u003C/p\\u003E\\u003C/div\\u003E");', @generator.replace('element', '

        This is a test

        ') end - + def test_remove assert_equal 'Element.remove("foo");', @generator.remove('foo') assert_equal '["foo", "bar", "baz"].each(Element.remove);', @generator.remove('foo', 'bar', 'baz') end - + def test_show assert_equal 'Element.show("foo");', @generator.show('foo') assert_equal '["foo", "bar", "baz"].each(Element.show);', - @generator.show('foo', 'bar', 'baz') + @generator.show('foo', 'bar', 'baz') end - + def test_hide assert_equal 'Element.hide("foo");', @generator.hide('foo') assert_equal '["foo", "bar", "baz"].each(Element.hide);', - @generator.hide('foo', 'bar', 'baz') + @generator.hide('foo', 'bar', 'baz') end - + def test_toggle assert_equal 'Element.toggle("foo");', @generator.toggle('foo') assert_equal '["foo", "bar", "baz"].each(Element.toggle);', - @generator.toggle('foo', 'bar', 'baz') + @generator.toggle('foo', 'bar', 'baz') end - + def test_alert assert_equal 'alert("hello");', @generator.alert('hello') end - + def test_redirect_to assert_equal 'window.location.href = "http://www.example.com/welcome";', @generator.redirect_to(:action => 'welcome') assert_equal 'window.location.href = "http://www.example.com/welcome?a=b&c=d";', @generator.redirect_to("http://www.example.com/welcome?a=b&c=d") end - + def test_reload assert_equal 'window.location.reload();', @generator.reload @@ -350,16 +351,16 @@ def test_delay @generator.delay(20) do @generator.hide('foo') end - + assert_equal "setTimeout(function() {\n;\nElement.hide(\"foo\");\n}, 20000);", @generator.to_s end - + def test_to_s @generator.insert_html(:top, 'element', '

        This is a test

        ') @generator.insert_html(:bottom, 'element', '

        This is a test

        ') @generator.remove('foo', 'bar') @generator.replace_html('baz', '

        This is a test

        ') - + assert_equal <<-EOS.chomp, @generator.to_s new Insertion.Top("element", "\\u003Cp\\u003EThis is a test\\u003C/p\\u003E"); new Insertion.Bottom("element", "\\u003Cp\\u003EThis is a test\\u003C/p\\u003E"); @@ -381,12 +382,12 @@ def test_element_proxy_one_deep @generator['hello'].hide assert_equal %($("hello").hide();), @generator.to_s end - + def test_element_proxy_variable_access @generator['hello']['style'] assert_equal %($("hello").style;), @generator.to_s end - + def test_element_proxy_variable_access_with_assignment @generator['hello']['style']['color'] = 'red' assert_equal %($("hello").style.color = "red";), @generator.to_s @@ -401,7 +402,7 @@ def test_element_proxy_two_deep @generator['hello'].hide("first").clean_whitespace assert_equal %($("hello").hide("first").cleanWhitespace();), @generator.to_s end - + def test_select_access assert_equal %($$("div.hello");), @generator.select('div.hello') end @@ -410,29 +411,29 @@ def test_select_proxy_one_deep @generator.select('p.welcome b').first.hide assert_equal %($$("p.welcome b").first().hide();), @generator.to_s end - + def test_visual_effect - assert_equal %(new Effect.Puff("blah",{});), + assert_equal %(new Effect.Puff("blah",{});), @generator.visual_effect(:puff,'blah') - end - + end + def test_visual_effect_toggle - assert_equal %(Effect.toggle("blah",'appear',{});), + assert_equal %(Effect.toggle("blah",'appear',{});), @generator.visual_effect(:toggle_appear,'blah') end - + def test_sortable - assert_equal %(Sortable.create("blah", {onUpdate:function(){new Ajax.Request('http://www.example.com/order', {asynchronous:true, evalScripts:true, parameters:Sortable.serialize("blah")})}});), + assert_equal %(Sortable.create("blah", {onUpdate:function(){new Ajax.Request('http://www.example.com/order', {asynchronous:true, evalScripts:true, parameters:Sortable.serialize("blah")})}});), @generator.sortable('blah', :url => { :action => "order" }) end - + def test_draggable - assert_equal %(new Draggable("blah", {});), + assert_equal %(new Draggable("blah", {});), @generator.draggable('blah') end - + def test_drop_receiving - assert_equal %(Droppables.add("blah", {onDrop:function(element){new Ajax.Request('http://www.example.com/order', {asynchronous:true, evalScripts:true, parameters:'id=' + encodeURIComponent(element.id)})}});), + assert_equal %(Droppables.add("blah", {onDrop:function(element){new Ajax.Request('http://www.example.com/order', {asynchronous:true, evalScripts:true, parameters:'id=' + encodeURIComponent(element.id)})}});), @generator.drop_receiving('blah', :url => { :action => "order" }) end @@ -534,7 +535,7 @@ def test_collection_proxy_with_zip }); EOS end - + def test_collection_proxy_with_find_all @generator.select('p').find_all 'a' do |value, index| @generator << '(value.className == "welcome")' @@ -546,7 +547,7 @@ def test_collection_proxy_with_find_all }); EOS end - + def test_collection_proxy_with_in_groups_of @generator.select('p').in_groups_of('a', 3) @generator.select('p').in_groups_of('a', 3, 'x') @@ -555,13 +556,13 @@ def test_collection_proxy_with_in_groups_of var a = $$("p").inGroupsOf(3, "x"); EOS end - + def test_collection_proxy_with_each_slice @generator.select('p').each_slice('a', 3) @generator.select('p').each_slice('a', 3) do |group, index| group.reverse end - + assert_equal <<-EOS.strip, @generator.to_s var a = $$("p").eachSlice(3); var a = $$("p").eachSlice(3, function(value, index) { @@ -569,7 +570,7 @@ def test_collection_proxy_with_each_slice }); EOS end - + def test_debug_rjs ActionView::Base.debug_rjs = true @generator['welcome'].replace_html 'Welcome' @@ -577,7 +578,7 @@ def test_debug_rjs ensure ActionView::Base.debug_rjs = false end - + def test_literal literal = @generator.literal("function() {}") assert_equal "function() {}", literal.to_json @@ -588,7 +589,7 @@ def test_class_proxy @generator.form.focus('my_field') assert_equal "Form.focus(\"my_field\");", @generator.to_s end - + def test_call_with_block @generator.call(:before) @generator.call(:my_method) do |p| @@ -601,7 +602,7 @@ def test_call_with_block end assert_equal "before();\nmy_method(function() { $(\"one\").show();\n$(\"two\").hide(); });\nin_between();\nmy_method_with_arguments(true, \"hello\", function() { $(\"three\").visualEffect(\"highlight\"); });", @generator.to_s end - + def test_class_proxy_call_with_block @generator.my_object.my_method do |p| p[:one].show diff --git a/actionpack/test/template/record_tag_helper_test.rb b/actionpack/test/template/record_tag_helper_test.rb index 441dc6b7205b8..34a200b933164 100644 --- a/actionpack/test/template/record_tag_helper_test.rb +++ b/actionpack/test/template/record_tag_helper_test.rb @@ -2,7 +2,7 @@ class Post def id - 45 + 45 end def body "What a wonderful world!" @@ -15,35 +15,36 @@ class RecordTagHelperTest < ActionView::TestCase def setup @post = Post.new end - + def test_content_tag_for expected = %(
      • ) actual = content_tag_for(:li, @post, :class => 'bar') { } assert_dom_equal expected, actual end - + def test_content_tag_for_prefix expected = %(
          ) actual = content_tag_for(:ul, @post, :archived) { } - assert_dom_equal expected, actual + assert_dom_equal expected, actual end - + def test_content_tag_for_with_extra_html_tags expected = %() actual = content_tag_for(:tr, @post, {:class => "bar", :style => "background-color: #f0f0f0"}) { } - assert_dom_equal expected, actual + assert_dom_equal expected, actual end - - def test_block_works_with_content_tag_for + + def test_block_works_with_content_tag_for_in_erb + __in_erb_template = '' expected = %(#{@post.body}) actual = content_tag_for(:tr, @post) { concat @post.body } - assert_dom_equal expected, actual + assert_dom_equal expected, actual end - - def test_div_for + + def test_div_for_in_erb + __in_erb_template = '' expected = %(
          #{@post.body}
          ) actual = div_for(@post, :class => "bar") { concat @post.body } assert_dom_equal expected, actual - end - + end end diff --git a/actionpack/test/template/tag_helper_test.rb b/actionpack/test/template/tag_helper_test.rb index 2941dfe217770..fc49d340efbb6 100644 --- a/actionpack/test/template/tag_helper_test.rb +++ b/actionpack/test/template/tag_helper_test.rb @@ -33,23 +33,40 @@ def test_content_tag assert_equal content_tag("a", "Create", "href" => "create"), content_tag("a", "Create", :href => "create") end - - def test_content_tag_with_block + + def test_content_tag_with_block_in_erb + __in_erb_template = '' content_tag(:div) { concat "Hello world!" } assert_dom_equal "
          Hello world!
          ", output_buffer end - - def test_content_tag_with_block_and_options + + def test_content_tag_with_block_and_options_in_erb + __in_erb_template = '' content_tag(:div, :class => "green") { concat "Hello world!" } assert_dom_equal %(
          Hello world!
          ), output_buffer end - - def test_content_tag_with_block_and_options_outside_of_action_view - self.output_buffer = nil + + def test_content_tag_with_block_and_options_out_of_erb + assert_dom_equal %(
          Hello world!
          ), content_tag(:div, :class => "green") { "Hello world!" } + end + + def test_content_tag_with_block_and_options_outside_out_of_erb assert_equal content_tag("a", "Create", :href => "create"), - content_tag("a", "href" => "create") { "Create" } + content_tag("a", "href" => "create") { "Create" } end - + + def test_content_tag_nested_in_content_tag_out_of_erb + assert_equal content_tag("p", content_tag("b", "Hello")), + content_tag("p") { content_tag("b", "Hello") }, + output_buffer + end + + def test_content_tag_nested_in_content_tag_in_erb + __in_erb_template = true + content_tag("p") { concat content_tag("b", "Hello") } + assert_equal '

          Hello

          ', output_buffer + end + def test_cdata_section assert_equal "]]>", cdata_section("") end diff --git a/actionpack/test/template/text_helper_test.rb b/actionpack/test/template/text_helper_test.rb index cbb5c7ee7431b..df6be3bb13045 100644 --- a/actionpack/test/template/text_helper_test.rb +++ b/actionpack/test/template/text_helper_test.rb @@ -13,9 +13,7 @@ def setup def test_concat self.output_buffer = 'foo' - concat 'bar' - assert_equal 'foobar', output_buffer - assert_nothing_raised { concat nil } + assert_equal 'foobar', concat('bar') assert_equal 'foobar', output_buffer end diff --git a/actionpack/test/template/url_helper_test.rb b/actionpack/test/template/url_helper_test.rb index 0713cea8ac970..44ceed66611f5 100644 --- a/actionpack/test/template/url_helper_test.rb +++ b/actionpack/test/template/url_helper_test.rb @@ -80,7 +80,7 @@ def test_button_to_with_method_get def test_link_tag_with_straight_url assert_dom_equal "Hello", link_to("Hello", "http://www.example.com") end - + def test_link_tag_without_host_option ActionController::Base.class_eval { attr_accessor :url } url = {:controller => 'weblog', :action => 'show'} @@ -121,7 +121,7 @@ def test_link_tag_with_back @controller.request = RequestMock.new("http://www.example.com/weblog/show", nil, nil, {'HTTP_REFERER' => 'http://www.example.com/referer'}) assert_dom_equal "go back", link_to('go back', :back) end - + def test_link_tag_with_back_and_no_referer @controller.request = RequestMock.new("http://www.example.com/weblog/show", nil, nil, {}) assert_dom_equal "go back", link_to('go back', :back) @@ -212,14 +212,14 @@ def test_link_tag_using_post_javascript_and_popup assert_raises(ActionView::ActionViewError) { link_to("Hello", "http://www.example.com", :popup => true, :method => :post, :confirm => "Are you serious?") } end - def test_link_tag_using_block - self.output_buffer = '' + def test_link_tag_using_block_in_erb + __in_erb_template = '' link_to("http://example.com") { concat("Example site") } assert_equal 'Example site', output_buffer end - + def test_link_to_unless assert_equal "Showing", link_to_unless(true, "Showing", :action => "show", :controller => "weblog") assert_dom_equal "Listing", link_to_unless(false, "Listing", :action => "list", :controller => "weblog") @@ -293,7 +293,7 @@ def test_mail_to_with_replace_options assert_dom_equal "me(at)domain(dot)com", mail_to("me@domain.com", nil, :encode => "hex", :replace_at => "(at)", :replace_dot => "(dot)") assert_dom_equal "", mail_to("me@domain.com", "My email", :encode => "javascript", :replace_at => "(at)", :replace_dot => "(dot)") end - + def protect_against_forgery? false end @@ -420,11 +420,11 @@ class Workshop def initialize(id, new_record) @id, @new_record = id, new_record end - + def new_record? @new_record end - + def to_s id.to_s end From f47c81ff37afb5e5340b6f2cb47a5bb76b94f5c0 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Fri, 20 Jun 2008 00:25:41 -0700 Subject: [PATCH 170/200] Fall back to #to_s for cache key expansion --- activesupport/lib/active_support/cache.rb | 4 +++- activesupport/test/caching_test.rb | 6 ++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/activesupport/lib/active_support/cache.rb b/activesupport/lib/active_support/cache.rb index 2f1143e61052c..b8ccd3575367f 100644 --- a/activesupport/lib/active_support/cache.rb +++ b/activesupport/lib/active_support/cache.rb @@ -19,7 +19,7 @@ def self.lookup_store(*store_option) def self.expand_cache_key(key, namespace = nil) expanded_cache_key = namespace ? "#{namespace}/" : "" - + if ENV["RAILS_CACHE_ID"] || ENV["RAILS_APP_VERSION"] expanded_cache_key << "#{ENV["RAILS_CACHE_ID"] || ENV["RAILS_APP_VERSION"]}/" end @@ -31,6 +31,8 @@ def self.expand_cache_key(key, namespace = nil) key.collect { |element| expand_cache_key(element) }.to_param when key.respond_to?(:to_param) key.to_param + else + key.to_s end expanded_cache_key diff --git a/activesupport/test/caching_test.rb b/activesupport/test/caching_test.rb index 09b56525e0d87..f3220d27aad5a 100644 --- a/activesupport/test/caching_test.rb +++ b/activesupport/test/caching_test.rb @@ -1,5 +1,11 @@ require 'abstract_unit' +class CacheKeyTest < Test::Unit::TestCase + def test_expand_cache_key + assert_equal 'name/1/2/true', ActiveSupport::Cache.expand_cache_key([1, '2', true], :name) + end +end + class CacheStoreSettingTest < Test::Unit::TestCase def test_file_fragment_cache_store store = ActiveSupport::Cache.lookup_store :file_store, "/path/to/cache/directory" From 83c3e9903d2ad76ef4aea1d64d6514443f6ad438 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Fri, 20 Jun 2008 00:35:11 -0700 Subject: [PATCH 171/200] Don't profile GC runs/time by default --- activesupport/lib/active_support/testing/performance.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/activesupport/lib/active_support/testing/performance.rb b/activesupport/lib/active_support/testing/performance.rb index 23171714674ed..cadcb155c1ec0 100644 --- a/activesupport/lib/active_support/testing/performance.rb +++ b/activesupport/lib/active_support/testing/performance.rb @@ -18,7 +18,7 @@ module Performance { :benchmark => false, :runs => 1, :min_percent => 0.02, - :metrics => [:process_time, :memory, :objects, :gc_runs, :gc_time], + :metrics => [:process_time, :memory, :objects], :formats => [:flat, :graph_html, :call_tree], :output => 'tmp/performance' } end From 879245de1ce294e8951927f0e694333746772187 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Fri, 20 Jun 2008 00:36:59 -0700 Subject: [PATCH 172/200] Tired of seeing /run --- activesupport/lib/active_support/testing/performance.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/activesupport/lib/active_support/testing/performance.rb b/activesupport/lib/active_support/testing/performance.rb index cadcb155c1ec0..1f8a5eec8f1a6 100644 --- a/activesupport/lib/active_support/testing/performance.rb +++ b/activesupport/lib/active_support/testing/performance.rb @@ -99,7 +99,7 @@ def initialize(harness, metric) def report rate = @total / profile_options[:runs] - '%20s: %s/run' % [@metric.name, @metric.format(rate)] + '%20s: %s' % [@metric.name, @metric.format(rate)] end protected From 00ba4c0cf32f9417d47bd891eba97f2e04609520 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Fri, 20 Jun 2008 00:46:00 -0700 Subject: [PATCH 173/200] true#to_param => true, so be sure to #to_s expanded cache keys --- activesupport/lib/active_support/cache.rb | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/activesupport/lib/active_support/cache.rb b/activesupport/lib/active_support/cache.rb index b8ccd3575367f..27beff9815514 100644 --- a/activesupport/lib/active_support/cache.rb +++ b/activesupport/lib/active_support/cache.rb @@ -25,15 +25,15 @@ def self.expand_cache_key(key, namespace = nil) end expanded_cache_key << case - when key.respond_to?(:cache_key) - key.cache_key - when key.is_a?(Array) - key.collect { |element| expand_cache_key(element) }.to_param - when key.respond_to?(:to_param) - key.to_param - else - key.to_s - end + when key.respond_to?(:cache_key) + key.cache_key + when key.is_a?(Array) + key.collect { |element| expand_cache_key(element) }.to_param + when key.respond_to?(:to_param) + key.to_param + else + key + end.to_s expanded_cache_key end From 161ab28b7c0bd28828b6d18613a9de09d47023c6 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Fri, 20 Jun 2008 11:31:06 -0500 Subject: [PATCH 174/200] Added block-handling to Enumerable#many? (Damian Janowski) [#452 state:resolved] --- activesupport/CHANGELOG | 2 +- activesupport/lib/active_support/core_ext/enumerable.rb | 4 +++- activesupport/test/core_ext/enumerable_test.rb | 9 +++++++-- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG index d87558aba93c4..4f61311d951e4 100644 --- a/activesupport/CHANGELOG +++ b/activesupport/CHANGELOG @@ -6,7 +6,7 @@ * Added Object#present? which is equivalent to !Object#blank? [DHH] -* Added Enumberable#many? to encapsulate collection.size > 1 [DHH] +* Added Enumberable#many? to encapsulate collection.size > 1 [DHH/Damian Janowski] * Add more standard Hash methods to ActiveSupport::OrderedHash [Steve Purcell] diff --git a/activesupport/lib/active_support/core_ext/enumerable.rb b/activesupport/lib/active_support/core_ext/enumerable.rb index 9647797ec2110..e451e9933afc8 100644 --- a/activesupport/lib/active_support/core_ext/enumerable.rb +++ b/activesupport/lib/active_support/core_ext/enumerable.rb @@ -79,7 +79,9 @@ def index_by end # Returns true if the collection has more than 1 element. Functionally equivalent to collection.size > 1. - def many? + # Works with a block too ala any?, so people.many? { |p| p.age > 26 } # => returns true if more than 1 person is over 26. + def many?(&block) + size = block_given? ? select(&block).size : self.size size > 1 end end diff --git a/activesupport/test/core_ext/enumerable_test.rb b/activesupport/test/core_ext/enumerable_test.rb index fb9b9b8916f76..2315d8f3db438 100644 --- a/activesupport/test/core_ext/enumerable_test.rb +++ b/activesupport/test/core_ext/enumerable_test.rb @@ -63,10 +63,15 @@ def test_index_by assert_equal({ 5 => payments[0], 15 => payments[1], 10 => payments[2] }, payments.index_by { |p| p.price }) end - - def test_several + + def test_many assert ![].many? assert ![ 1 ].many? assert [ 1, 2 ].many? + + assert ![].many? {|x| x > 1 } + assert ![ 2 ].many? {|x| x > 1 } + assert ![ 1, 2 ].many? {|x| x > 1 } + assert [ 1, 2, 2 ].many? {|x| x > 1 } end end From 8d24a029df47fa9ad36f0e027d3cd447f92af344 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Fri, 20 Jun 2008 10:39:44 -0700 Subject: [PATCH 175/200] Don't assume RubyProf constants are defined --- .../lib/active_support/testing/performance.rb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/activesupport/lib/active_support/testing/performance.rb b/activesupport/lib/active_support/testing/performance.rb index 1f8a5eec8f1a6..5f2027eb3b32a 100644 --- a/activesupport/lib/active_support/testing/performance.rb +++ b/activesupport/lib/active_support/testing/performance.rb @@ -288,7 +288,7 @@ def measure end class CpuTime < Time - Mode = RubyProf::CPU_TIME + Mode = RubyProf::CPU_TIME if RubyProf.const_defined?(:CPU_TIME) def initialize(*args) # FIXME: yeah my CPU is 2.33 GHz @@ -302,7 +302,7 @@ def measure end class Memory < Base - Mode = RubyProf::MEMORY + Mode = RubyProf::MEMORY if RubyProf.const_defined?(:MEMORY) # ruby-prof wrapper if RubyProf.respond_to?(:measure_memory) @@ -335,7 +335,7 @@ def format(measurement) end class Objects < Base - Mode = RubyProf::ALLOCATIONS + Mode = RubyProf::ALLOCATIONS if RubyProf.const_defined?(:ALLOCATIONS) if RubyProf.respond_to?(:measure_allocations) def measure @@ -353,7 +353,7 @@ def format(measurement) end class GcRuns < Base - Mode = RubyProf::GC_RUNS + Mode = RubyProf::GC_RUNS if RubyProf.const_defined?(:GC_RUNS) if RubyProf.respond_to?(:measure_gc_runs) def measure @@ -375,7 +375,7 @@ def format(measurement) end class GcTime < Base - Mode = RubyProf::GC_TIME + Mode = RubyProf::GC_TIME if RubyProf.const_defined?(:GC_TIME) if RubyProf.respond_to?(:measure_gc_time) def measure From 9a0e4437199a233105348938e490808fc0688626 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Sat, 21 Jun 2008 04:19:30 -0700 Subject: [PATCH 176/200] Everything responds to #to_param --- activesupport/lib/active_support/cache.rb | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/activesupport/lib/active_support/cache.rb b/activesupport/lib/active_support/cache.rb index 27beff9815514..3e3dc1826374a 100644 --- a/activesupport/lib/active_support/cache.rb +++ b/activesupport/lib/active_support/cache.rb @@ -29,10 +29,8 @@ def self.expand_cache_key(key, namespace = nil) key.cache_key when key.is_a?(Array) key.collect { |element| expand_cache_key(element) }.to_param - when key.respond_to?(:to_param) + when key key.to_param - else - key end.to_s expanded_cache_key From f4ccc179530d5b9436da87d3c221dfa8fa89119a Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Sat, 21 Jun 2008 14:54:10 -0700 Subject: [PATCH 177/200] Performance: javascript helper tweaks to speed up escaping and reduce object allocations when building options strings --- .../action_view/helpers/javascript_helper.rb | 129 ++++++++++-------- .../lib/action_view/helpers/tag_helper.rb | 2 +- .../lib/action_view/helpers/url_helper.rb | 2 +- 3 files changed, 73 insertions(+), 60 deletions(-) diff --git a/actionpack/lib/action_view/helpers/javascript_helper.rb b/actionpack/lib/action_view/helpers/javascript_helper.rb index 7404a251e492d..31571de6c4e46 100644 --- a/actionpack/lib/action_view/helpers/javascript_helper.rb +++ b/actionpack/lib/action_view/helpers/javascript_helper.rb @@ -4,10 +4,10 @@ module ActionView module Helpers # Provides functionality for working with JavaScript in your views. - # + # # == Ajax, controls and visual effects - # - # * For information on using Ajax, see + # + # * For information on using Ajax, see # ActionView::Helpers::PrototypeHelper. # * For information on using controls and visual effects, see # ActionView::Helpers::ScriptaculousHelper. @@ -20,22 +20,22 @@ module Helpers # and ActionView::Helpers::ScriptaculousHelper), you must do one of the # following: # - # * Use <%= javascript_include_tag :defaults %> in the HEAD - # section of your page (recommended): This function will return + # * Use <%= javascript_include_tag :defaults %> in the HEAD + # section of your page (recommended): This function will return # references to the JavaScript files created by the +rails+ command in # your public/javascripts directory. Using it is recommended as - # the browser can then cache the libraries instead of fetching all the + # the browser can then cache the libraries instead of fetching all the # functions anew on every request. - # * Use <%= javascript_include_tag 'prototype' %>: As above, but + # * Use <%= javascript_include_tag 'prototype' %>: As above, but # will only include the Prototype core library, which means you are able - # to use all basic AJAX functionality. For the Scriptaculous-based - # JavaScript helpers, like visual effects, autocompletion, drag and drop + # to use all basic AJAX functionality. For the Scriptaculous-based + # JavaScript helpers, like visual effects, autocompletion, drag and drop # and so on, you should use the method described above. # * Use <%= define_javascript_functions %>: this will copy all the # JavaScript support functions within a single script block. Not # recommended. # - # For documentation on +javascript_include_tag+ see + # For documentation on +javascript_include_tag+ see # ActionView::Helpers::AssetTagHelper. module JavaScriptHelper unless const_defined? :JAVASCRIPT_PATH @@ -43,13 +43,13 @@ module JavaScriptHelper end include PrototypeHelper - - # Returns a link that will trigger a JavaScript +function+ using the + + # Returns a link that will trigger a JavaScript +function+ using the # onclick handler and return false after the fact. # # The +function+ argument can be omitted in favor of an +update_page+ # block, which evaluates to a string when the template is rendered - # (instead of making an Ajax request first). + # (instead of making an Ajax request first). # # Examples: # link_to_function "Greeting", "alert('Hello world!')" @@ -70,36 +70,31 @@ module JavaScriptHelper # Show me more # def link_to_function(name, *args, &block) - html_options = args.extract_options! - function = args[0] || '' - - html_options.symbolize_keys! - function = update_page(&block) if block_given? - content_tag( - "a", name, - html_options.merge({ - :href => html_options[:href] || "#", - :onclick => (html_options[:onclick] ? "#{html_options[:onclick]}; " : "") + "#{function}; return false;" - }) - ) + html_options = args.extract_options!.symbolize_keys! + + function = block_given? ? update_page(&block) : args[0] || '' + onclick = "#{"#{html_options[:onclick]}; " if html_options[:onclick]}#{function}; return false;" + href = html_options[:href] || '#' + + content_tag(:a, name, html_options.merge(:href => href, :onclick => onclick)) end - - # Returns a button that'll trigger a JavaScript +function+ using the + + # Returns a button that'll trigger a JavaScript +function+ using the # onclick handler. # # The +function+ argument can be omitted in favor of an +update_page+ # block, which evaluates to a string when the template is rendered - # (instead of making an Ajax request first). + # (instead of making an Ajax request first). # # Examples: # button_to_function "Greeting", "alert('Hello world!')" @@ -111,45 +106,56 @@ def link_to_function(name, *args, &block) # page[:details].visual_effect :toggle_slide # end def button_to_function(name, *args, &block) - html_options = args.extract_options! - function = args[0] || '' - - html_options.symbolize_keys! - function = update_page(&block) if block_given? - tag(:input, html_options.merge({ - :type => "button", :value => name, - :onclick => (html_options[:onclick] ? "#{html_options[:onclick]}; " : "") + "#{function};" - })) + html_options = args.extract_options!.symbolize_keys! + + function = block_given? ? update_page(&block) : args[0] || '' + onclick = "#{"#{html_options[:onclick]}; " if html_options[:onclick]}#{function};" + + tag(:input, html_options.merge(:type => 'button', :value => name, :onclick => onclick)) end - # Includes the Action Pack JavaScript libraries inside a single ' end + + JS_ESCAPE_MAP = { + '\\' => '\\\\', + ' '<\/', + "\r\n" => '\n', + "\n" => '\n', + "\r" => '\n', + '"' => '\\"', + "'" => "\\'" } + # Escape carrier returns and single and double quotes for JavaScript segments. def escape_javascript(javascript) - (javascript || '').gsub('\\','\0\0').gsub(' # # +html_options+ may be a hash of attributes for the # # Instead of passing the content as an argument, you can also use a block @@ -180,30 +186,37 @@ def javascript_tag(content_or_options_with_block = nil, html_options = {}, &bloc content_or_options_with_block end - tag = content_tag("script", javascript_cdata_section(content), html_options.merge(:type => Mime::JS)) + tag = content_tag(:script, javascript_cdata_section(content), html_options.merge(:type => Mime::JS)) - block_given? ? concat(tag) : tag + if block_called_from_erb?(block) + concat(tag) + else + tag + end end def javascript_cdata_section(content) #:nodoc: "\n//#{cdata_section("\n#{content}\n//")}\n" end - + protected def options_for_javascript(options) - '{' + options.map {|k, v| "#{k}:#{v}"}.sort.join(', ') + '}' + if options.empty? + '{}' + else + "{#{options.keys.map { |k| "#{k}:#{options[k]}" }.sort.join(', ')}}" + end end - + def array_or_string_for_javascript(option) - js_option = if option.kind_of?(Array) + if option.kind_of?(Array) "['#{option.join('\',\'')}']" elsif !option.nil? "'#{option}'" end - js_option end end - + JavascriptHelper = JavaScriptHelper unless const_defined? :JavascriptHelper end end diff --git a/actionpack/lib/action_view/helpers/tag_helper.rb b/actionpack/lib/action_view/helpers/tag_helper.rb index 649ec87a2486d..aeafd3906d3c8 100644 --- a/actionpack/lib/action_view/helpers/tag_helper.rb +++ b/actionpack/lib/action_view/helpers/tag_helper.rb @@ -115,7 +115,7 @@ def escape_once(html) # can't take an <% end %> later on, so we have to use <% ... %> # and implicitly concat. def block_called_from_erb?(block) - eval(BLOCK_CALLED_FROM_ERB, block) + block && eval(BLOCK_CALLED_FROM_ERB, block) end def content_tag_string(name, content, options, escape = true) diff --git a/actionpack/lib/action_view/helpers/url_helper.rb b/actionpack/lib/action_view/helpers/url_helper.rb index baecd304cd7dd..d5b72556424fe 100644 --- a/actionpack/lib/action_view/helpers/url_helper.rb +++ b/actionpack/lib/action_view/helpers/url_helper.rb @@ -535,7 +535,7 @@ def convert_options_to_javascript!(html_options, url = '') when method "#{method_javascript_function(method, url, href)}return false;" when popup - popup_javascript_function(popup) + 'return false;' + "#{popup_javascript_function(popup)}return false;" else html_options["onclick"] end From a02d672cd7aead8a24e3b10a6b8e12dd91ee0a49 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Sun, 22 Jun 2008 10:38:25 -0700 Subject: [PATCH 178/200] Horo rdoc template --- .gitignore | 2 +- Rakefile | 14 +- actionmailer/Rakefile | 3 +- actionpack/Rakefile | 2 +- activemodel/Rakefile | 4 +- activerecord/Rakefile | 2 +- activeresource/Rakefile | 2 +- activesupport/Rakefile | 2 +- doc/template/horo.rb | 613 ++++++++++++++++++++++++++++++++++++++++ railties/Rakefile | 2 +- 10 files changed, 630 insertions(+), 16 deletions(-) create mode 100644 doc/template/horo.rb diff --git a/.gitignore b/.gitignore index 96a1c2117ab2c..bba7d5dce838d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,5 @@ debug.log -doc +doc/rdoc activeresource/doc activerecord/doc actionpack/doc diff --git a/Rakefile b/Rakefile index 368f36e56ccef..352fd632d98dc 100644 --- a/Rakefile +++ b/Rakefile @@ -13,7 +13,7 @@ end desc 'Run all tests by default' task :default => :test -%w(test rdoc package release).each do |task_name| +%w(test rdoc pgem package release).each do |task_name| desc "Run #{task_name} task for all projects" task task_name do PROJECTS.each do |project| @@ -25,14 +25,14 @@ end desc "Generate documentation for the Rails framework" Rake::RDocTask.new do |rdoc| - rdoc.rdoc_dir = 'doc' - rdoc.title = "Rails Framework Documentation" + rdoc.rdoc_dir = 'doc/rdoc' + rdoc.title = "Ruby on Rails Documentation" rdoc.options << '--line-numbers' << '--inline-source' rdoc.options << '-A cattr_accessor=object' rdoc.options << '--charset' << 'utf-8' - rdoc.template = "#{ENV['template']}.rb" if ENV['template'] + rdoc.template = ENV['template'] ? "#{ENV['template']}.rb" : './doc/template/horo' rdoc.rdoc_files.include('railties/CHANGELOG') rdoc.rdoc_files.include('railties/MIT-LICENSE') @@ -68,13 +68,13 @@ end # Enhance rdoc task to copy referenced images also task :rdoc do - FileUtils.mkdir_p "doc/files/examples/" - FileUtils.copy "activerecord/examples/associations.png", "doc/files/examples/associations.png" + FileUtils.mkdir_p "doc/rdoc/files/examples/" + FileUtils.copy "activerecord/examples/associations.png", "doc/rdoc/files/examples/associations.png" end desc "Publish API docs for Rails as a whole and for each component" task :pdoc => :rdoc do - Rake::SshDirPublisher.new("wrath.rubyonrails.org", "public_html/api", "doc").upload + Rake::SshDirPublisher.new("wrath.rubyonrails.org", "public_html/api", "doc/rdoc").upload PROJECTS.each do |project| system %(cd #{project} && #{env} #{$0} pdoc) end diff --git a/actionmailer/Rakefile b/actionmailer/Rakefile index b003f4d559798..612bd277741a9 100755 --- a/actionmailer/Rakefile +++ b/actionmailer/Rakefile @@ -35,7 +35,7 @@ Rake::RDocTask.new { |rdoc| rdoc.title = "Action Mailer -- Easy email delivery and testing" rdoc.options << '--line-numbers' << '--inline-source' << '-A cattr_accessor=object' rdoc.options << '--charset' << 'utf-8' - rdoc.template = "#{ENV['template']}.rb" if ENV['template'] + rdoc.template = ENV['template'] ? "#{ENV['template']}.rb" : '../doc/template/horo' rdoc.rdoc_files.include('README', 'CHANGELOG') rdoc.rdoc_files.include('lib/action_mailer.rb') rdoc.rdoc_files.include('lib/action_mailer/*.rb') @@ -77,6 +77,7 @@ end desc "Publish the API documentation" task :pgem => [:package] do Rake::SshFilePublisher.new("wrath.rubyonrails.org", "public_html/gems/gems", "pkg", "#{PKG_FILE_NAME}.gem").upload + `ssh wrath.rubyonrails.org './gemupdate.sh'` end desc "Publish the API documentation" diff --git a/actionpack/Rakefile b/actionpack/Rakefile index 86a2f3af14d2a..1880f21f67ba6 100644 --- a/actionpack/Rakefile +++ b/actionpack/Rakefile @@ -49,7 +49,7 @@ Rake::RDocTask.new { |rdoc| rdoc.title = "Action Pack -- On rails from request to response" rdoc.options << '--line-numbers' << '--inline-source' rdoc.options << '--charset' << 'utf-8' - rdoc.template = "#{ENV['template']}.rb" if ENV['template'] + rdoc.template = ENV['template'] ? "#{ENV['template']}.rb" : '../doc/template/horo' if ENV['DOC_FILES'] rdoc.rdoc_files.include(ENV['DOC_FILES'].split(/,\s*/)) else diff --git a/activemodel/Rakefile b/activemodel/Rakefile index 87e9b547f3f50..99c9fc3bd1276 100644 --- a/activemodel/Rakefile +++ b/activemodel/Rakefile @@ -10,7 +10,7 @@ Rake::RDocTask.new { |rdoc| rdoc.title = "Active Model" rdoc.options << '--line-numbers' << '--inline-source' << '-A cattr_accessor=object' rdoc.options << '--charset' << 'utf-8' - rdoc.template = "#{ENV['template']}.rb" if ENV['template'] + rdoc.template = ENV['template'] ? "#{ENV['template']}.rb" : '../doc/template/horo' rdoc.rdoc_files.include('README', 'CHANGES') rdoc.rdoc_files.include('lib/**/*.rb') -} \ No newline at end of file +} diff --git a/activerecord/Rakefile b/activerecord/Rakefile index 7a5296c458030..60b17e02b9347 100755 --- a/activerecord/Rakefile +++ b/activerecord/Rakefile @@ -141,7 +141,7 @@ Rake::RDocTask.new { |rdoc| rdoc.title = "Active Record -- Object-relation mapping put on rails" rdoc.options << '--line-numbers' << '--inline-source' << '-A cattr_accessor=object' rdoc.options << '--charset' << 'utf-8' - rdoc.template = "#{ENV['template']}.rb" if ENV['template'] + rdoc.template = ENV['template'] ? "#{ENV['template']}.rb" : '../doc/template/horo' rdoc.rdoc_files.include('README', 'RUNNING_UNIT_TESTS', 'CHANGELOG') rdoc.rdoc_files.include('lib/**/*.rb') rdoc.rdoc_files.exclude('lib/active_record/vendor/*') diff --git a/activeresource/Rakefile b/activeresource/Rakefile index 89901292a5d9f..8c3ad36a02555 100644 --- a/activeresource/Rakefile +++ b/activeresource/Rakefile @@ -42,7 +42,7 @@ Rake::RDocTask.new { |rdoc| rdoc.title = "Active Resource -- Object-oriented REST services" rdoc.options << '--line-numbers' << '--inline-source' << '-A cattr_accessor=object' rdoc.options << '--charset' << 'utf-8' - rdoc.template = "#{ENV['template']}.rb" if ENV['template'] + rdoc.template = ENV['template'] ? "#{ENV['template']}.rb" : '../doc/template/horo' rdoc.rdoc_files.include('README', 'CHANGELOG') rdoc.rdoc_files.include('lib/**/*.rb') rdoc.rdoc_files.exclude('lib/activeresource.rb') diff --git a/activesupport/Rakefile b/activesupport/Rakefile index 74d2750011286..f2885c69a457f 100644 --- a/activesupport/Rakefile +++ b/activesupport/Rakefile @@ -33,7 +33,7 @@ Rake::RDocTask.new { |rdoc| rdoc.title = "Active Support -- Utility classes and standard library extensions from Rails" rdoc.options << '--line-numbers' << '--inline-source' rdoc.options << '--charset' << 'utf-8' - rdoc.template = "#{ENV['template']}.rb" if ENV['template'] + rdoc.template = ENV['template'] ? "#{ENV['template']}.rb" : '../doc/template/horo' rdoc.rdoc_files.include('README', 'CHANGELOG') rdoc.rdoc_files.include('lib/active_support.rb') rdoc.rdoc_files.include('lib/active_support/**/*.rb') diff --git a/doc/template/horo.rb b/doc/template/horo.rb new file mode 100644 index 0000000000000..e028422a2e090 --- /dev/null +++ b/doc/template/horo.rb @@ -0,0 +1,613 @@ +# Horo RDoc template +# Author: Hongli Lai - http://izumi.plan99.net/blog/ +# +# Based on the Jamis template: +# http://weblog.jamisbuck.org/2005/4/8/rdoc-template + +if defined?(RDoc::Diagram) + RDoc::Diagram.class_eval do + remove_const(:FONT) + const_set(:FONT, "\"Bitstream Vera Sans\"") + end +end + +module RDoc +module Page + +FONTS = "\"Bitstream Vera Sans\", Verdana, Arial, Helvetica, sans-serif" + +STYLE = < pre { + padding: 0.5em; + border: 1px dotted black; + background: #FFE; +} + +dt { + font-weight: bold +} + +dd { + margin-bottom: 0.7em; +} +CSS + +XHTML_PREAMBLE = %{ + +} + +XHTML_FRAMESET_PREAMBLE = %{ + +} + +HEADER = XHTML_PREAMBLE + < + + %title% + + + + + + + +ENDHEADER + +FILE_PAGE = < + + + + +
          File
          %short_name%
          + + + + + + + + + +
          Path:%full_path% +IF:cvsurl +  (CVS) +ENDIF:cvsurl +
          Modified:%dtm_modified%
          +
          + +
          +HTML + +################################################################### + +CLASS_PAGE = < + %classmod%
          %full_name% + + + + + + +IF:parent + + + + +ENDIF:parent +
          In: +START:infiles +HREF:full_path_url:full_path: +IF:cvsurl + (CVS) +ENDIF:cvsurl +END:infiles +
          Parent: +IF:par_url + +ENDIF:par_url +%parent% +IF:par_url + +ENDIF:par_url +
          + + + +HTML + +################################################################### + +METHOD_LIST = < +IF:diagram +
          + %diagram% +
          +ENDIF:diagram + +IF:description +
          %description%
          +ENDIF:description + +IF:requires +
          Required Files
          +
            +START:requires +
          • HREF:aref:name:
          • +END:requires +
          +ENDIF:requires + +IF:toc +
          Contents
          + +ENDIF:toc + +IF:methods +
          Methods
          +
            +START:methods +
          • HREF:aref:name:
          • +END:methods +
          +ENDIF:methods + +IF:includes +
          Included Modules
          +
            +START:includes +
          • HREF:aref:name:
          • +END:includes +
          +ENDIF:includes + +START:sections +IF:sectitle + +IF:seccomment +
          +%seccomment% +
          +ENDIF:seccomment +ENDIF:sectitle + +IF:classlist +
          Classes and Modules
          + %classlist% +ENDIF:classlist + +IF:constants +
          Constants
          + +START:constants + + + + + +IF:desc + + + + +ENDIF:desc +END:constants +
          %name%=%value%
           %desc%
          +ENDIF:constants + +IF:attributes +
          Attributes
          + +START:attributes + + + + + +END:attributes +
          +IF:rw +[%rw%] +ENDIF:rw + %name%%a_desc%
          +ENDIF:attributes + +IF:method_list +START:method_list +IF:methods +
          %type% %category% methods
          +START:methods +
          +
          +IF:callseq + %callseq% +ENDIF:callseq +IFNOT:callseq + %name%%params% +ENDIF:callseq +IF:codeurl +[ source ] +ENDIF:codeurl +
          +IF:m_desc +
          + %m_desc% +
          +ENDIF:m_desc +IF:aka +
          + This method is also aliased as +START:aka + %name% +END:aka +
          +ENDIF:aka +IF:sourcecode +
          + +
          +
          +%sourcecode%
          +
          +
          +
          +ENDIF:sourcecode +
          +END:methods +ENDIF:methods +END:method_list +ENDIF:method_list +END:sections + +HTML + +FOOTER = < + +ENDFOOTER + +BODY = HEADER + < + +
          + #{METHOD_LIST} +
          + + #{FOOTER} +ENDBODY + +########################## Source code ########################## + +SRC_PAGE = XHTML_PREAMBLE + < +%title% + + + + +
          %code%
          + + +HTML + +########################## Index ################################ + +FR_INDEX_BODY = < + + +Index + + + + + +
          +START:entries +%name%
          +END:entries +
          + +HTML + +CLASS_INDEX = FILE_INDEX +METHOD_INDEX = FILE_INDEX + +INDEX = XHTML_FRAMESET_PREAMBLE + < + + %title% + + + + + + + + + + + + <body bgcolor="white"> + Click <a href="html/index.html">here</a> for a non-frames + version of this page. + </body> + + + + +HTML + +end +end + diff --git a/railties/Rakefile b/railties/Rakefile index 78fe8e6662d87..f64b60b0dddeb 100644 --- a/railties/Rakefile +++ b/railties/Rakefile @@ -264,7 +264,7 @@ Rake::RDocTask.new { |rdoc| rdoc.title = "Railties -- Gluing the Engine to the Rails" rdoc.options << '--line-numbers' << '--inline-source' << '--accessor' << 'cattr_accessor=object' rdoc.options << '--charset' << 'utf-8' - rdoc.template = "#{ENV['template']}.rb" if ENV['template'] + rdoc.template = ENV['template'] ? "#{ENV['template']}.rb" : '../doc/template/horo' rdoc.rdoc_files.include('README', 'CHANGELOG') rdoc.rdoc_files.include('lib/*.rb') rdoc.rdoc_files.include('lib/rails/*.rb') From 2e1b56c93745bf0513e449e95830edd390abfaf2 Mon Sep 17 00:00:00 2001 From: Diego Algorta Date: Sat, 21 Jun 2008 20:18:30 -0300 Subject: [PATCH 179/200] MySQL: rename_column preserves default values. [#466 state:resolved] --- activerecord/CHANGELOG | 2 ++ .../connection_adapters/mysql_adapter.rb | 10 ++++++- activerecord/test/cases/migration_test.rb | 26 +++++++++++++++++++ 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index a1d82fb45dbbc..e2d67c7ff504e 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,5 +1,7 @@ *Edge* +* MySQL: rename_column preserves column defaults. #466 [Diego Algorta] + * Add :from option to calculations. #397 [Ben Munat] * Add :validate option to associations to enable/disable the automatic validation of associated models. Resolves #301. [Jan De Poorter] diff --git a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb index 93aafaaad1da6..8805c79c5bd62 100755 --- a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb @@ -450,8 +450,16 @@ def change_column(table_name, column_name, type, options = {}) #:nodoc: end def rename_column(table_name, column_name, new_column_name) #:nodoc: + options = {} + if column = columns(table_name).find { |c| c.name == column_name.to_s } + options[:default] = column.default + else + raise ActiveRecordError, "No such column: #{table_name}.#{column_name}" + end current_type = select_one("SHOW COLUMNS FROM #{quote_table_name(table_name)} LIKE '#{column_name}'")["Type"] - execute "ALTER TABLE #{quote_table_name(table_name)} CHANGE #{quote_column_name(column_name)} #{quote_column_name(new_column_name)} #{current_type}" + rename_column_sql = "ALTER TABLE #{quote_table_name(table_name)} CHANGE #{quote_column_name(column_name)} #{quote_column_name(new_column_name)} #{current_type}" + add_column_options!(rename_column_sql, options) + execute(rename_column_sql) end # Maps logical Rails types to MySQL-specific data types. diff --git a/activerecord/test/cases/migration_test.rb b/activerecord/test/cases/migration_test.rb index f36255e2090c1..2cac7f04ab71d 100644 --- a/activerecord/test/cases/migration_test.rb +++ b/activerecord/test/cases/migration_test.rb @@ -483,6 +483,32 @@ def test_rename_column end end + def test_rename_column_preserves_default_value_not_null + begin + default_before = Developer.connection.columns("developers").find { |c| c.name == "salary" }.default + assert_equal 70000, default_before + Developer.connection.rename_column "developers", "salary", "anual_salary" + Developer.reset_column_information + assert Developer.column_names.include?("anual_salary") + default_after = Developer.connection.columns("developers").find { |c| c.name == "anual_salary" }.default + assert_equal 70000, default_after + ensure + Developer.connection.rename_column "developers", "anual_salary", "salary" + Developer.reset_column_information + end + end + + def test_rename_nonexistent_column + ActiveRecord::Base.connection.create_table(:hats) do |table| + table.column :hat_name, :string, :default => nil + end + assert_raises(ActiveRecord::ActiveRecordError) do + Person.connection.rename_column "hats", "nonexistent", "should_fail" + end + ensure + ActiveRecord::Base.connection.drop_table(:hats) + end + def test_rename_column_with_sql_reserved_word begin assert_nothing_raised { Person.connection.rename_column "people", "first_name", "group" } From 509374ebe2dba0dc2ea7e44d67a6f8d530d12fac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tarmo=20T=C3=A4nav?= Date: Mon, 12 May 2008 17:58:03 +0300 Subject: [PATCH 180/200] Named bind variables can now be used with postgresql-style typecasts For example :conditions => ['stringcol::integer = :var', { :var => 10 }] will no longer raise an exception about ':integer' having a missing value. --- activerecord/lib/active_record/base.rb | 7 ++++--- activerecord/test/cases/finder_test.rb | 7 +++++++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 8fca34e52457c..8d5ea271a7b1e 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -2055,9 +2055,10 @@ def replace_bind_variables(statement, values) #:nodoc: end def replace_named_bind_variables(statement, bind_vars) #:nodoc: - statement.gsub(/:([a-zA-Z]\w*)/) do - match = $1.to_sym - if bind_vars.include?(match) + statement.gsub(/(:?):([a-zA-Z]\w*)/) do + if $1 == ':' # skip postgresql casts + $& # return the whole match + elsif bind_vars.include?(match = $2.to_sym) quote_bound_value(bind_vars[match]) else raise PreparedStatementInvalid, "missing value for :#{match} in #{statement}" diff --git a/activerecord/test/cases/finder_test.rb b/activerecord/test/cases/finder_test.rb index 80936d51f3217..f48b62ba6bfbb 100644 --- a/activerecord/test/cases/finder_test.rb +++ b/activerecord/test/cases/finder_test.rb @@ -1,5 +1,6 @@ require "cases/helper" require 'models/author' +require 'models/categorization' require 'models/comment' require 'models/company' require 'models/topic' @@ -394,6 +395,12 @@ def test_bind_record assert_equal '1,1,1', bind('?', os) end + def test_named_bind_with_postgresql_type_casts + l = Proc.new { bind(":a::integer '2009-01-01'::date", :a => '10') } + assert_nothing_raised(&l) + assert_equal "#{ActiveRecord::Base.quote_value('10')}::integer '2009-01-01'::date", l.call + end + def test_string_sanitation assert_not_equal "#{ActiveRecord::Base.connection.quoted_string_prefix}'something ' 1=1'", ActiveRecord::Base.sanitize("something ' 1=1") assert_equal "#{ActiveRecord::Base.connection.quoted_string_prefix}'something; select table'", ActiveRecord::Base.sanitize("something; select table") From a73ac9812383a887089462df443d5e89ef087f58 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Sun, 22 Jun 2008 16:21:08 -0700 Subject: [PATCH 181/200] Changelog for 509374e --- activerecord/CHANGELOG | 3 +++ 1 file changed, 3 insertions(+) diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index e2d67c7ff504e..d1f3f4c7c7f9e 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,5 +1,8 @@ *Edge* +* PostgreSQL: support :conditions => [':foo::integer', { :foo => 1 }] without +treating the ::integer typecast as a bind variable. [Tarmo Tänav] + * MySQL: rename_column preserves column defaults. #466 [Diego Algorta] * Add :from option to calculations. #397 [Ben Munat] From 43cbcb10ae85adc4403e950e69ee14123a20d8ae Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Sun, 22 Jun 2008 16:22:23 -0700 Subject: [PATCH 182/200] nix extra newline --- activerecord/CHANGELOG | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index d1f3f4c7c7f9e..df62f3fbf78b4 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,7 +1,6 @@ *Edge* -* PostgreSQL: support :conditions => [':foo::integer', { :foo => 1 }] without -treating the ::integer typecast as a bind variable. [Tarmo Tänav] +* PostgreSQL: support :conditions => [':foo::integer', { :foo => 1 }] without treating the ::integer typecast as a bind variable. [Tarmo Tänav] * MySQL: rename_column preserves column defaults. #466 [Diego Algorta] From 1afae84ab2656cd58a861ab4a4b1745d80088d0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tarmo=20T=C3=A4nav?= Date: Fri, 13 Jun 2008 23:39:10 +0300 Subject: [PATCH 183/200] Fixed that scopes defined with a string name could not be composed --- activerecord/lib/active_record/named_scope.rb | 1 + activerecord/test/cases/named_scope_test.rb | 6 ++++++ activerecord/test/models/topic.rb | 1 + 3 files changed, 8 insertions(+) diff --git a/activerecord/lib/active_record/named_scope.rb b/activerecord/lib/active_record/named_scope.rb index b0c8a8b815d3a..eac61e9e43ede 100644 --- a/activerecord/lib/active_record/named_scope.rb +++ b/activerecord/lib/active_record/named_scope.rb @@ -82,6 +82,7 @@ def scopes # expected_options = { :conditions => { :colored => 'red' } } # assert_equal expected_options, Shirt.colored('red').proxy_options def named_scope(name, options = {}, &block) + name = name.to_sym scopes[name] = lambda do |parent_scope, *args| Scope.new(parent_scope, case options when Hash diff --git a/activerecord/test/cases/named_scope_test.rb b/activerecord/test/cases/named_scope_test.rb index d890cf79369e9..73673e0f0ed5b 100644 --- a/activerecord/test/cases/named_scope_test.rb +++ b/activerecord/test/cases/named_scope_test.rb @@ -59,6 +59,12 @@ def test_scopes_with_options_limit_finds_to_those_matching_the_criteria_specifie assert_equal Topic.count(:conditions => {:approved => true}), Topic.approved.count end + def test_scopes_with_string_name_can_be_composed + # NOTE that scopes defined with a string as a name worked on their own + # but when called on another scope the other scope was completely replaced + assert_equal Topic.replied.approved, Topic.replied.approved_as_string + end + def test_scopes_are_composable assert_equal (approved = Topic.find(:all, :conditions => {:approved => true})), Topic.approved assert_equal (replied = Topic.find(:all, :conditions => 'replies_count > 0')), Topic.replied diff --git a/activerecord/test/models/topic.rb b/activerecord/test/models/topic.rb index f63e862cfda1e..423b6fe52b474 100755 --- a/activerecord/test/models/topic.rb +++ b/activerecord/test/models/topic.rb @@ -4,6 +4,7 @@ class Topic < ActiveRecord::Base { :conditions => ['written_on < ?', time] } } named_scope :approved, :conditions => {:approved => true} + named_scope 'approved_as_string', :conditions => {:approved => true} named_scope :replied, :conditions => ['replies_count > 0'] named_scope :anonymous_extension do def one From f94600bdaf7d73971ad9a53148b0844c2efb901d Mon Sep 17 00:00:00 2001 From: Michael Raidel Date: Fri, 13 Jun 2008 16:14:07 +0200 Subject: [PATCH 184/200] ActiveRecord::Migrator#run records version-state after migrating. [#369 state:resolved] --- activerecord/CHANGELOG | 2 ++ activerecord/lib/active_record/migration.rb | 5 ++++- activerecord/test/cases/migration_test.rb | 25 ++++++++++++--------- 3 files changed, 21 insertions(+), 11 deletions(-) diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index df62f3fbf78b4..0ac5ce960c275 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,5 +1,7 @@ *Edge* +* db:migrate:down and :up update schema_migrations. #369 [Michael Raidel, RaceCondition] + * PostgreSQL: support :conditions => [':foo::integer', { :foo => 1 }] without treating the ::integer typecast as a bind variable. [Tarmo Tänav] * MySQL: rename_column preserves column defaults. #466 [Diego Algorta] diff --git a/activerecord/lib/active_record/migration.rb b/activerecord/lib/active_record/migration.rb index b47b01e99a070..e095b3c76674a 100644 --- a/activerecord/lib/active_record/migration.rb +++ b/activerecord/lib/active_record/migration.rb @@ -399,7 +399,10 @@ def current_migration def run target = migrations.detect { |m| m.version == @target_version } raise UnknownMigrationVersionError.new(@target_version) if target.nil? - target.migrate(@direction) + unless (up? && migrated.include?(target.version.to_i)) || (down? && !migrated.include?(target.version.to_i)) + target.migrate(@direction) + record_version_state_after_migrating(target.version) + end end def migrate diff --git a/activerecord/test/cases/migration_test.rb b/activerecord/test/cases/migration_test.rb index 2cac7f04ab71d..fe7fa7ba03b7d 100644 --- a/activerecord/test/cases/migration_test.rb +++ b/activerecord/test/cases/migration_test.rb @@ -825,6 +825,21 @@ def test_migrator_one_up_one_down assert !Reminder.table_exists? end + def test_migrator_double_up + assert_equal(0, ActiveRecord::Migrator.current_version) + ActiveRecord::Migrator.run(:up, MIGRATIONS_ROOT + "/valid", 1) + assert_nothing_raised { ActiveRecord::Migrator.run(:up, MIGRATIONS_ROOT + "/valid", 1) } + assert_equal(1, ActiveRecord::Migrator.current_version) + end + + def test_migrator_double_down + assert_equal(0, ActiveRecord::Migrator.current_version) + ActiveRecord::Migrator.run(:up, MIGRATIONS_ROOT + "/valid", 1) + ActiveRecord::Migrator.run(:down, MIGRATIONS_ROOT + "/valid", 1) + assert_nothing_raised { ActiveRecord::Migrator.run(:down, MIGRATIONS_ROOT + "/valid", 1) } + assert_equal(0, ActiveRecord::Migrator.current_version) + end + def test_finds_migrations migrations = ActiveRecord::Migrator.new(:up, MIGRATIONS_ROOT + "/valid").migrations [['1', 'people_have_last_names'], @@ -914,16 +929,6 @@ def test_migrator_rollback ActiveRecord::Migrator.rollback(MIGRATIONS_ROOT + "/valid") assert_equal(0, ActiveRecord::Migrator.current_version) end - - def test_migrator_run - assert_equal(0, ActiveRecord::Migrator.current_version) - ActiveRecord::Migrator.run(:up, MIGRATIONS_ROOT + "/valid", 3) - assert_equal(0, ActiveRecord::Migrator.current_version) - - assert_equal(0, ActiveRecord::Migrator.current_version) - ActiveRecord::Migrator.run(:down, MIGRATIONS_ROOT + "/valid", 3) - assert_equal(0, ActiveRecord::Migrator.current_version) - end def test_schema_migrations_table_name ActiveRecord::Base.table_name_prefix = "prefix_" From 3532eaf92a96b87fb422625142ff0efa1cac17ab Mon Sep 17 00:00:00 2001 From: ian Date: Mon, 16 Jun 2008 17:45:50 -0500 Subject: [PATCH 185/200] Only use DROP ... IF EXISTS for PostgreSQL 8.2 or later. [#400 state:resolved] --- .../connection_adapters/postgresql_adapter.rb | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb index 294f4c1929e46..d0585d52ac44f 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb @@ -553,7 +553,15 @@ def create_database(name, options = {}) # Example: # drop_database 'matt_development' def drop_database(name) #:nodoc: - execute "DROP DATABASE IF EXISTS #{quote_table_name(name)}" + if postgresql_version >= 80200 + execute "DROP DATABASE IF EXISTS #{quote_table_name(name)}" + else + begin + execute "DROP DATABASE #{quote_table_name(name)}" + rescue ActiveRecord::StatementInvalid + @logger.warn "#{name} database doesn't exist." if @logger + end + end end From a210f503619dd27dc9575bcae1df596fd2387563 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Sun, 22 Jun 2008 18:50:19 -0700 Subject: [PATCH 186/200] Oops, already had a postgresql_version method! --- .../connection_adapters/postgresql_adapter.rb | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb index d0585d52ac44f..5641b5cca50bd 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb @@ -324,12 +324,7 @@ def supports_standard_conforming_strings? end def supports_insert_with_returning? - unless defined? @supports_insert_with_returning - @supports_insert_with_returning = - @connection.respond_to?(:server_version) && - @connection.server_version >= 80200 - end - @supports_insert_with_returning + postgresql_version >= 80200 end # Returns the configured supported identifier length supported by PostgreSQL, From bb6e8eea5a8190aaab67da0a7efedb3bb3d9fccb Mon Sep 17 00:00:00 2001 From: Tammer Saleh Date: Fri, 20 Jun 2008 15:21:04 -0400 Subject: [PATCH 187/200] Fixed polymorphic_url to be able to handle singleton resources. Example usage: polymorphic_url([:admin, @user, :blog, @post]) # => admin_user_blog_post_url(@user, @post) [#461 state:resolved] --- actionpack/CHANGELOG | 2 + .../action_controller/polymorphic_routes.rb | 37 +++++++++++++------ .../controller/polymorphic_routes_test.rb | 33 +++++++++++++++++ 3 files changed, 60 insertions(+), 12 deletions(-) diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG index 4a6e70a153def..a28bf66b8823b 100644 --- a/actionpack/CHANGELOG +++ b/actionpack/CHANGELOG @@ -1,5 +1,7 @@ *Edge* +* Fix polymorphic_url with singleton resources. #461 [Tammer Saleh] + * Replaced TemplateFinder abstraction with ViewLoadPaths [Josh Peek] * Added block-call style to link_to [Sam Stephenson/DHH]. Example: diff --git a/actionpack/lib/action_controller/polymorphic_routes.rb b/actionpack/lib/action_controller/polymorphic_routes.rb index 509fa6a08e920..7c30bf0778493 100644 --- a/actionpack/lib/action_controller/polymorphic_routes.rb +++ b/actionpack/lib/action_controller/polymorphic_routes.rb @@ -48,6 +48,9 @@ module PolymorphicRoutes # # # calls post_url(post) # polymorphic_url(post) # => "http://example.com/posts/1" + # polymorphic_url([blog, post]) # => "http://example.com/blogs/1/posts/1" + # polymorphic_url([:admin, blog, post]) # => "http://example.com/admin/blogs/1/posts/1" + # polymorphic_url([user, :blog, post]) # => "http://example.com/users/1/blog/posts/1" # # ==== Options # @@ -83,8 +86,6 @@ def polymorphic_url(record_or_hash_or_array, options = {}) else [ record_or_hash_or_array ] end - args << format if format - inflection = case when options[:action].to_s == "new" @@ -96,6 +97,9 @@ def polymorphic_url(record_or_hash_or_array, options = {}) else :singular end + + args.delete_if {|arg| arg.is_a?(Symbol) || arg.is_a?(String)} + args << format if format named_route = build_named_route_call(record_or_hash_or_array, namespace, inflection, options) send!(named_route, *args) @@ -136,11 +140,19 @@ def build_named_route_call(records, namespace, inflection, options = {}) else record = records.pop route = records.inject("") do |string, parent| - string << "#{RecordIdentifier.send!("singular_class_name", parent)}_" + if parent.is_a?(Symbol) || parent.is_a?(String) + string << "#{parent}_" + else + string << "#{RecordIdentifier.send!("singular_class_name", parent)}_" + end end end - route << "#{RecordIdentifier.send!("#{inflection}_class_name", record)}_" + if record.is_a?(Symbol) || record.is_a?(String) + route << "#{record}_" + else + route << "#{RecordIdentifier.send!("#{inflection}_class_name", record)}_" + end action_prefix(options) + namespace + route + routing_type(options).to_s end @@ -163,16 +175,17 @@ def extract_format(record_or_hash_or_array, options) end end + # Remove the first symbols from the array and return the url prefix + # implied by those symbols. def extract_namespace(record_or_hash_or_array) - returning "" do |namespace| - if record_or_hash_or_array.is_a?(Array) - record_or_hash_or_array.delete_if do |record_or_namespace| - if record_or_namespace.is_a?(String) || record_or_namespace.is_a?(Symbol) - namespace << "#{record_or_namespace}_" - end - end - end + return "" unless record_or_hash_or_array.is_a?(Array) + + namespace_keys = [] + while (key = record_or_hash_or_array.first) && key.is_a?(String) || key.is_a?(Symbol) + namespace_keys << record_or_hash_or_array.shift end + + namespace_keys.map {|k| "#{k}_"}.join end end end diff --git a/actionpack/test/controller/polymorphic_routes_test.rb b/actionpack/test/controller/polymorphic_routes_test.rb index 4ec0d3cd4e4c2..3f52526f08415 100644 --- a/actionpack/test/controller/polymorphic_routes_test.rb +++ b/actionpack/test/controller/polymorphic_routes_test.rb @@ -118,6 +118,39 @@ def test_nested_with_array_and_namespace polymorphic_url([:site, :admin, @article, @response, @tag]) end + def test_nesting_with_array_ending_in_singleton_resource + expects(:article_response_url).with(@article) + polymorphic_url([@article, :response]) + end + + def test_nesting_with_array_containing_singleton_resource + @tag = Tag.new + @tag.save + expects(:article_response_tag_url).with(@article, @tag) + polymorphic_url([@article, :response, @tag]) + end + + def test_nesting_with_array_containing_namespace_and_singleton_resource + @tag = Tag.new + @tag.save + expects(:admin_article_response_tag_url).with(@article, @tag) + polymorphic_url([:admin, @article, :response, @tag]) + end + + def test_nesting_with_array_containing_singleton_resource_and_format + @tag = Tag.new + @tag.save + expects(:formatted_article_response_tag_url).with(@article, @tag, :pdf) + formatted_polymorphic_url([@article, :response, @tag, :pdf]) + end + + def test_nesting_with_array_containing_singleton_resource_and_format_option + @tag = Tag.new + @tag.save + expects(:article_response_tag_url).with(@article, @tag, :pdf) + polymorphic_url([@article, :response, @tag], :format => :pdf) + end + # TODO: Needs to be updated to correctly know about whether the object is in a hash or not def xtest_with_hash expects(:article_url).with(@article) From 0fd3e4cd2b2b1b31304a922dc65284d5363f78b6 Mon Sep 17 00:00:00 2001 From: Mark Catley Date: Sat, 21 Jun 2008 23:41:30 +1200 Subject: [PATCH 188/200] Fix column collision with named_scope and :joins. [#46 state:resolved] --- activerecord/CHANGELOG | 2 ++ activerecord/lib/active_record/base.rb | 2 +- .../test/cases/method_scoping_test.rb | 10 +++++++++ activerecord/test/cases/named_scope_test.rb | 21 ++++++++++++++++++- activerecord/test/models/post.rb | 7 ++++++- 5 files changed, 39 insertions(+), 3 deletions(-) diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index 0ac5ce960c275..aaa7c3a0d106d 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,5 +1,7 @@ *Edge* +* Fix column collision with named_scope and :joins. #46 [Duncan Beevers, Mark Catley] + * db:migrate:down and :up update schema_migrations. #369 [Michael Raidel, RaceCondition] * PostgreSQL: support :conditions => [':foo::integer', { :foo => 1 }] without treating the ::integer typecast as a bind variable. [Tarmo Tänav] diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 8d5ea271a7b1e..e6ab87e8fc983 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -1479,7 +1479,7 @@ def type_name_with_module(type_name) def construct_finder_sql(options) scope = scope(:find) - sql = "SELECT #{options[:select] || (scope && scope[:select]) || (options[:joins] && quoted_table_name + '.*') || '*'} " + sql = "SELECT #{options[:select] || (scope && scope[:select]) || ((options[:joins] || (scope && scope[:joins])) && quoted_table_name + '.*') || '*'} " sql << "FROM #{(scope && scope[:from]) || options[:from] || quoted_table_name} " add_joins!(sql, options, scope) diff --git a/activerecord/test/cases/method_scoping_test.rb b/activerecord/test/cases/method_scoping_test.rb index 1a9a87573052e..d6b3e341dfca7 100644 --- a/activerecord/test/cases/method_scoping_test.rb +++ b/activerecord/test/cases/method_scoping_test.rb @@ -87,6 +87,16 @@ def test_scoped_find_include assert_equal 1, scoped_developers.size end + def test_scoped_find_joins + scoped_developers = Developer.with_scope(:find => { :joins => 'JOIN developers_projects ON id = developer_id' } ) do + Developer.find(:all, :conditions => 'developers_projects.project_id = 2') + end + assert scoped_developers.include?(developers(:david)) + assert !scoped_developers.include?(developers(:jamis)) + assert_equal 1, scoped_developers.size + assert_equal developers(:david).attributes, scoped_developers.first.attributes + end + def test_scoped_count_include # with the include, will retrieve only developers for the given project Developer.with_scope(:find => { :include => :projects }) do diff --git a/activerecord/test/cases/named_scope_test.rb b/activerecord/test/cases/named_scope_test.rb index 73673e0f0ed5b..393ba086c9f34 100644 --- a/activerecord/test/cases/named_scope_test.rb +++ b/activerecord/test/cases/named_scope_test.rb @@ -6,7 +6,7 @@ require 'models/author' class NamedScopeTest < ActiveRecord::TestCase - fixtures :posts, :authors, :topics, :comments + fixtures :posts, :authors, :topics, :comments, :author_addresses def test_implements_enumerable assert !Topic.find(:all).empty? @@ -83,6 +83,25 @@ def test_procedural_scopes assert_equal topics_written_before_the_second, Topic.written_before(topics(:second).written_on) end + def test_scopes_with_joins + address = author_addresses(:david_address) + posts_with_authors_at_address = Post.find( + :all, :joins => 'JOIN authors ON authors.id = posts.author_id', + :conditions => [ 'authors.author_address_id = ?', address.id ] + ) + assert_equal posts_with_authors_at_address, Post.with_authors_at_address(address) + end + + def test_scopes_with_joins_respects_custom_select + address = author_addresses(:david_address) + posts_with_authors_at_address_titles = Post.find(:all, + :select => 'title', + :joins => 'JOIN authors ON authors.id = posts.author_id', + :conditions => [ 'authors.author_address_id = ?', address.id ] + ) + assert_equal posts_with_authors_at_address_titles, Post.with_authors_at_address(address).find(:all, :select => 'title') + end + def test_extensions assert_equal 1, Topic.anonymous_extension.one assert_equal 2, Topic.named_extension.two diff --git a/activerecord/test/models/post.rb b/activerecord/test/models/post.rb index d9101706b5c04..3adbc0ce1f078 100644 --- a/activerecord/test/models/post.rb +++ b/activerecord/test/models/post.rb @@ -1,6 +1,11 @@ class Post < ActiveRecord::Base named_scope :containing_the_letter_a, :conditions => "body LIKE '%a%'" - + named_scope :with_authors_at_address, lambda { |address| { + :conditions => [ 'authors.author_address_id = ?', address.id ], + :joins => 'JOIN authors ON authors.id = posts.author_id' + } + } + belongs_to :author do def greeting "hello" From 3610997ba32128921115bedb89c322a7bcbe161a Mon Sep 17 00:00:00 2001 From: Daniel Morrison Date: Sun, 15 Jun 2008 16:25:59 -0400 Subject: [PATCH 189/200] Partial updates don't update lock_version if nothing changed. [#426 state:resolved] --- activerecord/CHANGELOG | 2 ++ .../lib/active_record/locking/optimistic.rb | 1 + activerecord/test/cases/dirty_test.rb | 19 ++++++++++++++++++ activerecord/test/cases/locking_test.rb | 20 +++++++++++++++++++ 4 files changed, 42 insertions(+) diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index aaa7c3a0d106d..306e17a8dec39 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,5 +1,7 @@ *Edge* +* Partial updates don't update lock_version if nothing changed. #426 [Daniel Morrison] + * Fix column collision with named_scope and :joins. #46 [Duncan Beevers, Mark Catley] * db:migrate:down and :up update schema_migrations. #369 [Michael Raidel, RaceCondition] diff --git a/activerecord/lib/active_record/locking/optimistic.rb b/activerecord/lib/active_record/locking/optimistic.rb index c66034d18bc0c..ff9899d032612 100644 --- a/activerecord/lib/active_record/locking/optimistic.rb +++ b/activerecord/lib/active_record/locking/optimistic.rb @@ -68,6 +68,7 @@ def attributes_from_column_definition_with_lock def update_with_lock(attribute_names = @attributes.keys) #:nodoc: return update_without_lock(attribute_names) unless locking_enabled? + return 0 if attribute_names.empty? lock_col = self.class.locking_column previous_value = send(lock_col).to_i diff --git a/activerecord/test/cases/dirty_test.rb b/activerecord/test/cases/dirty_test.rb index c011ffaf575ab..d70a787208f47 100644 --- a/activerecord/test/cases/dirty_test.rb +++ b/activerecord/test/cases/dirty_test.rb @@ -2,6 +2,7 @@ require 'models/topic' # For booleans require 'models/pirate' # For timestamps require 'models/parrot' +require 'models/person' # For optimistic locking class Pirate # Just reopening it, not defining it attr_accessor :detected_changes_in_after_update # Boolean for if changes are detected @@ -125,6 +126,24 @@ def test_partial_update end end + def test_partial_update_with_optimistic_locking + person = Person.new(:first_name => 'foo') + old_lock_version = 1 + + with_partial_updates Person, false do + assert_queries(2) { 2.times { person.save! } } + Person.update_all({ :first_name => 'baz' }, :id => person.id) + end + + with_partial_updates Person, true do + assert_queries(0) { 2.times { person.save! } } + assert_equal old_lock_version, person.reload.lock_version + + assert_queries(1) { person.first_name = 'bar'; person.save! } + assert_not_equal old_lock_version, person.reload.lock_version + end + end + def test_changed_attributes_should_be_preserved_if_save_failure pirate = Pirate.new pirate.parrot_id = 1 diff --git a/activerecord/test/cases/locking_test.rb b/activerecord/test/cases/locking_test.rb index 7db6c570b5c63..701187223f697 100644 --- a/activerecord/test/cases/locking_test.rb +++ b/activerecord/test/cases/locking_test.rb @@ -29,10 +29,12 @@ def test_lock_existing assert_equal 0, p1.lock_version assert_equal 0, p2.lock_version + p1.first_name = 'stu' p1.save! assert_equal 1, p1.lock_version assert_equal 0, p2.lock_version + p2.first_name = 'sue' assert_raises(ActiveRecord::StaleObjectError) { p2.save! } end @@ -42,11 +44,14 @@ def test_lock_repeating assert_equal 0, p1.lock_version assert_equal 0, p2.lock_version + p1.first_name = 'stu' p1.save! assert_equal 1, p1.lock_version assert_equal 0, p2.lock_version + p2.first_name = 'sue' assert_raises(ActiveRecord::StaleObjectError) { p2.save! } + p2.first_name = 'sue2' assert_raises(ActiveRecord::StaleObjectError) { p2.save! } end @@ -54,15 +59,18 @@ def test_lock_new p1 = Person.new(:first_name => 'anika') assert_equal 0, p1.lock_version + p1.first_name = 'anika2' p1.save! p2 = Person.find(p1.id) assert_equal 0, p1.lock_version assert_equal 0, p2.lock_version + p1.first_name = 'anika3' p1.save! assert_equal 1, p1.lock_version assert_equal 0, p2.lock_version + p2.first_name = 'sue' assert_raises(ActiveRecord::StaleObjectError) { p2.save! } end @@ -81,10 +89,12 @@ def test_lock_column_name_existing assert_equal 0, t1.version assert_equal 0, t2.version + t1.tps_report_number = 700 t1.save! assert_equal 1, t1.version assert_equal 0, t2.version + t2.tps_report_number = 800 assert_raises(ActiveRecord::StaleObjectError) { t2.save! } end @@ -93,6 +103,7 @@ def test_lock_column_is_mass_assignable assert_equal 0, p1.lock_version assert_equal p1.lock_version, Person.new(p1.attributes).lock_version + p1.first_name = 'bianca2' p1.save! assert_equal 1, p1.lock_version assert_equal p1.lock_version, Person.new(p1.attributes).lock_version @@ -146,6 +157,15 @@ def test_quote_table_name assert ref.save end + # Useful for partial updates, don't only update the lock_version if there + # is nothing else being updated. + def test_update_without_attributes_does_not_only_update_lock_version + assert_nothing_raised do + p1 = Person.new(:first_name => 'anika') + p1.send(:update_with_lock, []) + end + end + private def add_counter_column_to(model) From baddea95e183b3bef290ec433f917ee8cd806934 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tarmo=20T=C3=A4nav?= Date: Sun, 15 Jun 2008 05:55:56 +0300 Subject: [PATCH 190/200] Always treat integer :limit as byte length. [#420 state:resolved] --- activerecord/CHANGELOG | 2 + .../connection_adapters/mysql_adapter.rb | 21 +++++---- .../connection_adapters/postgresql_adapter.rb | 16 ++++--- activerecord/test/cases/migration_test.rb | 5 ++ activerecord/test/cases/schema_dumper_test.rb | 46 +++++++++++++++++++ activerecord/test/schema/schema.rb | 7 +++ 6 files changed, 81 insertions(+), 16 deletions(-) diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index 306e17a8dec39..4e05d0fcfd6dc 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,5 +1,7 @@ *Edge* +* Always treat integer :limit as byte length. #420 [Tarmo Tänav] + * Partial updates don't update lock_version if nothing changed. #426 [Daniel Morrison] * Fix column collision with named_scope and :joins. #46 [Duncan Beevers, Mark Catley] diff --git a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb index 8805c79c5bd62..ed1f08ac4f306 100755 --- a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb @@ -99,7 +99,8 @@ def simplified_type(field_type) end def extract_limit(sql_type) - if sql_type =~ /blob|text/i + case sql_type + when /blob|text/i case sql_type when /tiny/i 255 @@ -110,6 +111,10 @@ def extract_limit(sql_type) else super # we could return 65535 here, but we leave it undecorated by default end + when /^int/i; 4 + when /^bigint/i; 8 + when /^smallint/i; 2 + when /^mediumint/i; 3 else super end @@ -168,7 +173,7 @@ class MysqlAdapter < AbstractAdapter :primary_key => "int(11) DEFAULT NULL auto_increment PRIMARY KEY".freeze, :string => { :name => "varchar", :limit => 255 }, :text => { :name => "text" }, - :integer => { :name => "int"}, + :integer => { :name => "int", :limit => 4 }, :float => { :name => "float" }, :decimal => { :name => "decimal" }, :datetime => { :name => "datetime" }, @@ -467,14 +472,10 @@ def type_to_sql(type, limit = nil, precision = nil, scale = nil) return super unless type.to_s == 'integer' case limit - when 0..3 - "smallint(#{limit})" - when 4..8 - "int(#{limit})" - when 9..20 - "bigint(#{limit})" - else - 'int(11)' + when 1..2; 'smallint' + when 3; 'mediumint' + when 4, nil; 'int(11)' + when 5..8; 'bigint' end end diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb index 5641b5cca50bd..361d1779677fe 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb @@ -47,6 +47,12 @@ def initialize(name, default, sql_type = nil, null = true) end private + def extract_limit(sql_type) + return 8 if sql_type =~ /^bigint/i + return 2 if sql_type =~ /^smallint/i + super + end + # Extracts the scale from PostgreSQL-specific data types. def extract_scale(sql_type) # Money type has a fixed scale of 2. @@ -785,12 +791,10 @@ def remove_index(table_name, options = {}) def type_to_sql(type, limit = nil, precision = nil, scale = nil) return super unless type.to_s == 'integer' - if limit.nil? || limit == 4 - 'integer' - elsif limit < 4 - 'smallint' - else - 'bigint' + case limit + when 1..2; 'smallint' + when 3..4, nil; 'integer' + when 5..8; 'bigint' end end diff --git a/activerecord/test/cases/migration_test.rb b/activerecord/test/cases/migration_test.rb index fe7fa7ba03b7d..4735f136dfb10 100644 --- a/activerecord/test/cases/migration_test.rb +++ b/activerecord/test/cases/migration_test.rb @@ -173,6 +173,11 @@ def test_create_table_with_limits assert_equal 'smallint', one.sql_type assert_equal 'integer', four.sql_type assert_equal 'bigint', eight.sql_type + elsif current_adapter?(:MysqlAdapter) + assert_match /^int\(\d+\)/, default.sql_type + assert_match /^smallint\(\d+\)/, one.sql_type + assert_match /^int\(\d+\)/, four.sql_type + assert_match /^bigint\(\d+\)/, eight.sql_type elsif current_adapter?(:OracleAdapter) assert_equal 'NUMBER(38)', default.sql_type assert_equal 'NUMBER(1)', one.sql_type diff --git a/activerecord/test/cases/schema_dumper_test.rb b/activerecord/test/cases/schema_dumper_test.rb index c42b0efba05ce..942d686b92140 100644 --- a/activerecord/test/cases/schema_dumper_test.rb +++ b/activerecord/test/cases/schema_dumper_test.rb @@ -72,6 +72,52 @@ def test_schema_dump_includes_not_null_columns assert_match %r{:null => false}, output end + def test_schema_dump_includes_limit_constraint_for_integer_columns + stream = StringIO.new + + ActiveRecord::SchemaDumper.ignore_tables = [/^(?!integer_limits)/] + ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, stream) + output = stream.string + + if current_adapter?(:PostgreSQLAdapter) + assert_match %r{c_int_1.*:limit => 2}, output + assert_match %r{c_int_2.*:limit => 2}, output + + # int 3 is 4 bytes in postgresql + assert_match %r{c_int_3.*}, output + assert_no_match %r{c_int_3.*:limit}, output + + assert_match %r{c_int_4.*}, output + assert_no_match %r{c_int_4.*:limit}, output + elsif current_adapter?(:MysqlAdapter) + assert_match %r{c_int_1.*:limit => 2}, output + assert_match %r{c_int_2.*:limit => 2}, output + assert_match %r{c_int_3.*:limit => 3}, output + + assert_match %r{c_int_4.*}, output + assert_no_match %r{c_int_4.*:limit}, output + elsif current_adapter?(:SQLiteAdapter) + assert_match %r{c_int_1.*:limit => 1}, output + assert_match %r{c_int_2.*:limit => 2}, output + assert_match %r{c_int_3.*:limit => 3}, output + assert_match %r{c_int_4.*:limit => 4}, output + end + assert_match %r{c_int_without_limit.*}, output + assert_no_match %r{c_int_without_limit.*:limit}, output + + if current_adapter?(:SQLiteAdapter) + assert_match %r{c_int_5.*:limit => 5}, output + assert_match %r{c_int_6.*:limit => 6}, output + assert_match %r{c_int_7.*:limit => 7}, output + assert_match %r{c_int_8.*:limit => 8}, output + else + assert_match %r{c_int_5.*:limit => 8}, output + assert_match %r{c_int_6.*:limit => 8}, output + assert_match %r{c_int_7.*:limit => 8}, output + assert_match %r{c_int_8.*:limit => 8}, output + end + end + def test_schema_dump_with_string_ignored_table stream = StringIO.new diff --git a/activerecord/test/schema/schema.rb b/activerecord/test/schema/schema.rb index 423929fd55d37..234c43494a1cf 100644 --- a/activerecord/test/schema/schema.rb +++ b/activerecord/test/schema/schema.rb @@ -407,6 +407,13 @@ def create_table(*args, &block) t.column :key, :string end + create_table :integer_limits, :force => true do |t| + t.integer :"c_int_without_limit" + (1..8).each do |i| + t.integer :"c_int_#{i}", :limit => i + end + end + except 'SQLite' do # fk_test_has_fk should be before fk_test_has_pk create_table :fk_test_has_fk, :force => true do |t| From 4d5ac3f3d2e1667971752c9a4e536de1a5bec364 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Sun, 22 Jun 2008 20:52:29 -0700 Subject: [PATCH 191/200] Check for mocha gem without requiring the lib. [#403 state:resolved] --- .../lib/active_support/testing/setup_and_teardown.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/activesupport/lib/active_support/testing/setup_and_teardown.rb b/activesupport/lib/active_support/testing/setup_and_teardown.rb index 21d71eb92a576..d934246d4d973 100644 --- a/activesupport/lib/active_support/testing/setup_and_teardown.rb +++ b/activesupport/lib/active_support/testing/setup_and_teardown.rb @@ -18,9 +18,9 @@ def self.included(base) alias_method :run, :run_with_callbacks_and_miniunit else begin - require 'mocha' + gem 'mocha' alias_method :run, :run_with_callbacks_and_mocha - rescue LoadError + rescue Gem::LoadError alias_method :run, :run_with_callbacks_and_testunit end end From d7462ea36550bd59cfbcd7961f5870c49bcf8963 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Mon, 23 Jun 2008 00:31:49 -0700 Subject: [PATCH 192/200] Revert "Check for mocha gem without requiring the lib. [#403 state:resolved]" This reverts commit 4d5ac3f3d2e1667971752c9a4e536de1a5bec364. --- .../lib/active_support/testing/setup_and_teardown.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/activesupport/lib/active_support/testing/setup_and_teardown.rb b/activesupport/lib/active_support/testing/setup_and_teardown.rb index d934246d4d973..21d71eb92a576 100644 --- a/activesupport/lib/active_support/testing/setup_and_teardown.rb +++ b/activesupport/lib/active_support/testing/setup_and_teardown.rb @@ -18,9 +18,9 @@ def self.included(base) alias_method :run, :run_with_callbacks_and_miniunit else begin - gem 'mocha' + require 'mocha' alias_method :run, :run_with_callbacks_and_mocha - rescue Gem::LoadError + rescue LoadError alias_method :run, :run_with_callbacks_and_testunit end end From f1cfd1248734ceaa50c1857f33d7ee0ecfdce3e6 Mon Sep 17 00:00:00 2001 From: Cheah Chu Yeow Date: Mon, 23 Jun 2008 21:56:02 +0800 Subject: [PATCH 193/200] Allow script/about to run in production mode instead of failing with a cryptic const_missing error. [#370 state:resolved] --- railties/CHANGELOG | 2 ++ railties/bin/about | 3 ++- railties/lib/commands/about.rb | 1 + 3 files changed, 5 insertions(+), 1 deletion(-) diff --git a/railties/CHANGELOG b/railties/CHANGELOG index 27d431386268a..7e5c35da15134 100644 --- a/railties/CHANGELOG +++ b/railties/CHANGELOG @@ -1,5 +1,7 @@ *Edge* +* Fix script/about in production mode. #370 [Cheah Chu Yeow, Xavier Noria, David Krmpotic] + * Add the gem load paths before the framework is loaded, so certain gems like RedCloth and BlueCloth can be frozen. * Fix discrepancies with loading rails/init.rb from gems. diff --git a/railties/bin/about b/railties/bin/about index cd38a32abfa40..ed8deb0dfc0e5 100644 --- a/railties/bin/about +++ b/railties/bin/about @@ -1,3 +1,4 @@ #!/usr/bin/env ruby require File.dirname(__FILE__) + '/../config/boot' -require 'commands/about' +$LOAD_PATH.unshift "#{RAILTIES_PATH}/builtin/rails_info" +require 'commands/about' \ No newline at end of file diff --git a/railties/lib/commands/about.rb b/railties/lib/commands/about.rb index 313bc18c6a933..7f53ac8a2ec35 100644 --- a/railties/lib/commands/about.rb +++ b/railties/lib/commands/about.rb @@ -1,2 +1,3 @@ require 'environment' +require 'rails/info' puts Rails::Info From 290e1e2fc53d80165cc876491ec0cbe18be376cf Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Mon, 23 Jun 2008 18:16:03 -0700 Subject: [PATCH 194/200] Treat any limit > 4 as bigint --- .../connection_adapters/mysql_adapter.rb | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb index ed1f08ac4f306..dd549507903c8 100755 --- a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb @@ -111,10 +111,11 @@ def extract_limit(sql_type) else super # we could return 65535 here, but we leave it undecorated by default end - when /^int/i; 4 when /^bigint/i; 8 - when /^smallint/i; 2 + when /^int/i; 4 when /^mediumint/i; 3 + when /^smallint/i; 2 + when /^tinyint/i; 1 else super end @@ -472,10 +473,11 @@ def type_to_sql(type, limit = nil, precision = nil, scale = nil) return super unless type.to_s == 'integer' case limit - when 1..2; 'smallint' - when 3; 'mediumint' - when 4, nil; 'int(11)' - when 5..8; 'bigint' + when 1; 'tinyint' + when 2; 'smallint' + when 3; 'mediumint' + when 4, nil; 'int(11)' + else; 'bigint' end end From b40947432540ad29abacaad9d9254bf70048190d Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Mon, 23 Jun 2008 23:41:21 -0700 Subject: [PATCH 195/200] link_to_function and button_to_function shouldn't modify their options hashes --- actionpack/lib/action_view/helpers/javascript_helper.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/actionpack/lib/action_view/helpers/javascript_helper.rb b/actionpack/lib/action_view/helpers/javascript_helper.rb index 31571de6c4e46..f89b6c2f70045 100644 --- a/actionpack/lib/action_view/helpers/javascript_helper.rb +++ b/actionpack/lib/action_view/helpers/javascript_helper.rb @@ -80,7 +80,7 @@ module JavaScriptHelper # return false;">Show me more # def link_to_function(name, *args, &block) - html_options = args.extract_options!.symbolize_keys! + html_options = args.extract_options!.symbolize_keys function = block_given? ? update_page(&block) : args[0] || '' onclick = "#{"#{html_options[:onclick]}; " if html_options[:onclick]}#{function}; return false;" @@ -106,7 +106,7 @@ def link_to_function(name, *args, &block) # page[:details].visual_effect :toggle_slide # end def button_to_function(name, *args, &block) - html_options = args.extract_options!.symbolize_keys! + html_options = args.extract_options!.symbolize_keys function = block_given? ? update_page(&block) : args[0] || '' onclick = "#{"#{html_options[:onclick]}; " if html_options[:onclick]}#{function};" From f6520b7dc7e460c62ba36dc761e39480bf77fe83 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Mon, 23 Jun 2008 23:42:06 -0700 Subject: [PATCH 196/200] Test for tinyint --- activerecord/test/cases/migration_test.rb | 2 +- activerecord/test/cases/schema_dumper_test.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/activerecord/test/cases/migration_test.rb b/activerecord/test/cases/migration_test.rb index 4735f136dfb10..908951590c2f6 100644 --- a/activerecord/test/cases/migration_test.rb +++ b/activerecord/test/cases/migration_test.rb @@ -175,7 +175,7 @@ def test_create_table_with_limits assert_equal 'bigint', eight.sql_type elsif current_adapter?(:MysqlAdapter) assert_match /^int\(\d+\)/, default.sql_type - assert_match /^smallint\(\d+\)/, one.sql_type + assert_match /^tinyint\(\d+\)/, one.sql_type assert_match /^int\(\d+\)/, four.sql_type assert_match /^bigint\(\d+\)/, eight.sql_type elsif current_adapter?(:OracleAdapter) diff --git a/activerecord/test/cases/schema_dumper_test.rb b/activerecord/test/cases/schema_dumper_test.rb index 942d686b92140..ee7e285a73d11 100644 --- a/activerecord/test/cases/schema_dumper_test.rb +++ b/activerecord/test/cases/schema_dumper_test.rb @@ -90,7 +90,7 @@ def test_schema_dump_includes_limit_constraint_for_integer_columns assert_match %r{c_int_4.*}, output assert_no_match %r{c_int_4.*:limit}, output elsif current_adapter?(:MysqlAdapter) - assert_match %r{c_int_1.*:limit => 2}, output + assert_match %r{c_int_1.*:limit => 1}, output assert_match %r{c_int_2.*:limit => 2}, output assert_match %r{c_int_3.*:limit => 3}, output From 69e72af62261dd8971890711b51c6eef2c68bc71 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Tue, 24 Jun 2008 11:53:49 -0700 Subject: [PATCH 197/200] Improve readability --- .../lib/action_controller/caching/fragments.rb | 18 ++++++++++-------- .../lib/action_view/template_handlers/erb.rb | 4 +++- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/actionpack/lib/action_controller/caching/fragments.rb b/actionpack/lib/action_controller/caching/fragments.rb index e4f5de44abc1a..57b31ec9d1b84 100644 --- a/actionpack/lib/action_controller/caching/fragments.rb +++ b/actionpack/lib/action_controller/caching/fragments.rb @@ -61,16 +61,18 @@ def fragment_cache_key(key) end def fragment_for(block, name = {}, options = nil) #:nodoc: - unless perform_caching then block.call; return end - - buffer = yield - - if cache = read_fragment(name, options) - buffer.concat(cache) + if perform_caching + buffer = yield + + if cache = read_fragment(name, options) + buffer.concat(cache) + else + pos = buffer.length + block.call + write_fragment(name, buffer[pos..-1], options) + end else - pos = buffer.length block.call - write_fragment(name, buffer[pos..-1], options) end end diff --git a/actionpack/lib/action_view/template_handlers/erb.rb b/actionpack/lib/action_view/template_handlers/erb.rb index 458416b6cbabd..ac715e30c2dda 100644 --- a/actionpack/lib/action_view/template_handlers/erb.rb +++ b/actionpack/lib/action_view/template_handlers/erb.rb @@ -53,7 +53,9 @@ def compile(template) end def cache_fragment(block, name = {}, options = nil) #:nodoc: - @view.fragment_for(block, name, options) { @view.response.template.output_buffer ||= '' } + @view.fragment_for(block, name, options) do + @view.response.template.output_buffer + end end end end From 6874caa4070f10cdbe104c9ac97dafd33dbdd282 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Tue, 24 Jun 2008 21:49:49 -0700 Subject: [PATCH 198/200] Performance: minor Array#to_param and #to_query speedups --- .../lib/active_support/core_ext/array/conversions.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/activesupport/lib/active_support/core_ext/array/conversions.rb b/activesupport/lib/active_support/core_ext/array/conversions.rb index a9882828cae1b..49ada8f174853 100644 --- a/activesupport/lib/active_support/core_ext/array/conversions.rb +++ b/activesupport/lib/active_support/core_ext/array/conversions.rb @@ -27,7 +27,7 @@ def to_sentence(options = {}) # Calls to_param on all its elements and joins the result with # slashes. This is used by url_for in Action Pack. def to_param - map(&:to_param).join '/' + collect { |e| e.to_param }.join '/' end # Converts an array into a string suitable for use as a URL query string, @@ -35,7 +35,8 @@ def to_param # # ['Rails', 'coding'].to_query('hobbies') # => "hobbies%5B%5D=Rails&hobbies%5B%5D=coding" def to_query(key) - collect { |value| value.to_query("#{key}[]") } * '&' + prefix = "#{key}[]" + collect { |value| value.to_query(prefix) }.join '&' end def self.included(base) #:nodoc: From 071fe79279e89e650acce6613f13027527d01650 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Tue, 24 Jun 2008 22:07:09 -0700 Subject: [PATCH 199/200] Include cache key in ModelName --- activerecord/lib/active_record/base.rb | 8 ++++---- .../lib/active_support/core_ext/module/model_naming.rb | 5 +++-- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index e6ab87e8fc983..021aaf4ca1dbf 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -2170,11 +2170,11 @@ def to_param def cache_key case when new_record? - "#{self.class.name.tableize}/new" - when self[:updated_at] - "#{self.class.name.tableize}/#{id}-#{updated_at.to_s(:number)}" + "#{self.class.model_name.cache_key}/new" + when timestamp = self[:updated_at] + "#{self.class.model_name.cache_key}/#{id}-#{timestamp.to_s(:number)}" else - "#{self.class.name.tableize}/#{id}" + "#{self.class.model_name.cache_key}/#{id}" end end diff --git a/activesupport/lib/active_support/core_ext/module/model_naming.rb b/activesupport/lib/active_support/core_ext/module/model_naming.rb index 26e76ab556755..abb02f1f70520 100644 --- a/activesupport/lib/active_support/core_ext/module/model_naming.rb +++ b/activesupport/lib/active_support/core_ext/module/model_naming.rb @@ -1,12 +1,13 @@ module ActiveSupport class ModelName < String - attr_reader :singular, :plural, :partial_path + attr_reader :singular, :plural, :cache_key, :partial_path def initialize(name) super @singular = underscore.tr('/', '_').freeze @plural = @singular.pluralize.freeze - @partial_path = "#{tableize}/#{demodulize.underscore}".freeze + @cache_key = tableize + @partial_path = "#{@cache_key}/#{demodulize.underscore}".freeze end end From 670e22e3724791f51d639f409930fba5af920081 Mon Sep 17 00:00:00 2001 From: Jimmy Baker Date: Tue, 24 Jun 2008 22:21:58 -0700 Subject: [PATCH 200/200] Patched HTML::Document#initialize call to Node.parse so that it includes the strict argument. [#330] --- .../vendor/html-scanner/html/document.rb | 2 +- .../controller/html-scanner/document_test.rb | 25 +++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/actionpack/lib/action_controller/vendor/html-scanner/html/document.rb b/actionpack/lib/action_controller/vendor/html-scanner/html/document.rb index 607fd186b91c7..b8d73c350d82f 100644 --- a/actionpack/lib/action_controller/vendor/html-scanner/html/document.rb +++ b/actionpack/lib/action_controller/vendor/html-scanner/html/document.rb @@ -17,7 +17,7 @@ def initialize(text, strict=false, xml=false) @root = Node.new(nil) node_stack = [ @root ] while token = tokenizer.next - node = Node.parse(node_stack.last, tokenizer.line, tokenizer.position, token) + node = Node.parse(node_stack.last, tokenizer.line, tokenizer.position, token, strict) node_stack.last.children << node unless node.tag? && node.closing == :close if node.tag? diff --git a/actionpack/test/controller/html-scanner/document_test.rb b/actionpack/test/controller/html-scanner/document_test.rb index 0519533dbd057..1c3facb9e3e54 100644 --- a/actionpack/test/controller/html-scanner/document_test.rb +++ b/actionpack/test/controller/html-scanner/document_test.rb @@ -120,4 +120,29 @@ def test_find_empty_tag assert doc.find(:tag => "div", :attributes => { :id => "map" }, :content => "") assert doc.find(:tag => "div", :attributes => { :id => "map" }, :content => nil) end + + def test_parse_invalid_document + assert_nothing_raised do + doc = HTML::Document.new(" + + + + +
          About Us
          + ") + end + end + + def test_invalid_document_raises_exception_when_strict + assert_raises RuntimeError do + doc = HTML::Document.new(" + + + + +
          About Us
          + ", true) + end + end + end