Skip to content

Instantly share code, notes, and snippets.

@jourdein
Created November 26, 2020 07:20
Show Gist options
  • Save jourdein/ee243dd2dc78fa98301dabd21513f35c to your computer and use it in GitHub Desktop.
Save jourdein/ee243dd2dc78fa98301dabd21513f35c to your computer and use it in GitHub Desktop.
Module
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)
end
end
end
class User
include Extensions::Popular
end
module Extensions
module ClassEvaluator
def self.included(base)
base.class_eval do
# share an association
belong_to :parent
# share scope
scope :recent, where(:created_at => 1.day.ago..Time.now)
end
end
end
end
module Extensions
module Popular
def popularity
1 + (self.points/100)
end
def self.included(base)
# base. => defining class methods
def base.most_popular(limit = 10)
order('points DESC').limit(limit).all
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment