Skip to content

Commit

Permalink
feat: added support for category grouping
Browse files Browse the repository at this point in the history
  • Loading branch information
frknasir committed Apr 21, 2022
1 parent b596a19 commit 8c38058
Show file tree
Hide file tree
Showing 12 changed files with 102 additions and 38 deletions.
1 change: 1 addition & 0 deletions database/migrations/create_pigeonhole_table.php.stub
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ return new class extends Migration

$table->unsignedBigInteger('team_id')->nullable();

$table->string('type')->nullable();
$table->string('name');

$table->timestamps();
Expand Down
4 changes: 3 additions & 1 deletion src/Actions/CreateCategory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
class CreateCategory implements CreatesCategories
{
/**
* Create a new tax.
* Create a new category.
*
* @param mixed $user
* @param array $data
Expand All @@ -28,10 +28,12 @@ public function __invoke($user, array $data, $teamId = null)

Validator::make($data, [
'name' => 'required|string|max:255',
'type' => 'nullable|string|max:255',
])->validateWithBag('createCategory');

$fields = collect($data)->only([
'name',
'type',
])->toArray();

return Pigeonhole::$supportsTeams ?
Expand Down
28 changes: 27 additions & 1 deletion src/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace StarfolkSoftware\Pigeonhole;

use Illuminate\Contracts\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphToMany;
Expand All @@ -24,6 +25,7 @@ class Category extends Model
*/
protected $fillable = [
'name',
'type',
];

/**
Expand All @@ -34,7 +36,7 @@ class Category extends Model
protected $casts = [];

/**
* Get the team that owns the tax.
* Get the team that owns the category.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
Expand All @@ -61,4 +63,28 @@ public function entries(string $class): MorphToMany
'id'
);
}

/**
* Scope query with all the given types.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param array $types
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeOfTypes(Builder $query, array $types): Builder
{
return $query->whereIn('type', $types);
}

/**
* Scope query with the given type.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param string $type
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeOfType(Builder $query, string $type): Builder
{
return $query->where('type', $type);
}
}
2 changes: 1 addition & 1 deletion src/Contracts/CreatesCategories.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
interface CreatesCategories
{
/**
* Create a new tax.
* Create a new category.
*
* @param mixed $user
* @param array $data
Expand Down
6 changes: 3 additions & 3 deletions src/Contracts/DeletesCategories.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
interface DeletesCategories
{
/**
* Delete an existing tax.
* Delete an existing category.
*
* @param mixed $user
* @param \StarfolkSoftware\Pigeonhole\Category $tax
* @param \StarfolkSoftware\Pigeonhole\Category $category
* @return void
*/
public function __invoke($user, Category $tax);
public function __invoke($user, Category $category);
}
6 changes: 3 additions & 3 deletions src/Contracts/UpdatesCategories.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
interface UpdatesCategories
{
/**
* Update an existing tax.
* Update an existing category.
*
* @param mixed $user
* @param \StarfolkSoftware\Pigeonhole\Category $tax
* @param \StarfolkSoftware\Pigeonhole\Category $category
* @param array $data
* @return \StarfolkSoftware\Pigeonhole\Category
*/
public function __invoke($user, Category $tax, array $data);
public function __invoke($user, Category $category, array $data);
}
24 changes: 0 additions & 24 deletions tests/Feature/Actions/CategoryCreationTest.php

This file was deleted.

51 changes: 51 additions & 0 deletions tests/Feature/Actions/CreateCategoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

use StarfolkSoftware\Pigeonhole\Category;
use StarfolkSoftware\Pigeonhole\Contracts\CreatesCategories;
use StarfolkSoftware\Pigeonhole\Tests\Mocks\TestUser;

beforeAll(function () {
\StarfolkSoftware\Pigeonhole\Pigeonhole::supportsTeams(false);
});

it('can create a category', function () {
$createsCategories = app(CreatesCategories::class);

$user = TestUser::first();

$category = $createsCategories(
$user,
[
'name' => 'Category',
]
);

expect($category->refresh())
->name->toBe('Category');
});

it('can create a category with type', function () {
$createsCategories = app(CreatesCategories::class);

$user = TestUser::first();

$category = $createsCategories(
$user,
[
'name' => 'Category',
'type' => 'product',
]
);

expect($category->refresh())
->name->toBe('Category');

expect($category->refresh())
->type->toBe('product');

expect(Category::ofType('product')->count())
->toBe(1);

expect(Category::ofTypes(['product', 'project'])->count())
->toBe(1);
});
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,21 @@
\StarfolkSoftware\Pigeonhole\Pigeonhole::supportsTeams(false);
});

it('can update a tax', function () {
it('can update a category', function () {
$updatesCategories = app(UpdatesCategories::class);

$user = TestUser::first();

$tax = Category::factory()->create();
$category = Category::factory()->create();

$tax = $updatesCategories(
$category = $updatesCategories(
$user,
$tax,
$category,
[
'name' => 'Category',
]
);

expect($tax->refresh())
expect($category->refresh())
->name->toBe('Category');
});
8 changes: 8 additions & 0 deletions tests/Feature/CategoriesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,22 @@

$response = actingAs($user)->post(route('categories.store'), [
'name' => 'Category',
'type' => 'product',
'redirect' => '/redirect/path',
]);

$response->assertRedirect('/redirect/path');

$this->assertDatabaseHas('categories', [
'name' => 'Category',
'type' => 'product',
]);

expect(Category::count())->toEqual(1);

expect(Category::ofType('product')->count())->toEqual(1);

expect(Category::ofTypes(['product', 'project'])->count())->toEqual(1);
});

test('category can be updated', function () {
Expand Down

0 comments on commit 8c38058

Please sign in to comment.