Sha256: 3f3a890c898dc20e271930866a25223350ff4954aeb460fc98e0513d4fb96748

Contents?: true

Size: 1.67 KB

Versions: 1

Compression:

Stored size: 1.67 KB

Contents

module Qa::Authorities
  class Local::TableBasedAuthority < Base
    def self.check_for_index
      conn = ActiveRecord::Base.connection
      if conn.table_exists?('local_authority_entries') && !conn.indexes('local_authority_entries').find { |i| i.name == 'index_local_authority_entries_on_lower_label' }
        Rails.logger.error "You've installed local authority tables, but you haven't indexed the label.  Rails doesn't support functional indexes in migrations, so you'll have to add this manually:\n" \
          "CREATE INDEX \"index_qa_local_authority_entries_on_lower_label\" ON \"qa_local_authority_entries\" (local_authority_id, lower(label))\n" \
          "   OR on Sqlite: \n" \
          "CREATE INDEX \"index_qa_local_authority_entries_on_lower_label\" ON \"qa_local_authority_entries\" (local_authority_id, label collate nocase)\n" \
      end
    end

    attr_reader :subauthority

    def initialize(subauthority)
      @subauthority = subauthority
    end

    def search(q)
      return [] if q.blank?
      output_set(base_relation.where('lower(label) like ?', "#{q.downcase}%").limit(25))
    end

    def all
      output_set(base_relation.limit(1000))
    end

    def find(uri)
      record = base_relation.find_by(uri: uri)
      return unless record
      output(record)
    end

    private

      def base_relation
        Qa::LocalAuthorityEntry.where(local_authority: local_authority)
      end

      def output_set(set)
        set.map { |item| output(item) }
      end

      def output(item)
        { id: item[:uri], label: item[:label] }.with_indifferent_access
      end

      def local_authority
        Qa::LocalAuthority.find_by_name(subauthority)
      end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
qa-0.8.0 lib/qa/authorities/local/table_based_authority.rb