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

Merchant Item Epic User Stories 44-51 #156

Merged
merged 35 commits into from
Apr 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
ff41521
First test for merchant items dashboard
wipegup Apr 5, 2019
ccfe54a
Test for merchant item partial
wipegup Apr 5, 2019
5e52ba7
Add file for merchant_item partial
wipegup Apr 5, 2019
c2953c9
Passing merchant item partial test for display of items
wipegup Apr 5, 2019
aef220c
Add test for item buttons in partial
wipegup Apr 5, 2019
e8dc69b
Add test for ordered? method
wipegup Apr 5, 2019
f480e9f
Passing ordered? test
wipegup Apr 5, 2019
e1f4e54
All buttons correctly showing in render
wipegup Apr 5, 2019
a985248
Passing all index test for merchant item dashboard
wipegup Apr 5, 2019
f457ea6
Test for using enable and diable button
wipegup Apr 6, 2019
a980e3f
Passing enable/disable tests for merchatn items
wipegup Apr 6, 2019
afb1e6f
Add test for deleting merchant item
wipegup Apr 6, 2019
67f2566
Passing flash message test for dis/enabled
wipegup Apr 6, 2019
6b608a6
Add test for Item Creation
wipegup Apr 7, 2019
b76adb1
Add Shared Form partial for item page
wipegup Apr 7, 2019
f82a7f5
Passing new item test
wipegup Apr 7, 2019
757a5ef
Add new item form
wipegup Apr 7, 2019
ee2169f
Add skeleton for validations of input
wipegup Apr 7, 2019
7b28142
Add test for item form validations
wipegup Apr 7, 2019
ec4018a
Tests correctly failing
wipegup Apr 7, 2019
e562ad7
validations of quantity and current price passing tests
wipegup Apr 7, 2019
455cf34
Add default image to migration, passing default image test
wipegup Apr 7, 2019
0c22f24
Add test for default image xpath after leaving field blank
wipegup Apr 7, 2019
4e49f4a
Reorganize dashboard item specs
wipegup Apr 7, 2019
d719af2
Further breakout delete spec for items
wipegup Apr 7, 2019
2cddd7a
Add test for edit link in dashboard item partial
wipegup Apr 7, 2019
c05cdcf
Passing test for inclusion of link on partial
wipegup Apr 7, 2019
0eb5ae6
Happy path tests for editing item from dashboard
wipegup Apr 7, 2019
e27c31a
Passing happy-path tests for editing merchant items
wipegup Apr 7, 2019
f30057f
Passing merchant item edit tests
wipegup Apr 7, 2019
f696d26
Add default image test for editing
wipegup Apr 7, 2019
21488d1
Passing default image editing test
wipegup Apr 7, 2019
802e3da
Remove print stubs
wipegup Apr 7, 2019
80f54b4
Add flash message test / behavior for adding item
wipegup Apr 7, 2019
a390dc7
Merge branch 'master' into merchant_items
cnodland Apr 8, 2019
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
60 changes: 59 additions & 1 deletion app/controllers/merchants/items_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,62 @@
class Merchant::ItemsController < Merchant::BaseController
class Merchants::ItemsController < Merchants::BaseController
def index
@items = Item.where(user: current_user)
end

def new
@item = Item.new
end

def edit
@item = Item.find(params[:id])
end

def create
@item = current_user.items.new(item_info)
if @item.valid?
@item.save

flash[:info] = "#{@item.name} Saved"
redirect_to dashboard_items_path
else
render :new
end
end

def update
@item = Item.find(params[:id])
name = @item.name
if params[:enabled]
@item.update(enabled: params[:enabled])
completed_action = params[:enabled] == "true" ? "Enabled": "Disabled"
flash[:info] = [@item.name, completed_action].join(" ")
redirect_to dashboard_items_path
else
info = item_info
if info[:image_url] == ''
info[:image_url] = 'http://www.spore.com/static/image/500/404/515/500404515704_lrg.png'
end
@item.update(info)
if @item.valid?
flash[:info] = "#{name} Edited"
redirect_to dashboard_items_path
else
render :edit
end
end
end

