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
-
#delete_meta(key) ⇒ Object
delete meta.
-
#delete_option(key, meta_key = '_default') ⇒ Object
delete attribute from configuration.
-
#fix_save_metas_options_no_changed ⇒ Object
fix to save options and metas when a model was not changed.
-
#get_meta(key, default = nil) ⇒ Object
return value of meta with key: key, if meta not exist, return default return default if meta value == “”.
-
#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.
-
#options(meta_key = '_default') ⇒ Object
(also: #cama_options)
return configurations for current object, sample: “type”:“post_type”,“object_id”:“127”.
-
#save_metas_options ⇒ Object
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” }).
-
#save_metas_options_skip ⇒ Object
permit to skip save_metas_options in specific models.
-
#set_meta(key, value) ⇒ Object
Add meta with value or Update meta with key: key return true or false.
-
#set_metas(data_metas) ⇒ Object
save multiple metas sample: set_metas(‘Owen’, email: ‘owenperedo@gmail.com’).
-
#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”).
-
#set_options(h = {}, meta_key = '_default') ⇒ Object
(also: #set_multiple_options)
set multiple configurations h: “sdsds”, ff: “fdfdfdfd”.
Instance Method Details
#delete_meta(key) ⇒ Object
delete meta
46 47 48 49 |
# File 'app/models/concerns/camaleon_cms/metas.rb', line 46 def (key) .where(key: key).destroy_all cama_remove_cache("meta_#{key}") end |
#delete_option(key, meta_key = '_default') ⇒ Object
delete attribute from configuration
83 84 85 86 87 88 |
# File 'app/models/concerns/camaleon_cms/metas.rb', line 83 def delete_option(key, = '_default') values = () key = key.to_sym values.delete(key) if values.key?(key) (, values) end |
#fix_save_metas_options_no_changed ⇒ Object
fix to save options and metas when a model was not changed
117 118 119 |
# File 'app/models/concerns/camaleon_cms/metas.rb', line 117 def # 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 == “”
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'app/models/concerns/camaleon_cms/metas.rb', line 24 def (key, default = nil) key_str = key.is_a?(Symbol) ? key.to_s : key cama_fetch_cache("meta_#{key_str}") do option = .loaded? ? .select { |m| m.key == key }.first : .where(key: key_str).first res = '' if option.present? value = begin JSON.parse(option.value) rescue StandardError option.value end res = begin (value.is_a?(Hash) ? value.with_indifferent_access : value) rescue StandardError option.value end 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
76 77 78 79 80 |
# File 'app/models/concerns/camaleon_cms/metas.rb', line 76 def get_option(key = nil, default = nil, = '_default') values = () key = key.to_sym values.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”
52 53 54 |
# File 'app/models/concerns/camaleon_cms/metas.rb', line 52 def ( = '_default') (, {}) end |
#save_metas_options ⇒ Object
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” })
123 124 125 126 127 128 129 130 |
# File 'app/models/concerns/camaleon_cms/metas.rb', line 123 def () return unless .present? .each do |key, val| (key, val) end end |
#save_metas_options_skip ⇒ Object
permit to skip save_metas_options in specific models
112 113 114 |
# File 'app/models/concerns/camaleon_cms/metas.rb', line 112 def false end |
#set_meta(key, value) ⇒ Object
Add meta with value or Update meta with key: key return true or false
16 17 18 19 |
# File 'app/models/concerns/camaleon_cms/metas.rb', line 16 def (key, value) .where(key: key).update_or_create({ value: (value) }) cama_set_cache("meta_#{key}", value) end |
#set_metas(data_metas) ⇒ Object
save multiple metas sample: set_metas(‘Owen’, email: ‘owenperedo@gmail.com’)
105 106 107 108 109 |
# File 'app/models/concerns/camaleon_cms/metas.rb', line 105 def () (.nil? ? {} : ).each do |key, value| (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”)
62 63 64 65 66 67 68 69 |
# File 'app/models/concerns/camaleon_cms/metas.rb', line 62 def set_option(key, value = nil, = '_default') return if key.nil? data = () data[key] = (value) (, data) value end |
#set_options(h = {}, meta_key = '_default') ⇒ Object Also known as: set_multiple_options
set multiple configurations h: “sdsds”, ff: “fdfdfdfd”
92 93 94 95 96 97 98 99 100 |
# File 'app/models/concerns/camaleon_cms/metas.rb', line 92 def (h = {}, = '_default') return unless h.present? data = () PluginRoutes.fixActionParameter(h).to_sym.each do |key, value| data[key] = (value) end (, data) end |