Skip to content

Commit

Permalink
Moving more stuff to Valkyrie
Browse files Browse the repository at this point in the history
  • Loading branch information
jcoyne committed Oct 9, 2017
1 parent f56dd50 commit c85a2e1
Show file tree
Hide file tree
Showing 18 changed files with 187 additions and 87 deletions.
34 changes: 17 additions & 17 deletions app/models/admin_set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@
# @see Hyrax::DefaultAdminSetActor
# @see Hyrax::ApplyPermissionTemplateActor
class AdminSet < Valkyrie::Resource
#include Hydra::AccessControls::WithAccessRight
# include Hydra::AccessControls::WithAccessRight
include Hyrax::Noid
include Hyrax::HumanReadableType
#include Hyrax::HasRepresentative
# include Hyrax::HasRepresentative

DEFAULT_ID = 'admin_set/default'.freeze
DEFAULT_TITLE = ['Default Admin Set'].freeze
DEFAULT_WORKFLOW_NAME = Hyrax.config.default_active_workflow_name

#validates_with Hyrax::HasOneTitleValidator
# validates_with Hyrax::HasOneTitleValidator
class_attribute :human_readable_short_description
#self.indexer = Hyrax::AdminSetIndexer
# self.indexer = Hyrax::AdminSetIndexer
attribute :id, Valkyrie::Types::ID.optional
attribute :title, Valkyrie::Types::Set
attribute :description, Valkyrie::Types::Set
Expand All @@ -48,8 +48,8 @@ class AdminSet < Valkyrie::Resource
# predicate: Hyrax.config.admin_set_predicate,
# class_name: 'ActiveFedora::Base'

#before_destroy :check_if_not_default_set, :check_if_empty
#after_destroy :destroy_permission_template
# before_destroy :check_if_not_default_set, :check_if_empty
# after_destroy :destroy_permission_template

def self.default_set?(id)
id == DEFAULT_ID
Expand Down Expand Up @@ -102,15 +102,15 @@ def destroy_permission_template
true
end

# def check_if_empty
# return true if members.empty?
# errors[:base] << I18n.t('hyrax.admin.admin_sets.delete.error_not_empty')
# throw :abort
# end
#
# def check_if_not_default_set
# return true unless default_set?
# errors[:base] << I18n.t('hyrax.admin.admin_sets.delete.error_default_set')
# throw :abort
# end
# def check_if_empty
# return true if members.empty?
# errors[:base] << I18n.t('hyrax.admin.admin_sets.delete.error_not_empty')
# throw :abort
# end
#
# def check_if_not_default_set
# return true unless default_set?
# errors[:base] << I18n.t('hyrax.admin.admin_sets.delete.error_default_set')
# throw :abort
# end
end
2 changes: 1 addition & 1 deletion app/models/batch_upload_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# It should never actually be persisted in the repository.
# The properties on this form should be copied to a real work type.
class BatchUploadItem < Valkyrie::Resource
#include Hyrax::WorkBehavior
# include Hyrax::WorkBehavior
# This must come after the WorkBehavior because it finalizes the metadata
# schema (by adding accepts_nested_attributes)
include ::Hyrax::BasicMetadata
Expand Down
1 change: 0 additions & 1 deletion app/models/concerns/hyrax/basic_metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ module BasicMetadata
extend ActiveSupport::Concern

included do

attribute :label, Valkyrie::Types::SingleValuedString
# property :label, predicate: ActiveFedora::RDF::Fcrepo::Model.downloadFilename, multiple: false

Expand Down
6 changes: 3 additions & 3 deletions app/models/concerns/hyrax/collection_behavior.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ module CollectionBehavior
include Hyrax::Noid
include Hyrax::HumanReadableType
# include Hyrax::HasRepresentative
# include Hyrax::Permissions
include Hyrax::Permissions

included do
# validates_with HasOneTitleValidator
# self.indexer = Hyrax::CollectionIndexer
# validates_with HasOneTitleValidator
# self.indexer = Hyrax::CollectionIndexer
end