def destroy
@item = Item.find(params[:id])
name = @item.name
@item.destroy
flash[:info] = "#{name} Deleted"
redirect_to dashboard_items_path
end

private

def item_info
params.require(:item).permit(:name, :description, :image_url, :current_price, :quantity)
end
end
11 changes: 7 additions & 4 deletions app/models/item.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
class Item < ApplicationRecord
validates_presence_of :name,
:description,
:image_url,
:quantity,
:current_price #break out for nuericality checks

:image_url
validates :quantity, presence: true, numericality:{ only_integer: true, greater_than_or_equal_to: 0}
validates :current_price, presence: true, numericality:{ greater_than: 0}
belongs_to :user, foreign_key: 'merchant_id'
has_many :order_items
has_many :orders, through: :order_items
Expand Down Expand Up @@ -33,4 +32,8 @@ def fullfillment_time
order_items.where("order_items.fulfilled = true")
.average("order_items.updated_at - order_items.created_at")
end

def ordered?
orders != []
end
end
1 change: 1 addition & 0 deletions app/views/merchants/items/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= render 'shared/item_form', form_type: "Edit" %>
4 changes: 4 additions & 0 deletions app/views/merchants/items/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<%= link_to "Add Item", new_dashboard_item_path%>
<% @items.each do |item|%>
<%= render 'shared/merchant_item', item:item%>
<%end%>
1 change: 1 addition & 0 deletions app/views/merchants/items/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= render 'shared/item_form', form_type: "Create" %>
33 changes: 33 additions & 0 deletions app/views/shared/_item_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<% if @item.errors.any? %>
<ul>
<% @item.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
<% end %>

<%= form_for [:dashboard, @item] do |f| %>
<p>
<%= f.label :name %>
<%= f.text_field :name %>
</p>
<p>
<%= f.label :description%>
<%= f.text_field :description %>
</p>
<p>
<%= f.label :image_url, "Image URL"%>
<%= f.text_field :image_url %>
</p>
<p>
<%= f.label :current_price, "Price" %>
<%= f.text_field :current_price %>
</p>
<p>
<%= f.label :quantity%>
<%= f.number_field :quantity %>
</p>

<%= f.submit "#{form_type} Item" %>

<% end %>
17 changes: 17 additions & 0 deletions app/views/shared/_merchant_item.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<div id=<%="merchant-item-#{item.id}"%>>
<div id=item-id><%=item.id%></div>
<img src=<%=item.image_url%> height=20px width=20px/>
<div id=item-name><%=item.name%></div>
<div id=item-quantity><%=item.quantity%></div>
<div id=item-price><%=item.current_price%></div>
<%= link_to "Edit", edit_dashboard_item_path(item)%>
<%unless item.ordered? %>
<%= button_to "Delete", dashboard_item_path(item), method: :delete %>
<%end%>

<% if item.enabled %>
<%= button_to "Disable", dashboard_item_path(item, enabled:false), method: :patch %>
<% else %>
<%= button_to "Enable", dashboard_item_path(item, enabled:true), method: :patch %>
<%end%>
</div>
5 changes: 5 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
end
get '/profile', to: "users#show", as: 'profile'
get '/profile/edit', to: "users#edit", as: 'edit_profile'
# get '/dashboard/items', to: "merchants/items#index"
scope :dashboard, module: :merchants, as: :dashboard do
resources :items, only: [:index, :destroy, :update, :new, :create, :edit]

end

