comable_frontend 0.2.3 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. checksums.yaml +4 -4
  2. data/MIT-LICENSE +1 -1
  3. data/Rakefile +16 -4
  4. data/app/assets/javascripts/comable/frontend/application.coffee +5 -0
  5. data/app/assets/stylesheets/comable/frontend/application.scss +434 -0
  6. data/app/assets/stylesheets/comable/frontend/products.scss +0 -0
  7. data/app/assets/stylesheets/comable/products.scss +0 -0
  8. data/app/controllers/comable/application_controller.rb +2 -0
  9. data/app/controllers/comable/carts_controller.rb +32 -17
  10. data/app/controllers/comable/customers_controller.rb +5 -5
  11. data/app/controllers/comable/orders_controller.rb +31 -71
  12. data/app/controllers/comable/products_controller.rb +15 -1
  13. data/app/controllers/concerns/comable/payment_action.rb +3 -13
  14. data/app/controllers/concerns/comable/shipment_action.rb +2 -12
  15. data/app/controllers/concerns/comable/signin_action.rb +55 -0
  16. data/app/views/comable/carts/show.slim +65 -20
  17. data/app/views/comable/customers/_address.slim +7 -7
  18. data/app/views/comable/customers/addresses.slim +31 -29
  19. data/app/views/comable/customers/show.slim +60 -2
  20. data/app/views/comable/orders/confirm.slim +73 -30
  21. data/app/views/comable/orders/create.slim +6 -4
  22. data/app/views/comable/orders/delivery.slim +23 -14
  23. data/app/views/comable/orders/orderer.slim +38 -20
  24. data/app/views/comable/orders/payment.slim +12 -11
  25. data/app/views/comable/orders/shipment.slim +12 -10
  26. data/app/views/comable/orders/signin.slim +22 -0
  27. data/app/views/comable/products/index.slim +30 -10
  28. data/app/views/comable/products/show.slim +89 -21
  29. data/app/views/comable/shared/_address.slim +13 -20
  30. data/app/views/comable/shared/_address_form.slim +33 -15
  31. data/app/views/comable/shared/_footer.slim +2 -0
  32. data/app/views/comable/shared/_header.slim +74 -0
  33. data/app/views/comable/shared/_header_for_checkout.slim +18 -0
  34. data/app/views/kaminari/comable_frontend/_gap.html.slim +2 -0
  35. data/app/views/kaminari/comable_frontend/_next_page.html.slim +7 -0
  36. data/app/views/kaminari/comable_frontend/_page.html.slim +3 -0
  37. data/app/views/kaminari/comable_frontend/_paginator.html.slim +11 -0
  38. data/app/views/kaminari/comable_frontend/_prev_page.html.slim +7 -0
  39. data/app/views/layouts/comable/application.slim +23 -14
  40. data/config/routes.rb +9 -12
  41. data/lib/comable/frontend/engine.rb +8 -0
  42. metadata +151 -7
  43. data/app/controllers/concerns/comable/permitted_attributes.rb +0 -15
  44. data/app/views/comable/orders/new.slim +0 -4
File without changes
@@ -1,5 +1,7 @@
1
1
  module Comable
2
2
  class ApplicationController < ActionController::Base
3
3
  include Comable::ApplicationHelper
4
+
5
+ protect_from_forgery with: :exception
4
6
  end
5
7
  end
@@ -1,37 +1,52 @@
1
1
  module Comable
2
2
  class CartsController < Comable::ApplicationController
3
- rescue_from Comable::NoStock, with: :no_stock
3
+ before_filter :set_cart_item, only: [:add, :update]
4
+ before_filter :ensure_found_cart_item, only: [:add, :update]
4
5
 
5
6
  def add
6
- cart_item = find_cart_item
7
- return redirect_by_product_not_found unless cart_item
8
-
9
- current_customer.add_cart_item(cart_item, cart_item_options)
10
-
11
- flash[:notice] = I18n.t('comable.carts.add_product')
12
- redirect_to cart_path
7
+ if current_customer.add_cart_item(@cart_item, cart_item_options)
8
+ redirect_to comable.cart_path, notice: Comable.t('carts.added')
9
+ else
10
+ flash.now[:alert] = Comable.t('carts.invalid')
11
+ render :show
12
+ end
13
13
  end
14
14
 
15
15
  def update
16
+ if current_customer.reset_cart_item(@cart_item, cart_item_options)
17
+ redirect_to comable.cart_path, notice: Comable.t('carts.updated')
18
+ else
19
+ flash.now[:alert] = Comable.t('carts.invalid')
20
+ render :show
21
+ end
22
+ end
23
+
24
+ def destroy
16
25
  cart_item = find_cart_item
17
26
  return redirect_by_product_not_found unless cart_item
