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

Penguin/master - Lindsey & Alice #2

Open
wants to merge 121 commits into
base: penguin/master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
121 commits
Select commit Hold shift + click to select a range
d3d6e7f
Commented out production gems.
lindseyreno Aug 17, 2015
7362c86
added pry-rails to Gemset
a-lmx Aug 17, 2015
76ef3d6
added comments to help understand legacy code
a-lmx Aug 17, 2015
68bbbf8
added weight, length, width, and height attributes to model
a-lmx Aug 19, 2015
a1837f7
added --format doc to .rspec
a-lmx Aug 19, 2015
36b38e3
added weight, length, width, height to required_attributes
a-lmx Aug 19, 2015
9781c1b
commented out failing tests; added newly required attributes to uniqu…
a-lmx Aug 19, 2015
383ab89
updated product seeds with newly required attributes
a-lmx Aug 19, 2015
a1f568d
modified datatype of weight to integer
a-lmx Aug 19, 2015
46c6d66
re-migrated, updated schema
a-lmx Aug 19, 2015
c02a494
added logic to prevent users from editing other users products, or ad…
a-lmx Aug 19, 2015
ff0a8a4
added logic to display weight and dimensions on product show
a-lmx Aug 19, 2015
46e4f88
added weight and dimensions attributes to permitted params
a-lmx Aug 19, 2015
d214e78
added fields on form for weight and dimensions
a-lmx Aug 19, 2015
7cd30a4
added new attributes to example products in spec to make them valid
a-lmx Aug 19, 2015
bb6a731
added new attributes to example product in spec to make it valid
a-lmx Aug 19, 2015
bbf3877
added new attributes to example product
a-lmx Aug 19, 2015
98b9f36
added new attributes to example product
a-lmx Aug 19, 2015
31b5617
Added validations for new address attributes.
lindseyreno Aug 19, 2015
88982e7
Added address seed data to users.
lindseyreno Aug 19, 2015
f8bfb05
Added columns for city, state, zip, and country.
lindseyreno Aug 19, 2015
707c09b
Added tests for new model validations for city, state, zip, and country.
lindseyreno Aug 19, 2015
52c99fd
Added --format doc to be able to see tests better.
lindseyreno Aug 19, 2015
a6a34af
Added city, state, zip, and country to uer_params.
lindseyreno Aug 19, 2015
ac94606
Added city, state, zip, and country data where a User is created.
lindseyreno Aug 19, 2015
b05b913
Merge pull request #1 from arhx/product_model/ar
lindseyreno Aug 19, 2015
1246177
Fixed merge conflicts.
lindseyreno Aug 19, 2015
ebf364a
Merge pull request #2 from arhx/lr/user_model
arhx Aug 19, 2015
2b91e8c
Added shipping origin information to user create form.
lindseyreno Aug 19, 2015
78d8d65
Changed text fields to password fields in the user create form.
lindseyreno Aug 19, 2015
8325247
added spec and validation for weight_in_gms :numericality
a-lmx Aug 19, 2015
b9b1b6b
added specs and validations for numericality for remaining new attrib…
a-lmx Aug 19, 2015
afa8487
Merge pull request #4 from arhx/product_model/ar
lindseyreno Aug 19, 2015
6dd34d9
Merge pull request #3 from arhx/lr/user_model
arhx Aug 19, 2015
68db8d1
changed Order.find to Order.find_by in #logged_in
a-lmx Aug 19, 2015
9006322
removed @cart_quantity from ApplicationController#logged_in to fix bu…
a-lmx Aug 19, 2015
a159b8b
created migration to change :orders :zipcode to :zip; changed everywhere
a-lmx Aug 19, 2015
125d222
extracted order details into partial
a-lmx Aug 19, 2015
7706bc3
changed submit button to Shipping Options button
a-lmx Aug 19, 2015
165db5b
added get orders#shipping route
a-lmx Aug 19, 2015
6f4a7a4
created shipping view
a-lmx Aug 19, 2015
51c1953
pseudo-coded shipping and finalize actions
a-lmx Aug 19, 2015
b30992c
Created JSON shipment object for each distinct origin location.
lindseyreno Aug 19, 2015
e3659bd
Added httparty gem
lindseyreno Aug 19, 2015
502d8a2
WIP integrate shipping
lindseyreno Aug 19, 2015
eb5c56f
WIP
lindseyreno Aug 20, 2015
5d8f023
changed :orders :zip to integer
a-lmx Aug 20, 2015
95f1b5d
Merge branch 'integrate-shipper/la' of github.com:arhx/C3Projects--bE…
a-lmx Aug 20, 2015
57a0aaf
changed datatypes of :products dimensions to integers
a-lmx Aug 20, 2015
125708f
added display logic to show shipping options
a-lmx Aug 20, 2015
00c140a
added map-reduce function to process rates for rendering on view
a-lmx Aug 20, 2015
174b244
Merge pull request #5 from arhx/integrate-shipper/la
arhx Aug 20, 2015
4297d8a
added delivery date to calculated_rates
a-lmx Aug 20, 2015
11b799a
Added factory girl gem
lindseyreno Aug 20, 2015
5d29310
Required factory girl
lindseyreno Aug 20, 2015
49f340e
Added required config line.
lindseyreno Aug 20, 2015
dc4b254
Refactored tests to use factory girl
lindseyreno Aug 20, 2015
a7a8904
Created factories.
lindseyreno Aug 20, 2015
7b11772
Changed #normalize_names to handle a name error.
lindseyreno Aug 20, 2015
df6b0eb
Refactored to use factory girl
lindseyreno Aug 20, 2015
8df9d97
Removed _or_equal_to from quantity validation.
lindseyreno Aug 20, 2015
66eaa0c
added orders#update_total route
a-lmx Aug 20, 2015
494795a
added shipping_option_label view helper
a-lmx Aug 20, 2015
7b9b8c7
added form_tag with radio buttons displaying shipping options
a-lmx Aug 20, 2015
ae5fcdb
added #update_total action, and example of params[:shipping_option fo…
a-lmx Aug 20, 2015
78e39c1
Refactored tests to use factory girl
lindseyreno Aug 20, 2015
9890838
Refactored tests to use factory girl
lindseyreno Aug 20, 2015
3acaa90
Removed redundant validation for presence of email.
lindseyreno Aug 20, 2015
eddf6b1
Fixed typo in order factory
lindseyreno Aug 20, 2015
daf25f8
Refactored rating and product_id it statements to be more clear what …
lindseyreno Aug 20, 2015
ccb193b
Refactored tests to use factory girl.
lindseyreno Aug 20, 2015
3c4803d
Refactored to dry up the code a bit
lindseyreno Aug 20, 2015
a7b2324
Refactored to use factory girl
lindseyreno Aug 20, 2015
67a06fc
Merge pull request #7 from arhx/shipping-page/ar
lindseyreno Aug 20, 2015
05be929
Merge pull request #6 from arhx/lr/factory-girl
arhx Aug 20, 2015
f6278eb
Added columns for shipping service and shipping cost to orders table
lindseyreno Aug 20, 2015
a3d70a1
Added route for orders controller finalize action
lindseyreno Aug 20, 2015
5c04538
Created helper method to disable finalize button if shipping hasn't b…
lindseyreno Aug 20, 2015
531e313
Changed Total to Item Total
lindseyreno Aug 20, 2015
91e53f6
Added table to view order details and a button to finalize order
lindseyreno Aug 20, 2015
b06e986
Added logic to update_total and shipping to be able to select a shipp…
lindseyreno Aug 20, 2015
0101fb3
made update_total a GET method, and finalize a PUT method
a-lmx Aug 20, 2015
c068679
refactored order shipping/finalize/confirmation routes
a-lmx Aug 20, 2015
374966c
removed checkboxes, fixed indenting
a-lmx Aug 20, 2015
53e3a9c
added back in #update method, filled out finalize method
a-lmx Aug 20, 2015
c336f95
added TODO comment re: API call to audit log shipping option
a-lmx Aug 20, 2015
2ef3244
Merge pull request #8 from arhx/update-total/la
arhx Aug 20, 2015
feb8e98
added logic to make API call to post audit log in penguin shipper
a-lmx Aug 20, 2015
43b9d4e
Added error handling for api calls
lindseyreno Aug 20, 2015
55d0759
Added a continue shopping link to cart page that returns to root_path
lindseyreno Aug 21, 2015
c39436d
added #order_params; refactored #update
a-lmx Aug 21, 2015
5879fe7
Changed radio buttons to regular buttons
lindseyreno Aug 21, 2015
ac85c78
refactored methods from OrdersController
a-lmx Aug 21, 2015
92d2f7e
required penguin_shipper_interface
a-lmx Aug 21, 2015
c9f3b99
refactoring new methods
a-lmx Aug 21, 2015
d68a166
filled out .process_order(order)
a-lmx Aug 21, 2015
64310d8
removed commented code in #shipping
a-lmx Aug 21, 2015
c6d8347
Merge pull request #9 from arhx/lr/return-to-shopping-button
arhx Aug 21, 2015
832a0c7
Merge pull request #10 from arhx/lr/shipping-buttons
arhx Aug 21, 2015
98e9611
removed commented code
a-lmx Aug 21, 2015
2df2c2e
Merge pull request #11 from arhx/refactor_orders_controller/ar
lindseyreno Aug 21, 2015
d3fc29e
Fixed merge conflict in #shipping
lindseyreno Aug 21, 2015
7c34a20
Added error handling to #shipping
lindseyreno Aug 21, 2015
f203e25
Added error handling t0 #request_rates_from_API
lindseyreno Aug 21, 2015
cad7033
added readable_date helper
a-lmx Aug 21, 2015
35e4f5a
refactored shipping_option_label formatting
a-lmx Aug 21, 2015
33d23df
uncommented production gems
a-lmx Aug 21, 2015
22f9706
modified migrations to avoid change_columns
a-lmx Aug 21, 2015
1eb2864
removed change migration
a-lmx Aug 21, 2015
a01189b
changed user search in logged_in to find_by
a-lmx Aug 21, 2015
ae57791
changed logged_in Order.find_by to find
a-lmx Aug 21, 2015
87445d5
commented out late order
a-lmx Aug 21, 2015
c4ec66a
testing out find_by with id in params
a-lmx Aug 21, 2015
5a830d8
added heroku uris for PenguinShipper for production env
a-lmx Aug 21, 2015
0e8592b
fixed typo in log_choice_url
a-lmx Aug 21, 2015
a222873
fixed bug in storage and display of shipping cost
a-lmx Aug 21, 2015
072bb2f
debugging redirect error in shipping action
a-lmx Aug 21, 2015
9a2d6bb
debugging redirect loop
a-lmx Aug 21, 2015
5078685
debugging redirect loop
a-lmx Aug 21, 2015
10c6af9
removed commented code
a-lmx Aug 21, 2015
597f50d
destroyed session[:shipping_option]
a-lmx Aug 21, 2015
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
1 change: 1 addition & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
--color
--require spec_helper
--format doc
14 changes: 8 additions & 6 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ gem 'turbolinks'
gem 'jbuilder', '~> 2.0'
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', '~> 0.4.0', group: :doc

