Skip to content

Commit

Permalink
WIP on holiday discounts
Browse files Browse the repository at this point in the history
  • Loading branch information
alepbloyd committed Aug 10, 2022
1 parent 7e18d03 commit a0dac5e
Show file tree
Hide file tree
Showing 14 changed files with 177 additions and 17 deletions.
9 changes: 9 additions & 0 deletions app/assets/stylesheets/application.css
Original file line number Diff line number Diff line change
Expand Up @@ -382,4 +382,13 @@ html, body{
padding: 10px;
padding-bottom: 20px;
margin-bottom: 10px;
}

.holiday-discount {
background-color: #bbedd0;
border-radius: 10px;
border: 1px solid black;
padding: 10px;
padding-bottom: 20px;
margin-bottom: 10px;
}
25 changes: 25 additions & 0 deletions app/controllers/holiday_discounts_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class HolidayDiscountsController < ApplicationController

def new
@holiday_name = params[:holiday_name]
@merchant = Merchant.find(params[:merchant_id])
end

def create
if holiday_discount_params[:percent_discount] == "" || holiday_discount_params[:quantity_threshold] == ""
flash[:notice] = "Error - please complete all fields"

redirect_to new_merchant_holiday_discount_path(holiday_discount_params[:merchant_id])
else
HolidayDiscount.create(percent_discount: holiday_discount_params[:percent_discount], quantity_threshold: holiday_discount_params[:quantity_threshold], holiday_name: holiday_discount_params[:holiday_name],merchant_id: holiday_discount_params[:merchant_id])

redirect_to merchant_bulk_discounts_path(holiday_discount_params[:merchant_id])
end
end

private
def holiday_discount_params
params.permit(:merchant_id, :percent_discount, :quantity_threshold, :holiday_name)
end

end
7 changes: 7 additions & 0 deletions app/models/holiday_discount.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class HolidayDiscount < ApplicationRecord

belongs_to :merchant
has_many :items, through: :merchant
has_many :invoice_items, through: :items

end
1 change: 1 addition & 0 deletions app/models/merchant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ class Merchant < ApplicationRecord
enum status: { Disabled: 0, Enabled: 1 }

has_many :bulk_discounts
has_many :holiday_discounts
has_many :items
has_many :invoice_items, through: :items
has_many :invoices, through: :invoice_items
Expand Down
26 changes: 26 additions & 0 deletions app/views/bulk_discounts/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
<% @merchant.bulk_discounts.each do |discount| %>
<div class="bulk-discount">
<div id="bulk-discount-<%=discount.id%>">
<p>
<b>Bulk Discount</b>
</p>
<p>
Percent Discount: <%=discount.percent_discount%>%
</p>
Expand All @@ -38,6 +41,27 @@
</div>
<% end %>
</div>

<div id="holiday-discount-list">
<% @merchant.holiday_discounts.each do |discount|%>
<div class="holiday-discount">
<div id="holiday-discount-<%=discount.id%>">
<p>
<b>Holiday Discount</b>
</p>
<p>
<%=discount.holiday_name.titleize%> Discount
</p>
<p>
Percent Discount: <%=discount.percent_discount%>%
</p>
<p>
Quantity Threshold: <%=discount.quantity_threshold%>
</p>
</div>
</div>
<% end %>
</div>
</div>
</div>

Expand All @@ -50,12 +74,14 @@
<th>Order</th>
<th>Name</th>
<th>Date</th>
<th>Discount</th>
</tr>
<%@holiday_search.holiday_information.each_with_index do |holiday,index|%>
<tr id="upcoming-holiday-<%=index+1%>">
<td><%=index+1%></td>
<td><%=holiday.name%></td>
<td><%=holiday.date%></td>
<td><%=link_to "New Discount",new_merchant_holiday_discount_path(@merchant, :holiday_name => holiday.name)%></td>
</tr>
<% end %>
</table>
Expand Down
12 changes: 12 additions & 0 deletions app/views/holiday_discounts/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<h1> New Holiday Discount Page </h1>

Discount Name: <%=@holiday_name%> Discount

<%= form_with url: "/merchants/#{@merchant.id}/holiday_discounts",method: :post, class: "holiday_discount_form" do |form|%>
<%=form.label :percent_discount, "Percent Discount" %>
<%=form.number_field :percent_discount, value: 30%>
<%=form.label :quantity_threshold, "Quantity Threshold" %>
<%=form.number_field :quantity_threshold, value: 2%>
<%=form.hidden_field :holiday_name, value: @holiday_name%>
<%=form.submit "Submit" %>
<% end %>
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
resources :invoices, controller: 'merchant_invoices', only: %i[index show update]
resources :items, controller: 'merchant_items', only: %i[index edit show update new create]
resources :bulk_discounts, controller: 'bulk_discounts', only: %i[index show new create edit update destroy]
resources :holiday_discounts, controller: 'holiday_discounts'
end

resources :admin, only: [:index]
Expand Down
12 changes: 12 additions & 0 deletions db/migrate/20220810015919_create_holiday_discounts.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class CreateHolidayDiscounts < ActiveRecord::Migration[5.2]
def change
create_table :holiday_discounts do |t|
t.string :holiday_name
t.integer :percent_discount, default: 30
t.integer :quantity_threshold, default: 2
t.references :merchant, foreign_key: true

