Skip to content

Commit

Permalink
Move VersionService from dor-services
Browse files Browse the repository at this point in the history
  • Loading branch information
jcoyne committed Apr 11, 2019
1 parent 78b2217 commit d00e5b3
Show file tree
Hide file tree
Showing 5 changed files with 275 additions and 20 deletions.
33 changes: 22 additions & 11 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2019-03-18 15:30:42 -0500 using RuboCop version 0.65.0.
# on 2019-04-11 13:57:00 -0500 using RuboCop version 0.65.0.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
Expand Down Expand Up @@ -29,7 +29,7 @@ Lint/UriEscapeUnescape:
- 'app/controllers/sdr_controller.rb'
- 'spec/controllers/sdr_controller_spec.rb'

# Offense count: 13
# Offense count: 15
Metrics/AbcSize:
Max: 119

Expand All @@ -38,16 +38,16 @@ Metrics/AbcSize:
Metrics/ClassLength:
Max: 139

# Offense count: 2
# Offense count: 3
Metrics/CyclomaticComplexity:
Max: 24

# Offense count: 21
# Offense count: 22
# Configuration parameters: CountComments, ExcludedMethods.
Metrics/MethodLength:
Max: 58

# Offense count: 2
# Offense count: 3
Metrics/PerceivedComplexity:
Max: 24

Expand Down Expand Up @@ -93,7 +93,7 @@ RSpec/BeforeAfterAll:
- 'spec/support/**/*.rb'
- 'spec/dor/update_marc_record_service_spec.rb'

# Offense count: 18
# Offense count: 20
# Configuration parameters: Prefixes.
# Prefixes: when, with, without
RSpec/ContextWording:
Expand All @@ -102,6 +102,7 @@ RSpec/ContextWording:
- 'spec/controllers/sdr_controller_spec.rb'
- 'spec/dor/update_marc_record_service_spec.rb'
- 'spec/services/registration_service_spec.rb'
- 'spec/services/version_service_spec.rb'

# Offense count: 1
RSpec/DescribeClass:
Expand Down Expand Up @@ -133,7 +134,7 @@ RSpec/EmptyLineAfterSubject:
Exclude:
- 'spec/models/symphony_reader_spec.rb'

# Offense count: 31
# Offense count: 34
# Configuration parameters: Max.
RSpec/ExampleLength:
Exclude:
Expand All @@ -145,6 +146,7 @@ RSpec/ExampleLength:
- 'spec/dor/update_marc_record_service_spec.rb'
- 'spec/services/registration_service_spec.rb'
- 'spec/services/release_tags_spec.rb'
- 'spec/services/version_service_spec.rb'

# Offense count: 4
RSpec/ExpectInHook:
Expand All @@ -161,7 +163,7 @@ RSpec/InstanceVariable:
- 'spec/dor/update_marc_record_service_spec.rb'
- 'spec/services/registration_service_spec.rb'

# Offense count: 26
# Offense count: 54
# Configuration parameters: EnforcedStyle.
# SupportedStyles: have_received, receive
RSpec/MessageSpies:
Expand All @@ -172,11 +174,12 @@ RSpec/MessageSpies:
- 'spec/dor/goobi_spec.rb'
- 'spec/dor/update_marc_record_service_spec.rb'
- 'spec/services/registration_service_spec.rb'
- 'spec/services/version_service_spec.rb'

# Offense count: 55
# Offense count: 65
# Configuration parameters: AggregateFailuresByDefault.
RSpec/MultipleExpectations:
Max: 5
Max: 8

# Offense count: 9
RSpec/NestedGroups:
Expand Down Expand Up @@ -210,14 +213,15 @@ RSpec/SubjectStub:
Exclude:
- 'spec/dor/update_marc_record_service_spec.rb'

# Offense count: 17
# Offense count: 18
# Configuration parameters: IgnoreNameless, IgnoreSymbolicNames.
RSpec/VerifiedDoubles:
Exclude:
- 'spec/controllers/objects_controller_spec.rb'
- 'spec/dor/service_item_spec.rb'
- 'spec/dor/update_marc_record_service_spec.rb'
- 'spec/rails_helper.rb'
- 'spec/services/version_service_spec.rb'

# Offense count: 3
# Configuration parameters: EnforcedStyle.
Expand All @@ -228,6 +232,13 @@ Rails/FilePath:
- 'config/environments/development.rb'
- 'spec/rails_helper.rb'

# Offense count: 3
# Configuration parameters: EnforcedStyle.
# SupportedStyles: strict, flexible
Rails/TimeZone:
Exclude:
- 'spec/services/version_service_spec.rb'

# Offense count: 2
Style/CommentedKeyword:
Exclude:
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/versions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class VersionsController < ApplicationController
before_action :load_item

def create
Dor::VersionService.open(@item, open_params)
VersionService.open(@item, open_params)
render plain: @item.current_version
rescue Dor::Exception => e
render build_error(e)
Expand All @@ -13,7 +13,7 @@ def current
end

def close_current
Dor::VersionService.close(@item, close_params)
VersionService.close(@item, close_params)
render plain: "version #{@item.current_version} closed"
end

Expand Down
82 changes: 82 additions & 0 deletions app/services/version_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# frozen_string_literal: true

# Open and close versions
class VersionService
def self.open(work, opts = {})
new(work).open(opts)
end

def self.close(work, opts = {})
new(work).close(opts)
end

def initialize(work)
@work = work
end

