Module: CamaleonCms::CategoriesTagsForPosts

Extended by:
ActiveSupport::Concern
Included in:
Post
Defined in:
app/models/concerns/camaleon_cms/categories_tags_for_posts.rb

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]


58
59
60
61
62
63
64
65
# File 'app/models/concerns/camaleon_cms/categories_tags_for_posts.rb', line 58

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”


78
79
80
81
82
83
84
85
86
# File 'app/models/concerns/camaleon_cms/categories_tags_for_posts.rb', line 78

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)

13
14
15
# File 'app/models/concerns/camaleon_cms/categories_tags_for_posts.rb', line 13

def manage_categories?
  post_type.manage_categories?
end

#manage_tags?Boolean

check if this post can manage tags

Returns:

  • (Boolean)

18
19
20
# File 'app/models/concerns/camaleon_cms/categories_tags_for_posts.rb', line 18

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]


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

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”


90
91
92
93
94
95
# File 'app/models/concerns/camaleon_cms/categories_tags_for_posts.rb', line 90

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]


26
27
28
29
30
31
32
33
34
35
36
37
# File 'app/models/concerns/camaleon_cms/categories_tags_for_posts.rb', line 26

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


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

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”


43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/models/concerns/camaleon_cms/categories_tags_for_posts.rb', line 43

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