Skip to content

Commit

Permalink
Fix ambiguous created_recently scope
Browse files Browse the repository at this point in the history
When used in combination with `with_reason_like` scope, the scope
`create_recently` becomes ambiguous.
  • Loading branch information
tumido committed Mar 7, 2018
1 parent e55ac98 commit 804ba4e
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
2 changes: 1 addition & 1 deletion app/models/miq_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class MiqRequest < ApplicationRecord

include MiqRequestMixin

scope :created_recently, ->(days_ago) { where("created_on > ?", days_ago.days.ago) }
scope :created_recently, ->(days_ago) { where("miq_requests.created_on > ?", days_ago.days.ago) }
scope :with_approval_state, ->(state) { where(:approval_state => state) }
scope :with_type, ->(type) { where(:type => type) }
scope :with_request_type, ->(type) { where(:request_type => type) }
Expand Down
43 changes: 43 additions & 0 deletions spec/models/miq_request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,49 @@

request.deny(fred, reason)
end

describe ".with_reason_like" do
let(:reason) { %w(abcd abcde cde) }
subject { described_class.with_reason_like(pattern).count }

before { request.miq_approvals = approvals }

["ab*", "*bc*", "*de"].each do |pattern|
context "'#{pattern}'" do
let(:pattern) { pattern }
it { is_expected.to eq(2) }
end
end

context "integrates well with .created_recently" do
# when joined with MiqApprovals, there are two `created_on` columns
let(:pattern) { "*c*" }
subject { described_class.with_reason_like(pattern).created_recently(days_ago).distinct.count }

before do
FactoryGirl.create(:vm_migrate_request, :requester => fred, :created_on => 10.days.ago, :miq_approvals => approvals)
FactoryGirl.create(:vm_migrate_request, :requester => fred, :created_on => 14.days.ago, :miq_approvals => approvals)
FactoryGirl.create(:vm_migrate_request, :requester => fred, :created_on => 3.days.ago, :miq_approvals => approvals)
end

{
7 => 2,
11 => 3,
15 => 4
}.each do |days, count|
context "filtering #{days} ago" do
let(:days_ago) { days }
it { is_expected.to eq(count) }
end
end
end

def approvals
reason.collect do |r|
FactoryGirl.create(:miq_approval, :approver => fred, :reason => r, :stamper => barney, :stamped_on => Time.now)
end
end
end
end
end

Expand Down

0 comments on commit 804ba4e

Please sign in to comment.