Module: CamaleonCms::Metas

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

Overview

Camaleon CMS is a content management system

Copyright (C) 2015 by Owen Peredo Diaz
Email: [email protected]
This program is free software: you can redistribute it and/or modify   it under the terms of the GNU Affero General Public License as  published by the Free Software Foundation, either version 3 of the  License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,  but WITHOUT ANY WARRANTY; without even the implied warranty of  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the  GNU Affero General Public License (GPLv3) for more details.

Instance Method Summary collapse

Instance Method Details

#delete_meta(key) ⇒ Object

delete meta


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

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


78
79
80
81
82
83
# File 'app/models/concerns/camaleon_cms/metas.rb', line 78

def delete_option(key, meta_key = "_default")
  values = 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


103
104
105
# File 'app/models/concerns/camaleon_cms/metas.rb', line 103

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 == “”


30
31
32
33
34
35
36
37
38
39
40
# File 'app/models/concerns/camaleon_cms/metas.rb', line 30

def get_meta(key, default = nil)
  cama_fetch_cache("meta_#{key}") do
    option = metas.where(key: key).first
    res = ""
    if option.present?
      value = JSON.parse(option.value) rescue option.value
      res = (value.is_a?(Hash) ? value.to_sym : 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


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

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

#options(meta_key = "_default") ⇒ Object

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


49
50
51
# File 'app/models/concerns/camaleon_cms/metas.rb', line 49

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” })


109
110
111
112
113
114
115
116
# File 'app/models/concerns/camaleon_cms/metas.rb', line 109

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


98
99
100
# File 'app/models/concerns/camaleon_cms/metas.rb', line 98

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


22
23
24
25
# File 'app/models/concerns/camaleon_cms/metas.rb', line 22

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_multiple_options(h = {}, meta_key = "_default") ⇒ Object

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


87
88
89
90
91
92
93
94
95
# File 'app/models/concerns/camaleon_cms/metas.rb', line 87

def set_multiple_options(h = {}, meta_key = "_default")
  if h.present?
    data = options(meta_key)
    h.to_sym.each do |key, value|
      data[key] = fix_meta_var(value)
    end
    set_meta(meta_key, data)
  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”)


58
59
60
61
62
63
64
# File 'app/models/concerns/camaleon_cms/metas.rb', line 58

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