app/controllers/trade/my/carts_controller.rb in rails_trade-0.0.2 vs app/controllers/trade/my/carts_controller.rb in rails_trade-0.0.3
- old
+ new
@@ -1,32 +1,53 @@
-class Trade::My::CartsController < Trade::My::BaseController
+module Trade
+ class My::CartsController < My::BaseController
- def index
- end
+ def show
+ q_params = {
+ status: ['init', 'checked']
+ }
+ if params[:address_id].present?
+ current_cart.update address_id: params[:address_id]
+ end
- def show
- @trade_items = current_cart.trade_items.page(params[:page])
- @checked_ids = current_cart.trade_items.checked.pluck(:id)
+ @trade_items = current_cart.trade_items.default_where(q_params).page(params[:page])
+ @checked_ids = current_cart.trade_items.default_where(q_params).unscope(where: :status).checked.pluck(:id)
+ end
- current_cart.compute_price
- end
+ def add
+ trade_item = current_cart.trade_items.where(status: ['init', 'checked']).find_by(good_id: params[:good_id], good_type: params[:good_type]) || current_cart.trade_items.build(good_id: params[:good_id], good_type: params[:good_type])
+ if trade_item.persisted?
+ params[:number] ||= 1
+ trade_item.number += params[:number].to_i
+ end
+ trade_item.save
- def total
- end
+ @trade_items = current_cart.trade_items.where(status: ['init', 'checked']).page(params[:page])
+ @checked_ids = current_cart.trade_items.checked.pluck(:id)
- def edit
+ redirect_to({ action: 'show' })
+ end
- end
+ def addresses
+ @addresses = current_user.addresses.order(id: :asc)
+ end
- def update
+ def edit
+ end
- end
+ def update
+ current_cart.assign_attributes(cart_params)
- private
- def cart_params
- params.fetch(:cart, {}).permit(
- :buyer_type,
- :buyer_id
- )
- end
+ unless current_cart.save
+ render :edit, locals: { model: current_cart }, status: :unprocessable_entity
+ end
+ end
+ private
+ def cart_params
+ params.fetch(:cart, {}).permit(
+ :address_id
+ )
+ end
+
+ end
end