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

James Belta #81

Open
wants to merge 52 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
3fcccd0
set up, bundle and migrations
JBelta Nov 8, 2020
bc7ba93
update seeds file
JBelta Nov 8, 2020
620191b
Migration for discount table w/ name, percentage, item limit associat…
JBelta Nov 11, 2020
48c0d6e
merchant has many discounts
JBelta Nov 11, 2020
30940dd
def relation to merchant
JBelta Nov 11, 2020
1f53f92
relationships, no validations
JBelta Nov 11, 2020
3c0f66b
spec for discount relations
JBelta Nov 11, 2020
976808a
added discounts and bulk_discount spec.
JBelta Nov 11, 2020
f956856
added bulk discount/ edit grand total (review specs for each)
JBelta Nov 11, 2020
0b16637
discount check method. Needs spec
JBelta Nov 11, 2020
1327258
item discount check, needs spec
JBelta Nov 11, 2020
cfb1b9f
removed pry
JBelta Nov 11, 2020
e926b21
discount_spec for merchant
JBelta Nov 11, 2020
027942d
edits and expectations
JBelta Nov 11, 2020
3f2913e
added discounts
JBelta Nov 11, 2020
3a25725
discounts for brian
JBelta Nov 11, 2020
4c4b330
removed bulk_discount? def available discount
JBelta Nov 11, 2020
f2279bb
discount_rate(arg) for selecting correct discount
JBelta Nov 11, 2020
62a96e0
Dry up subtotal and grand total
JBelta Nov 11, 2020
0db1377
delete unused code
JBelta Nov 11, 2020
c6d3261
grand_total to return, not each result
JBelta Nov 11, 2020
8be94e8
starting cart discount spec
JBelta Nov 11, 2020
f222b89
changed AR syntax to SQL
JBelta Nov 12, 2020
8b0a5c5
add grand total w/ discounted prices included.
JBelta Nov 12, 2020
daa55ad
discounts added for @megan
JBelta Nov 12, 2020
b2a8371
index spec displays create link, each discount w/ css. Room for delete
JBelta Nov 12, 2020
f26536d
new spec for discounts.
JBelta Nov 12, 2020
37c931c
View for merchants discounts
JBelta Nov 12, 2020
0161466
Controller for merchant's discounts
JBelta Nov 12, 2020
9a8de18
new form for discounts
JBelta Nov 12, 2020
63d10cb
added link too discounts on the merchants/items page
JBelta Nov 12, 2020
3e7ae3b
no prefix or path for resources :create. had to hand roll
JBelta Nov 12, 2020
85b151e
model validations
JBelta Nov 12, 2020
f121b87
test for discount rates method
JBelta Nov 12, 2020
846a90d
deleted
JBelta Nov 12, 2020
f7014d4
edit spec to update discount
JBelta Nov 12, 2020
9e4a071
Added update link
JBelta Nov 12, 2020
2ea36a8
test describe edit
JBelta Nov 12, 2020
0e1e8ed
edit form/ form_tag
JBelta Nov 12, 2020
85de31b
edit file path
JBelta Nov 12, 2020
d6ebdc7
update action, var.update! not updating information
JBelta Nov 12, 2020
cb4c2da
not updating data
JBelta Nov 12, 2020
918e74f
interpilation
JBelta Nov 12, 2020
d9724f4
edit expectation
JBelta Nov 12, 2020
ddb3c38
removed save, update returns boolean.
JBelta Nov 12, 2020
7024d50
delete spec
JBelta Nov 12, 2020
a91eda0
route to delete discount
JBelta Nov 12, 2020
2841dcf
destory action
JBelta Nov 12, 2020
4f0f605
check for delete button within discount's css
JBelta Nov 12, 2020
b6cb928
deleted test for orders
JBelta Nov 12, 2020
16f84e5
removed pry
JBelta Nov 12, 2020
1be0e4c
Added reload, value changed but view shows old value
JBelta Nov 12, 2020
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
24 changes: 12 additions & 12 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ GEM
arel (9.0.0)
bcrypt (3.1.16)
bindex (0.8.1)
bootsnap (1.4.8)
bootsnap (1.5.0)
msgpack (~> 1.0)
builder (3.2.4)
byebug (11.1.3)
Expand Down Expand Up @@ -139,15 +139,15 @@ GEM
rb-fsevent (0.10.4)
rb-inotify (0.10.1)
ffi (~> 1.0)
regexp_parser (1.7.1)
rspec-core (3.9.2)
rspec-support (~> 3.9.3)
rspec-expectations (3.9.2)
regexp_parser (1.8.2)
rspec-core (3.10.0)
rspec-support (~> 3.10.0)
rspec-expectations (3.10.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.9.0)
rspec-mocks (3.9.1)
rspec-support (~> 3.10.0)
rspec-mocks (3.10.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.9.0)
rspec-support (~> 3.10.0)
rspec-rails (4.0.1)
actionpack (>= 4.2)
activesupport (>= 4.2)
Expand All @@ -156,7 +156,7 @@ GEM
rspec-expectations (~> 3.9)
rspec-mocks (~> 3.9)
rspec-support (~> 3.9)
rspec-support (3.9.3)
rspec-support (3.10.0)
ruby_dep (1.5.0)
sass (3.7.4)
sass-listen (~> 4.0.0)
Expand All @@ -171,10 +171,10 @@ GEM
tilt (>= 1.1, < 3)
shoulda-matchers (4.4.1)
activesupport (>= 4.2.0)
simplecov (0.19.0)
simplecov (0.19.1)
docile (~> 1.1)
simplecov-html (~> 0.11)
simplecov-html (0.12.2)
simplecov-html (0.12.3)
sprockets (3.7.2)
concurrent-ruby (~> 1.0)
rack (> 1, < 3)
Expand All @@ -185,7 +185,7 @@ GEM
thor (1.0.1)
thread_safe (0.3.6)
tilt (2.0.10)
tzinfo (1.2.7)
tzinfo (1.2.8)
thread_safe (~> 0.1)
uglifier (4.2.0)
execjs (>= 0.3.0, < 3)
Expand Down
51 changes: 51 additions & 0 deletions app/controllers/merchant/discounts_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
class Merchant::DiscountsController < Merchant::BaseController

