Skip to content

Commit

Permalink
Final touches
Browse files Browse the repository at this point in the history
  • Loading branch information
hectorcorrea committed May 17, 2022
1 parent 2912b46 commit 62c7bb1
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 34 deletions.
8 changes: 4 additions & 4 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ class UsersController < ApplicationController
def show
@can_edit = can_edit?
if current_user.id == @user.id
@my_datasets = Work.my_works(current_user)
@awaiting_datasets = Work.admin_awaiting_works(current_user)
@withdrawn_datasets = Work.admin_withdrawn_works(current_user)
@my_works = Work.my_works(current_user)
@awaiting_works = Work.admin_awaiting_works(current_user)
@withdrawn_works = Work.admin_withdrawn_works(current_user)
render "dashboard"
else
@datasets = Work.my_works(@user)
@works = Work.my_works(@user)
render "show"
end
end
Expand Down
6 changes: 4 additions & 2 deletions app/models/ark.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
require "ezid-client"

class Ark
EZID_INVALID_SHOULDER = "ark:/99999"
EZID_TEST_SHOULDER = "ark:/99999"

# Mints a new EZID identifier, returns the id (e.g. "ark:/99999/fk4tq65d6k")
def self.mint
Expand All @@ -21,6 +21,8 @@ def self.find(ezid)
# @param [ezid] [String] the EZID being validated
# @return [Boolean]
def self.valid?(ezid)
# Always consider test ARKs valid
return true if ezid.start_with?(EZID_TEST_SHOULDER)
# Try and retrieve the ARK
new(ezid)
true
Expand All @@ -29,7 +31,7 @@ def self.valid?(ezid)
end

def self.valid_shoulder?(ezid)
!ezid.include?(self::EZID_INVALID_SHOULDER)
!ezid.include?(self::EZID_TEST_SHOULDER)
end

def initialize(ezid)
Expand Down
12 changes: 4 additions & 8 deletions app/models/work.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class Work < ApplicationRecord

validate do |ds|
if ds.ark.present?
ds.errors.add(:base, "Invalid ARK provided for the Dataset: #{ds.ark}") unless Ark.valid?(ds.ark)
ds.errors.add(:base, "Invalid ARK provided for the Work: #{ds.ark}") unless Ark.valid?(ds.ark)
end
end

Expand Down Expand Up @@ -68,10 +68,6 @@ def self.create_dataset(title, user_id, collection_id)
work
end

def dataset_id
Dataset.where(work_id: id).first&.id
end

def approve(user)
self.state = "APPROVED"
save!
Expand Down Expand Up @@ -148,10 +144,10 @@ def self.admin_withdrawn_works(user)
admin_works_by_user_state(user, "WITHDRAWN")
end

# Returns that datasets that an admin user has in a given state.
# Returns that works that an admin user has in a given state.
#
# Notice that it *excludes* the datasets created by the admin user
# (since their own datasets will already be shown on their dashboard)
# Notice that it *excludes* the works created by the admin user
# (since their own works will already be shown on their dashboard)
def self.admin_works_by_user_state(user, state)
admin_collections = []
Collection.all.find_each do |collection|
Expand Down
12 changes: 6 additions & 6 deletions app/views/users/dashboard.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
<%= link_to 'Edit', edit_user_path(@user), :class => "btn btn-primary", :role => "button" %>
</div>

<% if @my_datasets.count > 0 %>
<% if @my_works.count > 0 %>
<h2>My Datasets</h2>
<% @my_datasets.each do |ds| %>
<% @my_works.each do |ds| %>
<p><%= link_to(ds.title, work_path(ds)) %> (<%= ds.created_by_user.uid %>), <%= ds.state %></p>
<% end %>
<% end %>
Expand All @@ -28,16 +28,16 @@
<%= link_to 'Add New Dataset', new_work_path, class: "btn btn-primary", :role => "button" %>
</div>

<% if @awaiting_datasets.count > 0 %>
<% if @awaiting_works.count > 0 %>
<h2>Datasets waiting to be approved</h2>
<% @awaiting_datasets.each do |ds| %>
<% @awaiting_works.each do |ds| %>
<p><%= link_to(ds.title, work_path(ds)) %> (<%= ds.created_by_user.uid %>), <%= ds.state %></p>
<% end %>
<% end %>
<% if @withdrawn_datasets.count > 0 %>
<% if @withdrawn_works.count > 0 %>
<h2>Datasets to have been withdrawn</h2>
<% @withdrawn_datasets.each do |ds| %>
<% @withdrawn_works.each do |ds| %>
<p><%= link_to(ds.title, work_path(ds)) %> (<%= ds.created_by_user.uid %>), <%= ds.state %></p>
<% end %>
<% end %>
Expand Down
2 changes: 1 addition & 1 deletion app/views/users/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
</div>
<% end %>
<% if @datasets.count == 0 %>
<% if @works.count == 0 %>
<p>This user has not deposited datasets.</p>
<% else %>
<h2>Datasets</h2>
Expand Down
2 changes: 1 addition & 1 deletion app/views/works/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
-->
<% if @work.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(dataset.errors.count, "error") %> prohibited this dataset from being saved:</h2>
<h2><%= pluralize(@work.errors.count, "error") %> prohibited this dataset from being saved:</h2>
<ul>
<% @work.errors.each do |error| %>
<li><%= error.full_message %></li>
Expand Down
16 changes: 4 additions & 12 deletions spec/models/work_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,23 +119,15 @@

context "and when the ARK is invalid" do
before do
# The HTTP call to EZID will fail because the id is invalid
allow(Ezid::Identifier).to receive(:find).and_raise(Net::HTTPServerException, '400 "Bad Request"')
end

# TODO: re-enable this once we fix the ARK validation to account for test ARKs
# See https://github.com/pulibrary/pdc_describe/issues/124
# it "raises an error" do
# expect(data_set.persisted?).not_to be false
# data_set.ark = ezid
# expect { data_set.save! }.to raise_error("Validation failed: Invalid ARK provided for the Dataset: #{ezid}")
# end
end

context "and when the ARK is valid but minted for the test environment" do
it "raises an error" do
expect(data_set.persisted?).not_to be false
data_set.ark = ezid
expect { data_set.save! }.to raise_error("Validation failed: Invalid ARK provided for the Dataset: #{ezid}")
bad_ezid = "ark:/bad-99999/fk4tq65d6k"
data_set.ark = bad_ezid
expect { data_set.save! }.to raise_error(ActiveRecord::RecordInvalid, "Validation failed: Invalid ARK provided for the Work: #{bad_ezid}")
end
end
end
Expand Down

0 comments on commit 62c7bb1

Please sign in to comment.