Skip to content

Commit

Permalink
cleaner way to access dead and alive status + use that for plants
Browse files Browse the repository at this point in the history
  • Loading branch information
one-m1nd committed Mar 4, 2024
1 parent dbf8f7d commit 59b709d
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
4 changes: 2 additions & 2 deletions app/models/plant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
16 changes: 16 additions & 0 deletions app/models/status.rb
Original file line number Diff line number Diff line change
@@ -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
18 changes: 18 additions & 0 deletions spec/models/status_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 59b709d

Please sign in to comment.