Module: DbMemoize::Model::ClassMethods

Defined in:
lib/db_memoize/model.rb

Instance Method Summary collapse

Instance Method Details

#db_memoize(method_name) ⇒ Object



65
66
67
68
# File 'lib/db_memoize/model.rb', line 65

def db_memoize(method_name)
  create_alias_method(method_name)
  create_memoized_values_association
end

#unmemoize(records_or_ids, method_name = :all) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/db_memoize/model.rb', line 70

def unmemoize(records_or_ids, method_name = :all)
  records_or_ids = Array(records_or_ids)
  return if records_or_ids.empty?

  ids = if records_or_ids.first.is_a?(ActiveRecord::Base)
          records_or_ids.map(&:id)
        else
          records_or_ids
        end

  conditions = {
    entity_table_name: table_name,
    entity_id: ids
  }
  conditions[:method_name] = method_name unless method_name == :all

  DbMemoize::Value.where(conditions).delete_all
end