Class: Hyrax::EmbargoManager

Inherits:
Object
  • Object
show all
Defined in:
app/services/hyrax/embargo_manager.rb

Overview

Provides utilities for managing the lifecycle of an ‘Hyrax::Embargo` on a `Hyrax::Resource`.

The embargo terminology used here is as follows:

- "Release Date" is the day an embargo is scheduled to be released.
- "Under Embargo" means the embargo is "active"; i.e. that its release
   date is today or later.
- "Applied" means the embargo's pre-release visibility has been set on
  the resource.
- "Enforced" means the object's visibility matches the pre-release
  visibility of the embargo; i.e. the embargo has been applied,
  but not released.
- "Released" means the embargo's post-release visibility has been set on
  the resource.
- "Deactivate" means that the existing embargo will be removed, even
  if it active.

Note that an resource may be ‘#under_embargo?` even if the embargo is not be `#enforced?` (in this case, the application should seek to apply the embargo, e.g. via a scheduled job). Additionally, an embargo may be `#enforced?` after its release date (in this case, the application should seek to release the embargo).

Examples:

check whether a resource is under an active embargo

manager = EmbargoManager.new(resource: my_resource)
manager.under_embargo? # => false

applying an embargo

embargo = Hyrax::Embargo.new(visibility_during_embargo: 'restricted',
                             visibility_after_embargo:  'open',
                             embargo_release_date:      Time.zone.today + 1000)

resource            = Hyrax::Resource.new(embargo: embargo)
resource.visibility = 'open'

manager = EmbargoManager.new(resource: resource)

manager.apply!
manager.enforced? => true
resource.visibility # => 'restricted'

releasing an embargo

embargo = Hyrax::Embargo.new(visibility_during_embargo: 'restricted',
                             visibility_after_embargo:  'open',
                             embargo_release_date:      Time.zone.today + 1000)

releasing an embargo

embargo = Hyrax::Embargo.new(visibility_during_embargo: 'restricted',
                             visibility_after_embargo:  'open',
                             embargo_release_date:      Time.zone.today + 1)

resource = Hyrax::Resource.new(embargo: embargo)
manager  = EmbargoManager.new(resource: resource)

manager.under_embargo? => true
manager.enforced? => false

manager.apply!

resource.visibility # => 'restricted'
manager.enforced? => true

manager.release! # => NotReleasableError

# <spongebob narrator>ONE DAY LATER</spongebob narrator>
manager.under_embargo? => false
manager.enforced? => true

manager.release!

resource.visibility # => 'open'
manager.enforced? => false

Defined Under Namespace

Classes: NotEnforcableError, NotReleasableError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource:, query_service: Hyrax.query_service) ⇒ EmbargoManager

Returns a new instance of EmbargoManager.

Parameters:



92
93
94
95
# File 'app/services/hyrax/embargo_manager.rb', line 92

def initialize(resource:, query_service: Hyrax.query_service)
  @query_service = query_service
  self.resource  = resource
end

Instance Attribute Details

#query_serviceObject (readonly)

Returns the value of attribute query_service.



88
89
90
# File 'app/services/hyrax/embargo_manager.rb', line 88

def query_service
  @query_service
end

#resourceHyrax::Resource

Returns:



83
84
85
# File 'app/services/hyrax/embargo_manager.rb', line 83

def resource
  @resource
end

Class Method Details

.apply_embargo_for(resource:, query_service: Hyrax.query_service) ⇒ Object



98
99
100
101
# File 'app/services/hyrax/embargo_manager.rb', line 98

def apply_embargo_for(resource:, query_service: Hyrax.query_service)
  new(resource: resource, query_service: query_service)
    .apply
end

.apply_embargo_for!(resource:, query_service: Hyrax.query_service) ⇒ Object



103
104
105
106
# File 'app/services/hyrax/embargo_manager.rb', line 103

def apply_embargo_for!(resource:, query_service: Hyrax.query_service)
  new(resource: resource, query_service: query_service)
    .apply!
end

.create_or_update_embargo_on_members(members, work) ⇒ Object

Creates or updates an existing embargo on a member to match the embargo on the parent work rubocop:disable Metrics/AbcSize, Metrics/MethodLength

Parameters:

  • members (Array<Valkyrie::Resource>)
  • work (Hyrax::Work)


141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'app/services/hyrax/embargo_manager.rb', line 141

def create_or_update_embargo_on_members(members, work)
  # TODO: account for all members and levels, not just file sets. ref: #6131

  members.each do |member|
    member_embargo_needs_updating = work.embargo.updated_at > member.embargo&.updated_at if member.embargo

    if member.embargo && member_embargo_needs_updating
      member.embargo.embargo_release_date = work.embargo['embargo_release_date']
      member.embargo.visibility_during_embargo = work.embargo['visibility_during_embargo']
      member.embargo.visibility_after_embargo = work.embargo['visibility_after_embargo']
      member.embargo = Hyrax.persister.save(resource: member.embargo)
    else
      work_embargo_manager = Hyrax::EmbargoManager.new(resource: work)
      work_embargo_manager.copy_embargo_to(target: member)
      member = Hyrax.persister.save(resource: member)
    end

    user ||= ::User.find_by_user_key(member.depositor)
    # the line below works in that it indexes the file set with the necessary lease properties
    # I do not know however if this is the best event_id to pass
    Hyrax.publisher.publish('object.metadata.updated', object: member, user: user)
  end
end

.deactivate_embargo_for(resource:, query_service: Hyrax.query_service) ⇒ Boolean

Returns:

  • (Boolean)


