Skip to content

Commit 8f0bf37

Browse files
authored
Page objects after (#5)
* Adds product scaffold * Adds product system spec setup * Adds simple product system specs to refactor * Improves the test for product * Improves the specs to better handle the product publishing * Refactors the product spec file with a page object for product creation * Refactors the publish spec to page object * Moves the page class definition to a file * Adds assignment on README
1 parent 264a42f commit 8f0bf37

19 files changed

+264
-8
lines changed

.bash_history

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,5 @@ exit
3535
export
3636
#!/bin/bash -i
3737
export
38+
#!/bin/bash -i
39+
export

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ This repository will hold all of the exercises for the Advanced Testing Techniqu
1414

1515
## Assignments
1616

17-
You assignment for this will be to go to a local repository, legacy project or something you are currently working on, and refactor using one of this techniques. After that create a PR and assign it to @kurenn @vovimayhem @miguejs on github.
17+
You assignment for this, will be to refactor some system specs you have than can use this pattern, you know you have some!, refactor them, and place PR assigned to anyone from tech, @vovimayhem @miguejs or @kurenn.
1818

1919
Happy coding!
2020

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
class ProductsController < ApplicationController
2+
before_action :set_product, only: [:show, :edit, :update, :destroy]
3+
4+
# GET /products
5+
def index
6+
@products = Product.all
7+
end
8+
9+
# GET /products/1
10+
def show
11+
end
12+
13+
# GET /products/new
14+
def new
15+
@product = Product.new
16+
end
17+
18+
# GET /products/1/edit
19+
def edit
20+
end
21+
22+
# POST /products
23+
def create
24+
@product = Product.new(product_params)
25+
26+
if @product.save
27+
redirect_to @product, notice: 'Product was successfully created.'
28+
else
29+
render :new
30+
end
31+
end
32+
33+
# PATCH/PUT /products/1
34+
def update
35+
if @product.update(product_params)
36+
redirect_to @product, notice: 'Product was successfully updated.'
37+
else
38+
render :edit
39+
end
40+
end
41+
42+
# DELETE /products/1
43+
def destroy
44+
@product.destroy
45+
redirect_to products_url, notice: 'Product was successfully destroyed.'
46+
end
47+
48+
private
49+
# Use callbacks to share common setup or constraints between actions.
50+
def set_product
51+
@product = Product.find(params[:id])
52+
end
53+
54+
# Only allow a trusted parameter "white list" through.
55+
def product_params
56+
params.require(:product).permit(:name, :description, :price, :status)
57+
end
58+
end

app/models/product.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Product < ApplicationRecord
2+
enum status: {
3+
draft: 0,
4+
published: 1,
5+
cancelled: 2
6+
}
7+
8+
validates :name,
9+
:description,
10+
:price, presence: true
11+
end

app/views/products/_form.html.erb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<%= form_with(model: product, local: true) do |form| %>
2+
<% if product.errors.any? %>
3+
<div id="error_explanation">
4+
<h2><%= pluralize(product.errors.count, "error") %> prohibited this product from being saved:</h2>
5+
6+
<ul>
7+
<% product.errors.full_messages.each do |message| %>
8+
<li><%= message %></li>
9+
<% end %>
10+
</ul>
11+
</div>
12+
<% end %>
13+
14+
<div class="field">
15+
<%= form.label :name %>
16+
<%= form.text_field :name %>
17+
</div>
18+
19+
<div class="field">
20+
<%= form.label :description %>
21+
<%= form.text_area :description %>
22+
</div>
23+
24+
<div class="field">
25+
<%= form.label :price %>
26+
<%= form.text_field :price %>
27+
</div>
28+
29+
<div class="actions">
30+
<%= form.submit 'Create' %>
31+
</div>
32+
<% end %>

app/views/products/edit.html.erb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<h1>Editing Product</h1>
2+
3+
<%= render 'form', product: @product %>
4+
5+
<%= link_to 'Show', @product %> |
6+
<%= link_to 'Back', products_path %>

app/views/products/index.html.erb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<p id="notice"><%= notice %></p>
2+
3+
<h1>Products</h1>
4+
5+
<table>
6+
<thead>
7+
<tr>
8+
<th>Name</th>
9+
<th>Description</th>
10+
<th>Price</th>
11+
<th colspan="3"></th>
12+
</tr>
13+
</thead>
14+
15+
<tbody>
16+
<% @products.each do |product| %>
17+
<tr>
18+
<td><%= product.name %></td>
19+
<td><%= product.description %></td>
20+
<td><%= product.price %></td>
21+
<td><%= link_to 'Show', product %></td>
22+
<td><%= link_to 'Edit', edit_product_path(product) %></td>
23+
<td><%= link_to 'Destroy', product, method: :delete, data: { confirm: 'Are you sure?' } %></td>
24+
</tr>
25+
<% end %>
26+
</tbody>
27+
</table>
28+
29+
<br>
30+
31+
<%= link_to 'New Product', new_product_path %>

app/views/products/new.html.erb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<h1>New Product</h1>
2+
3+
<%= render 'form', product: @product %>
4+
5+
<%= link_to 'Back', products_path %>

app/views/products/show.html.erb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<p id="notice"><%= notice %></p>
2+
3+
<%= form_for @product do |f| %>
4+
<%= f.hidden_field :status, value: :published %>
5+
<%= f.submit 'Publish' %>
6+
<% end %>
7+
8+
<p class='product_title'>
9+
<%= @product.status.capitalize %> - <%= @product.name %>
10+
</p>
11+
12+
<p>
13+
<strong>Name:</strong>
14+
<%= @product.name %>
15+
</p>
16+
17+
<p>
18+
<strong>Description:</strong>
19+
<%= @product.description %>
20+
</p>
21+
22+
<p>
23+
<strong>Price:</strong>
24+
<%= @product.price %>
25+
</p>
26+
27+
<%= link_to 'Edit', edit_product_path(@product) %> |
28+
<%= link_to 'Back', products_path %>

config/routes.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
Rails.application.routes.draw do
2+
resources :products
23
devise_for :users
34
root to: "landing#index"
45
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html

0 commit comments

Comments
 (0)