Class: PostCategory

Inherits:
ApplicationRecord show all
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

Class Method Details

.creation_parametersObject



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_parametersObject



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_idsArray<Integer>

Returns:

  • (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

Returns:

  • (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

Parameters:

  • delta (Integer)


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

#depthObject



51
52
53
# File 'app/models/post_category.rb', line 51

def depth
  parent_ids.count
end

#full_titleObject



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

Parameters:

Returns:

  • (Boolean)


92
93
94
# File 'app/models/post_category.rb', line 92

def has_post?(post)
  post. == self
end

#parent_idsObject



55
56
57
# File 'app/models/post_category.rb', line 55

def parent_ids
  parents_cache.split(',').compact
end

#parentsObject



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_idsArray<Integer>

Returns:

  • (Array<Integer>)


65
66
67
# File 'app/models/post_category.rb', line 65

def subbranch_ids
  [id] + children_cache
end