18
27
 
19
- current_customer.reset_cart_item(cart_item, cart_item_options)
28
+ if current_customer.reset_cart_item(cart_item)
29
+ redirect_to comable.cart_path, notice: Comable.t('carts.updated')
30
+ else
31
+ flash.now[:alert] = Comable.t('carts.invalid')
32
+ render :show
33
+ end
34
+ end
20
35
 
21
- flash[:notice] = I18n.t('comable.carts.update')
22
- redirect_to cart_path
36
+ def checkout
37
+ current_order.next_state if current_order.state?(:cart)
38
+ redirect_to comable.next_order_path(state: :confirm)
23
39
  end
24
40
 
25
41
  private
26
42
 
27
- def redirect_by_product_not_found
28
- flash[:alert] = I18n.t('comable.errors.messages.products_not_found')
29
- redirect_to :back
43
+ def set_cart_item
44
+ @cart_item = find_cart_item
30
45
  end
31
46
 
32
- def no_stock
33
- flash[:alert] = I18n.t('comable.errors.messages.products_soldout')
34
- redirect_to cart_path
47
+ def ensure_found_cart_item
48
+ return if @cart_item
49
+ redirect_to :back, alert: Comable.t('errors.messages.products_not_found')
35
50
  end
36
51
 
37
52
  def find_cart_item
@@ -5,17 +5,17 @@ module Comable
5
5
  before_filter :authenticate_customer!
6
6
 
7
7
  def show
8
+ @orders = current_customer.orders.page(params[:page]).per(Comable::Config.orders_per_page)
8
9
  end
9
10
 
10
- def addresses
11
- return unless request.put?
12
-
11
+ def update_addresses
13
12
  current_customer.attributes = customer_params
14
13
  if current_customer.save
15
- flash.now[:notice] = 'Success'
14
+ flash.now[:notice] = Comable.t('successful')
16
15
  else
17
- flash.now[:alert] = 'Error'
16
+ flash.now[:alert] = Comable.t('failure')
18
17
  end
18
+ render :addresses
19
19
  end
20
20
 
21
21
  def customer_params
@@ -1,45 +1,38 @@
1
1
  module Comable
2
2
  class OrdersController < Comable::ApplicationController
3
- prepend Comable::ShipmentAction
4
- prepend Comable::PaymentAction
5
- include Comable::PermittedAttributes
6
-
7
- helper_method :next_order_path
8
-
9
3
  # TODO: Change the method name to load_order_with_params
10
4
  before_filter :load_order
11
- before_filter :verify
12
- # TODO: Remove
13
- after_filter :save_order, except: :create
14
-
15
- rescue_from Comable::InvalidOrder, with: :order_invalid
5
+ before_filter :ensure_cart_not_empty
6
+ before_filter :ensure_saleable_stocks
16
7
 
17
- def new
18
- redirect_to next_order_path unless agreement_required?
19
- end
8
+ prepend Comable::SigninAction
9
+ prepend Comable::ShipmentAction
10
+ prepend Comable::PaymentAction
11
+ include Comable::PermittedAttributes
20
12
 
21
- def orderer
22
- case request.method_symbol
23
- when :post
24
- redirect_to next_order_path if @order.save
13
+ def edit
14
+ if @order.state?(params[:state]) || @order.stated?(params[:state])
15
+ render params[:state]
16
+ else
17
+ redirect_to next_order_path
25
18
  end
26
19
  end
27
20
 
28
- def delivery
29
- case request.method_symbol
30
- when :post
31
- redirect_to next_order_path if @order.save
21
+ def update
22
+ if @order.stated?(params[:state]) ? @order.save : @order.next_state
23
+ redirect_to next_order_path
24
+ else
25
+ render @order.state
32
26
  end
33
27
  end
34
28
 
35
29
  def create
36
- order = current_customer.order
37
- if order.complete?
38
- flash[:notice] = I18n.t('comable.orders.success')
30
+ if @order.state?(:confirm) && @order.next_state
31
+ flash.now[:notice] = Comable.t('orders.success')
39
32
  send_order_complete_mail
40
33
  else
41
- flash[:alert] = I18n.t('comable.orders.failure')
42
- redirect_to comable.confirm_order_path
34
+ flash[:alert] = Comable.t('orders.failure')
35
+ redirect_to next_order_path
43
36
  end
44
37
  end
45
38
 
@@ -49,53 +42,27 @@ module Comable
49
42
  Comable::OrderMailer.complete(@order).deliver if current_store.email_activate?
50
43
  end
51
44
 
