From 421074db8f46bb10a4e20073fcfd358dcc1e19ea Mon Sep 17 00:00:00 2001 From: Keenan Brock Date: Tue, 3 Jul 2018 11:16:41 -0400 Subject: [PATCH 1/2] MiqServer#zone_description is a virtual delegate This is a virtual column delegated to a belongs_to class Classic example of virtual_delegate --- app/models/miq_server.rb | 4 +--- spec/models/miq_server_spec.rb | 7 +++++++ 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/app/models/miq_server.rb b/app/models/miq_server.rb index 2623ce40d3b..0a19367df20 100644 --- a/app/models/miq_server.rb +++ b/app/models/miq_server.rb @@ -29,14 +29,12 @@ class MiqServer < ApplicationRecord before_destroy :validate_is_deleteable after_destroy :destroy_linked_events_queue - virtual_column :zone_description, :type => :string - default_value_for(:name, "EVM") default_value_for(:zone) { Zone.default_zone } scope :active_miq_servers, -> { where(:status => STATUSES_ACTIVE) } scope :with_zone_id, ->(zone_id) { where(:zone_id => zone_id) } - delegate :description, :to => :zone, :prefix => true + virtual_delegate :description, :to => :zone, :prefix => true STATUS_STARTING = 'starting'.freeze STATUS_STARTED = 'started'.freeze diff --git a/spec/models/miq_server_spec.rb b/spec/models/miq_server_spec.rb index 046e75ee4db..108f0ec715f 100644 --- a/spec/models/miq_server_spec.rb +++ b/spec/models/miq_server_spec.rb @@ -449,4 +449,11 @@ expect(described_class.new(:status => "stopped").active?).to be_falsey end end + + describe "#zone_description" do + it "delegates to zone" do + _, miq_server, zone = EvmSpecHelper.create_guid_miq_server_zone + expect(miq_server.zone_description).to eq(zone.description) + end + end end From 545e771d963e09337281309178c12e702cd937fb Mon Sep 17 00:00:00 2001 From: Keenan Brock Date: Tue, 3 Jul 2018 11:18:22 -0400 Subject: [PATCH 2/2] MiqServer#description - most models have desc If the server name is nil, then code sometimes blows up This defines the description on miq server so it acts like most of our other models displayed in the ui --- app/models/miq_server.rb | 2 ++ spec/models/miq_server_spec.rb | 7 +++++++ 2 files changed, 9 insertions(+) diff --git a/app/models/miq_server.rb b/app/models/miq_server.rb index 0a19367df20..3b621e41138 100644 --- a/app/models/miq_server.rb +++ b/app/models/miq_server.rb @@ -19,6 +19,8 @@ class MiqServer < ApplicationRecord acts_as_miq_taggable include RelationshipMixin + alias_attribute :description, :name + belongs_to :vm, :inverse_of => :miq_server belongs_to :zone has_many :messages, :as => :handler, :class_name => 'MiqQueue' diff --git a/spec/models/miq_server_spec.rb b/spec/models/miq_server_spec.rb index 108f0ec715f..b18ffe04972 100644 --- a/spec/models/miq_server_spec.rb +++ b/spec/models/miq_server_spec.rb @@ -456,4 +456,11 @@ expect(miq_server.zone_description).to eq(zone.description) end end + + describe "#description" do + it "doesnt blowup" do + s = described_class.new(:name => "name") + expect(s.description).to eq(s.name) + end + end end