# Use ActiveModel has_secure_password
gem 'bcrypt', '~> 3.1.7'

# Styling
gem 'bootstrap-sass', '~> 3.3.5'

# Reading APIs
gem 'httparty'

# Use Unicorn as the app server
# gem 'unicorn'

Expand All @@ -42,24 +44,24 @@ group :development, :test do
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring'

# Troubleshooting
gem 'better_errors'
gem 'binding_of_caller'
gem 'pry-rails'

# Testing
gem 'simplecov', require: false
gem 'rspec-rails', '~> 3.0'
gem 'factory_girl_rails', "~> 4.0"

gem 'traceroute'

gem 'pry'

# Use sqlite3 as the database for Active Record
gem 'sqlite3'

end

group :production do
gem 'pg'

gem 'rails_12factor'

end
17 changes: 15 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,16 @@ GEM
docile (1.1.5)
erubis (2.7.0)
execjs (2.5.2)
factory_girl (4.5.0)
activesupport (>= 3.0.0)
factory_girl_rails (4.5.0)
factory_girl (~> 4.5.0)
railties (>= 3.0.0)
globalid (0.3.5)
activesupport (>= 4.1.0)
httparty (0.13.5)
json (~> 1.8)
multi_xml (>= 0.5.2)
i18n (0.7.0)
jbuilder (2.3.1)
activesupport (>= 3.0.0, < 5)
Expand All @@ -87,13 +95,16 @@ GEM
mini_portile (0.6.2)
minitest (5.7.0)
multi_json (1.11.2)
multi_xml (0.5.5)
nokogiri (1.6.6.2)
mini_portile (~> 0.6.0)
pg (0.18.2)
pry (0.10.1)
coderay (~> 1.1.0)
method_source (~> 0.8.1)
slop (~> 3.4)
pry-rails (0.3.4)
pry (>= 0.9.10)
rack (1.6.4)
rack-test (0.6.3)
rack (>= 1.0)
Expand All @@ -120,7 +131,7 @@ GEM
rails_serve_static_assets
rails_stdout_logging
rails_serve_static_assets (0.0.4)
rails_stdout_logging (0.0.3)
rails_stdout_logging (0.0.4)
railties (4.2.3)
actionpack (= 4.2.3)
activesupport (= 4.2.3)
Expand Down Expand Up @@ -197,10 +208,12 @@ DEPENDENCIES
bootstrap-sass (~> 3.3.5)
byebug
coffee-rails (~> 4.1.0)
factory_girl_rails (~> 4.0)
httparty
jbuilder (~> 2.0)
jquery-rails
pg
pry
pry-rails
rails (= 4.2.3)
rails_12factor
rspec-rails (~> 3.0)
Expand Down
16 changes: 9 additions & 7 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'penguin_shipper_interface'

