-
-
Notifications
You must be signed in to change notification settings - Fork 638
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement detection for sti for including subclasses in check #689
Changes from 6 commits
4c21cef
79a0296
e410e10
4dec403
9a24aa5
34b23d8
c65a9cd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# frozen_string_literal: true | ||
|
||
class StiDetector | ||
def self.sti_class?(subject) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A general detection for STI classes There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Love its isolation |
||
return false unless defined?(ActiveRecord::Base) | ||
return false unless subject.respond_to?(:descends_from_active_record?) | ||
return false if subject == :all || subject.descends_from_active_record? | ||
return false unless subject < ActiveRecord::Base | ||
|
||
true | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1033,6 +1033,35 @@ class JsonTransaction < ActiveRecord::Base | |
end | ||
end | ||
|
||
context 'with rule application to subclass for non sti class' do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Check that rules are no longer applied to parents for non sti |
||
before do | ||
ActiveRecord::Schema.define do | ||
create_table :parents, force: true | ||
|
||
create_table :children, force: true | ||
end | ||
|
||
class ApplicationRecord < ActiveRecord::Base | ||
self.abstract_class = true | ||
end | ||
|
||
class Parent < ActiveRecord::Base | ||
end | ||
|
||
class Child < Parent | ||
end | ||
end | ||
|
||
it 'rules are not effecting parent class' do | ||
This conversation was marked as resolved.
Show resolved
Hide resolved
|
||
u1 = User.create!(name: 'pippo') | ||
ability = Ability.new(u1) | ||
ability.can :manage, Parent | ||
ability.cannot :manage, Child | ||
expect(ability).not_to be_able_to(:index, Child) | ||
expect(ability).to be_able_to(:index, Parent) | ||
end | ||
end | ||
|
||
context 'when STI is in use' do | ||
before do | ||
ActiveRecord::Schema.define do | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only include the subclasses in the check if the model is an sti class