From 37859cb003daf7cf3ea57f5052f73cb8ff1381bc Mon Sep 17 00:00:00 2001 From: sjoulbak Date: Wed, 10 Jul 2024 14:41:21 +0200 Subject: [PATCH 1/3] fix(STI): compress rules per subjects --- CHANGELOG.md | 6 ++++++ lib/cancan/rules_compressor.rb | 4 ++-- lib/cancan/version.rb | 2 +- spec/cancan/rule_compressor_spec.rb | 26 ++++++++++++++++++++++++++ 4 files changed, 35 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c8a88d2d..9b53550d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,9 +1,14 @@ +## 3.6.2 + +* [#855](https://github.com/CanCanCommunity/cancancan/pull/855): Fix issue with compressing STI rules. ([@sjoulbak][]) + ## 3.6.0 * [#849](https://github.com/CanCanCommunity/cancancan/pull/849): Update tests matrix. ([@coorasse][]) * [#843](https://github.com/CanCanCommunity/cancancan/pull/843): Compress duplicate rules. ([@MrChoclate][]) * [#841](https://github.com/CanCanCommunity/cancancan/pull/841): New https://cancancan.dev website. ([@pandermatt][]) * [#839](https://github.com/CanCanCommunity/cancancan/pull/839): switch from database column detection to Rails attributes detection. ([@kalsan][]) + ## 3.5.0 * [#653](https://github.com/CanCanCommunity/cancancan/pull/653): Add support for using an nil relation as a condition. ([@ghiculescu][]) @@ -716,3 +721,4 @@ Please read the [guide on migrating from CanCanCan 2.x to 3.0](https://github.co [@MrChoclate]: https://github.com/MrChoclate [@pandermatt]: https://github.com/pandermatt [@kalsan]: https://github.com/kalsan +[@sjoulbak]: https://github.com/sjoulbak diff --git a/lib/cancan/rules_compressor.rb b/lib/cancan/rules_compressor.rb index 46f5a4cb..8e463d21 100644 --- a/lib/cancan/rules_compressor.rb +++ b/lib/cancan/rules_compressor.rb @@ -31,9 +31,9 @@ def compress(array) def simplify(rules) seen = Set.new rules.reverse_each.filter_map do |rule| - next if seen.include?(rule.conditions) + next if seen.include?(rule.subjects => rule.conditions) - seen.add(rule.conditions) + seen.add(rule.subjects => rule.conditions) rule end.reverse end diff --git a/lib/cancan/version.rb b/lib/cancan/version.rb index 7477402a..029f1e7d 100644 --- a/lib/cancan/version.rb +++ b/lib/cancan/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module CanCan - VERSION = '3.5.0'.freeze + VERSION = '3.6.2'.freeze end diff --git a/spec/cancan/rule_compressor_spec.rb b/spec/cancan/rule_compressor_spec.rb index a2e1d295..42d5354a 100644 --- a/spec/cancan/rule_compressor_spec.rb +++ b/spec/cancan/rule_compressor_spec.rb @@ -137,6 +137,32 @@ def cannot(action, subject, args = nil) end end + context 'when STI is in use' do + before do + class Pet + end + + class Cat < Pet + end + + class Dog < Pet + end + end + + let(:rules) do + [ + can(:read, Pet, capacity: 5), + can(:read, Cat, capacity: 5), + can(:read, Dog, capacity: 6), + can(:read, Pet, capacity: 5) + ] + end + + it 'minimizes the rules, by removing useless previous rules' do + expect(described_class.new(rules).rules_collapsed).to eq [rules[1], rules[2], rules[3]] + end + end + # TODO: not supported yet xcontext 'merges rules' do let(:rules) do From 20de8c4c7db24f38d2dcb669b4e7379373897a0b Mon Sep 17 00:00:00 2001 From: sjoulbak Date: Wed, 10 Jul 2024 14:58:32 +0200 Subject: [PATCH 2/3] fix(changelog): entry body should not start lowercased --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9b53550d..6ab4f513 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,7 @@ * [#849](https://github.com/CanCanCommunity/cancancan/pull/849): Update tests matrix. ([@coorasse][]) * [#843](https://github.com/CanCanCommunity/cancancan/pull/843): Compress duplicate rules. ([@MrChoclate][]) * [#841](https://github.com/CanCanCommunity/cancancan/pull/841): New https://cancancan.dev website. ([@pandermatt][]) -* [#839](https://github.com/CanCanCommunity/cancancan/pull/839): switch from database column detection to Rails attributes detection. ([@kalsan][]) +* [#839](https://github.com/CanCanCommunity/cancancan/pull/839): Switch from database column detection to Rails attributes detection. ([@kalsan][]) ## 3.5.0 From 969392061ee0704f4516ff66dbd0678e65f2d9ce Mon Sep 17 00:00:00 2001 From: sjoulbak Date: Wed, 10 Jul 2024 15:03:26 +0200 Subject: [PATCH 3/3] chore(changelog): add missing 3.6.1 entry --- CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6ab4f513..cee13668 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ * [#855](https://github.com/CanCanCommunity/cancancan/pull/855): Fix issue with compressing STI rules. ([@sjoulbak][]) +## 3.6.1 + +* [#847](https://github.com/CanCanCommunity/cancancan/pull/847): Fix: rule_spec should honor DB setting ([@tardate][]) + ## 3.6.0 * [#849](https://github.com/CanCanCommunity/cancancan/pull/849): Update tests matrix. ([@coorasse][]) @@ -721,4 +725,5 @@ Please read the [guide on migrating from CanCanCan 2.x to 3.0](https://github.co [@MrChoclate]: https://github.com/MrChoclate [@pandermatt]: https://github.com/pandermatt [@kalsan]: https://github.com/kalsan +[@tardate]: https://github.com/tardate [@sjoulbak]: https://github.com/sjoulbak