Skip to content
This repository has been archived by the owner on Apr 17, 2018. It is now read-only.

Commit

Permalink
Fixed Collection#blank? to work
Browse files Browse the repository at this point in the history
* Fixed delegation code in Collection to only forward methods explicitly
  defined in the model to it, rather than methods that could've been
  mixed in or inherited from Object.

[#976 state:resolved]
  • Loading branch information
dkubb committed Jul 16, 2009
1 parent 50e35e1 commit 2e4536c
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 11 deletions.
2 changes: 1 addition & 1 deletion lib/dm-core/collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1321,7 +1321,7 @@ def sliced_query(offset, limit)
#
# @api public
def method_missing(method, *args, &block)
if model.respond_to?(method)
if model.model_method_defined?(method)
delegate_to_model(method, *args, &block)
elsif relationship = relationships[method] || relationships[method.to_s.singular.to_sym]
delegate_to_relationship(relationship, *args)
Expand Down
38 changes: 28 additions & 10 deletions lib/dm-core/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -566,21 +566,14 @@ def repositories

# TODO: document
# @api private
def resource_methods
resource_methods = Set.new

ancestors.each do |mod|
next unless mod <= DataMapper::Resource
resource_methods.merge(mod.instance_methods(false).map { |method| method.to_s })
end

resource_methods
def model_method_defined?(method)
model_methods.include?(method.to_s)
end

# TODO: document
# @api private
def resource_method_defined?(method)
resource_methods.include?(method)
resource_methods.include?(method.to_s)
end

private
Expand Down Expand Up @@ -675,5 +668,30 @@ def assert_valid # :nodoc:
end
end
end

# TODO: document
# @api private
def model_methods
@model_methods ||= ancestor_instance_methods { |mod| mod.meta_class }
end

# TODO: document
# @api private
def resource_methods
@resource_methods ||= ancestor_instance_methods { |mod| mod }
end

# TODO: document
# @api private
def ancestor_instance_methods
methods = Set.new

ancestors.each do |mod|
next unless mod <= DataMapper::Resource
methods.merge(yield(mod).instance_methods(false).map { |method| method.to_s })
end

methods
end
end # module Model
end # module DataMapper
16 changes: 16 additions & 0 deletions spec/public/shared/collection_shared_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,22 @@
end
end

it { @articles.should respond_to(:blank?) }

describe '#blank?' do
describe 'when the collection is empty' do
it 'should be true' do
@articles.clear.blank?.should be_true
end
end

describe 'when the collection is not empty' do
it 'should be false' do
@articles.blank?.should be_false
end
end
end

it { @articles.should respond_to(:clear) }

describe '#clear' do
Expand Down

0 comments on commit 2e4536c

Please sign in to comment.