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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kelsey 1027 #43

Merged
merged 9 commits into from
Oct 28, 2019
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
11 changes: 7 additions & 4 deletions app/controllers/categories_controller.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
class CategoriesController < ApplicationController

def index; end

def new
@category = Category.new
end

def create
@category = Category.new

end

def show
Expand All @@ -14,9 +17,9 @@ def show
@category = Category.find_by(id: category_id)
@products = @category.products
end

private

def category_params
return params.require(:category).permit(:name)
end
Expand Down
26 changes: 20 additions & 6 deletions app/controllers/order_items_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,20 @@ def index
end

def create
# kelsey problems #
# no idea why default attribute values don't work in tests? :|
if session[:order_id].nil?
# creates a new order if one doesn't exist
@order = Order.create
@order_id = @order.id
else
# uses current order if it exists
@order_id = session[:order_id]
end
if @qty.nil?
# set qty to 1
@qty = 1
end

order_item = OrderItem.new( order_item_params )
# end kelsey problems #

if order_item.save
flash[:success] = "Item added to order"
Expand Down Expand Up @@ -36,6 +46,10 @@ def update
return
end
end
if params[:qty] < 0
flash[:error] = "You cannot order fewer than 1"
redirect_to edit_order_path(@order.id)
end
if @order_item.update(qty: params[:quantity])
flash[:success] = "Successfully updated order"
redirect_to edit_order_path(@order.id)
Expand All @@ -50,9 +64,9 @@ def destroy; end


def order_item_params

return params.require(:order_item).permit(:product_id, :subtotal, :order_id, :qty)
return params.require(:order_item).permit(:product_id, :subtotal).merge(qty: @qty, order_id: @order_id)
end


end
4 changes: 3 additions & 1 deletion app/controllers/orders_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ class OrdersController < ApplicationController

def index
@orders = Order.all
@status = "pending"
end

# placeholder method
Expand All @@ -14,6 +15,7 @@ def new
end

def create
@status = "pending"
@order = Order.new( order_params )
if @order.save
# sets the session order id
Expand Down Expand Up @@ -79,7 +81,7 @@ def find_order

def order_params
# not sure whether it will return an order item or a product in the params
return params.require(:order).permit(:status, :order_items, :grand_total)
return params.require(:order).permit(:order_items, :grand_total).merge(status: @status)
end

end
2 changes: 1 addition & 1 deletion app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def make_thumbnail_link(productInstance)
# ex: <%= make_thumbnail_link(@product1) %>
if productInstance.class == Product
if productInstance.img_url != nil
return link_to image_tag(productInstance.img_url, alt: 'picture of #{productInstance.name}', class: 'thumbnail'), product_path(id: productInstance.id)
return link_to image_tag(productInstance.img_url, alt: 'picture of ' + productInstance.name, class: 'thumbnail'), product_path(id: productInstance.id)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yay!

else
return "NO IMG AVAILABLE"
end
Expand Down
9 changes: 0 additions & 9 deletions app/models/order.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
class Order < ApplicationRecord

before_save :default_values

has_many :order_items

# default_values code src:
# https://stackoverflow.com/questions/1550688/how-do-i-create-a-default-value-for-attributes-in-rails-activerecords-model
# retrieved 10/26/19

def default_values
self.status ||= "pending"
end

private

Expand Down
1 change: 0 additions & 1 deletion app/models/review.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ class Review < ApplicationRecord

validates :description, presence: true
# validates :description, presence: true, length: {maximum: 255}, on: :create, allow_nil: false


# VALIDATIONS:
# 1. Rating must be present
Expand Down
5 changes: 3 additions & 2 deletions app/views/products/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@

<%= link_to "View Products by Seller " + @merchant.name, merchant_products_path(@merchant.id) %>
<% if @product.merchant_id != session[:merchant_id] %>
<%= link_to "Add To Cart", order_items_path(order_item: { product_id: @product.id, order_id: (@order ? @order.id : Order.create.id), qty: 1 } ), method: :post, permitted: true %>

<%= link_to "Add To Cart", order_items_path(order_item: { product_id: @product.id } ), method: :post, permitted: true %>
<% end %>

<%= link_to "Create Review", new_product_review_path(product_id: @product.id), class:"btn btn-primary" %>

<%= link_to "Create Review", new_product_review_path(product_id: @product.id), class:"btn btn-primary" %>
10 changes: 6 additions & 4 deletions test/controllers/order_items_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,23 @@
before do
@order = Order.find_by(grand_total: 111111)
@product = Product.find_by(name: "product2")
@order_item = OrderItem.create(order_id: @order.id, product_id: @product.id)
@order_item = OrderItem.create(product_id: @product.id)
end

describe "create" do
it "can create an Order Item" do
it "can create an Order Item with a new Order" do
params = {
product: @product
product_id: @product.id
}
item = OrderItem.create(params)
p item

expect(item.product_id).must_equal @product.id
expect(item.qty).must_equal 1
end

it "can create an Order Item in a current Order" do
end

it "can calculate the subtotal" do
expect(@order_item.subtotal).must_be_instance_of Integer

Expand Down
18 changes: 18 additions & 0 deletions test/controllers/orders_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,25 @@
end

describe "update" do
it "updates the order if an order item is updated" do
end

it "cancels the order if all items are removed from order" do
end

it "gives an error if the order can't be updated" do
end
end

describe "merchants" do
it "can display all orders with items belonging to the logged-in merchant" do
end

it "will not display an order with no items belonging to the logged-in merchant" do
end

it "will not display any orders if there is no logged-in merchant" do
end
end


Expand Down
42 changes: 42 additions & 0 deletions test/controllers/products_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,47 @@

describe ProductsController do


describe "index/show" do
it "can show product#index page" do
get products_path
must_respond_with :success
end

it "can retrieve products by category" do
@product = Product.last
@categories = @product.categories

get categories_path(@categories.first.id)
must_respond_with :success

end

it "can retrieve products by merchant" do

end

it "can retrieve details about a product" do
end

end

describe "create" do
it "can create new product by logged in merchant" do
@merchant = Merchant.first
perform_login(@merchant)


end

it "will not create new product by not-logged-in user" do
end

end





describe "index" do
it "responds with success when there is at least one Product saved" do
Expand Down Expand Up @@ -62,4 +103,5 @@




end
1 change: 1 addition & 0 deletions test/fixtures/categories.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
c1:
name: Toys
products: p1

c2:
name: Live Animals
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/products.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,5 @@ p4:
merchant: m2
description: evil ducks
img_url: https://live.staticflickr.com/1203/566154297_84ec445540_z.jpg
categories: c2