Skip to content

Instantly share code, notes, and snippets.

@arches
Created July 20, 2012 14:25
Show Gist options
  • Select an option

  • Save arches/3151006 to your computer and use it in GitHub Desktop.

Select an option

Save arches/3151006 to your computer and use it in GitHub Desktop.
Write methods against a collection of ActiveRecord objects
module OrderCollectionExtension
def revenue
self.sum(:payment_total_in_cents)
end
end
Order.non_zero_price.extend(OrderCollectionExtension).revenue
# Probably going too far...
class Array # or would it be ActiveRelation?
def initialize(*args)
super
begin
extension_class = "#{first.class}CollectionExtension".constantize
self.extend(extension_class)
rescue NameError
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment