copy_product.rb 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. # encoding:utf-8
  2. # 商品复制
  3. module RailsAdmin
  4. module Config
  5. module Actions
  6. class CopyProduct < RailsAdmin::Config::Actions::Base
  7. register_instance_option :visible? do
  8. if not bindings[:object].blank?
  9. authorized?
  10. else
  11. authorized?
  12. end
  13. end
  14. # We want the action on members, not the Users collection
  15. register_instance_option :member do
  16. true
  17. end
  18. register_instance_option :link_icon do
  19. 'icon-refresh'
  20. end
  21. # You may or may not want pjax for your action
  22. register_instance_option :pjax? do
  23. false
  24. end
  25. register_instance_option :controller do
  26. Proc.new do
  27. if !@object.blank?
  28. @copy_product = Product.new
  29. @copy_product.name = @object.name
  30. @copy_product.ptype = @object.ptype
  31. @copy_product.merchant_id = @object.merchant_id
  32. @copy_product.category_id = @object.category_id
  33. @copy_product.detail = @object.detail
  34. @copy_product.price = @object.price
  35. @copy_product.robo_balance_price = @object.robo_balance_price
  36. @copy_product.buy_price = @object.buy_price
  37. @copy_product.user_sale_price = @object.user_sale_price
  38. @copy_product.count = @object.count
  39. @copy_product.recommend = @object.recommend
  40. @copy_product.status = 0
  41. @copy_product.virtual_sold_count = @object.virtual_sold_count
  42. @copy_product.purchase_limit_count = @object.purchase_limit_count
  43. @copy_product.share_content = @object.share_content
  44. @copy_product.share_img = @object.share_img
  45. @copy_product.deliver_stop_at = @object.deliver_stop_at
  46. @copy_product.deliver_start_at = @object.deliver_start_at
  47. @copy_product.seckill_start = @object.seckill_start
  48. @copy_product.seckill_end = @object.seckill_end
  49. @copy_product.seckill_price = @object.seckill_price
  50. @copy_product.specification = @object.specification
  51. @copy_product.no_delivery_area = @object.no_delivery_area
  52. @copy_product.video_state = @object.video_state
  53. @copy_product.video_url = @object.video_url
  54. @copy_product.show_flag = 0
  55. @copy_product.relate_product_id = @object.id
  56. begin
  57. @copy_product.save
  58. rescue Exception => e
  59. p "************ copy product save fail . #{e.to_s}"
  60. end
  61. # 复制商品图片
  62. pictures = ProductPicture.find_by_sql("select * from product_pictures where pic_type=0 and product_id = #{@object.id} limit 1")
  63. # 创建商品图片
  64. pictures.each do |u|
  65. ProductPicture.create({
  66. :product_id => @copy_product.id,
  67. :img => u.img,
  68. :pic_type => u.pic_type,
  69. :sort => 0,
  70. })
  71. end
  72. redirect_to :back, notice: "已复制!"
  73. # redirect_to "https://d5ctest.oss-cn-shanghai.aliyuncs.com/temp/20180910.xlsx"
  74. end
  75. end
  76. end
  77. end
  78. end
  79. end
  80. end