Class: PostCategory

Inherits:
ApplicationRecord show all
Includes:
Checkable, NestedPriority, Toggleable
Defined in:
app/models/post_category.rb

Overview

Post category

Attributes:

children_cache [Array<Integer>]
created_at [DateTime]
meta_description [String], optional
name [String]
nav_text [String], optional
parent_id [PostCategory], optional
parents_cache [String]
post_type_id [PostType]
priority [Integer]
slug [String]
updated_at [DateTime]
visible [Boolean]

Constant Summary collapse

META_LIMIT =
250
NAME_LIMIT =
100
SLUG_LIMIT =
100
SLUG_PATTERN =
/\A[a-z][-_0-9a-z]*[0-9a-z]\z/i.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.creation_parametersObject



62
63
64
# File 'app/models/post_category.rb', line 62

def self.creation_parameters
  entity_parameters + %i[parent_id post_type_id]
end

.entity_parametersObject



58
59
60
# File 'app/models/post_category.rb', line 58

def self.entity_parameters
  %i[meta_description name nav_text slug priority visible]
end

.list_for_tree(post_type_id) ⇒ Object

Parameters:

  • post_type_id (Integer)


72
73
74
75
76
77
78
79
80
81
# File 'app/models/post_category.rb', line 72

def self.list_for_tree(post_type_id)
  buffer = {}
  where(post_type_id: post_type_id).ordered_by_priority.each do |item|
    buffer[item.id] = {
      parent_id: item.parent_id,
      item: item
    }
  end
  buffer
end

.siblings(entity) ⇒ Object

Parameters:



67
68
69
# File 'app/models/post_category.rb', line 67

def self.siblings(entity)
  where(post_type_id: entity.post_type_id, parent_id: entity.parent_id)
end

Instance Method Details

#add_post(post) ⇒ Object

Parameters:



135
136
137
# File 'app/models/post_category.rb', line 135

def add_post(post)
  post_post_categories.create(post: post)
end

#branch_idsArray<Integer>

Returns:

  • (Array<Integer>)


96
97
98
# File 'app/models/post_category.rb', line 96

def branch_ids
  parents_cache.split(',').map(&:to_i).reject { |i| i < 1 }.uniq + [id]
end

#cache_children!Object



118
119
120
121
122
123
# File 'app/models/post_category.rb', line 118

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



111
112
113
114
115
116
# File 'app/models/post_category.rb', line 111

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)


125
126
127
# File 'app/models/post_category.rb', line 125

def can_be_deleted?
  !locked? && child_categories.count < 1
end

#depthObject



87
88
89
# File 'app/models/post_category.rb', line 87

def depth
  parent_ids.count
end

#full_titleObject



83
84
85
# File 'app/models/post_category.rb', line 83

def full_title
  (parents.pluck(:name) + [name]).join ' / '
end

#parent_idsObject



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

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

#parentsObject



105
106
107
108
109
# File 'app/models/post_category.rb', line 105

def parents
  return [] if parents_cache.blank?

  PostCategory.where(id: parent_ids).order('id asc')
end

#post?(post) ⇒ Boolean

Parameters:

Returns:

  • (Boolean)


130
131
132
# File 'app/models/post_category.rb', line 130

def post?(post)
  post_post_categories.where(post: post).exists?
end

#remove_post(post) ⇒ Object

Parameters:



140
141
142
# File 'app/models/post_category.rb', line 140

def remove_post(post)
  post_post_categories.where(post: post).delete_all
end

#subbranch_idsArray<Integer>

Returns:

  • (Array<Integer>)


101
102
103
# File 'app/models/post_category.rb', line 101

def subbranch_ids
  [id] + children_cache
end


150
151
152
# File 'app/models/post_category.rb', line 150

def text_for_link
  nav_text.blank? ? name : nav_text
end

#url(locale = I18n.default_locale) ⇒ Object

Parameters:

  • locale (Symbol) (defaults to: I18n.default_locale)


145
146
147
148
# File 'app/models/post_category.rb', line 145

def url(locale = I18n.default_locale)
  prefix = locale.nil? || locale == I18n.default_locale ? '' : "/#{locale}"
  "#{prefix}/#{post_type.url_part}/#{long_slug}"
end