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



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

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



62
63
64
65
66
# File 'app/models/hyrax/collection_type.rb', line 62

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



80
81
82
83
84
85
86
87
88
89
90
91
# File 'app/models/hyrax/collection_type.rb', line 80

def self.find_by_gid!(gid)
  raise(URI::InvalidURIError) if gid.nil?
  GlobalID::Locator.locate(gid)
rescue NameError
  Hyrax.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:



146
147
148
# File 'app/models/hyrax/collection_type.rb', line 146

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:



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

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

.for(collection:) ⇒ Hyrax::PcdmCollection | ::Collection

Find the collection type associated with the collection

Parameters:

Returns:

Raises:

  • (ActiveRecord::RecordNotFound)

    if record matching gid is not found



51
52
53
54
55
56
# File 'app/models/hyrax/collection_type.rb', line 51

def self.for(collection:)
  gid = collection.collection_type_gid
  gid = gid.first if gid.is_a?(Enumerable)

  find_by_gid!(gid)
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:



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

def self.gids_that_do_not_allow_multiple_membership
  where(allow_multiple_membership: false).map { |c| c.to_global_id.to_s }
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?)



97
98
99
# File 'app/models/hyrax/collection_type.rb', line 97

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



110
111
112
# File 'app/models/hyrax/collection_type.rb', line 110

def admin_set?
  machine_id == ADMIN_SET_MACHINE_ID
end

#collections(use_valkyrie: Hyrax.config.use_valkyrie?) ⇒ Enumerable<Collection, PcdmCollection>

Returns:



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

def collections(use_valkyrie: Hyrax.config.use_valkyrie?)
  return [] unless id
  return Hyrax.custom_queries.find_collections_by_type(global_id: to_global_id.to_s) if use_valkyrie
  ActiveFedora::Base.where(Hyrax.config.collection_type_index_field.to_sym => to_global_id.to_s)
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



115
116
117
# File 'app/models/hyrax/collection_type.rb', line 115

def user_collection?
  machine_id == USER_COLLECTION_MACHINE_ID
end