Skip to content

Commit

Permalink
Merge e781c68 into 4315de8
Browse files Browse the repository at this point in the history
  • Loading branch information
jcoyne committed Nov 30, 2018
2 parents 4315de8 + e781c68 commit 642015e
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 12 deletions.
17 changes: 8 additions & 9 deletions .rubocop_todo.yml
@@ -1,6 +1,6 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2018-10-11 13:34:20 -0700 using RuboCop version 0.57.2.
# on 2018-11-30 16:59:33 -0600 using RuboCop version 0.57.2.
# 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 @@ -56,12 +56,11 @@ Layout/SpaceInsideArrayLiteralBrackets:
Exclude:
- 'config/environments/production.rb'

# Offense count: 21
# Offense count: 12
# Cop supports --auto-correct.
# Configuration parameters: AllowInHeredoc.
Layout/TrailingWhitespace:
Exclude:
- 'app/controllers/objects_controller.rb'
- 'app/controllers/sdr_controller.rb'
- 'app/controllers/workflows_controller.rb'
- 'config/routes.rb'
Expand Down Expand Up @@ -181,7 +180,7 @@ RSpec/EmptyLineAfterSubject:
Exclude:
- 'spec/models/symphony_reader_spec.rb'

# Offense count: 26
# Offense count: 27
# Configuration parameters: Max.
RSpec/ExampleLength:
Exclude:
Expand All @@ -192,6 +191,7 @@ RSpec/ExampleLength:
- 'spec/dor/goobi_spec.rb'
- 'spec/dor/service_item_spec.rb'
- 'spec/dor/update_marc_record_service_spec.rb'
- 'spec/services/release_tags_spec.rb'

# Offense count: 1
RSpec/ExpectInHook:
Expand All @@ -218,7 +218,7 @@ RSpec/MessageSpies:
- 'spec/dor/goobi_spec.rb'
- 'spec/dor/update_marc_record_service_spec.rb'

# Offense count: 47
# Offense count: 49
# Configuration parameters: AggregateFailuresByDefault.
RSpec/MultipleExpectations:
Max: 6
Expand Down Expand Up @@ -308,7 +308,7 @@ Style/GuardClause:
Exclude:
- 'config/initializers/okcomputer.rb'

# Offense count: 53
# Offense count: 45
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle, UseHashRocketsWithSymbolValues, PreferHashRocketsForNonAlnumEndingSymbols.
# SupportedStyles: ruby19, hash_rockets, no_mixed_keys, ruby19_no_mixed_keys
Expand Down Expand Up @@ -336,15 +336,14 @@ Style/IfUnlessModifier:
Style/NumericLiterals:
MinDigits: 11

# Offense count: 13
# Offense count: 12
# Cop supports --auto-correct.
# Configuration parameters: PreferredDelimiters.
Style/PercentLiteralDelimiters:
Exclude:
- 'app/controllers/sdr_controller.rb'
- 'app/models/dor/update_marc_record_service.rb'
- 'config/deploy.rb'
- 'config/deploy/dev.rb'
- 'config/deploy/prod.rb'
- 'config/deploy/stage.rb'
- 'config/initializers/okcomputer.rb'
Expand All @@ -365,7 +364,7 @@ Style/StringLiterals:
- 'spec/rails_helper.rb'
- 'spec/spec_helper.rb'

# Offense count: 2
# Offense count: 3
# Cop supports --auto-correct.
# Configuration parameters: MinSize.
# SupportedStyles: percent, brackets
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/objects_controller.rb
Expand Up @@ -65,7 +65,7 @@ def release_tags
raw_params.symbolize_keys!

if raw_params.key?(:release) && (raw_params[:release] == true || raw_params[:release] == false)
@item.add_release_node(raw_params[:release], raw_params)
ReleaseTags.create(@item, raw_params.slice(:release, :what, :to, :who, :when))
@item.save
head :created
else
Expand Down
41 changes: 41 additions & 0 deletions app/services/release_tags.rb
@@ -0,0 +1,41 @@
# frozen_string_literal: true

