Skip to content

Commit

Permalink
Lets try adding basic approve support
Browse files Browse the repository at this point in the history
  • Loading branch information
pakomp committed Nov 23, 2017
1 parent 1fffb47 commit b6e859a
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 1 deletion.
5 changes: 4 additions & 1 deletion migrations/2014_10_12_000000_create_futures_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@ public function up()
{
Schema::create('futures', function (Blueprint $table) {
$table->increments('id');
$table->integer('createe_user_id')->unsinged()->nullable();
$table->unsignedInteger('createe_user_id')->nullable();
$table->morphs('futureable');
$table->json('data');
$table->timestamp('commit_at');
$table->timestamp('committed_at')->nullable()->default(null);
$table->boolean('needs_approval')->nullable()->default(null);
$table->unsignedInteger('approvee_user_id')->nullable()->default(null);
$table->timestamp('approved_at')->nullable()->default(null);
$table->timestamps();
$table->softDeletes();

Expand Down
21 changes: 21 additions & 0 deletions src/FuturePlanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,27 @@ public function plan(array $attributes)
return $this;
}

/**
* Set if the future needs approval.
*
* @param boolean $needed
* Set to false to remove the approval need from the future
*
* @return Dixie\EloquentModelFuture\Models\Future
*/
public function needsApproval($needed=true)
{
if($needed) {
$this->newFuture->needs_approve = true;
} else {
$this->newFuture->needs_approve = null;
}

$this->newFuture->save();

return $this->newFuture;
}

/**
* Set the date for when future should be committed.
*
Expand Down
39 changes: 39 additions & 0 deletions src/Models/Future.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class Future extends Model
'commit_at',
'committed_at',
'deleted_at',
'approved_at',
];

/**
Expand All @@ -41,6 +42,7 @@ class Future extends Model
protected $fillable = [
'futureable_id', 'futureable_type',
'commit_at', 'data', 'committed_at',
'needs_approval'
];

/**
Expand Down Expand Up @@ -78,6 +80,19 @@ public function creator()
);
}

/**
* Get the relationship to user who created the future plan.
*
* @return Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function approver()
{
return $this->belongsTo(
config('auth.providers.users.model'),
'approvee_user_id'
);
}

/**
* Narrow the scope of a query to only include futures for given date.
*
Expand Down Expand Up @@ -128,4 +143,28 @@ public function scopeCommitted(Builder $query)
{
return $query->whereNotNull('committed_at');
}

/**
* Narrow the scope of a query to only include unapproved futures.
*
* @param Builder $query
*
* @return Illuminate\Database\Eloquent\Builder
*/
public function scopeUnapproved(Builder $query)
{
return $query->whereNotNull('needs_approval')->whereNull('approvee_user_id');
}

/**
* Narrow the scope of a query to only include approved futures.
*
* @param Builder $query
*
* @return Illuminate\Database\Eloquent\Builder
*/
public function scopeApproved(Builder $query)
{
return $query->whereNotNull('approvee_user_id')->orWhereNull('needs_approval');
}
}
37 changes: 37 additions & 0 deletions src/Traits/HasFuture.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Auth;
use Dixie\EloquentModelFuture\Models\Future;
use Dixie\EloquentModelFuture\FuturePlanner;

Expand Down Expand Up @@ -34,6 +35,16 @@ public function uncommittedFutures()
return $this->futures()->whereNull('committed_at');
}

/**
* Defines the relationship between the model and its unapproved futures.
*
* @return Illuminate\Database\Eloquent\Relations\MorphMany
*/
public function unapprovedFutures()
{
return $this->futures()->whereNotNull('needs_approval')->whereNull('approvee_user_id');
}

/**
* Start planning the future of a model
*
Expand Down Expand Up @@ -68,4 +79,30 @@ public function commitFuturePlan(Future $futurePlan)

return $futurePlan->save();
}

/**
* Approve to the presented result of the model
*
* @return boolean
*/
public function approve()
{
$this->future()->getPlansUntil(Carbon::now())
->each([$this, 'approveFuturePlan']);

return $this->save();
}

/**
* Approve the given future.
*
* @param boolean
*/
public function approveFuturePlan(Future $futurePlan)
{
$futurePlan->approved_at = Carbon::now();
$futurePlan->approver()->associate(Auth::user());

return $futurePlan->save();
}
}

0 comments on commit b6e859a

Please sign in to comment.