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_multiple_options, #set_option

Instance Method Details

#add_fields(items, item_options) ⇒ Object

only used by form on admin panel (protected)



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

def add_fields(items, item_options)
  ids_old = self.fields.pluck("#{CamaleonCms::CustomField.table_name}.id")
  ids_saved = []
  order_index = 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.find(item[:id])
        saved = field_item.update(item)
      else
        field_item = self.fields.new(item)
        saved = field_item.save
        auto_save_default_values(field_item, options) if saved
      end
      if saved
        field_item.set_meta('_default', options)
        ids_saved << field_item.id
        order_index += 1
      end
    end
  end
  ids_deletes = ids_old - ids_saved
  self.fields.where(id: ids_deletes).destroy_all if ids_deletes.any?
end

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

check all options for each case in Admin::CustomFieldsHelper for select, radio and checkboxes add: – multiple_options: [Title”, “value”=>“2”, “default”=>“1”, “value”=>“3”] – add default for default value SAMPLE: my_model.add_field(Title”, “slug”=>“subtitle”, “translate”=>true, default_value: “Get in Touch”)



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

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

  field_item = self.fields.create!(item)
  field_item.set_meta('_default', options)
  auto_save_default_values(field_item, options)
  field_item
end

#get_captionObject

generate the caption for this group



76
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
# File 'app/models/camaleon_cms/custom_field_group.rb', line 76

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



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

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