Skip to content

Commit

Permalink
Added a convenience method
Browse files Browse the repository at this point in the history
  • Loading branch information
hectorcorrea committed May 17, 2022
1 parent a6b2257 commit 2912b46
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 32 deletions.
2 changes: 1 addition & 1 deletion app/controllers/works_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def index

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

Expand Down
29 changes: 16 additions & 13 deletions app/models/work.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,29 +41,32 @@ class Work < ApplicationRecord
end
end

def self.create_skeleton(title, user_id, collection_id, work_type)
def self.create_skeleton(title, user_id, collection_id, work_type, profile)
work = Work.new(
title: title,
created_by_user_id: user_id,
collection_id: collection_id,
work_type: work_type,
state: "AWAITING-APPROVAL"
state: "AWAITING-APPROVAL",
profile: profile
)
work.save!
work
end

# def self.create_dataset(title, user_id, collection_id, work_type)
# work = Work.new(
# title: title,
# created_by_user_id: user_id,
# collection_id: collection_id,
# work_type: "DATASET",
# state: "AWAITING-APPROVAL"
# )
# work.save!
# work
# end
# Convenience method to create Datasets with the DataCite profile
def self.create_dataset(title, user_id, collection_id)
work = Work.new(
title: title,
created_by_user_id: user_id,
collection_id: collection_id,
work_type: "DATASET",
state: "AWAITING-APPROVAL",
profile: "DATACITE"
)
work.save!
work
end

def dataset_id
Dataset.where(work_id: id).first&.id
Expand Down
2 changes: 1 addition & 1 deletion app/services/dspace_import_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def import!
end

def work
@work ||= Work.create_skeleton(title, user.id, collection.id, work_type)
@work ||= Work.create_skeleton(title, user.id, collection.id, work_type, "DUBLINCORE")
end

private
Expand Down
2 changes: 1 addition & 1 deletion spec/controllers/works_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
end
let(:user) { FactoryBot.create(:user) }
let(:collection) { Collection.first }
let(:work) { Work.create_skeleton("test dataset", user.id, collection.id, "DATASET") }
let(:work) { Work.create_dataset("test dataset", user.id, collection.id) }

context "valid user login" do
it "handles the index page" do
Expand Down
31 changes: 15 additions & 16 deletions spec/models/work_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@
let(:identifier) { @identifier }

it "creates a skeleton dataset" do
work = described_class.create_skeleton("test title", user.id, collection.id, "DATASET")
work = described_class.create_dataset("test title", user.id, collection.id)
expect(work.created_by_user.id).to eq user.id
expect(work.collection.id).to eq collection.id
expect(work.ark).to be_blank
expect(work.doi).to be_blank
end

it "mints an ARK on save (and only when needed)" do
work = described_class.create_skeleton("test title", user.id, collection.id, "DATASET")
work = described_class.create_dataset("test title", user.id, collection.id)
expect(work.ark).to be_blank
work.save
expect(work.ark).to be_present
Expand All @@ -30,38 +30,37 @@
end

it "prevents datasets with no users" do
expect { described_class.create_skeleton("test title", 0, collection.id, "DATASET") }.to raise_error
expect { described_class.create_dataset("test title", 0, collection.id) }.to raise_error
end

it "prevents datasets with no collections" do
expect { described_class.create_skeleton("test title", user.id, 0, "DATASET") }.to raise_error
expect { described_class.create_dataset("test title", user.id, 0) }.to raise_error
end

it "approves works and records the change history" do
work = described_class.create_skeleton("test title", user.id, collection.id, "DATASET")
work = described_class.create_dataset("test title", user.id, collection.id)
work.approve(user)
expect(work.state_history.first.state).to eq "APPROVED"
end

it "withdraw works and records the change history" do
work = described_class.create_skeleton("test title", user.id, collection.id, "DATASET")
work = described_class.create_dataset("test title", user.id, collection.id)
work.withdraw(user)
expect(work.state_history.first.state).to eq "WITHDRAWN"
end

it "resubmit works and records the change history" do
work = described_class.create_skeleton("test title", user.id, collection.id, "DATASET")
work = described_class.create_dataset("test title", user.id, collection.id)
work.resubmit(user)
expect(work.state_history.first.state).to eq "AWAITING-APPROVAL"
end

describe "#created_by_user" do
context "when the ID is invalid" do
subject(:work) { described_class.create_skeleton(title, user_id, collection_id, work_type) }
subject(:work) { described_class.create_dataset(title, user_id, collection_id) }
let(:title) { "test title" }
let(:user_id) { user.id }
let(:collection_id) { collection.id }
let(:work_type) { "DATASET" }

before do
allow(User).to receive(:find).and_raise(ActiveRecord::RecordNotFound)
Expand All @@ -74,7 +73,7 @@
end

describe "#dublin_core=" do
subject(:work) { described_class.create_skeleton(title, user_id, collection_id, work_type) }
subject(:work) { described_class.create_skeleton(title, user_id, collection_id, work_type, "DUBLINCORE") }
let(:title) { "test title" }
let(:user_id) { user.id }
let(:collection_id) { collection.id }
Expand All @@ -88,7 +87,7 @@
end

context "when created with an existing ARK" do
subject(:data_set) { described_class.create_skeleton("test title", user.id, collection.id, "DATASET") }
subject(:data_set) { described_class.create_dataset("test title", user.id, collection.id) }

context "and when the ARK is valid" do
let(:ezid) { "ark:/88435/dsp01qb98mj541" }
Expand All @@ -109,7 +108,7 @@
end

it "updates the ARK metadata" do
data_set = described_class.create_skeleton("test title", user.id, collection.id, "DATASET")
data_set = described_class.create_dataset("test title", user.id, collection.id)

data_set.ark = ezid
data_set.save
Expand Down Expand Up @@ -142,8 +141,8 @@
end

it "returns datasets waiting for approval depending on the user" do
described_class.create_skeleton("test title", user.id, collection.id, "DATASET")
described_class.create_skeleton("test title", user_other.id, collection.id, "DATASET")
described_class.create_dataset("test title", user.id, collection.id)
described_class.create_dataset("test title", user_other.id, collection.id)

# Superadmins can approve pending works
awaiting = described_class.admin_works_by_user_state(superadmin_user, "AWAITING-APPROVAL")
Expand All @@ -164,8 +163,8 @@

describe ".my_datasets" do
before do
described_class.create_skeleton("test title", user.id, collection.id, "DATASET")
described_class.create_skeleton("test title", user.id, collection.id, "DATASET")
described_class.create_dataset("test title", user.id, collection.id)
described_class.create_dataset("test title", user.id, collection.id)
end

it "retrieves Dataset models associated with a given User" do
Expand Down

0 comments on commit 2912b46

Please sign in to comment.