def index
@discounts = current_user.merchant.discounts
end

def new
@discount = Discount.new
end

def create
merchant = current_user.merchant
discount = merchant.discounts.new(discount_params)
if discount.save
redirect_to "/merchant/discounts"
else
flash[:alert] = discount.errors.full_messages.to_sentence
render :new
end
end

def edit
@discount = Discount.find(params[:id])
end

def update
merchant = current_user.merchant
@discount = merchant.discounts.find(params[:id])
if @discount.update!(discount_params)
redirect_to '/merchant/discounts'
else
flash[:alert] = discount.errors.full_messages.to_sentence
render :edit
end
end

def destroy
Discount.destroy(params[:id])
redirect_to '/merchant/discounts'
end

private

def discount_params
params.permit(:name, :percentage, :limit)
end
#resources was not providing a prefix on form_with, using form tag (create and edit)
#def discount_params
#params.require(:discount).permit(:name, :percentage, :limit)
#end
end
12 changes: 8 additions & 4 deletions app/models/cart.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ def items

def grand_total
grand_total = 0.0
@contents.each do |item_id, quantity|
grand_total += Item.find(item_id).price * quantity
end
@contents.each { |item_id, quantity| grand_total += subtotal_of(item_id)}
grand_total
end

Expand All @@ -37,7 +35,13 @@ def count_of(item_id)
end

def subtotal_of(item_id)
@contents[item_id.to_s] * Item.find(item_id).price
subtotal = 0.0
savings = 0.0
item = Item.find(item_id)
quantity = @contents[item.id.to_s]
discount = item.available_discounts.discount_rate(quantity)
(savings += ((item.price * quantity) * discount[:percentage])) if discount != nil
(subtotal += (item.price * quantity) - savings)
end

def limit_reached?(item_id)
Expand Down
12 changes: 12 additions & 0 deletions app/models/discount.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Discount < ApplicationRecord
belongs_to :merchant

validates_presence_of :name,
:percentage,
:limit


def self.discount_rate(amount)
where("discounts.limit <= ?", amount).order(limit: :desc).first
end
end
4 changes: 4 additions & 0 deletions app/models/item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,8 @@ def sorted_reviews(limit = nil, order = :asc)
def average_rating
reviews.average(:rating)
end

def available_discounts
self.merchant.discounts
end
end
1 change: 1 addition & 0 deletions app/models/merchant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ class Merchant < ApplicationRecord
has_many :order_items, through: :items
has_many :orders, through: :order_items
has_many :users
has_many :discounts

