Sha256: 5bdc2229a73ae94d423666419422f561af0d37c8348c2a51d2c5ae6e0167c3ff

Contents?: true

Size: 1.4 KB

Versions: 12

Compression:

Stored size: 1.4 KB

Contents

module ActiveFedora
  module Associations
    class BelongsToAssociation < AssociationProxy #:nodoc:
      def replace(record)
        if record.nil?
          ### TODO a more efficient way of doing this would be to write a clear_relationship method
          old_record = find_target
          @owner.remove_relationship(@reflection.options[:property], old_record) unless old_record.nil?
        else
          raise_on_type_mismatch(record)

          ### TODO a more efficient way of doing this would be to write a clear_relationship method
          old_record = find_target
          @owner.remove_relationship(@reflection.options[:property], old_record) unless old_record.nil?

          @target = (AssociationProxy === record ? record.target : record)
          @owner.add_relationship(@reflection.options[:property], record) unless record.new_record?
          @updated = true
        end

        loaded
        record
      end

      private
        def find_target
          pid = @owner.ids_for_outbound(@reflection.options[:property]).first
          return if pid.nil?
          query = ActiveFedora::SolrService.construct_query_for_pids([pid])
          solr_result = SolrService.instance.conn.query(query)
          return ActiveFedora::SolrService.reify_solr_results(solr_result).first
        end

        def foreign_key_present
          !@owner.send(@reflection.primary_key_name).nil?
        end

    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
active-fedora-4.0.0.rc1 lib/active_fedora/associations/belongs_to_association.rb
active-fedora-3.3.2 lib/active_fedora/associations/belongs_to_association.rb
active-fedora-3.3.1 lib/active_fedora/associations/belongs_to_association.rb
active-fedora-3.3.0 lib/active_fedora/associations/belongs_to_association.rb
active-fedora-3.2.2 lib/active_fedora/associations/belongs_to_association.rb
active-fedora-3.2.0 lib/active_fedora/associations/belongs_to_association.rb
active-fedora-3.2.0.pre7 lib/active_fedora/associations/belongs_to_association.rb
active-fedora-3.2.0.pre6 lib/active_fedora/associations/belongs_to_association.rb
active-fedora-3.2.0.pre5 lib/active_fedora/associations/belongs_to_association.rb
active-fedora-3.2.0.pre4 lib/active_fedora/associations/belongs_to_association.rb
active-fedora-3.2.0.pre3 lib/active_fedora/associations/belongs_to_association.rb
active-fedora-3.2.0.pre2 lib/active_fedora/associations/belongs_to_association.rb