Skip to content

Commit

Permalink
Merge pull request #146 from seq-code/observers
Browse files Browse the repository at this point in the history
Observers
  • Loading branch information
lmrodriguezr authored Jan 4, 2024
2 parents 11b6b17 + 3d9afce commit 066efcf
Show file tree
Hide file tree
Showing 75 changed files with 2,328 additions and 1,336 deletions.
3 changes: 2 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
# Custom gems:
gem 'serrano', '~> 1.0'
gem 'devise'
gem 'bootstrap', '~> 4.3'
gem 'bootstrap', '~> 4.6'
#gem 'dartsass-sprockets'
gem 'jquery-rails'
gem 'will_paginate'
gem 'will_paginate-bootstrap4'
Expand Down
8 changes: 4 additions & 4 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ GEM
minitest (>= 5.1)
tzinfo (~> 2.0)
zeitwerk (~> 2.3)
addressable (2.8.5)
addressable (2.8.6)
public_suffix (>= 2.0.2, < 6.0)
afm (0.2.2)
autoprefixer-rails (10.4.16.0)
Expand Down Expand Up @@ -155,10 +155,10 @@ GEM
mini_mime (1.1.5)
minitest (5.20.0)
multi_json (1.15.0)
net-ftp (0.3.0)
net-ftp (0.3.3)
net-protocol
time
net-imap (0.4.7)
net-imap (0.4.8)
date
net-protocol
net-pop (0.1.2)
Expand Down Expand Up @@ -329,7 +329,7 @@ PLATFORMS
x86_64-linux

DEPENDENCIES
bootstrap (~> 4.3)
bootstrap (~> 4.6)
byebug
capybara (~> 2.13)
coffee-rails (~> 5.0)
Expand Down
14 changes: 13 additions & 1 deletion app/assets/stylesheets/custom.scss
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
}
}

input[type=checkbox] {
accent-color: theme-color-level(primary, 0);
}

#dashboard-actions, .btn {
.fa, .fas, .far, .fal, .fad, .fab {
min-width: 1.5em;
Expand Down Expand Up @@ -203,7 +207,7 @@ nav.navbar-dark {
}
}
}
dd {
dd:not(.qc-body) {
padding-left: 4rem;
margin-left: 0.2rem;
}
Expand All @@ -215,3 +219,11 @@ nav.navbar-dark {
}
}

.workflow-panel {
.workflow-arrow {
position: absolute;
left: -1.1rem;
top: 0.25rem;
}
}

8 changes: 7 additions & 1 deletion app/controllers/checks_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
class ChecksController < ApplicationController
before_action(:authenticate_curator!)

# POST /checks/123
# POST /checks/123.json
def update
unless Name.exists?(params[:name_id].to_i)
Expand All @@ -27,7 +28,12 @@ def update
end

if success
render(json: @check.to_json, status: :ok)
respond_to do |format|
format.html { redirect_to(@check.name) }
format.json do
render(json: @check.to_json, status: :ok)
end
end
else
render(
json: @check.try(:errors) || { error: 'something went wrong' },
Expand Down
139 changes: 64 additions & 75 deletions app/controllers/names_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class NamesController < ApplicationController
edit_rank edit_notes edit_etymology edit_links edit_type
autofill_etymology link_parent link_parent_commit
return validate endorse claim unclaim new_correspondence
observe unobserve
]
)
before_action(
Expand All @@ -28,6 +29,7 @@ class NamesController < ApplicationController
return validate endorse
]
)
before_action(:authenticate_user!, only: %i[observe unobserve])

# GET /autocomplete_names.json?q=Maco
# GET /autocomplete_names.json?q=Allo&rank=genus
Expand Down Expand Up @@ -65,7 +67,7 @@ def index(opts = {})
Name.valid_status
end

@names =
@names ||=
case @sort
when 'date'
if opts[:status] == 15
Expand All @@ -81,7 +83,7 @@ def index(opts = {})
@sort = 'alphabetically'
Name.order(name: :asc)
end
@names = @names.where(status: opts[:status])
@names = @names.where(status: opts[:status]) if opts[:status]
@names = @names.where(opts[:where]) if opts[:where]
@names = @names.paginate(page: params[:page], per_page: 30)

Expand All @@ -104,9 +106,22 @@ def user_names
if params[:user] && current_user.admin?
user = User.find_by(username: params[:user])
end
@title = "Names by #{user.username}"
@title = "Names by #{user.username}"
@status = 'user'
index(where: { created_by: user }, status: Name.status_hash.keys)
index(where: { created_by: user })
render(:index)
end

# GET /observing-names
def observing_names
user = current_user
if params[:user] && current_user.admin?
user = User.find_by(username: params[:user])
end
@title = 'Names with active alerts'
@status = 'user'
@names = user.observing_names.reverse
index
render(:index)
end

Expand Down Expand Up @@ -203,6 +218,7 @@ def create

