Skip to content

Instantly share code, notes, and snippets.

@adelevie
Created February 12, 2016 20:53
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 adelevie/67fbde8c66b6bf476f6f to your computer and use it in GitHub Desktop.
Save adelevie/67fbde8c66b6bf476f6f to your computer and use it in GitHub Desktop.
class AuctionQuery
def initialize(relation = Auction.all)
@relation = relation.extending(Scopes)
end
module Scopes
def delivery_deadline_expired
where('delivery_deadline < ?', Time.zone.now)
end
def accepted
where(result: accepted_enum)
end
def delivered
where.not(delivery_url: [nil, ""])
end
def cap_submitted
where.not(cap_proposal_url: [nil, ""])
end
def paid
where(
awardee_paid_status: paid_enum
)
end
def not_paid
where(
awardee_paid_status: not_paid_enum
)
end
private
def paid_enum
Auction.awardee_paid_statuses['paid']
end
def not_paid_enum
Auction.awardee_paid_statuses['not_paid']
end
def accepted_enum
Auction.results['accepted']
end
end
[
:delivery_deadline_expired,
:accepted,
:delivered,
:cap_submitted,
:paid,
:not_paid
].each do |key|
define_method key do
@relation.send(key)
end
end
def complete_and_successful
@relation
.delivery_deadline_expired
.delivered
.accepted
.cap_submitted
.paid
end
def payment_pending
@relation
.delivery_deadline_expired
.delivered.accepted
.cap_submitted
.not_paid
end
end
@baccigalupi
Copy link

Cool!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment