From e664d21d1b1b5c5291b149fb97806705f5b6acf8 Mon Sep 17 00:00:00 2001 From: Tomas Coufal Date: Wed, 7 Mar 2018 15:29:28 +0100 Subject: [PATCH] Fix ambiguous created_recently scope When used in combination with `with_reason_like` scope, the scope `create_recently` becomes ambiguous. --- app/models/miq_request.rb | 2 +- spec/models/miq_request_spec.rb | 43 +++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/app/models/miq_request.rb b/app/models/miq_request.rb index 23aaaa7fa32..34d06505449 100644 --- a/app/models/miq_request.rb +++ b/app/models/miq_request.rb @@ -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) } diff --git a/spec/models/miq_request_spec.rb b/spec/models/miq_request_spec.rb index f27c2a50794..4c2b12b2809 100644 --- a/spec/models/miq_request_spec.rb +++ b/spec/models/miq_request_spec.rb @@ -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.utc) + end + end + end end end