Skip to content

Commit

Permalink
Merge pull request #304 from projecthydra/remove_cruft
Browse files Browse the repository at this point in the history
Remove unused delegates. Simplify method_missing logic
  • Loading branch information
dchandekstark committed Jan 7, 2014
2 parents 5eddd93 + ade96ee commit 2c8fbf6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 35 deletions.
40 changes: 5 additions & 35 deletions lib/active_fedora/associations/collection_proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,7 @@ module Associations
# is computed directly through SQL and does not trigger by itself the
# instantiation of the actual post records.
class CollectionProxy < Relation # :nodoc:
delegate :group, :order, :limit, :joins, :where, :preload, :eager_load, :includes, :from,
:lock, :readonly, :having, :to => :scoped

delegate :target, :load_target, :loaded?, :scoped,
:to => :@association
delegate :target, :load_target, :loaded?, :to => :@association

delegate :select, :find, :first, :last,
:build, :create, :create!, :count,
Expand All @@ -55,34 +51,8 @@ def initialize(association)

alias_method :new, :build

def respond_to?(*args)
super ||
(load_target && target.respond_to?(*args)) ||
@association.klass.respond_to?(*args)
end

def method_missing(method, *args, &block)
if target.respond_to?(method) || (!@association.klass.respond_to?(method) && Class.respond_to?(method))
if load_target
if target.respond_to?(method)
target.send(method, *args, &block)
else
begin
super
rescue NoMethodError => e
raise e, e.message.sub(/ for #<.*$/, " via proxy for #{target}")
end
end
end
else
raise NoMethodError, "Couldn't find method `#{method}' via proxy for #{target}"
end
end

# Forwards <tt>===</tt> explicitly to the \target because the instance method
# removal above doesn't catch it. Loads the \target if needed.
def ===(other)
other === load_target
def proxy_association
@association
end

def to_ary
Expand All @@ -91,7 +61,7 @@ def to_ary
alias_method :to_a, :to_ary

def <<(*records)
@association.concat(records) && self
proxy_association.concat(records) && self
end
alias_method :push, :<<

Expand All @@ -101,7 +71,7 @@ def clear
end

def reload
@association.reload
proxy_association.reload
self
end

Expand Down
10 changes: 10 additions & 0 deletions lib/active_fedora/relation/delegation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,15 @@ module Delegation # :nodoc:
# for each different klass, and the delegations are compiled into that subclass only.

delegate :length, :collect, :map, :each, :all?, :include?, :to_ary, :to => :to_a


def method_missing(method, *args, &block)
if Array.method_defined?(method)
self.class.delegate method, :to => :to_a
to_a.send(method, *args, &block)
else
super
end
end
end
end

0 comments on commit 2c8fbf6

Please sign in to comment.