Created
August 21, 2015 19:04
-
-
Save davetapley/9ba50e7ec6b171dbe13d to your computer and use it in GitHub Desktop.
Rails decorator delegation with method missing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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