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

Story 15 #134

Merged
merged 8 commits into from
Apr 3, 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
2 changes: 2 additions & 0 deletions app/controllers/items_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
class ItemsController < ApplicationController
def index
@items = Item.enabled_items
@top_5 = Item.sort_sold("DESC")
@bottom_5 = Item.sort_sold("ASC")
end
end
15 changes: 15 additions & 0 deletions app/models/item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,19 @@ class Item < ApplicationRecord
def self.enabled_items
where(enabled: true)
end

def self.sort_sold(order)
joins(:order_items)
.select("items.*, sum(order_items.quantity)as item_count")
.where(enabled: true)
.where("order_items.fulfilled = ?", true)
.group(:id)
.order("item_count #{order}")
.limit(5)
end

def total_sold
order_items.where("order_items.fulfilled = ?", true)
.sum('order_items.quantity')
end
whois-jon-peterson marked this conversation as resolved.
Show resolved Hide resolved
end
14 changes: 14 additions & 0 deletions app/views/items/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
<h1>All Items:</h1>

<ul id="most-popular-items">
<h3>5 Most Popular Items: </h3>
<% @top_5.each do |item| %>
<li id="item"><%= item.name %>: <%= item.total_sold %> sold</li>
<% end %>
</ul>

<ul id="least-popular-items">
<h3>5 Least Popular Items: </h3>
<% @bottom_5.each do |item| %>
<li id="item"><%= item.name %>: <%= item.total_sold %> sold</li>
<% end %>
</ul>

<% @items.each do |item| %>
<section id="item-<%= item.id %>">
<h3><%= link_to "#{item.name}", item_path(item) %></h3>
Expand Down
1 change: 0 additions & 1 deletion spec/factories/orders.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
FactoryBot.define do
factory :order do
user

status { 0 }
end

Expand Down
82 changes: 65 additions & 17 deletions spec/features/items/index_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,80 @@

RSpec.describe "Items index page" do
context "as any kind of user" do
before :each do
@merchant1 = create(:merchant)
@merchant2 = create(:merchant)
@item1 = create(:item, user: @merchant1)
@item2 = create(:item, user: @merchant2)
@item3 = create(:item, user: @merchant1)
@item4 = create(:inactive_item, user: @merchant1)
end

it "can see all enabled items" do
merchant1 = create(:merchant)
merchant2 = create(:merchant)
item1 = create(:item, user: merchant1)
item2 = create(:item, user: merchant2)
item3 = create(:inactive_item, user: merchant1)

visit items_path
within "#item-#{item1.id}" do
expect(page).to have_link(item1.name)

within "#item-#{@item1.id}" do
expect(page).to have_link(@item1.name)
expect(page).to have_xpath("//img[@src='https://vignette.wikia.nocookie.net/animalcrossing/images/7/72/Tom_Nook.png/revision/latest?cb=20101105231130']")
expect(page).to have_content("Sold by: #{merchant1.name}")
expect(page).to have_content("Quantity: #{item1.quantity}")
expect(page).to have_content("Price: $#{item1.current_price}")
expect(page).to have_content("Sold by: #{@merchant1.name}")
expect(page).to have_content("Quantity: #{@item1.quantity}")
expect(page).to have_content("Price: $#{@item1.current_price}")
end

within "#item-#{item2.id}" do
expect(page).to have_link(item2.name)
within "#item-#{@item2.id}" do
expect(page).to have_link(@item2.name)
expect(page).to have_xpath("//img[@src='https://vignette.wikia.nocookie.net/animalcrossing/images/7/72/Tom_Nook.png/revision/latest?cb=20101105231130']")
expect(page).to have_content(merchant2.name)
expect(page).to have_content(item2.quantity)
expect(page).to have_content(item2.current_price)
expect(page).to have_content(@merchant2.name)
expect(page).to have_content(@item2.quantity)
expect(page).to have_content(@item2.current_price)
end

expect(page).to_not have_content(item3.name)
expect(page).to_not have_content(@item4.name)
end

