Skip to content

Commit

Permalink
Fixed an error in installing and deleting unprotected localizations
Browse files Browse the repository at this point in the history
  • Loading branch information
andrey-helldar committed Nov 6, 2024
1 parent 6017c86 commit d6d3912
Show file tree
Hide file tree
Showing 18 changed files with 62 additions and 46 deletions.
3 changes: 1 addition & 2 deletions src/Events/AllTranslationsHasBeenForgetEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,5 @@ class AllTranslationsHasBeenForgetEvent

public function __construct(
public Model $model
) {
}
) {}
}
3 changes: 1 addition & 2 deletions src/Events/TranslationHasBeenForgetEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,5 @@ class TranslationHasBeenForgetEvent
public function __construct(
public Model $model,
public ?Locale $locale
) {
}
) {}
}
3 changes: 1 addition & 2 deletions src/Events/TranslationHasBeenSetEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,5 @@ public function __construct(
public ?Locale $locale,
public mixed $oldValue,
public mixed $newValue,
) {
}
) {}
}
7 changes: 2 additions & 5 deletions src/Generators/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ abstract protected function filename(): string;
public function __construct(
protected string $model,
protected array $columns
) {
}
) {}

public static function of(string $model, array $columns = []): static
{
Expand All @@ -55,9 +54,7 @@ protected function make(): string
->toString();
}

protected function finish(string $path): void
{
}
protected function finish(string $path): void {}

protected function baseData(): array
{
Expand Down
2 changes: 1 addition & 1 deletion src/Generators/ModelGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ protected function data(): array
protected function filename(): string
{
$directory = dirname($path = $this->path());
$filename = $this->getModel() . $this->modelSuffix();
$filename = $this->getModel() . $this->modelSuffix();
$extension = $this->extension($path);

return $directory . '/' . $filename . '.' . $extension;
Expand Down
12 changes: 9 additions & 3 deletions src/HasTranslations.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,14 @@ trait HasTranslations

public function translations(): HasMany
{
return $this->hasMany($this->translationModelName(), 'item_id')
->tap(new FilterTranslationsScope());
return $this->translationsRaw()->tap(
new FilterTranslationsScope()
);
}

public function translationsRaw(): HasMany
{
return $this->hasMany($this->translationModelName(), 'item_id');
}

public function hasTranslated(string $column, Locale|LocaleData|string|null $locale = null): bool
Expand Down Expand Up @@ -114,7 +120,7 @@ public function setRelation($relation, $value): static

public function newInstance($attributes = [], $exists = false): static
{
$basic = Arr::except($attributes, $this->translatable());
$basic = Arr::except($attributes, $this->translatable());
$translatable = Arr::only($attributes, $this->translatable());

$model = parent::newInstance($basic, $exists);
Expand Down
2 changes: 1 addition & 1 deletion src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ protected function bootCommands(): void

protected function registry(): void
{
$this->app->singleton(Registry::class, fn () => new Registry());
$this->app->singleton(Registry::class);
}
}
5 changes: 2 additions & 3 deletions src/Services/Attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ public function __construct(
protected EloquentModel $model,
protected ?string $column = null,
protected Locale|LocaleData|string|null $locale = null
) {
}
) {}

public static function of(
EloquentModel $model,
Expand Down Expand Up @@ -96,7 +95,7 @@ public function forget(): Model

public function forgetAll(): Model
{
$this->model->translations()->delete();
$this->model->translationsRaw()->delete();

Relation::clear($this->model);

Expand Down
4 changes: 4 additions & 0 deletions src/Services/Relation.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ public static function initialize(Model $model): Model

public static function initializeLocale(Model $model, LocaleData $locale): Translation
{
if ($item = $model->translationsRaw()->where('locale', $locale->code)->first()) {
return static::setAttributes($model, $item, $locale);
}

return static::setAttributes($model, new (static::modelName($model))(), $locale);
}

Expand Down
8 changes: 4 additions & 4 deletions tests/Constants/FakeValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
class FakeValue
{
public const ColumnDescription = 'description';
public const ColumnTitle = 'title';
public const LocaleCustom = 'as';
public const LocaleFallback = 'de';
public const LocaleMain = 'fr';
public const ColumnTitle = 'title';
public const LocaleCustom = 'as';
public const LocaleFallback = 'de';
public const LocaleMain = 'fr';
public const LocaleUninstalled = 'af';
}
2 changes: 1 addition & 1 deletion tests/Unit/Console/ModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
'--columns' => ['title', 'description'],
])->run();

$model = base_path('app/Models/SomeTranslation.php');
$model = base_path('app/Models/SomeTranslation.php');
$helper = sprintf('%s/_ide_helper_models_%s.php', Config::shared()->models->helpers, md5('App\Models\Some'));

$migrations = File::allPaths(database_path('migrations'), fn (string $path) => Path::extension($path) === 'php');
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Events/TranslationHasBeenForgetEventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@

Event::assertDispatched(function (TranslationHasBeenForgetEvent $event) use ($model) {
return $event->model->getKey() === $model->getKey()
&& $event->locale === Locale::French;
&& $event->locale === Locale::French;
});
});
32 changes: 16 additions & 16 deletions tests/Unit/Events/TranslationHasBeenSetEventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@

