Skip to content

Commit

Permalink
Merge pull request drapergem#160 from nashby/refactor
Browse files Browse the repository at this point in the history
move finding association reflection to method
  • Loading branch information
steveklabnik committed Mar 27, 2012
2 parents 7d011a4 + a976e35 commit 2ee832e
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions lib/draper/base.rb
Expand Up @@ -69,15 +69,16 @@ def self.decorates_association(association_symbol, options = {})

return options[:with].decorate(orig_association) if options[:with]

if options[:polymorphic]
klass = orig_association.class
elsif model.class.respond_to?(:reflect_on_association) && model.class.reflect_on_association(association_symbol)
klass = model.class.reflect_on_association(association_symbol).klass
elsif orig_association.respond_to?(:first)
klass = orig_association.first.class
else
klass = orig_association.class
end
klass = if options[:polymorphic]
orig_association.class
elsif association_reflection = find_association_reflection(association_symbol)
association_reflection.klass
elsif orig_association.respond_to?(:first)
orig_association.first.class
else
orig_association.class
end

"#{klass}Decorator".constantize.decorate(orig_association, options)
end
end
Expand Down Expand Up @@ -259,5 +260,11 @@ def source
def allow?(method)
(allowed.empty? || allowed.include?(method) || FORCED_PROXY.include?(method)) && !denied.include?(method)
end

def find_association_reflection(association)
if model.class.respond_to?(:reflect_on_association)
model.class.reflect_on_association(association)
end
end
end
end

0 comments on commit 2ee832e

Please sign in to comment.