class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
Expand Down Expand Up @@ -29,10 +31,10 @@ def logged_in

# guards from errors when order hasn't been initalized yet
if session[:order_id] != nil
@order = Order.find(session[:order_id])
@cart_quantity = quantity_in_cart(@order)
else
@cart_quantity = 0
@order = Order.find_by(id: session[:order_id])
# @cart_quantity = quantity_in_cart(@order)
# else
# @cart_quantity = 0
end
end

Expand All @@ -42,7 +44,7 @@ def view_active

private

def quantity_in_cart(order)
order.order_items.reduce(0) { |sum, n| sum + n.quantity }
end
# def quantity_in_cart(order)
# order.order_items.reduce(0) { |sum, n| sum + n.quantity }
# end
end
2 changes: 2 additions & 0 deletions app/controllers/order_items_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ def index
end

def create
# FIXME is this checking for the current order_id? (it isn't!)
if OrderItem.find_by(product_id: params[:id])
@order_item = OrderItem.find_by(product_id: params[:product_id])
@order_item.quantity += 1
Expand All @@ -24,6 +25,7 @@ def create
@order_item = OrderItem.create!(order_id: @order.id, product_id: params[:product_id])
@order.order_items << @order_item
end
# OPTIMIZE why do you need to specify the method get here?
redirect_to order_path(@order), method: :get
end

