LoginSignup
1
1

More than 5 years have passed since last update.

cancancan 1.12.0 -> 1.16.0 のアップデートをしたときの話

Posted at

cancancanとは

authorization(認可、権限付与)を管理できるRuby Gem :gem: です。
GitHub Repository: CanCanCommunity/cancancan: The authorization Gem for Ruby on Rails.

1.12.0 -> 1.16.0 へのアップデート

1.12.0 っていつリリースのものを使ってたんですかって話はさておき…

Gemfileを書き換えて bundle update して bundle exec rspec すると cancancan 周りでテストがコケまくりました。原因を探っていてわかったことは draperdecorate したものに対して authorize!can? していた箇所がコケているということでした。

irb(main):001:0> user = User.first
  User Load (1.1ms)  SELECT  "users".* FROM "users"  ORDER BY "users"."id" ASC LIMIT 1
=> #<User id: 1, name: "user1", blog_id: 1, created_at: "2017-03-10 06:50:54", updated_at: "2017-03-10 06:50:54">
irb(main):002:0> blog = user.blog
  Blog Load (0.5ms)  SELECT  "blogs".* FROM "blogs" WHERE "blogs"."id" = $1 LIMIT 1  [["id", 1]]
=> #<Blog id: 1, name: "blog1", created_at: "2017-03-10 06:50:54", updated_at: "2017-03-10 06:50:54">
irb(main):003:0> ability = Ability.new user
(中略)
irb(main):004:0> ability.can? :read, blog
=> true
irb(main):005:0> ability.can? :read, blog.decorate
=> false

まとめ

  • decoったモデルに対して権限確認はやめよう
  • decoるタイミングをちゃんと設計しよう
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1