Event::assertDispatched(function (TranslationHasBeenSetEvent $event) use ($model, $oldText, $newText) {
return $event->model->getKey() === $model->getKey()
&& $event->column === FakeValue::ColumnTitle
&& $event->locale === null
&& $event->oldValue === $oldText
&& $event->newValue === $newText;
&& $event->column === FakeValue::ColumnTitle
&& $event->locale === null
&& $event->oldValue === $oldText
&& $event->newValue === $newText;
});
});

Expand All @@ -38,10 +38,10 @@

Event::assertDispatched(function (TranslationHasBeenSetEvent $event) use ($model, $oldText, $newText) {
return $event->model->getKey() === $model->getKey()
&& $event->column === FakeValue::ColumnTitle
&& $event->locale === Locale::French
&& $event->oldValue === $oldText
&& $event->newValue === $newText;
&& $event->column === FakeValue::ColumnTitle
&& $event->locale === Locale::French
&& $event->oldValue === $oldText
&& $event->newValue === $newText;
});
});

Expand All @@ -55,10 +55,10 @@

Event::assertDispatched(function (TranslationHasBeenSetEvent $event) use ($model, $oldText, $newText) {
return $event->model->getKey() === $model->getKey()
&& $event->column === FakeValue::ColumnTitle
&& $event->locale === Locale::German
&& $event->oldValue === $oldText
&& $event->newValue === $newText;
&& $event->column === FakeValue::ColumnTitle
&& $event->locale === Locale::German
&& $event->oldValue === $oldText
&& $event->newValue === $newText;
});
});

Expand All @@ -72,9 +72,9 @@

Event::assertDispatched(function (TranslationHasBeenSetEvent $event) use ($model, $oldText, $newText) {
return $event->model->getKey() === $model->getKey()
&& $event->column === FakeValue::ColumnTitle
&& $event->locale === Locale::Assamese
&& $event->oldValue === $oldText
&& $event->newValue === $newText;
&& $event->column === FakeValue::ColumnTitle
&& $event->locale === Locale::Assamese
&& $event->oldValue === $oldText
&& $event->newValue === $newText;
});
});
10 changes: 10 additions & 0 deletions tests/Unit/Models/ForgetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@
FakeValue::ColumnTitle => [
FakeValue::LocaleMain => 'qwerty 10',
FakeValue::LocaleFallback => 'qwerty 11',
FakeValue::LocaleCustom => 'qwerty 12',
],

FakeValue::ColumnDescription => [
FakeValue::LocaleMain => 'qwerty 20',
FakeValue::LocaleFallback => 'qwerty 21',
FakeValue::LocaleCustom => 'qwerty 22',
],
])
);
Expand Down Expand Up @@ -67,25 +69,33 @@

