Skip to content

Commit

Permalink
Create redirections
Browse files Browse the repository at this point in the history
  • Loading branch information
lmrodriguezr committed Aug 2, 2024
1 parent 556f8c4 commit 2da2937
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 6 deletions.
28 changes: 26 additions & 2 deletions app/controllers/names_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class NamesController < ApplicationController
proposed_in not_validly_proposed_in emended_in assigned_in
corrigendum_in corrigendum_orphan corrigendum
edit_description edit_rank edit_notes edit_etymology edit_links edit_type
autofill_etymology edit_parent
edit_redirect autofill_etymology edit_parent
return validate endorse claim unclaim demote new_correspondence
observe unobserve
]
Expand All @@ -32,7 +32,7 @@ class NamesController < ApplicationController
:authenticate_curator!,
only: %i[
unranked unknown_proposal submitted endorsed draft
return validate endorse
return validate endorse edit_redirect
]
)
before_action(:authenticate_user!, only: %i[observe unobserve observing])
Expand Down Expand Up @@ -93,6 +93,7 @@ def index(opts = {})
@sort = 'alphabetically'
Name.order(name: :asc)
end
@names = @names.where(redirect: nil)
@names = @names.where(status: opts[:status]) if opts[:status]
@names = @names.where(rank: opts[:rank]) if opts[:rank]
@names = @names.where(opts[:where]) if opts[:where]
Expand Down Expand Up @@ -176,6 +177,14 @@ def syllabify
# GET /names/1.json
# GET /names/1.pdf
def show
if @name.redirect.present? && !params[:no_redirect]
redirect_to(
name_url(@name.redirect, format: params[:format], redirector: @name.id)
)
return
end

@redirector = Name.find(params[:redirector]) if params[:redirector]
@publication_names =
@name.publication_names_ordered
.paginate(page: params[:page], per_page: 10)
Expand Down Expand Up @@ -266,6 +275,10 @@ def edit_type
end
end

# GET /names/1/edit_redirect
def edit_redirect
end

# GET /names/1/autofill_etymology
def autofill_etymology
@name.autofill_etymology
Expand Down Expand Up @@ -314,6 +327,15 @@ def update
end
end

if params[:edit].==('redirect')
if name_params[:redirect].present?
name_params[:redirect] = Name.find_by(name: name_params[:redirect])
name_params[:status] = @name.status <= 15 ? 0 : @name.status
else
name_params[:redirect] = nil
end
end

respond_to do |format|
if @name.update(name_params)
format.html { redirect_to(params[:return_to] || @name, notice: 'Name was successfully updated') }
Expand Down Expand Up @@ -558,6 +580,8 @@ def name_params
] + etymology_pars
end

fields << :redirect if current_user.try(:curator?)

@name_params ||= params.require(:name).permit(*fields.uniq)
end

Expand Down
5 changes: 5 additions & 0 deletions app/models/name.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ class Name < ApplicationRecord
:synonyms, class_name: 'Name', foreign_key: 'correct_name_id',
dependent: :nullify
)
has_many(
:redirectors, class_name: 'Name', foreign_key: 'redirect_id',
dependent: :nullify
)
alias :correspondences :name_correspondences
has_many(:checks, dependent: :destroy)
has_many(:check_users, -> { distinct }, through: :checks, source: :user)
Expand All @@ -42,6 +46,7 @@ class Name < ApplicationRecord
)
belongs_to(:parent, optional: true, class_name: 'Name')
belongs_to(:correct_name, optional: true, class_name: 'Name')
belongs_to(:redirect, optional: true, class_name: 'Name')
belongs_to(
:created_by, optional: true,
class_name: 'User', foreign_key: 'created_by_id'
Expand Down
8 changes: 8 additions & 0 deletions app/views/names/_alert_messages.html.erb
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
<% if @redirector %>
<div class="alert alert-info">
Redirected from
<%= link_to(@redirector.name_html,
name_url(@redirector, no_redirect: true)) %>
</div>
<% end %>
<% if @name.auto? %>
<div class="alert alert-info">
This name was automatically created through literature mining,
Expand Down
14 changes: 11 additions & 3 deletions app/views/names/_contributor.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,18 @@
<div class="alert alert-danger my-0">
<h5>Remove name</h5>
<p>
<u>This action cannot be undone</u>. Once you remove this name, all
associated metadata will also be removed, including any validation
or curation progress.
<u>This action cannot be undone</u>. Once you remove this name,
all associated metadata will also be removed, including any
validation or curation progress.
</p>
<% if current_curator? %>
<p>
<u>Prefer redirections</u>. If you are removing this name
because a different entry replaces it, a beter option is to
<%= link_to('create a redirection',
edit_redirect_name_url(@name)) %>.
</p>
<% end %>
<%= link_to(
@name, method: :delete,
class: 'btn btn-danger',
Expand Down
6 changes: 6 additions & 0 deletions app/views/names/_curator.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,12 @@
<% end %>
&raquo; Generate source for Wikipedia affiliated pages
<hr/>
<%= link_to(edit_redirect_name_url(@name),
class: 'btn btn-sm btn-danger') do %>
<%= fa_icon('directions') %> Create redirection
<% end %>
&raquo; Soft-remove this name and redirect to a different entry
<hr/>
</dd>
</dl>
<% end %>
23 changes: 23 additions & 0 deletions app/views/names/edit_redirect.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<h1>Soft-remove <%= @name.name_html %></h1>
<hr/>

<div class="alert alert-warning">
This will cause the page's name to redirect to a different entry,
but the data and metadata might still be accessible.
</div>

<%= simple_form_for(@name) do |f| %>
<%= hidden_field_tag(:edit, :redirect) %>
<%=
f.input(
:redirect, label: 'Redirect to name',
input_html: {
value: @name.redirect&.name,
data: { behavior: 'autocomplete', autocomplete: 'names' }
}
)
%>
<%= f.submit('Soft-remove', class: 'btn btn-danger') %>
<%= link_to('Cancel', @name, class: 'btn btn-secondary') %>
<% end %>

1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
get :edit_notes
get :edit_rank
get :edit_links
get :edit_redirect
post :return
post :validate
post :endorse
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20240802161929_add_redirect_to_names.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddRedirectToNames < ActiveRecord::Migration[6.1]
def change
add_column :names, :redirect_id, :integer, default: nil
end
end
3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2024_07_22_112248) do
ActiveRecord::Schema.define(version: 2024_08_02_161929) do

create_table "action_text_rich_texts", force: :cascade do |t|
t.string "name", null: false
Expand Down Expand Up @@ -208,6 +208,7 @@
t.string "gtdb_accession"
t.string "algaebase_species"
t.string "algaebase_taxonomy"
t.integer "redirect_id"
t.index ["genome_id"], name: "index_names_on_genome_id"
t.index ["name"], name: "index_names_on_name", unique: true
t.index ["parent_id"], name: "index_names_on_parent_id"
Expand Down

0 comments on commit 2da2937

Please sign in to comment.