validates_presence_of :name,
:address,
Expand Down
9 changes: 9 additions & 0 deletions app/models/order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,13 @@ def is_packaged?
def self.by_status
order(:status)
end

def discount?
merchant = user.merchant
minimum_discount = merchant.discounts.order(:limit).first
merchant_item_orders = order_items
.joins("JOIN items ON order_items.item_id = items.id")
.where("items.merchant_id = ? AND order_items.quantity >= ?", merchant.id, minimum_discount.limit)
merchant_item_orders.empty? == false
end
end
13 changes: 13 additions & 0 deletions app/views/merchant/discounts/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<%= form_tag "/merchant/discounts/#{@discount.id}", method: :patch do %>

<%= label_tag :name %>
<%= text_field_tag :name, "#{@discount.name}"%>

<%= label_tag :percentage %>
<%= number_field_tag :percentage, "#{@discount.percentage}", input_html: { step: 0.0 } %>

<%= label_tag :limit %>
<%= number_field_tag :limit, "#{@discount.limit}" %>

<%= submit_tag "Update Discount" %>
<% end %>
11 changes: 11 additions & 0 deletions app/views/merchant/discounts/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<%= link_to "Create Discount", '/merchant/discounts/new' %>
<% @discounts.each do |discount| %>
<section id="discount-<%=discount.id %>">
<ul>
<%= discount.name %> Discount <br>
Limit: <%= discount.limit %>
</ul>
<%= link_to "Update", "/merchant/discounts/#{discount.id}/edit" %> <br>
<%= button_to "Delete", "/merchant/discounts/#{discount.id}", method: :delete %>
<% end %>
</section>
13 changes: 13 additions & 0 deletions app/views/merchant/discounts/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<%= form_tag '/merchant/discounts', method: :post do %>

<%= label_tag :name %>
<%= text_field_tag :name %>

<%= label_tag :percentage %>
<%= number_field_tag :percentage, input_html: { step: 0.0 } %>

<%= label_tag :limit %>
<%= number_field_tag :limit %>

<%= submit_tag "Create Discount" %>
<% end %>
2 changes: 1 addition & 1 deletion app/views/merchant/items/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<h1>My Items</h1>
<%= link_to 'New Item', '/merchant/items/new' %>

<%= link_to 'Current Discounts', '/merchant/discounts' %>
<%= render '/items/index' %>
4 changes: 4 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@

namespace :merchant do
get '/', to: 'dashboard#index', as: :dashboard
resources :discounts, only: [:index, :new, :edit]
post '/discounts', to: 'discounts#create'
patch '/discounts/:id', to: 'discounts#update'
delete '/discounts/:id', to: 'discounts#destroy'
resources :orders, only: :show
resources :items, only: [:index, :new, :create, :edit, :update, :destroy]
put '/items/:id/change_status', to: 'items#change_status'
Expand Down
8 changes: 8 additions & 0 deletions db/migrate/20201109013506_create_discounts.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class CreateDiscounts < ActiveRecord::Migration[5.2]
def change
create_table :discounts do |t|
t.string :name, default: nil
t.float :percentage, default: nil
end
end
end
5 changes: 5 additions & 0 deletions db/migrate/20201109014614_add_merchants_to_discounts.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddMerchantsToDiscounts < ActiveRecord::Migration[5.2]
def change
add_reference :discounts, :merchant, foreign_key: true
end
end
5 changes: 5 additions & 0 deletions db/migrate/20201109015529_add_limit_to_discounts.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddLimitToDiscounts < ActiveRecord::Migration[5.2]
def change
add_column :discounts, :limit, :integer
end
end
11 changes: 10 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,19 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20190724161751) do
ActiveRecord::Schema.define(version: 2020_11_09_015529) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"

create_table "discounts", force: :cascade do |t|
t.string "name"
t.float "percentage"
t.bigint "merchant_id"
t.integer "limit"
t.index ["merchant_id"], name: "index_discounts_on_merchant_id"
end

create_table "items", force: :cascade do |t|
t.string "name"
t.string "description"
Expand Down Expand Up @@ -84,6 +92,7 @@
t.index ["merchant_id"], name: "index_users_on_merchant_id"
end