52
- # TODO: Switch to state_machine
53
- # rubocop:disable all
54
- def next_order_path(target_action_name = nil)
55
- case (target_action_name || action_name).to_sym
56
- when :new
57
- orderer_required? ? comable.orderer_order_path : next_order_path(:orderer)
58
- when :orderer
59
- delivery_required? ? comable.delivery_order_path : next_order_path(:delivery)
60
- when :delivery
61
- shipment_required? ? comable.shipment_order_path : next_order_path(:shipment)
62
- when :shipment
63
- payment_required? ? comable.payment_order_path : next_order_path(:payment)
64
- else
65
- comable.confirm_order_path
66
- end
67
- end
68
- # rubocop:enable all
69
-
70
- def agreement_required?
71
- @order.customer.nil?
72
- end
73
-
74
- def orderer_required?
75
- @order.bill_address.nil?
76
- end
77
-
78
- def delivery_required?
79
- @order.ship_address.nil?
80
- end
81
-
82
- def verify
45
+ def ensure_cart_not_empty
83
46
  return if current_customer.cart.any?
84
- flash[:alert] = I18n.t('comable.carts.empty')
47
+ flash[:alert] = Comable.t('carts.empty')
85
48
  redirect_to comable.cart_path
86
49
  end
87
50
 
88
- def load_order
89
- @order = current_customer.preorder(order_params || {})
51
+ def ensure_saleable_stocks
52
+ return if current_order.soldout_stocks.empty?
53
+ flash[:alert] = Comable.t('errors.messages.products_soldout')
54
+ redirect_to comable.cart_path
90
55
  end
91
56
 
92
- def save_order
93
- @order.save
57
+ def load_order
58
+ @order = current_order
59
+ @order.attributes = order_params if order_params
94
60
  end
95
61
 
96
62
  def order_params
97
63
  return unless params[:order]
98
- case action_name.to_sym
64
+ return unless params[:state]
65
+ case params[:state].to_sym
99
66
  when :orderer
100
67
  order_params_for_orderer
101
68
  when :delivery
@@ -105,8 +72,6 @@ module Comable
105
72
 
106
73
  def order_params_for_orderer
107
74
  params.require(:order).permit(
108
- :family_name, # TODO: Remove
109
- :first_name, # TODO: Remove
110
75
  :email,
111
76
  bill_address_attributes: permitted_address_attributes
112
77
  )
@@ -117,10 +82,5 @@ module Comable
117
82
  ship_address_attributes: permitted_address_attributes
118
83
  )
119
84
  end
120
-
121
- def order_invalid
122
- flash[:alert] = I18n.t('comable.orders.failure')
123
- redirect_to comable.cart_path
124
- end
125
85
  end
126
86
  end
@@ -1,11 +1,25 @@
1
1
  module Comable
2
2
  class ProductsController < Comable::ApplicationController
3
+ before_filter :load_category_and_products, only: :index
4
+
3
5
  def index
4
- @products = Comable::Product.all
6
+ @products = @products.page(params[:page]).per(Comable::Config.products_per_page)
5
7
  end
6
8
 
7
9
  def show
8
10
  @product = Comable::Product.find(params[:id])
9
11
  end
12
+
13
+ private
14
+
15
+ def load_category_and_products
16
+ @category = Comable::Category.where(id: params[:category_id]).first
17
+ if @category
18
+ subtree_of_category = Comable::Category.subtree_of(@category)
19
+ @products = Comable::Product.eager_load(:categories).merge(subtree_of_category)
20
+ else
21
+ @products = Comable::Product.search(params[:q])
22
+ end
23
+ end
10
24
  end
11
25
  end
@@ -1,26 +1,16 @@
1
1
  module Comable
2
2
  module PaymentAction
3
- def payment
4
- case request.method_symbol
5
- when :post
6
- redirect_to next_order_path
7
- end
8
- end
9
-
10
3
  private
11
4
 
12
5
  # orderride OrdersController#order_params
13
6
  def order_params
14
- (action_name.to_sym == :payment) ? order_params_for_payment : super
15
- end
16
-
17
- def payment_required?
18
- Comable::Payment.exists?
7
+ return super unless params[:state] == 'payment'
8
+ order_params_for_payment
19
9
  end
20
10
 
21
11
  def order_params_for_payment
22
12
  params.fetch(:order, {}).permit(
23
- :comable_payment_id
13
+ :payment_method_id
24
14
  )
25
15
  end
26
16
  end
@@ -1,21 +1,11 @@
1
1
  module Comable
2
2
  module ShipmentAction
3
- def shipment
4
- case request.method_symbol
5
- when :post
6
- redirect_to next_order_path
7
- end
8
- end
9
-
10
3
  private
11
4
 
12
5
  # orderride OrdersController#order_params
13
6
  def order_params
14
- (action_name.to_sym == :shipment) ? order_params_for_shipment : super
15
- end
16
-
17
- def shipment_required?
18
- Comable::ShipmentMethod.activated.exists?
7
+ return super unless params[:state] == 'shipment'
8
+ order_params_for_shipment
19
9
  end
