DEV Community

n350071πŸ‡―πŸ‡΅
n350071πŸ‡―πŸ‡΅

Posted on

1 2

organize the authorizations of the banken/pundit gem with define_method

πŸ”— Parent Note

banken is ...,

Simple and lightweight authorization library for Rails inspired by Pundit. Banken provides a set of helpers which restricts what resources a given user is allowed to access.

πŸ€” Situation

Authorization will be grown with your app, and it will be like this.

new? refers show?, show? refers index?.

class BooksLoyalty < ApplicationLoyalty
  def index?
    @user_context.user == @record.user
  end
  def show?
    index?
  end
  def new?
    show?
  end
end
Enter fullscreen mode Exit fullscreen mode

πŸ‘ Solution

Because each method is the same, they don't transfer their logic. Then, define_method may be nice.

class BooksLoyalty < ApplicationLoyalty
  [:index?, :show?, :new?].each do |method|
    define_method method do
      @user_context.user == @record.user
    end
  end
end
Enter fullscreen mode Exit fullscreen mode

If you need arguments, see also πŸ“š my github note.

AWS Q Developer image

Your AI Code Assistant

Generate and update README files, create data-flow diagrams, and keep your project fully documented. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free β†’