Skip to content

Commit 93e0bb6

Browse files
author
Tim Standen
committed
[F] Add external identifier model
1 parent fb753bc commit 93e0bb6

File tree

8 files changed

+163
-81
lines changed

8 files changed

+163
-81
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# frozen_string_literal: true
2+
3+
module ExternallyIdentifiable
4+
extend ActiveSupport::Concern
5+
6+
included do
7+
has_one :external_identifier_record, class_name: "ExternalIdentifier", as: :identifiable, dependent: :destroy
8+
end
9+
10+
def external_identifier
11+
external_identifier_record&.identifier || nil
12+
end
13+
14+
def external_identifier=(new_id)
15+
unless external_identifier
16+
build_external_identifier_record(identifier: new_id)
17+
else
18+
external_identifier_record.identifier = new_id
19+
end
20+
external_identifier_record.save
21+
end
22+
end
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# frozen_string_literal: true
2+
3+
class ExternalIdentifier < ApplicationRecord
4+
belongs_to :identifiable, polymorphic: true, optional: false
5+
6+
validates :identifier, presence: true, uniqueness: true
7+
end

api/app/models/journal.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class Journal < ApplicationRecord
2525
include WithProjectCollectionLayout
2626
include WithConfigurableAvatar
2727
include HasKeywordSearch
28+
include ExternallyIdentifiable
2829

2930
multisearch_draftable true
3031

api/app/models/project.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class Project < ApplicationRecord
2828
include TimestampScopes
2929
include WithConfigurableAvatar
3030
include HasKeywordSearch
31+
include ExternallyIdentifiable
3132

3233
has_formatted_attributes :description, :subtitle, :image_credits
3334
has_formatted_attributes :restricted_access_body, include_wrap: false

api/app/models/project_collection.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class ProjectCollection < ApplicationRecord
1212
include TrackedCreator
1313
include Taggable
1414
include WithProjectCollectionLayout
15+
include ExternallyIdentifiable
1516

1617
# Attachments
1718
manifold_has_attached_file :hero, :image

api/app/models/user_group.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
class UserGroup < ApplicationRecord
22
include Authority::Abilities
33
include Filterable
4+
include ExternallyIdentifiable
45

56
has_many :memberships, class_name: "UserGroupMembership", inverse_of: :user_group
67
has_many :entitleables, class_name: "UserGroupEntitleable", inverse_of: :user_group
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class AddExternalIdentifiersTable < ActiveRecord::Migration[7.0]
2+
def change
3+
create_table :external_identifiers, id: :uuid do |t|
4+
t.string :identifier, null: false
5+
t.references :identifiable, polymorphic: true
6+
t.timestamps
7+
end
8+
end
9+
end

0 commit comments

Comments
 (0)