Class: CamaleonRecord

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
ActiveRecordExtras::Relation
Defined in:
app/models/camaleon_record.rb

Constant Summary collapse

TRANSLATION_TAG_HIDE_MAP =
{ '<!--' => '!--', '-->' => '--!' }.freeze
TRANSLATION_TAG_HIDE_REGEX =
Regexp.new(TRANSLATION_TAG_HIDE_MAP.keys.map { |x| Regexp.escape(x) }.join('|')).freeze
TRANSLATION_TAG_RESTORE_MAP =
{ '--!' => '-->', '!--' => '<!--' }.freeze
TRANSLATION_TAG_RESTORE_REGEX =
Regexp.new(TRANSLATION_TAG_RESTORE_MAP.keys.map { |x| Regexp.escape(x) }.join('|')).freeze

Instance Method Summary collapse

Instance Method Details

#abilityObject



69
70
71
72
73
# File 'app/models/camaleon_record.rb', line 69

def ability
  # Memoize Ability per request to avoid repeated DB queries and object instantiation.
  # In tests that modify role meta mid-request, call reset_ability to invalidate cache.
  @ability ||= CamaleonCms::Ability.new(current_user, current_site)
end

#cama_build_cache_key(key) ⇒ Object

internal helper to generate cache key



49
50
51
# File 'app/models/camaleon_record.rb', line 49

def cama_build_cache_key(key)
  _key = "cama_cache_#{self.class.name}_#{id}_#{key}"
end

#cama_fetch_cache(key) ⇒ Object

fetch the cache value for this key



27
28
29
30
31
32
33
34
35
36
# File 'app/models/camaleon_record.rb', line 27

def cama_fetch_cache(key)
  @cama_cache_vars ||= {}
  _key = cama_build_cache_key(key)
  if @cama_cache_vars.key?(_key)
    # puts "*********** using model cache var: #{_key}"
  else
    @cama_cache_vars[_key] = yield
  end
  @cama_cache_vars[_key]
end

#cama_get_cache(key) ⇒ Object

return the cache value for this key



39
40
41
42
43
44
45
46
# File 'app/models/camaleon_record.rb', line 39

def cama_get_cache(key)
  @cama_cache_vars ||= {}
  begin
    @cama_cache_vars[cama_build_cache_key(key)]
  rescue StandardError
    nil
  end
end

#cama_remove_cache(key) ⇒ Object

remove cache value for this key



22
23
24
# File 'app/models/camaleon_record.rb', line 22

def cama_remove_cache(key)
  @cama_cache_vars.delete(cama_build_cache_key(key))
end

#cama_set_cache(key, val) ⇒ Object

save cache value for this key



15
16
17
18
19
# File 'app/models/camaleon_record.rb', line 15

def cama_set_cache(key, val)
  @cama_cache_vars ||= {}
  @cama_cache_vars[cama_build_cache_key(key)] = val
  val
end

#can?(*args) ⇒ Boolean

Authorization helpers that delegate to central Ability (CanCan)

Returns:

  • (Boolean)


62
63
64
65
66
67
# File 'app/models/camaleon_record.rb', line 62

def can?(*args)
  # Return false if no user or site context (e.g., background jobs, console)
  return false if current_user.nil? || current_site.nil?

  ability.can?(*args)
end

#current_siteObject

current_site memoized from the CurrentRequest.site



81
82
83
84
85
# File 'app/models/camaleon_record.rb', line 81

def current_site
  return @current_site if defined?(@current_site) && @current_site.present?

  @current_site = CurrentRequest.site
end

#current_userObject

Return the current user for this thread/request context. Uses ActiveSupport::CurrentAttributes (CurrentRequest.user)



55
56
57
58
59
# File 'app/models/camaleon_record.rb', line 55

def current_user
  return @current_user if defined?(@current_user)

  @current_user = CurrentRequest.user
end

#reset_abilityObject

Reset cached ability instance (useful in tests when role meta changes)



76
77
78
# File 'app/models/camaleon_record.rb', line 76

def reset_ability
  @ability = nil
end