Skip to content

Commit

Permalink
add indexable keys options
Browse files Browse the repository at this point in the history
  • Loading branch information
nivv committed Jun 19, 2024
1 parent 590eacd commit f4b8fc8
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/Models/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ public function getRevisionOptions(): RevisionOptions
'image' => 'getImages',
'repeater' => 'getRepeater',
])
->registerCacheTagsToFlush(['cms_pages']);
->registerCacheTagsToFlush(['cms_pages'])
->setIndexable(true, ['page_title'], 'page_title');
}

/**
Expand Down
18 changes: 17 additions & 1 deletion src/Traits/RevisionOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ class RevisionOptions
*/
public $indexableKeys = [];

/**
* Key to save as title
*
* @var string
*/
public $titleKey = '';

public static function create(): self
{
return new static();
Expand Down Expand Up @@ -100,10 +107,19 @@ public function registerCacheTagsToFlush(array $tags): self
return $this;
}

public function setIndexable(bool $searchable, array $indexableKeys = []): self
/**
* Undocumented function
*
* @param bool $searchable
* @param array $indexableKeys
* @param string $titleKey
* @return self
*/
public function setIndexable(bool $searchable, array $indexableKeys = [], string $titleKey = ''): self
{
$this->isIndexable = $searchable;
$this->indexableKeys = $indexableKeys;
$this->titleKey = $titleKey;

return $this;
}
Expand Down
18 changes: 18 additions & 0 deletions tests/Page/PageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -415,4 +415,22 @@ public function it_can_get_revision_options()
$this->assertIsArray($tagsToFlush);
$this->assertEquals('cms_pages', $tagsToFlush[0]);
}

/** @test **/
public function it_can_get_indexable_keys_from_options()
{
// Arrange
$page = Page::factory()->create([
'revision' => 10,
]);
$indexableKeys = $page->getRevisionOptions()->indexableKeys;

// Act

// Assert
$this->assertIsArray($indexableKeys);
$this->assertEquals('page_title', $indexableKeys[0]);
$this->assertEquals(true, $page->getRevisionOptions()->isIndexable);
$this->assertEquals('page_title', $page->getRevisionOptions()->titleKey);
}
}

0 comments on commit f4b8fc8

Please sign in to comment.