add_foreign_key "discounts", "merchants"
add_foreign_key "items", "merchants"
add_foreign_key "order_items", "items"
add_foreign_key "order_items", "orders"
Expand Down
28 changes: 25 additions & 3 deletions db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,31 @@
# movies = Movie.create([{ name: 'Star Wars' }, { name: 'Lord of the Rings' }])
# Character.create(name: 'Luke', movie: movies.first)

OrderItem.delete_all
Review.delete_all
Discount.delete_all
Item.delete_all
Order.delete_all
User.delete_all
Merchant.delete_all

#merchants ------------------

megan = Merchant.create!(name: 'Megans Marmalades', address: '123 Main St', city: 'Denver', state: 'CO', zip: 80218)
brian = Merchant.create!(name: 'Brians Bagels', address: '125 Main St', city: 'Denver', state: 'CO', zip: 80218)
megan.items.create!(name: 'Ogre', description: "I'm an Ogre!", price: 20, image: 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTaLM_vbg2Rh-mZ-B4t-RSU9AmSfEEq_SN9xPP_qrA2I6Ftq_D9Qw', active: true, inventory: 5 )
megan.items.create!(name: 'Giant', description: "I'm a Giant!", price: 50, image: 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTaLM_vbg2Rh-mZ-B4t-RSU9AmSfEEq_SN9xPP_qrA2I6Ftq_D9Qw', active: true, inventory: 3 )
brian.items.create!(name: 'Hippo', description: "I'm a Hippo!", price: 50, image: 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTaLM_vbg2Rh-mZ-B4t-RSU9AmSfEEq_SN9xPP_qrA2I6Ftq_D9Qw', active: true, inventory: 3 )

#discounts
discount_m5_percent = megan.discounts.create(name: '5 Percent', percentage: 0.05, limit: 5)
discount_m10_percent = megan.discounts.create(name: '10 Percent', percentage: 0.1, limit: 10)
discount_b5_percent = brian.discounts.create(name: '5 Percent', percentage: 0.05, limit: 5)
discount_b10_percent = brian.discounts.create(name: '10 Percent', percentage: 0.1, limit: 10)
#employees ------------------

m_user = megan.users.create(name: 'Megan', address: '123 Main St', city: 'Denver', state: 'CO', zip: 80218, email: '[email protected]', password: 'securepassword')

#items ----------------------

megan.items.create!(name: 'Ogre', description: "I'm an Ogre!", price: 20, image: 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTaLM_vbg2Rh-mZ-B4t-RSU9AmSfEEq_SN9xPP_qrA2I6Ftq_D9Qw', active: true, inventory: 20 )

megan.items.create!(name: 'Giant', description: "I'm a Giant!", price: 50, image: 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTaLM_vbg2Rh-mZ-B4t-RSU9AmSfEEq_SN9xPP_qrA2I6Ftq_D9Qw', active: true, inventory: 20 )
brian.items.create!(name: 'Hippo', description: "I'm a Hippo!", price: 50, image: 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTaLM_vbg2Rh-mZ-B4t-RSU9AmSfEEq_SN9xPP_qrA2I6Ftq_D9Qw', active: true, inventory: 20 )
3 changes: 3 additions & 0 deletions spec/features/cart/show_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,9 @@
expect(page).to_not have_content("#{@hippo.name}")
expect(page).to have_content("Cart: 0")
end
it 'has a grand total and sub total with discounts implimented' do

end
end
end
end
24 changes: 24 additions & 0 deletions spec/features/merchant/discount_delete_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require 'rails_helper'

RSpec.describe 'Merchant Deletess Discounts' do
describe 'As a Merchant' do
before :each do
@merchant_1 = Merchant.create!(name: 'Megans Marmalades', address: '123 Main St', city: 'Denver', state: 'CO', zip: 80218)
@m_user = @merchant_1.users.create(name: 'Megan', address: '123 Main St', city: 'Denver', state: 'CO', zip: 80218, email: '[email protected]', password: 'securepassword')
@discount = @merchant_1.discounts.create(name: '7 Percent', percentage: 0.07, limit: 14)

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

it 'A merchant can delete their own Discount' do
visit '/merchant/discounts'

within "#discount-#{@discount.id}" do
expect(page).to have_button("Delete")
click_button ("Delete")
end

expect(current_path).to eq('/merchant/discounts')
end
end
end
Loading