Skip to content

Commit b4ddc87

Browse files
committed
feat: add simple JavaabuTranslatable check w/Tests
1 parent 20a40dc commit b4ddc87

File tree

10 files changed

+153
-1
lines changed

10 files changed

+153
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ coverage
66
.idea
77
.php-cs-fixer.cache
88
clover.xml
9+
coverage.xml

src/Views/Components/Select.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ public function getOptionsFromQueryBuilder(BuilderContract $query): array
153153
$model->hasGetMutator($id_field);
154154
}
155155

156-
if ($is_accessor) {
156+
if ($is_accessor || $this->isJavaabuTranslatableModel($model)) {
157157
return $query->get()
158158
->pluck($name_field, $id_field)
159159
->all();
@@ -162,6 +162,11 @@ public function getOptionsFromQueryBuilder(BuilderContract $query): array
162162
return $query->pluck($name_field, $id_field)->all();
163163
}
164164

165+
public function isJavaabuTranslatableModel($model): bool
166+
{
167+
return method_exists($model, 'isTranslatable');
168+
}
169+
165170
public function isSelected($key): bool
166171
{
167172
return in_array($key, Arr::wrap($this->selectedKey));

tests/Feature/Select2Test.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@
33
namespace Javaabu\Forms\Tests\Feature;
44

55
use Illuminate\Support\Facades\Route;
6+
use Illuminate\Foundation\Testing\RefreshDatabase;
67
use Javaabu\Forms\Tests\TestCase;
78
use Javaabu\Forms\Tests\TestSupport\Enums\ArticleStatuses;
89
use Javaabu\Forms\Tests\TestSupport\Models\Article;
910

1011
class Select2Test extends TestCase
1112
{
13+
use RefreshDatabase;
14+
1215
/** @test */
1316
public function it_can_render_a_select_2_basic_element_whose_value_comes_from_an_enum_cast()
1417
{
@@ -174,4 +177,30 @@ public function it_can_render_a_select2_element_with_custom_form_group_class_boo
174177
->seeElement('option[value="2"]:selected')
175178
->seeElement('option[value="3"]');
176179
}
180+
181+
/** @test */
182+
public function it_only_loads_selected_options_for_ajax_selects()
183+
{
184+
$article1 = Article::create(['title' => 'Article 1']);
185+
$article2 = Article::create(['title' => 'Article 2']);
186+
$article3 = Article::create(['title' => 'Article 3']);
187+
188+
$model = [
189+
'article' => $article2->id,
190+
];
191+
192+
$options = Article::query();
193+
194+
Route::get('select2-ajax-query', function () use ($model, $options) {
195+
return view('select2-ajax-query')
196+
->with('model', $model)
197+
->with('options', $options);
198+
})->middleware('web');
199+
200+
$this->visit('/select2-ajax-query')
201+
->seeElement('select.select2-ajax[name="article"]')
202+
->dontSeeElement('option[value="' . $article1->id . '"]')
203+
->seeElement('option[value="' . $article2->id . '"]:selected')
204+
->dontSeeElement('option[value="' . $article3->id . '"]');
205+
}
177206
}

tests/Feature/SelectTest.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,15 @@
22

33
namespace Javaabu\Forms\Tests\Feature;
44

5+
use Illuminate\Foundation\Testing\RefreshDatabase;
6+
use Illuminate\Support\Facades\Route;
57
use Javaabu\Forms\Tests\TestCase;
8+
use Javaabu\Forms\Tests\TestSupport\Models\Category;
69

710
class SelectTest extends TestCase
811
{
12+
use RefreshDatabase;
13+
914
/** @test */
1015
public function it_shows_the_slot_if_the_options_are_empty()
1116
{
@@ -40,4 +45,48 @@ public function it_adds_a_sync_field_for_multi_selects()
4045
->seeElement('option[value="b"]')
4146
->seeElement('option[value="c"]');
4247
}
48+
49+
/** @test */
50+
public function it_can_exclude_the_sync_field_for_multi_selects()
51+
{
52+
$this->registerTestRoute('select-sync-field');
53+
54+
$this->visit('/select-sync-field')
55+
->dontSeeElement('input[type="hidden"][name="sync_exclude_sync"]')
56+
->dontSeeElement('input[type="hidden"][name="sync_disabled_sync"]')
57+
->seeElement('input[type="hidden"][name="custom_sync_name"][value="1"]');
58+
}
59+
60+
/** @test */
61+
public function it_includes_old_values_for_tags()
62+
{
63+
$this->registerTestRoute('select-tags');
64+
65+
$this->session(['_old_input' => ['tags' => ['a', 'b', 'c']]]);
66+
67+
$this->visit('/select-tags')
68+
->seeElement('option[value="a"]:selected')
69+
->seeElement('option[value="b"]:selected')
70+
->seeElement('option[value="c"]:selected');
71+
}
72+
73+
/** @test */
74+
public function it_can_load_options_from_a_translatable_model()
75+
{
76+
$category1 = Category::create(['name' => 'Category 1']);
77+
$category2 = Category::create(['name' => 'Category 2']);
78+
79+
$options = Category::query();
80+
81+
Route::get('select-translatable', function () use ($options) {
82+
return view('select-translatable')
83+
->with('options', $options);
84+
})->middleware('web');
85+
86+
$this->visit('/select-translatable')
87+
->seeElement('option[value="' . $category1->id . '"]')
88+
->see('Category 1')
89+
->seeElement('option[value="' . $category2->id . '"]')
90+
->see('Category 2');
91+
}
4392
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Javaabu\Forms\Tests\TestSupport\Models;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
7+
class Category extends Model
8+
{
9+
protected $fillable = ['name'];
10+
11+
public function isTranslatable($attribute): bool
12+
{
13+
return $attribute == 'name';
14+
}
15+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
return new class () extends Migration {
8+
/**
9+
* Run the migrations.
10+
*
11+
* @return void
12+
*/
13+
public function up()
14+
{
15+
Schema::create('categories', function (Blueprint $table) {
16+
$table->bigIncrements('id');
17+
$table->string('name');
18+
$table->timestamps();
19+
});
20+
}
21+
22+
/**
23+
* Reverse the migrations.
24+
*
25+
* @return void
26+
*/
27+
public function down()
28+
{
29+
Schema::dropIfExists('categories');
30+
}
31+
};
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<x-forms::form>
2+
<x-forms::select name="exclude_sync[]" multiple exclude-sync-field>
3+
<option value="a">A</option>
4+
</x-forms::select>
5+
6+
<x-forms::select name="disabled_sync[]" multiple disabled>
7+
<option value="a">A</option>
8+
</x-forms::select>
9+
10+
<x-forms::select name="custom_sync[]" multiple sync-field-name="custom_sync_name">
11+
<option value="a">A</option>
12+
</x-forms::select>
13+
</x-forms::form>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<x-forms::form>
2+
<x-forms::select name="tags[]" :options="['a' => 'A']" tags multiple />
3+
</x-forms::form>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<x-forms::form>
2+
<x-forms::select name="category" :options="$options" />
3+
</x-forms::form>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<x-forms::form :model="$model">
2+
<x-forms::select2 name="article" :options="$options" is-ajax name-field="title" />
3+
</x-forms::form>

0 commit comments

Comments
 (0)