Skip to content

Commit

Permalink
Merge pull request #131 from tnodland/story_14
Browse files Browse the repository at this point in the history
Story 14
  • Loading branch information
whois-jon-peterson authored Apr 2, 2019
2 parents e1e42c1 + 1f41a08 commit e80f2d7
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 3 deletions.
5 changes: 5 additions & 0 deletions app/controllers/items_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class ItemsController < ApplicationController
def index
@items = Item.enabled_items
end
end
4 changes: 4 additions & 0 deletions app/models/item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,8 @@ class Item < ApplicationRecord
belongs_to :user, foreign_key: 'merchant_id'
has_many :order_items
has_many :orders, through: :order_items

def self.enabled_items
where(enabled: true)
end
end
11 changes: 11 additions & 0 deletions app/views/items/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<h1>All Items:</h1>

<% @items.each do |item| %>
<section id="item-<%= item.id %>">
<h3><%= link_to "#{item.name}", item_path(item) %></h3>
<%= image_tag "#{item.image_url}", size: "100x100" %>
<p>Sold by: <%= item.user.name %></p>
<p>Quantity: <%= item.quantity %></p>
<p>Price: <%= number_to_currency(item.current_price) %></p>
</section>
<% end %>
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Rails.application.routes.draw do
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
resources :items, only: [:index, :show]
end
2 changes: 1 addition & 1 deletion spec/factories/items.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
association :user, factory: :merchant
sequence(:name) { |n| "Item #{n}"}
description { "An item you should buy" }
image_url { "image" }
image_url { "https://vignette.wikia.nocookie.net/animalcrossing/images/7/72/Tom_Nook.png/revision/latest?cb=20101105231130" }
sequence(:quantity) { |n| ("#{n}".to_i+1)*2}
sequence(:current_price) { |n| ("#{n}".to_i+1)*1.5}
enabled { true }
Expand Down
14 changes: 14 additions & 0 deletions spec/factories/orders.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
FactoryBot.define do
factory :order do
user

status { 0 }
end

factory :packaged_order, parent: :order do
status { 1 }
end

factory :shipped_order, parent: :order do
status { 2 }
end

factory :cancelled_order, parent: :order do
status { 3 }
end
end
33 changes: 33 additions & 0 deletions spec/features/items/index_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
require 'rails_helper'

RSpec.describe "Items index page" do
context "as any kind of user" do
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)
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}")
end

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)
end

expect(page).to_not have_content(item3.name)
end
end
end
13 changes: 12 additions & 1 deletion spec/models/item_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,21 @@
it {should validate_presence_of :merchant_id}

end

describe 'relationships' do
it {should belong_to :user}
it {should have_many :order_items}
it {should have_many(:orders).through :order_items}
end

describe 'class methods' do
it ".enabled_items" do
merchant = create(:merchant)
item1 = create(:item, user: merchant)
item2 = create(:item, user: merchant)
item3 = create(:inactive_item, user: merchant)

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

0 comments on commit e80f2d7

Please sign in to comment.