Created
November 26, 2020 07:20
-
-
Save jourdein/ee243dd2dc78fa98301dabd21513f35c to your computer and use it in GitHub Desktop.
Module
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
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 |
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
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 |
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
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