-
Notifications
You must be signed in to change notification settings - Fork 1
Playground: Dirty Tracking and Change Sets
edejesusyale edited this page Sep 26, 2019
·
15 revisions
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.
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
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
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
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"]
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)