Skip to content

Commit

Permalink
Added content fields to the taxons table
Browse files Browse the repository at this point in the history
  • Loading branch information
fulopattila122 committed Jun 7, 2024
1 parent 2276e69 commit 42e7b84
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 0 deletions.
6 changes: 6 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@
- `street_address` (fetches $billpayer->address->address) - can't use `address` since that collides with the address() relation
- `address2`
- `access_code`
- Added the following content fields to the Taxon model/table:
- `subtitle`
- `excerpt`
- `description`
- `top_content`
- `bottom_content`
- Changed the offline payment gateway's icon from a circle to a plug+x
- Fixed the possible missing configuration copy from cart_items to order_items in the OrderFactory

Expand Down
10 changes: 10 additions & 0 deletions src/Category/Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@

## 4.x Series

## Unreleased
##### 2024-XX-YY

- Added the following content fields to the Taxon model/table:
- `subtitle`
- `excerpt`
- `description`
- `top_content`
- `bottom_content`

## 4.0.0
##### 2024-04-25

Expand Down
21 changes: 21 additions & 0 deletions src/Category/Models/Taxon.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,31 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Support\Carbon;
use Illuminate\Support\Collection;
use Vanilo\Category\Contracts\Taxon as TaxonContract;
use Vanilo\Category\Contracts\Taxonomy as TaxonomyContract;

/**
* @property-read int $id
* @property string $name
* @property string $slug
* @property int|null $parent_id
* @property int|null $priority
* @property string|null $ext_title
* @property string|null $meta_keywords
* @property string|null $meta_description
* @property string|null $subtitle
* @property string|null $excerpt
* @property string|null $description
* @property string|null $top_content
* @property string|null $bottom_content
* @property-read Carbon $created_at
* @property-read Carbon $updated_at
*
* @property-read Taxonomy $taxonomy
* @property-read Taxon|null $parent
*/
class Taxon extends Model implements TaxonContract
{
use Sluggable;
Expand Down
22 changes: 22 additions & 0 deletions src/Category/Tests/TaxonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -373,4 +373,26 @@ public function taxons_can_tell_if_they_are_root_level_ones()
$child->removeParent();
$this->assertTrue($child->isRootLevel());
}

/** @test */
public function content_fields_can_be_mass_assigned()
{
$taxonomy = Taxonomy::create(['name' => 'Occasion']);
$taxon = Taxon::create([
'name' => 'Birthday',
'taxonomy_id' => $taxonomy->id,
'subtitle' => 'Birthday Gifts',
'excerpt' => 'Birthday gifts should be given for Bday',
'description' => 'This is a longer description, should be enough for unit tests',
'top_content' => 'Hey I am an html content above the product list',
'bottom_content' => 'I am a markdown content below the product list',
])->fresh();

$this->assertEquals('Birthday', $taxon->name);
$this->assertEquals('Birthday Gifts', $taxon->subtitle);
$this->assertEquals('Birthday gifts should be given for Bday', $taxon->excerpt);
$this->assertEquals('This is a longer description, should be enough for unit tests', $taxon->description);
$this->assertEquals('Hey I am an html content above the product list', $taxon->top_content);
$this->assertEquals('I am a markdown content below the product list', $taxon->bottom_content);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration {
public function up(): void
{
Schema::table('taxons', function (Blueprint $table) {
// The `subtitle`, `excerpt` and `description` are very generic
// others may already have such fields, treating them nicely
$this->addColumnIfNotYetExists($table, 'subtitle', 'string');
$this->addColumnIfNotYetExists($table, 'excerpt', 'text');
$this->addColumnIfNotYetExists($table, 'description', 'text');
$table->text('top_content')->nullable();
$table->text('bottom_content')->nullable();
});
}

public function down(): void
{
Schema::table('taxons', function (Blueprint $table) {
$table->dropColumn('subtitle');
$table->dropColumn('excerpt');
$table->dropColumn('description');
$table->dropColumn('top_content');
$table->dropColumn('bottom_content');
});
}

private function addColumnIfNotYetExists(Blueprint $table, string $column, string $type): void
{
if (!Schema::hasColumn('taxons', $column)) {
match ($type) {
'text' => $table->text($column)->nullable(),
'string' => $table->string($column)->nullable(),
default => 'WTF',
};
}
}
};

0 comments on commit 42e7b84

Please sign in to comment.