Class: Hyrax::CollectionType

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

Overview

rubocop:disable Metrics/ClassLength

Constant Summary collapse

SETTINGS_ATTRIBUTES =

these need to updated in tandem with database migrations, use `.settings_attributes`

[:nestable?, :discoverable?, :brandable?, :sharable?, :share_applies_to_new_works?, :allow_multiple_membership?,
:require_membership?, :assigns_workflow?, :assigns_visibility?].freeze
USER_COLLECTION_MACHINE_ID =
'user_collection'
USER_COLLECTION_DEFAULT_TITLE =
I18n.t('hyrax.collection_type.default_title', default: 'User Collection').freeze
ADMIN_SET_MACHINE_ID =
'admin_set'
ADMIN_SET_DEFAULT_TITLE =
I18n.t('hyrax.collection_type.admin_set_title', default: 'Admin Set').freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.any_nestable?Boolean

Returns True if there is at least one collection type that has nestable? true.

Returns:

  • (Boolean)

    True if there is at least one collection type that has nestable? true


137
138
139
# File 'app/models/hyrax/collection_type.rb', line 137

def self.any_nestable?
  where(nestable: true).any?
end

.find_by_gid(gid) ⇒ Hyrax::CollectionType, False

Find the collection type associated with the Global Identifier (gid)

Parameters:

  • gid (String)
    • Global Identifier for this collection_type (e.g. gid://internal/hyrax-collectiontype/3)

Returns:

  • (Hyrax::CollectionType)

    if record matching gid is found, an instance of Hyrax::CollectionType with id = the model_id portion of the gid (e.g. 3)

  • (False)

    if record matching gid is not found


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

def self.find_by_gid(gid)
  find_by_gid!(gid)
rescue ActiveRecord::RecordNotFound, URI::InvalidURIError
  false
end

.find_by_gid!(gid) ⇒ Hyrax::CollectionType

Find the collection type associated with the Global Identifier (gid)

Parameters:

  • gid (String)
    • Global Identifier for this collection_type (e.g. gid://internal/hyrax-collectiontype/3)

Returns:

  • (Hyrax::CollectionType)

    an instance of Hyrax::CollectionType with id = the model_id portion of the gid (e.g. 3)

Raises:

  • (ActiveRecord::RecordNotFound)

    if record matching gid is not found


77
78
79
80
81
82
83
84
85
86
87
88
# File 'app/models/hyrax/collection_type.rb', line 77

def self.find_by_gid!(gid)
  raise(URI::InvalidURIError) if gid.nil?
  GlobalID::Locator.locate(gid)
rescue NameError
  Rails.logger.warn "#{self.class}##{__method__} called with #{gid}, which is " \
                    "a legacy collection type global id format. If this " \
                    "collection type gid is attached to a collection in " \
                    "your repository you'll want to run " \
                    "`hyrax:collections:update_collection_type_global_ids` to " \
                    "update references. see: https://github.com/samvera/hyrax/issues/4696"
  find(GlobalID.new(gid).model_id)
end

.find_or_create_admin_set_typeHyrax::CollectionType

Find or create the Admin Set collection type as defined by:

  • ADMIN_SET_MACHINE_ID

  • ADMIN_SET_DEFAULT_TITLE

  • Hyrax::CollectionTypes::CreateService::ADMIN_SET_OPTIONS

Returns:

See Also:


163
164
165
# File 'app/models/hyrax/collection_type.rb', line 163

def self.find_or_create_admin_set_type
  find_by(machine_id: ADMIN_SET_MACHINE_ID) || Hyrax::CollectionTypes::CreateService.create_admin_set_type
end

.find_or_create_default_collection_typeHyrax::CollectionType

Find or create the default type (i.e., user collection) as defined by:

  • USER_COLLECTION_MACHINE_ID

  • USER_COLLECTION_DEFAULT_TITLE

  • Hyrax::CollectionTypes::CreateService::USER_COLLECTION_OPTIONS

Returns:

See Also:


150
151
152
# File 'app/models/hyrax/collection_type.rb', line 150

def self.find_or_create_default_collection_type
  find_by(machine_id: USER_COLLECTION_MACHINE_ID) || Hyrax::CollectionTypes::CreateService.create_user_collection_type
end

.gids_that_do_not_allow_multiple_membershipArray<String>

Return an array of global identifiers for collection types that do not allow multiple membership.

Returns:

  • (Array<String>)

    an array of Global Identifiers

See Also:


69
70
71
# File 'app/models/hyrax/collection_type.rb', line 69

def self.gids_that_do_not_allow_multiple_membership
  where(allow_multiple_membership: false).map(&:gid)
end

.settings_attributesArray<Symbol>

Note:

this replaces the deprecated .collection_type_settings_methods

Returns the predicates for collection type settings flags (e.g. #nestable?).

Returns:

  • (Array<Symbol>)

    the predicates for collection type settings flags (e.g. #nestable?)


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

def self.settings_attributes
  SETTINGS_ATTRIBUTES
end

Instance Method Details

#admin_set?Boolean

Returns True if this is the Admin Set type.

Returns:

  • (Boolean)

    True if this is the Admin Set type


127
128
129
# File 'app/models/hyrax/collection_type.rb', line 127

def admin_set?
  machine_id == ADMIN_SET_MACHINE_ID
end

#collections(use_valkyrie: false) ⇒ Enumerable<Collection, PcdmCollection>

Returns:


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

def collections(use_valkyrie: false)
  return [] unless id
  return Hyrax.custom_queries.find_collections_by_type(global_id: to_global_id) if use_valkyrie
  ActiveFedora::Base.where(Hyrax.config.collection_type_index_field.to_sym => to_global_id.to_s)
end

#collections?Boolean

Deprecated.

Use #collections.any? instead

Returns True if there is at least one collection of this type.

Returns:

  • (Boolean)

    True if there is at least one collection of this type


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

def collections?
  Deprecation.warn('Use #collections.any? instead.') && collections.any?
end

#gidString?

Return the Global Identifier for this collection type.

Returns:

  • (String, nil)

    Global Identifier (gid) for this collection_type (e.g. gid://internal/hyrax-collectiontype/3)

See Also:


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

def gid
  Deprecation.warn('use #to_global_id.')
  to_global_id.to_s if id
end

#title=(value) ⇒ void

Note:

mints a #machine_id (?!)

This method returns an undefined value.


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

def title=(value)
  super
  assign_machine_id
end

#user_collection?Boolean

Returns True if this is the User Collection type.

Returns:

  • (Boolean)

    True if this is the User Collection type


132
133
134
# File 'app/models/hyrax/collection_type.rb', line 132

def user_collection?
  machine_id == USER_COLLECTION_MACHINE_ID
end