Permalink
Browse files

Episode 76

  • Loading branch information...
JeffreyWay committed Sep 6, 2017
1 parent 5afff93 commit a76952a216a1913bbc60dba3c4ede49254d5d204
@@ -65,7 +65,7 @@ protected function validator(array $data)
*/
protected function create(array $data)
{
User::forceCreate([
return User::forceCreate([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
@@ -68,7 +68,8 @@ public function store(Request $request)
'user_id' => auth()->id(),
'channel_id' => request('channel_id'),
'title' => request('title'),
'body' => request('body')
'body' => request('body'),
'slug' => request('title')
]);
return redirect($thread->path())
@@ -51,7 +51,7 @@ protected static function boot()
*/
public function path()
{
return "/threads/{$this->channel->slug}/{$this->id}";
return "/threads/{$this->channel->slug}/{$this->slug}";
}
/**
@@ -172,4 +172,47 @@ public function hasUpdatesFor($user)
return $this->updated_at > cache($key);
}
/**
* Get the route key name.
*
* @return string
*/
public function getRouteKeyName()
{
return 'slug';
}
/**
* Set the proper slug attribute.
*
* @param string $value
*/
public function setSlugAttribute($value)
{
if (static::whereSlug($slug = str_slug($value))->exists()) {
$slug = $this->incrementSlug($slug);
}
$this->attributes['slug'] = $slug;
}
/**
* Increment a slug's suffix.
*
* @param string $slug
* @return string
*/
protected function incrementSlug($slug)
{
$max = static::whereTitle($this->title)->latest('id')->value('slug');
if (is_numeric($max[-1])) {
return preg_replace_callback('/(\d+)$/', function ($matches) {
return $matches[1] + 1;
}, $max);
}
return "{$slug}-2";
}
}
@@ -33,16 +33,19 @@
$factory->define(App\Thread::class, function ($faker) {
$title = $faker->sentence;
return [
'user_id' => function () {
return factory('App\User')->create()->id;
},
'channel_id' => function () {
return factory('App\Channel')->create()->id;
},
'title' => $faker->sentence,
'title' => $title,
'body' => $faker->paragraph,
'visits' => 0
'visits' => 0,
'slug' => str_slug($title)
];
});
@@ -15,6 +15,7 @@ public function up()
{
Schema::create('threads', function (Blueprint $table) {
$table->increments('id');
$table->string('slug')->unique();
$table->unsignedInteger('user_id');
$table->unsignedInteger('channel_id');
$table->unsignedInteger('replies_count')->default(0);
@@ -3,6 +3,7 @@
namespace Tests\Feature;
use App\Activity;
use App\Thread;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Tests\TestCase;
@@ -76,6 +77,24 @@ function a_thread_requires_a_valid_channel()
->assertSessionHasErrors('channel_id');
}
/** @test */
function a_thread_requires_a_unique_slug()
{
$this->signIn();
$thread = create('App\Thread', ['title' => 'Foo Title', 'slug' => 'foo-title']);
$this->assertEquals($thread->fresh()->slug, 'foo-title');
$this->post(route('threads'), $thread->toArray());
$this->assertTrue(Thread::whereSlug('foo-title-2')->exists());
$this->post(route('threads'), $thread->toArray());
$this->assertTrue(Thread::whereSlug('foo-title-3')->exists());
}
/** @test */
function unauthorized_users_may_not_delete_threads()
{
@@ -21,12 +21,12 @@ public function setUp()
}
/** @test */
function a_thread_can_make_a_string_path()
function a_thread_has_a_path()
{
$thread = create('App\Thread');
$this->assertEquals(
"/threads/{$thread->channel->slug}/{$thread->id}", $thread->path()
"/threads/{$thread->channel->slug}/{$thread->slug}", $thread->path()
);
}

0 comments on commit a76952a

Please sign in to comment.