Sha256: 7245c854dcb47b4fb8c2dc6d527346c408adaf16cbc27631d23401ae3622b92f

Contents?: true

Size: 1.77 KB

Versions: 11

Compression:

Stored size: 1.77 KB

Contents

module ActiveFedora
  module Associations
    class BelongsToAssociation < SingularAssociation #:nodoc:
      def replace(record)
        if record
          raise_on_type_mismatch(record)
          run_type_validator(record)
          # update_counters(record)
          replace_keys(record)
          set_inverse_instance(record)
          @updated = true
        else
          # decrement_counters
          remove_keys
        end

        self.target = record
      end

      def reset
        super
        @updated = false
      end

      def updated?
        @updated
      end

      private

        def replace_keys(record)
          owner[reflection.foreign_key] = record.id
        end

        def remove_keys
          owner[reflection.foreign_key] = nil
        end

        # Constructs a query that checks solr for the correct id & class_name combination
        def construct_query(ids)
          # Some descendants of ActiveFedora::Base are anonymous classes. They don't have names. Filter them out.
          candidate_classes = klass.descendants.select(&:name)
          candidate_classes += [klass] unless klass == ActiveFedora::Base
          model_pairs = candidate_classes.each_with_object([]) { |klass, arr| arr << [:has_model, klass.to_class_uri] }
          '(' + ActiveFedora::SolrQueryBuilder.construct_query_for_ids(ids) + ') AND (' +
            ActiveFedora::SolrQueryBuilder.construct_query_for_rel(model_pairs, 'OR') + ')'
        end

        def foreign_key_present?
          owner[reflection.foreign_key]
        end

        # belongs_to is not invertible (unless we implement has_one, then make an exception here)
        def invertible_for?(_)
          false
        end

        def stale_state
          owner[reflection.foreign_key]
        end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
active-fedora-9.7.3 lib/active_fedora/associations/belongs_to_association.rb
active-fedora-9.7.2 lib/active_fedora/associations/belongs_to_association.rb
active-fedora-9.9.1 lib/active_fedora/associations/belongs_to_association.rb
active-fedora-9.9.0 lib/active_fedora/associations/belongs_to_association.rb
active-fedora-9.8.2 lib/active_fedora/associations/belongs_to_association.rb
active-fedora-9.8.1 lib/active_fedora/associations/belongs_to_association.rb
active-fedora-9.8.0 lib/active_fedora/associations/belongs_to_association.rb
active-fedora-9.7.1 lib/active_fedora/associations/belongs_to_association.rb
active-fedora-9.7.0 lib/active_fedora/associations/belongs_to_association.rb
active-fedora-9.6.2 lib/active_fedora/associations/belongs_to_association.rb
active-fedora-9.6.1 lib/active_fedora/associations/belongs_to_association.rb