Skip to content

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also .

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also .
...
  • 8 commits
  • 7 files changed
  • 0 commit comments
  • 1 contributor
Showing with 102 additions and 42 deletions.
  1. +1 −1 README
  2. +1 −1 Rakefile
  3. +16 −17 lib/sidekick/active_record.rb
  4. +41 −0 lib/sidekick/association_proxy_monkey.rb
  5. +7 −1 lib/sidekick/target.rb
  6. +0 −22 test/basic_test.rb
  7. +36 −0 test/reload_test.rb
View
2 README
@@ -1 +1 @@
-Hello
+Not yet.
View
2 Rakefile
@@ -5,7 +5,7 @@ task :default => :test
Rake::TestTask.new(:test) do |t|
t.libs << "test"
- t.pattern = 'test/**/*_test.rb'
+ t.test_files = Dir.glob('test/**/*_test.rb')
t.verbose = true
end
Rake::Task['test'].comment = "Run tests"
View
33 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
@@ -41,6 +42,10 @@ def to_a_with_kick
alias_method_chain :to_a, :kick
end
+if ActiveRecord::VERSION::STRING < '3.1' && !ActiveRecord::Associations::AssociationProxy.instance_methods.include?(:load)
+ require 'sidekick/association_proxy_monkey'
+end
+
[
ActiveRecord::Associations::BelongsToAssociation,
ActiveRecord::Associations::HasManyAssociation,
@@ -50,12 +55,6 @@ def to_a_with_kick
ActiveRecord::Associations::HasManyThroughAssociation
].each do |association_klass|
association_klass.send :include, Sidekick::Target
-end
-
-[
- ActiveRecord::Associations::HasManyAssociation,
- ActiveRecord::Associations::HasManyThroughAssociation
-].each do |association_klass|
association_klass.send :include, Sidekick::Reload
end
View
41 lib/sidekick/association_proxy_monkey.rb
@@ -0,0 +1,41 @@
+# Please remove me as soon as you can - __FILE__
+
+class ActiveRecord::Associations::AssociationProxy
+ def load
+ load_target unless loaded?
+ self unless @target.nil?
+ end
+
+ def reload
+ reset
+ load
+ end
+end
+
+module Sidekick
+ module MonkeyAssociationAccessor
+ def association_accessor_methods(reflection, association_proxy_class)
+ super(reflection, association_proxy_class)
+
+ redefine_method(reflection.name) do |*params|
+ force_reload = params.first unless params.empty?
+ association = association_instance_get(reflection.name)
+
+ if association.nil? || force_reload
+ association = association_proxy_class.new(self, reflection)
+ retval = force_reload ? reflection.klass.uncached { association.reload } : association.load
+ if retval.nil? and association_proxy_class == ActiveRecord::Associations::BelongsToAssociation
+ association_instance_set(reflection.name, nil)
+ return nil
+ end
+ association_instance_set(reflection.name, association)
+ end
+
+ association.target.nil? ? nil : association
+ end
+
+ end
+ end
+end
+
+ActiveRecord::Base.send :extend, Sidekick::MonkeyAssociationAccessor
View
8 lib/sidekick/target.rb
@@ -10,7 +10,13 @@ def find_target_with_kick
reflection_name = @reflection.name
# Fucking STI
- working_record_set = @owner._parent_record_set.find_all {|r| r.class.reflect_on_association(reflection_name) }
+ working_record_set = @owner._parent_record_set.find_all {|r| r.class.reflect_on_association(reflection_name) }.uniq
+
+ # Sorry new records. You don't belong.
+ working_record_set.each do |r|
+ x = r.send(:instance_variable_get, "@#{reflection_name}")
+ x.reset if x
+ end
@owner.class.send(:preload_associations, working_record_set, reflection_name.to_sym)
View
22 test/basic_test.rb
@@ -58,28 +58,6 @@ def test_belongs_to
end
end
- def test_belongs_reload
- # Need to fix AR 3.0.x to not call .reload for loading the association the first time
- flunk
-
- # Create a post with missing user_id
- orphan_post = Post.new(:title => 'Whatever', :content => 'Fake')
- orphan_post.user_id = User.maximum(:id) + 10
- orphan_post.save!
-
- assert_queries(3) do
- # Query 1
- posts = Post.all
- orphan_post = posts.detect {|p| p.id == orphan_post.id }
-
- # Query 2 - Load all the authors
- assert_equal ["Lifo", "Bob"], posts.map(&:user).compact.map(&:name).uniq
- assert_nil orphan_post.user
-
- assert_nil orphan_post.user(:reload)
- 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(4) 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 & 4- Reload Bob. Also fires SHOW TABLES query.
+ assert_equal 'Bob', hello_post.user.reload.name
+ end
+ end
+
+end

No commit comments for this range

Something went wrong with that request. Please try again.