it "can see statistics about popular items" do
item4 = create(:item, user: @merchant1)
item5 = create(:item, user: @merchant1)
item6 = create(:item, user: @merchant1)
item7 = create(:item, user: @merchant1)
item8 = create(:item, user: @merchant1)
item9 = create(:item, user: @merchant1)
item10 = create(:item, user: @merchant1)

shopper = create(:user)
order = create(:shipped_order, user: shopper)

create(:fulfilled_order_item, order: order, item: @item1, quantity: @item1.quantity)
create(:fulfilled_order_item, order: order, item: @item2, quantity: @item2.quantity)
create(:fulfilled_order_item, order: order, item: @item3, quantity: @item3.quantity)
create(:fulfilled_order_item, order: order, item: item4, quantity: item4.quantity)
create(:fulfilled_order_item, order: order, item: item5, quantity: item5.quantity)
create(:fulfilled_order_item, order: order, item: item6, quantity: item6.quantity)
create(:fulfilled_order_item, order: order, item: item7, quantity: item7.quantity)
create(:fulfilled_order_item, order: order, item: item8, quantity: item8.quantity)
create(:fulfilled_order_item, order: order, item: item9, quantity: item9.quantity)
create(:fulfilled_order_item, order: order, item: item10, quantity: item10.quantity)

visit items_path

within "#most-popular-items" do
expect(page).to have_content("5 Most Popular Items:")
expect(page.all("#item")[0]).to have_content("#{item10.name}: #{item10.total_sold} sold")
expect(page.all("#item")[1]).to have_content("#{item9.name}: #{item9.total_sold} sold")
expect(page.all("#item")[2]).to have_content("#{item8.name}: #{item8.total_sold} sold")
expect(page.all("#item")[3]).to have_content("#{item7.name}: #{item7.total_sold} sold")
expect(page.all("#item")[4]).to have_content("#{item6.name}: #{item6.total_sold} sold")
end

within "#least-popular-items" do
expect(page).to have_content("5 Least Popular Items:")
expect(page.all("#item")[0]).to have_content("#{@item1.name}: #{@item1.total_sold} sold")
expect(page.all("#item")[1]).to have_content("#{@item2.name}: #{@item2.total_sold} sold")
expect(page.all("#item")[2]).to have_content("#{@item3.name}: #{@item3.total_sold} sold")
expect(page.all("#item")[3]).to have_content("#{item4.name}: #{item4.total_sold} sold")
expect(page.all("#item")[4]).to have_content("#{item5.name}: #{item5.total_sold} sold")
end
end
end
end
35 changes: 35 additions & 0 deletions spec/models/item_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,40 @@

expect(Item.enabled_items).to eq([item1, item2])
end

it ".sort_sold" do
merchant = create(:merchant)
item1 = create(:item, user: merchant)
item2 = create(:item, user: merchant)
item3 = create(:item, user: merchant)
item4 = create(:item, user: merchant)
item5 = create(:item, user: merchant)

shopper = create(:user)
order = create(:shipped_order, user: shopper)

create(:fulfilled_order_item, order: order, item: item1, quantity: item1.quantity)
create(:fulfilled_order_item, order: order, item: item2, quantity: item2.quantity)
create(:fulfilled_order_item, order: order, item: item3, quantity: item3.quantity)
create(:fulfilled_order_item, order: order, item: item4, quantity: item4.quantity)
create(:fulfilled_order_item, order: order, item: item5, quantity: item5.quantity)

expect(Item.sort_sold("ASC")).to eq([item1, item2, item3, item4, item5])
expect(Item.sort_sold("DESC")).to eq([item5, item4, item3, item2, item1])
end
end

describe 'instance methods' do
it ".total_sold" do
merchant = create(:merchant)
item1 = create(:item, user: merchant, quantity: 100)
shopper = create(:user)
order = create(:order, user: shopper)
create(:fulfilled_order_item, order: order, item: item1, quantity: 10)
create(:fulfilled_order_item, order: order, item: item1, quantity: 5)
create(:order_item, order: order, item: item1, quantity: 5)

expect(item1.total_sold).to eq(15)
end
end
end