Module: CamaleonCms::CustomFieldsRead
- Extended by:
- ActiveSupport::Concern
- Included in:
- PostDefault, TermTaxonomy, User
- Defined in:
- app/models/concerns/camaleon_cms/custom_fields_read.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
-
#add_custom_field_group(values, kind = "Post") ⇒ Object
(also: #add_field_group)
Model supported: PostType, Category, Post, Posttag, Widget, Plugin, Theme, User and Custom models pre configured Note 1: If you need add fields for all post’s or all categories, then you need to add the fields into the post_type.add_custom_field_group(values, kind = “Post”) post_type.add_custom_field_group(values, kind = “Category”) Note 2: If you need add fields for only the Post_type, you have to use options or metas return: CustomFieldGroup object kind: argument only for PostType model: (Post | Category | PostTag), default => Post.
-
#add_custom_field_to_default_group(item, options, kind = "Post") ⇒ Object
(also: #add_field)
Add custom fields for a default group: This will create a new group with slug=_default if it doesn’t exist yet more details in add_manual_field(item, options) from custom field groups kind: argument only for PostType model: (Post | Category | PostTag), default => Post.
-
#get_field!(_key, _default = nil) ⇒ Object
the same as the_field() but if the value is not present, this will return default value.
-
#get_field_groups(args = {}) ⇒ Object
args: (String) => is a value for kind attribute.
-
#get_field_object(slug) ⇒ Object
return field object for current model.
-
#get_field_value(_key, _default = nil) ⇒ Object
(also: #get_field)
get custom field value _key: custom field key if value is not present, then return default return default only if the field was not registered.
-
#get_field_values(_key) ⇒ Object
(also: #get_fields)
get custom field values _key: custom field key.
-
#get_field_values_hash(include_options = false) ⇒ Object
return all values “single value”, key2: [multiple, values], key3: value4 if include_options = false {values: “single value”, options: {a:1, b: 4}, key2: [multiple, values], options: {a=1, b=2 }} if include_options = true.
-
#get_fields_object(f = true) ⇒ Object
return all custom fields for current element {options: {, values: [], name: ”, …} } deprecated f attribute.
-
#get_user_field_groups(site) ⇒ Object
get custom field groups for current user return collections CustomFieldGroup site: site object.
-
#save_field_value(key, value, order = 0, clear = true) ⇒ Object
clear and register values for this custom field key: slug of the custom field value: array of values for multiple values support value: string value.
-
#set_field_values(datas = {}) ⇒ Object
only custom field plugin (protected) example: id: custom_field_id { :key : 123, values: [‘uno’,‘dos’] :key2 : 455, values: [‘uno’,‘dos’] :key3 : 4555, values: [‘uno’,‘dos’] }.
-
#update_field_value(_key, value = nil) ⇒ Object
————- new function update field value ————-.
Instance Method Details
#add_custom_field_group(values, kind = "Post") ⇒ Object Also known as: add_field_group
Model supported: PostType, Category, Post, Posttag, Widget, Plugin, Theme, User and Custom models pre configured Note 1: If you need add fields for all post’s or all categories, then you need to add the fields into the
post_type.add_custom_field_group(values, kind = "Post")
post_type.add_custom_field_group(values, kind = "Category")
Note 2: If you need add fields for only the Post_type, you have to use options or metas return: CustomFieldGroup object kind: argument only for PostType model: (Post | Category | PostTag), default => Post. If kind = “” this will add group for all post_types
141 142 143 144 145 146 147 148 |
# File 'app/models/concerns/camaleon_cms/custom_fields_read.rb', line 141 def add_custom_field_group(values, kind = "Post") values = values.with_indifferent_access group = get_field_groups(kind).where(slug: values[:slug]).first unless group.present? group = get_field_groups(kind).create(values) end group end |
#add_custom_field_to_default_group(item, options, kind = "Post") ⇒ Object Also known as: add_field
Add custom fields for a default group: This will create a new group with slug=_default if it doesn’t exist yet more details in add_manual_field(item, options) from custom field groups kind: argument only for PostType model: (Post | Category | PostTag), default => Post
155 156 157 158 159 |
# File 'app/models/concerns/camaleon_cms/custom_fields_read.rb', line 155 def add_custom_field_to_default_group(item, , kind = "Post") g = get_field_groups(kind).where(slug: "_default").first g = add_custom_field_group({name: "Default Field Group", slug: "_default"}, kind) unless g.present? f = g.add_manual_field(item, ) end |
#get_field!(_key, _default = nil) ⇒ Object
the same as the_field() but if the value is not present, this will return default value
80 81 82 83 84 |
# File 'app/models/concerns/camaleon_cms/custom_fields_read.rb', line 80 def get_field!(_key, _default = nil) v = _default v = get_field_values(_key).first rescue _default v.present? ? v : _default end |
#get_field_groups(args = {}) ⇒ Object
args: (String) => is a value for kind attribute
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'app/models/concerns/camaleon_cms/custom_fields_read.rb', line 26 def get_field_groups(args = {}) args = args.is_a?(String) ? {kind: args, include_parent: false } : {kind: "post", include_parent: false }.merge(args) class_name = self.class.to_s.gsub("Decorator","").gsub("CamaleonCms::","") case class_name when 'Category','Post','PostTag' if args[:include_parent] self.post_type.site.custom_field_groups.where("(objectid = ? AND object_class = ?) OR (objectid = ? AND object_class = ?)", self.id || -1, class_name, self.post_type.id, "PostType_#{class_name}") else self.post_type.site.custom_field_groups.where(objectid: self.id || -1, object_class: class_name) end when 'Widget::Main' self.site.custom_field_groups.where(object_class: class_name, objectid: self.id) when 'Theme' self.site.custom_field_groups.where(object_class: class_name, objectid: self.id) when 'Site' self.custom_field_groups.where(object_class: class_name) when 'NavMenuItem' # self.main_menu.custom_field_groups //verify this problem puts "get_field_groups - NavMenuItem: **************#{self.inspect}***** #{self..inspect}" CamaleonCms::NavMenu.find(self..id).get_field_groups when 'PostType' if args[:kind] == "all" self.site.custom_field_groups.where(object_class: ["PostType_Post", "PostType_Post", "PostType_PostTag", "PostType"], objectid: self.id ) elsif args[:kind] == "post_type" self.site.custom_field_groups.where(object_class: class_name) else self.site.custom_field_groups.where(object_class: "PostType_#{args[:kind]}", objectid: self.id ) end else # 'Plugin' or other class self.site.custom_field_groups.where(object_class: class_name, objectid: self.id) if defined?(self.site) end end |
#get_field_object(slug) ⇒ Object
return field object for current model
194 195 196 |
# File 'app/models/concerns/camaleon_cms/custom_fields_read.rb', line 194 def get_field_object(slug) CamaleonCms::CustomField.where(parent_id: get_field_groups.pluck(:id), slug: slug).first || CamaleonCms::CustomField.where(slug: slug, parent_id: get_field_groups({include_parent: true})).first end |
#get_field_value(_key, _default = nil) ⇒ Object Also known as: get_field
get custom field value _key: custom field key if value is not present, then return default return default only if the field was not registered
72 73 74 75 76 |
# File 'app/models/concerns/camaleon_cms/custom_fields_read.rb', line 72 def get_field_value(_key, _default = nil) v = _default v = get_field_values(_key).first rescue _default v.present? ? v : _default end |
#get_field_values(_key) ⇒ Object Also known as: get_fields
get custom field values _key: custom field key
88 89 90 |
# File 'app/models/concerns/camaleon_cms/custom_fields_read.rb', line 88 def get_field_values(_key) self.field_values.where(custom_field_slug: _key).pluck(:value) end |
#get_field_values_hash(include_options = false) ⇒ Object
return all values “single value”, key2: [multiple, values], key3: value4 if include_options = false {values: “single value”, options: {a:1, b: 4}, key2: [multiple, values], options: {a=1, b=2 }} if include_options = true
102 103 104 105 106 107 108 109 110 111 |
# File 'app/models/concerns/camaleon_cms/custom_fields_read.rb', line 102 def get_field_values_hash( = false) fields = {} self.field_values.to_a.uniq.each do |field_value| custom_field = field_value.custom_fields values = custom_field.values.where(objectid: self.id).pluck(:value) fields[field_value.custom_field_slug] = custom_field.[:multiple].to_s.to_bool ? values : values.first unless fields[field_value.custom_field_slug] = {values: custom_field.[:multiple].to_s.to_bool ? values : values.first, options: custom_field., id: custom_field.id} if end fields.to_sym end |
#get_fields_object(f = true) ⇒ Object
return all custom fields for current element {options: {, values: [], name: ”, …} } deprecated f attribute
116 117 118 119 120 121 122 123 124 125 126 |
# File 'app/models/concerns/camaleon_cms/custom_fields_read.rb', line 116 def get_fields_object(f=true) fields = {} self.field_values.to_a.uniq.each do |field_value| custom_field = field_value.custom_fields # if custom_field.options[:show_frontend].to_s.to_bool values = custom_field.values.where(objectid: self.id).pluck(:value) fields[field_value.custom_field_slug] = custom_field.attributes.merge(options: custom_field., values: custom_field.[:multiple].to_s.to_bool ? values : values.first) # end end fields.to_sym end |
#get_user_field_groups(site) ⇒ Object
get custom field groups for current user return collections CustomFieldGroup site: site object
62 63 64 65 |
# File 'app/models/concerns/camaleon_cms/custom_fields_read.rb', line 62 def get_user_field_groups(site) class_name = self.class.to_s.gsub("Decorator","") site.custom_field_groups.where(object_class: class_name) end |
#save_field_value(key, value, order = 0, clear = true) ⇒ Object
clear and register values for this custom field key: slug of the custom field value: array of values for multiple values support value: string value
202 203 204 205 206 207 208 209 210 211 212 213 |
# File 'app/models/concerns/camaleon_cms/custom_fields_read.rb', line 202 def save_field_value(key, value, order = 0, clear = true) field = get_field_object(key) return unless field.present? self.field_values.where({custom_field_slug: key}).destroy_all if clear if value.is_a?(Array) value.each do |val| self.field_values.create!({custom_field_id: field.id, custom_field_slug: key, value: (val), term_order: order}) end else self.field_values.create!({custom_field_id: field.id, custom_field_slug: key, value: (value), term_order: order}) end end |
#set_field_values(datas = {}) ⇒ Object
only custom field plugin (protected) example: id: custom_field_id { :key : 123, values: [‘uno’,‘dos’] :key2 : 455, values: [‘uno’,‘dos’] :key3 : 4555, values: [‘uno’,‘dos’] }
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'app/models/concerns/camaleon_cms/custom_fields_read.rb', line 170 def set_field_values(datas = {}) ids_old = self.field_values.pluck(:id) ids_saved = [] if datas.present? datas.each do |key, values| if values[:values].present? order_value = 0 values[:values].each do |value| item = self.field_values.where({custom_field_id: values[:id], custom_field_slug: key, value: (value)}).first_or_create!() if defined?(item.id) item.update_column('term_order', order_value) ids_saved << item.id order_value += 1 end end end end end ids_deletes = ids_old - ids_saved self.field_values.where(id: ids_deletes).destroy_all if ids_deletes.present? end |
#update_field_value(_key, value = nil) ⇒ Object
————- new function update field value ————-
94 95 96 |
# File 'app/models/concerns/camaleon_cms/custom_fields_read.rb', line 94 def update_field_value(_key, value = nil) self.field_values.where(custom_field_slug: _key).first.update_column('value', value) rescue nil end |