Skip to content
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

Rule aliases in an inheriting policy don't override aliases in parent policies #18

Closed
jacobwalkr opened this issue Jun 21, 2018 · 7 comments
Labels
bug Something isn't working investigate Something is probably wrong

Comments

@jacobwalkr
Copy link

jacobwalkr commented Jun 21, 2018

Tell us about your environment

Ruby Version:
2.4.3 (in Docker)

Framework Version (Rails, whatever):
Rails 5.2.0.rc2

Action Policy Version:
0.2.0

What did you do?

class ApplicationPolicy < ActionPolicy::Base; end

class GroupPolicy < ApplicationPolicy
  def new?
    user.staff?
  end
  alias_rule :create?, to: :new?
end

What did you expect to happen?

For GroupPolicy#create? to be aliased to GroupPolicy#new?

What actually happened?

GroupPolicy#create? was actually still aliased to ActionPolicy::Base#new? (because of the alias_rule :create, to: :new? in ActionPolicy::Policy::Defaults, I think) and so always returned false. The solution was to define another create? method that called new? explicitly.

(Sorry, I accidentally submitted this before I'd finished it)

@palkan palkan added bug Something isn't working investigate Something is probably wrong and removed bug Something isn't working labels Jun 21, 2018
@palkan
Copy link
Owner

palkan commented Jun 21, 2018

Aliases should be overridable (see this test: https://github.com/palkan/action_policy/blob/master/test/action_policy/policy/aliases_test.rb#L52).

Could you, please, provide the output for: GroupPolicy.new(user: User.new).resolve_rule(:new?) ?

@jacobwalkr
Copy link
Author

jacobwalkr commented Jun 22, 2018

irb(main):012:0> GroupPolicy.new(user: User.new).resolve_rule(:new?)
=> :index?
irb(main):013:0> GroupPolicy.new(user: User.new).resolve_rule(:create?)
=> :create?

This is with defaults included in the ApplicationPolicy and this GroupPolicy:

class GroupPolicy < ApplicationPolicy
  def index?
    true
  end
  alias_rule :new?, :create?, :edit?, :update?, to: :index?
end

@jacobwalkr
Copy link
Author

jacobwalkr commented Jun 22, 2018

I tried the other routes for fun:

irb(main):014:0> GroupPolicy.new(user: User.new).resolve_rule(:index?)
=> :index?
irb(main):015:0> GroupPolicy.new(user: User.new).resolve_rule(:edit?)
=> :index?
irb(main):016:0> GroupPolicy.new(user: User.new).resolve_rule(:update?)
=> :index?
irb(main):017:0> GroupPolicy.new(user: User.new).resolve_rule(:destroy?)
=> :default_rule
irb(main):018:0> GroupPolicy.new(user: User.new).resolve_rule(:show?)
=> :default_rule

default_rule is defined in our ApplicationPolicy.

@jacobwalkr
Copy link
Author

If I remove defaults from ApplicationPolicy:

irb(main):025:0> GroupPolicy.new(user: User.new).resolve_rule(:create?)
=> :index?

@palkan palkan added the bug Something isn't working label Jun 22, 2018
@brendon
Copy link
Contributor

brendon commented Jun 25, 2018

I've hit this too. It seems the idea is that if a hard rule has been defined in a superclass, an alias_rule in a subclass has no effect.

@brendon brendon mentioned this issue Jun 25, 2018
3 tasks
@palkan palkan closed this as completed in #22 Jul 2, 2018
@jacobwalkr
Copy link
Author

Thanks for your work, guys. This is super helpful and I wouldn't have known where to start 😊

@brendon
Copy link
Contributor

brendon commented Jul 3, 2018

You're most welcome :) Now I know a lot about action_policy! :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working investigate Something is probably wrong
Projects
None yet
Development

No branches or pull requests

3 participants