Skip to content
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

Simplify Reactable scopes API #102

Merged
merged 18 commits into from
Aug 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ Follow [upgrade instructions](UPGRADING.md#from-v7-to-v8) to migrate code & data
- ([#91]) Added `Cog\Contracts\Love\Reaction\Exceptions\RateOutOfRange` exception
- ([#100]) Added `Cog\Contracts\Love\Reaction\Exceptions\RateInvalid` exception
- ([#96]) Added progress bar to `love:recount` Artisan command
- ([#97]) Added ability to call `Reactable::joinReactionCounterOfType` more than once
- ([#102]) Added `scopeWhereNotReactedBy` scope to `Reactable` model trait

### Changed

Expand Down Expand Up @@ -64,6 +66,14 @@ Follow [upgrade instructions](UPGRADING.md#from-v7-to-v8) to migrate code & data
- ([#96]) Changed signature of `love:recount` Artisan command to `love:recount {--model=} {--type=}`
- ([#99]) Make `Reacterable` parameter nullable in `isReactedBy` method of `Reactant` facade contract
- ([#99]) Make `Reacterable` parameter nullable in `isNotReactedBy` method of `Reactant` facade contract
- ([#102]) Changed second parameter type from `Reactant` to `Reacterable` in `scopeWhereReactedBy` method of `Reactable` model trait
- ([#102]) Changed third parameter type from `?ReactionType` to `?string` in `scopeWhereReactedBy` method of `Reactable` model trait
- ([#97]) Added third `?string $alias` parameter to `scopeJoinReactionCounterOfType` method of `Reactable` model trait
- ([#102]) Added second `?string $alias` parameter to `scopeJoinReactionTotal` method of `Reactable` model trait
- ([#102]) Renamed virtual column `reactions_count` to `reaction_{$type}_count` in `scopeJoinReactionCounterOfType` method of `Reactable` model trait
- ([#102]) Renamed virtual column `reactions_weight` to `reaction_{$type}_weight` in `scopeJoinReactionCounterOfType` method of `Reactable` model trait
- ([#102]) Renamed virtual column `reactions_total_count` to `reaction_total_count` in `scopeJoinReactionTotal` method of `Reactable` model trait
- ([#102]) Renamed virtual column `reactions_total_weight` to `reaction_total_weight` in `scopeJoinReactionTotal` method of `Reactable` model trait

### Removed

Expand Down Expand Up @@ -396,5 +406,7 @@ Follow [upgrade instructions](UPGRADING.md#from-v5-to-v6) to migrate database to
[#90]: https://github.com/cybercog/laravel-love/pull/90
[#91]: https://github.com/cybercog/laravel-love/pull/91
[#96]: https://github.com/cybercog/laravel-love/pull/96
[#97]: https://github.com/cybercog/laravel-love/pull/97
[#99]: https://github.com/cybercog/laravel-love/pull/99
[#100]: https://github.com/cybercog/laravel-love/pull/100
[#102]: https://github.com/cybercog/laravel-love/pull/102
8 changes: 6 additions & 2 deletions UPGRADING.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,19 @@

## From v7 to v8

- All `weight` values are `float` now. Round them to get `integer` values as it was before.
- All `weight` values are `float` now. Round them to get `integer` values as it was before
- Find all `isReactedTo` method usages and replace it with `hasReactedTo`
- Find all `isReactedToWithType` method usages and replace it with `hasReactedTo`
- Find all `isNotReactedTo` method usages and replace it with `hasNotReactedTo`
- Find all `isNotReactedToWithType` method usages and replace it with `hasNotReactedTo`
- Find all `isReactedByWithType` method usages and replace it with `isReactedBy`
- Find all `isNotReactedByWithType` method usages and replace it with `isNotReactedBy`
- The `ReactionType` method `getWeight` was renamed to `getMass`. If you're using your own implementation of `ReactionType`, please update the method name.
- The `ReactionType` method `getWeight` was renamed to `getMass`. If you're using your own implementation of `ReactionType`, please update the method name
- Find all `whereReactedByWithType` method usages and replace it with `whereReactedBy`
- Find all `whereReactedBy` method usages and replace first `Reacter` argument with `Reacterable` and second `ReactionType` argument with its string name
- Find all `joinReactionCounterOfType` method usages and replace first `ReactionType` argument with its string name
- Find all `joinReactionCounterOfType` method usages and update virtual attributes prefix. It was changed from `reactions_` to `reaction_{$type}_`
- Find all `joinReactionTotal` method usages and update virtual attributes prefix. It was changed from `reactions_total` to `reaction_total_`

### Database migration

Expand Down
2 changes: 1 addition & 1 deletion src/Console/Commands/UpgradeV5ToV6.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ final class UpgradeV5ToV6 extends Command
*
* @var string
*/
protected $description = 'Upgrade love package from v5 to v6';
protected $description = 'Upgrade Love package from v5 to v6';

/**
* Execute the console command.
Expand Down
2 changes: 1 addition & 1 deletion src/Console/Commands/UpgradeV7ToV8.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ final class UpgradeV7ToV8 extends Command
*
* @var string
*/
protected $description = 'Upgrade love package from v7 to v8';
protected $description = 'Upgrade Love package from v7 to v8';

/**
* Execute the console command.
Expand Down
45 changes: 31 additions & 14 deletions src/Reactable/Models/Traits/Reactable.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,19 @@
use Cog\Contracts\Love\Reactable\Exceptions\AlreadyRegisteredAsLoveReactant;
use Cog\Contracts\Love\Reactant\Facades\Reactant as ReactantFacadeContract;
use Cog\Contracts\Love\Reactant\Models\Reactant as ReactantContract;
use Cog\Contracts\Love\Reacter\Models\Reacter as ReacterContract;
use Cog\Contracts\Love\ReactionType\Models\ReactionType as ReactionTypeContract;
use Cog\Contracts\Love\Reacterable\Models\Reacterable as ReacterableContract;
use Cog\Laravel\Love\Reactable\Observers\ReactableObserver;
use Cog\Laravel\Love\Reactant\Facades\Reactant as ReactantFacade;
use Cog\Laravel\Love\Reactant\Models\NullReactant;
use Cog\Laravel\Love\Reactant\Models\Reactant;
use Cog\Laravel\Love\Reactant\ReactionCounter\Models\ReactionCounter;
use Cog\Laravel\Love\Reactant\ReactionTotal\Models\ReactionTotal;
use Cog\Laravel\Love\ReactionType\Models\ReactionType;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Query\JoinClause;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;

/**
* @mixin \Cog\Contracts\Love\Reactable\Models\Reactable
Expand Down Expand Up @@ -81,23 +82,37 @@ public function registerAsLoveReactant(): void

public function scopeWhereReactedBy(
Builder $query,
ReacterContract $reacter,
?ReactionTypeContract $reactionType = null
ReacterableContract $reacterable,
?string $reactionTypeName = null
): Builder {
return $query->whereHas('loveReactant.reactions', function (Builder $reactionsQuery) use ($reacter, $reactionType) {
$reactionsQuery->where('reacter_id', $reacter->getId());
if (!is_null($reactionType)) {
$reactionsQuery->where('reaction_type_id', $reactionType->getId());
return $query->whereHas('loveReactant.reactions', function (Builder $reactionsQuery) use ($reacterable, $reactionTypeName) {
$reactionsQuery->where('reacter_id', $reacterable->getLoveReacter()->getId());
if (!is_null($reactionTypeName)) {
$reactionsQuery->where('reaction_type_id', ReactionType::fromName($reactionTypeName)->getId());
}
});
}

public function scopeWhereNotReactedBy(
Builder $query,
ReacterableContract $reacterable,
?string $reactionTypeName = null
): Builder {
return $query->whereDoesntHave('loveReactant.reactions', function (Builder $reactionsQuery) use ($reacterable, $reactionTypeName) {
$reactionsQuery->where('reacter_id', $reacterable->getLoveReacter()->getId());
if (!is_null($reactionTypeName)) {
$reactionsQuery->where('reaction_type_id', ReactionType::fromName($reactionTypeName)->getId());
}
});
}

public function scopeJoinReactionCounterOfType(
Builder $query,
ReactionTypeContract $reactionType,
string $reactionTypeName,
?string $alias = null
): Builder {
$alias = is_null($alias) ? strtolower($reactionType->getName()) : $alias;
$reactionType = ReactionType::fromName($reactionTypeName);
$alias = is_null($alias) ? 'reaction_' . Str::snake($reactionType->getName()) : $alias;

$select = $query->getQuery()->columns ?? ["{$this->getTable()}.*"];
$select[] = DB::raw("COALESCE({$alias}.count, 0) as {$alias}_count");
Expand All @@ -112,14 +127,16 @@ public function scopeJoinReactionCounterOfType(
}

public function scopeJoinReactionTotal(
Builder $query
Builder $query,
?string $alias = null
): Builder {
$alias = is_null($alias) ? 'reaction_total' : $alias;
$select = $query->getQuery()->columns ?? ["{$this->getTable()}.*"];
$select[] = DB::raw('COALESCE(lrrt.count, 0) as reactions_total_count');
$select[] = DB::raw('COALESCE(lrrt.weight, 0) as reactions_total_weight');
$select[] = DB::raw("COALESCE({$alias}.count, 0) as {$alias}_count");
$select[] = DB::raw("COALESCE({$alias}.weight, 0) as {$alias}_weight");

return $query
->leftJoin((new ReactionTotal())->getTable() . ' as lrrt', 'lrrt.reactant_id', '=', "{$this->getTable()}.love_reactant_id")
->leftJoin((new ReactionTotal())->getTable() . ' as ' . $alias, "{$alias}.reactant_id", '=', "{$this->getTable()}.love_reactant_id")
->select($select);
}
}
Loading