Skip to content

Commit

Permalink
Merge branch 'master' into 3-1-stable
Browse files Browse the repository at this point in the history
Conflicts:
	test/basic_test.rb
  • Loading branch information
lifo committed Sep 13, 2011
2 parents 5a1988d + 8bf889f commit 5fb05f8
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 26 deletions.
23 changes: 12 additions & 11 deletions 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
Expand Down
5 changes: 4 additions & 1 deletion lib/sidekick/reload.rb
Expand Up @@ -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
14 changes: 0 additions & 14 deletions test/basic_test.rb
Expand Up @@ -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
Expand Down
36 changes: 36 additions & 0 deletions 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.