114
115
116
117
# File 'app/services/hyrax/embargo_manager.rb', line 114

def deactivate_embargo_for(resource:, query_service: Hyrax.query_service)
  new(resource: resource, query_service: query_service)
    .deactivate
end

.deactivate_embargo_for!(resource:, query_service: Hyrax.query_service) ⇒ Boolean

Returns:

  • (Boolean)


120
121
122
123
# File 'app/services/hyrax/embargo_manager.rb', line 120

def deactivate_embargo_for!(resource:, query_service: Hyrax.query_service)
  new(resource: resource, query_service: query_service)
    .deactivate!
end

.embargo_for(resource:, query_service: Hyrax.query_service) ⇒ Object



108
109
110
111
# File 'app/services/hyrax/embargo_manager.rb', line 108

def embargo_for(resource:, query_service: Hyrax.query_service)
  new(resource: resource, query_service: query_service)
    .embargo
end

.release_embargo_for(resource:, query_service: Hyrax.query_service) ⇒ Boolean

Returns:

  • (Boolean)


126
127
128
129
# File 'app/services/hyrax/embargo_manager.rb', line 126

def release_embargo_for(resource:, query_service: Hyrax.query_service)
  new(resource: resource, query_service: query_service)
    .release
end

.release_embargo_for!(resource:, query_service: Hyrax.query_service) ⇒ Boolean

Returns:

  • (Boolean)


132
133
134
135
# File 'app/services/hyrax/embargo_manager.rb', line 132

def release_embargo_for!(resource:, query_service: Hyrax.query_service)
  new(resource: resource, query_service: query_service)
    .release!
end

Instance Method Details

#applyBoolean

Sets the visibility of the resource to the embargo’s visibility condition

Returns:

  • (Boolean)


200
201
202
203
204
# File 'app/services/hyrax/embargo_manager.rb', line 200

def apply
  return false unless under_embargo?

  resource.visibility = embargo.visibility_during_embargo
end

#apply!void

This method returns an undefined value.

Raises:



209
210
211
# File 'app/services/hyrax/embargo_manager.rb', line 209

def apply!
  apply || raise(NotEnforcableError)
end

#copy_embargo_to(target:) ⇒ Boolean

Copies and applies the embargo to a new (target) resource.

Parameters:

Returns:

  • (Boolean)


189
190
191
192
193
194
# File 'app/services/hyrax/embargo_manager.rb', line 189

def copy_embargo_to(target:)
  return false unless under_embargo?

  target.embargo = Hyrax.persister.save(resource: Embargo.new(clone_attributes))
  self.class.apply_embargo_for(resource: target)
end

#deactivateBoolean

Deactivates the embargo

Returns:

  • (Boolean)


170
171
172
173
# File 'app/services/hyrax/embargo_manager.rb', line 170

def deactivate
  release(force: true) &&
    nullify(force: true)
end

#deactivate!Boolean

Deactivates the embargo

Returns:

  • (Boolean)


178
179
180
181
# File 'app/services/hyrax/embargo_manager.rb', line 178

def deactivate!
  release(force: true)
  nullify(force: true)
end

#embargoHyrax::Embargo

Returns:



222
223
224
# File 'app/services/hyrax/embargo_manager.rb', line 222

def embargo
  resource.embargo || Embargo.new
end

#enforced?Boolean

Returns:

  • (Boolean)


215
216
217
218
# File 'app/services/hyrax/embargo_manager.rb', line 215

def enforced?
  embargo.embargo_release_date.present? &&
    (embargo.visibility_during_embargo.to_s == resource.visibility)
end

#nullify(force: false) ⇒ Boolean

Drop the embargo by setting its release date and visibility settings to ‘nil`.

Parameters:

  • force (Boolean) (defaults to: false)

    force the nullify even when the embargo period is current

Returns:

  • (Boolean)


232
233
234
235
236
237
238
239
# File 'app/services/hyrax/embargo_manager.rb', line 232

def nullify(force: false)
  return false if !force && under_embargo?

  embargo.embargo_release_date = nil
  embargo.visibility_during_embargo = nil
  embargo.visibility_after_embargo = nil
  true
end

#release(force: false) ⇒ Boolean

Sets the visibility of the resource to the embargo’s after embargo visibility. no-op if the embargo period is current and the force flag is false.

Parameters:

  • force (boolean) (defaults to: false)

    force the release even when the embargo period is current

Returns:

  • (Boolean)

    truthy if the embargo has been applied



248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'app/services/hyrax/embargo_manager.rb', line 248

def release(force: false)
  return false if !force && under_embargo?

  embargo_state = embargo.active? ? 'active' : 'expired'
  history_record = embargo_history_message(
    embargo_state,
    Hyrax::TimeService.time_in_utc,
    embargo.embargo_release_date,
    embargo.visibility_during_embargo,
    embargo.visibility_after_embargo
  )
  embargo.embargo_history += [history_record]

  return true if embargo.visibility_after_embargo.nil?

  resource.visibility = embargo.visibility_after_embargo
end

#release!void

This method returns an undefined value.

Raises:



270
271
272
# File 'app/services/hyrax/embargo_manager.rb', line 270

def release!
  release || raise(NotReleasableError)
end

#under_embargo?Boolean

Returns indicates whether the date range for the embargo’s applicability includes the present date.

Returns:

  • (Boolean)

    indicates whether the date range for the embargo’s applicability includes the present date.



277
278
279
# File 'app/services/hyrax/embargo_manager.rb', line 277

def under_embargo?
  embargo.active?
end