2013-05-09 3 views
0

Я пытаюсь создать систему, в которой есть пользователи, у которых есть пользователи. Пока нет проблем. Я создаю две роли: служащий и: босс, у босса может быть много сотрудников, а у сотрудника должен быть один босс.Rails cancan пользователь, у которого много пользователей

Я использую devise, cancan, mongoid в приложении rails, проблема заключается в настройке файла capability.rb, я думаю, что все в порядке, но когда я пытаюсь проверить его на rspec, он терпит неудачу.

ability.rb

class Ability 
    include CanCan::Ability 

    def initialize(user) 
    user ||= User.new # guest user (not log in) 

    alias_action :read, :create, :update, :destroy, :to => :crud 

    if user.role? == :boss 
     can :crud, User do |people| 
     user.users.include?(people) 
     end 
    end 
    end 
end 

И файл спецификации:

require 'cancan' 
require 'cancan/matchers' 
require_relative '../../app/models/ability.rb' 
require 'spec_helper' 

describe Ability do 
    let(:user) { FactoryGirl.create(:user) } 

    describe "role user system" do 
    context "when there are a boss" do 

     before(:each)do 
     @user = user 
     @ability = Ability.new(@user) 
     @emplo = FactoryGirl.create(:user, :role => :employee, :user => @user) 
     end 

     it "allows to crud employees" do 
     @ability.should be_able_to(:crud, @emplo) 
     end 

     it "denies a non boss user to manage employees" do 
     ability1 = Ability.new(FactoryGirl.create(:user, :role => :employee)) 
     ability1.should_not be_able_to(:manage, @emplo) 
     end 
    end 
    end 
end 

Завод создать пользователя в качестве босса по умолчанию. Я запустил тест и получил:

Ability 
    role user system 
    when there are a boss 
     allows to crud employees (FAILED - 1) 
     denies a non boss user to manage employees 

Failures: 

    1) Ability role user system when there are a boss allows to crud employees 
    Failure/Error: @ability.should be_able_to(:crud, @emplo) 
     expected to be able to :crud #<User _id: 518b5a860009d6307f000002, _type: nil, 
     email: "[email protected]", encrypted_password: "$2a$04$OJeSUDPFf223r79jJP6odeBcuRoBJa1y0u3omOnAsE7SUyPwtgdmW", 
     reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, 
     sign_in_count: 0, current_sign_in_at: nil, last_sign_in_at: nil, current_sign_in_ip: nil, 
     last_sign_in_ip: nil, confirmation_token: nil, confirmed_at: 2013-05-09 08:12:54 UTC, 
     confirmation_sent_at: nil, unconfirmed_email: nil, address: "C\\ Romeo love Julieta street, 9, 5º A", 
     name: "fooname", surname: "foosurname", phone: "+34957957957", role: :employee, user_id: "518b5a860009d6307f000001"> 

Что я делаю неправильно, я ищу в Интернете, но я не нахожу ответа. Спасибо.

PD: Я refactoriced на ability.rb файл

if user.role? == :boss 
    can :crud, User do |people| 
    user.users.include?(people) 
    end 
end 

для

if user.role? == :boss 
    can :crud, User, User.where(:user_id => user.id) 
end 

все еще имеют те же проблемы, но более разборчивыми в настоящее время

ответ

0

я, наконец, решить проблема, я положил решение для других newbe, как я:

if user.role? == :boss 
    can :crud, User, User.where(:user_id => user.id) 
end 

Это должно измениться:

if user.role? == :boss 
    can :crud, User, :user_id => user.id 
end 
Смежные вопросы