Skip to content

Commit

Permalink
Add meta migration
Browse files Browse the repository at this point in the history
  • Loading branch information
jarektkaczyk committed May 6, 2015
1 parent 862dd10 commit a7dcbff
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
/coverage/
/vendor/
composer.lock
composer.lock
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ If you want to know more about new extensions you can check our [Roadmap](#roadm
* [Mutator](#mutator)
* [Mixing extensions](#mixing-extensions)
* [Roadmap](#roadmap)
* [Contribution](#contribution)

# <a name="getting-started"></a>Getting Started

Expand Down Expand Up @@ -359,6 +360,7 @@ Feel free to mix the extensions, however mind that the **order of including trai

use Sofa\Eloquence\Eloquence; // base trait
use Sofa\Eloquence\Mappable; // extension trait
use Sofa\Eloquence\Mutable; // extension trait

class User extends \Eloquent {

Expand Down Expand Up @@ -520,3 +522,9 @@ $hotelsWithSauna = App\Hotel::whereNotNull('sauna')->get();
- [x] Easy model validation.

...and much more to come soon!

# <a name="contribution"></a>Contribution

All contributions are welcome, PRs must be **tested** and **PSR-2 compliant**.

Thanks to [contributors](https://github.com/jarektkaczyk/eloquence/graphs/contributors).
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sofa/eloquence",
"description": "Extensions for the Eloquent ORM.",
"description": "Extensions for the Eloquent ORM. Validation, attribute mutators, mapping relations, queryable meta attributes and more.",
"license": "MIT",
"support": {
"issues": "https://github.com/jarektkaczyk/eloquence/issues",
Expand All @@ -9,10 +9,11 @@
"keywords": [
"laravel",
"eloquent",
"meta",
"orm",
"database",
"active record",
"sql"
"sql",
],
"authors": [
{
Expand Down
46 changes: 46 additions & 0 deletions src/Metable/CreateMetaAttributesTable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php namespace Sofa\Eloquence\Metable;

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateMetaAttributesTable extends Migration
{
/**
* Meta attributes table name.
*
* @todo allow table name customization via config
*
* @var string
*/
protected $table = 'meta_attributes';

/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create($this->table, function(Blueprint $table)
{
$table->increments('meta_id');
$table->string('meta_key');
$table->longText('meta_value');
$table->string('meta_type')->default('string');
$table->morphs('metable');

$table->index('meta_key');
$table->index(['meta_key', 'meta_value']);
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop($this->table);
}
}
26 changes: 23 additions & 3 deletions src/Validable.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,17 @@ public function validationEnabled()
*/
public function getValidationErrors()
{
return $this->getValidator()->errors();
return $this->getMessageBag();
}

/**
* Retrieve validation error messages.
*
* @return \Illuminate\Support\MessageBag
*/
public function getMessageBag()
{
return $this->getValidator()->getMessageBag();
}

/**
Expand Down Expand Up @@ -217,11 +227,21 @@ protected static function gatherRules()
$group = static::getRulesGroup($groupName);

if (isset($group[$key])) {
$rule = is_array($group[$key])
$rules = is_array($group[$key])
? $group[$key]
: explode('|', $group[$key]);

$result[$key] = array_unique(array_merge($result[$key], $rule));
foreach ($rules as &$rule) {
if ($rule === 'unique') {
$table = (new static)->getTable();

$rule .= ":{$table}";
}
}

unset($rule);

$result[$key] = array_unique(array_merge($result[$key], $rules));
}
}
}
Expand Down
8 changes: 5 additions & 3 deletions tests/ValidableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public function it_gets_invalid_attributes_from_validator_instance()
$messageBag = new MessageBag(['name' => 'name is required']);
$model = $this->getModel();
$validator = $model->getValidator();
$validator->shouldReceive('errors')->once()->andReturn($messageBag);
$validator->shouldReceive('getMessageBag')->once()->andReturn($messageBag);

$this->assertEquals(['name'], $model->getInvalidAttributes());
}
Expand All @@ -104,7 +104,7 @@ public function it_gets_error_messages_from_validator_instance()
$messageBag = new MessageBag(['name' => 'name is required']);
$model = $this->getModel();
$validator = $model->getValidator();
$validator->shouldReceive('errors')->once()->andReturn($messageBag);
$validator->shouldReceive('getMessageBag')->once()->andReturn($messageBag);

$this->assertEquals(new MessageBag(['name' => 'name is required']), $model->getValidationErrors());
}
Expand Down Expand Up @@ -152,8 +152,10 @@ protected function getModel()
class ValidableEloquentStub extends Model {
use Eloquence, Validable;

protected $table = 'users';

protected static $rules = [
'email' => 'required|email|unique:users',
'email' => 'required|email|unique',
'name' => ['required', 'max:10', 'unique:users,username,null,id,account_id,5'],
];

Expand Down

0 comments on commit a7dcbff

Please sign in to comment.