Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/Runway.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ public static function registerResource(string $model, array $config): self
return new static;
}

public static function clearRegisteredResources(): self
{
static::$registeredResources = [];

return new static;
}

public static function usesRouting(): bool
{
return static::allResources()->filter->hasRouting()->count() >= 1;
Expand Down
3 changes: 1 addition & 2 deletions src/Traits/HasRunwayResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace StatamicRadPack\Runway\Traits;

use Illuminate\Contracts\Database\Eloquent\Builder;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Str;
use Statamic\Contracts\Data\Augmented;
use Statamic\Contracts\Revisions\Revision;
Expand Down Expand Up @@ -58,7 +57,7 @@ public function scopeRunway(Builder $query)
public function scopeRunwaySearch(Builder $query, string $searchQuery)
{
$this->runwayResource()->blueprint()->fields()->all()
->filter(fn (Field $field) => Schema::hasColumn($this->getTable(), $field->handle()))
->filter(fn (Field $field) => $this->getConnection()->getSchemaBuilder()->hasColumn($this->getTable(), $field->handle()))
->reject(fn (Field $field) => $field->visibility() === 'computed')
->each(fn (Field $field) => $query->orWhere($this->getColumnForField($field->handle()), 'LIKE', '%'.$searchQuery.'%'));
}
Expand Down
51 changes: 51 additions & 0 deletions tests/HasRunwayResourceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace StatamicRadPack\Runway\Tests;

use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Schema;
use PHPUnit\Framework\Attributes\Test;
use StatamicRadPack\Runway\Runway;
use StatamicRadPack\Runway\Tests\Fixtures\Models\ExternalPost;

class HasRunwayResourceTest extends TestCase
{
#[Test]
public function scope_runway_search_works_with_custom_eloquent_connection()
{
Config::set('database.connections.external', [
'driver' => 'sqlite',
'database' => ':memory:',
'prefix' => '',
]);

Schema::connection('external')->create('external_posts', function ($table) {
$table->id();
$table->string('title');
$table->longText('body');
$table->timestamps();
});

Runway::registerResource(ExternalPost::class, []);

ExternalPost::create([
'title' => 'Test External Post',
'body' => 'This is the body of the test post.',
]);

ExternalPost::create([
'title' => 'Another Post',
'body' => 'This is different content.',
]);

ExternalPost::create([
'title' => 'Something Else',
'body' => 'No matching content here.',
]);

$results = ExternalPost::query()->runwaySearch('Test External')->get();

$this->assertCount(1, $results);
$this->assertEquals('Test External Post', $results->first()->title);
}
}
3 changes: 3 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Statamic\Stache\Stores\UsersStore;
use Statamic\Statamic;
use Statamic\Testing\AddonTestCase;
use StatamicRadPack\Runway\Runway;
use StatamicRadPack\Runway\ServiceProvider;

abstract class TestCase extends AddonTestCase
Expand Down Expand Up @@ -52,6 +53,8 @@ protected function resolveApplicationConfiguration($app)
Statamic::booted(function () {
Blueprint::setDirectory(__DIR__.'/__fixtures__/resources/blueprints');
});

Runway::clearRegisteredResources();
}

protected function getPackageProviders($app): array
Expand Down
20 changes: 20 additions & 0 deletions tests/__fixtures__/app/Models/ExternalPost.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace StatamicRadPack\Runway\Tests\Fixtures\Models;

use Illuminate\Database\Eloquent\Model;
use StatamicRadPack\Runway\Traits\HasRunwayResource;

class ExternalPost extends Model
{
use HasRunwayResource;

protected $connection = 'external';

protected $table = 'external_posts';

protected $fillable = [
'title',
'body',
];
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
tabs:
main:
sections:
-
fields:
-
handle: title
field:
type: text
listable: true
-
handle: body
field:
type: textarea
sidebar:
fields: { }