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

Why are merged rules still bound to the merged Ability class? #825

Open
niuage opened this issue May 4, 2023 · 0 comments
Open

Why are merged rules still bound to the merged Ability class? #825

niuage opened this issue May 4, 2023 · 0 comments

Comments

@niuage
Copy link

niuage commented May 4, 2023

Steps to reproduce

class AbilityA
  include CanCan::Ability

  def initialize
    can :destroy, Event do |event|
      puts self.class.name # this will output "AbilityA" even when the rule is called by AbilityB after the merge.
      can? :access_edit_mode, event
    end
  end
end

class AbilityB
  include CanCan::Ability

  def initialize
    merge(AbilityA.new)

    can :access_edit_mode, Event do |event|
      puts "access edit mode from #{self.class.name}"
      true
    end
  end
end

AbilityB.new.can? :destroy, Event.last
# AbilityA
# => false

Expected behavior

My understanding of merge would be that it would copy the can :destroy, Event from AbilityA in AbilityB, making AbilityB equivalent to:

class AbilityB
  include CanCan::Ability

  def initialize
      can :destroy, Event do |event|
        can? :access_edit_mode, event
      end
    end

    can :access_edit_mode, Event do |event|
      true
    end
  end
end

Actual behavior

The can :destroy, event rule is still bound to AbilityA, which means that when it's calling can? :access_edit_mode, event, it is not found, because AbilityA does not have this rule, and the one from AbilityB is never called.

System configuration

Rails version:
6.1.7.3

Ruby version:
3.0.6

CanCanCan version
3.3.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant