diff --git a/app/controllers/items_controller.rb b/app/controllers/items_controller.rb new file mode 100644 index 00000000..c89042ff --- /dev/null +++ b/app/controllers/items_controller.rb @@ -0,0 +1,5 @@ +class ItemsController < ApplicationController + def index + @items = Item.enabled_items + end +end diff --git a/app/models/item.rb b/app/models/item.rb index 8c3d09a6..ef7528e3 100644 --- a/app/models/item.rb +++ b/app/models/item.rb @@ -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 diff --git a/app/views/items/index.html.erb b/app/views/items/index.html.erb new file mode 100644 index 00000000..4467bb03 --- /dev/null +++ b/app/views/items/index.html.erb @@ -0,0 +1,11 @@ +

All Items:

+ +<% @items.each do |item| %> +
+

<%= link_to "#{item.name}", item_path(item) %>

+ <%= image_tag "#{item.image_url}", size: "100x100" %> +

Sold by: <%= item.user.name %>

+

Quantity: <%= item.quantity %>

+

Price: <%= number_to_currency(item.current_price) %>

+
+<% end %> diff --git a/config/routes.rb b/config/routes.rb index 787824f8..a093ccf0 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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 diff --git a/spec/factories/items.rb b/spec/factories/items.rb index b885f356..177f4f0a 100644 --- a/spec/factories/items.rb +++ b/spec/factories/items.rb @@ -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 } diff --git a/spec/factories/orders.rb b/spec/factories/orders.rb index 7ae0840a..49434626 100644 --- a/spec/factories/orders.rb +++ b/spec/factories/orders.rb @@ -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 diff --git a/spec/features/items/index_spec.rb b/spec/features/items/index_spec.rb new file mode 100644 index 00000000..c8ad076d --- /dev/null +++ b/spec/features/items/index_spec.rb @@ -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 diff --git a/spec/models/item_spec.rb b/spec/models/item_spec.rb index 0cdb838b..d008c498 100644 --- a/spec/models/item_spec.rb +++ b/spec/models/item_spec.rb @@ -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