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



43
44
45
46
47
48
# File 'app/models/hyrax/permission_template.rb', line 43

def admin_set
  return AdminSet.find(source_id) if AdminSet.exists?(source_id)
  raise ActiveFedora::ObjectNotFoundError
rescue ActiveFedora::ActiveFedoraError # TODO: remove the rescue when active_fedora issue #1276 is fixed
  raise ActiveFedora::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



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

#collectionCollection

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

Returns:

Raises:

  • (ActiveFedora::ObjectNotFoundError)

    when the we cannot find the Collection



53
54
55
56
57
58
# File 'app/models/hyrax/permission_template.rb', line 53

def collection
  return Collection.find(source_id) if Collection.exists?(source_id)
  raise ActiveFedora::ObjectNotFoundError
rescue ActiveFedora::ActiveFedoraError # TODO: remove the rescue when active_fedora issue #1276 is fixed
  raise ActiveFedora::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)


93
94
95
96
# File 'app/models/hyrax/permission_template.rb', line 93

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:



109
110
111
112
113
114
115
116
117
118
# File 'app/models/hyrax/permission_template.rb', line 109

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)


82
83
84
# File 'app/models/hyrax/permission_template.rb', line 82

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)


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

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)


87
88
89
# File 'app/models/hyrax/permission_template.rb', line 87

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

  • (ActiveFedora::ObjectNotFoundError)

    when neither an AdminSet or Collection is found



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

def source_model
  admin_set
rescue ActiveFedora::ObjectNotFoundError
  collection
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)


122
123
124
125
# File 'app/models/hyrax/permission_template.rb', line 122

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)


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

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