Class: PostCategory
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- PostCategory
- Includes:
- Toggleable
- Defined in:
- app/models/post_category.rb
Constant Summary collapse
- PRIORITY_RANGE =
(1..100)
- NAME_LIMIT =
50- SLUG_LIMIT =
50- SLUG_PATTERN =
/\A[a-z][-0-9a-z]*[0-9a-z]\z/i
Class Method Summary collapse
Instance Method Summary collapse
- #branch_ids ⇒ Array<Integer>
- #cache_children! ⇒ Object
- #cache_parents! ⇒ Object
- #can_be_deleted? ⇒ Boolean
- #change_priority(delta) ⇒ Object
- #depth ⇒ Object
- #full_title ⇒ Object
- #has_post?(post) ⇒ Boolean
- #parent_ids ⇒ Object
- #parents ⇒ Object
- #subbranch_ids ⇒ Array<Integer>
Class Method Details
.creation_parameters ⇒ Object
43 44 45 |
# File 'app/models/post_category.rb', line 43 def self.creation_parameters entity_parameters + %i(parent_id post_type_id) end |
.entity_parameters ⇒ Object
39 40 41 |
# File 'app/models/post_category.rb', line 39 def self.entity_parameters %i(name slug priority visible) end |
Instance Method Details
#branch_ids ⇒ Array<Integer>
60 61 62 |
# File 'app/models/post_category.rb', line 60 def branch_ids parents_cache.split(',').map(&:to_i).reject { |i| i < 1 }.uniq + [id] end |
#cache_children! ⇒ Object
80 81 82 83 84 85 |
# File 'app/models/post_category.rb', line 80 def cache_children! child_categories.order('id asc').each do |child| self.children_cache += [child.id] + child.children_cache end save! end |
#cache_parents! ⇒ Object
74 75 76 77 78 |
# File 'app/models/post_category.rb', line 74 def cache_parents! return if parent.nil? self.parents_cache = "#{parent.parents_cache},#{parent_id}".gsub(/\A,/, '') save! end |
#can_be_deleted? ⇒ Boolean
87 88 89 |
# File 'app/models/post_category.rb', line 87 def can_be_deleted? !locked? && child_categories.count < 1 end |
#change_priority(delta) ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 |
# File 'app/models/post_category.rb', line 97 def change_priority(delta) new_priority = priority + delta criteria = { post_type_id: post_type_id, parent_id: parent_id, priority: new_priority } adjacent = self.class.find_by(criteria) if adjacent.is_a?(self.class) && (adjacent.id != id) adjacent.update!(priority: priority) end self.update(priority: new_priority) self.class.for_tree(post_type_id, parent_id).map { |e| [e.id, e.priority] }.to_h end |
#depth ⇒ Object
51 52 53 |
# File 'app/models/post_category.rb', line 51 def depth parent_ids.count end |
#full_title ⇒ Object
47 48 49 |
# File 'app/models/post_category.rb', line 47 def full_title (parents.map { |parent| parent.name } + [name]).join ' / ' end |
#has_post?(post) ⇒ Boolean
92 93 94 |
# File 'app/models/post_category.rb', line 92 def has_post?(post) post.post_category == self end |
#parent_ids ⇒ Object
55 56 57 |
# File 'app/models/post_category.rb', line 55 def parent_ids parents_cache.split(',').compact end |
#parents ⇒ Object
69 70 71 72 |
# File 'app/models/post_category.rb', line 69 def parents return [] if parents_cache.blank? PostCategory.where(id: parent_ids).order('id asc') end |
#subbranch_ids ⇒ Array<Integer>
65 66 67 |
# File 'app/models/post_category.rb', line 65 def subbranch_ids [id] + children_cache end |