Skip to content

Commit

Permalink
Add hidden attributes to the preference model
Browse files Browse the repository at this point in the history
Add support for hiding preference attributes from JSON export by
defining hidden attributes in either the config file or the
MODEL_PREFERENCE_HIDDEN_ATTRIBUTES constant.

See issue #2 for the feature request.
  • Loading branch information
klaude committed Apr 24, 2016
1 parent 6c212e5 commit 6103195
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 1 deletion.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Use this library to bind multiple key/value pair preferences to your application
* [Removing Preferences](#removing-preferences)
* [Default Preference Values](#default-preference-values)
* [Casting Preference Values](#casting-preference-values)
* [Hidden Preference Attributes](#hidden-preference-attributes)

<a name="installation"></a>
## Installation
Expand Down Expand Up @@ -230,3 +231,27 @@ class MyModel extends Model
```

As with default values, casting preferences is only performed when using the `getPreference()`, `prefers()`, `setPreference()`, and `setPreferences()` helper methods.

<a name="hidden-preference-attributes"></a>
### Hidden Preference Attributes

By default all preference model attributes are visible when exporting to JSON. However it is possible to declare hidden attributes that act in the same manner as [Eloquent's hidden attributes](https://laravel.com/docs/5.2/eloquent-serialization#hiding-attributes-from-json). There are two ways to declare which preference attributes to hide from JSON export:

1) If this library is being used in a Laravel project then declare hidden attributes in the "hidden-attributes" key in `config/eloquent-preferences.php`.

```
return [
// ...
'hidden-attributes' => ['created_at', 'updated_at'],
// ...
];
```

2) If this library is being used outside the Laravel framework then define the `MODEL_PREFERENCE_HIDDEN_ATTRIBUTES` constant at your project's point of entry with a comma-separated list of attributes to hide from JSON export.

```php
const MODEL_PREFERENCE_HIDDEN_ATTRIBUTES = 'created_at,updated_at';
```
11 changes: 11 additions & 0 deletions config/eloquent-preferences.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,15 @@

'table' => 'model_preferences',

/*
|--------------------------------------------------------------------------
| "Hidden" preference columns
|--------------------------------------------------------------------------
|
| The names of the attributes to hide when exporting preferences to JSON.
|
*/

'hidden-attributes' => [],

];
23 changes: 23 additions & 0 deletions src/Preference.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class Preference extends Model
public function __construct(array $attributes = [])
{
$this->setTable($this->getQualifiedTableName());
$this->overrideHidden();

parent::__construct($attributes);
}
Expand Down Expand Up @@ -67,4 +68,26 @@ public function getQualifiedTableName()
// Otherwise use the default.
return self::DEFAULT_MODEL_PREFERENCE_TABLE;
}

/**
* Set hidden preference attributes.
*
* Declare hidden from JSON attributes for preferences either via Laravel
* config or by a comma-separated string constant
* MODEL_PREFERENCE_HIDDEN_ATTRIBUTES.
*
* @see https://laravel.com/docs/5.2/eloquent-serialization#hiding-attributes-from-json
*/
protected function overrideHidden()
{
if (function_exists('config')) {
return $this->setHidden(config('eloquent-preferences.hidden-attributes', $this->getHidden()));
}

if (defined('MODEL_PREFERENCE_HIDDEN_ATTRIBUTES')) {
return $this->setHidden(explode(',', MODEL_PREFERENCE_HIDDEN_ATTRIBUTES));
}

return $this;
}
}
26 changes: 26 additions & 0 deletions tests/PreferenceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,30 @@ public function testSetTheTableNameByLaravelConfig()

$this->assertEquals('foo-function', (new Preference)->getTable());
}

public function testPreferencesHaveNoHiddenAttributesByDefault()
{
$this->assertEquals([], (new Preference)->getHidden());
}

/**
* @runInSeparateProcess
*/
public function testSetHiddenAttributesByConstant()
{
define('MODEL_PREFERENCE_HIDDEN_ATTRIBUTES', 'foo,constant');

$this->assertEquals(['foo', 'constant'], (new Preference)->getHidden());
}

/**
* @runInSeparateProcess
*/
public function testSetHiddenAttributesByLaravelConfig()
{
// Define config() in the global namespace
require_once __DIR__ . '/Support/helpers.php';

$this->assertEquals(['foo', 'function'], (new Preference)->getHidden());
}
}
10 changes: 9 additions & 1 deletion tests/Support/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,13 @@
*/
function config($key = null, $default = null)
{
return 'foo-function';
switch ($key) {
case 'eloquent-preferences.table':
return 'foo-function';
break;

case 'eloquent-preferences.hidden-attributes':
return ['foo', 'function'];
break;
}
}

0 comments on commit 6103195

Please sign in to comment.