Sha256: 2738e15304d27399ba981df44d4790d3017a233f98738b0dcc51ac72c590cfb8

Contents?: true

Size: 1.54 KB

Versions: 11

Compression:

Stored size: 1.54 KB

Contents

# frozen_string_literal: true

require 'active_model/conversion'

module Blacklight::Document
  module ActiveModelShim
    extend ActiveSupport::Concern

    include ::ActiveModel::Conversion

    class_methods do
      # This is actually an ActiveRecord method starting in Rails 5.2
      def polymorphic_name
        base_class.name
      end

      def primary_key
        unique_key
      end

      def base_class
        self
      end

      def repository
        Blacklight.default_index
      end

      def find id
        repository.find(id).documents.first
      end

      # In Rails 7.1+, needs this method
      def composite_primary_key?
        false
      end

      # In Rails 7.1+, needs this method
      def has_query_constraints?
        false
      end
    end

    ##
    # Unique ID for the document
    def id
      self[self.class.unique_key]
    end

    def ==(other)
      super ||
        (other.instance_of?(self.class) &&
          id &&
          other.id == id)
    end

    def _read_attribute(attr)
      self[attr]
    end

    ##
    # ActiveRecord::Persistence method stubs to get non-AR objects to
    # play nice with e.g. Blacklight's bookmarks
    def persisted?
      true
    end

    def destroyed?
      false
    end

    def new_record?
      false
    end

    def marked_for_destruction?
      false
    end

    ##
    # #to_partial_path is also defined in Blacklight::Document, but
    # ActiveModel::Conversion (included above) will overwrite that..
    def to_partial_path
      'catalog/document'
    end
  end
end

Version data entries

11 entries across 11 versions & 2 rubygems

Version Path
blacklight-8.6.1 app/models/concerns/blacklight/document/active_model_shim.rb
blacklight-8.6.0 app/models/concerns/blacklight/document/active_model_shim.rb
blacklight-8.5.1 app/models/concerns/blacklight/document/active_model_shim.rb
blacklight-8.5.0 app/models/concerns/blacklight/document/active_model_shim.rb
blacklight-8.4.0 app/models/concerns/blacklight/document/active_model_shim.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/blacklight-8.3.0/app/models/concerns/blacklight/document/active_model_shim.rb
blacklight-8.3.0 app/models/concerns/blacklight/document/active_model_shim.rb
blacklight-8.2.2 app/models/concerns/blacklight/document/active_model_shim.rb
blacklight-8.2.1 app/models/concerns/blacklight/document/active_model_shim.rb
blacklight-8.2.0 app/models/concerns/blacklight/document/active_model_shim.rb
blacklight-8.1.0 app/models/concerns/blacklight/document/active_model_shim.rb