module ARIndexer::Model::ClassMethods

Public Class Methods

has_reverse_index(**indexing_opts) click to toggle source
# File lib/ar_indexer/has_reverse_index.rb, line 8
def has_reverse_index(**indexing_opts)
  if indexing_opts.nil?
    indexing_opts = {}
  end

  fields = indexing_opts[:fields] || []

  fields.each do |field_name|
    unless self.columns_hash.keys.include?(field_name.to_s)
      unless ['string', 'text'].include?(self.columns_hash[field_name.to_s].type.to_s)
        raise TypeError, 'Model properties provided to has_reverse_index() must be of field type string or text.'
      end
    end
  end

  associations = indexing_opts[:associations] || {}

  associations.each do |association_name, access_function|
    unless access_function.is_a?(Proc)
      raise TypeError, 'Model associations must have a Proc provided in order to reach the appropriate value.'
    end
  end

  send :include, InstanceMethods

  class_attribute :ari_configuration

  self.ari_configuration = {
    fields: [],
    associations: {},
    index_on_create: [],
    index_on_update: []
  }.merge(indexing_opts)

  after_create :ar_indexer_on_create
  after_update :ar_indexer_on_update
  before_destroy :ar_indexer_on_destroy
end

Private Instance Methods

has_reverse_index(**indexing_opts) click to toggle source
# File lib/ar_indexer/has_reverse_index.rb, line 8
def has_reverse_index(**indexing_opts)
  if indexing_opts.nil?
    indexing_opts = {}
  end

  fields = indexing_opts[:fields] || []

  fields.each do |field_name|
    unless self.columns_hash.keys.include?(field_name.to_s)
      unless ['string', 'text'].include?(self.columns_hash[field_name.to_s].type.to_s)
        raise TypeError, 'Model properties provided to has_reverse_index() must be of field type string or text.'
      end
    end
  end

  associations = indexing_opts[:associations] || {}

  associations.each do |association_name, access_function|
    unless access_function.is_a?(Proc)
      raise TypeError, 'Model associations must have a Proc provided in order to reach the appropriate value.'
    end
  end

  send :include, InstanceMethods

  class_attribute :ari_configuration

  self.ari_configuration = {
    fields: [],
    associations: {},
    index_on_create: [],
    index_on_update: []
  }.merge(indexing_opts)

  after_create :ar_indexer_on_create
  after_update :ar_indexer_on_update
  before_destroy :ar_indexer_on_destroy
end