# Add members using the members association.
Expand Down
2 changes: 1 addition & 1 deletion app/models/concerns/hyrax/in_admin_set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module InAdminSet

included do
attribute :admin_set_id, Valkyrie::Types::Set
#belongs_to :admin_set, predicate: Hyrax.config.admin_set_predicate
# belongs_to :admin_set, predicate: Hyrax.config.admin_set_predicate
end

def active_workflow
Expand Down
2 changes: 1 addition & 1 deletion app/models/concerns/hyrax/works/metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Metadata

included do
attribute :arkivo_checksum, Valkyrie::Types::String
#property :arkivo_checksum, predicate: ::RDF::URI.new('http://scholarsphere.psu.edu/ns#arkivoChecksum'), multiple: false
# property :arkivo_checksum, predicate: ::RDF::URI.new('http://scholarsphere.psu.edu/ns#arkivoChecksum'), multiple: false
end
end
end
34 changes: 34 additions & 0 deletions app/models/hyrax/file_node.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# frozen_string_literal: true

module Hyrax
class FileNode < Valkyrie::Resource
include Valkyrie::Resource::AccessControls
attribute :id, Valkyrie::Types::ID.optional
attribute :label, Valkyrie::Types::Set
attribute :mime_type, Valkyrie::Types::Set
attribute :height, Valkyrie::Types::Set
attribute :width, Valkyrie::Types::Set
attribute :checksum, Valkyrie::Types::Set
attribute :size, Valkyrie::Types::Set
attribute :original_filename, Valkyrie::Types::Set
attribute :file_identifiers, Valkyrie::Types::Set
attribute :use, Valkyrie::Types::Set

def self.for(file:)
new(label: file.original_filename, original_filename: file.original_filename, mime_type: file.content_type, use: file.try(:use) || [Valkyrie::Vocab::PCDMUse.OriginalFile])
end

def title
label
end

def download_id
id
end

def valid?
file = Valkyrie::StorageAdapter.find_by(id: file_identifiers.first)
file.valid?(size: size.first, digests: { sha256: checksum.first })
end
end
end
5 changes: 2 additions & 3 deletions app/persisters/hyrax/indexing_adapter.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# frozen_string_literal: true

# This was copied out of the Valkyrie app and I don't know why it isn't part of
# the valkyrie gem. -- Justin
##
Expand Down Expand Up @@ -61,9 +62,7 @@ def delete(resource:)
composite_persister.delete(resource: resource)
end

def wipe!
composite_persister.wipe!
end
delegate :wipe!, to: :composite_persister

# Yields the primary persister. At the end of the block, this will use changes tracked
# by an in-memory persister to replicate new and deleted objects into the
Expand Down
25 changes: 0 additions & 25 deletions app/persisters/hyrax/persister.rb

This file was deleted.

53 changes: 53 additions & 0 deletions app/services/hyrax/file_appender.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# frozen_string_literal: true

module Hyrax
class FileAppender
attr_reader :storage_adapter, :persister, :files
def initialize(storage_adapter:, persister:, files:)
@storage_adapter = storage_adapter
@persister = persister
@files = files
end

def append_to(resource)
return resource if files.blank?
file_sets = build_file_sets || file_nodes
resource.member_ids = resource.member_ids + file_sets.map(&:id)
resource
end

def build_file_sets
return if processing_derivatives?
file_nodes.map do |node|
file_set = create_file_set(node)
Valkyrie::DerivativeService.for(FileSetChangeSet.new(file_set)).create_derivatives if node.use.include?(Valkyrie::Vocab::PCDMUse.OriginalFile)
file_set
end
end

def processing_derivatives?
!file_nodes.first.use.include?(Valkyrie::Vocab::PCDMUse.OriginalFile)
end

def file_nodes
@file_nodes ||=
begin
files.map do |file|
create_node(file)
end
end
end