Expand Down
110 changes: 92 additions & 18 deletions app/controllers/orders_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
class OrdersController < ApplicationController
before_action :find_order, except: [ :index, :new, :create, :empty]

PENGUIN_LOG_CHOICE_URI = Rails.env.production? ?
"http://whispering-shore-8365.herokuapp.com/log_shipping_choice" :
"http://localhost:4000/log_shipping_choice"

def find_order
@order = Order.find(params[:id])
end
Expand All @@ -9,13 +13,14 @@ def index; end

def show
if session[:order_id] == @order.id
if @cart_quantity > 0
# if @cart_quantity > 0
if @order.order_items
@order_items = @order.order_items
else
render :index
render :index # there is session[:order_id], but no items in cart
end
else
redirect_to root_path
redirect_to root_path # no session[:order_id]
end
end

Expand All @@ -42,36 +47,100 @@ def payment
end

def update
# check for appropriate amount of inventory before accepting payment
# check for appropriate amount of inventory before accepting payment
check_inventory(@order)

if @enough_inventory
@order.update(order_params)
@order.card_last_4 = @order.card_number[-4, 4]
if @order.save # move and account for whether the order is cancelled?
redirect_to shipping_path(@order)
else
render :payment
end
else
redirect_to order_path(@order), notice: "#{@order_item.product.name} only has #{@order_item.product.inventory} item(s) in stock."
end
end

def shipping
@order_items = @order.order_items

@calculated_rates = PenguinShipperInterface.process_order(@order)

if @calculated_rates.first.keys.length > 1
@subtotal = 0
@shipping_cost = session[:shipping_option] ?
session[:shipping_option]["total_price"] : 0
render :shipping
elsif @calculated_rates.first.values.first == "422"
redirect_to :shipping, notice: "Error in shipping choice. Please try again."
elsif @calculated_rates.first.values.first == "408"
redirect_to :shipping, notice: "We could not process your request in a timely manner. Please try again later."
elsif @calculated_rates.first.values.first == "bad"
redirect_to root_path, notice: "NOPE. Please try again."
end
end

def update_total
## this is how :shipping_option is getting passed in the params before eval
## "{\"service_name\"=>\"UPS Next Day Air\", \"total_price\"=>15985, \"delivery_date\"=>\"2015-08-21T00:00:00.000+00:00\"}"

## WARNING eval is an unsafe method as it will run commands in the evaluated object
## FIXME change this to a safer way to convert the params data to something we can use
session[:shipping_option] = eval(params["shipping_option"])

redirect_to :shipping
end

