Skip to content

Commit

Permalink
Add support to show users of a recipe
Browse files Browse the repository at this point in the history
  • Loading branch information
ngmy committed Nov 17, 2015
1 parent 26e2f9e commit 348e9fe
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 82 deletions.
5 changes: 4 additions & 1 deletion app/Http/Controllers/RecipesController.php
Expand Up @@ -86,7 +86,10 @@ public function store(Request $request)
*/
public function show(Recipe $recipe)
{
return view('recipes.show')->with('recipe', $recipe);
$recipeProject = $recipe->getProjects()->toArray();

return view('recipes.show')->with('recipe', $recipe)
->with('recipeProject', $recipeProject);
}

/**
Expand Down
10 changes: 10 additions & 0 deletions app/Models/Recipe.php
Expand Up @@ -13,4 +13,14 @@ class Recipe extends BaseModel
'description',
'body',
];

public function projects()
{
return $this->belongsToMany('App\Models\Project');
}

public function getProjects()
{
return $this->projects()->orderBy('name')->get();
}
}
88 changes: 45 additions & 43 deletions resources/views/recipes/index.blade.php
Expand Up @@ -2,50 +2,52 @@

@section('content')
<div class="container-fluid">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<h1 class="page-header">Recipes</h1>
<div class="row">
<div class="col-md-8 col-md-offset-2">
<h1 class="page-header">Recipes</h1>

@if (Auth::user()->can('create.recipe'))
<div class="pull-right margin-bottom-lg">
{!! link_to_route('recipes.create', 'Create', [], ['class' => 'btn btn-primary btn-lg']) !!}
</div>
@endif
@if (Auth::user()->can('create.recipe'))
<div class="pull-right margin-bottom-lg">
{!! link_to_route('recipes.create', 'Create', [], ['class' => 'btn btn-primary btn-lg']) !!}
</div>
@endif

<table class="table table-striped">
<thead>
<tr>
<th><div align="center">Name</div></th>
<th><div align="center">Created At</div></th>
<th><div align="center">Updated At</div></th>
<th></th>
</tr>
</thead>
<tbody>
@foreach ($recipes as $recipe)
<tr>
<td>{{ $recipe->name }}</td>
<td>{{ $recipe->created_at }}</td>
<td>{{ $recipe->updated_at }}</td>
<td>
{!! link_to_route('recipes.show', 'Show', [$recipe->id], ['class' => 'btn btn-default']) !!}
@if (Auth::user()->can('update.recipe'))
{!! link_to_route('recipes.edit', 'Edit', [$recipe->id], ['class' => 'btn btn-default']) !!}
@endif
@if (Auth::user()->can('delete.recipe'))
{!! Form::open(['route' => ['recipes.destroy', $recipe->id], 'method' => 'delete', 'style' => 'display:inline']) !!}
{!! Form::submit('Destroy', ['class' => 'btn btn-danger']) !!}
{!! Form::close() !!}
@endif
</td>
</tr>
@endforeach
</tbody>
</table>
<div class="text-center">
{!! $recipes->render() !!}
</div>
</div>
</div>
<table class="table table-striped">
<thead>
<tr>
<th><div align="center">Name</div></th>
<th><div align="center">Users</div></th>
<th><div align="center">Created At</div></th>
<th><div align="center">Updated At</div></th>
<th></th>
</tr>
</thead>
<tbody>
@foreach ($recipes as $recipe)
<tr>
<td>{{ $recipe->name }}</td>
<td><div align="right">{{ number_format(count($recipe->getProjects())) }}</div></td>
<td>{{ $recipe->created_at }}</td>
<td>{{ $recipe->updated_at }}</td>
<td>
{!! link_to_route('recipes.show', 'Show', [$recipe->id], ['class' => 'btn btn-default']) !!}
@if (Auth::user()->can('update.recipe'))
{!! link_to_route('recipes.edit', 'Edit', [$recipe->id], ['class' => 'btn btn-default']) !!}
@endif
@if (Auth::user()->can('delete.recipe'))
{!! Form::open(['route' => ['recipes.destroy', $recipe->id], 'method' => 'delete', 'style' => 'display:inline']) !!}
{!! Form::submit('Destroy', ['class' => 'btn btn-danger']) !!}
{!! Form::close() !!}
@endif
</td>
</tr>
@endforeach
</tbody>
</table>
<div class="text-center">
{!! $recipes->render() !!}
</div>
</div>
</div>
</div>
@stop
64 changes: 39 additions & 25 deletions resources/views/recipes/show.blade.php
Expand Up @@ -2,30 +2,44 @@

@section('content')
<div class="container-fluid">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<table class="table">
<tbody>
<tr>
<th>Name</th>
<td>{{ $recipe->name }}</td>
</tr>
<tr>
<th>Description</th>
<td>{{ $recipe->description }}</td>
</tr>
<tr>
<th>Body</th>
<td><pre><code>{{ $recipe->body }}</code></pre></td>
</tr>
</tbody>
</table>
{!! link_to_route('recipes.index', 'Back', [], ['class' => 'btn btn-danger']) !!}
@if (Auth::user()->can('update.recipe'))
{!! link_to_route('recipes.edit', 'Edit', [$recipe->id], ['class' => 'btn btn-primary']) !!}
@endif
</div>
</div>
<div class="row">
<div class="col-md-8 col-md-offset-2">
<table class="table">
<tbody>
<tr>
<th>Name</th>
<td>{{ $recipe->name }}</td>
</tr>
<tr>
<th>Description</th>
<td>{{ $recipe->description }}</td>
</tr>
<tr>
<th>Body</th>
<td><pre><code>{{ $recipe->body }}</code></pre></td>
</tr>
@if (count($recipeProject) === 0)
<tr>
<th>Used By</th>
<td></td>
</tr>
@else
@foreach ($recipeProject as $i => $project)
<tr>
@if ($i === 0)
<th rowspan="{{ count($recipeProject) }}">Used By</th>
@endif
<td>{!! link_to_route('projects.show', $project['name'], $project['id']) !!}</td>
</tr>
@endforeach
@endif
</tbody>
</table>
{!! link_to_route('recipes.index', 'Back', [], ['class' => 'btn btn-danger']) !!}
@if (Auth::user()->can('update.recipe'))
{!! link_to_route('recipes.edit', 'Edit', [$recipe->id], ['class' => 'btn btn-primary']) !!}
@endif
</div>
</div>
</div>
@stop

29 changes: 16 additions & 13 deletions tests/Http/Controllers/RecipesControllerTest.php
Expand Up @@ -25,15 +25,21 @@ public function setUp()

$this->mockRecipeRepository = $this->mock('App\Repositories\Recipe\RecipeInterface');
$this->mockRecipeForm = $this->mock('App\Services\Form\Recipe\RecipeForm');
$this->mockRecipeModel = $this->mockPartial('App\Models\Recipe');
}

public function test_Should_DisplayIndexPage_When_IndexPageIsRequested()
{
$recipes = Factory::buildList('App\Models\Recipe', [
['id' => 1, 'name' => 'Recipe 1', 'description' => '', 'body' => '', 'created_at' => new Carbon\Carbon, 'updated_at' => new Carbon\Carbon],
['id' => 2, 'name' => 'Recipe 2', 'description' => '', 'body' => '', 'created_at' => new Carbon\Carbon, 'updated_at' => new Carbon\Carbon],
['id' => 3, 'name' => 'Recipe 3', 'description' => '', 'body' => '', 'created_at' => new Carbon\Carbon, 'updated_at' => new Carbon\Carbon],
]);
$recipes[] = $this->mockRecipeModel
->shouldReceive('getProjects')
->once()
->andReturn(new Illuminate\Database\Eloquent\Collection)
->mock();
$recipes[] = $this->mockRecipeModel
->shouldReceive('getProjects')
->once()
->andReturn(new Illuminate\Database\Eloquent\Collection)
->mock();

$perPage = 10;

Expand Down Expand Up @@ -87,14 +93,11 @@ public function test_Should_RedirectToCreatePage_When_StoreProcessFails()

public function test_Should_DisplayShowPage_When_ShowPageIsRequestedAndResourceIsFound()
{
$recipe = Factory::build('App\Models\Recipe', [
'id' => 1,
'name' => 'Recipe 1',
'description' => '',
'body' => '',
'created_at' => new Carbon\Carbon,
'updated_at' => new Carbon\Carbon,
]);
$recipe = $this->mockRecipeModel
->shouldReceive('getProjects')
->once()
->andReturn(new Illuminate\Database\Eloquent\Collection)
->mock();

$this->mockRecipeRepository
->shouldReceive('byId')
Expand Down

0 comments on commit 348e9fe

Please sign in to comment.