action_policy icon indicating copy to clipboard operation
action_policy copied to clipboard

Improve rule aliases support

Open palkan opened this issue 4 years ago • 3 comments

  • Make testing utils work with aliases (so, we can do more black-box-like testing).
  • Consider adding testing utils to test aliases (expect(:index?).to be_an_alias_of(:show?)).

Closes #165. Closes #188.

palkan avatar Sep 14 '21 09:09 palkan

Hello, @palkan !

I was thinking about separating this feature (bugfix?) into two PRs: one for the actual bugfix and one for adding testing utils. WDYT?

Lokideos avatar Oct 06 '21 21:10 Lokideos

Hey, @palkan, @Lokideos! Thank you for this awesome library and all your work!

I just bumped into the alias testing myself. Spend some time figuring out why be_an_alias_of doesn't work according to documentation until I realized that this feature isn't present in the current 0.6 release 🤦‍♂️

I tried to install it from the master branch with gem 'action_policy', github: 'palkan/action_policy', but it failed to run this way because of unmet ruby-next dependency (Ruby 3.1, Rails 7.0.1, Bundle 2.3.3).

Nonetheless, I just wanted to suggest "embedding" alias testing into current DSL, something like this:

  # alias_rule :show?, to: :index?

  describe_rule :show? do
    is_an_alias_of :index?
  end

  # - or -

  describe_rule_alias :show?, to: :index?

Or something similar. Because the current implementation looks a bit out of place:

  describe_rule :index? do
    succeed 'for all admins'
  end

  describe '#show?' do
    it "is an alias of :index? authorization rule" do
      expect(:show?).to be_an_alias_of(policy, :index?)
    end
  end

  # even when shortened

  describe '#show?' do
    it { expect(:show?).to be_an_alias_of(policy, :index?) }
  end

Is it something you considered or are planning to add? I might help with PR if we agree on the exact syntax.

svyatov avatar Jan 08 '22 12:01 svyatov

Hey @svyatov!

I like the idea of adding a DSL method to check aliases; I'd suggest using something less verbose, e.g., alias_rule :index? (similarly to the policy class API). I will be glad to merge a PR)

palkan avatar Jan 11 '22 15:01 palkan