Module: CommonDataCaching::ClassMethods

Defined in:
lib/common_data_caching/class_methods.rb

Instance Method Summary collapse

Instance Method Details

#common_data_cache_collectionObject



12
13
14
# File 'lib/common_data_caching/class_methods.rb', line 12

def common_data_cache_collection
  Rails.cache.fetch(common_data_cache_key) || []
end

#common_data_cache_keyObject



8
9
10
# File 'lib/common_data_caching/class_methods.rb', line 8

def common_data_cache_key
  "common-data-caching_#{to_s}"
end

#common_data_caching?Boolean

Returns:

  • (Boolean)


4
5
6
# File 'lib/common_data_caching/class_methods.rb', line 4

def common_data_caching?
  true
end

#prepare_common_data_collectionObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/common_data_caching/class_methods.rb', line 30

def prepare_common_data_collection
  # колонка ID по умолчанию
  hash = { id: :id }

  cache_opts[:attrs].flatten.map do |key|
    key.is_a?(Hash) ? hash.merge!(key) : hash.merge!(key => key)
  end

  keys = hash.keys.unshift(:id)

  all.map do |object|
    keys.to_h { |title| [title, object.send(hash[title])] }
  end
end

#update_common_data_cacheObject



16
17
18
19
20
21
22
# File 'lib/common_data_caching/class_methods.rb', line 16

def update_common_data_cache
  scope = cache_opts[:scope] || proc { where('1=1') }
  ordering = cache_opts[:order] || :id

  data = merge(scope).order(ordering).prepare_common_data_collection
  Rails.cache.write(common_data_cache_key, data)
end

#update_common_data_cache_versionsObject



24
25
26
27
28
# File 'lib/common_data_caching/class_methods.rb', line 24

def update_common_data_cache_versions
  versions = Rails.cache.read('common-data-caching-versions') || {}
  versions[to_s] = Time.now.to_i
  Rails.cache.write('common-data-caching-versions', versions)
end