Class: Hyrax::PermissionTemplate

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/hyrax/permission_template.rb

Overview

Defines behavior that is applied to objects added as members of an AdminSet

* access rights to stamp on each object
* calculate embargo/lease release dates

There is an interplay between an AdminSet and a PermissionTemplate.

See Also:

  • for further discussion

Constant Summary collapse

RELEASE_TEXT_VALUE_FIXED =

Valid Release Period values

'fixed'
RELEASE_TEXT_VALUE_NO_DELAY =
'now'
RELEASE_TEXT_VALUE_BEFORE_DATE =

Valid Release Varies sub-options

'before'
RELEASE_TEXT_VALUE_EMBARGO =
'embargo'
RELEASE_TEXT_VALUE_6_MONTHS =
'6mos'
RELEASE_TEXT_VALUE_1_YEAR =
'1yr'
RELEASE_TEXT_VALUE_2_YEARS =
'2yrs'
RELEASE_TEXT_VALUE_3_YEARS =
'3yrs'
RELEASE_EMBARGO_PERIODS =

Key/value pair of valid embargo periods. Values are number of months embargoed.

{
  RELEASE_TEXT_VALUE_6_MONTHS => 6,
  RELEASE_TEXT_VALUE_1_YEAR => 12,
  RELEASE_TEXT_VALUE_2_YEARS => 24,
  RELEASE_TEXT_VALUE_3_YEARS => 36
}.freeze

Instance Method Summary collapse

Instance Method Details

#admin_setAdminSet

Deprecated.

Use #source_model instead

A bit of an analogue for a `belongs_to :admin_set` as it crosses from Fedora to the DB

Returns:

Raises:


59
60
61
62
63
64
65
# File 'app/models/hyrax/permission_template.rb', line 59

def admin_set
  Deprecation.warn('Use #source_model instead')
  return AdminSet.find(source_id) if AdminSet.exists?(source_id)
  raise Hyrax::ObjectNotFoundError
rescue ActiveFedora::ActiveFedoraError # TODO: remove the rescue when active_fedora issue #1276 is fixed
  raise Hyrax::ObjectNotFoundError
end

#agent_ids_for(agent_type:, access:) ⇒ Array<String>

Retrieve the agent_ids associated with the given agent_type and access

Parameters:

  • agent_type (String)
  • access (String)

Returns:

  • (Array<String>)

    of agent_ids that match the given parameters


27
28
29
# File 'app/models/hyrax/permission_template.rb', line 27

def agent_ids_for(agent_type:, access:)
  access_grants.where(agent_type: agent_type, access: access).pluck(:agent_id)
end

#collectionCollection

Deprecated.

Use #source_model instead

A bit of an analogue for a `belongs_to :collection` as it crosses from Fedora to the DB

Returns:

Raises:


71
72
73
74
75
76
77
# File 'app/models/hyrax/permission_template.rb', line 71

def collection
  Deprecation.warn('Use #source_model instead')
  return ::Collection.find(source_id) if ::Collection.exists?(source_id)
  raise Hyrax::ObjectNotFoundError
rescue ActiveFedora::ActiveFedoraError # TODO: remove the rescue when active_fedora issue #1276 is fixed
  raise Hyrax::ObjectNotFoundError
end

#release_before_date?Boolean

Does this permission template require a date (or embargo) that all works are released before NOTE: date will be in release_date

Returns:

  • (Boolean)

112
113
114
115
# File 'app/models/hyrax/permission_template.rb', line 112

def release_before_date?
  # All PermissionTemplate embargoes are dynamically determined release before dates
  release_period == RELEASE_TEXT_VALUE_BEFORE_DATE || release_max_embargo?
end

#release_dateObject

Override release_date getter to return a dynamically calculated date of release based one release requirements. Returns embargo date when release_max_embargo?==true. Returns today's date when release_no_delay?==true.

See Also:


128
129
130
131
132
133
134
135
136
137
# File 'app/models/hyrax/permission_template.rb', line 128

def release_date
  # If no release delays allowed, return today's date as release date
  return Time.zone.today if release_no_delay?

  # If this isn't an embargo, just return release_date from database
  return self[:release_date] unless release_max_embargo?

  # Otherwise (if an embargo), return latest embargo date by adding specified months to today's date
  Time.zone.today + RELEASE_EMBARGO_PERIODS.fetch(release_period).months
end

#release_fixed_date?Boolean

Does this permission template require a specific date of release for all works NOTE: date will be in release_date

Returns:

  • (Boolean)

101
102
103
# File 'app/models/hyrax/permission_template.rb', line 101

def release_fixed_date?
  release_period == RELEASE_TEXT_VALUE_FIXED
end

#release_max_embargo?Boolean

Is there a maximum embargo period specified by this permission template NOTE: latest embargo date returned by release_date, maximum embargo period will be in release_period

Returns:

  • (Boolean)

119
120
121
122
# File 'app/models/hyrax/permission_template.rb', line 119

def release_max_embargo?
  # Is it a release period in one of our valid embargo periods?
  RELEASE_EMBARGO_PERIODS.key?(release_period)
end

#release_no_delay?Boolean

Does this permission template require no release delays (i.e. no embargoes allowed)

Returns:

  • (Boolean)

106
107
108
# File 'app/models/hyrax/permission_template.rb', line 106

def release_no_delay?
  release_period == RELEASE_TEXT_VALUE_NO_DELAY
end

#sourceHyrax::Resource

Note:

this is a convenience method for Hyrax.query_service.find_by(id: template.source_id)

Returns the collection this template is associated with.

Returns:


41
42
43
# File 'app/models/hyrax/permission_template.rb', line 41

def source
  Hyrax.query_service.find_by(id: source_id)
end

#source_modelAdminSet, ::Collection

A bit of an analogue for a `belongs_to :source_model` as it crosses from Fedora to the DB

Returns:

Raises:


49
50
51
52
53
# File 'app/models/hyrax/permission_template.rb', line 49

def source_model
  ActiveFedora::Base.find(source_id)
rescue ActiveFedora::ObjectNotFoundError
  raise Hyrax::ObjectNotFoundError
end

#valid_release_date?(date) ⇒ Boolean

Determines whether a given release date is valid based on this template's requirements

Parameters:

  • date (Date)

    to validate

Returns:

  • (Boolean)

141
142
143
144
# File 'app/models/hyrax/permission_template.rb', line 141

def valid_release_date?(date)
  # Validate date against all release date requirements
  check_no_delay_requirements(date) && check_before_date_requirements(date) && check_fixed_date_requirements(date)
end

#valid_visibility?(value) ⇒ Boolean

Determines whether a given visibility setting is valid based on this template's requirements

Parameters:

  • value (String)
    • visibility value to validate

Returns:

  • (Boolean)

148
149
150
151
152
153
154
# File 'app/models/hyrax/permission_template.rb', line 148

def valid_visibility?(value)
  # If template doesn't specify a visiblity (i.e. is "varies"), then any visibility is valid
  return true if visibility.blank?

  # Validate that passed in value matches visibility requirement exactly
  visibility == value
end