Skip to content

Commit

Permalink
Merge pull request #3 from evendis/fix/#2
Browse files Browse the repository at this point in the history
Fix: should load cancancan and perform valid cached abilities object verification #2
  • Loading branch information
tardate authored Jun 9, 2022
2 parents f1b82db + cee9e46 commit ba2cbed
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 15 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ tmp
mkmf.log
.rvm*
.ruby*
.byebug_history
4 changes: 2 additions & 2 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ task :default => :spec

desc "Open an irb session preloaded with this library"
task :console do
sh "irb -rubygems -I lib -r cancannible.rb"
end
sh "irb -I lib -r cancannible.rb"
end
2 changes: 1 addition & 1 deletion lib/cancannible.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'active_support'
require 'active_support/core_ext'
require 'cancan'
require 'cancancan'

require 'cancannible/version'
require 'cancannible/config'
Expand Down
9 changes: 3 additions & 6 deletions lib/cancannible/grantee.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,9 @@ def abilities(refresh = false)
nil
elsif Cancannible.get_cached_abilities.respond_to?(:call)
result = Cancannible.get_cached_abilities.call(self)
if result
# performs a crude compatibility check
rules_size = result.send(:rules).size rescue nil
rules_index_size = (result.instance_variable_get(:@rules_index) || []).size
result if !rules_size.nil? && rules_index_size == rules_size
end
# performs a crude compatibility check: cancan rules won't have a @rules_index
# (neither will an empty ability object, but we ignore this case)
result unless result && !result.instance_variable_defined?(:@rules_index)
end
return @abilities if @abilities

Expand Down
10 changes: 4 additions & 6 deletions spec/unit/cached_abilities_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
context "when store_cached_abilities provided" do
before do
@stored = nil
Cancannible.store_cached_abilities = proc { |grantee, ability| @stored = { grantee_id: grantee.id, ability: ability } }
Cancannible.store_cached_abilities = proc { |grantee, ability| @stored = { grantee_id: grantee.id, ability: cached_object } }
end
it "stores the cached object" do
expect { subject }.to change { @stored }.from(nil)
Expand All @@ -53,28 +53,26 @@
before do
@stored = nil
@store = 0
Cancannible.get_cached_abilities = proc { |grantee| @stored[:ability] if @stored }
Cancannible.get_cached_abilities = proc { |grantee| @stored && cached_object }
Cancannible.store_cached_abilities = proc { |grantee, ability| @store += 1 ; @stored = { grantee_id: grantee.id, ability: ability } }
end
it "stores the cached object on the first call" do
expect { subject }.to change { @stored }.from(nil)
expect(@store).to eql(1)
expect(@stored[:grantee_id]).to eql(grantee.id)
expect(@stored[:ability]).to be_an(Ability)
expect(grantee.abilities.instance_variable_get(:@rules).size).to eql(1)
expect(grantee.abilities.instance_variable_get(:@rules_index).size).to eql(1)
end
it "returns the cached object on the second call" do
expect { subject }.to change { @stored }.from(nil)
expect(@store).to eql(1)
expect { grantee.abilities }.to_not change { @store }
expect(grantee.abilities).to be_an(Ability)
expect(@stored[:grantee_id]).to eql(grantee.id)
end
it "should re-cache object on the second call if refresh requested" do
expect { subject }.to change { @stored }.from(nil)
expect(@store).to eql(1)
expect { grantee.abilities(true) }.to change { @store }.from(1).to(2)
expect(grantee.abilities).to be_an(Ability)
expect(@stored[:grantee_id]).to eql(grantee.id)
end
end
end
Expand Down

0 comments on commit ba2cbed

Please sign in to comment.