Skip to content

Commit

Permalink
Rename locale to site on redirects table (#156)
Browse files Browse the repository at this point in the history
* Fix validation for eloquent redirects

* Revert "Fix validation for eloquent redirects"

This reverts commit dbff7f0.

* Fix error on site creation

* Use site column in `findByUrl` query
  • Loading branch information
vluijkx authored Jan 8, 2024
1 parent a49c4d9 commit 372e1ab
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class CreateEloquentRedirectTable extends Migration
$table->string('destination');
$table->char('match_type', 10);
$table->char('type', 10);
$table->string('locale');
$table->string('site')->index();
$table->integer('order')->nullable();
$table->boolean('enabled');
$table->timestamps();
Expand Down
10 changes: 5 additions & 5 deletions src/Eloquent/Redirects/RedirectRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function findByUrl(string $siteHandle, string $url): ?Redirect
{
return $this->query()
->where('enabled', true)
->where('locale', $siteHandle)
->where('site', $siteHandle)
->orderBy('order')
->get()
->map(function (Redirect $redirect) use ($url) {
Expand Down Expand Up @@ -87,7 +87,7 @@ public function make(): Redirect
{
return new Redirect();
}

public static function fromModel(Model $model)
{
return (new Redirect)
Expand All @@ -98,9 +98,9 @@ public static function fromModel(Model $model)
->matchType($model->match_type)
->enabled($model->enabled)
->order($model->order)
->locale($model->locale);
->locale($model->site);
}

private function toModel(Redirect $redirect)
{
return RedirectModel::firstOrNew([
Expand All @@ -112,7 +112,7 @@ private function toModel(Redirect $redirect)
'type' => $redirect->type(),
'enabled' => $redirect->enabled(),
'order' => $redirect->order(),
'locale' => $redirect->locale(),
'site' => $redirect->locale(),
]);
}
}
4 changes: 3 additions & 1 deletion src/RedirectServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Rias\StatamicRedirect\UpdateScripts\AddHitsCount;
use Rias\StatamicRedirect\UpdateScripts\ClearErrors;
use Rias\StatamicRedirect\UpdateScripts\MoveRedirectsToDefaultSite;
use Rias\StatamicRedirect\UpdateScripts\RenameLocaleToSiteOnRedirectsTable;
use Rias\StatamicRedirect\Widgets\ErrorsLastDayWidget;
use Rias\StatamicRedirect\Widgets\ErrorsLastMonthWidget;
use Rias\StatamicRedirect\Widgets\ErrorsLastWeekWidget;
Expand All @@ -39,6 +40,7 @@ class RedirectServiceProvider extends AddonServiceProvider
AddHitsCount::class,
ClearErrors::class,
MoveRedirectsToDefaultSite::class,
RenameLocaleToSiteOnRedirectsTable::class,
];

protected $scripts = [
Expand Down Expand Up @@ -90,7 +92,7 @@ public function boot()
__DIR__ . '/../database/migrations/create_redirect_tables.php.stub' => database_path('migrations/' . date('Y_m_d_His', time()) . '_create_redirect_tables.php'),
], 'migrations');
}

if (! class_exists('CreateEloquentRedirectTable')) {
$this->publishes([
__DIR__ . '/../database/migrations/create_eloquent_redirect_table.php.stub' => database_path('migrations/' . date('Y_m_d_His', time()) . '_create_eloquent_redirect_table.php'),
Expand Down
24 changes: 24 additions & 0 deletions src/UpdateScripts/RenameLocaleToSiteOnRedirectsTable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Rias\StatamicRedirect\UpdateScripts;

use Illuminate\Support\Facades\Schema;
use Statamic\UpdateScripts\UpdateScript;

class RenameLocaleToSiteOnRedirectsTable extends UpdateScript
{
public function shouldUpdate($newVersion, $oldVersion)
{
return Schema::hasColumn('redirects', 'locale');
}

public function update()
{
Schema::table('redirects', function ($table) {
$table->renameColumn('locale', 'site');
$table->index(['site']);
});

$this->console()->info('Column renamed succesfully!');
}
}

0 comments on commit 372e1ab

Please sign in to comment.