Permalink
Browse files

Fix proxy extension. Fixes #1933

  • Loading branch information...
1 parent 7aa4eb5 commit d8e10c14796811d8564559ebf8b436bbf7d16178 @durran durran committed Apr 20, 2012
Showing with 56 additions and 4 deletions.
  1. +3 −0 CHANGELOG.md
  2. +5 −3 lib/mongoid/relations/proxy.rb
  3. +1 −1 spec/app/models/person.rb
  4. +47 −0 spec/functional/mongoid/relations/proxy_spec.rb
View
@@ -7,6 +7,9 @@ For instructions on upgrading to newer versions, visit
### Resolved Issues
+* \#1933 `Proxy#extend` should delegate through to the target, where
+ extending the proxy itself is now handled through `Proxy#proxy_extend`.
+
* \#1930 Ensure complex criteria are expanded in all where clauses.
(Hans Hasselberg)
@@ -5,14 +5,16 @@ module Relations #:nodoc:
# This class is the superclass for all relation proxy objects, and contains
# common behaviour for all of them.
class Proxy
- include Threaded::Lifecycle
+ alias :extend_proxy :extend
# We undefine most methods to get them sent through to the target.
instance_methods.each do |method|
undef_method(method) unless
- method =~ /(^__|^send$|^object_id$|^extend$|^respond_to\?$|^tap$)/
+ method =~ /(^__|^send|^object_id|^respond_to|^tap|extend_proxy)/
end
+ include Threaded::Lifecycle
+
attr_accessor :base, :loaded, :metadata, :target
# Backwards compatibility with Mongoid beta releases.
@@ -34,7 +36,7 @@ class Proxy
def init(base, target, metadata)
@base, @target, @metadata = base, target, metadata
yield(self) if block_given?
- extend metadata.extension if metadata.extension?
+ extend_proxy(metadata.extension) if metadata.extension?
end
# The default substitutable object for a relation proxy is the clone of
@@ -49,7 +49,7 @@ def extension
"Testing"
end
def find_by_street(street)
- @target.select { |doc| doc.street == street }
+ where(street: street).first
end
end
embeds_many :address_components, :validate => false
@@ -0,0 +1,47 @@
+require "spec_helper"
+
+describe Mongoid::Relations::Proxy do
+
+ describe "#extend" do
+
+ before(:all) do
+ module Testable
+ end
+ end
+
+ after(:all) do
+ Object.send(:remove_const, :Testable)
+ end
+
+ let(:person) do
+ Person.create
+ end
+
+ let(:name) do
+ person.build_name
+ end
+
+ before do
+ name.namable.extend(Testable)
+ end
+
+ it "extends the proxied object" do
+ person.should be_a(Testable)
+ end
+
+ context "when extending from the relation definition" do
+
+ let!(:address) do
+ person.addresses.create(street: "hobrecht")
+ end
+
+ let(:found) do
+ person.addresses.find_by_street("hobrecht")
+ end
+
+ it "extends the proxy" do
+ found.should eq(address)
+ end
+ end
+ end
+end

0 comments on commit d8e10c1

Please sign in to comment.