diff options
author | Aditya Chadha <aditya@sublucid.com> | 2009-05-02 18:32:45 -0400 |
---|---|---|
committer | Aditya Chadha <aditya@sublucid.com> | 2009-05-02 18:32:45 -0400 |
commit | 7a17ad3f2b89baea94450af8e63a640ace570938 (patch) | |
tree | 89cd60c05d4a812b3c8636373a05689b1df5bf75 /activerecord | |
parent | 5469a0be1a517a0c2d8ee553b704df4e6f7df306 (diff) | |
parent | 1b32f882091ed7744654f74238f3f3b1ba6701ae (diff) | |
download | rails-7a17ad3f2b89baea94450af8e63a640ace570938.tar.gz rails-7a17ad3f2b89baea94450af8e63a640ace570938.tar.bz2 rails-7a17ad3f2b89baea94450af8e63a640ace570938.zip |
Merge branch 'master' of git@github.com:lifo/docrails
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/lib/active_record/autosave_association.rb | 14 | ||||
-rwxr-xr-x | activerecord/lib/active_record/base.rb | 2 | ||||
-rw-r--r-- | activerecord/lib/active_record/serialization.rb | 8 | ||||
-rw-r--r-- | activerecord/lib/active_record/serializers/json_serializer.rb | 9 | ||||
-rw-r--r-- | activerecord/lib/active_record/test_case.rb | 2 | ||||
-rw-r--r-- | activerecord/test/cases/associations/eager_load_nested_include_test.rb | 10 | ||||
-rw-r--r-- | activerecord/test/cases/autosave_association_test.rb | 22 | ||||
-rwxr-xr-x | activerecord/test/cases/base_test.rb | 6 | ||||
-rw-r--r-- | activerecord/test/cases/calculations_test.rb | 2 | ||||
-rw-r--r-- | activerecord/test/cases/finder_test.rb | 8 | ||||
-rw-r--r-- | activerecord/test/cases/method_scoping_test.rb | 2 |
11 files changed, 57 insertions, 28 deletions
diff --git a/activerecord/lib/active_record/autosave_association.rb b/activerecord/lib/active_record/autosave_association.rb index 741aa2acbe..9717ca3d8b 100644 --- a/activerecord/lib/active_record/autosave_association.rb +++ b/activerecord/lib/active_record/autosave_association.rb @@ -311,11 +311,13 @@ module ActiveRecord # ActiveRecord::Base after the AutosaveAssociation module, which it does by default. def save_has_one_association(reflection) if (association = association_instance_get(reflection.name)) && !association.target.nil? - if reflection.options[:autosave] && association.marked_for_destruction? + autosave = reflection.options[:autosave] + + if autosave && association.marked_for_destruction? association.destroy - elsif new_record? || association.new_record? || association[reflection.primary_key_name] != id || reflection.options[:autosave] + elsif new_record? || association.new_record? || association[reflection.primary_key_name] != id || autosave association[reflection.primary_key_name] = id - association.save(false) + association.save(!autosave) end end end @@ -330,10 +332,12 @@ module ActiveRecord # ActiveRecord::Base after the AutosaveAssociation module, which it does by default. def save_belongs_to_association(reflection) if association = association_instance_get(reflection.name) - if reflection.options[:autosave] && association.marked_for_destruction? + autosave = reflection.options[:autosave] + + if autosave && association.marked_for_destruction? association.destroy else - association.save(false) if association.new_record? || reflection.options[:autosave] + association.save(!autosave) if association.new_record? || autosave if association.updated? self[reflection.primary_key_name] = association.id diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 97c36a675d..d331147270 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -829,7 +829,7 @@ module ActiveRecord #:nodoc: # Book.update_all "author = 'David'", "title LIKE '%Rails%'" # # # Update all avatars migrated more than a week ago - # Avatar.update_all ['migrated_at = ?, Time.now.utc], ['migrated_at > ?', 1.week.ago] + # Avatar.update_all ['migrated_at = ?', Time.now.utc], ['migrated_at > ?', 1.week.ago] # # # Update all books that match our conditions, but limit it to 5 ordered by date # Book.update_all "author = 'David'", "title LIKE '%Rails%'", :order => 'created_at', :limit => 5 diff --git a/activerecord/lib/active_record/serialization.rb b/activerecord/lib/active_record/serialization.rb index 870b4b2dd4..78f66c3a73 100644 --- a/activerecord/lib/active_record/serialization.rb +++ b/activerecord/lib/active_record/serialization.rb @@ -20,9 +20,9 @@ module ActiveRecord #:nodoc: if options[:only] options.delete(:except) - attribute_names = attribute_names & Array(options[:only]).collect { |n| n.to_s } + attribute_names = attribute_names & Array.wrap(options[:only]).collect { |n| n.to_s } else - options[:except] = Array(options[:except]) | Array(@record.class.inheritance_column) + options[:except] = Array.wrap(options[:except]) | Array.wrap(@record.class.inheritance_column) attribute_names = attribute_names - options[:except].collect { |n| n.to_s } end @@ -30,7 +30,7 @@ module ActiveRecord #:nodoc: end def serializable_method_names - Array(options[:methods]).inject([]) do |method_attributes, name| + Array.wrap(options[:methods]).inject([]) do |method_attributes, name| method_attributes << name if @record.respond_to?(name.to_s) method_attributes end @@ -51,7 +51,7 @@ module ActiveRecord #:nodoc: :only => options[:only] } include_has_options = include_associations.is_a?(Hash) - associations = include_has_options ? include_associations.keys : Array(include_associations) + associations = include_has_options ? include_associations.keys : Array.wrap(include_associations) for association in associations records = case @record.class.reflect_on_association(association).macro diff --git a/activerecord/lib/active_record/serializers/json_serializer.rb b/activerecord/lib/active_record/serializers/json_serializer.rb index e9cb8bfca9..48df15d2c0 100644 --- a/activerecord/lib/active_record/serializers/json_serializer.rb +++ b/activerecord/lib/active_record/serializers/json_serializer.rb @@ -82,14 +82,17 @@ module ActiveRecord #:nodoc: end end - # For compatibility with ActiveSupport::JSON.encode - alias rails_to_json to_json - def from_json(json) self.attributes = ActiveSupport::JSON.decode(json) self end + private + # For compatibility with ActiveSupport::JSON.encode + def rails_to_json(options, *args) + to_json(options) + end + class JsonSerializer < ActiveRecord::Serialization::Serializer #:nodoc: def serialize ActiveSupport::JSON.encode(serializable_record) diff --git a/activerecord/lib/active_record/test_case.rb b/activerecord/lib/active_record/test_case.rb index 8c6abaaccb..2dfe2c09ea 100644 --- a/activerecord/lib/active_record/test_case.rb +++ b/activerecord/lib/active_record/test_case.rb @@ -20,7 +20,7 @@ module ActiveRecord patterns_to_match.each do |pattern| failed_patterns << pattern unless $queries_executed.any?{ |sql| pattern === sql } end - assert failed_patterns.empty?, "Query pattern(s) #{failed_patterns.map(&:inspect).join(', ')} not found." + assert failed_patterns.empty?, "Query pattern(s) #{failed_patterns.map(&:inspect).join(', ')} not found.#{$queries_executed.size == 0 ? '' : "\nQueries:\n#{$queries_executed.join("\n")}"}" end def assert_queries(num = 1) diff --git a/activerecord/test/cases/associations/eager_load_nested_include_test.rb b/activerecord/test/cases/associations/eager_load_nested_include_test.rb index 1b2e0fc11e..677226ec89 100644 --- a/activerecord/test/cases/associations/eager_load_nested_include_test.rb +++ b/activerecord/test/cases/associations/eager_load_nested_include_test.rb @@ -1,6 +1,6 @@ require 'cases/helper' -require 'models/author' require 'models/post' +require 'models/author' require 'models/comment' require 'models/category' require 'models/categorization' @@ -66,13 +66,13 @@ class EagerLoadPolyAssocsTest < ActiveRecord::TestCase def setup generate_test_object_graphs end - + def teardown - [Circle, Square, Triangle, PaintColor, PaintTexture, + [Circle, Square, Triangle, PaintColor, PaintTexture, ShapeExpression, NonPolyOne, NonPolyTwo].each do |c| c.delete_all end - + end @@ -127,4 +127,4 @@ class EagerLoadNestedIncludeWithMissingDataTest < ActiveRecord::TestCase Author.all :include => includes, :conditions => {:authors => {:name => @davey_mcdave.name}}, :order => 'categories.name' end end -end
\ No newline at end of file +end diff --git a/activerecord/test/cases/autosave_association_test.rb b/activerecord/test/cases/autosave_association_test.rb index 436f50d395..919b6f857c 100644 --- a/activerecord/test/cases/autosave_association_test.rb +++ b/activerecord/test/cases/autosave_association_test.rb @@ -38,6 +38,17 @@ class TestAutosaveAssociationsInGeneral < ActiveRecord::TestCase end class TestDefaultAutosaveAssociationOnAHasOneAssociation < ActiveRecord::TestCase + def test_should_save_parent_but_not_invalid_child + firm = Firm.new(:name => 'GlobalMegaCorp') + assert firm.valid? + + firm.build_account_using_primary_key + assert !firm.build_account_using_primary_key.valid? + + assert firm.save + assert firm.account_using_primary_key.new_record? + end + def test_save_fails_for_invalid_has_one firm = Firm.find(:first) assert firm.valid? @@ -126,6 +137,17 @@ class TestDefaultAutosaveAssociationOnAHasOneAssociation < ActiveRecord::TestCas end class TestDefaultAutosaveAssociationOnABelongsToAssociation < ActiveRecord::TestCase + def test_should_save_parent_but_not_invalid_child + client = Client.new(:name => 'Joe (the Plumber)') + assert client.valid? + + client.build_firm + assert !client.firm.valid? + + assert client.save + assert client.firm.new_record? + end + def test_save_fails_for_invalid_belongs_to assert log = AuditLog.create(:developer_id => 0, :message => "") diff --git a/activerecord/test/cases/base_test.rb b/activerecord/test/cases/base_test.rb index d97cd17d75..7ca2807f7e 100755 --- a/activerecord/test/cases/base_test.rb +++ b/activerecord/test/cases/base_test.rb @@ -1,4 +1,5 @@ require "cases/helper" +require 'models/post' require 'models/author' require 'models/topic' require 'models/reply' @@ -12,7 +13,6 @@ require 'models/auto_id' require 'models/column_name' require 'models/subscriber' require 'models/keyboard' -require 'models/post' require 'models/comment' require 'models/minimalistic' require 'models/warehouse_thing' @@ -1115,7 +1115,7 @@ class BasicsTest < ActiveRecord::TestCase Time.zone = nil Topic.skip_time_zone_conversion_for_attributes = [] end - + def test_multiparameter_attributes_on_time_only_column_with_time_zone_aware_attributes_does_not_do_time_zone_conversion ActiveRecord::Base.time_zone_aware_attributes = true ActiveRecord::Base.default_timezone = :utc @@ -1439,7 +1439,7 @@ class BasicsTest < ActiveRecord::TestCase topic = Topic.create("content" => myobj).reload assert_equal(myobj, topic.content) end - + def test_serialized_string_attribute myobj = "Yes" topic = Topic.create("content" => myobj).reload diff --git a/activerecord/test/cases/calculations_test.rb b/activerecord/test/cases/calculations_test.rb index 56dcdea110..fd455e0864 100644 --- a/activerecord/test/cases/calculations_test.rb +++ b/activerecord/test/cases/calculations_test.rb @@ -233,7 +233,7 @@ class CalculationsTest < ActiveRecord::TestCase assert_equal 8, c['Jadedpixel'] end - def test_should_group_by_summed_field_with_conditions_and_having + def test_should_group_by_summed_field_through_association_and_having c = companies(:rails_core).companies.sum(:id, :group => :name, :having => 'sum(id) > 7') assert_nil c['Leetsoft'] diff --git a/activerecord/test/cases/finder_test.rb b/activerecord/test/cases/finder_test.rb index d8778957c0..28eb311618 100644 --- a/activerecord/test/cases/finder_test.rb +++ b/activerecord/test/cases/finder_test.rb @@ -1,4 +1,5 @@ require "cases/helper" +require 'models/post' require 'models/author' require 'models/categorization' require 'models/comment' @@ -7,7 +8,6 @@ require 'models/topic' require 'models/reply' require 'models/entrant' require 'models/developer' -require 'models/post' require 'models/customer' require 'models/job' require 'models/categorization' @@ -94,16 +94,16 @@ class FinderTest < ActiveRecord::TestCase assert_raise(NoMethodError) { Topic.exists?([1,2]) } end - + def test_exists_returns_true_with_one_record_and_no_args assert Topic.exists? end - + def test_does_not_exist_with_empty_table_and_no_args_given Topic.delete_all assert !Topic.exists? end - + def test_exists_with_aggregate_having_three_mappings existing_address = customers(:david).address assert Customer.exists?(:address => existing_address) diff --git a/activerecord/test/cases/method_scoping_test.rb b/activerecord/test/cases/method_scoping_test.rb index 3c34cdeade..2165d2f7e8 100644 --- a/activerecord/test/cases/method_scoping_test.rb +++ b/activerecord/test/cases/method_scoping_test.rb @@ -1,9 +1,9 @@ require "cases/helper" +require 'models/post' require 'models/author' require 'models/developer' require 'models/project' require 'models/comment' -require 'models/post' require 'models/category' class MethodScopingTest < ActiveRecord::TestCase |