Skip to content

Instantly share code, notes, and snippets.

@adelevie
Last active February 12, 2016 18:46
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/32d3e164bf1034e9c38f to your computer and use it in GitHub Desktop.
Save adelevie/32d3e164bf1034e9c38f to your computer and use it in GitHub Desktop.
class AuctionQuery
def initialize(relation = Auction.all)
@relation = relation
end
def delivery_deadline_expired
@relation.where('delivery_deadline < ?', Time.zone.now)
end
def accepted
@relation.where(result: accepted_enum)
end
def delivered
@relation.where.not(delivery_url: [nil, ""])
end
def cap_submitted
@relation.where.not(cap_proposal_url: [nil, ""])
end
def paid
@relation.where(
awardee_paid_status: paid_enum
)
end
def complete_and_successful
delivery_deadline_expired
accepted
delivered
cap_submitted
paid
end
private
def paid_enum
Auction.awardee_paid_statuses['paid']
end
def accepted_enum
Auction.results['accepted']
end
end
require 'rails_helper'
RSpec.describe AuctionQuery do
let(:query) { AuctionQuery.new }
describe '#accepted' do
let(:accepted_auction) { FactoryGirl.create(:auction, :accepted) }
let!(:rejected_auction) { FactoryGirl.create(:auction, result: :rejected) }
it 'should return only accepted auctions' do
expect(query.accepted).to match_array([accepted_auction])
end
end
describe '#delivery_deadline_expired' do
let!(:running_auction) { FactoryGirl.create(:auction, :running) }
let(:dd_expired_auction) do
FactoryGirl.create(:auction, :delivery_deadline_expired)
end
it 'should return only delivery deadline expired auctions' do
expect(query.delivery_deadline_expired).to match_array([dd_expired_auction])
end
end
describe '#delivered' do
let(:delivered_auction) { FactoryGirl.create(:auction, :delivered) }
let!(:running_auction) { FactoryGirl.create(:auction, :running) }
it 'should return only delivered auctions' do
expect(query.delivered).to match_array([delivered_auction])
end
end
describe '#cap_submitted' do
let(:cap_submitted) { FactoryGirl.create(:auction, :cap_submitted) }
let!(:running_auction) { FactoryGirl.create(:auction, :running) }
it 'should return only cap_submitted auctions' do
expect(query.cap_submitted).to match_array([cap_submitted])
end
end
describe '#paid' do
let(:paid_auction) { FactoryGirl.create(:auction, :paid) }
let!(:running_auction) { FactoryGirl.create(:auction, :running) }
it 'should only return paid auctions' do
expect(query.paid).to match_array([paid_auction])
end
end
describe '#complete_and_successful' do
let(:complete_and_successful) do
FactoryGirl.create(:auction, :complete_and_successful)
end
let!(:running_auction) { FactoryGirl.create(:auction, :running) }
let!(:rejected_auction) { FactoryGirl.create(:auction, result: :rejected) }
let!(:unpaid_auction) do
FactoryGirl.create(:auction, awardee_paid_status: :not_paid)
end
it 'should only return complete and successful auctions' do
expect(query.complete_and_successful)
.to match_array([complete_and_successful])
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment