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
-
#index(index_name, **options) ⇒ void
Adds an index to the model.
-
#indexes ⇒ Hash<String, ActiveModelPersistence::Index>
Returns a hash of indexes for the model keyed by name.
-
#remove_from_indexes(object) ⇒ void
Removes the given object from all defined indexes.
-
#update_indexes(object) ⇒ void
Adds or updates all defined indexes for the given object.
Instance Method Details
#index(index_name, **options) ⇒ void
This method returns an undefined value.
Adds an index to the model
96 97 98 99 100 101 102 103 104 105 |
# File 'lib/active_model_persistence/indexable.rb', line 96 def index(index_name, **) index = Index.new(**(index_name).merge()) 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 ⇒ Hash<String, ActiveModelPersistence::Index>
Returns a hash of indexes for the model keyed by name
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.
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.
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 |