Class: ApplicationPolicy

Inherits:
Object
  • Object
show all
Defined in:
app/policies/application_policy.rb

Defined Under Namespace

Classes: OwnerScope, Scope, UpdateScope

Constant Summary collapse

STANDARD_VIEW_PERMISSIONS =
["preview", "view", "edit", "own"]
STANDARD_EDIT_PERMISSIONS =
["edit", "own"]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user, record) ⇒ ApplicationPolicy

Returns a new instance of ApplicationPolicy.



9
10
11
12
# File 'app/policies/application_policy.rb', line 9

def initialize(user, record)
  @user = user
  @record = record
end

Instance Attribute Details

#record ⇒ Object (readonly)

Returns the value of attribute record.



4
5
6
# File 'app/policies/application_policy.rb', line 4

def record
  @record
end

#user ⇒ Object (readonly)

Returns the value of attribute user.



4
5
6
# File 'app/policies/application_policy.rb', line 4

def user
  @user
end

Instance Method Details

#create? ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
# File 'app/policies/application_policy.rb', line 25

def create?
  user&.is_contributor?
end

#destroy? ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
50
51
52
53
54
55
56
57
# File 'app/policies/application_policy.rb', line 47

def destroy?
  all_of(
    one_of(
      user&.is_moderator?,
      check_permissions(record, STANDARD_EDIT_PERMISSIONS, user, role_fallback: :moderator)
    ),
    none_of(
      SiteSettings.demo_mode_enabled?
    )
  )
end

#destroy_all? ⇒ Boolean

Returns:

  • (Boolean)


59
60
61
# File 'app/policies/application_policy.rb', line 59

def destroy_all?
  user&.is_administrator?
end

#edit? ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
# File 'app/policies/application_policy.rb', line 43

def edit?
  update?
end

#index? ⇒ Boolean

Returns:

  • (Boolean)


14
15
16
# File 'app/policies/application_policy.rb', line 14

def index?
  show?
end

#new? ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
# File 'app/policies/application_policy.rb', line 29

def new?
  create?
end

#show? ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
21
22
23
# File 'app/policies/application_policy.rb', line 18

def show?
  one_of(
    user&.is_moderator?,
    check_permissions(record, STANDARD_VIEW_PERMISSIONS, user, role_fallback: :member)
  )
end

#update? ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
36
37
38
39
40
41
# File 'app/policies/application_policy.rb', line 33

def update?
  all_of(
    record.respond_to?(:local?) ? record.local? : true,
    one_of(
      user&.is_moderator?,
      check_permissions(record, STANDARD_EDIT_PERMISSIONS, user, role_fallback: :moderator)
    )
  )
end