Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fix proxy extension. Fixes #1933

  • Loading branch information...
commit d8e10c14796811d8564559ebf8b436bbf7d16178 1 parent 7aa4eb5
@durran durran authored
View
3  CHANGELOG.md
@@ -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)
View
8 lib/mongoid/relations/proxy.rb
@@ -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
View
2  spec/app/models/person.rb
@@ -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
View
47 spec/functional/mongoid/relations/proxy_spec.rb
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.