Project

banken-dsl

0.0
No commit activity in last 3 years
No release in over 3 years
This plugin gives directly define query methods to the controller for a Banken loyalty class by DSL
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.15

Runtime

>= 1.0.2
 Project Readme

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.