Skip to content

Commit 35dd36c

Browse files
committed
Coauthors actions implemented
1 parent e6a3c6d commit 35dd36c

5 files changed

Lines changed: 59 additions & 5 deletions

File tree

app/controllers/registers_controller.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,32 @@ def coauthors
268268

269269
# POST /registers/r:abc/coauthors
270270
def coauthors_commit
271+
@register.coauthor = params.require('register').require('coauthor')
272+
@coauthor = User.find_by_email_or_username(@register.coauthor)
273+
if @coauthor.present?
274+
rc_par = { register: @register, user: @coauthor }
275+
success =
276+
case params['action']
277+
when 'unlink'
278+
RegisterCoauthor.find_by(rc_par).destroy
279+
when 'up'
280+
RegisterCoauthor.find_by(rc_par).move_up
281+
else
282+
rc_par.merge!(order: @register.register_coauthors.size + 1)
283+
RegisterCoauthor.create(rc_par)
284+
end
285+
286+
if success
287+
flash[:notice] = 'Successfully updated coauthors'
288+
redirect_back(fallback_location: @register)
289+
else
290+
flash.now[:alert] = 'Error updating coauthors'
291+
render :coauthors
292+
end
293+
else
294+
@register.add_error(:coauthor, 'does not exist in the system')
295+
render :coauthors
296+
end
271297
end
272298

273299
# GET /registers/r:abc/editorial_checks

app/models/register.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class Register < ApplicationRecord
3030
:type_strains, through: :names, source: :nomenclatural_type,
3131
source_type: 'Strain'
3232
)
33-
has_many(:register_coauthors)
33+
has_many(:register_coauthors, -> { order(:order) }, dependent: :destroy)
3434
has_many(:coauthors, through: :register_coauthors, source: :user)
3535
alias :correspondences :register_correspondences
3636
alias :created_by :user
@@ -56,7 +56,7 @@ class Register < ApplicationRecord
5656
include Register::SampleSet
5757

5858
attr_accessor :modal_form_id
59-
attr_accessor :new_coauthor
59+
attr_accessor :coauthor
6060

6161
def to_param
6262
accession

app/models/register_coauthor.rb

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,27 @@
11
class RegisterCoauthor < ApplicationRecord
22
belongs_to(:register)
33
belongs_to(:user)
4-
validates(:order, presence: true)
4+
validates(
5+
:order, presence: true,
6+
numericality: { only_integer: true, greater_than: 0 }
7+
)
8+
validates(:user, uniqueness: { scope: :register })
9+
10+
after_destroy(:update_coauthors_order)
11+
12+
def move_up
13+
return if order <= 1
14+
15+
register.register_coauthor.where(order: order - 1)
16+
.update(order: order) && update(order: order - 1)
17+
end
18+
19+
private
20+
21+
def update_coauthors_order
22+
register.reload
23+
register.register_coauthors.each_with_index do |rc, k|
24+
rc.update(order: k)
25+
end
26+
end
527
end

app/models/user.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ def self.curator_applications
7777
where('curator_statement is not null').where(curator: false)
7878
end
7979

80+
def self.find_by_email_or_username(query)
81+
where('LOWER(email) = ?', query.downcase).or(where(username: query)).first
82+
end
83+
8084
def roles
8185
o = ['User']
8286
o << 'Contributor' if contributor?

app/views/registers/coauthors.html.erb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
<% author = rc.user %>
2323
<%= display_link(author) %>
2424
<%= link_to(
25-
coauthors_register_url(@register, unlink: author.id),
25+
coauthors_register_url(
26+
@register, coauthor: author.username, action: :unlink),
2627
class: 'badge badge-pill badge-primary',
2728
method: :post, title: 'Unlink coauthor',
2829
data: { confirm: 'Are you sure?' }
@@ -31,7 +32,8 @@
3132
<% end %>
3233
<% unless rc.order == 1 %>
3334
<%= link_to(
34-
coauthors_register_url(@register, up: author.id),
35+
coauthors_register_url(
36+
@register, coauthor: author.username, action: :up),
3537
class: 'badge badge-pill badge-primary',
3638
method: :post, title: 'Move coauthor up'
3739
) do %>

0 commit comments

Comments
 (0)