Class: Trade::Admin::TradeItemsController

Inherits:
BaseController
  • Object
show all
Defined in:
app/controllers/trade/admin/trade_items_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/controllers/trade/admin/trade_items_controller.rb', line 41

def create
  cart_item = @cart_items.unscope(where: :status).where(good_id: params[:good_id], good_type: params[:good_type]).first
  if cart_item.present?
    params[:quantity] ||= 0
    cart_item.checked = true
    cart_item.status = 'pending'
    cart_item.quantity = cart_item.quantity + params[:quantity].to_i
    cart_item.save
  else
    cart_item = @cart_items.build(good_id: params[:good_id], good_type: params[:good_type], quantity: params[:quantity], myself: false)
    cart_item.checked = true
    cart_item.save
  end

  @checked_ids = @cart_items.checked.pluck(:id)

  render 'index'
end

#destroyObject



80
81
82
# File 'app/controllers/trade/admin/trade_items_controller.rb', line 80

def destroy
  @cart_item.destroy
end

#docObject



72
73
74
# File 'app/controllers/trade/admin/trade_items_controller.rb', line 72

def doc

end

#indexObject



7
8
9
10
# File 'app/controllers/trade/admin/trade_items_controller.rb', line 7

def index
  @cart_items = @cart_items.page(params[:page])
  @checked_ids = @cart_items.checked.pluck(:id)
end

#onlyObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'app/controllers/trade/admin/trade_items_controller.rb', line 12

def only
  unless params[:good_type] && params[:good_id]
    redirect_back(fallback_location: admin_cart_items_url) and return
  end

  good = params[:good_type].safe_constantize&.find_by(id: params[:good_id])
  if good.respond_to?(:user_id)
    @user = good.user
    @buyer = @user.buyer
    @cart_items = CartItem.where(good_type: params[:good_type], good_id: params[:good_id], user_id: good.user_id)
  end

  @cart_item = @cart_items.where(good_id: params[:good_id], good_type: params[:good_type]).first
  if @cart_item.present?
    params[:quantity] ||= 0
    @cart_item.checked = true
    @cart_item.quantity = @cart_item.quantity + params[:quantity].to_i
    @cart_item.save
  else
    @cart_item = @cart_items.build(good_id: params[:good_id], good_type: params[:good_type], quantity: params[:quantity])
    @cart_item.checked = true
    @cart_item.save
  end

  @additions = @cart_item.total

  render 'only'
end

#totalObject



60
61
62
63
64
65
66
67
68
69
70
# File 'app/controllers/trade/admin/trade_items_controller.rb', line 60

def total
  if params[:add_id].present?
    @add = @cart_items.find_by(id: params[:add_id])
    @add.update(checked: true)
  elsif params[:remove_id].present?
    @remove = @cart_items.find_by(id: params[:remove_id])
    @remove.update(checked: false)
  end

  response.headers['X-Request-URL'] = request.url
end

#updateObject



76
77
78
# File 'app/controllers/trade/admin/trade_items_controller.rb', line 76

def update
  @cart_item.update(quantity: params[:quantity])
end