Class: Hyrax::CollectionType

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

Constant Summary collapse

USER_COLLECTION_MACHINE_ID =
'user_collection'.freeze
USER_COLLECTION_DEFAULT_TITLE =
I18n.t('hyrax.collection_type.default_title').freeze
ADMIN_SET_MACHINE_ID =
'admin_set'.freeze
ADMIN_SET_DEFAULT_TITLE =
I18n.t('hyrax.collection_type.admin_set_title').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



83
84
85
# File 'app/models/hyrax/collection_type.rb', line 83

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



40
41
42
43
44
# File 'app/models/hyrax/collection_type.rb', line 40

def self.find_by_gid(gid)
  find(GlobalID.new(gid).model_id)
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



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

def self.find_by_gid!(gid)
  result = find_by_gid(gid)
  raise ActiveRecord::RecordNotFound, "Couldn't find Hyrax::CollectionType matching GID '#{gid}'" unless result
  result
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:



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

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:



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

def self.find_or_create_default_collection_type
  find_by(machine_id: USER_COLLECTION_MACHINE_ID) || Hyrax::CollectionTypes::CreateService.create_user_collection_type
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



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

def admin_set?
  machine_id == ADMIN_SET_MACHINE_ID
end

#collectionsObject



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

def collections
  return [] unless gid
  ActiveFedora::Base.where(collection_type_gid_ssim: gid.to_s)
end

#collections?Boolean

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



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

def collections?
  collections.count.positive?
end

#gidString

Return the Global Identifier for this collection type.

Returns:

  • (String)

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



58
59
60
# File 'app/models/hyrax/collection_type.rb', line 58

def gid
  URI::GID.build(app: GlobalID.app, model_name: model_name.name.parameterize.to_sym, model_id: id).to_s if id
end

#title=(value) ⇒ Object



18
19
20
21
# File 'app/models/hyrax/collection_type.rb', line 18

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



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

def user_collection?
  machine_id == USER_COLLECTION_MACHINE_ID
end