Skip to content

Commit

Permalink
Merge pull request #50 from hyoshida/new-orders-in-admin-panel
Browse files Browse the repository at this point in the history
New orders in admin panel
  • Loading branch information
YOSHIDA Cake committed Nov 18, 2015
2 parents c95437b + c838c98 commit 2daa5a0
Show file tree
Hide file tree
Showing 52 changed files with 1,206 additions and 313 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,12 @@ initialize_select2 = ->
$select = $(this)
options = { theme: 'bootstrap' }
if $select.hasClass('ajax')
term_column = $select.data('term') || ['name_cont']
options.ajax = {
url: $select.data('source')
dataType: 'json'
cache: true
data: (params) -> { q: { name_cont: params.term }, page: params.page }
data: (params) -> { q: { "#{term_column}" : params.term }, page: params.page }
processResults: (data, page) -> { results: data }
}
$select.select2(options)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ class Dispatcher
action_name = path[1]

switch page
when 'orders:edit', 'pages:update'
when 'draft_orders:new', 'draft_orders:create', 'draft_orders:edit', 'draft_orders:update'
new OrderItemBuilder
new UserSelector
new DynamicOrder
when 'orders:new', 'orders:create', 'orders:edit', 'orders:update'
new DynamicOrder
when 'pages:new', 'pages:show', 'pages:edit', 'pages:update', 'pages:create'
new Page
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class @DynamicOrder