20
10
 
21
11
  def order_params_for_shipment
@@ -0,0 +1,55 @@
1
+ module Comable
2
+ module SigninAction
3
+ class << self
4
+ def prepended(base)
5
+ base.instance_eval do
6
+ before_filter :ensure_signed_in_or_guest, except: [:signin, :guest]
7
+
8
+ helper_method :resource
9
+ helper_method :resource_name
10
+ helper_method :devise_mapping
11
+ end
12
+ end
13
+ end
14
+
15
+ def guest
16
+ if @order.state?(:cart) ? @order.next_state : @order.save
17
+ redirect_to next_order_path
18
+ else
19
+ render :signin
20
+ end
21
+ end
22
+
23
+ private
24
+
25
+ def ensure_signed_in_or_guest
26
+ return if @order.email
27
+ store_location
28
+ redirect_to comable.signin_order_path
29
+ end
30
+
31
+ def resource
32
+ current_customer
33
+ end
34
+
35
+ def resource_name
36
+ :customer
37
+ end
38
+
39
+ def devise_mapping
40
+ Devise.mappings[resource_name]
41
+ end
42
+
43
+ # orderride OrdersController#order_params
44
+ def order_params
45
+ return super unless action_name.in? %w( signin guest )
46
+ order_params_for_signin
47
+ end
48
+
49
+ def order_params_for_signin
50
+ params.fetch(:order, {}).permit(
51
+ :email
52
+ )
53
+ end
54
+ end
55
+ end
@@ -1,22 +1,67 @@
1
- h1 カート
1
+ #carts
2
+ section.cart.col-sm-8
3
+ h1
4
+ = Comable.t('shopping_cart')
2
5
 
3
- ul.cart
4
- - current_customer.cart.each do |cart_item|
5
- = form_tag comable.cart_path, method: :put do
6
- - stock = cart_item.stock
7
- - product = stock.product
8
- li.product
9
- h2.name
10
- = link_to stock.name_with_sku, comable.product_path(product)
11
- .caption
12
- = product.caption
13
- .price
14
- = number_to_currency product.price
15
- .quantity
16
- - selected = cart_item.quantity
17
- = select_tag :quantity, options_for_select(1.upto([10, selected].max).to_a, selected)
18
- = hidden_field_tag :stock_id, stock.id
19
- = submit_tag '変更'
6
+ - if current_customer.cart.errors.any?
7
+ .errors
8
+ ul
9
+ - current_customer.cart.errors.full_messages.each do |message|
10
+ li = message
20
11
 
21
- .order
22
- = link_to '注文', comable.new_order_path
12
+ - if current_customer.cart.empty?
13
+ = Comable.t('carts.empty')
14
+ - else
15
+ table
16
+ thead
17
+ tr
18
+ th
19
+ th
20
+ = Comable.t('price')
21
+ th
22
+ = Comable.t('quantity')
23
+ th
24
+
25
+ tbody
26
+ - current_customer.cart.each do |cart_item|
27
+ - stock = cart_item.stock
28
+ - product = stock.product
29
+ tr
30
+ td
31
+ .name
32
+ = link_to stock.name_with_sku, comable.product_path(product)
33
+ .caption
34
+ = product.caption
35
+ - if cart_item.soldout_stock?
36
+ .error.soldout = Comable.t('carts.soldout')
37
+ td.price
38
+ = number_to_currency product.price
39
+ td.quantity
40
+ = form_tag comable.cart_path, method: :put do
41
+ .form-inline.form-group
42
+ - selected = cart_item.quantity
43
+ = select_tag :quantity, options_for_select(1.upto([10, selected].max).to_a, selected)
44
+ = hidden_field_tag :stock_id, stock.id
45
+ = submit_tag Comable.t('actions.update'), class: 'btn-default'
46
+ td
47
+ = link_to Comable.t('actions.destroy'), comable.cart_path(stock_id: stock.id), method: :delete
48
+
49
+ - if current_customer.cart.any?
50
+ section.order.col-sm-4
51
+ table
52
+ thead
53
+ tr
54
+ th colspan="2"
55
+ = form_for current_order, as: :order, url: comable.checkout_cart_path, method: :put do |f|
56
+ = f.submit Comable.t('checkout'), class: 'btn btn-primary btn-lg btn-block'
57
+ tbody
58
+ tr.price
59
+ th.text-right
60
+ = Comable.t('item_total_price')
61
+ td
62
+ = number_to_currency current_customer.cart.price
63
+ tr.count
64
+ th.text-right
65
+ = Comable.t('item_total_quantity')
66
+ td
67
+ = number_with_delimiter current_customer.cart.count