Giter Club home page Giter Club logo

approval's Introduction

Approval

Build Status Gem Version

πŸ™†β€β™€οΈπŸ™…Approval flow for Rails

Installation

  1. Add approval to your Gemfile:
gem 'approval'
  1. Add approval_requests, approval_comments, approval_items tables to your database and an initializer file for configuration:
$ bundle exec rails generate approval:install
  1. Add acts_as_approval_user to your user model (User, AdminUser, Member ...etc)::
class User < ApplicationRecord
  acts_as_approval_user
end
  1. Add acts_as_approval_resource to the models you want use approval flow:
class Book < ApplicationRecord
  acts_as_approval_resource
end

Or if you want to use PORO:

class SomeProcess
  def self.perform
    # something
  end
end

Approval Flow

Make request

You send request, but resources aren't created/updated/destroyed.

πŸ™ Create

staff = User.find_or_create_by(email: "[email protected]")

record  = Book.new(name: "Ruby Way", price: 2980)
request = staff.request_for_create(record, reason: "something")
request.save # Created Approval::Request record.

records = 10.times.map {|n| Book.new(name: "my_book_#{n}", price: 300) }
request = staff.request_for_create(records, reason: "something")
request.save!

πŸ™ Update

staff = User.find_or_create_by(email: "[email protected]")

record  = Book.find(1).tap {|record| record.name = "new book title" }
request = staff.request_for_update(record, reason: "something")
request.save

records = Book.where(id: [1, 2, 3]).each {|record| record.price *= 0.5 }
request = staff.request_for_update(records, reason: "something")
request.save!

πŸ™ Destroy

staff = User.find_or_create_by(email: "[email protected]")

record  = Book.find(1)
request = staff.request_for_destroy(record, reason: "something")
request.save

records = Book.where(id: [1, 2, 3])
request = staff.request_for_destroy(records, reason: "something")
request.save!

πŸ™ Perform

staff = User.find_or_create_by(email: "[email protected]")

record  = MyProcess.new(recipient: "[email protected]")
request = staff.request_for_perform(record, reason: "something")
request.save

Respond

πŸ™†β€β™€οΈ Approve

Then resources are created/updated/destroyed, if respond user have approved the request.

admin = User.find_or_create_by(email: "[email protected]")

request = Approval::Request.first
respond = admin.approve_request(request, reason: "something")
respond.save! # Create/Update/Destroy resources
πŸ™… Reject

Then resources are not created/updated/destroyed, if respond user have rejected the request.

admin = User.find_or_create_by(email: "[email protected]")

request = Approval::Request.first
respond = admin.reject_request(request, reason: "something")
respond.save!
πŸ—‘οΈ Cancel
staff = User.find_or_create_by(email: "[email protected]")

request = Approval::Request.first
respond = staff.cancel_request(request, reason: "something")
respond.save!

Comment

admin = User.find_or_create_by(email: "[email protected]")

request = Approval::Request.first
admin.approval_comments.create(request: request, content: "Hello")

Configuration

# config/initializers/approval.rb

Approval.configure do |config|
  # User Class Name (e.g: User, AdminUser, Member)
  config.user_class_name = "User"

  # Maximum characters of comment for reason (default: 2000)
  config.comment_maximum = 2000

  # Permit to respond to own request? (default: false)
  config.permit_to_respond_to_own_request = false
end

License

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.