Skip to content

Commit

Permalink
Merge pull request #7 from nzesalem/revert-6-feature/allow-any-column…
Browse files Browse the repository at this point in the history
…-for-status

Revert "Allow another column for status"
  • Loading branch information
nzesalem authored Oct 13, 2020
2 parents b07622b + b4ec85d commit 96419a2
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 121 deletions.
7 changes: 0 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,6 @@ $user = User::whereRaw('status = ' . User::getStatusCode('suspended'))->first();
$user->status == 'suspended' // true
```
To use a custom field in your model for status, add the `$statusFieldName` Property to the model. Example:
```php
class User {
protected $statusFieldName = 'sent_status';
}
```
Getting all the defined statuses for a given model is also easy as the snippet below. We get all the defined statuses for the `User` model and display them in a select element:
```php
Expand Down
74 changes: 5 additions & 69 deletions src/Traits/LastusTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,72 +2,10 @@

namespace Nzesalem\Lastus\Traits;

use ErrorException;
use Nzesalem\Lastus\Lastus;

trait LastusTrait
{
/**
* Return the status field name set in Model or return status (by default)
*
* @return string
*/
protected function getStatusFieldName() {
try {
return $this->statusFieldName;
} catch(ErrorException $ex) {
return 'status';
}
}

/**
* Set status field name
*
* @param string $fieldName
* @return void
*/
public function setStatusFieldName($fieldName) {
$this->statusFieldName = $fieldName;
}

/**
* Dynamically retrieve attributes on the model.
*
* @param string $key
* @return mixed
*/
public function getAttribute($key, $defaultVal=null)
{
if ($key === $this->getStatusFieldName()) {
$value = parent::getAttribute($key, 1);
return $this->getLastusStatus($value);
}

return parent::getAttribute($key, $defaultVal);
}

/**
* Dynamically retrieve attributes on the model.
*
* @param string $key
* @return mixed
*/
/**
* Dynamically set attributes on the model.
*
* @param string $key
* @param mixed $value
* @return void
*/
public function setAttribute($key, $value)
{
if ($key === $this->getStatusFieldName()) {
$this->setLastusStatus($key, $value);
} else {
parent::setAttribute($key, $value);
}
}

/**
* Status accessor
*
Expand All @@ -76,12 +14,11 @@ public function setAttribute($key, $value)
*
* @throws \InvalidArgumentException
*/
public function getLastusStatus($value)
public function getStatusAttribute($value)
{
if (! is_numeric($value)) {
throw new \InvalidArgumentException(sprintf('Model %s should be stored as an integer, %s given', $this->getStatusFieldName(), $value));
throw new \InvalidArgumentException('Model status should be stored as an integer');
}

return Lastus::statusName(static::class, $value);
}

Expand All @@ -93,13 +30,12 @@ public function getLastusStatus($value)
*
* @throws \InvalidArgumentException
*/
public function setLastusStatus($statusField, $value)
public function setStatusAttribute($value)
{
if (! is_string(($value))) {
throw new \InvalidArgumentException(sprintf('Expecting a string for model %s', $this->getStatusFieldName()));
throw new \InvalidArgumentException('Expecting a string for model status');
}

$this->attributes[$statusField] = Lastus::getStatusCode(static::class, $value);
$this->attributes['status'] = Lastus::getStatusCode(static::class, $value);
}

/**
Expand Down
43 changes: 0 additions & 43 deletions tests/BaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use Nzesalem\Lastus\Lastus;
use Illuminate\Support\Facades\DB;
use Nzesalem\Lastus\Tests\Models\User;
use Nzesalem\Lastus\Tests\Models\Email;

/**
* Class BaseTest
Expand All @@ -15,7 +14,6 @@
*/
class BaseTest extends TestCase
{
protected $statusFieldName = 'status';
/**
* Test basic Lastus functionality.
*/
Expand All @@ -32,47 +30,6 @@ public function testStatusMutatorAndAccessor()
$this->assertEquals('active', $user->status);
}

/**
* Test basic Lastus functionality.
*/
public function testMutatorAndAccessorWithCustomStatusField()
{
$user = new User();
$user->setStatusFieldName('email');

$user->fill([
'name' => 'Salem Nzeukwu',
'email' => 'blocked',
'password' => bcrypt('secret'),
'custom_status' => 'blocked',
]);

$user->save();

$this->assertEquals('blocked', $user->email);
}

/**
* Test basic Lastus functionality.
*/
public function testUpdateWithCustomStatusField()
{
$user = new User([
'name' => 'Salem Nzeukwu',
'email' => 'blocked',
'password' => bcrypt('secret'),
'custom_status' => 'blocked',
]);

$user->setStatusFieldName('email');
$user->save();

$user->email = 'active';

$this->assertEquals('active', $user->email);
$this->assertEquals(1, $user->getAttributes()['email']);
}

public function testModelStatusCodeMethod()
{
$now = Carbon::now();
Expand Down
2 changes: 0 additions & 2 deletions tests/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,4 @@ class User extends Authenticatable
protected $hidden = [
'password', 'remember_token',
];

protected $statusFieldName = 'status';
}

0 comments on commit 96419a2

Please sign in to comment.