From 1a73e98660b29a2ce9eccd5b0776367a4969a012 Mon Sep 17 00:00:00 2001 From: Kevin Glowacz Date: Thu, 22 May 2008 14:21:33 -0500 Subject: Fix auto_link helper for already linked urls. [#72 state:resolved] Signed-off-by: Pratik Naik --- actionpack/lib/action_view/helpers/text_helper.rb | 4 ++-- actionpack/test/template/text_helper_test.rb | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/actionpack/lib/action_view/helpers/text_helper.rb b/actionpack/lib/action_view/helpers/text_helper.rb index b710157f51..669a285424 100644 --- a/actionpack/lib/action_view/helpers/text_helper.rb +++ b/actionpack/lib/action_view/helpers/text_helper.rb @@ -454,7 +454,7 @@ module ActionView ( # leading text <\w+.*?>| # leading HTML tag, or [^=!:'"/]| # leading punctuation, or - | # nothing + ^ # beginning of line ) ( (?:https?://)| # protocol spec, or @@ -468,7 +468,7 @@ module ActionView (?:\?[\w\+@%&=.;-]+)? # query string (?:\#[\w\-]*)? # trailing anchor ) - ([[:punct:]]|\s|<|$) # trailing text + ([[:punct:]]|<|$|) # trailing text }x unless const_defined?(:AUTO_LINK_RE) # Turns all urls into clickable links. If a block is given, each url diff --git a/actionpack/test/template/text_helper_test.rb b/actionpack/test/template/text_helper_test.rb index 36c939f761..62cdca03d1 100644 --- a/actionpack/test/template/text_helper_test.rb +++ b/actionpack/test/template/text_helper_test.rb @@ -264,6 +264,7 @@ class TextHelperTest < ActionView::TestCase assert_equal '', auto_link(nil) assert_equal '', auto_link('') assert_equal "#{link_result} #{link_result} #{link_result}", auto_link("#{link_raw} #{link_raw} #{link_raw}") + assert_equal 'Ruby On Rails', auto_link('Ruby On Rails') end def test_auto_link_at_eol -- cgit v1.2.3 From 888d5059bdb71e6d034d0c09d7e3b3e235fd8507 Mon Sep 17 00:00:00 2001 From: Michael Koziarski Date: Thu, 22 May 2008 19:28:52 +1200 Subject: Return Partial Updates to be purely opt in to prevent users from inadvertently corrupting data. --- railties/configs/initializers/new_rails_defaults.rb | 3 --- 1 file changed, 3 deletions(-) diff --git a/railties/configs/initializers/new_rails_defaults.rb b/railties/configs/initializers/new_rails_defaults.rb index 1a718608ae..b959c6d549 100644 --- a/railties/configs/initializers/new_rails_defaults.rb +++ b/railties/configs/initializers/new_rails_defaults.rb @@ -1,9 +1,6 @@ # 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. -# Only save the attributes that have changed since the record was loaded. -ActiveRecord::Base.partial_updates = true - # Include ActiveRecord class name as root for JSON serialized output. ActiveRecord::Base.include_root_in_json = true -- cgit v1.2.3 From 5b8d0f134a5766aa1b3d0945b79f04d2bb911c02 Mon Sep 17 00:00:00 2001 From: Michael Koziarski Date: Fri, 23 May 2008 09:07:38 +1200 Subject: Mention partial updates in environment.rb to make it easier for people to opt-in if they know it's safe --- railties/environments/environment.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/railties/environments/environment.rb b/railties/environments/environment.rb index c33b80da4f..d03447e1d3 100644 --- a/railties/environments/environment.rb +++ b/railties/environments/environment.rb @@ -64,4 +64,7 @@ Rails::Initializer.run do |config| # Activate observers that should always be running # config.active_record.observers = :cacher, :garbage_collector + + # Make ActiveRecord only save the attributes that have changed since the record was loaded. + # config.active_record.partial_updates = true end \ No newline at end of file -- cgit v1.2.3 From 8d0b4fa39fab6e5d1e3382b4b137cc47e559b0be Mon Sep 17 00:00:00 2001 From: josevalim Date: Fri, 23 May 2008 11:20:13 +0200 Subject: Added :select option to has_one and belongs_to, remove unused :order option on belongs_to. Signed-off-by: Michael Koziarski [#241 state:resolved] --- activerecord/lib/active_record/associations.rb | 16 +++++++++------- .../active_record/associations/belongs_to_association.rb | 5 +++-- .../associations/belongs_to_polymorphic_association.rb | 5 +++-- .../active_record/associations/has_one_association.rb | 5 +++-- .../cases/associations/belongs_to_associations_test.rb | 5 +++++ .../test/cases/associations/has_one_associations_test.rb | 5 +++++ activerecord/test/models/company.rb | 2 ++ 7 files changed, 30 insertions(+), 13 deletions(-) mode change 100644 => 100755 activerecord/lib/active_record/associations.rb mode change 100644 => 100755 activerecord/lib/active_record/associations/belongs_to_association.rb mode change 100644 => 100755 activerecord/lib/active_record/associations/belongs_to_polymorphic_association.rb mode change 100644 => 100755 activerecord/lib/active_record/associations/has_one_association.rb mode change 100644 => 100755 activerecord/test/cases/associations/belongs_to_associations_test.rb mode change 100644 => 100755 activerecord/test/cases/associations/has_one_associations_test.rb diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb old mode 100644 new mode 100755 index 95caf68692..6c21fed8ea --- a/activerecord/lib/active_record/associations.rb +++ b/activerecord/lib/active_record/associations.rb @@ -667,7 +667,7 @@ module ActiveRecord # * :limit - An integer determining the limit on the number of rows that should be returned. # * :offset - An integer determining the offset from where the rows should be fetched. So at 5, it would skip the first 4 rows. # * :select - By default, this is * as in SELECT * FROM, but can be changed if you, for example, want to do a join - # but not include the joined columns. + # but not include the joined columns. Do not forget to include the primary and foreign keys, otherwise it will rise an error. # * :as - Specifies a polymorphic interface (See belongs_to). # * :through - Specifies a Join Model through which to perform the query. Options for :class_name and :foreign_key # are ignored, as the association uses the source reflection. You can only use a :through query through a belongs_to @@ -747,6 +747,8 @@ module ActiveRecord # as the default :foreign_key. # * :include - Specify second-order associations that should be eager loaded when this object is loaded. # * :as - Specifies a polymorphic interface (See belongs_to). + # * :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. # * :through: Specifies a Join Model through which to perform the query. Options for :class_name and :foreign_key # are ignored, as the association uses the source reflection. You can only use a :through query through a # has_one or belongs_to association on the join model. @@ -821,8 +823,8 @@ module ActiveRecord # if the real class name is Person, you'll have to specify it with this option. # * :conditions - Specify the conditions that the associated object must meet in order to be included as a +WHERE+ # SQL fragment, such as authorized = 1. - # * :order - Specify the order in which the associated objects are returned as an ORDER BY SQL fragment, - # such as last_name, first_name DESC. + # * :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. # * :foreign_key - Specify the foreign key used for the association. By default this is guessed to be the name # of the association with an "_id" suffix. So a class that defines a belongs_to :person association will use # "person_id" as the default :foreign_key. Similarly, belongs_to :favorite_person, :class_name => "Person" @@ -1011,7 +1013,7 @@ module ActiveRecord # * :limit - An integer determining the limit on the number of rows that should be returned. # * :offset - An integer determining the offset from where the rows should be fetched. So at 5, it would skip the first 4 rows. # * :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. + # 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. # # Option examples: @@ -1341,7 +1343,7 @@ module ActiveRecord def create_has_one_reflection(association_id, options) options.assert_valid_keys( - :class_name, :foreign_key, :remote, :conditions, :order, :include, :dependent, :counter_cache, :extend, :as, :readonly + :class_name, :foreign_key, :remote, :select, :conditions, :order, :include, :dependent, :counter_cache, :extend, :as, :readonly ) create_reflection(:has_one, association_id, options, self) @@ -1349,14 +1351,14 @@ module ActiveRecord def create_has_one_through_reflection(association_id, options) options.assert_valid_keys( - :class_name, :foreign_key, :remote, :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 ) create_reflection(:has_one, association_id, options, self) end def create_belongs_to_reflection(association_id, options) options.assert_valid_keys( - :class_name, :foreign_key, :foreign_type, :remote, :conditions, :order, :include, :dependent, + :class_name, :foreign_key, :foreign_type, :remote, :select, :conditions, :include, :dependent, :counter_cache, :extend, :polymorphic, :readonly ) diff --git a/activerecord/lib/active_record/associations/belongs_to_association.rb b/activerecord/lib/active_record/associations/belongs_to_association.rb old mode 100644 new mode 100755 index 9ff3f13592..7c28cbdd07 --- a/activerecord/lib/active_record/associations/belongs_to_association.rb +++ b/activerecord/lib/active_record/associations/belongs_to_association.rb @@ -42,10 +42,11 @@ module ActiveRecord private def find_target @reflection.klass.find( - @owner[@reflection.primary_key_name], + @owner[@reflection.primary_key_name], + :select => @reflection.options[:select], :conditions => conditions, :include => @reflection.options[:include], - :readonly => @reflection.options[:readonly] + :readonly => @reflection.options[:readonly] ) end diff --git a/activerecord/lib/active_record/associations/belongs_to_polymorphic_association.rb b/activerecord/lib/active_record/associations/belongs_to_polymorphic_association.rb old mode 100644 new mode 100755 index 9549b959fc..df4ae38f38 --- a/activerecord/lib/active_record/associations/belongs_to_polymorphic_association.rb +++ b/activerecord/lib/active_record/associations/belongs_to_polymorphic_association.rb @@ -29,12 +29,13 @@ module ActiveRecord if @reflection.options[:conditions] association_class.find( - @owner[@reflection.primary_key_name], + @owner[@reflection.primary_key_name], + :select => @reflection.options[:select], :conditions => conditions, :include => @reflection.options[:include] ) else - association_class.find(@owner[@reflection.primary_key_name], :include => @reflection.options[:include]) + association_class.find(@owner[@reflection.primary_key_name], :select => @reflection.options[:select], :include => @reflection.options[:include]) end end diff --git a/activerecord/lib/active_record/associations/has_one_association.rb b/activerecord/lib/active_record/associations/has_one_association.rb old mode 100644 new mode 100755 index 3ff9fe3b9f..c2b3503e0d --- a/activerecord/lib/active_record/associations/has_one_association.rb +++ b/activerecord/lib/active_record/associations/has_one_association.rb @@ -51,10 +51,11 @@ module ActiveRecord private def find_target @reflection.klass.find(:first, - :conditions => @finder_sql, + :conditions => @finder_sql, + :select => @reflection.options[:select], :order => @reflection.options[:order], :include => @reflection.options[:include], - :readonly => @reflection.options[:readonly] + :readonly => @reflection.options[:readonly] ) end diff --git a/activerecord/test/cases/associations/belongs_to_associations_test.rb b/activerecord/test/cases/associations/belongs_to_associations_test.rb old mode 100644 new mode 100755 index 4382ba17ef..3073eae355 --- a/activerecord/test/cases/associations/belongs_to_associations_test.rb +++ b/activerecord/test/cases/associations/belongs_to_associations_test.rb @@ -92,6 +92,11 @@ class BelongsToAssociationsTest < ActiveRecord::TestCase assert_not_nil Company.find(3).firm_with_condition, "Microsoft should have a firm" end + def test_with_select + assert_equal Company.find(2).firm_with_select.attributes.size, 1 + assert_equal Company.find(2, :include => :firm_with_select ).firm_with_select.attributes.size, 1 + end + def test_belongs_to_counter debate = Topic.create("title" => "debate") assert_equal 0, debate.send(:read_attribute, "replies_count"), "No replies yet" diff --git a/activerecord/test/cases/associations/has_one_associations_test.rb b/activerecord/test/cases/associations/has_one_associations_test.rb old mode 100644 new mode 100755 index 9e99caa7b7..e7d4de8246 --- a/activerecord/test/cases/associations/has_one_associations_test.rb +++ b/activerecord/test/cases/associations/has_one_associations_test.rb @@ -24,6 +24,11 @@ class HasOneAssociationsTest < ActiveRecord::TestCase assert_queries(0) { firms.each(&:account) } end + def test_with_select + assert_equal Firm.find(1).account_with_select.attributes.size, 2 + assert_equal Firm.find(1, :include => :account_with_select).attributes.size, 2 + end + def test_can_marshal_has_one_association_with_nil_target firm = Firm.new assert_nothing_raised do diff --git a/activerecord/test/models/company.rb b/activerecord/test/models/company.rb index f637490c59..cc521d2a02 100755 --- a/activerecord/test/models/company.rb +++ b/activerecord/test/models/company.rb @@ -47,6 +47,7 @@ class Firm < Company has_many :readonly_clients, :class_name => 'Client', :readonly => true has_one :account, :foreign_key => "firm_id", :dependent => :destroy + has_one :account_with_select, :foreign_key => "firm_id", :select => "id, firm_id" has_one :readonly_account, :foreign_key => "firm_id", :class_name => "Account", :readonly => true end @@ -64,6 +65,7 @@ end class Client < Company belongs_to :firm, :foreign_key => "client_of" belongs_to :firm_with_basic_id, :class_name => "Firm", :foreign_key => "firm_id" + belongs_to :firm_with_select, :class_name => "Firm", :foreign_key => "firm_id", :select => "id" belongs_to :firm_with_other_name, :class_name => "Firm", :foreign_key => "client_of" belongs_to :firm_with_condition, :class_name => "Firm", :foreign_key => "client_of", :conditions => ["1 = ?", 1] belongs_to :readonly_firm, :class_name => "Firm", :foreign_key => "firm_id", :readonly => true -- cgit v1.2.3 From b88ceb7dc8d31bdbea95ab4242bbdee17178cda9 Mon Sep 17 00:00:00 2001 From: Trevor Turk Date: Fri, 23 May 2008 15:51:56 -0500 Subject: add failing test case for block-setting of attributes via association Signed-off-by: Michael Koziarski --- activerecord/test/cases/associations_test.rb | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/activerecord/test/cases/associations_test.rb b/activerecord/test/cases/associations_test.rb index 3ad8c608e0..034fe14996 100755 --- a/activerecord/test/cases/associations_test.rb +++ b/activerecord/test/cases/associations_test.rb @@ -160,6 +160,15 @@ class AssociationProxyTest < ActiveRecord::TestCase assert_equal 1, developer.reload.audit_logs.size end + def test_create_via_association_with_block + post1 = Post.create(:title => "setting body with a block") {|p| p.body = "will work"} + assert_equal post1.body, "will work" + assert_nothing_raised do + post2 = authors(:david).posts.create(:title => "setting body with a block") {|p| p.body = "won't work"} + end + assert_equal post2.body, "won't work" + end + def test_failed_reload_returns_nil p = setup_dangling_association assert_nil p.author.reload -- cgit v1.2.3 From 6cba97d2a449faf21aec9fe9d4434067e414226f Mon Sep 17 00:00:00 2001 From: Ryan Bates Date: Fri, 23 May 2008 14:57:11 -0700 Subject: Create through associations can now work with blocks. Signed-off-by: Michael Koziarski [#248 state:resolved] --- .../active_record/associations/association_collection.rb | 10 ++++++++-- activerecord/test/cases/associations_test.rb | 15 +++++++++------ 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/activerecord/lib/active_record/associations/association_collection.rb b/activerecord/lib/active_record/associations/association_collection.rb index 2d3750e107..2f03197012 100644 --- a/activerecord/lib/active_record/associations/association_collection.rb +++ b/activerecord/lib/active_record/associations/association_collection.rb @@ -166,12 +166,18 @@ module ActiveRecord if attrs.is_a?(Array) attrs.collect { |attr| create(attr) } else - create_record(attrs) { |record| record.save } + create_record(attrs) do |record| + yield(record) if block_given? + record.save + end end end def create!(attrs = {}) - create_record(attrs) { |record| record.save! } + create_record(attrs) do |record| + yield(record) if block_given? + record.save! + end end # Returns the size of the collection by executing a SELECT COUNT(*) query if the collection hasn't been loaded and diff --git a/activerecord/test/cases/associations_test.rb b/activerecord/test/cases/associations_test.rb index 034fe14996..59349dd7cf 100755 --- a/activerecord/test/cases/associations_test.rb +++ b/activerecord/test/cases/associations_test.rb @@ -161,12 +161,15 @@ class AssociationProxyTest < ActiveRecord::TestCase end def test_create_via_association_with_block - post1 = Post.create(:title => "setting body with a block") {|p| p.body = "will work"} - assert_equal post1.body, "will work" - assert_nothing_raised do - post2 = authors(:david).posts.create(:title => "setting body with a block") {|p| p.body = "won't work"} - end - assert_equal post2.body, "won't work" + post = authors(:david).posts.create(:title => "New on Edge") {|p| p.body = "More cool stuff!"} + assert_equal post.title, "New on Edge" + assert_equal post.body, "More cool stuff!" + end + + def test_create_with_bang_via_association_with_block + post = authors(:david).posts.create!(:title => "New on Edge") {|p| p.body = "More cool stuff!"} + assert_equal post.title, "New on Edge" + assert_equal post.body, "More cool stuff!" end def test_failed_reload_returns_nil -- cgit v1.2.3 From 6277fd91133a3566333612857510d74de60d67f4 Mon Sep 17 00:00:00 2001 From: Michael Koziarski Date: Sat, 24 May 2008 18:34:59 +1200 Subject: Fix faulty tests introduced in 8d0b4fa39 --- activerecord/test/cases/associations/has_one_associations_test.rb | 2 +- activerecord/test/cases/reflection_test.rb | 5 +++-- activerecord/test/models/company.rb | 2 +- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/activerecord/test/cases/associations/has_one_associations_test.rb b/activerecord/test/cases/associations/has_one_associations_test.rb index e7d4de8246..abc7ee7e9d 100755 --- a/activerecord/test/cases/associations/has_one_associations_test.rb +++ b/activerecord/test/cases/associations/has_one_associations_test.rb @@ -26,7 +26,7 @@ class HasOneAssociationsTest < ActiveRecord::TestCase def test_with_select assert_equal Firm.find(1).account_with_select.attributes.size, 2 - assert_equal Firm.find(1, :include => :account_with_select).attributes.size, 2 + assert_equal Firm.find(1, :include => :account_with_select).account_with_select.attributes.size, 2 end def test_can_marshal_has_one_association_with_nil_target diff --git a/activerecord/test/cases/reflection_test.rb b/activerecord/test/cases/reflection_test.rb index c8ee40ea09..8b4d232554 100644 --- a/activerecord/test/cases/reflection_test.rb +++ b/activerecord/test/cases/reflection_test.rb @@ -159,9 +159,10 @@ class ReflectionTest < ActiveRecord::TestCase end def test_reflection_of_all_associations - assert_equal 19, Firm.reflect_on_all_associations.size + # 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 3, Firm.reflect_on_all_associations(:has_one).size + assert_equal 4, 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 cc521d2a02..70f83fa8e6 100755 --- a/activerecord/test/models/company.rb +++ b/activerecord/test/models/company.rb @@ -47,7 +47,7 @@ class Firm < Company has_many :readonly_clients, :class_name => 'Client', :readonly => true has_one :account, :foreign_key => "firm_id", :dependent => :destroy - has_one :account_with_select, :foreign_key => "firm_id", :select => "id, firm_id" + 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 -- cgit v1.2.3