post_policy.rb 680 Bytes
Newer Older
dgt's avatar
dgt committed
1 2 3
class PostPolicy < ApplicationPolicy

  def create?
azul's avatar
azul committed
4
    logged_in? && (page_policy.show? || page.public?)
dgt's avatar
dgt committed
5 6 7
  end

  def update?
8
    post &&
9
      post.user_id == user.id
dgt's avatar
dgt committed
10 11
  end

12 13 14
  def destroy?
    update? || (admin? && comment_by_visitor_on_public_page?)
  end
dgt's avatar
dgt committed
15 16

  def twinkle?
17 18 19
    page &&
      page_policy.show? &&
      user.id &&
20
      user.id != post.user_id
dgt's avatar
dgt committed
21 22 23 24
  end

  protected

25
  def admin?
26
    page && page_policy.admin?
27 28 29 30 31 32 33 34
  end

  def comment_by_visitor_on_public_page?
    page &&
      page.public? &&
      !post.user.may?(:view, page)
  end

dgt's avatar
dgt committed
35 36 37 38
  def page_policy
    Pundit.policy!(user, page)
  end

39 40 41 42
  def post
    record
  end

dgt's avatar
dgt committed
43
  def page
44
    post.discussion.page
dgt's avatar
dgt committed
45 46
  end
end