Skip to content
This repository has been archived by the owner on Jun 18, 2019. It is now read-only.

Added notTranslatedIn scopes #235

Merged
merged 1 commit into from
Apr 21, 2016
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
15 changes: 15 additions & 0 deletions src/Translatable/Translatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,21 @@ public function scopeTranslatedIn(Builder $query, $locale = null)
});
}

/**
* @param \Illuminate\Database\Eloquent\Builder $query
* @param string $locale
*
* @return \Illuminate\Database\Eloquent\Builder|static
*/
public function scopeNotTranslatedIn(Builder $query, $locale = null)
{
$locale = $locale ?: $this->locale();

return $query->whereDoesntHave('translations', function (Builder $q) use ($locale) {
$q->where($this->getLocaleKey(), '=', $locale);
});
}

/**
* @param \Illuminate\Database\Eloquent\Builder $query
*
Expand Down
21 changes: 21 additions & 0 deletions tests/ScopesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,27 @@ public function test_translated_in_scope_works_with_default_locale()
$this->assertSame('Griechenland', $translatedCountries->first()->name);
}

public function test_not_translated_in_scope_returns_only_not_translated_records_for_this_locale()
{
$notTranslatedCountries = Country::notTranslatedIn('en')->get();
$this->assertCount(2, $notTranslatedCountries);

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is really great! Can you please assert here that count(notTranslatedCountries) is positive?

foreach($notTranslatedCountries as $notTranslatedCountry) {
$this->assertFalse($notTranslatedCountry->hasTranslation('en'));
}
}

public function test_not_translated_in_scope_works_with_default_locale()
{
App::setLocale('en');
$notTranslatedCountries = Country::notTranslatedIn()->get();
$this->assertCount(2, $notTranslatedCountries);

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

foreach($notTranslatedCountries as $notTranslatedCountry) {
$this->assertFalse($notTranslatedCountry->hasTranslation('en'));
}
}

public function test_translated_scope_returns_records_with_at_least_one_translation()
{
$translatedCountries = Country::translated()->get();
Expand Down