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 Attribute Summary collapse
-
#indexes ⇒ Hash<String, ActiveModelPersistence::Index>
readonly
Returns a hash of indexes for the model keyed by name.
Instance Method Summary collapse
-
#index(index_name, **options) ⇒ void
Adds an index to the model.
-
#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 Attribute Details
#indexes ⇒ Hash<String, ActiveModelPersistence::Index> (readonly)
Returns a hash of indexes for the model keyed by name
|
# File 'lib/active_model_persistence/indexable.rb', line 63
|
Instance Method Details
#index(index_name, **options) ⇒ void
This method returns an undefined value.
Adds an index to the model
95 96 97 98 99 100 101 102 |
# File 'lib/active_model_persistence/indexable.rb', line 95 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).select { |object| object.is_a?(self) } end 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.
145 146 147 148 149 |
# File 'lib/active_model_persistence/indexable.rb', line 145 def remove_from_indexes(object) indexes.each_value do |index| index.remove(object) if object.is_a?(index.base_class) end 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.
124 125 126 127 128 |
# File 'lib/active_model_persistence/indexable.rb', line 124 def update_indexes(object) indexes.each_value do |index| index.add_or_update(object) if object.is_a?(index.base_class) end end |