get '/dashboard/items', to: "merchants/items#index"
get '/dashboard', to: 'merchants/orders#index'
Expand Down
2 changes: 1 addition & 1 deletion db/migrate/20190401221059_create_items.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ def change
create_table :items do |t|
t.string :name
t.string :description
t.string :image_url
t.string :image_url, default: 'http://www.spore.com/static/image/500/404/515/500404515704_lrg.png'
t.integer :quantity
t.money :current_price
t.boolean :enabled, default: true
Expand Down
2 changes: 1 addition & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
create_table "items", force: :cascade do |t|
t.string "name"
t.string "description"
t.string "image_url"
t.string "image_url", default: "http://www.spore.com/static/image/500/404/515/500404515704_lrg.png"
t.integer "quantity"
t.money "current_price", scale: 2
t.boolean "enabled", default: true
Expand Down
25 changes: 25 additions & 0 deletions spec/features/merchants/items/delete_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require 'rails_helper'

RSpec.describe 'Merchant Item Index', type: :feature do
before :each do
@merchant = create(:merchant)
@items = create_list(:item,2, user: @merchant)
@inactive_item = create(:inactive_item, user: @merchant)

@order_items_1 = create_list(:order_item, 3, item:@items[0])
@order_item_inactive = create(:order_item, item:@inactive_item)

allow_any_instance_of(ApplicationController).to receive(:current_user).and_return(@merchant)
end

it 'can delete an item which has not been ordered' do
visit dashboard_items_path
within "#merchant-item-#{@items[1].id}" do
click_button "Delete"
end

expect(page).to have_content("#{@items[1].name} Deleted")
expect(page).not_to have_selector('div', id:"merchant-item-#{@items[1].id}")
end

end
74 changes: 74 additions & 0 deletions spec/features/merchants/items/index_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
require 'rails_helper'

RSpec.describe 'Merchant Item Index', type: :feature do
before :each do
@merchant = create(:merchant)
@items = create_list(:item,2, user: @merchant)
@inactive_item = create(:inactive_item, user: @merchant)

@order_items_1 = create_list(:order_item, 3, item:@items[0])
@order_item_inactive = create(:order_item, item:@inactive_item)

allow_any_instance_of(ApplicationController).to receive(:current_user).and_return(@merchant)
end

it 'has an item index page displaying all items' do
visit dashboard_items_path

expect(page).to have_selector('div', id:"merchant-item-#{@items[0].id}")
expect(page).to have_selector('div', id:"merchant-item-#{@items[1].id}")
expect(page).to have_selector('div', id:"merchant-item-#{@inactive_item.id}")
end

end

RSpec.describe 'Merchant_Item partial', type: :view do
before :each do
@active_unordered_item = create(:item)
@inactive_item = create(:inactive_item)
@ordered_item = create(:item)
@order_item = create(:order_item, item: @ordered_item)
end
it 'shows all item information' do
render 'shared/merchant_item', item:@active_unordered_item

expect(rendered).to have_selector('div', id:"merchant-item-#{@active_unordered_item.id}")
expect(rendered).to have_selector('div', id: 'item-id', text: @active_unordered_item.id)
expect(rendered).to have_selector('div', id: 'item-name', text: @active_unordered_item.name)
expect(rendered).to have_selector('div', id: 'item-price', text: @active_unordered_item.current_price)
expect(rendered).to have_selector('div', id: 'item-quantity', text: @active_unordered_item.quantity)
expect(rendered).to have_xpath("//img[@src='#{@active_unordered_item.image_url}']")
end

it 'gives an edit button' do
render 'shared/merchant_item', item:@active_unordered_item

expect(rendered).to have_link("Edit", href:edit_dashboard_item_path(@active_unordered_item))
end

it 'Correctly shows disable button' do
render 'shared/merchant_item', item:@active_unordered_item

expect(rendered).to have_button("Disable")
expect(rendered).not_to have_button("Enable")
end

it 'Correctly shows enable button' do
render 'shared/merchant_item', item:@inactive_item

expect(rendered).not_to have_button("Disable")
expect(rendered).to have_button("Enable")
end

it 'Shows delete button when appropriate' do
render 'shared/merchant_item', item:@active_unordered_item

expect(rendered).to have_button("Delete")
end

it 'Does not show delete button when not appropriate' do
render 'shared/merchant_item', item:@ordered_item

expect(rendered).not_to have_button("Delete")
end
end
Loading