CanCan - 测试模型领域的能力?

发布于 2024-12-01 04:14:30 字数 747 浏览 3 评论 0原文

您好,我有以下

型号:

User (id)
Room (user_id, title)
RoomMember (user_id, room_id, admin)

给定: @用户1 @用户2 @用户3 @room

@user1 创建了房间并且是房间管理员。 @user1 应该使 @user2 成为 RoomUser.admin。非管理员的@user3 不应该能够使@user2 成为RoomUser.admin。这就是我想写的规范,但不知道如何用 CanCan 测试它。这是我到目前为止所拥有的:

room_member 是 Room 的 @user2 RoomMember 记录

  ability.should be_able_to(:update, room_member.admin)

关于如何为这个 w CanCan 编写规范有什么建议吗?

谢谢

根据我的能力更新

在规格中:

#Ability (user, room, room_password)
room_member = @room_for_user_1.room_members.create(:user_id => @user_2.id)
ability = Ability.new(@user_1, @room_for_user_1, nil)

Hello I have the following

Models:

User (id)
Room (user_id, title)
RoomMember (user_id, room_id, admin)

Given:
@user1
@user2
@user3
@room

@user1 created the room and is a room admin. @user1 should be make @user2 a RoomUser.admin. @user3 who is not an admin should not be able to make @user2 a RoomUser.admin. That's what I want to write a spec for but can't figure out how to test this with CanCan. Here is what I have so far:

room_member is the @user2 RoomMember record for the Room

  ability.should be_able_to(:update, room_member.admin)

Any advice on how I can write a spec for this w CanCan?

Thanks

Updated with my ability

In the specs:

#Ability (user, room, room_password)
room_member = @room_for_user_1.room_members.create(:user_id => @user_2.id)
ability = Ability.new(@user_1, @room_for_user_1, nil)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情话墙 2024-12-08 04:14:30

我不是 CanCan 方面的专家,但我认为您无法按照您尝试的方式定义这种能力。据我在文档和示例中看到的,操作仅在记录上定义,而不是在记录属性上定义。

当您想要定义不授予对记录的完全控制权的能力时,您需要定义自定义操作。

class Ability
  include CanCan::Ability
  def initialize (user = nil)
    user ||= User.new
    can :set_admin, [ Room ] { |room| user.room_members.any?{ |rm| rm.room == room && rm.admin? } }
  end
end

或者,如果您只希望房间所有者控制房间管理员,则可以是 can :set_admin, [ Room ] { |room| room.user == 用户}

然后就可以直接测试:ability.should be_able_to(:set_admin, room) 或者,根据测试中的变量:ability.should be_able_to(:set_admin, room_member.room )

I'm not an expert on CanCan, but I don't think you can define the ability in the way you are trying. As far as I've seen in the docs and examples, actions are only defined on the record, not on record attributes.

When you want to define an ability that does not confer complete control over the record, you need to define a custom action.

class Ability
  include CanCan::Ability
  def initialize (user = nil)
    user ||= User.new
    can :set_admin, [ Room ] { |room| user.room_members.any?{ |rm| rm.room == room && rm.admin? } }
  end
end

Alternatively, if you only want the room owner to control room admins, this would be can :set_admin, [ Room ] { |room| room.user == user }.

Then it is straightforward to test: ability.should be_able_to(:set_admin, room) or, depending on the variables you have in your test: ability.should be_able_to(:set_admin, room_member.room)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文