Skip to content

Commit

Permalink
Merge pull request #153 from sul-dlss/display_simplified
Browse files Browse the repository at this point in the history
Add Status#display_simplified
  • Loading branch information
mjgiarlo authored Feb 3, 2020
2 parents f82fc0b + 29bc3c8 commit 0952b4e
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 24 deletions.
52 changes: 34 additions & 18 deletions lib/dor/workflow/client/status.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,39 +42,49 @@ def initialize(druid:, version:, lifecycle_routes:)
end

# @return [Hash{Symbol => Object}] including :status_code and :status_time
# rubocop:disable Metrics/MethodLength
def info
# if we have an accessioned milestone, this is the last possible step and should be the status regardless of time stamp
accessioned_milestones = current_milestones.select { |m| m[:milestone] == 'accessioned' }
return { status_code: STEPS['accessioned'], status_time: accessioned_milestones.last[:at].utc.xmlschema } unless accessioned_milestones.empty?

status_code = 0
status_time = nil
# for each milestone in the current version, see if it comes at the same time or after the current 'last' step, if so, make it the last and record the date/time
current_milestones.each do |m|
m_name = m[:milestone]
m_time = m[:at].utc.xmlschema
next unless STEPS.key?(m_name) && (!status_time || m_time >= status_time)

status_code = STEPS[m_name]
status_time = m_time
@info ||= begin
# if we have an accessioned milestone, this is the last possible step and should be the status regardless of time stamp
accessioned_milestones = current_milestones.select { |m| m[:milestone] == 'accessioned' }
return { status_code: STEPS['accessioned'], status_time: accessioned_milestones.last[:at].utc.xmlschema } unless accessioned_milestones.empty?

status_code = 0
status_time = nil
# for each milestone in the current version, see if it comes at the same time or after the current 'last' step, if so, make it the last and record the date/time
current_milestones.each do |m|
m_name = m[:milestone]
m_time = m[:at].utc.xmlschema
next unless STEPS.key?(m_name) && (!status_time || m_time >= status_time)

status_code = STEPS[m_name]
status_time = m_time
end

{ status_code: status_code, status_time: status_time }
end
end
# rubocop:enable Metrics/MethodLength

{ status_code: status_code, status_time: status_time }
def status_code
info.fetch(:status_code)
end

# @param [Boolean] include_time
# @return [String] single composed status from status_info
def display(include_time: false)
status_info_hash = info
status_code = status_info_hash[:status_code]
status_time = status_info_hash[:status_time]
status_time = info[:status_time]

# use the translation table to get the appropriate verbage for the latest step
result = "v#{version} #{STATUS_CODE_DISP_TXT[status_code]}"
result += " #{format_date(status_time)}" if include_time
result
end

def display_simplified
simplified_status_code(STATUS_CODE_DISP_TXT[status_code])
end

def milestones
@milestones ||= lifecycle_routes.milestones('dor', druid)
end
Expand All @@ -83,6 +93,12 @@ def milestones

attr_reader :druid, :version, :lifecycle_routes

# @return [String] text translation of the status code, minus any trailing parenthetical explanation
# e.g. 'In accessioning (described)' and 'In accessioning (described, published)' both return 'In accessioning'
def simplified_status_code(display)
display.gsub(/\(.*\)$/, '').strip
end

def current_milestones
current = []
# only get steps that are part of accessioning and part of the current version. That can mean they were archived with the current version
Expand Down
25 changes: 19 additions & 6 deletions spec/workflow/client/status_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,6 @@
describe '#display' do
subject(:status) { instance.display }

before do
# TODO: this stub is too knowledgable about the inner workings of the LifecycleRoutest
# instead it should just stub :milestones which returns an array of hashes
# expect(lifecycle_routes).to receive(:query_lifecycle).and_return(xml)
end

context 'for gv054hp4128' do
context 'when current version is published, but does not have a version attribute' do
let(:xml) do
Expand Down Expand Up @@ -178,4 +172,23 @@
end
end
end

describe '#display_simplified' do
subject(:status) { instance.display_simplified }
let(:xml) do
'<?xml version="1.0" encoding="UTF-8"?>
<lifecycle objectId="druid:gv054hp4128">
<milestone date="2012-11-06T16:19:15-0800" version="2">described</milestone>
<milestone date="2012-11-06T16:21:02-0800">opened</milestone>
<milestone date="2012-11-06T16:30:03-0800">submitted</milestone>
<milestone date="2012-11-06T16:35:00-0800">described</milestone>
<milestone date="2012-11-06T16:59:39-0800" version="3">published</milestone>
<milestone date="2012-11-06T16:59:39-0800">published</milestone>
</lifecycle>'
end

it 'generates a status string' do
expect(status).to eq('In accessioning')
end
end
end

0 comments on commit 0952b4e

Please sign in to comment.