-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #50 from hyoshida/new-orders-in-admin-panel
New orders in admin panel
- Loading branch information
Showing
52 changed files
with
1,206 additions
and
313 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
backend/app/assets/javascripts/comable/admin/order_item_builder.coffee
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
15
backend/app/assets/javascripts/comable/admin/user_selector.coffee
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
88
backend/app/controllers/comable/admin/draft_orders_controller.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.