From 59b709dacec8258aeb8c2d1ccc98d4f7261e6736 Mon Sep 17 00:00:00 2001 From: Xero Date: Mon, 4 Mar 2024 16:35:49 -0500 Subject: [PATCH] cleaner way to access dead and alive status + use that for plants --- app/models/plant.rb | 4 ++-- app/models/status.rb | 16 ++++++++++++++++ spec/models/status_spec.rb | 18 ++++++++++++++++++ 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/app/models/plant.rb b/app/models/plant.rb index 32c66bd..09348d0 100644 --- a/app/models/plant.rb +++ b/app/models/plant.rb @@ -17,11 +17,11 @@ def trefle_data class << self def dead - Plant.where(status_id: Status.find_by(name: 'Dead')) + Plant.where(status_id: Status.dead) end def alive - Plant.where(status_id: Status.find_by(name: 'Alive')) + Plant.where(status_id: Status.alive) end end end diff --git a/app/models/status.rb b/app/models/status.rb index f26f779..d307885 100644 --- a/app/models/status.rb +++ b/app/models/status.rb @@ -1,3 +1,19 @@ class Status < ApplicationRecord has_many :plants + + DEAD = 'Dead'.freeze + + ALIVE = 'Alive'.freeze + + class << self + # @return [Status] + def dead + find_by(name: DEAD) + end + + # @return [Status] + def alive + find_by(name: ALIVE) + end + end end diff --git a/spec/models/status_spec.rb b/spec/models/status_spec.rb index 3cb6281..612250b 100644 --- a/spec/models/status_spec.rb +++ b/spec/models/status_spec.rb @@ -6,4 +6,22 @@ expect(subject).to all(be_instance_of(Plant)) end end + + describe '.dead' do + subject { Status.dead } + + it do + expect(subject).to be_instance_of(Status) + expect(subject.name).to eql('Dead') + end + end + + describe '.alive' do + subject { Status.alive } + + it do + expect(subject).to be_instance_of(Status) + expect(subject.name).to eql('Alive') + end + end end \ No newline at end of file