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'.freeze
RELEASE_TEXT_VALUE_NO_DELAY =
'now'.freeze
RELEASE_TEXT_VALUE_BEFORE_DATE =

Valid Release Varies sub-options

'before'.freeze
RELEASE_TEXT_VALUE_EMBARGO =
'embargo'.freeze
RELEASE_TEXT_VALUE_6_MONTHS =
'6mos'.freeze
RELEASE_TEXT_VALUE_1_YEAR =
'1yr'.freeze
RELEASE_TEXT_VALUE_2_YEARS =
'2yrs'.freeze
RELEASE_TEXT_VALUE_3_YEARS =
'3yrs'.freeze
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

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

Returns:

Raises:

  • (ActiveFedora::ObjectNotFoundError)

    when the we cannot find the AdminSet



34
35
36
# File 'app/models/hyrax/permission_template.rb', line 34

def admin_set
  AdminSet.find(admin_set_id)
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



21
22
23
# File 'app/models/hyrax/permission_template.rb', line 21

def agent_ids_for(agent_type:, access:)
  access_grants.where(agent_type: agent_type, access: access).pluck(:agent_id)
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)


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

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:



87
88
89
90
91
92
93
94
95
96
# File 'app/models/hyrax/permission_template.rb', line 87

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)


60
61
62
# File 'app/models/hyrax/permission_template.rb', line 60

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)


78
79
80
81
# File 'app/models/hyrax/permission_template.rb', line 78

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)


65
66
67
# File 'app/models/hyrax/permission_template.rb', line 65

def release_no_delay?
  release_period == RELEASE_TEXT_VALUE_NO_DELAY
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)


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

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)


107
108
109
110
111
112
113
# File 'app/models/hyrax/permission_template.rb', line 107

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