module PermissionsActor

Public Instance Methods

method_missing(meth, *args, &block) click to toggle source

taken from: www.trottercashion.com/2011/02/08/rubys-define_method-method_missing-and-instance_eval.html TODO: Write a test

Calls superclass method
# File lib/buweb/concerns/permissions_actor.rb, line 6
def method_missing(meth, *args, &block)
  if meth.to_s =~ /^can_(.+)\?$/
    run_can_method($1, *args, &block)
  else
    # You *must* call super if you don't handle the
    # method, otherwise you'll mess up Ruby's method
    # lookup.
    super
  end
end
run_can_method(ability, object_or_sym, *args, &block) click to toggle source

example: @user.can_edit? @department

# File lib/buweb/concerns/permissions_actor.rb, line 18
def run_can_method(ability, object_or_sym, *args, &block) # TODO: write a test
  # TODO: if object_or_sym is a symbol... then see if user has roles that
  # include the ability; (ie. current_user.can_edit? :departments -- this
  # should check the current_users' roles and see if any of those allow the
  # user to edit the Department class)
  object_or_sym.permissions.by_actor(self).where(ability: ability).count > 0
end