respond_to do |format|
if @name.save
@name.add_observer(current_user)
format.html { redirect_to @name, notice: 'Name was successfully created' }
format.json { render :show, status: :created, location: @name }
else
Expand Down Expand Up @@ -346,6 +362,7 @@ def link_parent_commit
end

if ok && @name.update(par)
par[:parent].add_observer(current_user)
flash[:notice] = 'Parent successfully updated'
redirect_to(@name)
else
Expand All @@ -355,103 +372,41 @@ def link_parent_commit

# POST /names/1/return
def return
par = { status: 5 }
if !@name.after_submission?
flash[:alert] = 'Name status is incompatible with return'
elsif @name.update(par)
# Email notification
AdminMailer.with(
user: @name.created_by,
name: @name,
action: 'return'
).name_status_email.deliver_later
flash[:notice] = 'Name returned to author'
else
flash[:alert] = 'An unexpected error occurred'
end
redirect_to(@name)
change_status(:return, 'Name returned to author', current_user)
end

# POST /names/1/validate
def validate
if params[:code] == 'icnp' || params[:code] == 'icn'
par = {
status: params[:code] == 'icnp' ? 20 : 25,
validated_by: current_user, validated_at: Time.now
}
if @name.validated?
flash[:alert] = 'Name status is incompatible with validation'
elsif @name.update(par)
# Email notification
AdminMailer.with(
user: @name.created_by,
name: @name,
action: 'validate'
).name_status_email.deliver_later
flash[:notice] = 'Name successfully validated'
else
flash[:alert] = 'An unexpected error occurred'
end
else
flash[:alert] =
'Invalid procedure for nomenclatural code ' + params[:code]
end
redirect_to(@name)
change_status(
:validate, 'Name successfully validated', current_user, params[:code]
)
end

# POST /names/1/endorse
def endorse
par = { status: 12, endorsed_by: current_user, endorsed_at: Time.now }
if @name.after_endorsement?
flash[:alert] = 'Name status is incompatible with endorsement'
elsif @name.update(par)
# Email notification
AdminMailer.with(
user: @name.created_by,
name: @name,
action: 'endorse'
).name_status_email.deliver_later
flash[:notice] = 'Name successfully endorsed'
else
flash[:alert] = 'An unexpected error occurred'
end
redirect_to(@name)
change_status(:endorse, 'Name successfully endorsed', current_user)
end

# POST /names/1/claim
def claim
if !@name.can_claim?(current_user)
flash[:alert] = 'You cannot claim this name'
elsif @name.claim(current_user)
flash[:notice] = 'Name successfully claimed'
else
flash[:alert] = 'An unexpected error occurred'
end
redirect_to(@name)
change_status(:claim, 'Name successfully claimed', current_user)
end

# POST /names/1/unclaim
def unclaim
par = { status: 0 }
if !@name.can_unclaim?(current_user)
flash[:alert] = 'You cannot unclaim this name'
elsif @name.unclaim(current_user)
flash[:notice] = 'Name successfully returned to the public pool'
else
flash[:alert] = 'An unexpected error occurred'
end
redirect_to(@name)
change_status(:unclaim, 'Name successfully claimed', current_user)
end

# POST /names/1/new_correspondence
def new_correspondence
@name_correspondence = NameCorrespondence.new(
params.require(:name_correspondence).permit(:message)
params.require(:name_correspondence).permit(:message, :notify)
)
unless @name_correspondence.message.empty?
@name_correspondence.user = current_user
@name_correspondence.name = @name
if @name_correspondence.save
@name.add_observer(current_user)
flash[:notice] = 'Correspondence recorded'
else
flash[:alert] = 'An unexpected error occurred with the correspondence'
Expand All @@ -460,6 +415,26 @@ def new_correspondence
redirect_to(@tutorial || @name)
end

# GET /names/1/observe
def observe
@name.add_observer(current_user)
if params[:from] && RedirectSafely.safe?(params[:from])
redirect_to(params[:from])
else
redirect_back(fallback_location: @name)
end
end

# GET /names/1/unobserve
def unobserve
@name.observers.delete(current_user)
if params[:from] && RedirectSafely.safe?(params[:from])
redirect_to(params[:from])
else
redirect_back(fallback_location: @name)
end
end

private

# Use callbacks to share common setup or constraints between actions
Expand Down Expand Up @@ -510,4 +485,18 @@ def etymology_pars
Name.etymology_fields.map { |j| :"etymology_#{i}_#{j}" }
end.flatten
end

def change_status(fun, success_msg, *extra_opts)
if @name.send(fun, *extra_opts)
flash[:notice] = success_msg
else
flash[:alert] = @name.status_alert
end
redirect_to(@name)
rescue ActiveRecord::RecordInvalid => inv
flash['alert'] =
'An unexpected error occurred while updating the name: ' +
inv.record.errors.map { |e| "#{e.attribute} #{e.message}" }.to_sentence
redirect_to(inv.record)
end
end
Loading

0 comments on commit 066efcf

Please sign in to comment.