Skip to content

Commit

Permalink
fix bugs, add db seed
Browse files Browse the repository at this point in the history
  • Loading branch information
orangewolf committed Jun 10, 2024
1 parent 7e06f18 commit 382c6d9
Show file tree
Hide file tree
Showing 11 changed files with 207 additions and 13 deletions.
162 changes: 162 additions & 0 deletions .dassie/config/metadata/m3_profile.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
---
m3_version: 1.0.beta2
profile:
date_modified: '2024-06-01'
responsibility: https://samvera.org
responsibility_statement: Hyrax Initial Profile
type:
version: 1
classes:
GenericWorkResource:
display_label: Generic Work
Monograph:
display_label: Monograph
AdminSet:
diplay_label: AdminSet
AdminSetResource:
diplay_label: AdministrativeSet
Collection:
display_label: Collection
CollectionResource:
display_label: PcdmCollection
Hyrax::FileSet:
display_label: FileSet
contexts:
flexible_context:
display_label: Flexible Metadata Example
mappings:
blacklight:
name: Additional Blacklight Solr Mappings
metatags:
name: Metatags
mods_oai_pmh:
name: MODS OAI PMH
qualified_dc_pmh:
name: Qualified DC OAI PMH
simple_dc_pmh:
name: Simple DC OAI PMH
properties:
title:
available_on:
class:
- AdminSetResource
- AdminSet
- Hyrax::FileSet
- CollectionResource
- GenericWorkResource
- Monograph
cardinality:
minimum: 0
multi_value: true
controlled_values:
format: http://www.w3.org/2001/XMLSchema#string
sources:
- 'null'
definition:
default: Enter a standardized title for display. If only one
title is needed, transcribe the title from the source
itself.
display_label:
default: Title
index_documentation: displayable, searchable
indexing:
- 'title_sim'
- 'title_tesim'
form:
required: true
primary: true
multi_value: true
mappings:
metatags: twitter:title, og:title
mods_oai_pmh: mods:titleInfo/mods:title
qualified_dc_pmh: dcterms:title
simple_dc_pmh: dc:title
property_uri: http://purl.org/dc/terms/title
range: http://www.w3.org/2001/XMLSchema#string
requirement: required
sample_values:
- Pencil drawn portrait study of woman
date_modified:
available_on:
class:
- AdminSetResource
- AdminSet
- Hyrax::FileSet
- CollectionResource
- GenericWorkResource
- Monograph
cardinality:
minimum: 0
maximum: 1
multi_value: false
display_label:
default: Date Modified
property_uri: http://purl.org/dc/terms/modified
range: http://www.w3.org/2001/XMLSchema#dateTime
sample_values:
- "2024-06-06 21:06:51 +0000"
date_uploaded:
available_on:
class:
- AdminSetResource
- AdminSet
- Hyrax::FileSet
- CollectionResource
- GenericWorkResource
- Monograph
cardinality:
minimum: 0
maximum: 1
multi_value: false
display_label:
default: Date Uploaded
property_uri: http://purl.org/dc/terms/dateSubmitted
range: http://www.w3.org/2001/XMLSchema#dateTime
sample_values:
- "2024-06-06 21:06:51 +0000"
depositor:
available_on:
class:
- AdminSetResource
- AdminSet
- Hyrax::FileSet
- CollectionResource
- GenericWorkResource
- Monograph
cardinality:
minimum: 0
maximum: 1
multi_value: false
controlled_values:
format: http://www.w3.org/2001/XMLSchema#string
sources:
- 'null'
display_label:
default: Depositor
index_documentation: searchable
indexing:
- 'depositor_tesim'
property_uri: http://id.loc.gov/vocabulary/relators/dpt
range: http://www.w3.org/2001/XMLSchema#string
sample_values:
- Julie Allinson
creator:
available_on:
class:
- Hyrax::FileSet
cardinality:
minimum: 1
multi_value: true
controlled_values:
format: http://www.w3.org/2001/XMLSchema#string
sources:
- 'null'
display_label:
default: Creator
index_documentation: searchable
indexing:
- 'creator_tesim'
property_uri: http://purl.org/dc/elements/1.1/creator
range: http://www.w3.org/2001/XMLSchema#string
sample_values:
- Julie Allinson
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
class CreateHyraxFlexibleSchemas < ActiveRecord::Migration[6.1]
def change
create_table :hyrax_flexible_schemas do |t|
t.string :version, index: { unique: true }
t.text :profile

t.timestamps
Expand Down
1 change: 0 additions & 1 deletion .dassie/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,6 @@
end

create_table "hyrax_flexible_schemas", force: :cascade do |t|
t.string "version"
t.text "profile"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
Expand Down
2 changes: 1 addition & 1 deletion app/models/concerns/hyrax/flexibility.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def new(attributes = default_attributes, safe = false, &block) # rubocop:disable

## Read the schema from the database and load the correct schemas for the instance in to the class
def load(attributes, safe = false)
attributes[:schema_version] ||= Hyrax::FlexibleSchema.order('created_at DESC').pick(:version)
attributes[:schema_version] ||= Hyrax::FlexibleSchema.order('id DESC').pick(:id)
struct = allocate
schema_version = attributes[:schema_version]
struct.singleton_class.attributes(Hyrax::Schema(self, schema_version:).attributes)
Expand Down
8 changes: 6 additions & 2 deletions app/models/hyrax/flexible_schema.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
class Hyrax::FlexibleSchema < ApplicationRecord
serialize :profile, coder: YAML

