Browse files

Merge branch 'master' into 3-1-stable

Conflicts:
	test/basic_test.rb
  • Loading branch information...
2 parents 5a1988d + 8bf889f commit 5fb05f82f0557475cb68e43f90f7448a77dc5c63 @lifo committed Sep 13, 2011
Showing with 52 additions and 26 deletions.
  1. +12 −11 lib/sidekick/active_record.rb
  2. +4 −1 lib/sidekick/reload.rb
  3. +0 −14 test/basic_test.rb
  4. +36 −0 test/reload_test.rb
View
23 lib/sidekick/active_record.rb
@@ -1,23 +1,24 @@
require 'sidekick/target'
require 'sidekick/reload'
-class ActiveRecord::Base
- attr_accessor :_parent_record_set
-
- def reload_with_kick(*)
- self._parent_record_set = nil
- reload_without_kick
- end
-
- alias_method_chain :reload, :kick
+module Sidekick
+ module Record
+ def reload(*)
+ self._parent_record_set = nil
+ super
+ end
- module Destructor
def destroy(*)
self._parent_record_set = nil
super
end
end
- include Destructor
+end
+
+class ActiveRecord::Base
+ attr_accessor :_parent_record_set
+
+ include Sidekick::Record
[:clone, :dup].each do |method_name|
define_method(:"#{method_name}_with_kick") do
View
5 lib/sidekick/reload.rb
@@ -6,7 +6,10 @@ def self.included(base)
def reload_with_kick
@owner._parent_record_set = nil
- reload_without_kick
+
+ reset
+ load_target
+ self unless @target.nil?
end
end
end
View
14 test/basic_test.rb
@@ -61,20 +61,6 @@ def test_belongs_to
end
end
- def test_belongs_reload
- assert_queries(3) do
- # Query 1
- posts = Post.all
- hello_post = posts.detect {|p| p.title == 'Hello' }
-
- # Query 2 - Load all the users
- assert_equal 'Bob', hello_post.user.name
-
- # Query 3 Reload Bob
- assert_equal 'Bob', hello_post.user.reload.name
- end
- end
-
def test_has_one
assert_queries(2) do
# Query 1
View
36 test/reload_test.rb
@@ -0,0 +1,36 @@
+require 'test_helper'
+
+class ReloadTest < ActiveRecord::TestCase
+ def test_reload_existing_record
+ assert_queries(2) do
+ # Query 1
+ users = User.all
+
+ lifo = users.detect {|u| u.name == 'Lifo'}
+
+ # Query 2 - Reload Lifo
+ assert_equal 'Lifo', lifo.reload.name
+ end
+ end
+
+ def test_reload_newly_created_record
+ noob = User.create(:name => 'Noob')
+
+ assert_queries(1) { assert_equal 'Noob', noob.reload.name }
+ end
+
+ def test_belongs_reload
+ assert_queries(3) do
+ # Query 1
+ posts = Post.all
+ hello_post = posts.detect {|p| p.title == 'Hello' }
+
+ # Query 2 - Load all the users
+ assert_equal 'Bob', hello_post.user.name
+
+ # Query 3- Reload Bob
+ assert_equal 'Bob', hello_post.user.reload.name
+ end
+ end
+
+end

0 comments on commit 5fb05f8

Please sign in to comment.