Module: HasHelpers::Index::ActiveRecord::ClassMethods

Defined in:
app/lib/has_helpers/index.rb

Instance Method Summary collapse

Instance Method Details

#define_inheritance_search(model_class) ⇒ Object



871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
# File 'app/lib/has_helpers/index.rb', line 871

def define_inheritance_search(model_class)
  model_class.class_eval do
    define_method("inheritance_search") do |dependency_array, id, model|
      keys = dependency_array.pop
      inner_to_column = keys[:to] || "id"
      target = keys[:target].to_s.safe_constantize

      if dependency_array.empty?
        target.where("#{inner_to_column}": id)
      else
        target.where("#{inner_to_column}": inheritance_search(dependency_array, id, model).ids)
      end
    end
  end
end

#process_dependencies(obj, from_column, to_column, index_model, dependent_attributes, dependency_inheritance, previous_changes) ⇒ Object



853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
# File 'app/lib/has_helpers/index.rb', line 853

def process_dependencies(obj, from_column, to_column, index_model, dependent_attributes, dependency_inheritance, previous_changes)
  if (id = obj.read_attribute(from_column)) &&
     (dependent_attributes.nil? || (previous_changes.keys & dependent_attributes).any?)

    if previous_changes[from_column]
      index_model.where("#{to_column}": previous_changes[from_column].reject(&:nil?)).find_each(&:reindex)
    else
      index_model.find_by("#{to_column}": id)&.reindex
    end
    if dependency_inheritance.present?
      dependency_inheritance.each do |keys|
        keys_copy = keys.dup
        obj.inheritance_search(keys_copy, id, index_model).reject(&:nil?).each(&:reindex)
      end
    end
  end
end

#search_indexObject



847
848
849
850
851
# File 'app/lib/has_helpers/index.rb', line 847

def search_index
  "#{self.base_class.name}Index".constantize
rescue
  raise HasHelpers::Index::Error, "Index not defined for #{self.name}"
end