Giter VIP home page Giter VIP logo

approval's Introduction

Approve new Model data before it is persisted

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

Approval is a Laravel package that provides a simple way to approve new Model data before it is persisted.

Installation

You can install the package via composer:

composer require cjmellor/approval

You can publish and run the migrations with:

php artisan vendor:publish --tag="approval-migrations"
php artisan migrate

You can publish the config file with:

php artisan vendor:publish --tag="approval-config"

This is the contents of the published config file:

return [
    'approval' => [
        /**
         * The approval polymorphic pivot name
         *
         * Default: 'approvalable'
         */
        'approval_pivot' => 'approvalable',
    ],
];

The config allows you to change the polymorphic pivot name. It should end with able though.

Usage

This packages utilises Enums, so both PHP 8.1 and Laravel 9 must be used.

Note This package does not approve/deny the data for you, it just stores the new/amended data into the database. It is up to you to decide how you implement a function to approve or deny the Model.

Add the MustBeApproved trait to your Model and now the data will be stored in an approvals table, ready for you to approve or deny.

For example, you add it to a Post Model and each time a Post is created or updated, all the dirty data will be stored in the database as JSON for you to do something with it.

<?php

use Cjmellor\Approval\Concerns\MustBeApproved;

class Post extends Model
{
    use MustBeApproved;

    // ...
}

All Models using the Trait will now be stored in a new table -- approvals. This is a polymorphic relationship.

Here is some info about the columns in the approvals table:

approvalable_type => The class name of the Model that the approval is for

approvalable_id => The ID of the Model that the approval is for

state => The state of the approval. This uses an Enum class. This column is cast to an ApprovalStatus Enum class

new_data => All the fields created or updated in the Model. This is a JSON column. This column is cast to the AsArrayObject Cast

original_data => All the fields in the Model before they were updated. This is a JSON column. This column is cast to the AsArrayObject Cast

If you want to check if the Model data will be bypassed, use the isApprovalBypassed method.

return $model->isApprovalBypassed();

Scopes

The package comes with some helper methods for the Builder, utilising a custom scope - ApprovalStateScope

By default, all queries to the approvals table will return all the Models' no matter the state.

There are three methods to help you retrieve the state of an Approval.

<?php

use App\Models\Approval;

Approval::approved()->get();
Approval::rejected()->get();
Approval::pending()->count();

You can also set a state for an approval:

<?php

use App\Models\Approval;

Approval::where('id', 1)->approve();
Approval::where('id', 2)->reject();
Approval::where('id', 3)->postpone();

In the event you need to reset a state, you can use the withAnyState helper.

Persisting data

By default, once you approve a Model, it will be inserted into the database.

If you don't want to persist to the database on approval, set a false flag on the approve method.

Approval::find(1)->approve(persist: false);

Disable Approvals

If you don't want Model data to be approved, you can bypass it with the withoutApproval method.

$model->withoutApproval()->update(['title' => 'Some Title']);

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please open a PR with as much detail as possible about what you're trying to achieve.

Credits

License

The MIT License (MIT). Please see License File for more information.

approval's People

Contributors

cjmellor avatar dependabot[bot] avatar github-actions[bot] avatar rawilk avatar

Watchers

James Cloos avatar

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.