Why is Pundit not coupled with Rolify like CanCanCan is?

泄露秘密 提交于 2019-12-03 09:36:55

问题


I am using Devise and interested in using Pundit but cannot find much on if it should be integrating with Rolify or if it is stand alone. CanCanCan works nicely with Rolify and I like the roles model. Am I missing a major reason why Pundit and Rolify do not seem to be used together a lot?


回答1:


Why don't use them together? They can be easily used in a fashion like this

class OrganisationPolicy
  def initialize(user, organisation)
    @user = user
    @organisation = organisation
  end

  def index?
    @user.has_role? :admin
  end

  def show?
    @user.has_role?(:admin) || @user.organisation == @organisation
  end
end

In fact, the thing that rolify and pundit are not coupled is something nice, and not a design failure ;)




回答2:


I recently used Pundit gem with Rails 4 using devise.Pundit is standalone system with no dependency on Rolify as per my experience.

Instead of using Rolify, I created migration to add roles in the existing devise user table which helps you to assign roles to users and check which roles they have.

Please take a look at the schema that I created for my project:

 create_table "users", force: true do |t|
    t.string   "email",                  default: "", null: false
    t.string   "encrypted_password",     default: "", null: false
    t.string   "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.integer  "sign_in_count",          default: 0,  null: false
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.string   "current_sign_in_ip"
    t.string   "last_sign_in_ip"
    t.datetime "created_at"
    t.datetime "updated_at"
    **t.boolean  "is_admin"
    t.boolean  "is_daily_user"

Where is_admin and is_daily_user field is added for user roles.

Hope this helps!



来源:https://stackoverflow.com/questions/26172128/why-is-pundit-not-coupled-with-rolify-like-cancancan-is

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!