From 7fc339059cbace0af8f8dbfb5c68db3f5dffd5a3 Mon Sep 17 00:00:00 2001 From: PikachuEXE Date: Wed, 30 Jan 2013 12:55:37 +0800 Subject: + Add test for auto timestamps update of both old & new parent records --- activerecord/test/cases/timestamp_test.rb | 34 +++++++++++++++++++++++++++++++ activerecord/test/fixtures/toys.yml | 4 ++++ 2 files changed, 38 insertions(+) diff --git a/activerecord/test/cases/timestamp_test.rb b/activerecord/test/cases/timestamp_test.rb index 777a2b70dd..6f983bce75 100644 --- a/activerecord/test/cases/timestamp_test.rb +++ b/activerecord/test/cases/timestamp_test.rb @@ -176,6 +176,40 @@ class TimestampTest < ActiveRecord::TestCase assert_not_equal time, owner.updated_at end + def test_changing_parent_of_a_record_touches_both_new_and_old_parent_record_and_grandparent_record + klass = Class.new(ActiveRecord::Base) do + def self.name; 'Toy'; end + belongs_to :pet, touch: true + end + + toy1 = klass.find(1) + old_pet = toy1.pet + old_owner = old_pet.owner + + toy2 = klass.find(2) + new_pet = toy2.pet + new_owner = new_pet.owner + time = 3.days.ago + + old_pet.update_columns(updated_at: time) + old_owner.update_columns(updated_at: time) + new_pet.update_columns(updated_at: time) + new_owner.update_columns(updated_at: time) + + toy1.pet = new_pet + toy1.save! + + old_pet.reload + old_owner.reload + new_pet.reload + new_owner.reload + + assert_not_equal time, old_pet.updated_at + assert_not_equal time, old_owner.updated_at + assert_not_equal time, new_pet.updated_at + assert_not_equal time, new_owner.updated_at + end + def test_timestamp_attributes_for_create toy = Toy.first assert_equal toy.send(:timestamp_attributes_for_create), [:created_at, :created_on] diff --git a/activerecord/test/fixtures/toys.yml b/activerecord/test/fixtures/toys.yml index 037e335e0a..07ed75e98e 100644 --- a/activerecord/test/fixtures/toys.yml +++ b/activerecord/test/fixtures/toys.yml @@ -2,3 +2,7 @@ bone: toy_id: 1 name: Bone pet_id: 1 +doll: + toy_id: 2 + name: Doll + pet_id: 2 -- cgit v1.2.3 From 8fccbc1ad4fff215c63d17b9321fc69ad17e89dc Mon Sep 17 00:00:00 2001 From: Adam Gamble Date: Thu, 31 Jan 2013 21:54:41 -0600 Subject: Modifies belong_to touch callback to touch old associations also #9091 --- activerecord/CHANGELOG.md | 22 ++++++++++++++++++++++ .../associations/builder/belongs_to.rb | 10 ++++++++++ activerecord/test/cases/timestamp_test.rb | 12 ++---------- 3 files changed, 34 insertions(+), 10 deletions(-) diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index f73fc9d9a3..7dbfad4473 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -447,6 +447,28 @@ *Aaron Stone + Rafael Mendonça França* * `Relation#merge` now only overwrites where values on the LHS of the +======= +* Belongs_to :touch behavior now touches old association when + transitioning to new association + + class Passenger < ActiveRecord::Base + belongs_to :car, touch: true + end + + car_1 = Car.create + car_2 = Car.create + + passenger = Passenger.create :car => car_1 + + passenger.car = car_2 + passenger.save + + Previously only car_2 would be touched. Now both car_1 and car_2 + will be touched. + + *Adam Gamble* + +* Relation#merge now only overwrites where values on the LHS of the merge. Consider: left = Person.where(age: [13, 14, 15]) diff --git a/activerecord/lib/active_record/associations/builder/belongs_to.rb b/activerecord/lib/active_record/associations/builder/belongs_to.rb index 97b1ff18e2..6b479968c8 100644 --- a/activerecord/lib/active_record/associations/builder/belongs_to.rb +++ b/activerecord/lib/active_record/associations/builder/belongs_to.rb @@ -48,6 +48,16 @@ module ActiveRecord::Associations::Builder def belongs_to_touch_after_save_or_destroy_for_#{name} record = #{name} + foreign_key_field = #{reflection.foreign_key} + if changed_attributes.key?(foreign_key_field) + reflection_klass = #{reflection.klass} + old_foreign_id = changed_attributes[foreign_key_field] + old_record = reflection_klass.where(foreign_key_field.to_sym => old_foreign_id).first + if old_record + old_record.touch #{options[:touch].inspect if options[:touch] != true} + end + end + unless record.nil? || record.new_record? record.touch #{options[:touch].inspect if options[:touch] != true} end diff --git a/activerecord/test/cases/timestamp_test.rb b/activerecord/test/cases/timestamp_test.rb index 6f983bce75..0c13bb946a 100644 --- a/activerecord/test/cases/timestamp_test.rb +++ b/activerecord/test/cases/timestamp_test.rb @@ -176,7 +176,7 @@ class TimestampTest < ActiveRecord::TestCase assert_not_equal time, owner.updated_at end - def test_changing_parent_of_a_record_touches_both_new_and_old_parent_record_and_grandparent_record + def test_changing_parent_of_a_record_touches_both_new_and_old_parent_record klass = Class.new(ActiveRecord::Base) do def self.name; 'Toy'; end belongs_to :pet, touch: true @@ -184,30 +184,22 @@ class TimestampTest < ActiveRecord::TestCase toy1 = klass.find(1) old_pet = toy1.pet - old_owner = old_pet.owner toy2 = klass.find(2) new_pet = toy2.pet - new_owner = new_pet.owner time = 3.days.ago old_pet.update_columns(updated_at: time) - old_owner.update_columns(updated_at: time) new_pet.update_columns(updated_at: time) - new_owner.update_columns(updated_at: time) toy1.pet = new_pet toy1.save! old_pet.reload - old_owner.reload new_pet.reload - new_owner.reload - assert_not_equal time, old_pet.updated_at - assert_not_equal time, old_owner.updated_at assert_not_equal time, new_pet.updated_at - assert_not_equal time, new_owner.updated_at + assert_not_equal time, old_pet.updated_at end def test_timestamp_attributes_for_create -- cgit v1.2.3 From 448381593edf0b87e3afd4945bd13650a7483b17 Mon Sep 17 00:00:00 2001 From: Michal Cichra Date: Fri, 29 Mar 2013 10:03:56 +0100 Subject: fix inverse_of association in block of new child This fixes inconsistency when building children of association which has inverse_of set properly. When creating new association object with a block: parent.association.build do |child| child.parent.equal?(parent) # false end So the block the `child.parent` did not point to the same object. But when the object is created it points to same instance: child = parent.association.build child.parent.equal?(parent) # true --- activerecord/CHANGELOG.md | 17 +++++++++++++++++ .../lib/active_record/associations/association.rb | 1 + .../cases/associations/inverse_associations_test.rb | 16 ++++++++++++++++ 3 files changed, 34 insertions(+) diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index 0c8c8c006e..b4f14c3cc8 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -388,6 +388,23 @@ # This will expand the order :name to "authors".name. Author.joins(:books).where('books.published = 1').order(:name) +* Fix associations with `:inverse_of` option when building association + with a block. Inside the block the parent object was different then + after the block. + + Example: + + parent.association.build do |child| + child.parent.equal?(parent) # false + end + + # vs + + child = parent.association.build + child.parent.equal?(parent) # true + + *Michal Cichra* + ## Rails 4.0.0.beta1 (February 25, 2013) ## diff --git a/activerecord/lib/active_record/associations/association.rb b/activerecord/lib/active_record/associations/association.rb index 4c4b0f08e5..6578a9096b 100644 --- a/activerecord/lib/active_record/associations/association.rb +++ b/activerecord/lib/active_record/associations/association.rb @@ -235,6 +235,7 @@ module ActiveRecord skip_assign = [reflection.foreign_key, reflection.type].compact attributes = create_scope.except(*(record.changed - skip_assign)) record.assign_attributes(attributes) + set_inverse_instance(record) end end end diff --git a/activerecord/test/cases/associations/inverse_associations_test.rb b/activerecord/test/cases/associations/inverse_associations_test.rb index c8cad84013..36f1e633ed 100644 --- a/activerecord/test/cases/associations/inverse_associations_test.rb +++ b/activerecord/test/cases/associations/inverse_associations_test.rb @@ -235,6 +235,22 @@ class InverseHasManyTests < ActiveRecord::TestCase assert_equal m.name, i.man.name, "Name of man should be the same after changes to newly-created-child-owned instance" end + def test_parent_instance_should_be_shared_within_create_block_of_new_child + man = Man.first + interest = man.interests.build do |i| + assert i.man.equal?(man), "Man of child should be the same instance as a parent" + end + assert interest.man.equal?(man), "Man of the child should still be the same instance as a parent" + end + + def test_parent_instance_should_be_shared_within_build_block_of_new_child + man = Man.first + interest = man.interests.build do |i| + assert i.man.equal?(man), "Man of child should be the same instance as a parent" + end + assert interest.man.equal?(man), "Man of the child should still be the same instance as a parent" + end + def test_parent_instance_should_be_shared_with_poked_in_child m = men(:gordon) i = Interest.create(:topic => 'Industrial Revolution Re-enactment') -- cgit v1.2.3 From 6303cef42c91639642d38a58112b4921927e2ce1 Mon Sep 17 00:00:00 2001 From: Neeraj Singh Date: Tue, 2 Apr 2013 13:51:30 -0400 Subject: not a relation. it's an arel select manager --- .../associations/join_dependency/join_association.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/activerecord/lib/active_record/associations/join_dependency/join_association.rb b/activerecord/lib/active_record/associations/join_dependency/join_association.rb index 0d3b4dbab1..a332034cb0 100644 --- a/activerecord/lib/active_record/associations/join_dependency/join_association.rb +++ b/activerecord/lib/active_record/associations/join_dependency/join_association.rb @@ -59,7 +59,7 @@ module ActiveRecord end end - def join_to(relation) + def join_to(manager) tables = @tables.dup foreign_table = parent_table foreign_klass = parent.active_record @@ -75,7 +75,7 @@ module ActiveRecord foreign_key = reflection.foreign_key when :has_and_belongs_to_many # Join the join table first... - relation.from(join( + manager.from(join( table, table[reflection.foreign_key]. eq(foreign_table[reflection.active_record_primary_key]) @@ -109,13 +109,13 @@ module ActiveRecord constraint = constraint.and(item.arel.constraints) unless item.arel.constraints.empty? end - relation.from(join(table, constraint)) + manager.from(join(table, constraint)) # The current table in this iteration becomes the foreign table in the next foreign_table, foreign_klass = table, reflection.klass end - relation + manager end def build_constraint(reflection, table, key, foreign_table, foreign_key) -- cgit v1.2.3 From 1bf4088aeb580e8e8353c058dd4caf6acffa1bbc Mon Sep 17 00:00:00 2001 From: Neeraj Singh Date: Tue, 2 Apr 2013 15:08:53 -0400 Subject: show name of the klass that has missing association --- activerecord/lib/active_record/associations/join_dependency.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/activerecord/lib/active_record/associations/join_dependency.rb b/activerecord/lib/active_record/associations/join_dependency.rb index e589959cf2..57fa6a8fc9 100644 --- a/activerecord/lib/active_record/associations/join_dependency.rb +++ b/activerecord/lib/active_record/associations/join_dependency.rb @@ -109,7 +109,7 @@ module ActiveRecord case associations when Symbol, String reflection = parent.reflections[associations.intern] or - raise ConfigurationError, "Association named '#{ associations }' was not found; perhaps you misspelled it?" + raise ConfigurationError, "Association named '#{ associations }' was not found on #{ parent.active_record.name }; perhaps you misspelled it?" unless join_association = find_join_association(reflection, parent) @reflections << reflection join_association = build_join_association(reflection, parent) -- cgit v1.2.3 From 22417c2112336b14a21a91819537bd951c15ee60 Mon Sep 17 00:00:00 2001 From: James Golick Date: Tue, 2 Apr 2013 15:15:22 -0700 Subject: Refactor CollectionProxy#scope to avoid calling #extend. --- activerecord/lib/active_record/associations/collection_proxy.rb | 6 ++---- activerecord/lib/active_record/relation.rb | 2 +- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/activerecord/lib/active_record/associations/collection_proxy.rb b/activerecord/lib/active_record/associations/collection_proxy.rb index c2add32aa6..c01ec36179 100644 --- a/activerecord/lib/active_record/associations/collection_proxy.rb +++ b/activerecord/lib/active_record/associations/collection_proxy.rb @@ -848,10 +848,8 @@ module ActiveRecord # Returns a Relation object for the records in this association def scope - association = @association - - @association.scope.extending! do - define_method(:proxy_association) { association } + @association.scope.tap do |scope| + scope.proxy_association = @association end end diff --git a/activerecord/lib/active_record/relation.rb b/activerecord/lib/active_record/relation.rb index c7b6c715f5..56462d355b 100644 --- a/activerecord/lib/active_record/relation.rb +++ b/activerecord/lib/active_record/relation.rb @@ -17,7 +17,7 @@ module ActiveRecord include FinderMethods, Calculations, SpawnMethods, QueryMethods, Batches, Explain, Delegation attr_reader :table, :klass, :loaded - attr_accessor :default_scoped + attr_accessor :default_scoped, :proxy_association alias :model :klass alias :loaded? :loaded alias :default_scoped? :default_scoped -- cgit v1.2.3 From 4bb26dd7b298b08f53c7e157ddc19dc7febcf37e Mon Sep 17 00:00:00 2001 From: Sam Pohlenz Date: Wed, 3 Apr 2013 14:42:48 +1030 Subject: Add block support for the helper --- actionpack/CHANGELOG.md | 4 ++++ actionpack/lib/action_view/helpers/url_helper.rb | 21 ++++++++++++++++----- actionpack/test/template/url_helper_test.rb | 10 ++++++++++ 3 files changed, 30 insertions(+), 5 deletions(-) diff --git a/actionpack/CHANGELOG.md b/actionpack/CHANGELOG.md index 798c34e87c..bb84a83208 100644 --- a/actionpack/CHANGELOG.md +++ b/actionpack/CHANGELOG.md @@ -1,5 +1,9 @@ ## Rails 4.0.0 (unreleased) ## +* Add block support for the `mail_to` helper, similar to the `link_to` helper. + + *Sam Pohlenz* + * Automatically configure cookie-based sessions to be encrypted if `secret_key_base` is set, falling back to signed if only `secret_token` is set. Automatically upgrade existing signed cookie-based sessions from diff --git a/actionpack/lib/action_view/helpers/url_helper.rb b/actionpack/lib/action_view/helpers/url_helper.rb index 775d93ed39..a1468b6e90 100644 --- a/actionpack/lib/action_view/helpers/url_helper.rb +++ b/actionpack/lib/action_view/helpers/url_helper.rb @@ -439,18 +439,29 @@ module ActionView # mail_to "me@domain.com", "My email", cc: "ccaddress@domain.com", # subject: "This is an example email" # # => My email - def mail_to(email_address, name = nil, html_options = {}) - email_address = ERB::Util.html_escape(email_address) - + # + # You can use a block as well if your link target is hard to fit into the name parameter. ERB example: + # + # <%= mail_to "me@domain.com" do %> + # Email me: me@domain.com + # <% end %> + # # => + # Email me: me@domain.com + # + def mail_to(email_address, name = nil, html_options = {}, &block) + html_options, name = name, nil if block_given? + html_options ||= {} html_options.stringify_keys! + email_address = ERB::Util.html_escape(email_address) + extras = %w{ cc bcc body subject }.map { |item| option = html_options.delete(item) || next "#{item}=#{Rack::Utils.escape_path(option)}" }.compact extras = extras.empty? ? '' : '?' + ERB::Util.html_escape(extras.join('&')) - - content_tag "a", name || email_address.html_safe, html_options.merge("href" => "mailto:#{email_address}#{extras}".html_safe) + + content_tag(:a, name || email_address.html_safe, html_options.merge("href" => "mailto:#{email_address}#{extras}".html_safe), &block) end # True if the current request URI was generated by the given +options+. diff --git a/actionpack/test/template/url_helper_test.rb b/actionpack/test/template/url_helper_test.rb index 88c4b72ad7..e7b04d40c0 100644 --- a/actionpack/test/template/url_helper_test.rb +++ b/actionpack/test/template/url_helper_test.rb @@ -538,6 +538,16 @@ class UrlHelperTest < ActiveSupport::TestCase assert mail_to("david@loudthinking.com").html_safe? end + def test_mail_to_with_block + assert_dom_equal %{Email me}, + mail_to('me@example.com') { content_tag(:span, 'Email me') } + end + + def test_link_tag_with_block_and_options + assert_dom_equal %{Email me}, + mail_to('me@example.com', cc: "ccaddress@example.com", class: "special") { content_tag(:span, 'Email me') } + end + def protect_against_forgery? self.request_forgery end -- cgit v1.2.3 From 1e703f5d41ee6b1d52aababa04ed9bb094ad2533 Mon Sep 17 00:00:00 2001 From: Carlos Antonio da Silva Date: Wed, 3 Apr 2013 14:04:33 -0300 Subject: Move changelog entry to the top and fix conflict [ci skip] --- activerecord/CHANGELOG.md | 42 ++++++++++++++++++++---------------------- 1 file changed, 20 insertions(+), 22 deletions(-) diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index 438064a403..cd6042c329 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,5 +1,25 @@ ## Rails 4.0.0 (unreleased) ## +* `belongs_to :touch` behavior now touches old association when + transitioning to new association. + + class Passenger < ActiveRecord::Base + belongs_to :car, touch: true + end + + car_1 = Car.create + car_2 = Car.create + + passenger = Passenger.create car: car_1 + + passenger.car = car_2 + passenger.save + + Previously only car_2 would be touched. Now both car_1 and car_2 + will be touched. + + *Adam Gamble* + * Extract and deprecate Firebird / Sqlserver / Oracle database tasks, because These tasks should be supported by 3rd-party adapter. @@ -598,28 +618,6 @@ *Aaron Stone + Rafael Mendonça França* * `Relation#merge` now only overwrites where values on the LHS of the -======= -* Belongs_to :touch behavior now touches old association when - transitioning to new association - - class Passenger < ActiveRecord::Base - belongs_to :car, touch: true - end - - car_1 = Car.create - car_2 = Car.create - - passenger = Passenger.create :car => car_1 - - passenger.car = car_2 - passenger.save - - Previously only car_2 would be touched. Now both car_1 and car_2 - will be touched. - - *Adam Gamble* - -* Relation#merge now only overwrites where values on the LHS of the merge. Consider: left = Person.where(age: [13, 14, 15]) -- cgit v1.2.3 From e632d035db36092236f01ab1553405de65245d64 Mon Sep 17 00:00:00 2001 From: Carlos Antonio da Silva Date: Wed, 3 Apr 2013 14:48:57 -0300 Subject: Add missing require to inheritance test --- activerecord/test/cases/persistence_test.rb | 2 +- activerecord/test/models/pet.rb | 2 -- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/activerecord/test/cases/persistence_test.rb b/activerecord/test/cases/persistence_test.rb index 572431ee87..db3bb56f1f 100644 --- a/activerecord/test/cases/persistence_test.rb +++ b/activerecord/test/cases/persistence_test.rb @@ -12,13 +12,13 @@ require 'models/minimalistic' require 'models/warehouse_thing' require 'models/parrot' require 'models/minivan' +require 'models/owner' require 'models/person' require 'models/pet' require 'models/toy' require 'rexml/document' class PersistencesTest < ActiveRecord::TestCase - fixtures :topics, :companies, :developers, :projects, :computers, :accounts, :minimalistics, 'warehouse-things', :authors, :categorizations, :categories, :posts, :minivans, :pets, :toys # Oracle UPDATE does not support ORDER BY diff --git a/activerecord/test/models/pet.rb b/activerecord/test/models/pet.rb index 3cd5bceed5..f7970d7aab 100644 --- a/activerecord/test/models/pet.rb +++ b/activerecord/test/models/pet.rb @@ -1,5 +1,4 @@ class Pet < ActiveRecord::Base - attr_accessor :current_user self.primary_key = :pet_id @@ -13,5 +12,4 @@ class Pet < ActiveRecord::Base after_destroy do |record| Pet.after_destroy_output = record.current_user end - end -- cgit v1.2.3 From 2282964de77eae623284c2cd47cb2f21045b290f Mon Sep 17 00:00:00 2001 From: Vipul A M Date: Wed, 3 Apr 2013 22:49:30 +0530 Subject: Fix some railties test warnings --- activesupport/lib/active_support/log_subscriber.rb | 5 ++--- railties/test/generators/plugin_new_generator_test.rb | 2 +- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/activesupport/lib/active_support/log_subscriber.rb b/activesupport/lib/active_support/log_subscriber.rb index 21a04a9152..c4b64bd1a6 100644 --- a/activesupport/lib/active_support/log_subscriber.rb +++ b/activesupport/lib/active_support/log_subscriber.rb @@ -53,10 +53,9 @@ module ActiveSupport class << self def logger - if defined?(Rails) && Rails.respond_to?(:logger) - @logger ||= Rails.logger + @logger ||= if defined?(Rails) && Rails.respond_to?(:logger) + Rails.logger end - @logger end attr_writer :logger diff --git a/railties/test/generators/plugin_new_generator_test.rb b/railties/test/generators/plugin_new_generator_test.rb index 48425cbf81..ac71fb5884 100644 --- a/railties/test/generators/plugin_new_generator_test.rb +++ b/railties/test/generators/plugin_new_generator_test.rb @@ -124,7 +124,7 @@ class PluginNewGeneratorTest < Rails::Generators::TestCase run_generator [destination_root, "--skip_active_record"] assert_no_file "test/dummy/config/database.yml" assert_file "test/test_helper.rb" do |contents| - assert_no_match /ActiveRecord/, contents + assert_no_match(/ActiveRecord/, contents) end end -- cgit v1.2.3 From 0875678edc48daf9eae4d13a0e06fbf300cf8169 Mon Sep 17 00:00:00 2001 From: Prathamesh Sonpatki Date: Thu, 4 Apr 2013 00:42:50 +0530 Subject: Fixed Typo --- activesupport/lib/active_support/core_ext/date_and_time/calculations.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/activesupport/lib/active_support/core_ext/date_and_time/calculations.rb b/activesupport/lib/active_support/core_ext/date_and_time/calculations.rb index 1f78b9eb5a..5b89ace66b 100644 --- a/activesupport/lib/active_support/core_ext/date_and_time/calculations.rb +++ b/activesupport/lib/active_support/core_ext/date_and_time/calculations.rb @@ -93,7 +93,7 @@ module DateAndTime # Returns a new date/time at the end of the quarter. # Example: 31st March, 30th June, 30th September. - # DateTIme objects will have a time set to 23:59:59. + # DateTime objects will have a time set to 23:59:59. def end_of_quarter last_quarter_month = [3, 6, 9, 12].detect { |m| m >= month } beginning_of_month.change(:month => last_quarter_month).end_of_month -- cgit v1.2.3 From db113d90caabf7a5d16aef4b61d41155eb5b86c6 Mon Sep 17 00:00:00 2001 From: Carlos Antonio da Silva Date: Wed, 3 Apr 2013 15:42:25 -0300 Subject: Remove duplicated require from AR base test --- activerecord/test/cases/base_test.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/activerecord/test/cases/base_test.rb b/activerecord/test/cases/base_test.rb index 08223902c7..acf003bd80 100644 --- a/activerecord/test/cases/base_test.rb +++ b/activerecord/test/cases/base_test.rb @@ -21,7 +21,6 @@ require 'models/parrot' require 'models/person' require 'models/edge' require 'models/joke' -require 'models/bulb' require 'models/bird' require 'models/car' require 'models/bulb' -- cgit v1.2.3 From 4da888f38452d9727c77fd91cd7a267e4e1eec42 Mon Sep 17 00:00:00 2001 From: Vipul A M Date: Thu, 4 Apr 2013 01:02:08 +0530 Subject: add require to suppress warning; remove variable --- activesupport/lib/active_support/core_ext/big_decimal/conversions.rb | 1 + railties/lib/rails/generators/named_base.rb | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/activesupport/lib/active_support/core_ext/big_decimal/conversions.rb b/activesupport/lib/active_support/core_ext/big_decimal/conversions.rb index 5dc5710c53..39b8cea807 100644 --- a/activesupport/lib/active_support/core_ext/big_decimal/conversions.rb +++ b/activesupport/lib/active_support/core_ext/big_decimal/conversions.rb @@ -1,4 +1,5 @@ require 'bigdecimal' +require 'bigdecimal/util' require 'yaml' class BigDecimal diff --git a/railties/lib/rails/generators/named_base.rb b/railties/lib/rails/generators/named_base.rb index 8b4f52bb3b..d891ba1215 100644 --- a/railties/lib/rails/generators/named_base.rb +++ b/railties/lib/rails/generators/named_base.rb @@ -40,7 +40,7 @@ module Rails def indent(content, multiplier = 2) spaces = " " * multiplier - content = content.each_line.map {|line| line.blank? ? line : "#{spaces}#{line}" }.join + content.each_line.map {|line| line.blank? ? line : "#{spaces}#{line}" }.join end def wrap_with_namespace(content) -- cgit v1.2.3 From f5644b0cd692715774aa04e1fd692597ca68e5ec Mon Sep 17 00:00:00 2001 From: Carlos Antonio da Silva Date: Wed, 3 Apr 2013 20:56:12 -0300 Subject: Fix typo in view name Introduced in 2c22376fe04b89e8f34620139720b85a85ce3428 --- actionpack/test/fixtures/test/change_priority.html.erb | 2 ++ actionpack/test/fixtures/test/change_priorty.html.erb | 2 -- actionpack/test/template/render_test.rb | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) create mode 100644 actionpack/test/fixtures/test/change_priority.html.erb delete mode 100644 actionpack/test/fixtures/test/change_priorty.html.erb diff --git a/actionpack/test/fixtures/test/change_priority.html.erb b/actionpack/test/fixtures/test/change_priority.html.erb new file mode 100644 index 0000000000..5618977d05 --- /dev/null +++ b/actionpack/test/fixtures/test/change_priority.html.erb @@ -0,0 +1,2 @@ +<%= render :partial => "test/json_change_priority", formats: :json %> +HTML Template, but <%= render :partial => "test/changing_priority" %> partial \ No newline at end of file diff --git a/actionpack/test/fixtures/test/change_priorty.html.erb b/actionpack/test/fixtures/test/change_priorty.html.erb deleted file mode 100644 index 5618977d05..0000000000 --- a/actionpack/test/fixtures/test/change_priorty.html.erb +++ /dev/null @@ -1,2 +0,0 @@ -<%= render :partial => "test/json_change_priority", formats: :json %> -HTML Template, but <%= render :partial => "test/changing_priority" %> partial \ No newline at end of file diff --git a/actionpack/test/template/render_test.rb b/actionpack/test/template/render_test.rb index 8111e58527..2237d747be 100644 --- a/actionpack/test/template/render_test.rb +++ b/actionpack/test/template/render_test.rb @@ -61,7 +61,7 @@ module RenderTestCases def test_render_partial_use_last_prepended_format_for_partials_with_the_same_names @view.lookup_context.formats = [:html] - assert_equal "\nHTML Template, but JSON partial", @view.render(:template => "test/change_priorty") + assert_equal "\nHTML Template, but JSON partial", @view.render(:template => "test/change_priority") end def test_render_template_with_a_missing_partial_of_another_format -- cgit v1.2.3 From dcc75b05d441ea379e84f47d2d4adee12e9f5ddd Mon Sep 17 00:00:00 2001 From: Vipul A M Date: Thu, 4 Apr 2013 10:21:43 +0530 Subject: fix warnings in railties test --- railties/test/application/rake_test.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/railties/test/application/rake_test.rb b/railties/test/application/rake_test.rb index a9e0e1bcb7..4b8e813105 100644 --- a/railties/test/application/rake_test.rb +++ b/railties/test/application/rake_test.rb @@ -131,11 +131,11 @@ module ApplicationTests %w(run recent uncommitted models helpers units controllers functionals integration).each do |test_suit_name| output = Dir.chdir(app_path) { `rake test:#{test_suit_name} 2>&1` } - assert_match /DEPRECATION WARNING: `rake test:#{test_suit_name}` is deprecated/, output + assert_match(/DEPRECATION WARNING: `rake test:#{test_suit_name}` is deprecated/, output) end - assert_match /DEPRECATION WARNING: `rake test:single` is deprecated/, - Dir.chdir(app_path) { `rake test:single TEST=test/models/user_test.rb 2>&1` } + assert_match(/DEPRECATION WARNING: `rake test:single` is deprecated/, + Dir.chdir(app_path) { `rake test:single TEST=test/models/user_test.rb 2>&1` }) end def test_rake_routes_calls_the_route_inspector -- cgit v1.2.3 From e87a513f4288634cdbd69842717162d2469c66da Mon Sep 17 00:00:00 2001 From: Neeraj Singh Date: Wed, 3 Apr 2013 11:53:02 -0400 Subject: expanded rdoc on source_reflection method --- activerecord/lib/active_record/reflection.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/activerecord/lib/active_record/reflection.rb b/activerecord/lib/active_record/reflection.rb index 0995750ecd..2026511e38 100644 --- a/activerecord/lib/active_record/reflection.rb +++ b/activerecord/lib/active_record/reflection.rb @@ -401,6 +401,16 @@ module ActiveRecord # has_many :tags, through: :taggings # end # + # class Tagging < ActiveRecord::Base + # belongs_to :post + # belongs_to :tag + # end + # + # tags_reflection = Post.reflect_on_association(:tags) + # + # taggings_reflection = tags_reflection.source_reflection + # #=> + # def source_reflection @source_reflection ||= source_reflection_names.collect { |name| through_reflection.klass.reflect_on_association(name) }.compact.first end -- cgit v1.2.3 From f73140fd4773a1de47017eb0d053a4164c9f3747 Mon Sep 17 00:00:00 2001 From: Anupam Choudhury Date: Wed, 3 Apr 2013 20:26:06 +0530 Subject: Removed unused comma after loop variable --- activesupport/test/core_ext/enumerable_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/activesupport/test/core_ext/enumerable_test.rb b/activesupport/test/core_ext/enumerable_test.rb index 0a1abac767..161e9ba1c3 100644 --- a/activesupport/test/core_ext/enumerable_test.rb +++ b/activesupport/test/core_ext/enumerable_test.rb @@ -24,7 +24,7 @@ class EnumerableTests < ActiveSupport::TestCase def test_group_by names = %w(marcel sam david jeremy) klass = Struct.new(:name) - objects = (1..50).inject([]) do |people,| + objects = (1..50).inject([]) do |people| p = klass.new p.name = names.sort_by { rand }.first people << p -- cgit v1.2.3 From ecdd432f5746a525a11b872c73308b9b20dd88ad Mon Sep 17 00:00:00 2001 From: Anupam Choudhury Date: Thu, 4 Apr 2013 20:35:11 +0530 Subject: Replaced inject with map --- activesupport/test/core_ext/enumerable_test.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/activesupport/test/core_ext/enumerable_test.rb b/activesupport/test/core_ext/enumerable_test.rb index 161e9ba1c3..a00b2dcf35 100644 --- a/activesupport/test/core_ext/enumerable_test.rb +++ b/activesupport/test/core_ext/enumerable_test.rb @@ -24,10 +24,10 @@ class EnumerableTests < ActiveSupport::TestCase def test_group_by names = %w(marcel sam david jeremy) klass = Struct.new(:name) - objects = (1..50).inject([]) do |people| + objects = (1..50).map do p = klass.new p.name = names.sort_by { rand }.first - people << p + p end enum = GenericEnumerable.new(objects) -- cgit v1.2.3 From 363c08fa6033a53c1fed18288921c98e70484064 Mon Sep 17 00:00:00 2001 From: Carlos Antonio da Silva Date: Thu, 4 Apr 2013 12:36:17 -0300 Subject: Further simplify enumerable group_by test --- activesupport/test/core_ext/enumerable_test.rb | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/activesupport/test/core_ext/enumerable_test.rb b/activesupport/test/core_ext/enumerable_test.rb index a00b2dcf35..6781e3c20e 100644 --- a/activesupport/test/core_ext/enumerable_test.rb +++ b/activesupport/test/core_ext/enumerable_test.rb @@ -25,9 +25,7 @@ class EnumerableTests < ActiveSupport::TestCase names = %w(marcel sam david jeremy) klass = Struct.new(:name) objects = (1..50).map do - p = klass.new - p.name = names.sort_by { rand }.first - p + klass.new names.sample end enum = GenericEnumerable.new(objects) -- cgit v1.2.3 From 113e39625ba533168d20a0e7c0019ca2e2ac7806 Mon Sep 17 00:00:00 2001 From: Vipul A M Date: Thu, 4 Apr 2013 23:23:46 +0530 Subject: fix private attribute warning --- railties/test/commands/console_test.rb | 5 +++-- railties/test/commands/dbconsole_test.rb | 4 +++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/railties/test/commands/console_test.rb b/railties/test/commands/console_test.rb index 3c784b43be..a34beaedb3 100644 --- a/railties/test/commands/console_test.rb +++ b/railties/test/commands/console_test.rb @@ -114,9 +114,10 @@ class Rails::ConsoleTest < ActiveSupport::TestCase assert_match('dev', options[:environment]) end - private - attr_reader :output + private :output + + private def start(argv = []) rails_console = Rails::Console.new(app, parse_arguments(argv)) diff --git a/railties/test/commands/dbconsole_test.rb b/railties/test/commands/dbconsole_test.rb index 38fe8ca544..edb92b3aa2 100644 --- a/railties/test/commands/dbconsole_test.rb +++ b/railties/test/commands/dbconsole_test.rb @@ -172,8 +172,10 @@ class Rails::DBConsoleTest < ActiveSupport::TestCase assert_match(/Usage:.*dbconsole/, stdout) end - private attr_reader :aborted, :output + private :aborted, :output + + private def dbconsole @dbconsole ||= Rails::DBConsole.new(nil) -- cgit v1.2.3 From e377228342e0185ef77ad8a58a8df4ece1683e80 Mon Sep 17 00:00:00 2001 From: Ben McRedmond Date: Wed, 2 Jan 2013 11:53:37 +0000 Subject: Fixes skipping object callback filters This allows you to skip callbacks that are defined by objects, e.g. for `ActionController`: skip_after_filter MySpecialFilter Previously this didn't work due to a bug in how Rails compared callbacks in `Callback#matches?`. When a callback is compiled, if it's an object filter (i.e. not a method, proc, etc.), `Callback` now defines a method on `@klass` that is derived from the class name rather than `@callback_id`. So, when `skip_callback` tries to find the appropriate callback to remove, `Callback` can regenerate the method name for the filter object and return the correct value for `Callback#matches?`. --- activesupport/CHANGELOG.md | 3 +++ activesupport/lib/active_support/callbacks.rb | 14 +++++++++++++- activesupport/test/callbacks_test.rb | 17 +++++++++++++++++ 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md index 31395d6da2..5a7cddb079 100644 --- a/activesupport/CHANGELOG.md +++ b/activesupport/CHANGELOG.md @@ -14,6 +14,9 @@ *Charles Jones* +* Fix skipping of filters defined by objects in `ActiveSupport::Callbacks::Callback`. + + *Ben McRedmond* ## Rails 4.0.0.beta1 (February 25, 2013) ## diff --git a/activesupport/lib/active_support/callbacks.rb b/activesupport/lib/active_support/callbacks.rb index 6c0cae71ed..4a3636eb56 100644 --- a/activesupport/lib/active_support/callbacks.rb +++ b/activesupport/lib/active_support/callbacks.rb @@ -131,6 +131,7 @@ module ActiveSupport end def matches?(_kind, _filter) + _filter = _method_name_for_object_filter(_kind, _filter) if @_is_object_filter @kind == _kind && @filter == _filter end @@ -234,6 +235,14 @@ module ActiveSupport @compiled_options = conditions.flatten.join(" && ") end + def _method_name_for_object_filter(kind, filter) + class_name = filter.kind_of?(Class) ? filter.to_s : filter.class.to_s + class_name.gsub!(/<|>|#/, '') + class_name.gsub!(/\/|:/, "_") + + "_callback_#{kind}_#{class_name}" + end + # Filters support: # # Arrays:: Used in conditions. This is used to specify @@ -255,6 +264,8 @@ module ActiveSupport # a method is created that calls the before_foo method # on the object. def _compile_filter(filter) + @_is_object_filter = false + case filter when Array filter.map {|f| _compile_filter(f)} @@ -269,7 +280,8 @@ module ActiveSupport method_name << (filter.arity == 1 ? "(self)" : " self, Proc.new ") else - method_name = "_callback_#{@kind}_#{next_id}" + method_name = _method_name_for_object_filter(kind, filter) + @_is_object_filter = true @klass.send(:define_method, "#{method_name}_object") { filter } _normalize_legacy_filter(kind, filter) diff --git a/activesupport/test/callbacks_test.rb b/activesupport/test/callbacks_test.rb index 13f2e3cdaf..5afc2094e8 100644 --- a/activesupport/test/callbacks_test.rb +++ b/activesupport/test/callbacks_test.rb @@ -66,6 +66,16 @@ module CallbacksTest end end + class CallbackClass + def self.before(model) + model.history << [:before_save, :class] + end + + def self.after(model) + model.history << [:after_save, :class] + end + end + class Person < Record [:before_save, :after_save].each do |callback_method| callback_method_sym = callback_method.to_sym @@ -73,6 +83,7 @@ module CallbacksTest send(callback_method, callback_string(callback_method_sym)) send(callback_method, callback_proc(callback_method_sym)) send(callback_method, callback_object(callback_method_sym.to_s.gsub(/_save/, ''))) + send(callback_method, CallbackClass) send(callback_method) { |model| model.history << [callback_method_sym, :block] } end @@ -86,6 +97,7 @@ module CallbacksTest skip_callback :save, :after, :before_save_method, :unless => :yes skip_callback :save, :after, :before_save_method, :if => :no skip_callback :save, :before, :before_save_method, :unless => :no + skip_callback :save, :before, CallbackClass , :if => :yes def yes; true; end def no; false; end end @@ -430,6 +442,7 @@ module CallbacksTest [:before_save, :object], [:before_save, :block], [:after_save, :block], + [:after_save, :class], [:after_save, :object], [:after_save, :proc], [:after_save, :string], @@ -449,8 +462,10 @@ module CallbacksTest [:before_save, :string], [:before_save, :proc], [:before_save, :object], + [:before_save, :class], [:before_save, :block], [:after_save, :block], + [:after_save, :class], [:after_save, :object], [:after_save, :proc], [:after_save, :string], @@ -715,8 +730,10 @@ module CallbacksTest [:before_save, :string], [:before_save, :proc], [:before_save, :object], + [:before_save, :class], [:before_save, :block], [:after_save, :block], + [:after_save, :class], [:after_save, :object], [:after_save, :proc], [:after_save, :string], -- cgit v1.2.3 From 11c1a5a1f85971c8e46defd614ea80c979de6bb8 Mon Sep 17 00:00:00 2001 From: Jason King Date: Thu, 4 Apr 2013 11:52:14 -0700 Subject: Model generator USAGE doc improved * Added information about syntax for precision/scale of decimals * Removed incorrect information about being able to set `default` * Added more examples of usage --- railties/lib/rails/generators/rails/model/USAGE | 26 +++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/railties/lib/rails/generators/rails/model/USAGE b/railties/lib/rails/generators/rails/model/USAGE index 6574200fbf..1998a392aa 100644 --- a/railties/lib/rails/generators/rails/model/USAGE +++ b/railties/lib/rails/generators/rails/model/USAGE @@ -52,20 +52,26 @@ Available field types: `rails generate model product supplier:references{polymorphic}` - You can also specify some options just after the field type. You can use the - following options: + For integer, string, text and binary fields an integer in curly braces will + be set as the limit: - limit Set the maximum size of the field giving a number between curly braces - default Set a default value for the field - precision Defines the precision for the decimal fields - scale Defines the scale for the decimal fields - uniq Defines the field values as unique - index Will add an index on the field + `rails generate model user pseudo:string{30}` - Examples: + For decimal two integers separated by a comma in curly braces will be used + for precision and scale: + + `rails generate model product price:decimal{10,2}` + + You can add a `:uniq` or `:index` suffix for unique or standard indexes + respectively: - `rails generate model user pseudo:string{30}` `rails generate model user pseudo:string:uniq` + `rails generate model user pseudo:string:index` + + You can combine any single curly brace option with the index options: + + `rails generate model user username:string{30}:uniq` + `rails generate model product supplier:references{polymorphic}:index` Examples: -- cgit v1.2.3 From 812469943cc6eddc132d137a0d5929b2c5d0346f Mon Sep 17 00:00:00 2001 From: Neeraj Singh Date: Wed, 3 Apr 2013 23:39:09 -0400 Subject: has_many through obeys order on through association fixes #10016 --- activerecord/CHANGELOG.md | 5 +++++ activerecord/lib/active_record/associations/association_scope.rb | 1 + activerecord/test/cases/associations/eager_test.rb | 2 +- .../test/cases/associations/has_many_through_associations_test.rb | 8 +++++++- activerecord/test/fixtures/pets.yml | 5 +++++ activerecord/test/fixtures/toys.yml | 6 ++++++ activerecord/test/models/owner.rb | 2 +- 7 files changed, 26 insertions(+), 3 deletions(-) diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index cd6042c329..0dcc7a4819 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,5 +1,10 @@ ## Rails 4.0.0 (unreleased) ## +* `has_many` using `:through` now obeys the order clause mentioned in + through association. Fixes #10016. + + *Neeraj Singh* + * `belongs_to :touch` behavior now touches old association when transitioning to new association. diff --git a/activerecord/lib/active_record/associations/association_scope.rb b/activerecord/lib/active_record/associations/association_scope.rb index a9525436fb..b62a16ea3f 100644 --- a/activerecord/lib/active_record/associations/association_scope.rb +++ b/activerecord/lib/active_record/associations/association_scope.rb @@ -101,6 +101,7 @@ module ActiveRecord scope.includes! item.includes_values scope.where_values += item.where_values + scope.order_values += (item.order_values - scope.order_values) end end diff --git a/activerecord/test/cases/associations/eager_test.rb b/activerecord/test/cases/associations/eager_test.rb index d6850215b5..4aa6567d85 100644 --- a/activerecord/test/cases/associations/eager_test.rb +++ b/activerecord/test/cases/associations/eager_test.rb @@ -302,7 +302,7 @@ class EagerAssociationTest < ActiveRecord::TestCase def test_eager_association_loading_with_belongs_to_and_foreign_keys pets = Pet.all.merge!(:includes => :owner).to_a - assert_equal 3, pets.length + assert_equal 4, pets.length end def test_eager_association_loading_with_belongs_to 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 67d18f313a..70c6b489aa 100644 --- a/activerecord/test/cases/associations/has_many_through_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_through_associations_test.rb @@ -583,7 +583,7 @@ class HasManyThroughAssociationsTest < ActiveRecord::TestCase end def test_has_many_association_through_a_has_many_association_with_nonstandard_primary_keys - assert_equal 1, owners(:blackbeard).toys.count + assert_equal 2, owners(:blackbeard).toys.count end def test_find_on_has_many_association_collection_with_include_and_conditions @@ -882,6 +882,12 @@ class HasManyThroughAssociationsTest < ActiveRecord::TestCase assert_equal [tags(:general)], post.reload.tags end + def test_has_many_through_obeys_order_on_through_association + owner = owners(:blackbeard) + assert owner.toys.to_sql.include?("pets.name desc") + assert_equal ["parrot", "bulbul"], owner.toys.map { |r| r.pet.name } + end + test "has many through associations on new records use null relations" do person = Person.new diff --git a/activerecord/test/fixtures/pets.yml b/activerecord/test/fixtures/pets.yml index a1601a53f0..2ec4f53e6d 100644 --- a/activerecord/test/fixtures/pets.yml +++ b/activerecord/test/fixtures/pets.yml @@ -12,3 +12,8 @@ mochi: pet_id: 3 name: mochi owner_id: 2 + +bulbul: + pet_id: 4 + name: bulbul + owner_id: 1 diff --git a/activerecord/test/fixtures/toys.yml b/activerecord/test/fixtures/toys.yml index 07ed75e98e..ae9044ec62 100644 --- a/activerecord/test/fixtures/toys.yml +++ b/activerecord/test/fixtures/toys.yml @@ -2,7 +2,13 @@ bone: toy_id: 1 name: Bone pet_id: 1 + doll: toy_id: 2 name: Doll pet_id: 2 + +bulbuli: + toy_id: 3 + name: Bulbuli + pet_id: 4 diff --git a/activerecord/test/models/owner.rb b/activerecord/test/models/owner.rb index fea55f4535..1c7ed4aa3e 100644 --- a/activerecord/test/models/owner.rb +++ b/activerecord/test/models/owner.rb @@ -1,5 +1,5 @@ class Owner < ActiveRecord::Base self.primary_key = :owner_id - has_many :pets + has_many :pets, -> { order 'pets.name desc' } has_many :toys, :through => :pets end -- cgit v1.2.3 From 98e42bc37e866508a671b569d905852c2e6ae17e Mon Sep 17 00:00:00 2001 From: Neeraj Singh Date: Thu, 4 Apr 2013 16:46:04 -0400 Subject: expanded rdoc for chain and reflection_names --- activerecord/lib/active_record/reflection.rb | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/activerecord/lib/active_record/reflection.rb b/activerecord/lib/active_record/reflection.rb index 2026511e38..3eee77e653 100644 --- a/activerecord/lib/active_record/reflection.rb +++ b/activerecord/lib/active_record/reflection.rb @@ -436,6 +436,17 @@ module ActiveRecord # The chain is built by recursively calling #chain on the source reflection and the through # reflection. The base case for the recursion is a normal association, which just returns # [self] as its #chain. + # + # class Post < ActiveRecord::Base + # has_many :taggings + # has_many :tags, through: :taggings + # end + # + # tags_reflection = Post.reflect_on_association(:tags) + # tags_reflection.chain + # #=> [:taggings}, @active_record=Post>, + # ] + # def chain @chain ||= begin chain = source_reflection.chain + through_reflection.chain @@ -506,9 +517,16 @@ module ActiveRecord source_reflection.options[:primary_key] || primary_key(klass || self.klass) end - # Gets an array of possible :through source reflection names: + # Gets an array of possible :through source reflection names in both singular and plural form. # - # [:singularized, :pluralized] + # class Post < ActiveRecord::Base + # has_many :taggings + # has_many :tags, through: :taggings + # end + # + # tags_reflection = Post.reflect_on_association(:tags) + # tags_reflection.source_reflection_names + # #=> [:tag, :tags] # def source_reflection_names @source_reflection_names ||= (options[:source] ? [options[:source]] : [name.to_s.singularize, name]).collect { |n| n.to_sym } -- cgit v1.2.3 From 02acd95d5700ea868c34f5d260882fda3cc836d3 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Wed, 3 Apr 2013 20:02:09 -0400 Subject: stop depending on callbacks --- activerecord/lib/active_record/fixtures.rb | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/activerecord/lib/active_record/fixtures.rb b/activerecord/lib/active_record/fixtures.rb index c26fc76515..45dc26f0ed 100644 --- a/activerecord/lib/active_record/fixtures.rb +++ b/activerecord/lib/active_record/fixtures.rb @@ -708,11 +708,18 @@ module ActiveRecord module TestFixtures extend ActiveSupport::Concern - included do - setup :setup_fixtures - teardown :teardown_fixtures + def before_setup + setup_fixtures + super + end + + def after_teardown + super + teardown_fixtures + end - class_attribute :fixture_path + included do + class_attribute :fixture_path, :instance_writer => false class_attribute :fixture_table_names class_attribute :fixture_class_names class_attribute :use_transactional_fixtures @@ -765,8 +772,7 @@ module ActiveRecord def try_to_load_dependency(file_name) require_dependency file_name rescue LoadError => e - # Let's hope the developer has included it himself - + # Let's hope the developer has included it # Let's warn in case this is a subdependency, otherwise # subdependency error messages are totally cryptic if ActiveRecord::Base.logger -- cgit v1.2.3 From 49a0f554a66ed427df2ab9942e64252b56b60c27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Thu, 4 Apr 2013 18:19:55 -0300 Subject: Revert "Merge pull request #10034 from benofsky/fix_skipping_object_callback_filters" This reverts commit c79c6980647eb76bfa52178711fb04ba7e9d403b, reversing changes made to ba4c27479add60b783a0e623c8a5d176c1dc9043. This broke all the tests. See https://travis-ci.org/rails/rails/builds/6061839 --- activesupport/CHANGELOG.md | 3 --- activesupport/lib/active_support/callbacks.rb | 14 +------------- activesupport/test/callbacks_test.rb | 17 ----------------- 3 files changed, 1 insertion(+), 33 deletions(-) diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md index 5a7cddb079..31395d6da2 100644 --- a/activesupport/CHANGELOG.md +++ b/activesupport/CHANGELOG.md @@ -14,9 +14,6 @@ *Charles Jones* -* Fix skipping of filters defined by objects in `ActiveSupport::Callbacks::Callback`. - - *Ben McRedmond* ## Rails 4.0.0.beta1 (February 25, 2013) ## diff --git a/activesupport/lib/active_support/callbacks.rb b/activesupport/lib/active_support/callbacks.rb index 4a3636eb56..6c0cae71ed 100644 --- a/activesupport/lib/active_support/callbacks.rb +++ b/activesupport/lib/active_support/callbacks.rb @@ -131,7 +131,6 @@ module ActiveSupport end def matches?(_kind, _filter) - _filter = _method_name_for_object_filter(_kind, _filter) if @_is_object_filter @kind == _kind && @filter == _filter end @@ -235,14 +234,6 @@ module ActiveSupport @compiled_options = conditions.flatten.join(" && ") end - def _method_name_for_object_filter(kind, filter) - class_name = filter.kind_of?(Class) ? filter.to_s : filter.class.to_s - class_name.gsub!(/<|>|#/, '') - class_name.gsub!(/\/|:/, "_") - - "_callback_#{kind}_#{class_name}" - end - # Filters support: # # Arrays:: Used in conditions. This is used to specify @@ -264,8 +255,6 @@ module ActiveSupport # a method is created that calls the before_foo method # on the object. def _compile_filter(filter) - @_is_object_filter = false - case filter when Array filter.map {|f| _compile_filter(f)} @@ -280,8 +269,7 @@ module ActiveSupport method_name << (filter.arity == 1 ? "(self)" : " self, Proc.new ") else - method_name = _method_name_for_object_filter(kind, filter) - @_is_object_filter = true + method_name = "_callback_#{@kind}_#{next_id}" @klass.send(:define_method, "#{method_name}_object") { filter } _normalize_legacy_filter(kind, filter) diff --git a/activesupport/test/callbacks_test.rb b/activesupport/test/callbacks_test.rb index 5afc2094e8..13f2e3cdaf 100644 --- a/activesupport/test/callbacks_test.rb +++ b/activesupport/test/callbacks_test.rb @@ -66,16 +66,6 @@ module CallbacksTest end end - class CallbackClass - def self.before(model) - model.history << [:before_save, :class] - end - - def self.after(model) - model.history << [:after_save, :class] - end - end - class Person < Record [:before_save, :after_save].each do |callback_method| callback_method_sym = callback_method.to_sym @@ -83,7 +73,6 @@ module CallbacksTest send(callback_method, callback_string(callback_method_sym)) send(callback_method, callback_proc(callback_method_sym)) send(callback_method, callback_object(callback_method_sym.to_s.gsub(/_save/, ''))) - send(callback_method, CallbackClass) send(callback_method) { |model| model.history << [callback_method_sym, :block] } end @@ -97,7 +86,6 @@ module CallbacksTest skip_callback :save, :after, :before_save_method, :unless => :yes skip_callback :save, :after, :before_save_method, :if => :no skip_callback :save, :before, :before_save_method, :unless => :no - skip_callback :save, :before, CallbackClass , :if => :yes def yes; true; end def no; false; end end @@ -442,7 +430,6 @@ module CallbacksTest [:before_save, :object], [:before_save, :block], [:after_save, :block], - [:after_save, :class], [:after_save, :object], [:after_save, :proc], [:after_save, :string], @@ -462,10 +449,8 @@ module CallbacksTest [:before_save, :string], [:before_save, :proc], [:before_save, :object], - [:before_save, :class], [:before_save, :block], [:after_save, :block], - [:after_save, :class], [:after_save, :object], [:after_save, :proc], [:after_save, :string], @@ -730,10 +715,8 @@ module CallbacksTest [:before_save, :string], [:before_save, :proc], [:before_save, :object], - [:before_save, :class], [:before_save, :block], [:after_save, :block], - [:after_save, :class], [:after_save, :object], [:after_save, :proc], [:after_save, :string], -- cgit v1.2.3 From 2f0acf6a0a82e60d670b90d39d1278c55fdfe766 Mon Sep 17 00:00:00 2001 From: Neeraj Singh Date: Thu, 4 Apr 2013 17:59:02 -0400 Subject: use | to have more intent revealing code thanks to @egilburg for suggestion --- activerecord/lib/active_record/associations/association_scope.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/activerecord/lib/active_record/associations/association_scope.rb b/activerecord/lib/active_record/associations/association_scope.rb index b62a16ea3f..aa5551fe0c 100644 --- a/activerecord/lib/active_record/associations/association_scope.rb +++ b/activerecord/lib/active_record/associations/association_scope.rb @@ -101,7 +101,7 @@ module ActiveRecord scope.includes! item.includes_values scope.where_values += item.where_values - scope.order_values += (item.order_values - scope.order_values) + scope.order_values |= item.order_values end end -- cgit v1.2.3 From 261520d91890a6844fc769ecf8bd221e35c8974c Mon Sep 17 00:00:00 2001 From: Neeraj Singh Date: Thu, 4 Apr 2013 18:00:50 -0400 Subject: template should have generic name since users just copy and paste and the class name is never changed --- guides/bug_report_templates/active_record_gem.rb | 2 +- guides/bug_report_templates/active_record_master.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/guides/bug_report_templates/active_record_gem.rb b/guides/bug_report_templates/active_record_gem.rb index 19b7309dd7..2c63342572 100644 --- a/guides/bug_report_templates/active_record_gem.rb +++ b/guides/bug_report_templates/active_record_gem.rb @@ -25,7 +25,7 @@ class Comment < ActiveRecord::Base belongs_to :post end -class HasManyBugTest < MiniTest::Unit::TestCase +class BugTest < MiniTest::Unit::TestCase def test_association_stuff post = Post.create! post.comments << Comment.create! diff --git a/guides/bug_report_templates/active_record_master.rb b/guides/bug_report_templates/active_record_master.rb index 99107b83cf..68069cdd8d 100644 --- a/guides/bug_report_templates/active_record_master.rb +++ b/guides/bug_report_templates/active_record_master.rb @@ -36,7 +36,7 @@ class Comment < ActiveRecord::Base belongs_to :post end -class HasManyBugTest < MiniTest::Unit::TestCase +class BugTest < MiniTest::Unit::TestCase def test_association_stuff post = Post.create! post.comments << Comment.create! -- cgit v1.2.3 From a77ad86ff1be70dee945e6425b3209ab8da72cd4 Mon Sep 17 00:00:00 2001 From: Francesco Rodriguez Date: Thu, 4 Apr 2013 17:28:44 -0500 Subject: Exclude template files for rdoc API [ci skip] --- railties/lib/rails/api/task.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/railties/lib/rails/api/task.rb b/railties/lib/rails/api/task.rb index 1e6458cf93..c829873da4 100644 --- a/railties/lib/rails/api/task.rb +++ b/railties/lib/rails/api/task.rb @@ -57,7 +57,8 @@ module Rails CHANGELOG.md MIT-LICENSE lib/**/*.rb - ) + ), + :exclude => 'lib/rails/generators/rails/**/templates/**/*.rb' } } -- cgit v1.2.3 From 249876b99bd518f93f5a9f8ae6c4ed7d6fb02936 Mon Sep 17 00:00:00 2001 From: Carlos Antonio da Silva Date: Thu, 4 Apr 2013 19:45:19 -0300 Subject: Use a space after the comment sign when showing the result of commands http://guides.rubyonrails.org/api_documentation_guidelines.html#example-code [ci skip] --- activerecord/lib/active_record/reflection.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/activerecord/lib/active_record/reflection.rb b/activerecord/lib/active_record/reflection.rb index 3eee77e653..9403273db0 100644 --- a/activerecord/lib/active_record/reflection.rb +++ b/activerecord/lib/active_record/reflection.rb @@ -409,7 +409,7 @@ module ActiveRecord # tags_reflection = Post.reflect_on_association(:tags) # # taggings_reflection = tags_reflection.source_reflection - # #=> + # # => # def source_reflection @source_reflection ||= source_reflection_names.collect { |name| through_reflection.klass.reflect_on_association(name) }.compact.first @@ -444,8 +444,8 @@ module ActiveRecord # # tags_reflection = Post.reflect_on_association(:tags) # tags_reflection.chain - # #=> [:taggings}, @active_record=Post>, - # ] + # # => [:taggings}, @active_record=Post>, + # ] # def chain @chain ||= begin @@ -526,7 +526,7 @@ module ActiveRecord # # tags_reflection = Post.reflect_on_association(:tags) # tags_reflection.source_reflection_names - # #=> [:tag, :tags] + # # => [:tag, :tags] # def source_reflection_names @source_reflection_names ||= (options[:source] ? [options[:source]] : [name.to_s.singularize, name]).collect { |n| n.to_sym } -- cgit v1.2.3 From 8f84cdd06f85c65c1949ea227285bbdeb47c5b14 Mon Sep 17 00:00:00 2001 From: Andrew White Date: Fri, 5 Apr 2013 00:39:37 +0100 Subject: Use inspect when writing the foreign key from the reflection If we don't use inspect inside the class_eval block then the foreign key is written without quotes causing us to fetch the foreign key value and not the column name. --- activerecord/lib/active_record/associations/builder/belongs_to.rb | 4 ++-- activerecord/test/cases/timestamp_test.rb | 6 ++++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/activerecord/lib/active_record/associations/builder/belongs_to.rb b/activerecord/lib/active_record/associations/builder/belongs_to.rb index 579d7789bd..da7e8cda3a 100644 --- a/activerecord/lib/active_record/associations/builder/belongs_to.rb +++ b/activerecord/lib/active_record/associations/builder/belongs_to.rb @@ -68,11 +68,11 @@ module ActiveRecord::Associations::Builder def belongs_to_touch_after_save_or_destroy_for_#{name} record = #{name} - foreign_key_field = #{reflection.foreign_key} + foreign_key_field = #{reflection.foreign_key.inspect} if changed_attributes.key?(foreign_key_field) reflection_klass = #{reflection.klass} old_foreign_id = changed_attributes[foreign_key_field] - old_record = reflection_klass.where(foreign_key_field.to_sym => old_foreign_id).first + old_record = reflection_klass.where(foreign_key_field => old_foreign_id).first if old_record old_record.touch #{options[:touch].inspect if options[:touch] != true} end diff --git a/activerecord/test/cases/timestamp_test.rb b/activerecord/test/cases/timestamp_test.rb index 0c13bb946a..f42ecbb340 100644 --- a/activerecord/test/cases/timestamp_test.rb +++ b/activerecord/test/cases/timestamp_test.rb @@ -192,6 +192,12 @@ class TimestampTest < ActiveRecord::TestCase old_pet.update_columns(updated_at: time) new_pet.update_columns(updated_at: time) + old_pet.reload + new_pet.reload + + assert_equal time, new_pet.updated_at + assert_equal time, old_pet.updated_at + toy1.pet = new_pet toy1.save! -- cgit v1.2.3 From e936cf9c47e716a32b75703cb1a8badf96be8a6b Mon Sep 17 00:00:00 2001 From: Carlos Antonio da Silva Date: Thu, 4 Apr 2013 21:25:44 -0300 Subject: Ensure mail_to helper does not modify the given html options hash --- actionpack/lib/action_view/helpers/url_helper.rb | 6 +++--- actionpack/test/template/url_helper_test.rb | 8 +++++++- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/actionpack/lib/action_view/helpers/url_helper.rb b/actionpack/lib/action_view/helpers/url_helper.rb index a1468b6e90..bfe11fc1d7 100644 --- a/actionpack/lib/action_view/helpers/url_helper.rb +++ b/actionpack/lib/action_view/helpers/url_helper.rb @@ -425,8 +425,8 @@ module ActionView # * :bcc - Blind Carbon Copy additional recipients on the email. # # ==== Obfuscation - # Prior to Rails 4.0, +mail_to+ provided options for encoding the address - # in order to hinder email harvesters. To take advantage of these options, + # Prior to Rails 4.0, +mail_to+ provided options for encoding the address + # in order to hinder email harvesters. To take advantage of these options, # install the +actionview-encoded_mail_to+ gem. # # ==== Examples @@ -451,7 +451,7 @@ module ActionView def mail_to(email_address, name = nil, html_options = {}, &block) html_options, name = name, nil if block_given? html_options ||= {} - html_options.stringify_keys! + html_options = html_options.stringify_keys email_address = ERB::Util.html_escape(email_address) diff --git a/actionpack/test/template/url_helper_test.rb b/actionpack/test/template/url_helper_test.rb index e7b04d40c0..9b4c419807 100644 --- a/actionpack/test/template/url_helper_test.rb +++ b/actionpack/test/template/url_helper_test.rb @@ -543,11 +543,17 @@ class UrlHelperTest < ActiveSupport::TestCase mail_to('me@example.com') { content_tag(:span, 'Email me') } end - def test_link_tag_with_block_and_options + def test_mail_to_with_block_and_options assert_dom_equal %{Email me}, mail_to('me@example.com', cc: "ccaddress@example.com", class: "special") { content_tag(:span, 'Email me') } end + def test_mail_to_does_not_modify_html_options_hash + options = { class: 'special' } + mail_to 'me@example.com', 'ME!', options + assert_equal({ class: 'special' }, options) + end + def protect_against_forgery? self.request_forgery end -- cgit v1.2.3 From 797fcdf738a2a2772544731027d4fc5ca9d358bc Mon Sep 17 00:00:00 2001 From: Carlos Antonio da Silva Date: Thu, 4 Apr 2013 21:29:50 -0300 Subject: Refactor mail_to to not generate intermediate hashes when adding href There's no need to use Hash#merge with a new hash just for setting the href option to pass it through. Since we're always dealing with a new html_options hash, we're free to just set the value instead. --- actionpack/lib/action_view/helpers/url_helper.rb | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/actionpack/lib/action_view/helpers/url_helper.rb b/actionpack/lib/action_view/helpers/url_helper.rb index bfe11fc1d7..22059a0170 100644 --- a/actionpack/lib/action_view/helpers/url_helper.rb +++ b/actionpack/lib/action_view/helpers/url_helper.rb @@ -449,19 +449,20 @@ module ActionView # Email me: me@domain.com # def mail_to(email_address, name = nil, html_options = {}, &block) - html_options, name = name, nil if block_given? - html_options ||= {} - html_options = html_options.stringify_keys - email_address = ERB::Util.html_escape(email_address) + html_options, name = name, nil if block_given? + html_options = (html_options || {}).stringify_keys + extras = %w{ cc bcc body subject }.map { |item| option = html_options.delete(item) || next "#{item}=#{Rack::Utils.escape_path(option)}" }.compact extras = extras.empty? ? '' : '?' + ERB::Util.html_escape(extras.join('&')) - content_tag(:a, name || email_address.html_safe, html_options.merge("href" => "mailto:#{email_address}#{extras}".html_safe), &block) + html_options["href"] = "mailto:#{email_address}#{extras}".html_safe + + content_tag(:a, name || email_address.html_safe, html_options, &block) end # True if the current request URI was generated by the given +options+. -- cgit v1.2.3 From 034f254d59dfa1c1f508db574fac5df263336ba1 Mon Sep 17 00:00:00 2001 From: Carlos Antonio da Silva Date: Thu, 4 Apr 2013 22:02:36 -0300 Subject: Use the correct pk field from the reflected class to find the old record The implementation was using the source class foreign key field instead of the reflected primary key one to find the old record. For instance, for this scenario class Bulb < ActiveRecord::Base belongs_to :car, :touch => true end class Car < ActiveRecord::Base has_many :bulbs end the current implementation was trying to do this query: Car.where(car_id: X).first where we should be doing this query: Car.where(id: X).first This should hopefully fix the build. --- activerecord/lib/active_record/associations/builder/belongs_to.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/activerecord/lib/active_record/associations/builder/belongs_to.rb b/activerecord/lib/active_record/associations/builder/belongs_to.rb index da7e8cda3a..e0ec4392b4 100644 --- a/activerecord/lib/active_record/associations/builder/belongs_to.rb +++ b/activerecord/lib/active_record/associations/builder/belongs_to.rb @@ -71,8 +71,9 @@ module ActiveRecord::Associations::Builder foreign_key_field = #{reflection.foreign_key.inspect} if changed_attributes.key?(foreign_key_field) reflection_klass = #{reflection.klass} + primary_key_field = reflection_klass.primary_key old_foreign_id = changed_attributes[foreign_key_field] - old_record = reflection_klass.where(foreign_key_field => old_foreign_id).first + old_record = reflection_klass.where(primary_key_field => old_foreign_id).first if old_record old_record.touch #{options[:touch].inspect if options[:touch] != true} end -- cgit v1.2.3 From dce398d579e484ff64a0260cea4591ad952dc99c Mon Sep 17 00:00:00 2001 From: Carlos Antonio da Silva Date: Thu, 4 Apr 2013 22:22:42 -0300 Subject: Avoid an attempt to fetch old record when id was not present in touch callback --- .../lib/active_record/associations/builder/belongs_to.rb | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/activerecord/lib/active_record/associations/builder/belongs_to.rb b/activerecord/lib/active_record/associations/builder/belongs_to.rb index e0ec4392b4..3ba6a71366 100644 --- a/activerecord/lib/active_record/associations/builder/belongs_to.rb +++ b/activerecord/lib/active_record/associations/builder/belongs_to.rb @@ -66,19 +66,19 @@ module ActiveRecord::Associations::Builder def add_touch_callbacks(reflection) mixin.class_eval <<-CODE, __FILE__, __LINE__ + 1 def belongs_to_touch_after_save_or_destroy_for_#{name} - record = #{name} + foreign_key_field = #{reflection.foreign_key.inspect} + old_foreign_id = attribute_was(foreign_key_field) + + if old_foreign_id + reflection_klass = #{reflection.klass} + old_record = reflection_klass.find_by(reflection_klass.primary_key => old_foreign_id) - foreign_key_field = #{reflection.foreign_key.inspect} - if changed_attributes.key?(foreign_key_field) - reflection_klass = #{reflection.klass} - primary_key_field = reflection_klass.primary_key - old_foreign_id = changed_attributes[foreign_key_field] - old_record = reflection_klass.where(primary_key_field => old_foreign_id).first if old_record old_record.touch #{options[:touch].inspect if options[:touch] != true} end end + record = #{name} unless record.nil? || record.new_record? record.touch #{options[:touch].inspect if options[:touch] != true} end -- cgit v1.2.3 From 87543d1cd2428297a546723b0ad3bd6b175da81a Mon Sep 17 00:00:00 2001 From: Shunsuke Osa Date: Fri, 5 Apr 2013 15:21:48 +0900 Subject: Sort modules in alphabetical order. --- activerecord/lib/active_record.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/activerecord/lib/active_record.rb b/activerecord/lib/active_record.rb index f08cca651f..3bfc6772b2 100644 --- a/activerecord/lib/active_record.rb +++ b/activerecord/lib/active_record.rb @@ -35,8 +35,8 @@ module ActiveRecord autoload :Base autoload :Callbacks autoload :Core - autoload :CounterCache autoload :ConnectionHandling + autoload :CounterCache autoload :DynamicMatchers autoload :Explain autoload :Inheritance @@ -69,8 +69,8 @@ module ActiveRecord autoload :Aggregations autoload :Associations - autoload :AttributeMethods autoload :AttributeAssignment + autoload :AttributeMethods autoload :AutosaveAssociation autoload :Relation -- cgit v1.2.3 From 155f86675ce6f2995a61eee9e3d814c974d2bec8 Mon Sep 17 00:00:00 2001 From: Andrew White Date: Fri, 5 Apr 2013 11:27:54 +0100 Subject: Improve `belongs_to touch: true` timestamp test Round off time to a whole second value to compensate for databases that don't support fractional timestamps. Also add a assertion to check that the old record is touched when the association is cleared. --- activerecord/test/cases/timestamp_test.rb | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/activerecord/test/cases/timestamp_test.rb b/activerecord/test/cases/timestamp_test.rb index f42ecbb340..9d84f64c96 100644 --- a/activerecord/test/cases/timestamp_test.rb +++ b/activerecord/test/cases/timestamp_test.rb @@ -187,17 +187,11 @@ class TimestampTest < ActiveRecord::TestCase toy2 = klass.find(2) new_pet = toy2.pet - time = 3.days.ago + time = 3.days.ago.at_beginning_of_hour old_pet.update_columns(updated_at: time) new_pet.update_columns(updated_at: time) - old_pet.reload - new_pet.reload - - assert_equal time, new_pet.updated_at - assert_equal time, old_pet.updated_at - toy1.pet = new_pet toy1.save! @@ -208,6 +202,26 @@ class TimestampTest < ActiveRecord::TestCase assert_not_equal time, old_pet.updated_at end + def test_clearing_association_touches_the_old_record + klass = Class.new(ActiveRecord::Base) do + def self.name; 'Toy'; end + belongs_to :pet, touch: true + end + + toy = klass.find(1) + pet = toy.pet + time = 3.days.ago.at_beginning_of_hour + + pet.update_columns(updated_at: time) + + toy.pet = nil + toy.save! + + pet.reload + + assert_not_equal time, pet.updated_at + end + def test_timestamp_attributes_for_create toy = Toy.first assert_equal toy.send(:timestamp_attributes_for_create), [:created_at, :created_on] -- cgit v1.2.3 From f029fb07c210dd384f8ad02b5ce8903911c540d3 Mon Sep 17 00:00:00 2001 From: Neeraj Singh Date: Mon, 25 Mar 2013 17:32:40 -0400 Subject: failing test for #9869 --- activerecord/test/cases/scoping/named_scoping_test.rb | 5 +++++ activerecord/test/models/comment.rb | 1 + 2 files changed, 6 insertions(+) diff --git a/activerecord/test/cases/scoping/named_scoping_test.rb b/activerecord/test/cases/scoping/named_scoping_test.rb index 3e2e6ab701..1ac68746de 100644 --- a/activerecord/test/cases/scoping/named_scoping_test.rb +++ b/activerecord/test/cases/scoping/named_scoping_test.rb @@ -459,4 +459,9 @@ class NamedScopingTest < ActiveRecord::TestCase end assert_equal [posts(:welcome).title], klass.all.map(&:title) end + + def test_subclass_merges_scopes_properly + assert_equal 1, SpecialComment.crazy_all.count + end + end diff --git a/activerecord/test/models/comment.rb b/activerecord/test/models/comment.rb index ede5fbd0c6..9d594bd7d6 100644 --- a/activerecord/test/models/comment.rb +++ b/activerecord/test/models/comment.rb @@ -29,6 +29,7 @@ class Comment < ActiveRecord::Base end class SpecialComment < Comment + scope :crazy_all, -> { where(body: 'go crazy').created } end class SubSpecialComment < SpecialComment -- cgit v1.2.3 From 8606a7fbe9367e9ae37ad058dd07f0dd38daf015 Mon Sep 17 00:00:00 2001 From: Jon Leighton Date: Fri, 5 Apr 2013 12:46:56 +0100 Subject: Fix scope chaining + STI See #9869 and #9929. The problem arises from the following example: class Project < ActiveRecord::Base scope :completed, -> { where completed: true } end class MajorProject < Project end When calling: MajorProject.where(tasks_count: 10).completed This expands to: MajorProject.where(tasks_count: 10).scoping { MajorProject.completed } However the lambda for the `completed` scope is defined on Project. This means that when it is called, `self` is Project rather than MajorProject. So it expands to: MajorProject.where(tasks_count: 10).scoping { Project.where(completed: true) } Since the scoping was applied on MajorProject, and not Project, this fails to apply the tasks_count condition. The solution is to make scoping apply across STI classes. I am slightly concerned about the possible side-effects of this, but no tests fail and it seems ok. I guess we'll see. --- activerecord/lib/active_record/scoping.rb | 4 ++-- activerecord/lib/active_record/scoping/named.rb | 9 ++------- activerecord/test/cases/scoping/named_scoping_test.rb | 2 +- activerecord/test/models/comment.rb | 1 - 4 files changed, 5 insertions(+), 11 deletions(-) diff --git a/activerecord/lib/active_record/scoping.rb b/activerecord/lib/active_record/scoping.rb index 9746b1c3c2..886182f534 100644 --- a/activerecord/lib/active_record/scoping.rb +++ b/activerecord/lib/active_record/scoping.rb @@ -9,11 +9,11 @@ module ActiveRecord module ClassMethods def current_scope #:nodoc: - Thread.current["#{self}_current_scope"] + Thread.current["#{base_class}_current_scope"] end def current_scope=(scope) #:nodoc: - Thread.current["#{self}_current_scope"] = scope + Thread.current["#{base_class}_current_scope"] = scope end end diff --git a/activerecord/lib/active_record/scoping/named.rb b/activerecord/lib/active_record/scoping/named.rb index 12317601b6..da73bead32 100644 --- a/activerecord/lib/active_record/scoping/named.rb +++ b/activerecord/lib/active_record/scoping/named.rb @@ -160,13 +160,8 @@ module ActiveRecord singleton_class.send(:define_method, name) do |*args| if body.respond_to?(:call) - scope = extension ? body.call(*args).extending(extension) : body.call(*args) - - if scope - default_scoped = scope.default_scoped - scope = relation.merge(scope) - scope.default_scoped = default_scoped - end + scope = all.scoping { body.call(*args) } + scope = scope.extending(extension) if extension else scope = body end diff --git a/activerecord/test/cases/scoping/named_scoping_test.rb b/activerecord/test/cases/scoping/named_scoping_test.rb index 1ac68746de..afe32af1d1 100644 --- a/activerecord/test/cases/scoping/named_scoping_test.rb +++ b/activerecord/test/cases/scoping/named_scoping_test.rb @@ -461,7 +461,7 @@ class NamedScopingTest < ActiveRecord::TestCase end def test_subclass_merges_scopes_properly - assert_equal 1, SpecialComment.crazy_all.count + assert_equal 1, SpecialComment.where(body: 'go crazy').created.count end end diff --git a/activerecord/test/models/comment.rb b/activerecord/test/models/comment.rb index 9d594bd7d6..ede5fbd0c6 100644 --- a/activerecord/test/models/comment.rb +++ b/activerecord/test/models/comment.rb @@ -29,7 +29,6 @@ class Comment < ActiveRecord::Base end class SpecialComment < Comment - scope :crazy_all, -> { where(body: 'go crazy').created } end class SubSpecialComment < SpecialComment -- cgit v1.2.3 From bbfddf8470fcea21f31b285f6d1ef1bb723e07e1 Mon Sep 17 00:00:00 2001 From: Carlos Antonio da Silva Date: Thu, 4 Apr 2013 22:37:49 -0300 Subject: Fix indent and remove extra white spaces --- .../test/template/form_options_helper_test.rb | 32 ++++++++++++---------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/actionpack/test/template/form_options_helper_test.rb b/actionpack/test/template/form_options_helper_test.rb index 1437ff7285..94ae8549f7 100644 --- a/actionpack/test/template/form_options_helper_test.rb +++ b/actionpack/test/template/form_options_helper_test.rb @@ -1110,15 +1110,15 @@ class FormOptionsHelperTest < ActionView::TestCase "", html end - + def test_time_zone_select_with_priority_zones_as_regexp_using_grep_finds_no_zones @firm = Firm.new("D") - + priority_zones = /A|D/ @fake_timezones.each do |tz| priority_zones.stubs(:===).with(tz).raises(Exception) end - + html = time_zone_select("firm", "time_zone", priority_zones) assert_dom_equal "" + + + html = time_zone_select( "firm", "time_zone", nil, :default => 'B' ) + assert_dom_equal "" + - "\n" + - "\n" + - "\n" + - "\n" + - "" + - "", - html + @firm = Firm.new('D') + + html = time_zone_select( "firm", "time_zone", nil, :default => 'B' ) + assert_dom_equal "", + html end def test_options_for_select_with_element_attributes -- cgit v1.2.3 From 877dfcbb6437acfd56c7ead4f9998b5b0bf82c0e Mon Sep 17 00:00:00 2001 From: Carlos Antonio da Silva Date: Fri, 5 Apr 2013 10:01:38 -0300 Subject: Move changelog to the top [ci skip] --- activerecord/CHANGELOG.md | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index db9aa858c2..e1d3686b95 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,5 +1,22 @@ ## Rails 4.0.0 (unreleased) ## +* Fix associations with `:inverse_of` option when building association + with a block. Inside the block the parent object was different then + after the block. + + Example: + + parent.association.build do |child| + child.parent.equal?(parent) # false + end + + # vs + + child = parent.association.build + child.parent.equal?(parent) # true + + *Michal Cichra* + * `has_many` using `:through` now obeys the order clause mentioned in through association. Fixes #10016. @@ -418,23 +435,6 @@ # This will expand the order :name to "authors".name. Author.joins(:books).where('books.published = 1').order(:name) -* Fix associations with `:inverse_of` option when building association - with a block. Inside the block the parent object was different then - after the block. - - Example: - - parent.association.build do |child| - child.parent.equal?(parent) # false - end - - # vs - - child = parent.association.build - child.parent.equal?(parent) # true - - *Michal Cichra* - ## Rails 4.0.0.beta1 (February 25, 2013) ## -- cgit v1.2.3 From 661365e7ce445955996c9024b76cea84e4f31a02 Mon Sep 17 00:00:00 2001 From: Erik Peterson Date: Thu, 4 Apr 2013 21:06:52 -0400 Subject: Correctly parse bigint defaults in PostgreSQL --- activerecord/CHANGELOG.md | 5 +++++ .../lib/active_record/connection_adapters/postgresql_adapter.rb | 2 +- activerecord/test/cases/schema_dumper_test.rb | 5 +++++ activerecord/test/schema/postgresql_specific_schema.rb | 1 + 4 files changed, 12 insertions(+), 1 deletion(-) diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index 0dcc7a4819..c2580b2913 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,5 +1,10 @@ ## Rails 4.0.0 (unreleased) ## +* Default values for PostgreSQL bigint types now get parsed and dumped to the + schema correctly. + + *Erik Peterson* + * `has_many` using `:through` now obeys the order clause mentioned in through association. Fixes #10016. diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb index e34e1fc10c..bf403c3ae0 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb @@ -80,7 +80,7 @@ module ActiveRecord when /\A'(.*)'::(num|date|tstz|ts|int4|int8)range\z/m $1 # Numeric types - when /\A\(?(-?\d+(\.\d*)?\)?)\z/ + when /\A\(?(-?\d+(\.\d*)?\)?(::bigint)?)\z/ $1 # Character types when /\A\(?'(.*)'::.*\b(?:character varying|bpchar|text)\z/m diff --git a/activerecord/test/cases/schema_dumper_test.rb b/activerecord/test/cases/schema_dumper_test.rb index 9944527d48..a48ae1036f 100644 --- a/activerecord/test/cases/schema_dumper_test.rb +++ b/activerecord/test/cases/schema_dumper_test.rb @@ -242,6 +242,11 @@ class SchemaDumperTest < ActiveRecord::TestCase end if current_adapter?(:PostgreSQLAdapter) + def test_schema_dump_includes_bigint_default + output = standard_dump + assert_match %r{t.integer\s+"bigint_default",\s+limit: 8,\s+default: 0}, output + end + def test_schema_dump_includes_extensions connection = ActiveRecord::Base.connection skip unless connection.supports_extensions? diff --git a/activerecord/test/schema/postgresql_specific_schema.rb b/activerecord/test/schema/postgresql_specific_schema.rb index d8271ac8d1..6b7012a172 100644 --- a/activerecord/test/schema/postgresql_specific_schema.rb +++ b/activerecord/test/schema/postgresql_specific_schema.rb @@ -32,6 +32,7 @@ ActiveRecord::Schema.define do char3 text default 'a text field', positive_integer integer default 1, negative_integer integer default -1, + bigint_default bigint default 0::bigint, decimal_number decimal(3,2) default 2.78, multiline_default text DEFAULT '--- [] -- cgit v1.2.3 From 48dc5192eff45fce5ce39c41cdc3188be97ca614 Mon Sep 17 00:00:00 2001 From: Ryan McGeary Date: Fri, 5 Apr 2013 14:51:15 -0400 Subject: Fix explicit names on multiple file fields If a file field tag is passed the multiple option, it is turned into an array field (appending "[]"), but if the file field is passed an explicit name as an option, leave the name alone (do not append "[]"). Fixes #9830 --- actionpack/CHANGELOG.md | 7 +++++++ actionpack/lib/action_view/helpers/tags/base.rb | 15 +++++++-------- actionpack/test/template/form_helper_test.rb | 10 ++++++++++ 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/actionpack/CHANGELOG.md b/actionpack/CHANGELOG.md index bb84a83208..3121167d2d 100644 --- a/actionpack/CHANGELOG.md +++ b/actionpack/CHANGELOG.md @@ -1,5 +1,12 @@ ## Rails 4.0.0 (unreleased) ## +* Fix explicit names on multiple file fields. If a file field tag is passed + the multiple option, it is turned into an array field (appending `[]`), + but if the file field is passed an explicit name as an option, leave the + name alone (do not append `[]`). Fixes #9830 + + *Ryan McGeary* + * Add block support for the `mail_to` helper, similar to the `link_to` helper. *Sam Pohlenz* diff --git a/actionpack/lib/action_view/helpers/tags/base.rb b/actionpack/lib/action_view/helpers/tags/base.rb index aef1572290..10dec66b4f 100644 --- a/actionpack/lib/action_view/helpers/tags/base.rb +++ b/actionpack/lib/action_view/helpers/tags/base.rb @@ -73,27 +73,26 @@ module ActionView def add_default_name_and_id(options) if options.has_key?("index") - options["name"] ||= options.fetch("name"){ tag_name_with_index(options["index"]) } + options["name"] ||= options.fetch("name"){ tag_name_with_index(options["index"], options["multiple"]) } options["id"] = options.fetch("id"){ tag_id_with_index(options["index"]) } options.delete("index") elsif defined?(@auto_index) - options["name"] ||= options.fetch("name"){ tag_name_with_index(@auto_index) } + options["name"] ||= options.fetch("name"){ tag_name_with_index(@auto_index, options["multiple"]) } options["id"] = options.fetch("id"){ tag_id_with_index(@auto_index) } else - options["name"] ||= options.fetch("name"){ tag_name } + options["name"] ||= options.fetch("name"){ tag_name(options["multiple"]) } options["id"] = options.fetch("id"){ tag_id } end - options["name"] += "[]" if options["multiple"] && !options["name"].ends_with?("[]") options["id"] = [options.delete('namespace'), options["id"]].compact.join("_").presence end - def tag_name - "#{@object_name}[#{sanitized_method_name}]" + def tag_name(multiple = false) + "#{@object_name}[#{sanitized_method_name}]#{"[]" if multiple}" end - def tag_name_with_index(index) - "#{@object_name}[#{index}][#{sanitized_method_name}]" + def tag_name_with_index(index, multiple = false) + "#{@object_name}[#{index}][#{sanitized_method_name}]#{"[]" if multiple}" end def tag_id diff --git a/actionpack/test/template/form_helper_test.rb b/actionpack/test/template/form_helper_test.rb index dff0b8bdc2..1ff320224d 100644 --- a/actionpack/test/template/form_helper_test.rb +++ b/actionpack/test/template/form_helper_test.rb @@ -361,6 +361,16 @@ class FormHelperTest < ActionView::TestCase assert_dom_equal expected, file_field("user", "avatar") end + def test_file_field_with_multiple_behavior + expected = '' + assert_dom_equal expected, file_field("import", "file", :multiple => true) + end + + def test_file_field_with_multiple_behavior_and_explicit_name + expected = '' + assert_dom_equal expected, file_field("import", "file", :multiple => true, :name => "custom") + end + def test_hidden_field assert_dom_equal( '', -- cgit v1.2.3 From 096ee1594d329f53ef47d0aff9bb6eef25a18b96 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Fri, 5 Apr 2013 16:43:04 -0300 Subject: Improve the changelog entry [ci skip] --- actionpack/CHANGELOG.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/actionpack/CHANGELOG.md b/actionpack/CHANGELOG.md index 3121167d2d..913edbd8df 100644 --- a/actionpack/CHANGELOG.md +++ b/actionpack/CHANGELOG.md @@ -1,9 +1,10 @@ ## Rails 4.0.0 (unreleased) ## -* Fix explicit names on multiple file fields. If a file field tag is passed +* Fix explicit names on multiple file fields. If a file field tag has the multiple option, it is turned into an array field (appending `[]`), - but if the file field is passed an explicit name as an option, leave the - name alone (do not append `[]`). Fixes #9830 + but if an explicit name is passed to `file_field` the `[]` is not + appended. + Fixes #9830. *Ryan McGeary* -- cgit v1.2.3