Skip to content

🐕🔌 Directory define query methods to the controller for a Banken loyalty class by DSL

License

Notifications You must be signed in to change notification settings

yhirano55/banken-dsl

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Banken::DSL

Build Status Gem Version

This plugin gives directly define query methods to the controller for a Banken loyalty class by DSL

Installation

gem "banken-dsl"

Include Banken in your application controller:

# app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
  include Banken
  protect_from_forgery
end

Optionally, you can run the generator, which will set up an application loyalty with some useful defaults for you:

rails g banken:install

After generating your application loyalty, restart the Rails server so that Rails can pick up any classes in the new app/loyalties/ directory.

Usage

This plugin provides DSL to define query methods to the Controller without creating a loyalty class:

# app/controllers/posts_controller.rb
class PostsController < ApplicationController
  authorization_policy do
    index   { true }
    show    { true }
    create  { user.admin? }
    update  { user.admin? && record.unpublished? }
    destroy { user.admin? && record.unpublished? }
  end

  before_action :set_post, only: [:show, :edit, :update, :destroy]

  def index
    @posts = Post.all
  end

  def show
  end
end

You can define the loyalty class by authorization_policy method as same as this code:

# app/loyalties/posts_loyalty.rb
class PostsLoyalty < ApplicationLoyalty
  def index?
    true
  end

  def show?
    true
  end

  def create?
    user.admin?
  end

  def update?
    user.admin? && record.unpublished?
  end

  def destroy?
    user.admin? && record.unpublished?
  end
end

License

The gem is available as open source under the terms of the MIT License.

About

🐕🔌 Directory define query methods to the controller for a Banken loyalty class by DSL

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages