Skip to content

Playground: Dirty Tracking and Change Sets

edejesusyale edited this page Sep 26, 2019 · 15 revisions

Info

Dirty Tracking avoids unwanted changes when tracking modifications of objects. Changesets allow the consolidation of object changes across many sources to ensure these changes are valid.

Adding the ChangeSet persister

Create the directory app/change_set_persisters Add the file change_set_persister.rb to that directory.

  • This allows any changes we make to objects to be saved

Add ChangeSet

under app/models create the file map_change_set.rb Inside that file add the following:

class MapChangeSet < Valkyrie::ChangeSet
  self.fields = [:title]
  self.fields = [:member_ids]
  validates :title, presence: true
  validates :member_ids, presence: true
end

Mandatory Reset for Spring

In the terminal at the project directory

spring stop
spring binstub --all
spring start

This is so the ChangeSetPersister can be used in rails c

Testing

In rails c

map_object = Map.new(title: "Alen")
mychangeset = MapChangeSet.new(map_object)
mychangeset.valid? #false, since map_object does not have member_ids

map_object.member_ids = [12,11] #now give map_object member_ids

mychangeset2 = MapChangeSet.new(map_object)
mychangeset2.valid? #true, since both the title and member_ids fields are valid

mychangeset.changed? #false, the changeset is only aware if itself has been changed.
mychangeset.validate(title: "Alen redux") # => false
mychangeset.changed #{title: => true}

map_object.title # ["Alen"]

mychangeset.sync #applies changes from map_object to the change set
map_object.title# ["Alen redux"]

Persisting

If you want to see the changes in Solr/Blacklight go to config/valkyrie.yml Under the development stanza, change the metadata_adapter to solr

change_set_persister = ChangeSetPersister.new(
  metadata_adapter: Valkyrie.config.metadata_adapter,
  storage_adapter: Valkyrie.config.storage_adapter
)
updated_resource = change_set_persister.save(change_set: mychangeset)