Skip to content

Commit

Permalink
Use global scope for active channel results
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffreyWay committed Feb 1, 2018
1 parent b321a9d commit 061b3eb
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 3 deletions.
10 changes: 10 additions & 0 deletions app/Channel.php
Expand Up @@ -20,6 +20,16 @@ class Channel extends Model
'archived' => 'boolean'
];

protected static function boot()
{
parent::boot();

static::addGlobalScope('active', function ($builder) {
$builder->where('archived', false)
->orderBy('name', 'asc');
});
}

/**
* Get the route key name for Laravel.
*
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/Admin/ChannelsController.php
Expand Up @@ -15,7 +15,7 @@ class ChannelsController extends Controller
*/
public function index()
{
$channels = Channel::orderBy('name', 'asc')->with('threads')->get();
$channels = Channel::withoutGlobalScopes()->orderBy('name', 'asc')->with('threads')->get();

return view('admin.channels.index', compact('channels'));
}
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/Api/ChannelsController.php
Expand Up @@ -13,7 +13,7 @@ class ChannelsController extends Controller
public function index()
{
return cache()->rememberForever('channels', function () {
return Channel::where('archived', false)->orderBy('name', 'asc')->get();
return Channel::all();
});
}
}
2 changes: 1 addition & 1 deletion app/Http/Controllers/ThreadsController.php
Expand Up @@ -48,7 +48,7 @@ public function index(Channel $channel, ThreadFilters $filters, Trending $trendi
public function create()
{
return view('threads.create', [
'channels' => Channel::where('archived', false)->orderBy('name', 'asc')->get()
'channels' => Channel::all()
]);
}

Expand Down
10 changes: 10 additions & 0 deletions tests/Unit/ChannelTest.php
Expand Up @@ -2,6 +2,7 @@

namespace Tests\Feature;

use App\Channel;
use Tests\TestCase;
use Illuminate\Foundation\Testing\RefreshDatabase;

Expand Down Expand Up @@ -29,4 +30,13 @@ public function a_channel_can_be_archived()

$this->assertTrue($channel->archived);
}

/** @test */
public function archived_channels_are_excluded_by_default()
{
create('App\Channel');
create('App\Channel', ['archived' => true]);

$this->assertEquals(1, Channel::count());
}
}

0 comments on commit 061b3eb

Please sign in to comment.