From 27f5408dc58af98e2085af464a0caeb1b2d7f51e Mon Sep 17 00:00:00 2001 From: Justin Coyne Date: Fri, 17 Nov 2023 14:17:50 -0600 Subject: [PATCH] Use a delegator for the hierarchical file The hierarchy feature requires an index property, that is not relevant to ResourceFile itself, so a delegator can keep this concern separate --- app/models/embed/hierarchical_contents.rb | 2 +- app/models/embed/hierarchical_file.rb | 7 +++++++ app/models/embed/purl/resource_file.rb | 3 --- spec/models/embed/hierarchical_contents_spec.rb | 2 +- 4 files changed, 9 insertions(+), 5 deletions(-) create mode 100644 app/models/embed/hierarchical_file.rb diff --git a/app/models/embed/hierarchical_contents.rb b/app/models/embed/hierarchical_contents.rb index f5171aef3..ad2dec38d 100644 --- a/app/models/embed/hierarchical_contents.rb +++ b/app/models/embed/hierarchical_contents.rb @@ -22,7 +22,7 @@ def contents attr_reader :resources def file_contents - resources.map(&:files).flatten + resources.map(&:files).flatten.map { |file| HierarchicalFile.new(file) } end def add_to_hierarchy(file, root_directory) diff --git a/app/models/embed/hierarchical_file.rb b/app/models/embed/hierarchical_file.rb new file mode 100644 index 000000000..a2b88ff29 --- /dev/null +++ b/app/models/embed/hierarchical_file.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +module Embed + class HierarchicalFile < SimpleDelegator + attr_accessor :index + end +end diff --git a/app/models/embed/purl/resource_file.rb b/app/models/embed/purl/resource_file.rb index 942f3615f..39d98c660 100644 --- a/app/models/embed/purl/resource_file.rb +++ b/app/models/embed/purl/resource_file.rb @@ -12,11 +12,8 @@ def initialize(druid, description, file, rights) @description = description @file = file @rights = rights - @index = nil end - attr_accessor :index - def label @description end diff --git a/spec/models/embed/hierarchical_contents_spec.rb b/spec/models/embed/hierarchical_contents_spec.rb index 0f5456988..0b4a12416 100644 --- a/spec/models/embed/hierarchical_contents_spec.rb +++ b/spec/models/embed/hierarchical_contents_spec.rb @@ -16,7 +16,7 @@ expect(root_dir.title).to eq '' expect(root_dir.files.count).to eq 1 file1 = root_dir.files.first - expect(file1).to be_an Embed::Purl::ResourceFile + expect(file1).to be_an Embed::HierarchicalFile expect(file1.title).to eq 'Title_of_the_PDF.pdf' expect(file1.index).to eq 1 expect(root_dir.dirs.count).to eq 1