Class: CamaleonCms::PostType

Inherits:
TermTaxonomy
  • Object
show all
Defined in:
app/models/camaleon_cms/post_type.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 inherited from TermTaxonomy

#children, #in_nav_menu_items, #set_options_from_form, #skip_slug_validation?

Methods included from CustomFieldsRead

#add_custom_field_group, #add_custom_field_to_default_group, #get_field!, #get_field_groups, #get_field_object, #get_field_value, #get_field_values, #get_field_values_hash, #get_fields_object, #get_user_field_groups, #save_field_value, #set_field_values, #update_field_value

Methods included from Metas

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

Instance Method Details

#add_post(args) ⇒ Object

add a post for current model

title: title for post,    => required
content: html text content, => required
thumb: image url, => default (empty). check http://camaleon.tuzitio.com/api-methods.html#section_fileuploads
categories: [1,3,4,5],    => default (empty)
tags: String comma separated, => default (empty)
slug: string key for post,    => default (empty)
summary: String resume (optional)  => default (empty)
post_order: Integer to define the order position in the list (optional)
fields: Hash of values for custom fields, sample => fields: {subtitle: 'abc', icon: 'test' } (optional)
settings: Hash of post settings, sample => settings:
  {has_content: false, has_summary: true, default_layout: 'my_layout', default_template: 'my_template' } (optional, see more in post.set_setting(...))
data_metas: {template: "", layout: ""}

return created post if it was created, else return errors


107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'app/models/camaleon_cms/post_type.rb', line 107

def add_post(args)
  _fields = args.delete(:fields)
  _settings = args.delete(:settings)
  _summary = args.delete(:summary)
  _order_position = args.delete(:order_position)
  _categories = args.delete(:categories)
  _tags = args.delete(:tags)
  _thumb = args.delete(:thumb)
  p = self.posts.new(args)
  p.slug = self.site.get_valid_post_slug(p.title.parameterize) unless p.slug.present?
  if p.save!
    _settings.each{ |k, v| p.set_setting(k, v) } if _settings.present?
    p.assign_category(_categories) if _categories.present? && self.manage_categories?
    p.assign_tags(_tags) if _tags.present? && self.manage_tags?
    p.set_position(_order_position) if _order_position.present?
    p.set_summary(_summary) if _summary.present?
    p.set_thumb(_thumb) if _thumb.present?
    _fields.each{ |k, v| p.save_field_value(k, v) } if _fields.present?
    return p.decorate
  else
    p.errors
  end
end

#contents_route_formatObject

return the configuration of routes for post contents


143
144
145
# File 'app/models/camaleon_cms/post_type.rb', line 143

def contents_route_format
  get_option("contents_route_format", "post")
end

#contents_route_formatsObject

return all available route formats of this post type for content posts


132
133
134
135
136
137
138
139
140
# File 'app/models/camaleon_cms/post_type.rb', line 132

def contents_route_formats
  {
    "post_of_post_type" => "<code>/group/:post_type_id-:title/:slug</code><br>  (Sample: http://localhost.com/group/17-services/myservice.html)",
    "post_of_category" => "<code>/category/:category_id-:title/:slug</code><br>  (Sample: http://localhost.com/category/17-services/myservice.html)",
    "post_of_category_post_type" => "<code>/:post_type_title/category/:category_id-:title/:slug</code><br>  (Sample: http://localhost.com/services/category/17-services/myservice.html)",
    "post_of_posttype" => "<code>/:post_type_title/:slug</code><br>  (Sample: http://localhost.com/services/myservice.html)",
    "post" => "<code>/:slug</code><br>  (Sample: http://localhost.com/myservice.html)"
  }
end

#default_categoryObject

return default category for this post type only return a category for post types that manage categories


82
83
84
85
86
87
88
89
90
91
# File 'app/models/camaleon_cms/post_type.rb', line 82

def default_category
  if manage_categories?
    cat = self.categories.find_by_slug("uncategorized")
    unless cat.present?
      cat = self.categories.create({name: 'Uncategorized', slug: 'uncategorized', parent: self.id})
      cat.set_option("not_deleted", true)
    end
    cat
  end
end

#full_categoriesObject

select full_categories for the post type, include all children categories


75
76
77
78
# File 'app/models/camaleon_cms/post_type.rb', line 75

def full_categories
  s = self.site
  CamaleonCms::Category.where("term_group = ? or status in (?)", s.id, s.post_types.pluck(:id).to_s)
end

#manage_categories?Boolean

check if current post type manage categories


28
29
30
# File 'app/models/camaleon_cms/post_type.rb', line 28

def manage_categories?
  options[:has_category]
end

#manage_tags?Boolean

check if this post type manage post tags


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

def manage_tags?
  options[:has_tags]
end

#set_setting(key, value) ⇒ Object

set or update a setting for this post type


70
71
72
# File 'app/models/camaleon_cms/post_type.rb', line 70

def set_setting(key, value)
  self.set_option(key, value)
end

#set_settings(settings = {}) ⇒ Object

assign settings for this post type default values:

has_category: false,
has_tags: false,
has_summary: true,
has_content: true,
has_comments: false,
has_picture: true,
has_template: true,
has_keywords: true,
not_deleted: false,
has_layout: false,
default_layout: '',
contents_route_format: 'post'


63
64
65
66
67
# File 'app/models/camaleon_cms/post_type.rb', line 63

def set_settings(settings = {})
  settings.each do |key, val|
    self.set_option(key, val)
  end
end

#show_for_admin_menu?Boolean

check if this post type is shown on admin -> contents -> menu


39
40
41
# File 'app/models/camaleon_cms/post_type.rb', line 39

def show_for_admin_menu?
  self.term_group == nil
end

#toggle_show_for_admin_menu(flag) ⇒ Object

hide or show this post type on admin -> contents -> menu true => enable, false => disable


34
35
36
# File 'app/models/camaleon_cms/post_type.rb', line 34

def toggle_show_for_admin_menu(flag)
  self.update(term_group: flag == true ? nil : -1)
end