t.timestamps
end
end
end
13 changes: 12 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2022_08_05_012510) do
ActiveRecord::Schema.define(version: 2022_08_10_015919) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand All @@ -29,6 +29,16 @@
t.datetime "updated_at"
end

create_table "holiday_discounts", force: :cascade do |t|
t.string "holiday_name"
t.integer "percent_discount", default: 30
t.integer "quantity_threshold", default: 2
t.bigint "merchant_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["merchant_id"], name: "index_holiday_discounts_on_merchant_id"
end

create_table "invoice_items", force: :cascade do |t|
t.integer "item_id"
t.integer "invoice_id"
Expand Down Expand Up @@ -78,5 +88,6 @@
end

add_foreign_key "bulk_discounts", "merchants"
add_foreign_key "holiday_discounts", "merchants"
add_foreign_key "items", "merchants"
end
1 change: 0 additions & 1 deletion spec/features/admin/merchants/index_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,6 @@
expect(page).to have_content('Trader Joes')
expect(page).to have_content('Disabled')
expect(page).to have_button('Enable')
click_on('Enable')
end

expect(current_path).to eq(admin_merchants_path)
Expand Down
16 changes: 16 additions & 0 deletions spec/features/merchants/bulk_discounts/index_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,20 @@
expect(page).to have_content("2022-11-11")
end
end

it "has a 'create discount' button next to each upcoming holiday" do
merchant1 = Merchant.create!(name: "Snake Shop")

bulk_discount1 = BulkDiscount.create!(percent_discount: 10, quantity_threshold: 5, merchant_id: merchant1.id)
bulk_discount2 = BulkDiscount.create!(percent_discount: 20, quantity_threshold: 10, merchant_id: merchant1.id)
bulk_discount3 = BulkDiscount.create!(percent_discount: 30, quantity_threshold: 15, merchant_id: merchant1.id)

visit merchant_bulk_discounts_path(merchant1.id)

within "#upcoming-holiday-1" do
click_on "New Discount"
end

expect(current_path).to eq(new_merchant_holiday_discount_path(merchant1))
end
end
46 changes: 46 additions & 0 deletions spec/features/merchants/holiday_discounts/new_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
require 'rails_helper'

RSpec.describe 'merchant new holiday discount page' do

it 'has pre-filled percent discount, quantity threshold, and holiday name fields, and reroutes to the discount index page where the new discount is shown' do
merchant1 = Merchant.create!(name: "Snake Shop")

bulk_discount1 = BulkDiscount.create!(percent_discount: 10, quantity_threshold: 5, merchant_id: merchant1.id)

visit merchant_bulk_discounts_path(merchant1.id)

within "#upcoming-holiday-1" do
click_on "New Discount"
end

expect(current_path).to eq(new_merchant_holiday_discount_path(merchant1))

expect(page).to have_content("Discount Name: Labor Day Discount")
expect(page).to have_field(:percent_discount, :with => 30)
expect(page).to have_field(:quantity_threshold, :with => 2)

click_on "Submit"

expect(current_path).to eq(merchant_bulk_discounts_path(merchant1.id))

within "#holiday-discount-list" do
expect(page).to have_content("Labor Day Discount")
expect(page).to have_content("Percent Discount: 30")
expect(page).to have_content("Quantity Threshold: 2")
end
end
end

# Create a Holiday Discount

# As a merchant,
# when I visit the discounts index page,
# In the Holiday Discounts section, I see a `create discount` button next to each of the 3 upcoming holidays.
# When I click on the button I am taken to a new discount form that has the form fields auto populated with the following:

# Discount name: <name of holiday> discount
# Percentage Discount: 30
# Quantity Threshold: 2

# I can leave the information as is, or modify it before saving.
# I should be redirected to the discounts index page where I see the newly created discount added to the list of discounts.
9 changes: 9 additions & 0 deletions spec/models/holiday_discount_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require 'rails_helper'

RSpec.describe HolidayDiscount do
describe 'relationships' do
it { should belong_to :merchant}
it { should have_many(:items).through(:merchant)}
it { should have_many(:invoice_items).through(:items)}
end
end
16 changes: 1 addition & 15 deletions spec/models/merchant_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
it { should have_many(:invoice_items).through(:items)}

it { should have_many :bulk_discounts}
it { should have_many :holiday_discounts}
end

describe 'instance methods' do
Expand Down Expand Up @@ -124,21 +125,6 @@

expect(actual).to eq(expected)
end
it 'disabled_merchants returns all merchants with status 0 (Disabled)' do
merchant1 = Merchant.create!(name: 'Potions and Things', status: 'Enabled')
merchant2 = Merchant.create!(name: 'Wands and Wigs', status: 'Disabled')
merchant3 = Merchant.create!(name: 'Berties Beans', status: 'Disabled')
merchant4 = Merchant.create!(name: 'Butterbeer Superstore', status: 'Disabled')

actual = Merchant.disabled_merchants.map do |merchant|
merchant.name
end
expected = [merchant2, merchant3, merchant4].map do |merchant|
merchant.name
end

expect(actual).to eq(expected)
end

it 'returns all associated enabled items' do
merchant1 = Merchant.create!(name: "Snake Shop", status: 'Enabled')
Expand Down

0 comments on commit a0dac5e

Please sign in to comment.