Class: Notee::Category

Inherits:
ApplicationRecord show all
Defined in:
app/models/notee/category.rb

Instance Method Summary collapse

Methods inherited from ApplicationRecord

#create_authority, #destroy_authority, #is_destroy?, #skip_callback_block, #update_authority

Instance Method Details

#delete_parent_idObject



62
63
64
65
66
67
68
69
70
# File 'app/models/notee/category.rb', line 62

def delete_parent_id
  return false if self.children.nil?

  skip_callback_block(Category) do
    self.children.each do |child|
      child.update(parent_id: nil)
    end
  end
end

#delete_post_category_idObject



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

def 
  return false if self.posts.nil?

  skip_callback_block(Category) do
    self.posts.each do |post|
      post.update(category_id: 1)
    end
  end
end

#protect_defaultObject



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

def protect_default
  raise if self.id == 1
end

#recursive_children_loop(arr, childs_arr) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'app/models/notee/category.rb', line 44

def recursive_children_loop(arr, childs_arr)
  childs_arr.each do |category|
    if category.children.present?
      recursive_children_loop(arr, category.children)
    end

    arr.push(category.id)
  end
end

#restrict_parent_ids(cate_id) ⇒ Object



35
36
37
38
39
40
41
42
# File 'app/models/notee/category.rb', line 35

def restrict_parent_ids(cate_id)
  cate = Category.find(cate_id)
  arr = [cate.id]
  return arr if cate.children.nil?

  recursive_children_loop(arr, cate.children)
  arr
end

#restrict_set_parent_idObject



29
30
31
32
33
# File 'app/models/notee/category.rb', line 29

def restrict_set_parent_id
  if self.id
    raise if restrict_parent_ids(self.id).include?(self.parent_id)
  end
end

#set_slugObject



54
55
56
# File 'app/models/notee/category.rb', line 54

def set_slug
  self.slug = self.name.downcase unless self.slug.present?
end