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]



61
62
63
64
65
66
67
68
# File 'app/models/concerns/camaleon_cms/categories_tags_for_posts.rb', line 61

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”



81
82
83
84
85
86
87
88
89
# File 'app/models/concerns/camaleon_cms/categories_tags_for_posts.rb', line 81

def assign_tags(tag_titles)
  update_counters_before
  tags = tag_titles.split(',').strip
  tags.each do |t|
     = post_type..where(name: t).first_or_create!
    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)


15
16
17
# File 'app/models/concerns/camaleon_cms/categories_tags_for_posts.rb', line 15

def manage_categories?
  post_type.manage_categories?
end

#manage_tags?Boolean

check if this post can manage tags

Returns:

  • (Boolean)


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

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]



72
73
74
75
76
77
# File 'app/models/concerns/camaleon_cms/categories_tags_for_posts.rb', line 72

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”



93
94
95
96
97
98
# File 'app/models/concerns/camaleon_cms/categories_tags_for_posts.rb', line 93

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

#update_categories(cats = []) ⇒ Object

update category assignations for this post remove assignations that no longer exist add new assignations cats: (Array) array of category ids assigned for this post, sample: [1,2,3]



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

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



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

def update_extra_data
  rescue_extra_data
  update_counters
end

#update_tags(tags) ⇒ Object

update tags assignations for this post remove assignations that no longer exist add new assignations tags: (String) tags name separated by commas, sample: “Tag1,Tag two,tag new”



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

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