-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add option to index specific keys for a model (#19)
* add option to index specific keys for a model * remove * ignore vscode
- Loading branch information
Showing
9 changed files
with
207 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,3 +10,4 @@ node_modules | |
.php-cs-fixer.cache | ||
pnpm-lock.yaml | ||
.phpunit.cache | ||
.vscode |
33 changes: 33 additions & 0 deletions
33
database/migrations/2024_06_17_102630_create_search_terms_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
return new class extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function up() | ||
{ | ||
Schema::create('search_terms', function (Blueprint $table) { | ||
$table->id(); | ||
$table->morphs('model'); | ||
$table->string('locale')->default('sv'); | ||
$table->string('path')->nullable(); | ||
$table->text('search_string')->nullable(); | ||
$table->timestamps(); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
*/ | ||
public function down(): void | ||
{ | ||
Schema::dropIfExists('search_terms'); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
|
||
namespace Ikoncept\Fabriq\Listeners; | ||
|
||
use Ikoncept\Fabriq\Models\SearchTerm; | ||
use Illuminate\Support\Collection; | ||
use Infab\TranslatableRevisions\Events\DefinitionsPublished; | ||
|
||
class UpdateSearchTerms | ||
{ | ||
/** | ||
* Create the event listener. | ||
* | ||
* @return void | ||
*/ | ||
public function __construct() | ||
{ | ||
|
||
} | ||
|
||
/** | ||
* Handle the event. | ||
* | ||
* @param object $event | ||
* @return void | ||
*/ | ||
public function handle(DefinitionsPublished $event) | ||
Check failure on line 27 in src/Listeners/UpdateSearchTerms.php GitHub Actions / Laravel (PHP 8.3 on ubuntu-latest)
|
||
{ | ||
if (! $event->model->getRevisionOptions()->isIndexable) { | ||
return; | ||
} | ||
|
||
$event->model->paths->each(function ($path) use ($event) { | ||
$locale = collect($path)->keys()->first(); | ||
Check failure on line 34 in src/Listeners/UpdateSearchTerms.php GitHub Actions / Laravel (PHP 8.3 on ubuntu-latest)
|
||
|
||
$indexableKeys = $this->array_value_recursive($event->definitions[$locale], $event->model->getRevisionOptions()->indexableKeys); | ||
|
||
$data = [ | ||
'model_id' => $event->model->id, | ||
'model_type' => get_class($event->model), | ||
'locale' => $locale, | ||
'path' => collect($path)->flatten()->first(), | ||
Check failure on line 42 in src/Listeners/UpdateSearchTerms.php GitHub Actions / Laravel (PHP 8.3 on ubuntu-latest)
|
||
'search_string' => implode(' ', $indexableKeys), | ||
]; | ||
|
||
SearchTerm::updateOrCreate([ | ||
'model_id' => $data['model_id'], | ||
'model_type' => $data['model_type'], | ||
'locale' => $data['locale'], | ||
], $data); | ||
}); | ||
} | ||
|
||
protected function array_value_recursive(Collection $collection, ?array $keys = null, bool $unique = true): array | ||
{ | ||
$arr = $collection->toArray(); | ||
array_walk_recursive($arr, function ($v, $k) use ($keys, &$val) { | ||
if (in_array($k, $keys)) { | ||
$val[] = strip_tags($v); | ||
} | ||
}); | ||
|
||
return $unique ? array_unique($val ?? []) : $val ?? []; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
namespace Ikoncept\Fabriq\Models; | ||
|
||
use Illuminate\Database\Eloquent\Factories\HasFactory; | ||
use Illuminate\Database\Eloquent\Model; | ||
|
||
class SearchTerm extends Model | ||
{ | ||
use HasFactory; | ||
|
||
protected $guarded = []; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?php | ||
|
||
namespace Tests\Feature; | ||
|
||
use Ikoncept\Fabriq\Database\Seeders\DatabaseSeeder; | ||
use Ikoncept\Fabriq\Database\Seeders\PageTemplateSeeder; | ||
use Ikoncept\Fabriq\Tests\AdminUserTestCase; | ||
use Infab\TranslatableRevisions\Models\RevisionTemplate; | ||
|
||
class SearchTermsFeatureTest extends AdminUserTestCase | ||
{ | ||
public function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
app(DatabaseSeeder::class)->call(PageTemplateSeeder::class); | ||
} | ||
|
||
public function test_it_can_include_content_for_a_single_page() | ||
{ | ||
// Arrange | ||
$page = \Ikoncept\Fabriq\Models\Page::factory()->create([ | ||
'name' => 'En sida som ska publiceras', | ||
'template_id' => RevisionTemplate::all()->first()->id, | ||
'revision' => 1, | ||
]); | ||
|
||
$page->updateContent([ | ||
'page_title' => 'The page title for the page', | ||
'page_header' => 'The page title for the page', | ||
'page_content' => '<h1>Wow, a header</h1><p>Ok lets see</p>', | ||
'meta_title' => 'Meta title', | ||
'meta_description' => 'Describing the page', | ||
'meta_og_image' => 'https://placehold.it/40', | ||
'boxes' => [ | ||
['header' => 'EN Box 1 title!', 'url' => 'https://google.com'], | ||
['header' => 'EN Box 2 title!', 'url' => 'https://bog.com'], | ||
['header' => 'EN Box 3 title!', 'url' => 'http://flank.se'], | ||
], | ||
], 'en'); | ||
$page->updateContent([ | ||
'page_title' => 'En siee saom skau paublisers', | ||
'page_header' => 'En siee saom skau paublisers', | ||
'page_content' => '<h1>Wow</h1><p>Da ska vi se...</p>', | ||
'meta_title' => 'Meta titel', | ||
'meta_description' => 'En forklaring av sidan', | ||
'meta_og_image' => 'https://placehold.it/40', | ||
'boxes' => [ | ||
['header' => 'DK fosta titeln!', 'url' => 'https://google.com'], | ||
['header' => 'DK andra titeln!', 'url' => 'https://bog.com'], | ||
], | ||
], 'dk'); | ||
|
||
// Act | ||
$response = $this->json('POST', "/pages/{$page->id}/publish"); | ||
|
||
// Assert | ||
$this->assertDatabaseHas('pages', [ | ||
'id' => $page->id, | ||
'revision' => 2, | ||
'published_version' => 1, | ||
]); | ||
|
||
$this->assertDatabaseHas('search_terms', [ | ||
'model_id' => $page->id, | ||
'model_type' => config('fabriq.models.page'), | ||
'locale' => 'dk', | ||
'path' => 'en-siee-saom-skau-paublisers', | ||
'search_string' => 'DK fosta titeln! DK andra titeln! En siee saom skau paublisers', | ||
]); | ||
|
||
$this->assertDatabaseHas('search_terms', [ | ||
'model_id' => $page->id, | ||
'model_type' => config('fabriq.models.page'), | ||
'locale' => 'en', | ||
'path' => 'the-page-title-for-the-page', | ||
'search_string' => 'EN Box 1 title! EN Box 2 title! EN Box 3 title! The page title for the page', | ||
]); | ||
|
||
$this->assertDatabaseCount('search_terms', 3); | ||
} | ||
} |