Skip to content

Commit

Permalink
Feature: user can see empty cart
Browse files Browse the repository at this point in the history
  • Loading branch information
Kirill Oleynik committed Aug 22, 2016
1 parent 6288704 commit cd90aab
Show file tree
Hide file tree
Showing 24 changed files with 599 additions and 17 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ spec/dummy/db/*.sqlite3
spec/dummy/db/*.sqlite3-journal
spec/dummy/log/*.log
spec/dummy/tmp/
.ruby-gemset
2 changes: 2 additions & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--color
--require spec_helper
1 change: 1 addition & 0 deletions .ruby-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2.3.1
126 changes: 126 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
PATH
remote: .
specs:
shopping_cart (0.1.0)
aasm
haml
pg
rails (~> 5.0.0, >= 5.0.0.1)

GEM
remote: https://rubygems.org/
specs:
aasm (4.11.0)
actioncable (5.0.0.1)
actionpack (= 5.0.0.1)
nio4r (~> 1.2)
websocket-driver (~> 0.6.1)
actionmailer (5.0.0.1)
actionpack (= 5.0.0.1)
actionview (= 5.0.0.1)
activejob (= 5.0.0.1)
mail (~> 2.5, >= 2.5.4)
rails-dom-testing (~> 2.0)
actionpack (5.0.0.1)
actionview (= 5.0.0.1)
activesupport (= 5.0.0.1)
rack (~> 2.0)
rack-test (~> 0.6.3)
rails-dom-testing (~> 2.0)
rails-html-sanitizer (~> 1.0, >= 1.0.2)
actionview (5.0.0.1)
activesupport (= 5.0.0.1)
builder (~> 3.1)
erubis (~> 2.7.0)
rails-dom-testing (~> 2.0)
rails-html-sanitizer (~> 1.0, >= 1.0.2)
activejob (5.0.0.1)
activesupport (= 5.0.0.1)
globalid (>= 0.3.6)
activemodel (5.0.0.1)
activesupport (= 5.0.0.1)
activerecord (5.0.0.1)
activemodel (= 5.0.0.1)
activesupport (= 5.0.0.1)
arel (~> 7.0)
activesupport (5.0.0.1)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (~> 0.7)
minitest (~> 5.1)
tzinfo (~> 1.1)
arel (7.1.1)
builder (3.2.2)
concurrent-ruby (1.0.2)
erubis (2.7.0)
globalid (0.3.7)
activesupport (>= 4.1.0)
haml (4.0.7)
tilt
i18n (0.7.0)
loofah (2.0.3)
nokogiri (>= 1.5.9)
mail (2.6.4)
mime-types (>= 1.16, < 4)
method_source (0.8.2)
mime-types (3.1)
mime-types-data (~> 3.2015)
mime-types-data (3.2016.0521)
mini_portile2 (2.1.0)
minitest (5.9.0)
nio4r (1.2.1)
nokogiri (1.6.8)
mini_portile2 (~> 2.1.0)
pkg-config (~> 1.1.7)
pg (0.18.4)
pkg-config (1.1.7)
rack (2.0.1)
rack-test (0.6.3)
rack (>= 1.0)
rails (5.0.0.1)
actioncable (= 5.0.0.1)
actionmailer (= 5.0.0.1)
actionpack (= 5.0.0.1)
actionview (= 5.0.0.1)
activejob (= 5.0.0.1)
activemodel (= 5.0.0.1)
activerecord (= 5.0.0.1)
activesupport (= 5.0.0.1)
bundler (>= 1.3.0, < 2.0)
railties (= 5.0.0.1)
sprockets-rails (>= 2.0.0)
rails-dom-testing (2.0.1)
activesupport (>= 4.2.0, < 6.0)
nokogiri (~> 1.6.0)
rails-html-sanitizer (1.0.3)
loofah (~> 2.0)
railties (5.0.0.1)
actionpack (= 5.0.0.1)
activesupport (= 5.0.0.1)
method_source
rake (>= 0.8.7)
thor (>= 0.18.1, < 2.0)
rake (11.2.2)
sprockets (3.7.0)
concurrent-ruby (~> 1.0)
rack (> 1, < 3)
sprockets-rails (3.1.1)
actionpack (>= 4.0)
activesupport (>= 4.0)
sprockets (>= 3.0.0)
thor (0.19.1)
thread_safe (0.3.5)
tilt (2.0.5)
tzinfo (1.2.2)
thread_safe (~> 0.1)
websocket-driver (0.6.4)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.2)

PLATFORMS
ruby

DEPENDENCIES
shopping_cart!

BUNDLED WITH
1.12.5
47 changes: 47 additions & 0 deletions app/controllers/shopping_cart/orders_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
module ShoppingCart
class OrdersController < ApplicationController
# before_action :set_order, only: [:update, :destroy]

# decorates_assigned :order

# def index
# @orders = current_customer.orders
# end
#
# def show
# @order = Order.with_books.find_by(id: params[:id])
# back_to_shop('Order') unless @order
# end

def edit
# @order = current_customer.orders.in_progress.decorate.first
@order = ShoppingCart::Order.where(user_id: current_user.id).in_progress.first
end

# def update
# UpdateOrder.call(@order, params) do
# on(:ok) { updated_notice('Order') }
# on(:invalid) { update_error('Order') }
# end
# redirect_to cart_path
# end
#
# def destroy
# @order.destroy ? deleted_notice('Order') : delete_error('Order')
# redirect_to cart_path
# end
#
# def create
# CreateOrder.call(params) do
# on(:ok) { redirect_to cart_path }
# on(:invalid) { add_book_failed }
# end
# end

# private

# def set_order
# @order = Order.find_by_id params[:id]
# end
end
end
75 changes: 75 additions & 0 deletions app/models/shopping_cart/order.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
require 'aasm'

module ShoppingCart
class Order < ApplicationRecord
# has_many :order_items, dependent: :destroy
# belongs_to :customer
# belongs_to :credit_card
# belongs_to :delivery
# has_one :coupon, dependent: :destroy
# has_one :order_shipping,
# -> { where addressable_type: 'order_shipping' },
# class_name: Address, foreign_key: :addressable_id,
# dependent: :destroy
# has_one :order_billing,
# -> { where addressable_type: 'order_billing' },
# class_name: Address, foreign_key: :addressable_id,
# dependent: :destroy

# accepts_nested_attributes_for :order_items, allow_destroy: true
#
# after_validation :update_total

include AASM
# attr_accessor :active_admin_requested_event
aasm column: :state, whiny_transitions: false do
state :in_progress, initial: true
state :processing
state :in_delivery
state :delivered
state :canceled

event :completed do
transitions from: :in_progress, to: :processing
end

event :sent_to_client do
transitions from: :processing, to: :in_delivery
end

event :delivered do
transitions from: :in_delivery, to: :delivered
end

event :canceled do
transitions from: [:processing, :in_delivery], to: :canceled
end
end

# scope :with_books, -> { includes(order_items: :book) }
#
# def add_item(book_id, quantity = 1)
# item = order_items.find_by(book_id: book_id)
# if item
# item.update_amount(quantity)
# else
# order_items.create(quantity: quantity, book_id: book_id)
# end
# end
#
# def update_total
# return if errors.any?
# new_total = order_items.map(&:item_total).sum
# new_total -= (new_total * coupon.discount / 100) if coupon
# self.total = new_total
# end
#
# def destroy_if_orphant
# self.destroy if order_items.count.zero?
# end
#
# def has_all_data?
# order_billing && order_shipping && credit_card && delivery
# end
end
end
14 changes: 0 additions & 14 deletions app/views/layouts/shopping_cart/application.html.erb

This file was deleted.

10 changes: 10 additions & 0 deletions app/views/layouts/shopping_cart/application.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
!!!
%html
%head
%meta{:content => "text/html; charset=UTF-8", "http-equiv" => "Content-Type"}/
%title Shopping cart
= stylesheet_link_tag "shopping_cart/application", media: "all"
= javascript_include_tag "shopping_cart/application"
= csrf_meta_tags
%body
= yield
3 changes: 3 additions & 0 deletions app/views/shopping_cart/orders/_empty_cart.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.text-center
%p.lead= I18n.t('cart.empty_cart')
-# = back_to_shop_link
36 changes: 36 additions & 0 deletions app/views/shopping_cart/orders/_form.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
= form_for order do |f|
%table.table#current_order
%thead
%tr
%th.lead.text-center{colspan:2}= t('index.book')
%th.lead.text-center= t('index.quantity')
%th.lead.text-center= t('index.price')
%th.lead.text-center= t('index.total')
%tbody.text-center
= f.fields_for :order_items do |oi|
%tr
%td{width:"15%"}= image_tag oi.object.book.cover.small
%td.text-left{width:'55%'}
%h4= link_to oi.object.book.title, oi.object.book
%p= oi.object.book.preview
%td{width:'10%'}= oi.number_field :quantity, class: 'form-control'
%td{width:'10%'}= number_to_currency oi.object.book.price
%td{width:'10%'}= delete_item_link oi.object.id
%tr.lead
%td{colspan:3}
%td.text-right
%p= t('cart.discount') if order.coupon
%p= t('cart.total')
%td
%p= order.discount if order.coupon
%p= order.total_in_currency
%tr
%td.text-center= empty_cart_link order
%td.text-left= continue_shopping_link
%td
= f.fields_for :coupon do |c|
= c.text_field :code, class: 'form-control',
placeholder: (order.coupon ? order.coupon.code : t('coupon_code')),
disabled: (true if order.coupon)
%td.text-center= f.submit t('orders.edit.update'), class: 'btn btn-success'
%td.text-center= link_to_checkout
8 changes: 8 additions & 0 deletions app/views/shopping_cart/orders/edit.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.raw
.col-md-12
%h1.page-header= I18n.t('cart.page_title')
.well
- if @order
= render partial: 'form', locals: {order: @order}
- else
= render partial: 'empty_cart'
31 changes: 31 additions & 0 deletions config/locales/shopping_cart.en.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
en:
# states:
# in_progress: "In progress"
# processing: "Processing"
# in_delivery: "In delivery"
# delivered: "Delivered"
cart:
page_title: "Cart"
empty_cart: "Nothing added to your order."
# total: "Order total:"
# back_to_shop: "Back to Shop"
# deleted: "Your order deleted successfuly"
# delete_error: "Something went wrong and your order was not deleted. Please, try again."
# index:
# orders_title: "Your orders"
# book: "Book"
# price: "Price"
# quantity: "QTY"
# total: "Total"
# to_cart: "Go to cart"
# subtotal: "Subtotal:"
# number: "Number"
# completed_at: "Completed at"
# total: "Total"
# view: "View order"
# no_orders: "You still have not ordered anything"
# back_to_shop: "Back to Shop"
# show:
# title: "Order information"
# order_name_html: "Order %{num} <small>%{state}</small>"
# back_to_orders: "Back to Orders"
4 changes: 4 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
ShoppingCart::Engine.routes.draw do
get '/', to: 'orders#edit'
resources :orders, only: [:update, :create, :destroy]
resources :order_items, only: [:destroy]
resources :checkout
end
15 changes: 15 additions & 0 deletions db/migrate/20160814135512_create_shopping_cart_addresses.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class CreateShoppingCartAddresses < ActiveRecord::Migration[5.0]
def change
create_table :shopping_cart_addresses do |t|
t.string :street
t.string :zipcode
t.string :city
t.string :phone
t.string :first_name
t.string :last_name
t.string :country_code
t.belongs_to :order, index: true
t.timestamps
end
end
end
Loading

0 comments on commit cd90aab

Please sign in to comment.