Module: ActiveModelPersistence::Indexable::ClassMethods

Defined in:
lib/active_model_persistence/indexable.rb

Overview

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

Instance Method Summary collapse

Instance Method Details

#index(index_name, **options) ⇒ void

This method returns an undefined value.

Adds an index to the model

Examples:

Add a unique index on the id attribute

Employee.index(:id, unique: true)

with a key_value_source when the name and the attribute are different

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

with a Proc for key_value_source

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

Parameters:

  • index_name (String)

    the name of the index

  • options (Hash)

    the options for the index

Options Hash (**options):

  • :unique (Boolean)

    whether the index is unique, default is false

  • :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.



96
97
98
99
100
101
102
103
104
105
# 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

#indexesHash<String, ActiveModelPersistence::Index>

Returns a hash of indexes for the model keyed by name

Examples:

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

Returns:



70
71
72
# File 'lib/active_model_persistence/indexable.rb', line 70

def indexes
  @indexes ||= {}
end

#remove_from_indexes(object) ⇒ void

This method returns an undefined value.

Removes the given object from all defined indexes

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

Examples:

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

Parameters:

  • object (Object)

    the object to remove from the indexes



146
147
148
# 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) ⇒ void

This method returns an undefined value.

Adds or updates all defined indexes for the given object

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

Examples:

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]

Parameters:

  • object (Object)

    the object to add to the indexes



127
128
129
# File 'lib/active_model_persistence/indexable.rb', line 127

def update_indexes(object)
  indexes.each_value { |index| index.add_or_update(object) }
end