listen_events: ->
self = this
$('input').on('change', ->
$(document).on('change', 'input', ->
attribute_name = $(this).attr('data-name')
return unless jQuery.inArray(attribute_name, self.refresh_trigger_attributes)
self.refresh_order_item_prices_for(this)
Expand All @@ -18,7 +18,7 @@ class @DynamicOrder

$price = $group.find('[data-name="price"]')
$quantity = $group.find('[data-name="quantity"]')
$subtotal_price = $group.find('[data-name="subtotal_price"]')
$subtotal_price = $group.find('[data-name="subtotal-price"]')

price = Number($price.val())
quantity = Number($quantity.val())
Expand All @@ -28,14 +28,14 @@ class @DynamicOrder

refresh_order_prices: ->
item_total_price = 0
$('[data-name="subtotal_price"]').each( ->
$('[data-name="subtotal-price"]').each( ->
item_total_price += Number($(this).val())
)

$item_total_price = $('[data-name="item_total_price"]')
$payment_fee = $('[data-name="payment_fee"]')
$shipment_fee = $('[data-name="shipment_fee"]')
$total_price = $('[data-name="total_price"]')
$item_total_price = $('[data-name="item-total-price"]')
$payment_fee = $('[data-name="payment-fee"]')
$shipment_fee = $('[data-name="shipment-fee"]')
$total_price = $('[data-name="total-price"]')

payment_fee = Number($payment_fee.val())
shipment_fee = Number($shipment_fee.val())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
class @OrderItemBuilder
NEW_ORDER_ITEM_SELECTOR = '.js-new-order-item'
LINK_TO_REMOVE_ORDER_ITEM_SELECTOR = '.js-remove-order-item'

constructor: ->
@setSelectors()
@$variantSelector.on('select2:select', (event) => @buildOrderItem(event.params.data))
$(document).on('click', LINK_TO_REMOVE_ORDER_ITEM_SELECTOR, (event) => @removeOrderItem(event.target))

setSelectors: ->
@$variantSelector = $('#js-variant-selector')
@$linkToAddOrderItem = $('#js-add-order-item')
@$orderItemsTable = $('#js-order-items-table').find('tbody')

buildOrderItem: (variant) ->
$orderItem = @newOrderItem()
@fillOrderItem($orderItem, variant)
@$orderItemsTable.append($orderItem)
@refreshPricesFor($orderItem)

# TODO: Commonize this method
newOrderItem: ->
newId = new Date().getTime()
@$linkToAddOrderItem.click()
$orderItem = $(NEW_ORDER_ITEM_SELECTOR).filter('.hidden').last()
$orderItem.removeClass('hidden')
$orderItem.html($orderItem.html().replace(/new_order_item/g, newId))
$orderItem

fillOrderItem: ($orderItem, variant) ->
$orderItem.find('[data-name="variant-id"]').val(variant.id)
$orderItem.find('[data-name="name"]').val(variant.text)
$orderItem.find('[data-name="sku"]').val(variant.sku)
$orderItem.find('[data-name="price"]').val(variant.price)
$orderItem.find('[data-name="subtotal-price"]').val(variant.price)
$orderItem.find('[data-name="image-url"]').attr('src', variant.image_url)

removeOrderItem: (element) ->
$(element).closest(NEW_ORDER_ITEM_SELECTOR).remove()

refreshPricesFor: ($orderItem) ->
$orderItem.find('[data-name="price"]').trigger('change')
15 changes: 15 additions & 0 deletions backend/app/assets/javascripts/comable/admin/user_selector.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class @UserSelector
constructor: ->
@setSelectors()
@$userSelector.on('select2:select', (event) => @fillUser(event.params.data))

setSelectors: ->
@$userSelector = $('#js-user-selector')
@$user = $('#js-user-fields')

fillUser: (user) ->
@$user.find('[data-name="email"]').val(user.email)
@$user.find('[data-name="bill-family-name"]').val(user.bill_address.family_name)
@$user.find('[data-name="bill-first-name"]').val(user.bill_address.first_name)
@$user.find('[data-name="bill-zip-code"]').val(user.bill_address.zip_code)
@$user.find('[data-name="bill-state-name"]').val(user.bill_address.state_name)
88 changes: 88 additions & 0 deletions backend/app/controllers/comable/admin/draft_orders_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
require_dependency 'comable/admin/application_controller'

module Comable
module Admin
class DraftOrdersController < Comable::Admin::ApplicationController
include Comable::PermittedAttributes

load_and_authorize_resource :order, parent: false, class: Comable::DraftOrder.name

def index
@q = Comable::Order.draft.ransack(params[:q])
@orders = @q.result.page(params[:page]).per(15).recent.accessible_by(current_ability)
end

def show
render :edit
end

def new
build_associations
end

def create
if save_order_as_draft
@order.update!(draft: false) if @order.completed?
redirect_to admin_order_path, notice: Comable.t('successful')
else
build_associations
flash.now[:alert] = Comable.t('failure')
render :new
end
end

def edit
end

def update
@order.attributes = order_params

if save_order_as_draft
@order.update!(draft: false) if @order.completed?
redirect_to admin_order_path, notice: Comable.t('successful')
else
flash.now[:alert] = Comable.t('failure')
render :edit
end
end

private

def admin_order_path
if @order.draft?
comable.admin_draft_order_path(@order)
else
comable.admin_order_path(@order)
end
end

def save_order_as_draft
@order.next_draft_state
rescue ActiveRecord::RecordInvalid
false
end

# rubocop:disable Metrics/MethodLength
def order_params
params.require(:order).permit(
:id,
:user_id,
:email,
:same_as_bill_address,
bill_address_attributes: permitted_address_attributes,
ship_address_attributes: permitted_address_attributes,
order_items_attributes: [:id, :name, :sku, :price, :quantity, :variant_id],
payment_attributes: [:id, :_destroy, :payment_method_id],
shipments_attributes: [:id, :_destroy, :shipment_method_id]
)
end
# rubocop:enable Metrics/MethodLength

def build_associations
@order.build_bill_address unless @order.bill_address
@order.build_ship_address unless @order.ship_address
@order.build_payment unless @order.payment
end
end
end
end
6 changes: 5 additions & 1 deletion backend/app/controllers/comable/admin/orders_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ def index
def show
end

def new
redirect_to comable.new_admin_draft_order_path
end

def edit
end

Expand Down Expand Up @@ -87,7 +91,7 @@ def order_params
:total_price,
bill_address_attributes: permitted_address_attributes,
ship_address_attributes: permitted_address_attributes,
order_items_attributes: [:id, :name, :code, :price, :quantity]
order_items_attributes: [:id, :name, :price, :quantity]
)
end

Expand Down
5 changes: 5 additions & 0 deletions backend/app/controllers/comable/admin/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ class UsersController < Comable::Admin::ApplicationController
def index
@q = Comable::User.ransack(params[:q])
@users = @q.result.page(params[:page]).accessible_by(current_ability).by_newest

respond_to do |format|
format.html
format.json { render json: @users }
end
end

def show
Expand Down
12 changes: 10 additions & 2 deletions backend/app/controllers/comable/admin/variants_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,20 @@
module Comable
module Admin
class VariantsController < Comable::Admin::ApplicationController
load_and_authorize_resource :product, class: Comable::Product.name
load_and_authorize_resource :variant, class: Comable::Variant.name, through: :product
# for /admin/variants
load_and_authorize_resource :variant, class: Comable::Variant.name, only: :index

# for /admin/products/:product_id/variants/:id
load_and_authorize_resource :product, class: Comable::Product.name, except: :index
load_and_authorize_resource :variant, class: Comable::Variant.name, except: :index, through: :product

def index
@q = @variants.ransack(params[:q])
@variants = @q.result.includes(:product).page(params[:page]).accessible_by(current_ability).by_newest

respond_to do |format|
format.json { render json: @variants }
end
end

def show
Expand Down
14 changes: 13 additions & 1 deletion backend/app/navigations/comable/admin/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
end

item Comable.t('admin.nav.order') do
link comable.admin_orders_path
nest :orders
icon 'shopping-cart'
end

Expand Down Expand Up @@ -43,6 +43,18 @@
end
end

navigation :orders do
brand Comable.t('admin.nav.order')

item Comable.t('admin.nav.order') do
link comable.admin_orders_path
end

item Comable.t('admin.nav.draft_order') do
link comable.admin_draft_orders_path
end
end

navigation :products do
brand Comable.t('admin.nav.product')

Expand Down

0 comments on commit 2daa5a0

Please sign in to comment.