Skip to content

Commit

Permalink
Add fallbacks for old keys
Browse files Browse the repository at this point in the history
  • Loading branch information
adevade committed Nov 25, 2024
1 parent 7899839 commit af97cf2
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
16 changes: 16 additions & 0 deletions packages/admin/src/Models/Staff.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,22 @@ class Staff extends Authenticatable implements FilamentUser, HasName
'full_name',
];

protected function firstName(): Attribute
{
return Attribute::make(
get: fn (): string => $this->first_name,
set: fn (?string $value) => $this->first_name = $value,
);
}

protected function lastName(): Attribute
{
return Attribute::make(
get: fn (): string => $this->last_name,
set: fn (?string $value) => $this->last_name = $value,
);
}

protected function fullName(): Attribute
{
return Attribute::get(
Expand Down
38 changes: 38 additions & 0 deletions tests/admin/Unit/Models/StaffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,41 @@
->and(\Lunar\Admin\Models\Staff::search('Bill')->get())->toHaveCount(1)
->and(\Lunar\Admin\Models\Staff::search('Joe Bloggs')->get())->toHaveCount(1);
});

test('can get first name by old key without underscore', function () {
$staff = \Lunar\Admin\Models\Staff::factory()->create([
'first_name' => 'Joe',
]);

expect($staff->firstname)->toBe('Joe');
});

test('can get last name by old key without underscore', function () {
$staff = \Lunar\Admin\Models\Staff::factory()->create([
'last_name' => 'Bloggs',
]);

expect($staff->lastname)->toBe('Bloggs');
});

test('can set first name by old key without underscore', function () {
$staff = \Lunar\Admin\Models\Staff::factory()->create([
'first_name' => 'Joe',
]);

$staff->firstname = 'Tim';

expect($staff->firstname)->toBe('Tim');
expect($staff->first_name)->toBe('Tim');
});

test('can set last name by old key without underscore', function () {
$staff = \Lunar\Admin\Models\Staff::factory()->create([
'last_name' => 'Bloggs',
]);

$staff->lastname = 'Chance';

expect($staff->lastname)->toBe('Chance');
expect($staff->last_name)->toBe('Chance');
});

0 comments on commit af97cf2

Please sign in to comment.