Skip to content

Added tests, fixed a couple of bugs, added new feature #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 24, 2018
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Added moment scope (select the version at a specific point in time)
  • Loading branch information
Peter Klooster committed May 19, 2018
commit b911037445a8ccb533a9f22d88e202281a2c24f8
25 changes: 24 additions & 1 deletion src/VersioningScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Scope;
use Illuminate\Database\Query\JoinClause;
use Carbon\Carbon;

class VersioningScope implements Scope
{
Expand All @@ -14,7 +15,7 @@ class VersioningScope implements Scope
*
* @var array
*/
protected $extensions = ['Version', 'AllVersions'];
protected $extensions = ['Version', 'AllVersions', 'Moment'];

/**
* Apply the scope to a given Eloquent query builder.
Expand Down Expand Up @@ -111,6 +112,28 @@ protected function addAllVersions(Builder $builder)
});
}

/**
* Add the moment extension to the builder.
*
* @param \Illuminate\Database\Eloquent\Builder $builder
* @return void
*/
protected function addMoment(Builder $builder)
{
$builder->macro('moment', function(Builder $builder, Carbon $moment) {
$model = $builder->getModel();

$this->remove($builder, $builder->getModel());

$builder->join($model->getVersionTable(), function($join) use ($model, $moment) {
$join->on($model->getQualifiedKeyName(), '=', $model->getQualifiedVersionKeyName());
$join->where('updated_at', '<=', $moment)->orderBy('updated_at', 'desc')->limit(1);
})->orderBy('updated_at', 'desc')->limit(1);

return $builder;
});
}

/**
* Determine if the given join clause is a version constraint.
*
Expand Down