public
Last active

Extending models in Rails

  • Download Gist
gistfile1.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
# See http://blog.waxman.me/extending-your-models-in-rails-3 for original post
 
module Extensions
module Popular
 
module ClassMethods
def most_popular(limit=10)
order('points desc').limit(limit).all
end
end
 
def popularity
1+(self.points/100)
end
 
def self.included(base)
base.extend(ClassMethods)
base.extend(Extensions::OtherCoolStuff)
end
 
end
end
 
class User
include Extensions::Popular
end
 
class Tags
include Extensions::Popular
end

Could you help me out? How would you write rspec tests for the first one?

Also the comment on your site some how messes up future comments. Might consider deleting it.

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.