Class: ArkName

Inherits:
Sequel::Model
  • Object
show all
Includes:
ASModel
Defined in:
backend/app/model/ark_name.rb

Constant Summary

Constants included from JSONModel

JSONModel::REFERENCE_KEY_REGEX

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ASModel

all_models, included, update_publish_flag, update_suppressed_flag

Methods included from JSONModel

JSONModel, #JSONModel, add_error_handler, all, allow_unmapped_enum_value, backend_url, check_valid_refs, client_mode?, custom_validations, destroy_model, enum_default_value, enum_values, handle_error, init, load_schema, #models, models, parse_jsonmodel_ref, parse_reference, repository, repository_for, schema_src, set_publish_flags!, set_repository, strict_mode, strict_mode?, validate_schema, with_repository

Class Method Details

.create_from_archival_object(archival_object) ⇒ Object



42
43
44
45
46
47
48
49
50
51
# File 'backend/app/model/ark_name.rb', line 42

def self.create_from_archival_object(archival_object)
  self.insert(:archival_object_id => archival_object.id,
              :resource_id        => nil,
              :created_by         => 'admin',
              :last_modified_by   => 'admin',
              :create_time        => Time.now,
              :system_mtime       => Time.now,
              :user_mtime         => Time.now,
              :lock_version       => 0)
end

.create_from_resource(resource) ⇒ Object



31
32
33
34
35
36
37
38
39
40
# File 'backend/app/model/ark_name.rb', line 31

def self.create_from_resource(resource)
  self.insert(:archival_object_id => nil,
              :resource_id        => resource.id,
              :created_by         => 'admin',
              :last_modified_by   => 'admin',
              :create_time        => Time.now,
              :system_mtime       => Time.now,
              :user_mtime         => Time.now,
              :lock_version       => 0)
end

.get_ark_url(id, type) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'backend/app/model/ark_name.rb', line 53

def self.get_ark_url(id, type)
  case type
  when :resource
    id_field = :resource_id
    klass_sym = :resource
  when :archival_object
    id_field = :archival_object_id
    klass_sym = :archival_object
  else
    return nil
  end

  external_url = get_external_ark_url(id, klass_sym)

  if !external_url.nil?
    return external_url
  else
    ark = ArkName.first(id_field => id)

    if !ark.nil?
      return "#{AppConfig[:ark_url_prefix]}/ark:/#{AppConfig[:ark_naan]}/#{ark.id}"
    else
      return nil
    end
  end
end

Instance Method Details

#validateObject

validations: must be linked to a resource or archival object cannot link to more than one type of resource can’t have more than one ark_id point to the same resource or archival object



14
15
16
17
18
19
# File 'backend/app/model/ark_name.rb', line 14

def validate
  validate_resources_defined
  validates_unique(:resource_id, :message => "ARK must point to a unique Resource")
  validates_unique(:archival_object_id, :message => "ARK must point to a unique Archival Object")
  super
end

#validate_resources_definedObject



21
22
23
24
25
26
27
28
29
# File 'backend/app/model/ark_name.rb', line 21

def validate_resources_defined
  resources_defined = 0
  resources_defined += 1 unless self.resource_id.nil?
  resources_defined += 1 unless self.archival_object_id.nil?

  unless resources_defined == 1
    errors.add(:base, 'Exactly one of [resource_id, archival_object_id] must be defined.')
  end
end