Forum: Ruby on Rails gem CanCan help

Posted by Kad Kerforn (kadoudal)
on 2012-11-11 13:46
(Received via mailing list)
I defined a block in my ability model

    elsif admin.has_role? :manager
      can [:update, :destroy, :lock, :unlock, :reset_passord], Admin do
|another_admin|
         another_admin.is? :employee
      end

but it seems not to be taken in account when testing it :  @manager.is?
 :manager true,  @employee.is? :employee true

  test "manager_should_be_able_to_reset_employee_password" do
    sign_in  @manager
    ability = BackofficeAbility.new(@manager)
    debugger
    assert ability.can? :reset_password, @employee

looking into the ability instance , I don't see anything related
to another_admin.is? :employee condition ...  => @conditions={},

#<CanCan::Rule:0x007fd92aed1718 @match_all=false, @base_behavior=true,
@actions=[:update, :destroy, :lock, :unlock, :reset_passord],
@subjects=[Admin(id: integer, email: string, encrypted_password: string,
reset_password_token: string, reset_password_sent_at: datetime,
remember_created_at: datetime, sign_in_count: integer, 
current_sign_in_at:
datetime, last_sign_in_at: datetime, current_sign_in_ip: string,
last_sign_in_ip: string, failed_attempts: integer, unlock_token: string,
locked_at: datetime, created_at: datetime, updated_at: datetime)],
@conditions={},
@block=#<Proc:0x007fd92aed1740@/Users/yves/github/local/yoogroop/app/models/backoffice_ability.rb:26>,
@expanded_actions=[:update, :edit, :destroy, :lock, :unlock,
:reset_passord]>,

what's wrong with my block ?
thanks for help
Posted by Kad Kerforn (kadoudal)
on 2012-11-11 18:13
(Received via mailing list)
[SOLVED.. I guess]

as per CanCan wiki ,
 https://github.com/ryanb/cancan/wiki/Defining-Abil...

I passed an Admin  scope

Admin class
  scope :employees, Admin.joins(:roles).where(:roles => { :name =>
"employee", :resource_type => nil}).select("DISTINCT admins.*")

in Ability model

if admin.has_role? :manager
       can [:manage, :destroy, :lock, :unlock, :reset_passord],
Admin.employees


Le dimanche 11 novembre 2012 13:45:53 UTC+1, Erwin a crit :
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.