Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/models/miq_task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class MiqTask < ApplicationRecord
scope :completed_warn, -> { finished.where(:status => 'Warn') }
scope :completed_error, -> { finished.where(:status => 'Error') }
scope :no_status_selected, -> { running.where.not(:status => %(Ok Error Warn)) }
scope :with_status_in, ->(s, *rest) { rest.reduce(MiqTask.send(s)) { |chain, r| chain.or(MiqTask.send(r)) } }
scope :with_status_in, ->(*scopes) { merge(scopes.collect { |s| MiqTask.unscoped.send(s) }.inject(:or)) }

def ensure_started
self.started_on ||= Time.now.utc if state == STATE_ACTIVE
Expand Down
20 changes: 20 additions & 0 deletions spec/models/miq_task_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,26 @@
end
end

describe ".with_status_in" do
it "does not discard prior scope conditions" do
task = FactoryBot.create(:miq_task, :state => 'Finished', :status => 'Ok', :zone => 'East')
FactoryBot.create(:miq_task, :state => 'Finished', :status => 'Ok', :zone => 'West')

results = MiqTask.with_zone('East').with_status_in('completed_ok')
expect(results).to eq([task])
end

it "does not discard prior scope conditions when combining multiple statuses" do
task_ok = FactoryBot.create(:miq_task, :state => 'Finished', :status => 'Ok', :zone => 'East')
task_warn = FactoryBot.create(:miq_task, :state => 'Finished', :status => 'Warn', :zone => 'East')
FactoryBot.create(:miq_task, :state => 'Finished', :status => 'Error', :zone => 'East')
FactoryBot.create(:miq_task, :state => 'Finished', :status => 'Ok', :zone => 'West')

results = MiqTask.with_zone('East').with_status_in('completed_ok', 'completed_warn')
expect(results).to match_array([task_ok, task_warn])
end
end

private

def create_test_task(name, status, updated)
Expand Down
Loading