1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-12-11 22:24:21 +01:00
fab-manager/app/policies/project_policy.rb

26 lines
796 B
Ruby
Raw Normal View History

2015-05-05 03:10:25 +02:00
class ProjectPolicy < ApplicationPolicy
class Scope < Scope
def resolve
if user
2019-06-06 16:34:53 +02:00
statistic_profile = StatisticProfile.find_by(user_id: user.id)
2015-05-05 03:10:25 +02:00
scope.includes(:project_image, :machines, :users)
2019-06-06 16:34:53 +02:00
.where("state = 'published' OR (state = 'draft' AND (author_statistic_profile_id = ? OR users.id = ?))", statistic_profile.id, user.id)
.references(:users)
.order(created_at: :desc)
2015-05-05 03:10:25 +02:00
else
scope.includes(:project_image, :machines, :users)
2019-06-06 16:34:53 +02:00
.where("state = 'published'")
.order(created_at: :desc)
2015-05-05 03:10:25 +02:00
end
end
end
def update?
2019-06-06 16:34:53 +02:00
user.admin? or record.author.user_id == user.id or record.users.include?(user)
2015-05-05 03:10:25 +02:00
end
def destroy?
2019-06-06 16:34:53 +02:00
user.admin? or record.author.user_id == user
2015-05-05 03:10:25 +02:00
end
end