Skip to content

Commit

Permalink
Remove dataspace code from DatasetFile (#764)
Browse files Browse the repository at this point in the history
refs #685
  • Loading branch information
carolyncole authored Mar 5, 2025
1 parent 2f80dac commit 87f4fce
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 70 deletions.
32 changes: 2 additions & 30 deletions app/models/dataset_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,8 @@ class DatasetFile
attr_accessor :name, :description, :format, :size, :display_size, :mime_type, :sequence, :handle, :extension,
:source, :download_url, :full_path

def self.from_hash(data, data_source)
if data_source == "pdc_describe"
from_hash_describe(data)
else
from_hash_dataspace(data)
end
end

def self.from_hash_dataspace(data)
hash = data.with_indifferent_access
file = DatasetFile.new
file.source = "dataspace"
file.full_path = hash[:name]
file.name = hash[:name]
file.extension = File.extname(file.name)
file.extension = file.extension[1..] if file.extension != "." # drop the leading period
file.description = hash[:description]
file.mime_type = hash[:mime_type]
file.size = hash[:size]
file.display_size = ActiveSupport::NumberHelper.number_to_human_size(file.size)
file.sequence = (hash[:sequence] || "").to_i
# Technically the handle is a property of the dataset item rather than the file (aka bitstream)
# but we store it at the file level for convenience.
file.handle = hash[:handle]
file.download_url = "#{DatasetFile.download_root}/#{file.handle}/#{file.sequence}"
file
def self.from_hash(data, _data_source)
from_hash_describe(data)
end

def self.from_hash_describe(data)
Expand Down Expand Up @@ -70,10 +46,6 @@ def self.filename_without_doi(full_path)
end
end

def self.download_root
"#{Rails.configuration.pdc_discovery.dataspace_url}/bitstream"
end

def self.sort_file_array(file_array)
sorted_by_name = file_array.sort_by(&:name)
sorted_file_array = []
Expand Down
6 changes: 0 additions & 6 deletions spec/models/dataset_file_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,6 @@

RSpec.describe DatasetFile do
describe "#download_url" do
it "builds the proper URL for a DataSpace record" do
hash = { name: "big file.zip", handle: "123/dsp456", sequence: 1 }
file = described_class.from_hash(hash, "dataspace")
expect(file.download_url).to eq "https://dataspace-dev.princeton.edu/bitstream/123/dsp456/1"
end

it "builds the proper URL for a PDC Describe record" do
hash = {
filename: "10.34770/qyrs-vg25/50/file_name.txt",
Expand Down
19 changes: 0 additions & 19 deletions spec/models/document_export_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@
{ files: [{ filename: "/folder1/file1.zip", size: 27, url: "https://pdc_describe", display_size: "11 KB" }, { filename: "data.csv", size: 100 }, { filename: "file2.zip", size: 200 }] }
end

let(:files_dataspace) do
[{ name: "file1.zip", size: 27, handle: "xyz" }, { name: "data.csv", size: 29, handle: "yzx" }, { name: "file2.zip", size: 28, handle: "zxy" }]
end

let(:solr_doc_pdc_describe) do
SolrDocument.new({
id: "1", title_tesim: ["Hello World"], pdc_describe_json_ss: files_pdc_describe.to_json,
Expand All @@ -25,10 +21,6 @@
})
end

let(:solr_doc_dataspace) do
SolrDocument.new({ id: "1", title_tesim: ["Hello World"], files_ss: files_dataspace.to_json, data_source_ssi: "dataspace", description_tsim: ["Something"], abstract_tsim: ["Abstract"] })
end

it "returns DocumentExport object's information from pdc_describe" do
document = described_class.new(solr_doc_pdc_describe)
expect(document.id).to be "1"
Expand All @@ -47,16 +39,5 @@
expect(document.authors.count).to be 6
expect(document.authors[0].value).to eq "Alt, Andrew"
end

it "returns DocumentExport object's information from dataspace" do
document = described_class.new(solr_doc_dataspace)
expect(document.id).to be "1"
expect(document.title).to be "Hello World"
expect(document.files.count).to eq 3
expect(document.description).to eq "Something"
expect(document.abstract).to eq "Abstract"
expect(document.files.first.name).to eq "file1.zip"
expect(document.files.first.download_url).to eq "https://dataspace-dev.princeton.edu/bitstream/xyz/0"
end
end
# rubocop:enable Layout/LineLength
18 changes: 3 additions & 15 deletions spec/models/solr_document_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,6 @@
doc = described_class.new({ id: "2", data_source_ssi: "pdc_describe" })
expect(doc.dates_created.first).to be nil
end
it "handles dataspace dates" do
doc = described_class.new({ id: "3", date_created_ssim: ["2024-10-30"], data_source_ssi: "dataspace" })
expect(doc.dates_created.first).to eq "2024-10-30"

doc = described_class.new({ id: "4", data_source_ssi: "dataspace" })
expect(doc.dates_created.first).to be nil
end
end

describe "#dates_modified" do
Expand All @@ -52,13 +45,6 @@
doc = described_class.new({ id: "4", pdc_updated_at_dtsi: "string", data_source_ssi: "pdc_describe" })
expect(doc.dates_modified.first).to be nil
end
it "handles dataspace dates" do
doc = described_class.new({ id: "4", date_modified_ssim: ["2024-10-30"], data_source_ssi: "dataspace" })
expect(doc.dates_modified.first).to eq "2024-10-30"

doc = described_class.new({ id: "6", data_source_ssi: "dataspace" })
expect(doc.dates_modified.first).to be nil
end
end

describe "#icons_css" do
Expand All @@ -83,7 +69,9 @@
end

it "calculates file counts and sorts data descending by count" do
files = [{ name: "file1.zip" }, { name: "data.csv" }, { name: "file2.zip" }]
files = [{ filename: "file1.zip", "size": 10_588, "display_size": "10.6 KB", "url": "https://example.com" },
{ filename: "data.csv", "size": 10_588, "display_size": "10.6 KB", "url": "https://example.com" },
{ filename: "file2.zip", "size": 10_588, "display_size": "10.6 KB", "url": "https://example.com" }]
doc = described_class.new({ id: "1", files_ss: files.to_json })
zip_group = { extension: "zip", file_count: 2 }
csv_group = { extension: "csv", file_count: 1 }
Expand Down

0 comments on commit 87f4fce

Please sign in to comment.