Skip to content

Commit

Permalink
Get rid of overengineered scopes in models
Browse files Browse the repository at this point in the history
  • Loading branch information
range-of-motion committed Dec 30, 2023
1 parent d57e6ae commit 6ce81d3
Show file tree
Hide file tree
Showing 17 changed files with 40 additions and 58 deletions.
4 changes: 3 additions & 1 deletion app/Http/Controllers/ActivityController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ class ActivityController extends Controller
{
public function index()
{
$activities = Activity::ofSpace(session('space_id'))->get();
$activities = Activity::query()
->where('space_id', session('space_id'))
->get();

return view('activities.index', ['activities' => $activities]);
}
Expand Down
4 changes: 3 additions & 1 deletion app/Http/Controllers/Api/ActivitiesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ public function __invoke(Request $request)
/** @var ApiKey $apiKey */
$apiKey = $request->get('apiKey');

$activities = Activity::ofSpace($apiKey->user->spaces()->first()->id)->get();
$activities = Activity::query()
->where('space_id', $apiKey->user->spaces()->first()->id)
->get();

return ActivityResource::collection($activities);
}
Expand Down
5 changes: 4 additions & 1 deletion app/Http/Controllers/BudgetController.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ public function index()

public function create()
{
$tags = Tag::ofSpace(session('space_id'))->latest()->get();
$tags = Tag::query()
->where('space_id', session('space_id'))
->latest()
->get();

return view('budgets.create', ['tags' => $tags]);
}
Expand Down
6 changes: 4 additions & 2 deletions app/Http/Controllers/ImportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function __construct(SpendingRepository $spendingRepository)
public function index()
{
return view('imports.index')->with([
'imports' => Import::ofSpace(session('space_id'))->latest()->get()
'imports' => Import::query()->where('space_id', session('space_id'))->latest()->get()
]);
}

