nathanl / authority

*CURRENTLY UNMAINTAINED*. Authority helps you authorize actions in your Rails app. It's ORM-neutral and has very little fancy syntax; just group your models under one or more Authorizer classes and write plain Ruby methods on them.
MIT License
1.21k stars 67 forks source link

undefined local variable or method `resource' for SchoolAuthorizer:Class #34

Closed shlomizadok closed 11 years ago

shlomizadok commented 11 years ago

Hi, I try using authority (great gem and concept). I have followed the readme. When I try to check an instance, I get the following error:

undefined local variable or method `resource' for SchoolAuthorizer:Class

My Group model looks like:

class Group < ActiveRecord::Base

  resourcify
  include Authority::Abilities
  self.authorizer_name = 'SchoolAuthorizer'
  ...

My GroupController looks like:

class GroupsController < ApplicationController
  authorize_actions_for Group
  ...
  def show
    @group = Group.find(params[:id])
    authorize_action_for @group
    @users = @group.users.where("first_name ilike :name OR last_name ilike :name", {:name => "%%#{@q}%%"}).paginate(:page => params[:page], :per_page => params[:limit] || Settings.users_per_page)
    @title = @group.name
  end

And my SchoolAuthorizer:

class SchoolAuthorizer < ApplicationAuthorizer
  def self.creatable_by?(user)
    user.has_role?("admin")
  end

  def self.readable_by?(user)
    true
  end

  def self.updatable_by?(user)
    user.has_role?("admin", resource)
  end
end

And I get the error above (undefined local variable or method `resource' for SchoolAuthorizer:Class ) What am I doing wrong? Thanks for helping

adamhunter commented 11 years ago

Hey @shlomizadok,

resource isn't available in the class methods (as it get's passed in when creating in instance). If change your updatable_by? method to an instance method (remove the self. it) you'll be able to use that code for authorizing resources. Let me know if that helps.

Thanks!

shlomizadok commented 11 years ago

@adamhunter -- Works like charm! Thank you so much!