Skip to content

Instantly share code, notes, and snippets.

@hading
Last active December 16, 2015 08:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hading/5406113 to your computer and use it in GitHub Desktop.
Save hading/5406113 to your computer and use it in GitHub Desktop.
#It is possible to change the class in the Rails ext.
#As a preface, note that ActiveFedora lets you do ActiveFedora::Base::AnySubclass.find('any:pid')
#This will work even if the fedora rels-ext doesn't assert the correct model. It's also essential
#for changing the class.
#Note that all of this is quite fiddly and it's likely best to avoid it if possible. I've tried some things that
#seem spiritually equivalent to the below and not had them work.
#create an ActiveFedora::Base object and verify that there are no Medusa::Set objects
1.9.3p194 :011 > x = ActiveFedora::Base.create(:pid => 'my:pid')
=> #<ActiveFedora::Base pid:"my:pid", >
1.9.3p194 :012 > x.rels_ext.content
=> "\n<rdf:RDF xmlns:ns0=\"info:fedora/fedora-system:def/model#\" xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">\n <rdf:Description rdf:about=\"info:fedora/my:pid\">\n <ns0:hasModel rdf:resource=\"info:fedora/afmodel:ActiveFedora_Base\"></ns0:hasModel>\n </rdf:Description>\n</rdf:RDF>\n"
1.9.3p194 :013 > Medusa::Set.find(:all)
=> []
#replace the model in the rels ext (in an unclean way, but could be done cleaner) and save
#note that we seem to need a whole new string for the model to realize it is dirty, hence gsub and not gsub!
#At the end of this we still don't see any Medusa::Set objects
1.9.3p194 :014 > c = x.rels_ext.content.gsub('ActiveFedora_Base', 'Medusa_Set')
=> "\n<rdf:RDF xmlns:ns0=\"info:fedora/fedora-system:def/model#\" xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">\n <rdf:Description rdf:about=\"info:fedora/my:pid\">\n <ns0:hasModel rdf:resource=\"info:fedora/afmodel:Medusa_Set\"></ns0:hasModel>\n </rdf:Description>\n</rdf:RDF>\n"
1.9.3p194 :015 > x.rels_ext.content = c
=> "\n<rdf:RDF xmlns:ns0=\"info:fedora/fedora-system:def/model#\" xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">\n <rdf:Description rdf:about=\"info:fedora/my:pid\">\n <ns0:hasModel rdf:resource=\"info:fedora/afmodel:Medusa_Set\"></ns0:hasModel>\n </rdf:Description>\n</rdf:RDF>\n"
1.9.3p194 :016 > x.save
=> true
1.9.3p194 :017 > Medusa::Set.find(:all)
=> []
#okay, let's try reindexing
onseHeader"=>{"status"=>0, "QTime"=>230}}
1.9.3p194 :019 > Medusa::Set.find(:all)
=> []
#Nope. What we actually have to do is load this up as a Medusa::Set and reindex that.
#You can see here that the rels_ext actually was saved with the new data (we could have seen that above as well),
#but ActiveFedora/Solr couldn't do anything with it until after we did the reindex in this way.
#Then we were able to find it through ActiveFedora. Though it's not demonstrated here,
#it's at this point that it will also disappear from being findable through the old class's
#find(:all)
1.9.3p194 :020 > y = Medusa::Set.find('my:pid')
=> #<Medusa::Set pid:"my:pid", >
1.9.3p194 :021 > y.rels_ext.content
=> "\n<rdf:RDF xmlns:ns0=\"info:fedora/fedora-system:def/model#\" xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">\n <rdf:Description rdf:about=\"info:fedora/my:pid\">\n <ns0:hasModel rdf:resource=\"info:fedora/afmodel:Medusa_Set\"></ns0:hasModel>\n </rdf:Description>\n</rdf:RDF>\n"
1.9.3p194 :022 > y.update_index
=> {"responseHeader"=>{"status"=>0, "QTime"=>242}}
1.9.3p194 :023 > Medusa::Set.find(:all)
=> [#<Medusa::Set pid:"my:pid", >]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment