Skip to content

Commit

Permalink
Merge pull request #125 from Kurozora/improve-tv-rating
Browse files Browse the repository at this point in the history
[Update] TV Rating Weights
  • Loading branch information
kiritokatklian authored Apr 28, 2021
2 parents b246f06 + 128cb7d commit ccec8f4
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 11 deletions.
7 changes: 4 additions & 3 deletions app/Models/Anime.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,11 @@ protected static function boot()

static::addGlobalScope('tv_rating', function (Builder $builder) {
if (Auth::user() != null) {
$tvRating = settings('tv_rating');
$preferredTvRating = settings('tv_rating');
$tvRating = TvRating::firstWhere('weight', $preferredTvRating);

if ($tvRating != -1) {
$builder->where('tv_rating_id', '<=', $tvRating);
if (!empty($tvRating)) {
$builder->where('tv_rating_id', '<=', $tvRating->id);
}
}
});
Expand Down
1 change: 1 addition & 0 deletions app/Models/TvRating.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class TvRating extends Model
protected $fillable = [
'rating',
'description',
'weight',
];

/**
Expand Down
6 changes: 6 additions & 0 deletions app/Nova/TvRating.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Http\Request;
use Laravel\Nova\Fields\HasMany;
use Laravel\Nova\Fields\ID;
use Laravel\Nova\Fields\Number;
use Laravel\Nova\Fields\Text;

class TvRating extends Resource
Expand Down Expand Up @@ -58,6 +59,11 @@ public function fields(Request $request)
->help('A very short description of the rating. E.g. Not Rated, All Ages, Children...')
->required(),

Number::make('Weight')
->help('The priority of the rating. E.g: if a TV rating with a weight of 5 is selected, all ratings that are less than, and equal to, 5 are accessible to the user.')
->rules(['min:0', 'max:255'])
->required(),

HasMany::make('Anime'),
];
}
Expand Down
2 changes: 1 addition & 1 deletion config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
| or any other location as required by the application or its packages.
*/

'version' => '1.2.0-alpha.31',
'version' => '1.2.0-alpha.32',

/*
|--------------------------------------------------------------------------
Expand Down
3 changes: 2 additions & 1 deletion database/factories/TvRatingFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ public function definition()
{
return [
'name' => $this->faker->randomLetter,
'description' => $this->faker->words(3, true)
'description' => $this->faker->words(3, true),
'weight' => $this->faker->unique()->numberBetween(1, 10)
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public function up()
$table->bigIncrements('id');
$table->string('name');
$table->string('description');
$table->unsignedTinyInteger('weight');
$table->timestamps();
});
}
Expand Down
2 changes: 1 addition & 1 deletion database/seeders/DatabaseSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ public function run()
RoleSeeder::class,
StudioSeeder::class,
TvRatingSeeder::class,
GenreSeeder::class,
AnimeDummySeeder::class,
AnimeRelationsSeeder::class,
UserSeeder::class,
ForumSectionSeeder::class,
BadgeSeeder::class,
ForumThreadSeeder::class,
ForumReplySeeder::class,
GenreSeeder::class,
AppThemeSeeder::class,
ActorCharacterAnimeSeeder::class,
]);
Expand Down
10 changes: 6 additions & 4 deletions database/seeders/TvRatingSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,27 @@ class TvRatingSeeder extends Seeder
[
'name' => 'NR',
'description' => 'Not Rated',
'weight' => 1,
],
[
'name' => 'G',
'description' => 'All Ages',
'weight' => 2,
],
[
'name' => 'PG-12',
'description' => 'Parental Guidance Suggested',
'weight' => 3,
],
[
'name' => 'R15+',
'description' => 'Violence & Profanity',
'weight' => 4,
],
[
'name' => 'R18+',
'description' => 'Adults Only',
'weight' => 5,
]
];

Expand All @@ -43,10 +48,7 @@ class TvRatingSeeder extends Seeder
public function run()
{
foreach ($this->tvRatings as $tvRating) {
TvRating::create([
'name' => $tvRating['name'],
'description' => $tvRating['description'],
]);
TvRating::create($tvRating);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<x-select id="tv_rating" wire:model.defer="state.tv_rating">
<option value="-1">{{ __('Allow All Shows') }}</option>
@foreach (App\Models\TvRating::all()->where('id', '!=', 1) as $tvRating)
<option value="{{ $tvRating->id }}">{{ $tvRating->full_name }}</option>
<option value="{{ $tvRating->weight }}">{{ $tvRating->full_name }}</option>
@endforeach
</x-select>
<x-input-error for="tv_rating" class="mt-2" />
Expand Down

0 comments on commit ccec8f4

Please sign in to comment.