Skip to content

Defining change set properties

David Chandek-Stark edited this page Sep 18, 2024 · 10 revisions

back to Introduction

Goals

  • Understand property definitions in ChangeSets.
  • Define a Change Set for a Book resource.
  • Define a Change Set for a Page resource.

Properties

  • defining a property with the same name will set the property with the value from the resource when the ChangeSet is created AND set the properties value in the matching attribute in the resource when the #sync method is called
  • defining a property with a name not matching an attribute will raise NoMethodError (see virtual option)
  • OPTION readable - when false, the value of this property will NOT be read into the twin on creation
  • OPTION writable - when false, the value of this property will NOT be written to resource on sync
  • OPTION virtual - when true, the value of this property will NOT be read into the twin on creation nor written to the resource on sync which is useful when constructing forms to gather complex information for use in coercing values of other properties or as part of the validations
  • OPTION required - when true, method required?(property_name) returns true which is useful when constructing forms to set a field to required and when running custom validations
  • OPTION multiple - when true, method multiple?(property_name) returns true which is useful when constructing forms to set a field to allow multiple values and when running custom validations
  • only values that have property definitions are allowed which is used while processing params returned from a form

Define Change Sets with Properties

Create the directory app/change_sets inside your project directory. Edit app/change_sets/book_change_set.rb to contain the following:

# frozen_string_literal: true
class BookChangeSet < Valkyrie::ChangeSet
  property :title, multiple: true, required: true
  property :author, multiple: true, required: true
  property :series, multiple: false, required: false
  property :member_ids, multiple: true, required: false
end

Edit app/change_sets/page_change_set.rb to contain the following:

# frozen_string_literal: true
class PageChangeSet < Valkyrie::ChangeSet
  property :page_num, multiple: false, required: true
  property :structure, multiple: false, required: false
  property :image, multiple: false, required: false
end

Previous | Next