Skip to content

Instantly share code, notes, and snippets.

@PerezIgnacio
Created January 31, 2020 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PerezIgnacio/b427f353bd8b8bbeb06be7c9e2b222b2 to your computer and use it in GitHub Desktop.
Save PerezIgnacio/b427f353bd8b8bbeb06be7c9e2b222b2 to your computer and use it in GitHub Desktop.
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