Class: CamaleonCms::CustomFieldGroup

Inherits:
CustomField
  • Object
show all
Defined in:
app/models/camaleon_cms/custom_field_group.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

Methods included from Metas

#delete_meta, #delete_option, #fix_save_metas_options_no_changed, #get_meta, #get_option, #options, #save_metas_options, #save_metas_options_skip, #set_meta, #set_metas, #set_multiple_options, #set_option

Instance Method Details

#add_fields(items, item_options) ⇒ Object

only used by form on admin panel (protected) return array of failed_fields and full_fields [[failed fields], [all fields]]



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'app/models/camaleon_cms/custom_field_group.rb', line 50

def add_fields(items, item_options)
  self.fields.where.not(id: items.map{|k, obj| obj['id'] }.uniq).destroy_all
  cache_fields, order_index, errors_saved = [], 0, []
  if items.present?
    items.each do |i,item|
      item[:field_order] = order_index
      options = item_options[i] || {}
      if item[:id].present? && (field_item = self.fields.where(id: item[:id]).first).present?
        saved = field_item.update(item)
        cache_fields << field_item
      else
        field_item = self.fields.new(item)
        cache_fields << field_item
        saved = field_item.save
        auto_save_default_values(field_item, options) if saved
        errors_saved << field_item unless saved
      end
      if saved
        field_item.set_meta('_default', options)
        order_index += 1
      end
    end
  end
  [errors_saved, cache_fields]
end

#add_manual_field(item, options) ⇒ Object Also known as: add_field

add fields to group item:

  • sample: “slug”=>“my_slug”, “description”=>“my description (optional)”

  • options (textbox sample): “field_key”:“text_box”,“multiple”:“1”,“required”:“1”,“translate”:“1”

  • field_key (string) | translate (boolean) | default_value (unique value) | default_values (array - multiple values for this field) | label_eval (boolean) | multiple_options (array)

  • multiple_options (used for select, radio and checkboxes ): [Title”, “value”=>“2”, “default”=>“1”, “value”=>“3”]

  • label_eval: (Boolean, default false), true => will evaluate the label and description of current field using (eval(‘my_label’)) to have translatable|dynamic labels

****** check all options for each case in Admin::CustomFieldsHelper **** SAMPLE: my_model.add_field(Title”, “slug”=>“subtitle”, “translate”=>true, default_value: “Get in Touch”)



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

def add_manual_field(item, options)
  c = get_field(item[:slug] || item["slug"])
  return c if c.present?

  field_item = self.fields.new(item)
  if field_item.save
    field_item.set_options(options)
    auto_save_default_values(field_item, options)
  end
  field_item
end

#get_captionObject

generate the caption for this group



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'app/models/camaleon_cms/custom_field_group.rb', line 77

def get_caption
  caption = ""
  begin
    case self.object_class
      when "PostType_Post"
        caption = "Fields for Contents in <b>#{self.site.post_types.find(self.objectid).decorate.the_title}</b>"
      when 'PostType_Category'
        caption = "Fields for Categories in <b>#{self.site.post_types.find(self.objectid).decorate.the_title}</b>"
      when 'PostType_PostTag'
        caption = "Fields for Post tags in <b>#{self.site.post_types.find(self.objectid).decorate.the_title}</b>"
      when 'Widget::Main'
        caption = "Fields for Widget <b>(#{CamaleonCms::Widget::Main.find(self.objectid).name.translate})</b>"
      when 'Theme'
        caption = "Field settings for Theme <b>(#{self.site.themes.find(self.objectid).name rescue self.objectid})</b>"
      when 'NavMenu'
        caption = "Field settings for Menus <b>(#{CamaleonCms::NavMenu.find(self.objectid).name})</b>"
      when 'Site'
        caption = "Field settings the site"
      when 'PostType'
        caption = "Fields for all <b>Post_Types</b>"
      when 'Post'
        p = CamaleonCms::Post.find(self.objectid).decorate
        caption = "Fields for content <b>(#{p.the_title})</b>"
      else # 'Plugin' or other class
        caption = "Fields for <b>#{self.object_class}</b>"
    end
  rescue => e
    Rails.logger.info "----------#{e.message}----#{self.attributes}"
  end
  caption
end

#get_field(slug) ⇒ Object

return a field with slug = slug from current group



44
45
46
# File 'app/models/camaleon_cms/custom_field_group.rb', line 44

def get_field(slug)
  self.fields.where(slug: slug).first
end