Skip to content

Commit

Permalink
define PersistentVolumeClaim#persistent_volume as a relation
Browse files Browse the repository at this point in the history
define the relation and clean up some rubocops
  • Loading branch information
kbrock committed Jun 21, 2023
1 parent 58ffa23 commit bbe966b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
10 changes: 4 additions & 6 deletions app/models/persistent_volume_claim.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
class PersistentVolumeClaim < ApplicationRecord
belongs_to :ext_management_system, :foreign_key => "ems_id"
# technically this belongs_to :class_name => "ManageIQ::Providers::ContainerManager"
belongs_to :ext_management_system, :foreign_key => "ems_id", :inverse_of => :persistent_volume_claims
belongs_to :container_project
has_many :container_volumes
has_many :container_volumes, :inverse_of => :persistent_volume_claim # rubocop:disable Rails/HasManyOrHasOneDependent
has_one :persistent_volume, -> { where(:type => 'PersistentVolume') }, :class_name => "ContainerVolume", :inverse_of => :persistent_volume_claim # rubocop:disable Rails/HasManyOrHasOneDependent
serialize :capacity, Hash
serialize :requests, Hash
serialize :limits, Hash

virtual_column :storage_capacity, :type => :integer

def persistent_volume
container_volumes.find_by_type('PersistentVolume')
end

def storage_capacity
capacity[:storage]
end
Expand Down
15 changes: 15 additions & 0 deletions spec/models/persistent_volume_claim_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,19 @@
expect(persistent_volume.storage_capacity).to be nil
end
end

describe "#persistent_volume" do
it "finds none" do
pvc = FactoryBot.create(:persistent_volume_claim)
FactoryBot.create(:container_volume, :persistent_volume_claim => pvc)
expect(pvc.persistent_volume).to be_nil
end

it "finds one" do
pvc = FactoryBot.create(:persistent_volume_claim)
FactoryBot.create(:container_volume, :persistent_volume_claim => pvc)
pv = FactoryBot.create(:persistent_volume, :persistent_volume_claim => pvc)
expect(pvc.persistent_volume).to eq(pv)
end
end
end

0 comments on commit bbe966b

Please sign in to comment.