Class: IshManager::ProductsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/ish_manager/products_controller.rb

Instance Method Summary collapse

Methods inherited from ApplicationController

#basic_auth, #home, #tinymce

Instance Method Details

#createObject

alphabetized : )



8
9
10
11
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
# File 'app/controllers/ish_manager/products_controller.rb', line 8

def create
  @product = Wco::Product.new params[:product].permit!
  authorize! :create, @product

  stripe_product = Stripe::Product.create({ name: params[:product][:name] })
  puts! stripe_product, 'stripe_product'

  price_hash = {
    product: stripe_product.id,
    unit_amount: params[:product][:price_cents],
    currency: 'usd',
  }
  if params[:product][:interval].present?
    price_hash[:recurring] = { interval: params[:product][:interval] }
  end
  stripe_price = Stripe::Price.create( price_hash )
  puts! stripe_price, 'stripe_price'

  @product.product_id = stripe_product[:id]
  @product.price_id   = stripe_price[:id]

  flag = @product.save
  if flag
    flash[:notice] = 'Created the product.'
    redirect_to action: :index
  else
    flash[:alert] = "No luck: #{@product.errors.full_messages.join(', ')}."
    render action: :index
  end
end

#destroyObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/controllers/ish_manager/products_controller.rb', line 39

def destroy
  authorize! :destroy, Wco::Product
  # product = Wco::Product.find params[:id]

  Stripe.api_key = ::STRIPE_SK
  Stripe.api_version = '2020-08-27'
  flag = Stripe::Product.delete( params[:id] )
  # puts! flag, 'flag'
  flash[:notice] = flag
  # if product.destroy
  #   flash[:notice] = 'Success'
  # else
  #   flash[:alert] = "Cannot destroy product: #{product.errors.fill_messages.join(', ')}."
  # end
  redirect_to action: :index
end

#editObject



56
57
58
59
# File 'app/controllers/ish_manager/products_controller.rb', line 56

def edit
  @product = Wco::Product.find params[:id]
  authorize! :edit, @product
end

#indexObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'app/controllers/ish_manager/products_controller.rb', line 61

def index
  authorize! :index, Wco::Product
  @stripe_products = {}

  Stripe.api_key = ::STRIPE_SK
  Stripe.api_version = '2020-08-27'
  @_stripe_products = Stripe::Product.list().data
  @_stripe_prices = Stripe::Price.list().data

  @_stripe_products.each do |sp|
    @stripe_products[sp[:id]] = sp
    @stripe_products[sp[:id]][:prices] ||= {}
  end
  @_stripe_prices.each do |price|
    @stripe_products[price[:product]][:prices][price[:id]] = price
  end

  @products = Wco::Product.all

end

#showObject



82
83
84
85
# File 'app/controllers/ish_manager/products_controller.rb', line 82

def show
  authorize! :show, @product
  @product = Wco::Product.find params[:id]
end

#updateObject



87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'app/controllers/ish_manager/products_controller.rb', line 87

def update
  authorize! :edit, @product
  @product = Wco::Product.find params[:id]

  if @product.update_attributes params[:product].permit!
    flash[:notice] = 'Success.'
    redirect_to action: :show, id: @product.id
  else
    flash[:alert] = "No luck: #{@product.errors.full_messages.join(', ')}."
    render action: :edit
  end

end