Skip to content

Commit

Permalink
Fixing respond_to on the many relations
Browse files Browse the repository at this point in the history
  • Loading branch information
durran committed Aug 4, 2011
1 parent bf30e80 commit 44419cd
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 6 deletions.
3 changes: 2 additions & 1 deletion lib/mongoid/relations/many.rb
Expand Up @@ -80,7 +80,8 @@ def nil?
#
# @since 2.0.0
def respond_to?(name, include_private = false)
[].respond_to?(name, include_private) || super
[].respond_to?(name, include_private) ||
klass.respond_to?(name, include_private) || super
end

# This is public access to the relation's criteria.
Expand Down
2 changes: 1 addition & 1 deletion lib/mongoid/relations/proxy.rb
Expand Up @@ -9,7 +9,7 @@ class Proxy
# 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$|^tap$)/
method =~ /(^__|^send$|^object_id$|^extend$|^respond_to\?$|^tap$)/
end

attr_accessor :base, :loaded, :metadata, :target
Expand Down
42 changes: 38 additions & 4 deletions spec/unit/mongoid/relations/embedded/many_spec.rb
Expand Up @@ -595,14 +595,48 @@
end
end

describe "respond_to?" do
describe "#respond_to?" do

let(:relation) do
described_class.new(base, target, metadata)
let(:person) do
Person.new
end

let(:addresses) do
person.addresses
end

Array.public_instance_methods.each do |method|

context "when checking #{method}" do

it "returns true" do
addresses.respond_to?(method).should be_true
end
end
end

Mongoid::Relations::Embedded::Many.public_instance_methods.each do |method|

context "when checking #{method}" do

it "returns true" do
addresses.respond_to?(method).should be_true
end
end
end

Address.scopes.keys.each do |method|

context "when checking #{method}" do

it "returns true" do
addresses.respond_to?(method).should be_true
end
end
end

it "supports 'include_private = boolean'" do
expect { relation.respond_to?(:Rational, true) }.not_to raise_error
expect { addresses.respond_to?(:Rational, true) }.not_to raise_error
end
end
end
41 changes: 41 additions & 0 deletions spec/unit/mongoid/relations/referenced/many_spec.rb
Expand Up @@ -43,6 +43,47 @@
end
end

describe "#respond_to?" do

let(:person) do
Person.new
end

let(:posts) do
person.posts
end

Array.public_instance_methods.each do |method|

context "when checking #{method}" do

it "returns true" do
posts.respond_to?(method).should be_true
end
end
end

Mongoid::Relations::Referenced::Many.public_instance_methods.each do |method|

context "when checking #{method}" do

it "returns true" do
posts.respond_to?(method).should be_true
end
end
end

Post.scopes.keys.each do |method|

context "when checking #{method}" do

it "returns true" do
posts.respond_to?(method).should be_true
end
end
end
end

describe ".stores_foreign_key?" do

it "returns false" do
Expand Down
41 changes: 41 additions & 0 deletions spec/unit/mongoid/relations/referenced/many_to_many_spec.rb
Expand Up @@ -43,6 +43,47 @@
end
end

describe "#respond_to?" do

let(:person) do
Person.new
end

let(:preferences) do
person.preferences
end

Array.public_instance_methods.each do |method|

context "when checking #{method}" do

it "returns true" do
preferences.respond_to?(method).should be_true
end
end
end

Mongoid::Relations::Referenced::Many.public_instance_methods.each do |method|

context "when checking #{method}" do

it "returns true" do
preferences.respond_to?(method).should be_true
end
end
end

Preference.scopes.keys.each do |method|

context "when checking #{method}" do

it "returns true" do
preferences.respond_to?(method).should be_true
end
end
end
end

describe ".stores_foreign_key?" do

it "returns true" do
Expand Down

0 comments on commit 44419cd

Please sign in to comment.