Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Refactored StorageFile to be a Data.define

Andreas Pfohl709613962e7a
diff --git a/modules/storages/app/common/storages/peripherals/storage_file_info_converter.rb b/modules/storages/app/common/storages/peripherals/storage_file_info_converter.rb
index 78fc7da9ec4..4bff762dd45 100644
--- a/modules/storages/app/common/storages/peripherals/storage_file_info_converter.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_file_info_converter.rb
@@ -30,16 +30,16 @@ module Storages::Peripherals
module StorageFileInfoConverter
def to_storage_file(storage_file_info)
Storages::StorageFile.new(
- storage_file_info.id,
- storage_file_info.name,
- storage_file_info.size,
- storage_file_info.mime_type,
- storage_file_info.created_at,
- storage_file_info.last_modified_at,
- storage_file_info.owner_name,
- storage_file_info.last_modified_by_name,
- storage_file_info.location,
- storage_file_info.permissions
+ id: storage_file_info.id,
+ name: storage_file_info.name,
+ size: storage_file_info.size,
+ mime_type: storage_file_info.mime_type,
+ created_at: storage_file_info.created_at,
+ last_modified_at: storage_file_info.last_modified_at,
+ created_by_name: storage_file_info.owner_name,
+ last_modified_by_name: storage_file_info.last_modified_by_name,
+ location: storage_file_info.location,
+ permissions: storage_file_info.permissions
)
end
end
diff --git a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/files_query.rb b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/files_query.rb
index 1cfe2485cdd..e1b0f255be0 100644
--- a/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/files_query.rb
+++ b/modules/storages/app/common/storages/peripherals/storage_interaction/nextcloud/files_query.rb
@@ -124,16 +124,7 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
# The ancestors are simply derived objects from the parents location string. Until we have real information
# from the nextcloud API about the path to the parent, we need to derive name, location and forge an ID.
def forge_ancestor(location)
- ::Storages::StorageFile.new(Digest::SHA256.hexdigest(location),
- name(location),
- nil,
- nil,
- nil,
- nil,
- nil,
- nil,
- location,
- nil)
+ ::Storages::StorageFile.new(id: Digest::SHA256.hexdigest(location), name: name(location), location:)
end
def name(location)
@@ -144,16 +135,14 @@ module Storages::Peripherals::StorageInteraction::Nextcloud
location = location(file_element)
::Storages::StorageFile.new(
- id(file_element),
- name(location),
- size(file_element),
- mime_type(file_element),
- nil,
- last_modified_at(file_element),
- created_by(file_element),
- nil,
- location,
- permissions(file_element)
+ id: id(file_element),
+ name: name(location),
+ size: size(file_element),
+ mime_type: mime_type(file_element),
+ last_modified_at: last_modified_at(file_element),
+ created_by_name: created_by(file_element),
+ location:,
+ permissions: permissions(file_element)
)
end
diff --git a/modules/storages/app/models/storages/storage_file.rb b/modules/storages/app/models/storages/storage_file.rb
index c2d3edde008..59dc8fed6ce 100644
--- a/modules/storages/app/models/storages/storage_file.rb
+++ b/modules/storages/app/models/storages/storage_file.rb
@@ -26,21 +26,43 @@
# See COPYRIGHT and LICENSE files for more details.
#++
-class Storages::StorageFile
- attr_reader :id, :name, :size, :mime_type, :created_at, :last_modified_at, :created_by_name, :last_modified_by_name,
- :location, :permissions
-
- def initialize(id, name, size, mime_type, created_at, last_modified_at, created_by_name, last_modified_by_name,
- location, permissions)
- @id = id
- @name = name
- @size = size
- @mime_type = mime_type
- @created_at = created_at
- @last_modified_at = last_modified_at
- @created_by_name = created_by_name
- @last_modified_by_name = last_modified_by_name
- @location = location
- @permissions = permissions
+module Storages
+ StorageFile = Data.define(
+ :id,
+ :name,
+ :size,
+ :mime_type,
+ :created_at,
+ :last_modified_at,
+ :created_by_name,
+ :last_modified_by_name,
+ :location,
+ :permissions
+ ) do
+ def initialize(
+ id:,
+ name:,
+ size: nil,
+ mime_type: nil,
+ created_at: nil,
+ last_modified_at: nil,
+ created_by_name: nil,
+ last_modified_by_name: nil,
+ location: nil,
+ permissions: nil
+ )
+ super(
+ id:,
+ name:,
+ size:,
+ mime_type:,
+ created_at:,
+ last_modified_at:,
+ created_by_name:,
+ last_modified_by_name:,
+ location:,
+ permissions:
+ )
+ end
end
end
diff --git a/modules/storages/spec/lib/api/v3/storage_files/storage_file_representer_spec.rb b/modules/storages/spec/lib/api/v3/storage_files/storage_file_representer_spec.rb
index ed40ac1fdd4..ef6d0496724 100644
--- a/modules/storages/spec/lib/api/v3/storage_files/storage_file_representer_spec.rb
+++ b/modules/storages/spec/lib/api/v3/storage_files/storage_file_representer_spec.rb
@@ -35,16 +35,16 @@ RSpec.describe API::V3::StorageFiles::StorageFileRepresenter do
let(:storage) { build_stubbed(:storage) }
let(:file) do
Storages::StorageFile.new(
- 42,
- 'readme.md',
- 4096,
- 'text/plain',
- created_at,
- last_modified_at,
- 'admin',
- 'admin',
- '/readme.md',
- %i[readable writeable]
+ id: 42,
+ name: 'readme.md',
+ size: 4096,
+ mime_type: 'text/plain',
+ created_at:,
+ last_modified_at:,
+ created_by_name: 'admin',
+ last_modified_by_name: 'admin',
+ location: '/readme.md',
+ permissions: %i[readable writeable]
)
end
let(:representer) { described_class.new(file, storage, current_user: user) }
diff --git a/modules/storages/spec/lib/api/v3/storage_files/storage_files_representer_spec.rb b/modules/storages/spec/lib/api/v3/storage_files/storage_files_representer_spec.rb
index a30cbd48dda..07eded1df96 100644
--- a/modules/storages/spec/lib/api/v3/storage_files/storage_files_representer_spec.rb
+++ b/modules/storages/spec/lib/api/v3/storage_files/storage_files_representer_spec.rb
@@ -36,46 +36,46 @@ RSpec.describe API::V3::StorageFiles::StorageFilesRepresenter do
let(:parent) do
Storages::StorageFile.new(
- 23,
- 'Documents',
- 2048,
- 'application/x-op-directory',
- created_at,
- last_modified_at,
- 'admin',
- 'admin',
- '/Documents',
- %i[readable writeable]
+ id: 23,
+ name: 'Documents',
+ size: 2048,
+ mime_type: 'application/x-op-directory',
+ created_at:,
+ last_modified_at:,
+ created_by_name: 'admin',
+ last_modified_by_name: 'admin',
+ location: '/Documents',
+ permissions: %i[readable writeable]
)
end
let(:file) do
Storages::StorageFile.new(
- 42,
- 'readme.md',
- 4096,
- 'text/plain',
- created_at,
- last_modified_at,
- 'admin',
- 'admin',
- '/Documents/readme.md',
- %i[readable writeable]
+ id: 42,
+ name: 'readme.md',
+ size: 4096,
+ mime_type: 'text/plain',
+ created_at:,
+ last_modified_at:,
+ created_by_name: 'admin',
+ last_modified_by_name: 'admin',
+ location: '/Documents/readme.md',
+ permissions: %i[readable writeable]
)
end
let(:ancestor) do
Storages::StorageFile.new(
- 47,
- '/',
- 4096,
- 'application/x-op-directory',
- created_at,
- last_modified_at,
- 'admin',
- 'admin',
- '/',
- %i[readable writeable]
+ id: 47,
+ name: '/',
+ size: 4096,
+ mime_type: 'application/x-op-directory',
+ created_at:,
+ last_modified_at:,
+ created_by_name: 'admin',
+ last_modified_by_name: 'admin',
+ location: '/',
+ permissions: %i[readable writeable]
)
end
diff --git a/modules/storages/spec/requests/api/v3/storages/storage_files_spec.rb b/modules/storages/spec/requests/api/v3/storages/storage_files_spec.rb
index 502de4f257e..4f5012823a6 100644
--- a/modules/storages/spec/requests/api/v3/storages/storage_files_spec.rb
+++ b/modules/storages/spec/requests/api/v3/storages/storage_files_spec.rb
@@ -65,13 +65,43 @@ RSpec.describe 'API v3 storage files', content_type: :json, webmock: true do
let(:response) do
Storages::StorageFiles.new(
[
- Storages::StorageFile.new(1, 'new_younglings.md', 4096, 'text/markdown', DateTime.now, DateTime.now,
- 'Obi-Wan Kenobi', 'Obi-Wan Kenobi', '/', %i[readable]),
- Storages::StorageFile.new(2, 'holocron_inventory.md', 4096, 'text/markdown', DateTime.now, DateTime.now,
- 'Obi-Wan Kenobi', 'Obi-Wan Kenobi', '/', %i[readable writeable])
+ Storages::StorageFile.new(
+ id: 1,
+ name: 'new_younglings.md',
+ size: 4096,
+ mime_type: 'text/markdown',
+ created_at: DateTime.now,
+ last_modified_at: DateTime.now,
+ created_by_name: 'Obi-Wan Kenobi',
+ last_modified_by_name: 'Obi-Wan Kenobi',
+ location: '/',
+ permissions: %i[readable]
+ ),
+ Storages::StorageFile.new(
+ id: 2,
+ name: 'holocron_inventory.md',
+ size: 4096,
+ mime_type: 'text/markdown',
+ created_at: DateTime.now,
+ last_modified_at: DateTime.now,
+ created_by_name: 'Obi-Wan Kenobi',
+ last_modified_by_name: 'Obi-Wan Kenobi',
+ location: '/',
+ permissions: %i[readable writeable]
+ )
],
- Storages::StorageFile.new(32, '/', 4096 * 2, 'application/x-op-directory', DateTime.now, DateTime.now,
- 'Obi-Wan Kenobi', 'Obi-Wan Kenobi', '/', %i[readable writeable]),
+ Storages::StorageFile.new(
+ id: 32,
+ name: '/',
+ size: 4096 * 2,
+ mime_type: 'application/x-op-directory',
+ created_at: DateTime.now,
+ last_modified_at: DateTime.now,
+ created_by_name: 'Obi-Wan Kenobi',
+ last_modified_by_name: 'Obi-Wan Kenobi',
+ location: '/',
+ permissions: %i[readable writeable]
+ ),
[]
)
end