Expand Down Expand Up @@ -97,7 +97,9 @@ public function getComplete(Import $import)
{
$this->authorize('modify', $import);

$tags = Tag::ofSpace(session('space_id'))->get();
$tags = Tag::query()
->where('space_id', session('space_id'))
->get();

$file = fopen(storage_path('app/imports/' . $import->file), 'r');

Expand Down
7 changes: 5 additions & 2 deletions app/Http/Controllers/RecurringController.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ public function __construct(RecurringRepository $recurringRepository)

public function index()
{
$recurrings = Recurring::ofSpace(session('space_id'))->latest()->get();
$recurrings = Recurring::query()
->where('space_id', session('space_id'))
->latest()
->get();

return view('recurrings.index', ['recurrings' => $recurrings]);
}
Expand All @@ -36,7 +39,7 @@ public function create()
{
$tags = [];

foreach (Tag::ofSpace(session('space_id'))->get() as $tag) {
foreach (Tag::query()->where('space_id', session('space_id'))->get() as $tag) {
$tags[] = ['key' => $tag->id, 'label' => $tag->name];
}

Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/ReportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ private function weeklyReport($year)

private function mostExpensiveTags()
{
$totalSpent = Spending::ofSpace(session('space_id'))->sum('amount');
$totalSpent = Spending::query()->where('space_id', session('space_id'))->sum('amount');
$mostExpensiveTags = $this->tagRepository->getMostExpensiveTags(session('space_id'));

return view('reports.most_expensive_tags', compact('totalSpent', 'mostExpensiveTags'));
Expand Down
10 changes: 8 additions & 2 deletions app/Http/Controllers/SpendingController.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ public function __construct(

public function create()
{
$tags = Tag::ofSpace(session('space_id'))->latest()->get();
$tags = Tag::query()
->where('space_id', session('space_id'))
->latest()
->get();

return view('spendings.create', ['tags' => $tags]);
}
Expand Down Expand Up @@ -71,7 +74,10 @@ public function edit(Spending $spending)
{
$this->authorize('edit', $spending);

$tags = Tag::ofSpace(session('space_id'))->latest()->get();
$tags = Tag::query()
->where('space_id', session('space_id'))
->latest()
->get();

return view('spendings.edit', compact('tags', 'spending'));
}
Expand Down
5 changes: 4 additions & 1 deletion app/Http/Controllers/TagController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ public function __construct(TagRepository $tagRepository)

public function index()
{
$tags = Tag::ofSpace(session('space_id'))->latest()->get();
$tags = Tag::query()
->where('space_id', session('space_id'))
->latest()
->get();

return view('tags.index', ['tags' => $tags]);
}
Expand Down
4 changes: 2 additions & 2 deletions app/Http/Controllers/TransactionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ public function index(Request $request)

return view('transactions.index', [
'yearMonths' => $this->repository->getTransactionsByYearMonth($filterBy),
'tags' => Tag::ofSpace(session('space_id'))->get()
'tags' => Tag::query()->where('space_id', session('space_id'))->get(),
]);
}

public function create()
{
$tags = [];

foreach (Tag::ofSpace(session('space_id'))->get() as $tag) {
foreach (Tag::query()->where('space_id', session('space_id'))->get() as $tag) {
$tags[] = [
'key' => $tag->id,
'label' => '<div class="row"><div class="row__column row__column--compact row__column--middle mr-1"><div style="width: 15px; height: 15px; border-radius: 2px; background: #' . $tag->color . ';"></div></div><div class="row__column row__column--middle">' . $tag->name . '</div></div>' // phpcs:ignore
Expand Down
6 changes: 0 additions & 6 deletions app/Models/Activity.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,4 @@ public function user()
{
return $this->belongsTo(User::class);
}

// Scopes
public function scopeOfSpace($query, $spaceId)
{
return $query->where('space_id', $spaceId);
}
}
6 changes: 0 additions & 6 deletions app/Models/Currency.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,4 @@ protected function isoLowercased(): Attribute
{
return Attribute::make(fn () => strtolower($this->iso));
}

// Scopes
public function scopeOfSpace($query, $spaceId)
{
return $query->where('space_id', $spaceId);
}
}
6 changes: 0 additions & 6 deletions app/Models/Earning.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,4 @@ public function attachments()
return $this->hasMany(Attachment::class, 'transaction_id')
->where('transaction_type', 'earning');
}

// Scopes
public function scopeOfSpace($query, $spaceId)
{
return $query->where('space_id', $spaceId);
}
}
6 changes: 0 additions & 6 deletions app/Models/Import.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,4 @@ public function spendings()
{
return $this->hasMany(Spending::class);
}

// Scopes
public function scopeOfSpace($query, $spaceId)
{
return $query->where('space_id', $spaceId);
}
}
6 changes: 0 additions & 6 deletions app/Models/Recurring.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,4 @@ public function tag()
{
return $this->belongsTo(Tag::class);
}

// Scopes
public function scopeOfSpace($query, $spaceId)
{
return $query->where('space_id', $spaceId);
}
}
6 changes: 0 additions & 6 deletions app/Models/Spending.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,4 @@ public function attachments()
return $this->hasMany(Attachment::class, 'transaction_id')
->where('transaction_type', 'spending');
}

// Scopes
public function scopeOfSpace($query, $spaceId)
{
return $query->where('space_id', $spaceId);
}
}
6 changes: 0 additions & 6 deletions app/Models/Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,4 @@ public static function randomColor()
{
return self::randomColorPart() . self::randomColorPart() . self::randomColorPart();
}

// Scopes
public function scopeOfSpace($query, $spaceId)
{
return $query->where('space_id', $spaceId);
}
}
9 changes: 6 additions & 3 deletions app/Widgets/Spent.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ public function render()
$currencySymbol = $space->currency->symbol;

if ($this->properties->period === 'today') {
$spent = Spending::ofSpace(session('space_id'))
$spent = Spending::query()
->where('space_id', session('space_id'))
->whereRaw('DATE(happened_on) = ?', [date('Y-m-d')])
->sum('amount');
}
Expand All @@ -31,13 +32,15 @@ public function render()
$monday = date('Y-m-d', strtotime('monday this week'));
$sunday = date('Y-m-d', strtotime('sunday this week'));

$spent = Spending::ofSpace(session('space_id'))
$spent = Spending::query()
->where('space_id', session('space_id'))
->whereRaw('DATE(happened_on) >= ? AND DATE(happened_ON) <= ?', [$monday, $sunday])
->sum('amount');
}

if ($this->properties->period === 'this_month') {
$spent = Spending::ofSpace(session('space_id'))
$spent = Spending::query()
->where('space_id', session('space_id'))
->whereRaw('YEAR(happened_on) = ? AND MONTH(happened_on) = ?', [date('Y'), date('n')])
->sum('amount');
}
Expand Down

0 comments on commit 6ce81d3

Please sign in to comment.