# Creates workspaces. This replaces https://github.com/sul-dlss/dor-services/blob/master/lib/dor/models/concerns/assembleable.rb
class ReleaseTags
# Add a release node for the item
# Will use the current time if timestamp not supplied. You can supply a timestap for correcting history, etc if desired
# Timestamp will be calculated by the function
#
# @param work [Dor::Item] the work to create the release tag for
# @param [Hash] params all the other stuff
# @option params [Boolean] release: True or false for the release node
# @return [Nokogiri::XML::Element] the tag added if successful
# @raise [ArgumentError] Raised if attributes are improperly supplied
#
# @example
# ReleaseTags.create(item, release: true, what: 'self', to: 'Searchworks', who: 'petucket'})
def self.create(work, attrs)
release = attrs.delete(:release)
attrs[:when] ||= Time.now.utc.iso8601 # add the timestamp
validate_release_attributes(release, attrs)
work.identityMetadata.add_value(:release, release.to_s, attrs)
end

# Determine if the supplied tag is a valid release node that meets all requirements
#
# @param tag [Boolean] True or false for the release node
# @param attrs [hash] A hash of attributes for the tag, must contain :when, a ISO 8601 timestamp and :who to identify who or what added the tag, :to,
# @raise [ArgumentError] Raises an error of the first fault in the release tag
# @return [Boolean] Returns true if no errors found
def self.validate_release_attributes(tag, attrs = {})
raise ArgumentError, ':when is not iso8601' if attrs[:when].match('\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z').nil?

[:who, :to, :what].each do |check_attr|
next unless attrs[check_attr]
raise ArgumentError, "#{check_attr} not supplied as a String" unless attrs[check_attr].is_a? String
end
raise ArgumentError, ':what must be self or collection' unless %w[self collection].include? attrs[:what]
raise ArgumentError, 'the value set for this tag is not a boolean' unless [true, false].include? tag
end
private_class_method :validate_release_attributes
end
4 changes: 2 additions & 2 deletions spec/controllers/objects_controller_spec.rb
Expand Up @@ -113,14 +113,14 @@

describe '/release_tags' do
it 'adds a release tag when posted to with false' do
expect(item).to receive(:add_release_node).with(false, :to => 'searchworks', :who => 'carrickr', :what => 'self', :release => false)
expect(ReleaseTags).to receive(:create).with(AssembleableVersionableItem, release: false, to: 'searchworks', who: 'carrickr', what: 'self')
expect(item).to receive(:save)
post :release_tags, params: { id: item.pid }, body: %( {"to":"searchworks","who":"carrickr","what":"self","release":false} )
expect(response.status).to eq(201)
end

it 'adds a release tag when posted to with true' do
expect(item).to receive(:add_release_node).with(true, :to => 'searchworks', :who => 'carrickr', :what => 'self', :release => true)
expect(ReleaseTags).to receive(:create).with(AssembleableVersionableItem, release: true, to: 'searchworks', who: 'carrickr', what: 'self')
expect(item).to receive(:save)
post :release_tags, params: { id: item.pid }, body: %( {"to":"searchworks","who":"carrickr","what":"self","release":true} )
expect(response.status).to eq(201)
Expand Down
22 changes: 22 additions & 0 deletions spec/services/release_tags_spec.rb
@@ -0,0 +1,22 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe ReleaseTags do
describe '.create' do
before do
allow(Dor::SuriService).to receive(:mint_id).and_return('druid:aa123bb7890')
end

let(:work) { Dor::Item.new }

it 'creates a plain directory in the workspace when passed no source directory' do
described_class.create(work, release: true, what: 'self', when: '2018-11-30T22:41:35Z')
expect(work.identityMetadata.to_xml).to be_equivalent_to <<-XML
<identityMetadata>
<release what="self" when="2018-11-30T22:41:35Z">true</release>
</identityMetadata>
XML
end
end
end

0 comments on commit 642015e

Please sign in to comment.