def finalize
# secondary check for appropriate amount of inventory before accepting payment
check_inventory(@order)

if @enough_inventory
@order.email = params[:order][:email]
@order.address1 = params[:order][:address1]
@order.address2 = params[:order][:address2]
@order.city = params[:order][:city]
@order.state = params[:order][:state]
@order.zipcode = params[:order][:zipcode]
@order.card_last_4 = params[:order][:card_number][-4, 4]
# @order.ccv = params[:order][:ccv]
@order.card_exp = params[:order][:card_exp]
shipping_option = session[:shipping_option]
@order.shipping_service = shipping_option["service_name"]
@order.shipping_cost = shipping_option["total_price"]

@order.status = "paid"

if @order.save # move and account for whether the order is cancelled?
update_inventory(@order)
session[:order_id] = nil # emptying the cart after confirming order
redirect_to order_confirmation_path(@order)

shipping_choice = {}
shipping_choice["shipping_choice"] = {} # create wrapper for JSON
shipping_choice["shipping_choice"]["shipping_service"] = @order.shipping_service
# multiply by 100 since PenguinShipper stores costs in cents
shipping_choice["shipping_choice"]["shipping_cost"] = @order.shipping_cost
shipping_choice["shipping_choice"]["order_id"] = @order.id
shipping_choice = shipping_choice.to_json

response = HTTParty.post(PENGUIN_LOG_CHOICE_URI, query: { json_data: shipping_choice })
case response.code
when 201
update_inventory(@order)
redirect_to order_confirmation_path(@order)
when 422
redirect_to :shipping, notice: "Error in shipping choice. Please try again."
when 408
redirect_to :shipping, notice: "We could not process your request in a timely manner. Please try again later."
else
redirect_to :shipping, notice: "NOPE. Please try again."
end
else
render :payment
redirect_to :shipping, notice: "Order could not be saved. Please try again."
end
else
else # if not enough_inventory
redirect_to order_path(@order), notice: "#{@order_item.product.name} only has #{@order_item.product.inventory} item(s) in stock."
end
end

def confirmation
session[:order_id] = nil # clears cart
session[:shipping_option] = nil
@subtotal = 0
@purchase_time = Time.now
@order_items = @order.order_items
render :confirmation
end

def destroy
Expand All @@ -94,6 +163,11 @@ def completed

private

def order_params
params.require(:order).permit(:email, :address1, :address2, :city, :state,
:zip, :card_number, :ccv, :card_exp)
end

def self.model
Order
end # USED FOR RSPEC SHARED EXAMPLES
Expand Down
4 changes: 4 additions & 0 deletions app/controllers/products_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ def create_params
:inventory,
:active,
:user_id,
:weight_in_gms,
:length_in_cms,
:width_in_cms,
:height_in_cms,
category_ids: []
)
end
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def self.model
end # USED FOR RSPEC SHARED EXAMPLES

def user_params
params.require(:user).permit(:username, :email, :password, :password_confirmation)
params.require(:user).permit(:username, :email, :password, :password_confirmation, :city, :state, :zip, :country)
end

def nil_flash_errors
Expand Down
3 changes: 3 additions & 0 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
module ApplicationHelper
def readable_date(datetime)
datetime.strftime("%b %d, %Y")
end
end
23 changes: 23 additions & 0 deletions app/helpers/orders_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,27 @@ def order_error_check_for(element)
return (@order.errors.messages[element][0].capitalize + ".")
end
end

def shipping_option_label(shipping_option)
%Q{#{shipping_option["service_name"]}:
#{display_dollars(shipping_option["total_price"])},
Estimated delivery:
#{delivery_date(shipping_option["delivery_date"])}}
end

def delivery_date(date)
if date
readable_date(date.to_datetime)
else
"(none available)"
end
end

def finalize_button
@shipping_cost == 0 ? "btn btn-default disabled" : "btn btn-success"
end

def display_dollars(cents)
number_to_currency(cents/100.00)
end
end
8 changes: 8 additions & 0 deletions app/helpers/products_helper.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,10 @@
module ProductsHelper
# eg. "10.0 x 15.3 x 2.5"
def dimensions(product)
length = product.length_in_cms
width = product.width_in_cms
height = product.height_in_cms

"#{length} cm x #{width} cm x #{height} cm"
end
end
2 changes: 1 addition & 1 deletion app/models/category.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ class Category < ActiveRecord::Base
before_validation :normalize_names!

def normalize_names!
self.name = self.name.titlecase
self.name = self.name.titlecase unless name == nil
end
end
Loading