Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Started adding auto complete to contributors #1994

Draft
wants to merge 11 commits into
base: main
Choose a base branch
from
49 changes: 49 additions & 0 deletions app/assets/stylesheets/application.scss
Original file line number Diff line number Diff line change
Expand Up @@ -389,3 +389,52 @@ table.dataTable {
.user-edit-long-field {
width: 400px;
}

.hidden-element {
display: none;
}

/* These are the styles used by the jQuery autocomplete plug-in */
.autocomplete-suggestions {
border: 1px solid #999;
background: #FFF;
overflow: auto;
}

.autocomplete-suggestion {
padding: 2px 5px;
white-space: nowrap;
overflow: hidden;
}

.autocomplete-selected {
background: #F0F0F0;
}

.autocomplete-suggestions strong {
font-weight: normal;
color: #3399FF;
}

.autocomplete-group {
padding: 2px 5px;
}

.autocomplete-group strong {
display: block;
border-bottom: 1px solid #000;
}

/*
* Adds the arrow to the autocomplete textbox.
* Notice that since CSS does not allow ::after on HTML <input> we apply this style to an HTML <span>.
* See https://stackoverflow.com/questions/2587669/can-i-use-a-before-or-after-pseudo-element-on-an-input-field
*/
.autocomplete-arrow::after {
content: "▾";
margin-left: -20px;
}

.autocomplete-error {
color: var(--Secondary-Red-Dark, #b00002);
}
14 changes: 14 additions & 0 deletions app/controllers/researchers_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true
class ResearchersController < ApplicationController
before_action :authenticate_user!

def ajax_list
researchers = { suggestions: Researcher.autocomplete_list}
render json: researchers.to_json
end

def index
@researchers = Researcher.all
end

end
24 changes: 24 additions & 0 deletions app/models/researcher.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# frozen_string_literal: true
class Researcher < ApplicationRecord
def self.new_researcher(first_name, last_name, orcid, netid)
researcher = Researcher.where(orcid: orcid).first
if researcher == nil
researcher = Researcher.new
researcher.netid = netid
end
researcher.first_name = first_name
researcher.last_name = last_name
researcher.orcid = orcid
researcher.save!
return researcher
end

def self.autocomplete_list
researchers = []
Researcher.all.each do |researcher|
display_value = "#{researcher.first_name} #{researcher.last_name} (#{researcher.netid})"
researchers << {value: display_value, data: researcher.netid}
end
return researchers
end
end
3 changes: 3 additions & 0 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
<!-- Provides https://jqueryui.com/sortable/ and /autocomplete/-->
<script src="https://code.jquery.com/ui/1.13.1/jquery-ui.js"></script>

<!-- Load our local Autocomplete plug-in which is a copy of https://github.com/devbridge/jQuery-Autocomplete -->
<script src="<%= root_path + 'jquery.autocomplete.js' %>"></script>

<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.11.3/css/jquery.dataTables.min.css"/>
<script type="text/javascript" src="https://cdn.datatables.net/1.11.3/js/jquery.dataTables.min.js"></script>
<%= favicon_link_tag asset_path('favicon.png') %>
Expand Down
26 changes: 26 additions & 0 deletions app/views/researchers/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<h1>Researchers</h1>

<table id="user-list-table">
<thead>
<tr class="user-list-header-row">
<th>First name</th>
<th>Last name</th>
<th>Net ID</th>
<th>ORCID</th>
</tr>
</thead>

<tbody>
<% @researchers.each do |researcher| %>
<tr class="user-list-data-row">
<td><%= researcher.first_name %></td>
<td><%= researcher.last_name %></td>
<td><%= researcher.netid %></td>
<td><%= researcher.orcid %></td>
</tr>
<% end %>
</tbody>
</table>



2 changes: 2 additions & 0 deletions app/views/works/_funder_table.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,5 @@
</tfoot>
</table>
</div>


26 changes: 24 additions & 2 deletions app/views/works/_required_creators_table.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@
<tr class="creators-table-row">
<td>
<span class="hidden given-name-required-message"><i class="bi bi-exclamation-diamond-fill required-field"></i>&nbsp;Must provide a given name</span>
<input name="creators[][given_name]" value="<%= creator&.given_name %>" class="given-entry-creator" />
<input id="creator-row-givenname" name="creators[][given_name]" value="<%= creator&.given_name %>" class="given-entry-creator" />
</td>
<td class="creators-table-row-family-name">
<span class="hidden family-name-required-message"><i class="bi bi-exclamation-diamond-fill required-field"></i>&nbsp;Must provide a family name</span>
<input name="creators[][family_name]" value="<%= creator&.family_name %>" class="family-entry-creator" />
<input id="creator-row-familyname" name="creators[][family_name]" value="<%= creator&.family_name %>" class="family-entry-creator" />
</td>
<td>
<input name="creators[][orcid]" value="<%= creator&.orcid %>" class="orcid-entry-creator" placeholder="0000-0000-0000-0000" />
Expand Down Expand Up @@ -77,3 +77,25 @@
</table>
</div>

<script>
// debugger;
// Documentation: https://github.com/devbridge/jQuery-Autocomplete
$('#creator-row-givenname').devbridgeAutocomplete({
serviceUrl: '<%= researchers_ajax_list_url %>',
onSelect: function (suggestion) {
$("#creator-row-familyname").val(suggestion.data);
}
});

// Settings that we use in TigerData
// $(elementId).autocomplete({
// minChars: 1,
// autoSelectFirst: true,
// showNoSuggestionNotice: true,
// noSuggestionNotice: "No results found",
// lookup: data,
// onSelect: function (suggestion) {
// $(elementId).val(suggestion.data);
// }
// });
</script>
5 changes: 5 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@
get "/collections/:id", to: redirect("/groups/%{id}"), as: :collections
get "/reports/dataset-list", to: "reports#dataset_list", as: :reports_dataset_list

# researchers
resources :researchers
get "/researchers/ajax-list", to: "researchers#ajax_list", as: :researchers_ajax_list
get "/researchers", to: "researchers#index", as: :researchers_list

# Anything still unmatched by the end of the routes file should go to the not_found page
# match '*a', to: redirect('/404'), via: :get

Expand Down
14 changes: 14 additions & 0 deletions db/migrate/20241202152610_create_researchers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class CreateResearchers < ActiveRecord::Migration[6.1]
def change
create_table :researchers do |t|
t.string "first_name", null: false
t.string "last_name", null: false
t.string "orcid", null: false
t.string "affiliation", null: true
t.string "affiliation_ror", null: true
t.string "netid", null: true

t.timestamps
end
end
end
13 changes: 12 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions lib/tasks/researchers.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# frozen_string_literal: true
namespace :researchers do
desc "Creates default researchers"
task create_default_researchers: :environment do
User.all.each do |user|
if user.orcid != nil
Researcher.new_researcher(user.given_name, user.family_name, user.orcid, user.uid)
end
end
end

task researchers_from_works: :environment do
Work.all.each do |work|
creators = work.resource.creators
creators.each do |creator|
if creator.identifier&.scheme == "ORCID"
Researcher.new_researcher(creator.given_name, creator.family_name, creator.identifier.value, nil)
end
end
end
end
end
Loading