def title
"#{profile['profile']['responsibility_statement']} - version #{id}"
end

def attributes_for(class_name)
class_names[class_name]
end
Expand All @@ -22,8 +26,8 @@ def class_names
end

def values_map(values)
values['type'] = lookup_type(value['range'])
values['predicate'] = value['property_uri']
values['type'] = lookup_type(values['range'])
values['predicate'] = values['property_uri']
values['index_keys'] = values['indexing']
values['multiple'] = values['multi_value']
values
Expand Down
4 changes: 3 additions & 1 deletion app/services/hyrax/m3_schema_loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ class M3SchemaLoader < Hyrax::SchemaLoader
# @param [#to_s] schema_name
# @return [Enumerable<AttributeDefinition]
def definitions(schema_name, version)
Hyrax::FlexibleSchema.find_by(version: version).attributes_for(schema_name).map do |name, config|
Hyrax::FlexibleSchema.find(version).attributes_for(schema_name).map do |name, config|
AttributeDefinition.new(name, config)
end
rescue NoMethodError
raise UndefinedSchemaError, "Flexible schema not found in version #{version} for #{schema_name}"
end
end
end
8 changes: 5 additions & 3 deletions app/services/hyrax/schema_loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ module Hyrax
#
# @see config/metadata/basic_metadata.yaml for an example configuration
class SchemaLoader
class UndefinedSchemaError < ArgumentError; end

##
# @param [Symbol] schema
#
# @return [Hash<Symbol, Dry::Types::Type>] a map from attribute names to
# types
def attributes_for(schema:, version: 1)
definitions(schema).each_with_object({}) do |definition, hash|
definitions(schema, version).each_with_object({}) do |definition, hash|
hash[definition.name] = definition.type.meta(definition.config)
end
end
Expand All @@ -24,7 +26,7 @@ def attributes_for(schema:, version: 1)
#
# @return [Hash{Symbol => Hash{Symbol => Object}}]
def form_definitions_for(schema:, version: 1)
definitions(schema).each_with_object({}) do |definition, hash|
definitions(schema, version).each_with_object({}) do |definition, hash|
next if definition.form_options.empty?

hash[definition.name] = definition.form_options
Expand All @@ -36,7 +38,7 @@ def form_definitions_for(schema:, version: 1)
#
# @return [{Symbol => Symbol}] a map from index keys to attribute names
def index_rules_for(schema:, version: 1)
definitions(schema).each_with_object({}) do |definition, hash|
definitions(schema, version).each_with_object({}) do |definition, hash|
definition.index_keys.each do |key|
hash[key] = definition.name
end
Expand Down
4 changes: 1 addition & 3 deletions app/services/hyrax/simple_schema_loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,12 @@ def permissive_schema_for_valkrie_adapter
end
end

class UndefinedSchemaError < ArgumentError; end

private

##
# @param [#to_s] schema_name
# @return [Enumerable<AttributeDefinition]
def definitions(schema_name)
def definitions(schema_name, _version)
schema_config(schema_name)['attributes'].map do |name, config|
AttributeDefinition.new(name, config)
end
Expand Down
1 change: 1 addition & 0 deletions app/utils/hyrax/required_data_seeder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ def initialize(logger: Logger.new(STDOUT))
end

def generate_seed_data
Hyrax::RequiredDataSeeders::FlexibleProfileSeeder.generate_seeds(logger: logger) if ENV.fetch('HYRAX_FLEXIBLE', false)
Hyrax::RequiredDataSeeders::CollectionTypeSeeder.generate_seeds(logger: logger)
Hyrax::RequiredDataSeeders::CollectionSeeder.generate_seeds(logger: logger)
end
Expand Down
28 changes: 28 additions & 0 deletions app/utils/hyrax/required_data_seeders/flexible_profile_seeder.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# frozen_string_literal: true

module Hyrax
module RequiredDataSeeders
# This class was created for use in rake tasks and db/seeds.rb. It generates
# collections that are required for all Hyrax applications.
#
# Seeders of required data are non-destructive. If the data already exists,
# it will not be replaced.
class FlexibleProfileSeeder
class << self
attr_accessor :logger

def generate_seeds(logger: Logger.new(STDOUT))
@logger = logger

logger.info("Adding required collections...")

flexible_schema = Hyrax::FlexibleSchema.first_or_create do |f|
f.profile = YAML.safe_load_file(Rails.root.join('config', 'metadata', 'm3_profile.yaml'))
end

logger.info " #{flexible_schema.title} -- FOUND OR CREATED"
end
end
end
end
end
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
class CreateHyraxFlexibleSchemas < ActiveRecord::Migration<%= migration_version %>
def change
create_table :hyrax_flexible_schemas do |t|
t.string :version, index: { unique: true }
t.text :profile

t.timestamps
Expand Down

0 comments on commit 382c6d9

Please sign in to comment.