Module: CamaleonCms::CamaleonHelper

Included in:
CamaleonController, UploaderHelper
Defined in:
app/helpers/camaleon_cms/camaleon_helper.rb

Instance Method Summary collapse

Instance Method Details

#cama_cache_fetch(var_name) ⇒ Object

save value as cache instance and return value sample: cama_cache_fetch("my_key"){ 10+20*12 }



68
69
70
71
72
73
74
75
# File 'app/helpers/camaleon_cms/camaleon_helper.rb', line 68

def cama_cache_fetch(var_name)
  var_name = "@cama_cache_#{var_name}"
  return instance_variable_get(var_name) if instance_variable_defined?(var_name)

  cache = yield
  instance_variable_set(var_name, cache)
  cache
end

create the html link with the url passed verify if current user is logged in, if not, then return nil return html link



6
7
8
9
10
11
12
13
14
15
16
# File 'app/helpers/camaleon_cms/camaleon_helper.rb', line 6

def cama_edit_link(url, title = nil, attrs = {})
  return '' if cama_current_user.blank?
  return '' unless cama_current_user.admin?

  attrs = { target: '_blank', style: 'font-size:11px !important;cursor:pointer;' }.merge(attrs)
  link_to(
    safe_join(['', title || ct('edit', default: 'Edit')]),
    url,
    attrs
  )
end

#cama_is_admin_request?Boolean

Public compatibility API for plugins such as camaleon-ecommerce, which use this predicate to switch from visitor pricing/locale behavior to admin mode. This still matters while previewing frontend content from admin, for example a theme preview that renders ecommerce helpers before the request reaches the public frontend controller stack.

Returns:

  • (Boolean)


36
37
38
# File 'app/helpers/camaleon_cms/camaleon_helper.rb', line 36

def cama_is_admin_request?
  respond_to?(:cama_get_i18n_frontend) && cama_get_i18n_frontend.present?
end

#cama_pluralize_text(text) ⇒ Object

function that converts string into plural format SafeBuffer#pluralize returns a plain String, so callers composing the result with other markup lose the safe flag before they can use it. The safeness of the input is propagated -- never added, so an unsafe input stays unsafe and no caller becomes less safe than before.



87
88
89
90
# File 'app/helpers/camaleon_cms/camaleon_helper.rb', line 87

def cama_pluralize_text(text)
  res = text.try(:pluralize)
  text.try(:html_safe?) ? res.try(:html_safe) : res
end

#cama_sitemap_cats_generator(cats, skip_config = {}) ⇒ Object

generate loop categories html sitemap links this is a helper for sitemap generator to print categories, sub categories and post contents in html list format



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'app/helpers/camaleon_cms/camaleon_helper.rb', line 42

def cama_sitemap_cats_generator(cats, skip_config = {})
  skip_config = {} unless skip_config.is_a?(Hash)
  # merge! alone is not enough: an on_render_sitemap hook that sets one of these keys to nil
  # overwrites the default with nil and brings back the very nil.include? crash the defaults
  # exist to prevent. compact drops those before they win.
  skip_config = { skip_cat_ids: [], skip_post_ids: [] }.merge!(skip_config.compact)
  res = []
  cats.decorate.each do |cat|
    next if skip_config[:skip_cat_ids].include?(cat.id)

    res_posts = []
    cat.the_posts.decorate.each do |post|
      next if skip_config[:skip_post_ids].include?(post.id)

      post_href = ERB::Util.html_escape(post.the_url)
      res_posts << "<li><a href='#{post_href}'>#{post.the_title}</a></li>"
    end
    cat_href = ERB::Util.html_escape(cat.the_url)
    res << "<li><h4><a href='#{cat_href}'>#{cat.the_title}</a></h4><ul>#{res_posts.join('')}</ul></li>"
    res << cama_sitemap_cats_generator(cat.the_categories, skip_config)
  end
  res.join('')
end

#cama_t(key, args = {}) ⇒ Object

return normal translation with default value with translation of english



78
79
80
81
# File 'app/helpers/camaleon_cms/camaleon_helper.rb', line 78

def cama_t(key, args = {})
  args[:default] = I18n.t(key, **args.dup.merge(locale: :en)) if args[:default].blank?
  I18n.t(key, **args)
end

#ct(key, args = {}) ⇒ Object

theme common translation text key: key for translation args: hash of arguments for i18n.t() database customized translations



22
23
24
25
26
27
28
29
# File 'app/helpers/camaleon_cms/camaleon_helper.rb', line 22

def ct(key, args = {})
  language = I18n.locale
  r = { flag: false, key: key, translation: '', locale: language.to_sym }
  hooks_run('on_translation', r)
  return r[:translation] if r[:flag]

  I18n.translate("camaleon_cms.common.#{key}", **args)
end