Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
## 3.6.2

* [#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][])
* [#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

* [#653](https://github.com/CanCanCommunity/cancancan/pull/653): Add support for using an nil relation as a condition. ([@ghiculescu][])
Expand Down Expand Up @@ -716,3 +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
4 changes: 2 additions & 2 deletions lib/cancan/rules_compressor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/cancan/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module CanCan
VERSION = '3.5.0'.freeze
VERSION = '3.6.2'.freeze
end
26 changes: 26 additions & 0 deletions spec/cancan/rule_compressor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down