expect($model->hasTranslated(FakeValue::ColumnTitle, FakeValue::LocaleMain))->toBeTrue();
expect($model->hasTranslated(FakeValue::ColumnTitle, FakeValue::LocaleFallback))->toBeTrue();
expect($model->hasTranslated(FakeValue::ColumnTitle, FakeValue::LocaleCustom))->toBeTrue();
expect($model->hasTranslated(FakeValue::ColumnDescription, FakeValue::LocaleMain))->toBeTrue();
expect($model->hasTranslated(FakeValue::ColumnDescription, FakeValue::LocaleFallback))->toBeTrue();
expect($model->hasTranslated(FakeValue::ColumnDescription, FakeValue::LocaleCustom))->toBeTrue();

expect($model->getTranslation(FakeValue::ColumnTitle, FakeValue::LocaleMain))->toBe('qwerty 10');
expect($model->getTranslation(FakeValue::ColumnTitle, FakeValue::LocaleFallback))->toBe('qwerty 11');
expect($model->getTranslation(FakeValue::ColumnTitle, FakeValue::LocaleCustom))->toBe('qwerty 12');
expect($model->getTranslation(FakeValue::ColumnDescription, FakeValue::LocaleMain))->toBe('qwerty 20');
expect($model->getTranslation(FakeValue::ColumnDescription, FakeValue::LocaleFallback))->toBe('qwerty 21');
expect($model->getTranslation(FakeValue::ColumnDescription, FakeValue::LocaleCustom))->toBe('qwerty 22');

$model->forgetAllTranslations();

expect($model->hasTranslated(FakeValue::ColumnTitle, FakeValue::LocaleMain))->toBeFalse();
expect($model->hasTranslated(FakeValue::ColumnTitle, FakeValue::LocaleFallback))->toBeFalse();
expect($model->hasTranslated(FakeValue::ColumnTitle, FakeValue::LocaleCustom))->toBeFalse();
expect($model->hasTranslated(FakeValue::ColumnDescription, FakeValue::LocaleMain))->toBeFalse();
expect($model->hasTranslated(FakeValue::ColumnDescription, FakeValue::LocaleFallback))->toBeFalse();
expect($model->hasTranslated(FakeValue::ColumnDescription, FakeValue::LocaleCustom))->toBeFalse();

expect($model->getTranslation(FakeValue::ColumnTitle, FakeValue::LocaleMain))->toBeNull();
expect($model->getTranslation(FakeValue::ColumnTitle, FakeValue::LocaleFallback))->toBeNull();
expect($model->getTranslation(FakeValue::ColumnTitle, FakeValue::LocaleCustom))->toBeNull();
expect($model->getTranslation(FakeValue::ColumnDescription, FakeValue::LocaleMain))->toBeNull();
expect($model->getTranslation(FakeValue::ColumnDescription, FakeValue::LocaleFallback))->toBeNull();
expect($model->getTranslation(FakeValue::ColumnDescription, FakeValue::LocaleCustom))->toBeNull();

assertDatabaseMissing(TestModelTranslation::class, [
'item_id' => $model->id,
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Models/GetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
config()->set(Name::Shared() . '.models.filter.enabled', $enabled);

$hasNonFilteredQuery = false;
$hasFilteredQuery = false;
$hasFilteredQuery = false;

DB::listen(function (QueryExecuted $query) use (&$hasNonFilteredQuery, &$hasFilteredQuery) {
if (Str::is('select * where *."item_id" = ? and *."item_id" is not null', $query->sql)) {
Expand Down
5 changes: 5 additions & 0 deletions workbench/app/Models/TestModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;
use LaravelLang\Models\HasTranslations;

Expand All @@ -14,6 +15,10 @@
* @property string|null $title
* @property string|null $description
* @property Collection<TestModelTranslation> $translations
* @property Collection<TestModelTranslation> $translationsRaw
*
* @method HasMany translations
* @method HasMany translationsRaw()
*/
class TestModel extends Model
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
return new class extends Migration {
public function up(): void
{
Schema::create('test_models', function (Blueprint $table) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
return new class extends Migration {
public function up(): void
{
Schema::create('test_model_translations', function (Blueprint $table) {
Expand Down

0 comments on commit d6d3912

Please sign in to comment.