Skip to content

Playground: Creating Valkyrie Objects

edejesusyale edited this page Sep 26, 2019 · 11 revisions

Creating a Valkyrie Model

Work Types Generator

To create a custom Valkyrie model in your application, you can use the Rails generator. For example, to generate a model named FooBar with an unordered title field and an ordered member_ids field:

rails generate valkyrie:resource FooBar title member_ids:array

You can namespace your model class by including a slash in the model name:

rails generate valkyrie:resource Foo/Bar title member_ids:array

Then generate a decorator for your model. rails generate decorator <NameOfModel>

Move <file_name_decorator> from app/models/decorators to app/models

If you recieve an error message attempting to decorate an object check config/initializers/valkyrie.rb for connection = RSolr.connect :url => 'http://127.0.0.1:8983/solr/< Core_Name >'

Persisting a Valkyrie Object

Persist an Object to one backend

pg_adapter = Valkyrie::MetadataAdapter.find(:postgres) #could be a solr adapter if :solr is used instead of :postgres
book = Book.new # => #<Book:0x007fe383479988 @id=nil, @title=[], @member_ids=[]>
book = pg_adapter.persister.save(resource: book)

Persist an object to multiple backends

pg_adapter = Valkyrie::MetadataAdapter.find(:postgres)
solr_adapter = Valkyrie::MetadataAdapter.find(:solr)
composite_persister = Valkyrie::Persistence::CompositePersister.new(pg_adapter.persister, solr_adapter.persister)
book = Book.new
composite_persister.save(resource: book)