Skip to content

Work Type Inheritance

Adam Ploshay edited this page Dec 6, 2018 · 3 revisions

Archetypes

At IU we want to be able share behaviors of closely related things, which implies the need for specialties of common work types. Currently you can easily create new work types in Hyrax via a Rails generator, but ideally you should also be able to say it is 'a kind of' an existing work type, thus allowing a hierarchy of inheritance.

To support this, ESSI adds the concept of "archetypes". An archetype is a work type that has its specialty behaviors codified into mix-in modules that can be included into other work types. To be able to create a work type based on an archetype, the Hyrax generator was overridden to add the ability to pass an archetype name as an option and subsequently inject its behaviors into the work type's resulting classes. Also, the generator can create a new archetype and templates of its mix-in modules.

Usage:

You can create a new archetype with the --archetype flag with no value supplied. This creates the named work type and then asks if you want to promote it to an archetype:

rails g hyrax:work AnotherWork --archetype

If you supply a value to --archetype it creates a new work type and then injects the module mixins of the named archetype into the named work type:

rails g hyrax:work MoreWork --archetype AnotherWork

Of course, this is only useful if developers add useful behaviors to the resulting mix-in modules. For example, an archetype called Image might have additional metadata and presenter behaviors that are specific to providing a specialty user experience for images.

Archetype example

As discussed above, and archetype consists of specialty modules that can be mixed into new work types via the --archetype flag. This example breaks down those modules and also describes where they are mixed in to inheriting work types.

Currently ESSI has three functioning archetypes that are persisted in the code repo and would activate automatically when installing from scratch. One of those is the Image archetype, whose modules are described in more detail below:

app/controllers/concerns/essi/images_controller_behavior.rb

  • Adds custom controller behaviors that can be mixed in to work type controllers.
  • Gets mixed into app/controllers/hyrax/[image_work_type]_controller.rb

app/models/concerns/essi/image_behavior.rb

  • Adds custom model behaviors that can be mixed in to work type models.
  • Gets mixed into app/models/[image_work_type].rb

app/models/concerns/essi/image_metadata.rb

  • Adds custom metadata behaviors that can be mixed in to work type models.
  • Gets mixed into app/models/[image_work_type].rb

app/forms/concerns/essi/image_form_behavior.rb

  • Adds custom form behaviors that can be mixed in to work type form classes.
  • Gets mixed into app/forms/hyrax/[image_work_type]_form.rb

app/indexers/concerns/essi/image_indexer_behavior.rb

  • Adds custom indexing behaviors that can be mixed in to work type indexer classes.
  • Gets mixed into app/indexers/[image_work_type]_indexer.rb

When you run the generator with the --archetype option, you can see the files discussed above being manipulated via the Thor generator methods and commands:

$ rails g hyrax:work LillyImage --archetype Image
        info  GENERATING WORK MODEL: LillyImage
        (usual model creation, etc.)
        ...
        info  Behaviours of Image are being added to LillyImage
      insert  app/models/lilly_image.rb
      insert  app/models/lilly_image.rb
      insert  app/controllers/hyrax/lilly_images_controller.rb
        gsub  app/controllers/hyrax/lilly_images_controller.rb
      insert  app/indexers/lilly_image_indexer.rb
      insert  app/forms/hyrax/lilly_image_form.rb
        gsub  spec/features/create_lilly_image_spec.rb