# Increments the version number and initializes versioningWF for the object
# @param [Hash] opts optional params
# @option opts [Boolean] :assume_accessioned If true, does not check whether object has been accessioned.
# @option opts [Boolean] :create_workflows_ds If false, create_workflow() will not initialize the workflows datastream.
# @option opts [Hash] :vers_md_upd_info If present, used to add to the events datastream and set the desc and significance on the versionMetadata datastream
# @raise [Dor::Exception] if the object hasn't been accessioned, or if a version is already opened
def open(opts = {})
# During local development, we need a way to open a new version even if the object has not been accessioned.
raise(Dor::Exception, 'Object net yet accessioned') unless
opts[:assume_accessioned] || Dor::Config.workflow.client.lifecycle('dor', work.pid, 'accessioned')
raise Dor::VersionAlreadyOpenError, 'Object already opened for versioning' if open?
raise Dor::Exception, 'Object currently being accessioned' if Dor::Config.workflow.client.active_lifecycle('dor', work.pid, 'submitted')

sdr_version = Sdr::Client.current_version work.pid

vmd_ds = work.versionMetadata
vmd_ds.sync_then_increment_version sdr_version
vmd_ds.save unless work.new_record?

k = :create_workflows_ds
if opts.key?(k)
# During local development, Hydrus (or another app w/ local Fedora) does not want to initialize workflows datastream.
Dor::CreateWorkflowService.create_workflow(work, name: 'versioningWF', create_ds: opts[k])
else
Dor::CreateWorkflowService.create_workflow(work, name: 'versioningWF')
end

vmd_upd_info = opts[:vers_md_upd_info]
return unless vmd_upd_info

work.events.add_event('open', vmd_upd_info[:opening_user_name], "Version #{vmd_ds.current_version_id} opened")
vmd_ds.update_current_version(description: vmd_upd_info[:description], significance: vmd_upd_info[:significance].to_sym)
work.save
end

# Sets versioningWF:submit-version to completed and initiates accessionWF for the object
# @param [Hash] opts optional params
# @option opts [String] :description describes the version change
# @option opts [Symbol] :significance which part of the version tag to increment
# :major, :minor, :admin (see Dor::VersionTag#increment)
# @option opts [String] :version_num version number to archive rows with. Otherwise, current version is used
# @option opts [Boolean] :start_accesion set to true if you want accessioning to start (default), false otherwise
# @raise [Dor::Exception] if the object hasn't been opened for versioning, or if accessionWF has
# already been instantiated or the current version is missing a tag or description
def close(opts = {})
unless opts.empty?
work.versionMetadata.update_current_version opts
work.versionMetadata.save
end

raise Dor::Exception, 'latest version in versionMetadata requires tag and description before it can be closed' unless work.versionMetadata.current_version_closeable?
raise Dor::Exception, 'Trying to close version on an object not opened for versioning' unless open?
raise Dor::Exception, 'accessionWF already created for versioned object' if Dor::Config.workflow.client.active_lifecycle('dor', work.pid, 'submitted')

Dor::Config.workflow.client.close_version 'dor', work.pid, opts.fetch(:start_accession, true) # Default to creating accessionWF when calling close_version
end

# @return [Boolean] true if 'opened' lifecycle is active, false otherwise
def open?
return true if Dor::Config.workflow.client.active_lifecycle('dor', work.pid, 'opened')

false
end

attr_reader :work
end
14 changes: 7 additions & 7 deletions spec/controllers/versions_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@

describe '/versions/current/close' do
it 'closes the current version when posted to' do
expect(Dor::VersionService).to receive(:close)
expect(VersionService).to receive(:close)
post :close_current, params: { object_id: item.pid }, as: :json
expect(response.body).to match(/version 1 closed/)
end

it 'forwards optional params to the Dor::VersionService#close method' do
expect(Dor::VersionService).to receive(:close).with(item, description: 'some text', significance: :major)
it 'forwards optional params to the VersionService#close method' do
expect(VersionService).to receive(:close).with(item, description: 'some text', significance: :major)
post :close_current, params: { object_id: item.pid }, body: %( {"description": "some text", "significance": "major"} ), as: :json
expect(response.body).to match(/version 1 closed/)
end
Expand Down Expand Up @@ -56,7 +56,7 @@
context 'when opening a version succeedes' do
before do
# Do not test version service side effects in dor-services-app; that is dor-services' responsibility
allow(Dor::VersionService).to receive(:open)
allow(VersionService).to receive(:open)
end

it 'opens a new object version when posted to' do
Expand All @@ -65,9 +65,9 @@
expect(response).to be_successful
end

it 'forwards optional params to the Dor::VersionService#open method' do
it 'forwards optional params to the VersionService#open method' do
post :create, params: { object_id: item.pid }, body: open_params.to_json, as: :json
expect(Dor::VersionService).to have_received(:open).with(item, open_params)
expect(VersionService).to have_received(:open).with(item, open_params)
expect(response.body).to eq('2')
expect(response).to be_successful
end
Expand All @@ -76,7 +76,7 @@
context 'when opening a version fails' do
before do
# Do not test version service side effects in dor-services-app; that is dor-services' responsibility
allow(Dor::VersionService).to receive(:open).and_raise(Dor::Exception, 'Object net yet accessioned')
allow(VersionService).to receive(:open).and_raise(Dor::Exception, 'Object net yet accessioned')
end

it 'returns an error' do
Expand Down
Loading

0 comments on commit d00e5b3

Please sign in to comment.