Module: GacoCms::FieldsAssignable

Extended by:
ActiveSupport::Concern
Included in:
DraftRecord, Page, Theme
Defined in:
app/models/concerns/gaco_cms/fields_assignable.rb

Instance Method Summary collapse

Instance Method Details

#the_grouped_values(*keys, cache: true) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
# File 'app/models/concerns/gaco_cms/fields_assignable.rb', line 28

def the_grouped_values(*keys, cache: true)
  callback = proc do
    field_values.where(field_key: keys).grouped.map do |fields|
      fields.map { |f| [f.field_key, f.the_value] }.to_h
    end
  end
  return callback.call unless cache

  cache_key = cache_key_locale(:the_grouped_values, keys.join('-'))
  Rails.cache.fetch(cache_key, expires_at: Time.current.end_of_day, &callback)
end

#the_value(key, cache: true) ⇒ Object



14
15
16
17
18
19
# File 'app/models/concerns/gaco_cms/fields_assignable.rb', line 14

def the_value(key, cache: true)
  callback = proc { field_values.ordered.find_by(field_key: key)&.the_value }
  return callback.call unless cache

  Rails.cache.fetch(cache_key_locale(:the_value, key), expires_at: Time.current.end_of_day, &callback)
end

#the_values(key, cache: true) ⇒ Object



21
22
23
24
25
26
# File 'app/models/concerns/gaco_cms/fields_assignable.rb', line 21

def the_values(key, cache: true)
  callback = proc { field_values.ordered.where(field_key: key).map(&:the_value) }
  return callback.call unless cache

  Rails.cache.fetch(cache_key_locale(:the_values, key), expires_at: Time.current.end_of_day, &callback)
end