Browse Source

Add name link method to Project and User model

Move HasRoles trait methods to User Model
pull/1/head
Nafies Luthfi 8 years ago
parent
commit
72483c743b
  1. 5
      app/Entities/Projects/Project.php
  2. 59
      app/Entities/Users/HasRoles.php
  3. 59
      app/Entities/Users/User.php
  4. 4
      resources/views/projects/index.blade.php
  5. 9
      tests/Unit/Models/ProjectTest.php
  6. 19
      tests/Unit/Models/UserTest.php

5
app/Entities/Projects/Project.php

@ -19,6 +19,11 @@ class Project extends Model {
protected $guarded = ['id','created_at','updated_at']; protected $guarded = ['id','created_at','updated_at'];
// protected $dates = ['start_date','end_date']; // protected $dates = ['start_date','end_date'];
public function nameLink()
{
return link_to_route('projects.show', $this->name, [$this->id], ['target' => '_blank']);
}
public function features() public function features()
{ {
return $this->hasMany(Feature::class)->orderBy('position'); return $this->hasMany(Feature::class)->orderBy('position');

59
app/Entities/Users/HasRoles.php

@ -1,59 +0,0 @@
<?php
namespace App\Entities\Users;
trait HasRoles
{
/**
* A user may have multiple roles.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function roles()
{
return $this->belongsToMany(Role::class);
}
/**
* Assign the given role to the user.
*
* @param string $role
* @return mixed
*/
public function assignRole($role)
{
return $this->roles()->save(
Role::whereName($role)->firstOrFail()
);
}
/**
* Determine if the user has the given role.
*
* @param mixed $role
* @return boolean
*/
public function hasRole($role)
{
if (is_string($role)) {
return $this->roles->contains('name', $role);
}
return !! $role->intersect($this->roles)->count();
}
public function hasRoles(array $roleNameArray)
{
return $this->roles->contains(function($role, $key) use ($roleNameArray) {
return in_array($role->name, $roleNameArray);
});
}
public function scopeHasRoles($query, array $roleNameArray)
{
return $query->whereHas('roles', function($q) use ($roleNameArray) {
$q->whereIn('name',$roleNameArray);
});
}
}

59
app/Entities/Users/User.php

@ -9,7 +9,7 @@ use Laracasts\Presenter\PresentableTrait;
class User extends Authenticatable class User extends Authenticatable
{ {
use Notifiable, PresentableTrait, HasRoles;
use Notifiable, PresentableTrait;
protected $fillable = ['name', 'email', 'password']; protected $fillable = ['name', 'email', 'password'];
protected $hidden = ['password', 'remember_token', 'api_token']; protected $hidden = ['password', 'remember_token', 'api_token'];
@ -19,4 +19,61 @@ class User extends Authenticatable
{ {
$this->attributes['password'] = bcrypt($value); $this->attributes['password'] = bcrypt($value);
} }
public function nameLink()
{
return link_to_route('users.show', $this->name, [$this->id], ['target' => '_blank']);
}
/**
* A user may have multiple roles.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function roles()
{
return $this->belongsToMany(Role::class);
}
/**
* Assign the given role to the user.
*
* @param string $role
* @return mixed
*/
public function assignRole($role)
{
return $this->roles()->save(
Role::whereName($role)->firstOrFail()
);
}
/**
* Determine if the user has the given role.
*
* @param mixed $role
* @return boolean
*/
public function hasRole($role)
{
if (is_string($role)) {
return $this->roles->contains('name', $role);
}
return !! $role->intersect($this->roles)->count();
}
public function hasRoles(array $roleNameArray)
{
return $this->roles->contains(function($role, $key) use ($roleNameArray) {
return in_array($role->name, $roleNameArray);
});
}
public function scopeHasRoles($query, array $roleNameArray)
{
return $query->whereHas('roles', function($q) use ($roleNameArray) {
$q->whereIn('name',$roleNameArray);
});
}
} }

4
resources/views/projects/index.blade.php

@ -11,7 +11,7 @@
<div class="pull-left hidden-xs">{!! str_replace('/?', '?', $projects->appends(Request::except('page'))->render()) !!}</div> <div class="pull-left hidden-xs">{!! str_replace('/?', '?', $projects->appends(Request::except('page'))->render()) !!}</div>
{!! Form::open(['method'=>'get','class'=>'form-inline']) !!} {!! Form::open(['method'=>'get','class'=>'form-inline']) !!}
{!! FormField::select('status', getProjectStatusesList(), ['value' => Request::get('status'), 'placeholder'=> 'Semua Project']) !!} {!! FormField::select('status', getProjectStatusesList(), ['value' => Request::get('status'), 'placeholder'=> 'Semua Project']) !!}
{!! Form::text('q', Request::get('q'), ['class'=>'form-control index-search-field','placeholder'=>trans('project.search'),'style' => 'width:350px']) !!}
{!! Form::text('q', Request::get('q'), ['class'=>'form-control index-search-field','placeholder'=>trans('project.search'),'style' => 'width:100%;max-width:350px']) !!}
{!! Form::submit(trans('project.search'), ['class' => 'btn btn-info btn-sm']) !!} {!! Form::submit(trans('project.search'), ['class' => 'btn btn-info btn-sm']) !!}
{!! link_to_route('projects.index','Reset',[],['class' => 'btn btn-default btn-sm']) !!} {!! link_to_route('projects.index','Reset',[],['class' => 'btn btn-default btn-sm']) !!}
{!! Form::close() !!} {!! Form::close() !!}
@ -33,7 +33,7 @@
@forelse($projects as $key => $project) @forelse($projects as $key => $project)
<tr> <tr>
<td>{{ $projects->firstItem() + $key }}</td> <td>{{ $projects->firstItem() + $key }}</td>
<td>{{ $project->name }}</td>
<td>{{ $project->nameLink() }}</td>
<td class="text-center">{{ $project->start_date }}</td> <td class="text-center">{{ $project->start_date }}</td>
<td class="text-right">{{ $project->present()->workDuration }}</td> <td class="text-right">{{ $project->present()->workDuration }}</td>
{{-- <td class="text-center">{{ $project->payments_count }}</td> --}} {{-- <td class="text-center">{{ $project->payments_count }}</td> --}}

9
tests/Unit/Models/ProjectTest.php

@ -131,4 +131,13 @@ class ProjectTest extends TestCase
$project = factory(Project::class)->create(); $project = factory(Project::class)->create();
$this->assertTrue($project->files instanceof Collection); $this->assertTrue($project->files instanceof Collection);
} }
/** @test */
public function it_has_name_link_method()
{
$project = factory(Project::class)->make();
$this->assertEquals(link_to_route('projects.show', $project->name, [$project->id], [
'target' => '_blank'
]), $project->nameLink());
}
} }

19
tests/Unit/Models/UserTest.php

@ -0,0 +1,19 @@
<?php
namespace Tests\Unit\Models;
use App\Entities\Users\User;
use Tests\TestCase;
class UserTest extends TestCase
{
/** @test */
public function it_has_name_link_method()
{
$user = factory(User::class)->create();
$this->assertEquals(link_to_route('users.show', $user->name, [$user->id], [
'target' => '_blank'
]), $user->nameLink());
}
}
Loading…
Cancel
Save