diff --git a/lib/active_fedora/delegating.rb b/lib/active_fedora/delegating.rb index 309400524..fc80ebacd 100644 --- a/lib/active_fedora/delegating.rb +++ b/lib/active_fedora/delegating.rb @@ -31,11 +31,11 @@ def delegate(field, args ={}) def create_delegate_accessor(field, args) define_method field do ds = self.send(args[:to]) - val = if ds.kind_of? ActiveFedora::NokogiriDatastream + val = if ds.kind_of? ActiveFedora::MetadataDatastream + ds.send(:get_values, field) + else terminology = args[:at] || [field] ds.send(:term_values, *terminology) - else - ds.send(:get_values, field) end args[:unique] ? val.first : val @@ -45,11 +45,11 @@ def create_delegate_accessor(field, args) def create_delegate_setter(field, args) define_method "#{field}=".to_sym do |v| ds = self.send(args[:to]) - if ds.kind_of? ActiveFedora::NokogiriDatastream + if ds.kind_of? ActiveFedora::MetadataDatastream + ds.send(:set_value, field, v) + else terminology = args[:at] || [field] ds.send(:update_indexed_attributes, {terminology => v}) - else - ds.send(:set_value, field, v) end end end diff --git a/lib/active_fedora/metadata_datastream.rb b/lib/active_fedora/metadata_datastream.rb index daae1ae4e..bc47279fe 100644 --- a/lib/active_fedora/metadata_datastream.rb +++ b/lib/active_fedora/metadata_datastream.rb @@ -30,6 +30,31 @@ def to_solr(solr_doc = Hash.new) # :nodoc: return solr_doc end + # ** EXPERIMENTAL ** + # + # This is utilized by ActiveFedora::Base.load_instance_from_solr to set + # metadata values in this object using the Solr document passed in. + # Any keys in the solr document that map to a metadata field key within a MetadataDatastream object + # are set to the corresponding value. Any others are ignored. ActiveFedora::SolrService.solr_name + # is used to map solr key to field key name. + # + # ====Warning + # Solr must be synchronized with data in Fedora. + def from_solr(solr_doc) + fields.each do |field_key, field_info| + field_symbol = ActiveFedora::SolrService.solr_name(field_key, field_info[:type]) + value = (solr_doc[field_symbol].nil? ? solr_doc[field_symbol.to_s]: solr_doc[field_symbol]) + unless value.nil? + if value.is_a? Array + update_attributes({field_key=>value}) + else + update_indexed_attributes({field_key=>{0=>value}}) + end + end + end + end + + def update_attributes(params={},opts={}) diff --git a/lib/active_fedora/metadata_datastream_helper.rb b/lib/active_fedora/metadata_datastream_helper.rb index ea8c91dba..4a0da476e 100644 --- a/lib/active_fedora/metadata_datastream_helper.rb +++ b/lib/active_fedora/metadata_datastream_helper.rb @@ -36,29 +36,4 @@ def serialize! # :nodoc: end end - # ** EXPERIMENTAL ** - # - # This is utilized by ActiveFedora::Base.load_instance_from_solr to set - # metadata values in this object using the Solr document passed in. - # Any keys in the solr document that map to a metadata field key within a MetadataDatastream object - # are set to the corresponding value. Any others are ignored. ActiveFedora::SolrService.solr_name - # is used to map solr key to field key name. - # - # ====Warning - # Solr must be synchronized with data in Fedora. - def from_solr(solr_doc) - fields.each do |field_key, field_info| - field_symbol = ActiveFedora::SolrService.solr_name(field_key, field_info[:type]) - value = (solr_doc[field_symbol].nil? ? solr_doc[field_symbol.to_s]: solr_doc[field_symbol]) - unless value.nil? - if value.is_a? Array - update_attributes({field_key=>value}) - else - update_indexed_attributes({field_key=>{0=>value}}) - end - end - end - end - - end