Skip to content

Commit 4dcfbb4

Browse files
Copilotfbacall
andcommitted
Refactor Author to generic Person model with polymorphic PersonLink
Co-authored-by: fbacall <503373+fbacall@users.noreply.github.com>
1 parent 4f29add commit 4dcfbb4

26 files changed

Lines changed: 207 additions & 194 deletions

app/assets/javascripts/authors.js

Lines changed: 0 additions & 41 deletions
This file was deleted.

app/assets/javascripts/people.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
var People = {
2+
add: function (role) {
3+
var templateId = '#person-' + role + '-template';
4+
var listId = '#person-' + role + '-list';
5+
var newForm = $(templateId).clone().html();
6+
7+
// Ensure the index of the new form is 1 greater than the current highest index, to prevent collisions
8+
var index = 0;
9+
$(listId + ' .person-form').each(function () {
10+
var newIndex = parseInt($(this).data('index'));
11+
if (newIndex > index) {
12+
index = newIndex;
13+
}
14+
});
15+
16+
// Replace the placeholder index with the actual index
17+
newForm = $(newForm.replace(/replace-me/g, index + 1));
18+
newForm.appendTo(listId);
19+
20+
return false; // Stop form being submitted
21+
},
22+
23+
// This is just cosmetic. The actual removal is done by rails,
24+
// by virtue of the hidden checkbox being checked when the label is clicked.
25+
delete: function () {
26+
$(this).parents('.person-form').fadeOut();
27+
}
28+
};
29+
30+
document.addEventListener("turbolinks:load", function() {
31+
32+
$('[id^="person-"]')
33+
.on('click', '[id^="add-person-"]', function() {
34+
var role = $(this).data('role');
35+
People.add(role);
36+
return false;
37+
})
38+
.on('change', '.delete-person-btn input.destroy-attribute', People.delete);
39+
});

app/controllers/materials_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ def material_params
175175
{ scientific_topic_names: [] }, { scientific_topic_uris: [] },
176176
{ operation_names: [] }, { operation_uris: [] },
177177
{ node_ids: [] }, { node_names: [] }, { fields: [] },
178-
authors_attributes: %i[id first_name last_name orcid _destroy],
178+
person_links_attributes: [:id, :role, :_destroy, person_attributes: %i[id first_name last_name orcid]],
179179
external_resources_attributes: %i[id url title _destroy],
180180
external_resources: %i[url title],
181181
event_ids: [], locked_fields: [])

app/models/author.rb

Lines changed: 0 additions & 10 deletions
This file was deleted.

app/models/material.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,10 @@ class Material < ApplicationRecord
106106

107107
has_many :stars, as: :resource, dependent: :destroy
108108

109-
has_many :material_authors, dependent: :destroy
110-
has_many :authors, through: :material_authors
111-
accepts_nested_attributes_for :authors, allow_destroy: true, reject_if: :all_blank
109+
has_many :person_links, as: :resource, dependent: :destroy
110+
has_many :people, through: :person_links
111+
has_many :authors, -> { where(person_links: { role: 'author' }) }, through: :person_links, source: :person
112+
accepts_nested_attributes_for :person_links, allow_destroy: true, reject_if: :all_blank
112113

113114
# Remove trailing and squeezes (:squish option) white spaces inside the string (before_validation):
114115
# e.g. "James Bond " => "James Bond"

app/models/material_author.rb

Lines changed: 0 additions & 6 deletions
This file was deleted.

app/models/person.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Person < ApplicationRecord
2+
has_many :person_links, dependent: :destroy
3+
4+
validates :first_name, :last_name, presence: true
5+
6+
def full_name
7+
"#{first_name} #{last_name}".strip
8+
end
9+
end

app/models/person_link.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class PersonLink < ApplicationRecord
2+
belongs_to :resource, polymorphic: true
3+
belongs_to :person
4+
accepts_nested_attributes_for :person, reject_if: :all_blank
5+
6+
validates :resource, :person, :role, presence: true
7+
end

app/serializers/material_serializer.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@ class MaterialSerializer < ApplicationSerializer
1818
has_many :nodes
1919
has_many :collections
2020
has_many :events
21-
has_many :authors
21+
has_many :authors, serializer: PersonSerializer
2222
end
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
class AuthorSerializer < ApplicationSerializer
1+
class PersonSerializer < ApplicationSerializer
22
attributes :id, :first_name, :last_name, :orcid, :full_name
33
end

0 commit comments

Comments
 (0)