Skip to content

Commit

Permalink
Merge pull request #42 from Laravel-Lang/1.x
Browse files Browse the repository at this point in the history
Fixed an error in installing and deleting unprotected localizations
  • Loading branch information
andrey-helldar authored Nov 6, 2024
2 parents f459170 + 4f70a65 commit 5914712
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 4 deletions.
10 changes: 8 additions & 2 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
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);
}
}
2 changes: 1 addition & 1 deletion src/Services/Attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,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
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
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

0 comments on commit 5914712

Please sign in to comment.