Class: CourseCategory
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- CourseCategory
- Includes:
- Toggleable
- Defined in:
- app/models/course_category.rb
Constant Summary collapse
- PRIORITY_RANGE =
(1..500)
- NAME_LIMIT =
50
- SLUG_LIMIT =
50
- SLUG_PATTERN =
/\A[a-z][-0-9a-z]*[0-9a-z]\z/i
- SLUG_PATTERN_HTML =
'^[a-zA-Z][-0-9a-zA-Z]*[0-9a-zA-Z]$'
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
- #course?(course) ⇒ Boolean
- #depth ⇒ Object
- #full_title ⇒ Object
- #image_alt_text ⇒ Object
- #parent_ids ⇒ Object
- #parents ⇒ Object
- #subbranch_ids ⇒ Array<Integer>
- #title ⇒ Object
Class Method Details
.creation_parameters ⇒ Object
43 44 45 |
# File 'app/models/course_category.rb', line 43 def self.creation_parameters entity_parameters + i(parent_id) end |
.entity_parameters ⇒ Object
39 40 41 |
# File 'app/models/course_category.rb', line 39 def self.entity_parameters i(image name slug priority visible) end |
Instance Method Details
#branch_ids ⇒ Array<Integer>
60 61 62 |
# File 'app/models/course_category.rb', line 60 def branch_ids parents_cache.split(',').map(&:to_i).reject { |i| i < 1 }.uniq + [id] end |
#cache_children! ⇒ Object
88 89 90 91 92 93 |
# File 'app/models/course_category.rb', line 88 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
82 83 84 85 86 |
# File 'app/models/course_category.rb', line 82 def cache_parents! return if parent.nil? self.parents_cache = "#{parent.parents_cache},#{parent_id}".gsub(/\A,/, '') save! end |
#can_be_deleted? ⇒ Boolean
95 96 97 |
# File 'app/models/course_category.rb', line 95 def can_be_deleted? child_categories.count < 1 end |
#change_priority(delta) ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 |
# File 'app/models/course_category.rb', line 105 def change_priority(delta) new_priority = priority + delta criteria = { 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(parent_id).map { |e| [e.id, e.priority] }.to_h end |
#course?(course) ⇒ Boolean
100 101 102 |
# File 'app/models/course_category.rb', line 100 def course?(course) course.course_category == self end |
#depth ⇒ Object
51 52 53 |
# File 'app/models/course_category.rb', line 51 def depth parent_ids.count end |
#full_title ⇒ Object
47 48 49 |
# File 'app/models/course_category.rb', line 47 def full_title (parents.map { |parent| parent.name } + [name]).join ' / ' end |
#image_alt_text ⇒ Object
69 70 71 |
# File 'app/models/course_category.rb', line 69 def image_alt_text name end |
#parent_ids ⇒ Object
55 56 57 |
# File 'app/models/course_category.rb', line 55 def parent_ids parents_cache.split(',').compact end |
#parents ⇒ Object
77 78 79 80 |
# File 'app/models/course_category.rb', line 77 def parents return [] if parents_cache.blank? CourseCategory.where(id: parent_ids).order('id asc') end |
#subbranch_ids ⇒ Array<Integer>
65 66 67 |
# File 'app/models/course_category.rb', line 65 def subbranch_ids [id] + children_cache end |
#title ⇒ Object
73 74 75 |
# File 'app/models/course_category.rb', line 73 def title name end |