tap/app/models/ability.rb

36 lines
752 B
Ruby
Raw Normal View History

2014-12-09 19:44:58 +01:00
class Ability
include CanCan::Ability
def initialize(user)
2015-09-14 20:26:16 +02:00
return unless user
initialize_admin if user.admin?
2015-10-29 14:09:52 +01:00
initialize_koelkast if user.koelkast?
2015-10-29 14:27:06 +01:00
initialize_user(user)
2015-10-29 14:09:52 +01:00
2015-10-07 15:52:05 +02:00
can :read, Barcode
2015-10-29 14:09:52 +01:00
end
def initialize_admin
can :manage, :all
end
2015-09-20 21:21:18 +02:00
2015-10-29 14:09:52 +01:00
def initialize_koelkast
can :manage, Order do |order|
2016-02-29 11:27:05 +01:00
!order.try(:user).try(:private) && order.try(:user).try(:balance).try(:>, -500)
2015-10-29 14:09:52 +01:00
end
can :quickpay, User
end
2015-10-29 14:27:06 +01:00
def initialize_user(user)
2015-10-29 14:09:52 +01:00
can :read, :all
can :manage, User, id: user.id
2016-02-04 21:03:03 +01:00
can :create, Order do |order|
2016-02-29 11:27:05 +01:00
order.user == user && user.try(:balance).try(:>, -500)
2016-02-04 21:03:03 +01:00
end
2015-10-29 14:09:52 +01:00
can :destroy, Order do |order|
order.try(:user) == user && order.deletable
2014-12-09 19:44:58 +01:00
end
end
end