Skip to content

Commit

Permalink
nokogiri datastream integration
Browse files Browse the repository at this point in the history
  • Loading branch information
flyingzumwalt committed Jun 22, 2010
1 parent 0952d98 commit c15ff33
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 19 deletions.
6 changes: 6 additions & 0 deletions History.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
1.1.7

Nokogiri Datastream support
You now have the option of calling has_metadata without passing in a block


1.1.6

Bug #959: Base.delete fails to delete objects from Solr even when ENABLE_SOLR_UPDATES == true
Expand Down
5 changes: 4 additions & 1 deletion lib/active_fedora/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,10 @@ def configure_defined_datastreams
attributes = {:label=>""}
end
ds = ar.first.new(:dsid=>name)
ar.last.call(ds)
# If you called has_metadata with a block, pass the block into the Datastream class
if ar.last.class == Proc
ar.last.call(ds)
end
ds.attributes = attributes.merge(ds.attributes)
self.add_datastream(ds)
end
Expand Down
16 changes: 0 additions & 16 deletions lib/active_fedora/metadata_datastream_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,22 +65,6 @@ def to_xml(xml = Nokogiri::XML::Document.parse("<fields />")) #:nodoc:
end
end
end

# fields.each_pair do |field,field_info|
# el = REXML::Element.new("#{field.to_s}")
# if field_info[:element_attrs]
# field_info[:element_attrs].each{|k,v| el.add_attribute(k.to_s, v.to_s)}
# end
# field_info[:values].each do |val|
# el = el.clone
# el.text = val.to_s
# if xml.class == REXML::Document
# xml.root.elements.add(el)
# else
# xml.add(el)
# end
# end
# end
return builder.to_xml
end

Expand Down
22 changes: 22 additions & 0 deletions lib/active_fedora/nokogiri_datastream.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,28 @@ def initialize(attrs=nil)
self.class.from_xml(blob, self)
end

def to_xml(xml = self.ng_xml)
ng_xml = self.ng_xml
if ng_xml.root.nil? && self.class.respond_to?(:root_property_ref) && !self.class.root_property_ref.nil?
ng_xml = self.class.generate(self.class.root_property_ref, "")
if xml.root.nil?
xml = ng_xml
end
end

unless xml == ng_xml || ng_xml.root.nil?
if xml.kind_of?(Nokogiri::XML::Document)
xml.root.add_child(ng_xml.root)
elsif xml.kind_of?(Nokogiri::XML::Node)
xml.add_child(ng_xml.root)
else
raise "You can only pass instances of Nokogiri::XML::Node into this method. You passed in #{xml}"
end
end

return xml.to_xml {|config| config.no_declaration}
end

def to_solr(solr_doc = Solr::Document.new) # :nodoc:

unless self.class.accessors.nil?
Expand Down
4 changes: 2 additions & 2 deletions lib/hydra.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# module Hydra;end
require "hydra_libs/mods_datastream"
module Hydra;end
require "hydra/mods_article"
2 changes: 2 additions & 0 deletions lib/hydra_libs/mods_article.rb → lib/hydra/mods_article.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
module Hydra
class ModsArticle < ActiveFedora::NokogiriDatastream

# have to call this in order to set namespace & schema
Expand Down Expand Up @@ -44,4 +45,5 @@ class ModsArticle < ActiveFedora::NokogiriDatastream
]}}
]

end
end
File renamed without changes.
7 changes: 7 additions & 0 deletions spec/integration/full_featured_model_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require File.join( File.dirname(__FILE__), "../spec_helper" )

require 'rexml/document'
require "hydra"

include ActiveFedora

Expand Down Expand Up @@ -30,6 +31,12 @@ class OralHistory < ActiveFedora::Base
m.field "location", :string
end

# has_metadata :name=>"arbitrary_xml", :type=> ActiveFedora::NokogiriDatastream do |m|
# m.root_property :myxml, "my_xml", "http://www.example.gov/schema/v3"
# end

has_metadata :name=>"mods_article", :type=> Hydra::ModsArticle

has_metadata :name => "dublin_core", :type => ActiveFedora::QualifiedDublinCoreDatastream do |m|
# Default :multiple => true
#
Expand Down

0 comments on commit c15ff33

Please sign in to comment.