Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Internal PR #2 #3

Merged
merged 3 commits into from
Apr 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions app/controllers/products_controller.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,55 @@
class ProductsController < ApplicationController
def index
@products = Product.all
end

def new
@product = Product.new
end

def create
@product = Product.new(product_params)
@product.save
if @product.save
redirect_to products_path
else

render :new, status: :bad_request
flash[:failure] = "Could not add your product"
flash[:messages] = @product.errors.messages
end
end

def show
@product = Product.find_by(id: params[:id])
head :not_found if @product.nil?
end

def edit
@product = Product.find_by(id: params[:id])
end


def update
@product = Product.find_by(id: params[:id])
@product.update_attributes(product_params)
@product.save

if @product.save
redirect_to product_path(params[:id])
else
render :edit, status: :bad_request
end
end

def destroy
@product = Product.find_by(id: params[:id])
@product.destroy
redirect_to products_path
end

private
def product_params
return params.require(:product).permit(:name, :quantity, :category, :price, :description, :merchant_id)
end
end
16 changes: 16 additions & 0 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,22 @@
</head>

<body>
<header>
</header

<% if flash[:result_text] or flash[:message] %>
<section class="row status <%= flash[:status] %>">
<h3><%= flash[:status] == :failure ? "A problem occurred: " : "" %><%= flash[:result_text] %></h3>
<% if flash[:message] %>

<% flash.each do |name, problems| %>
<em> <%= problems %></em> <br>
<% end %>

<% end %>
</section>
<%end%>

<%= yield %>
</body>
</html>
Empty file.
Empty file.
12 changes: 12 additions & 0 deletions app/views/products/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<% if flash[:result_text] or flash[:message] %>
<section class="row status <%= flash[:status] %>">
<h3><%= flash[:status] == :failure ? "A problem occurred: " : "" %><%= flash[:result_text] %></h3>
<% if flash[:message] %>

<% flash.each do |name, problems| %>
<em> <%= problems %></em> <br>
<% end %>

<% end %>
</section>
<%end%>
12 changes: 12 additions & 0 deletions app/views/products/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<% if flash[:result_text] or flash[:message] %>
<section class="row status <%= flash[:status] %>">
<h3><%= flash[:status] == :failure ? "A problem occurred: " : "" %><%= flash[:result_text] %></h3>
<% if flash[:message] %>

<% flash.each do |name, problems| %>
<em> <%= problems %></em> <br>
<% end %>

<% end %>
</section>
<%end%>
12 changes: 12 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
Rails.application.routes.draw do
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html

get '/products', to: 'products#index'
get '/products/new', to:'products#new', as: "new_product"
post '/products', to: 'products#create'
get '/products/:id', to:'products#show', as:'product'
get 'products/:id/edit', to:'products#edit', as: 'edit_product'
patch '/products/:id', to:'products#update'


delete '/products/:id', to:'products#destroy'




get '/orders/new', to: "orders#new", as: 'new_order'
get '/orders', to: "orders#index", as: 'orders'
Expand Down
105 changes: 102 additions & 3 deletions test/controllers/products_controller_test.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,106 @@
require "test_helper"

describe ProductsController do
# it "must be a real test" do
# flunk "Need real tests"
# end
describe "index" do
it "lists all the products" do
Product.count.must_be :>, 0
get products_path
must_respond_with :success
end

it "still responds successfully when there are no works" do
Product.destroy_all
get products_path
must_respond_with :success
end

end

describe "new" do
it "successfully loads the new product page" do
get new_product_path
must_respond_with :success
end
end

describe "create" do
it "adds a product to the database" do
start_count = Product.count
product_data = {product:{name:"green", quantity:5, price:10, category:"toy", merchant_id: Merchant.first.id}}

post products_path, params: product_data
must_redirect_to products_path
end_count = Product.count
end_count.must_be :==, start_count + 1
end

it "re-renders the new work form if the work is invalid" do
start_count = Product.count
product_data = {product:{name:"green", quantity:5, price:10, category:"toy", merchant_id:""}}

post products_path, params: product_data
must_respond_with :bad_request
end_count = Product.count
end_count.must_be :==, start_count

end
end

describe "show" do
it "must show a specified product" do
first = products(:one)
get product_path(first.id)
must_respond_with :success
end

it "returns 404 not found if the product id doesn't exist" do
product_id = Product.last.id
product_id += 1
get product_path(product_id)
must_respond_with :not_found
end
end

describe "edit" do
it "successfully loads the edit product page" do
product = products(:one)
get edit_product_path(product.id)
must_respond_with :success
end
end

describe "update" do
it "changes a record in the database" do
product = products(:one)
product_data = {product:{name:"NewName"}}
patch product_path(product.id), params: product_data

must_redirect_to product_path(product.id)
end

it "redirects to edit path if update invalid" do
product = products(:one)
product_data = {product:{name:""}}
patch product_path(product), params: product_data

must_respond_with :bad_request
# must_redirect_to edit_product_path
#removed test for edit redirect because the :bad_request message was
#all that was getting through

#if I delete that line in the controller then I can detec the redirect
end
end

describe "destroy" do
it "destroys a record in the database" do
product = products(:one)
start_count = Product.count
delete product_path(product)
must_redirect_to products_path
end_count = Product.count

end_count.must_be :==, start_count - 1
end
end
end
4 changes: 3 additions & 1 deletion test/fixtures/products.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ one:
quantity: 3
price: 1.5
category: doll
merchant: :one
id: 1

aliens_two:
name: aliens
Expand Down Expand Up @@ -75,4 +77,4 @@ no_category:
description: she's a tour guide, she's a barbie
quantity: 5
price: -5
category:
category:
28 changes: 18 additions & 10 deletions test/models/product_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,25 @@
product.valid?.must_equal false
end
end
end


#merging this into master with the following test commented out because
#im getting some wierd errors on the Merchant model that I can't get green
#right now

describe "testing relations" do

it "has one merchant (belongs to a merchant)" do
product = products(:one)
product.merchant.nil?.must_equal false
end

it "can find a merchant's name through the relationship" do
product = products(:one)
product.merchant.name.must_equal "Marisol"
end

# describe "testing relations" do
#
# it "has one merchant (belongs to a merchant)" do
# product = products(:one)
# product.merchant.nil?.must_equal false
# end
it "can be found through the merchant's information" do
merchant = merchants(:one)
merchant.products.find_by(name:"aliens").class.must_equal Product
end
#Order-Products class has not been built out. Will test more after
end
end