Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Example of Query Object extending scopes
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
You can’t perform that action at this time.