Module: CamaleonCms::CategoriesTagsForPosts

Extended by:
ActiveSupport::Concern
Included in:
Post
Defined in:
app/models/concerns/camaleon_cms/categories_tags_for_posts.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

Instance Method Details

#assign_category(categories_id) ⇒ Object

Assign this post for category with id: category_id categories_id: (Array) array of category ids assigned for this post, sample: [1,2,3]


66
67
68
69
70
71
72
73
# File 'app/models/concerns/camaleon_cms/categories_tags_for_posts.rb', line 66

def assign_category(categories_id)
  categories_id = [categories_id] if categories_id.is_a?(Integer)
  rescue_extra_data
  categories_id.each do |key|
    term_relationships.where(:term_taxonomy_id => key).first_or_create!
  end
  update_counters("categories")
end

#assign_tags(tag_titles) ⇒ Object

Assign new tags to this post tags_title: (String) tags name separated by commas, sample: “Tag1,Tag two,tag new”


86
87
88
89
90
91
92
93
94
# File 'app/models/concerns/camaleon_cms/categories_tags_for_posts.rb', line 86

def assign_tags(tag_titles)
  update_counters_before
  tags = tag_titles.split(",").strip
  tags.each do |t|
     = self.post_type..where(name: t).first_or_create!
    self.term_relationships.where({term_taxonomy_id: .id}).first_or_create!
  end
  update_counters("tags")
end

#manage_categories?Boolean

check if this post can manage categories

Returns:

  • (Boolean)

21
22
23
# File 'app/models/concerns/camaleon_cms/categories_tags_for_posts.rb', line 21

def manage_categories?
  post_type.manage_categories?
end

#manage_tags?Boolean

check if this post can manage tags

Returns:

  • (Boolean)

26
27
28
# File 'app/models/concerns/camaleon_cms/categories_tags_for_posts.rb', line 26

def manage_tags?
  post_type.manage_tags?
end

#unassign_category(categories_id) ⇒ Object

Assign this post for category with id: category_id categories_id: (Array) array of category ids assigned for this post, sample: [1,2,3]


77
78
79
80
81
82
# File 'app/models/concerns/camaleon_cms/categories_tags_for_posts.rb', line 77

def unassign_category(categories_id)
  categories_id = [categories_id] if categories_id.is_a?(Integer)
  rescue_extra_data
  term_relationships.where(:term_taxonomy_id => categories_id).destroy_all
  update_counters("categories")
end

#unassign_tags(tag_titles) ⇒ Object

Unassign tags from this post tags_title: (String) tags name separated by commas, sample: “Tag1,Tag two,tag new”


98
99
100
101
102
103
# File 'app/models/concerns/camaleon_cms/categories_tags_for_posts.rb', line 98

def unassign_tags(tag_titles)
  update_counters_before
  tags = tag_titles.split(",").strip
  self.term_relationships.where({term_taxonomy_id: self.post_type..where(name: tags).pluck(:id)}).destroy_all
  update_counters("tags")
end

#update_categories(cats = []) ⇒ Object

cats: (Array) array of category ids assigned for this post, sample: [1,2,3]


34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/models/concerns/camaleon_cms/categories_tags_for_posts.rb', line 34

def update_categories(cats=[])
  rescue_extra_data
  cats = cats.to_i
  old_categories = categories.pluck("#{CamaleonCms::TermTaxonomy.table_name}.id")
  delete_categories = old_categories - cats
  news_categories =  cats - old_categories
  term_relationships.where("term_taxonomy_id in (?)", delete_categories ).destroy_all   if  delete_categories.present?
  news_categories.each do |key|
    term_relationships.create(:term_taxonomy_id => key)
  end
  update_counters("categories")
end

#update_extra_dataObject

update quantity of posts assigned for tags and categories assigned to this post


106
107
108
109
# File 'app/models/concerns/camaleon_cms/categories_tags_for_posts.rb', line 106

def update_extra_data
  rescue_extra_data
  update_counters
end

#update_tags(tags) ⇒ Object

tags: (String) tags name separated by commas, sample: “Tag1,Tag two,tag new”


51
52
53
54
55
56
57
58
59
60
61
62
# File 'app/models/concerns/camaleon_cms/categories_tags_for_posts.rb', line 51

def update_tags(tags)
  rescue_extra_data
  tags = tags.split(",").strip
   = self.post_type.
   = .where("name not in (?)", tags) if tags.present?
  self.term_relationships.where("term_taxonomy_id in (?)", .pluck("#{CamaleonCms::TermTaxonomy.table_name}.id")).destroy_all
  tags.each do |f|
     = self.post_type..where({name: f}).first_or_create(slug: f.parameterize)
    self.term_relationships.where({term_taxonomy_id: .id}).first_or_create
  end
  update_counters("tags")
end