From cdff3818608f30d79aad2a3626a06a4fb2d1c7a0 Mon Sep 17 00:00:00 2001 From: Elad Meidar Date: Fri, 25 Sep 2009 17:35:56 -0400 Subject: [PATCH] LH3218 - default scope overriding attributes on :create, fixed to overrite attributes with scoped defaults only if no value was specificied explicitly in the #create or #new calls of if a default column value was assigned --- activerecord/lib/active_record/base.rb | 12 +++++++++++- activerecord/test/cases/method_scoping_test.rb | 4 ++++ activerecord/test/cases/named_scope_test.rb | 1 + 3 files changed, 16 insertions(+), 1 deletions(-) diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 2ec2f73..5906755 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -2432,11 +2432,21 @@ module ActiveRecord #:nodoc: # hence you can't have attributes that aren't part of the table columns. def initialize(attributes = nil) @attributes = attributes_from_column_definition + attrs_with_default_values = @attributes.collect {|att, val| att if val.present? }.compact @attributes_cache = {} @new_record = true ensure_proper_type self.attributes = attributes unless attributes.nil? - self.class.send(:scope, :create).each { |att,value| self.send("#{att}=", value) } if self.class.send(:scoped?, :create) + # self.class.send(:scope, :create).each { |att,value| self.send("#{att}=", value) } if self.class.send(:scoped?, :create) + if self.class.send(:scoped?, :create) + create_scope_attrs = self.class.send(:scope, :create).stringify_keys! + + create_scope_attrs.each do |att, val| +# puts "\n#{att} => " + self.send("#{att}").to_s + " scope value => #{val.to_s}" + self.send("#{att}=", val) if self.send("#{att}").blank? || attrs_with_default_values.include?(att) + end + end + result = yield self if block_given? callback(:after_initialize) if respond_to_without_attributes?(:after_initialize) result diff --git a/activerecord/test/cases/method_scoping_test.rb b/activerecord/test/cases/method_scoping_test.rb index d8246f4..1197dfa 100644 --- a/activerecord/test/cases/method_scoping_test.rb +++ b/activerecord/test/cases/method_scoping_test.rb @@ -601,6 +601,10 @@ class DefaultScopingTest < ActiveRecord::TestCase assert_equal 'Jamis', DeveloperCalledJamis.create!.name end + def test_create_attribute_overwrites_default + assert_equal 'David', DeveloperCalledJamis.create!(:name => 'David').name + end + def test_default_scoping_with_threads scope = [{ :create => {}, :find => { :order => 'salary DESC' } }] diff --git a/activerecord/test/cases/named_scope_test.rb b/activerecord/test/cases/named_scope_test.rb index 208a2ee..8571cf3 100644 --- a/activerecord/test/cases/named_scope_test.rb +++ b/activerecord/test/cases/named_scope_test.rb @@ -295,6 +295,7 @@ class NamedScopeTest < ActiveRecord::TestCase def test_chaining_should_use_latest_conditions_when_creating post = Topic.rejected.new + assert !post.approved? post = Topic.rejected.approved.new -- 1.6.0.2 From 70d21a696b1e1756922bf9c3c643dc74ed105048 Mon Sep 17 00:00:00 2001 From: Elad Meidar Date: Fri, 25 Sep 2009 17:38:08 -0400 Subject: [PATCH] LH3218 - default scope overriding attributes on :create, fixed to overrite attributes with scoped defaults only if no value was specificied explicitly in the #create or #new calls of if a default column value was assigned --- activerecord/lib/active_record/base.rb | 2 -- 1 files changed, 0 insertions(+), 2 deletions(-) diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 5906755..375df89 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -2437,12 +2437,10 @@ module ActiveRecord #:nodoc: @new_record = true ensure_proper_type self.attributes = attributes unless attributes.nil? - # self.class.send(:scope, :create).each { |att,value| self.send("#{att}=", value) } if self.class.send(:scoped?, :create) if self.class.send(:scoped?, :create) create_scope_attrs = self.class.send(:scope, :create).stringify_keys! create_scope_attrs.each do |att, val| -# puts "\n#{att} => " + self.send("#{att}").to_s + " scope value => #{val.to_s}" self.send("#{att}=", val) if self.send("#{att}").blank? || attrs_with_default_values.include?(att) end end -- 1.6.0.2 From 535040eac4fa3a24f8fcdaaebe99a5e4ec928316 Mon Sep 17 00:00:00 2001 From: Elad Meidar Date: Fri, 25 Sep 2009 17:41:13 -0400 Subject: [PATCH] LH3218 - default scope overriding attributes on :create, fixed to overrite attributes with scoped defaults only if no value was specificied explicitly in the #create or #new calls of if a default column value was assigned --- activerecord/lib/active_record/base.rb | 3 ++- 1 files changed, 2 insertions(+), 1 deletions(-) diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 375df89..5c5008b 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -2437,9 +2437,10 @@ module ActiveRecord #:nodoc: @new_record = true ensure_proper_type self.attributes = attributes unless attributes.nil? + + # Override only default column and empty attribute values with default/named_scope :conditions attribute values if self.class.send(:scoped?, :create) create_scope_attrs = self.class.send(:scope, :create).stringify_keys! - create_scope_attrs.each do |att, val| self.send("#{att}=", val) if self.send("#{att}").blank? || attrs_with_default_values.include?(att) end -- 1.6.0.2