Module: CamaleonCms::Metas

Extended by:
ActiveSupport::Concern
Included in:
CustomField, PostComment, PostDefault, TermTaxonomy
Defined in:
app/models/concerns/camaleon_cms/metas.rb

Instance Method Summary collapse

Instance Method Details

#delete_meta(key) ⇒ Object

delete meta


36
37
38
39
# File 'app/models/concerns/camaleon_cms/metas.rb', line 36

def delete_meta(key)
  metas.where(key: key).destroy_all
  cama_remove_cache("meta_#{key}")
end

#delete_option(key, meta_key = "_default") ⇒ Object

delete attribute from configuration


72
73
74
75
76
77
# File 'app/models/concerns/camaleon_cms/metas.rb', line 72

def delete_option(key, meta_key = "_default")
  values = cama_options(meta_key)
  key = key.to_sym
  values.delete(key) if values.has_key?(key)
  set_meta(meta_key, values)
end

#fix_save_metas_options_no_changedObject

fix to save options and metas when a model was not changed


106
107
108
# File 'app/models/concerns/camaleon_cms/metas.rb', line 106

def fix_save_metas_options_no_changed
  save_metas_options #unless self.changed?
end

#get_meta(key, default = nil) ⇒ Object

return value of meta with key: key, if meta not exist, return default return default if meta value == “”


22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/models/concerns/camaleon_cms/metas.rb', line 22

def get_meta(key, default = nil)
  key_str = key.is_a?(Symbol) ? key.to_s : key
  cama_fetch_cache("meta_#{key_str}") do
    option = metas.loaded? ? metas.select{|m| m.key == key }.first : metas.where(key: key_str).first
    res = ''
    if option.present?
      value = JSON.parse(option.value) rescue option.value
      res = (value.is_a?(Hash) ? value.with_indifferent_access : value) rescue option.value
    end
    res == '' ? default : res
  end
end

#get_option(key = nil, default = nil, meta_key = "_default") ⇒ Object

return configuration for current object key: attribute name default: if attribute not exist, return default return default if option value == “” return value for attribute


65
66
67
68
69
# File 'app/models/concerns/camaleon_cms/metas.rb', line 65

def get_option(key = nil, default = nil, meta_key = "_default")
  values = cama_options(meta_key)
  key = key.to_sym
  values.has_key?(key) && values[key] != "" ? values[key] : default
end

#options(meta_key = "_default") ⇒ Object Also known as: cama_options

return configurations for current object, sample: “type”:“post_type”,“object_id”:“127”


42
43
44
# File 'app/models/concerns/camaleon_cms/metas.rb', line 42

def options(meta_key = "_default")
  get_meta(meta_key, {})
end

#save_metas_optionsObject

save all settings for this post type received in data_options and data_metas attribute (options and metas) sample: Site.first.post_types.create(“owen”, slug: “my_post_type”, data_options: { has_category: true, default_layout: “my_layout” })


112
113
114
115
116
117
118
119
# File 'app/models/concerns/camaleon_cms/metas.rb', line 112

def save_metas_options
  set_multiple_options(data_options)
  if data_metas.present?
    data_metas.each do |key, val|
      set_meta(key, val)
    end
  end
end

#save_metas_options_skipObject

permit to skip save_metas_options in specific models


101
102
103
# File 'app/models/concerns/camaleon_cms/metas.rb', line 101

def save_metas_options_skip
  false
end

#set_meta(key, value) ⇒ Object

Add meta with value or Update meta with key: key return true or false


14
15
16
17
# File 'app/models/concerns/camaleon_cms/metas.rb', line 14

def set_meta(key, value)
  metas.where(key: key).update_or_create({value: fix_meta_value(value)})
  cama_set_cache("meta_#{key}", value)
end

#set_metas(data_metas) ⇒ Object

save multiple metas sample: set_metas('Owen', email: '[email protected]')


94
95
96
97
98
# File 'app/models/concerns/camaleon_cms/metas.rb', line 94

def set_metas(data_metas)
  (data_metas.nil? ? {} : data_metas).each do |key, value|
    self.set_meta(key, value)
  end
end

#set_option(key, value = nil, meta_key = "_default") ⇒ Object

add configuration for current object key: attribute name value: attribute value meta_key: (String) name of the meta attribute sample: mymodel.set_custom_option(“my_settings”, “color”, “red”)


52
53
54
55
56
57
58
# File 'app/models/concerns/camaleon_cms/metas.rb', line 52

def set_option(key, value = nil, meta_key = "_default")
  return if key.nil?
  data = cama_options(meta_key)
  data[key] = fix_meta_var(value)
  set_meta(meta_key, data)
  value
end

#set_options(h = {}, meta_key = "_default") ⇒ Object Also known as: set_multiple_options

set multiple configurations h: “sdsds”, ff: “fdfdfdfd”


81
82
83
84
85
86
87
88
89
# File 'app/models/concerns/camaleon_cms/metas.rb', line 81

def set_options(h = {}, meta_key = "_default")
  if h.present?
    data = cama_options(meta_key)
    PluginRoutes.fixActionParameter(h).to_sym.each do |key, value|
      data[key] = fix_meta_var(value)
    end
    set_meta(meta_key, data)
  end
end