Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix policy_events relationship on VmOrTemplate #17036

Merged
merged 1 commit into from
Mar 16, 2018
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/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