-
-
Save PerezIgnacio/b427f353bd8b8bbeb06be7c9e2b222b2 to your computer and use it in GitHub Desktop.
Example of Query Object extending scopes
This file contains 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 UsersQuery | |
attr_reader :relation | |
def initialize(relation = User.all) | |
@relation = relation.extending(Scopes) | |
end | |
def subscribed | |
relation.subscribed | |
end | |
def without_recent_views | |
relation.where('last_visited < ?', User::RECENT_VISITED_DAYS.days.ago) | |
end | |
def for_notifications_of_similar_purchases | |
relation.with_recent_purchases.subscribed | |
end | |
module Scopes | |
def subscribed | |
where(receive_email_notifications: true) | |
end | |
def with_recent_purchases | |
joins(:orders) | |
.where(fulfilled: true) | |
.and('orders.updated_at > ?', User::RECENT_ORDER_DAYS.days.ago) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment