module ActiveModelPersistence::Indexable::ClassMethods

When this module is included in another class, ActiveSupport::Concern will make these class methods on that class.

Public Instance Methods

index(index_name, **options) click to toggle source

Adds an index to the model

@example Add a unique index on the id attribute

Employee.index(:id, unique: true)

@example with a key_value_source when the name and the attribute are different

Employee.index(:manager, key_value_source: :manager_id)

@example with a Proc for key_value_source

Employee.index(:birth_year, key_value_source: ->(o) { o.birth_date.year })

@param index_name [String] the name of the index @param options [Hash] the options for the index @option options :unique [Boolean] whether the index is unique, default is false @option options :key_value_source [Symbol, Proc] the source of the key value of the object for this index

If a Symbol is given, it will name a zero arg method on the object which returns
the key's value. If a Proc is given, the key's value will be the result of calling the
Proc with the object.

@return [void]

# File lib/active_model_persistence/indexable.rb, line 96
def index(index_name, **options)
  index = Index.new(**default_index_options(index_name).merge(options))
  indexes[index_name.to_sym] = index

  singleton_class.define_method("find_by_#{index_name}") do |key|
    index.objects(key).tap do |objects|
      objects.each { |o| o.instance_variable_set(:@previously_new_record, false) }
    end
  end
end
indexes() click to toggle source

Returns a hash of indexes for the model keyed by name

@example

Employee.indexes.keys # => %w[id manager_id birth_year]

@return [Hash<String, ActiveModelPersistence::Index>] the indexes defined by the model

# File lib/active_model_persistence/indexable.rb, line 70
def indexes
  @indexes ||= {}
end
remove_from_indexes(object) click to toggle source

Removes the given object from all defined indexes

Call this before deleting the object to ensure the indexes are up to date.

@example

e1 = Employee.new(id: 1, name: 'James', manager_id: 3, birth_date: Date.new(1967, 3, 15))
Employee.update_indexes(e1)
Employee.find_by_id(1) # => e1
Employee.remove_from_indexes(e1)
Employee.find_by_id(1) # => nil

@param object [Object] the object to remove from the indexes

@return [void]

# File lib/active_model_persistence/indexable.rb, line 146
def remove_from_indexes(object)
  indexes.each_value { |index| index.remove(object) }
end
update_indexes(object) click to toggle source

Adds or updates all defined indexes for the given object

Call this after changing the object to ensure the indexes are up to date.

@example

e1 = Employee.new(id: 1, name: 'James', manager_id: 3, birth_date: Date.new(1967, 3, 15))
Employee.update_indexes(e1)
Employee.find_by_id(1) # => e1
Employee.find_by_manager_id(3) # => [e1]
Employee.find_by_birth_year(1967) # => [e1]

e1.birth_date = Date.new(1968, 1, 27)
Employee.find_by_birth_year(1968) # => []
Employee.update_indexes(e1)
Employee.find_by_birth_year(1968) # => [e1]

@param object [Object] the object to add to the indexes

@return [void]

# File lib/active_model_persistence/indexable.rb, line 127
def update_indexes(object)
  indexes.each_value { |index| index.add_or_update(object) }
end

Private Instance Methods

default_index_options(index_name) click to toggle source

Defines the default options for a new ActiveModelPersistence::Index

@return [Hash] the default options

@api private

# File lib/active_model_persistence/indexable.rb, line 158
def default_index_options(index_name)
  {
    name: index_name.to_sym,
    unique: false
  }
end