def create_node(file)
node = persister.save(resource: FileNode.for(file: file))
stored_file = storage_adapter.upload(file: file, resource: node)
node.file_identifiers = node.file_identifiers + [stored_file.id]
node = Valkyrie::FileCharacterizationService.for(file_node: node, persister: persister).characterize(save: false)
persister.save(resource: node)
end

def create_file_set(file_node)
persister.save(resource: ::FileSet.new(title: file_node.original_filename, member_ids: file_node.id))
end
end
end
23 changes: 23 additions & 0 deletions app/services/hyrax/queries.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

module Hyrax
class Queries
class_attribute :metadata_adapter
self.metadata_adapter = Valkyrie.config.metadata_adapter
class << self
delegate :find_all, :find_by, :find_members, :find_inverse_references_by, to: :default_adapter

def default_adapter
new(metadata_adapter: metadata_adapter)
end
end

attr_reader :metadata_adapter
delegate :find_all, :find_by, :find_members, :find_inverse_references_by, to: :metadata_adapter_query_service
def initialize(metadata_adapter:)
@metadata_adapter = metadata_adapter
end

delegate :query_service, to: :metadata_adapter, prefix: true
end
end
5 changes: 4 additions & 1 deletion config/initializers/valkyrie.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# frozen_string_literal: true

require 'valkyrie'

# TODO move to the host app?
# TODO: move to the host app?
# rubocop:disable Metrics/BlockLength
Rails.application.config.to_prepare do
Valkyrie::MetadataAdapter.register(
Valkyrie::Persistence::Postgres::MetadataAdapter.new,
Expand Down Expand Up @@ -59,3 +61,4 @@

# Valkyrie::FileCharacterizationService.services << TikaFileCharacterizationService
end
# rubocop:enable Metrics/BlockLength
4 changes: 4 additions & 0 deletions spec/factories/collections_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
work.apply_depositor_metadata(evaluator.user.user_key)
end

to_create do |instance|
Valkyrie.config.metadata_adapter.persister.save(resource: instance)
end

factory :public_collection, traits: [:public]

trait :public do
Expand Down
4 changes: 4 additions & 0 deletions spec/factories/file_sets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
end
end

to_create do |instance|
Valkyrie.config.metadata_adapter.persister.save(resource: instance)
end

trait :public do
read_groups ["public"]
end
Expand Down
17 changes: 11 additions & 6 deletions spec/models/checksum_audit_log_spec.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
include ActionDispatch::TestProcess

RSpec.describe ChecksumAuditLog do
let(:files) { [fixture_file_upload('world.png', 'image/png')] }
let(:persister) { Valkyrie.config.metadata_adapter.persister }
let(:storage_adapter) { Valkyrie.config.storage_adapter }

let(:file_node) {}
let(:f) do
file = FileSet.new do |gf|
gf.apply_depositor_metadata('mjg36')
file = FileSet.new do |fs|
fs.apply_depositor_metadata('mjg36')
fs.member_ids = [file_node.id]
end
Hyrax::Persister.save(resource: file)
# TODO: Mock addition of file to fileset to avoid calls to .save.
# This will speed up tests and avoid uneccesary integration testing for fedora funcationality.
Hydra::Works::AddFileToFileSet.call(file, File.open(fixture_path + '/world.png'), :original_file)
persister.save(resource: file)
file
end

Expand Down
5 changes: 3 additions & 2 deletions spec/models/collection_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
RSpec.describe Collection do
let(:collection) { build(:public_collection) }
let(:persister) { Valkyrie.config.metadata_adapter.persister }

it "has open visibility" do
expect(collection.read_groups).to eq ['public']
Expand Down Expand Up @@ -75,11 +76,11 @@

describe "Collection by another name" do
before do
class OtherCollection < ActiveFedora::Base
class OtherCollection < Valkyrie::Resource
include Hyrax::CollectionBehavior
end

class Member < ActiveFedora::Base
class Member < Valkyrie::Resource
include Hydra::Works::WorkBehavior
end
collection.add_member_objects member.id
Expand Down
Loading

0 comments on commit c85a2e1

Please sign in to comment.