Skip to content

Commit

Permalink
Merge pull request ManageIQ#17036 from jntullo/bz_1546995
Browse files Browse the repository at this point in the history
Fix policy_events relationship on VmOrTemplate
  • Loading branch information
kbrock authored Mar 16, 2018
2 parents 825a676 + adc676c commit 9472102
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
2 changes: 1 addition & 1 deletion app/models/vm_or_template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ class VmOrTemplate < ApplicationRecord
has_many :ems_events_src, :class_name => "EmsEvent"
has_many :ems_events_dest, :class_name => "EmsEvent", :foreign_key => :dest_vm_or_template_id

has_many :policy_events, -> { where(["target_id = ? OR target_class = 'VmOrTemplate'", id]).order(:timestamp) }, :class_name => "PolicyEvent"
has_many :policy_events, ->(vm) { where(["target_id = ? AND target_class = 'VmOrTemplate'", vm.id]).order(:timestamp) }, :foreign_key => "target_id"

has_many :miq_events, :as => :target, :dependent => :destroy

Expand Down
8 changes: 8 additions & 0 deletions spec/models/vm_or_template_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1229,4 +1229,12 @@
expect(Vm.new(:location => "test location", :storage => storage).v_datastore_path).to eq("storage name/test location")
end
end

context "#policy_events" do
it "returns the policy events with target class of VmOrTemplate and target_id of the vm" do
policy_event = FactoryGirl.create(:policy_event, :target_class => "VmOrTemplate", :target_id => vm.id)

expect(vm.policy_events).to eq([policy_event])
end
end
end

0 comments on commit 9472102

Please sign in to comment.