Skip to content

Instantly share code, notes, and snippets.

@davetapley
Created August 21, 2015 19:04
Show Gist options
  • Save davetapley/9ba50e7ec6b171dbe13d to your computer and use it in GitHub Desktop.
Save davetapley/9ba50e7ec6b171dbe13d to your computer and use it in GitHub Desktop.
Rails decorator delegation with method missing
class << self
def method_missing(method, *args)
result = (method == :all) ? Account.scoped : Account.send(method, *args)
result.where(something: true).map { |a| self.new a }
rescue NoMethodError => e
if result.is_a? ActiveRecord::Relation
raise e
else
raise NoMethodError.new "Account.#{ method } did not return a relation", method, args
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment