Skip to content

Commit

Permalink
First pass at droping the Dataset and moving the functionality to the…
Browse files Browse the repository at this point in the history
… Work. System runs, tests have not been updated.
  • Loading branch information
hectorcorrea committed May 17, 2022
1 parent 4c4a684 commit cc43cad
Show file tree
Hide file tree
Showing 17 changed files with 160 additions and 214 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 = Dataset.my_datasets(current_user)
@awaiting_datasets = Dataset.admin_awaiting_datasets(current_user)
@withdrawn_datasets = Dataset.admin_withdrawn_datasets(current_user)
@my_datasets = Work.my_works(current_user)
@awaiting_datasets = Work.admin_awaiting_works(current_user)
@withdrawn_datasets = Work.admin_withdrawn_works(current_user)
render "dashboard"
else
@datasets = Dataset.my_datasets(@user)
@datasets = Work.my_works(@user)
render "show"
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,106 +4,86 @@
# rubocop:disable Metrics/AbcSize
# rubocop:disable Metrics/MethodLength
# rubocop:disable Style/For
class DatasetsController < ApplicationController
class WorksController < ApplicationController
def index
@datasets = Dataset.all
@works = Work.all
end

def new
default_collection_id = current_user.default_collection.id
dataset = Dataset.create_skeleton("New Dataset", current_user.id, default_collection_id)
redirect_to edit_dataset_path(dataset)
work = Work.create_skeleton("New Work", current_user.id, default_collection_id, "DATASET")
redirect_to edit_work_path(work)
end

def show
@dataset = Dataset.find(params[:id])
work = Work.find(@dataset.work_id)
@datacite = Datacite::Resource.new_from_json(work.data_cite)
@work = Work.find(params[:id])
@datacite = Datacite::Resource.new_from_json(@work.data_cite)

if @dataset.doi
service = S3QueryService.new(@dataset.doi)
if @work.doi
service = S3QueryService.new(@work.doi)
@files = service.data_profile
end
end

def edit
@dataset = Dataset.find(params[:id])
work = Work.find(@dataset.work_id)
@datacite = Datacite::Resource.new_from_json(work.data_cite)
@work = Work.find(params[:id])
@datacite = Datacite::Resource.new_from_json(@work.data_cite)
if @datacite.main_title.nil?
@datacite.titles << Datacite::Title.new(title: "Enter title here")
end
end

def update
@dataset = Dataset.find(params[:id])
@work = Work.find(params[:id])
respond_to do |format|
update_work_record
# And then update the dataset
if @dataset.update(dataset_params)
format.html { redirect_to dataset_url(@dataset), notice: "Dataset was successfully updated." }
format.json { render :show, status: :ok, location: @dataset }
work_params = {
title: params[:title_main],
collection_id: params[:collection_id],
data_cite: datacite_resource_from_form
}
if @work.update(work_params)
format.html { redirect_to work_url(@work), notice: "Work was successfully updated." }
format.json { render :show, status: :ok, location: @work }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: @dataset.errors, status: :unprocessable_entity }
format.json { render json: @work.errors, status: :unprocessable_entity }
end
end
end

def approve
dataset = Dataset.find(params[:id])
dataset.work.approve(current_user)
redirect_to dataset_path(dataset)
work = Work.find(params[:id])
work.approve(current_user)
redirect_to work_path(work)
end

def withdraw
dataset = Dataset.find(params[:id])
dataset.work.withdraw(current_user)
redirect_to dataset_path(dataset)
work = Work.find(params[:id])
work.withdraw(current_user)
redirect_to work_path(work)
end

def resubmit
dataset = Dataset.find(params[:id])
dataset.work.resubmit(current_user)
redirect_to dataset_path(dataset)
work = Work.find(params[:id])
work.resubmit(current_user)
redirect_to work_path(work)
end

# Outputs the Datacite XML representation of the dataset
# Outputs the Datacite XML representation of the work
def datacite
@dataset = Dataset.find(params[:id])
work = Work.find(@dataset.work_id)
work = Work.find(params[:id])
resource = Datacite::Resource.new_from_json(work.data_cite)
render xml: resource.to_xml
end

private

# Only allow a list of trusted parameters through.
def form_params
valid_list = [:work_id]
params.require(:dataset).permit(valid_list)
end

def work_params
{
title: params[:title],
collection_id: params[:collection_id]
}
end

def dataset_params
form_params.select { |x| x == "work_id" }
end

def new_creator(given_name, family_name, orcid)
return if family_name.blank? && given_name.blank? && orcid.blank?
Datacite::Creator.new_person(given_name, family_name, orcid)
end

# Populate the work.data_cite field
def update_work_record
work = Work.find(form_params[:work_id])
work_data = work_params
def datacite_resource_from_form
resource = Datacite::Resource.new(title: params["title"])

# Process the titles
Expand Down Expand Up @@ -131,9 +111,7 @@ def update_work_record
resource.creators << creator unless creator.nil?
end

work_data[:data_cite] = resource.to_json
work.update(work_data)
work.save!
resource.to_json
end
end
# rubocop:enable Metrics/ClassLength
Expand Down
106 changes: 0 additions & 106 deletions app/models/dataset.rb

This file was deleted.

74 changes: 74 additions & 0 deletions app/models/work.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# frozen_string_literal: true

class Work < ApplicationRecord
include Rails.application.routes.url_helpers

belongs_to :collection

before_save do |work|
Expand All @@ -10,6 +12,32 @@ class Work < ApplicationRecord
end
end

before_update do |ds|
if ds.ark.blank?
ds.ark = Ark.mint
end
end

after_save do |ds|
# We only want to update the ark url under certain conditions.
# Set this value in config/update_ark_url.yml
if Rails.configuration.update_ark_url
if ds.ark.present?
# Ensure that the ARK metadata is updated for the new URL
if ark_object.target != ds.url
ark_object.target = ds.url
ark_object.save!
end
end
end
end

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

def self.create_skeleton(title, user_id, collection_id, work_type)
work = Work.new(
title: title,
Expand Down Expand Up @@ -75,4 +103,50 @@ def dublin_core=(value)
rescue JSON::ParserError => parse_error
raise(ArgumentError, "Invalid JSON passed to Work#dublin_core=: #{parse_error}")
end

def ark_url
"https://ezid.cdlib.org/id/#{ark}"
end

def ark_object
@ark_object ||= Ark.new(ark)
end

def url
return unless persisted?

@url ||= url_for(self)
end

def self.my_works(user)
Work.where(created_by_user_id: user)
end

def self.admin_awaiting_works(user)
admin_works_by_user_state(user, "AWAITING-APPROVAL")
end

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.
#
# Notice that it *excludes* the datasets created by the admin user
# (since their own datasets will already be shown on their dashboard)
def self.admin_works_by_user_state(user, state)
admin_collections = []
Collection.all.find_each do |collection|
admin_collections << collection if user.can_admin?(collection.id)
end

works = []
admin_collections.each do |collection|
condition = "collection_id = :collection_id AND state = :state AND (created_by_user_id != :user_id)"
values = { collection_id: collection.id, state: state, user_id: user.id }
works += Work.where([condition, values])
end

works
end
end
10 changes: 0 additions & 10 deletions app/views/datasets/index.html.erb

This file was deleted.

Loading

0 comments on commit cc43cad

Please sign in to comment.