live_broad.rb 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. class LiveBroad < ActiveRecord::Base
  2. has_paper_trail
  3. self.table_name = "live_broads"
  4. validates :room_id,:title, presence: true
  5. attr_accessor :v_cover
  6. before_save :before_save
  7. def before_save
  8. #自动取消其他置顶项
  9. if self.show
  10. live_broads = LiveBroad.find_by_sql("select * from live_broads ")
  11. # 创建商品图片
  12. live_broads.each do |l|
  13. l.show=false
  14. l.save
  15. end
  16. end
  17. end
  18. IMG_STORE_PATH = "live_broads"
  19. rails_admin do
  20. navigation_label '直播管理'
  21. weight -100
  22. list do
  23. filters [:id,:title]
  24. field :id
  25. field :title
  26. field :room_id
  27. field :cover do
  28. formatted_value do
  29. bindings[:view].tag(:img,{:src => bindings[:object].get_cover_img,
  30. :style => 'width: 100px;height: 100px;cursor: pointer;display: block;max-width: 100px',
  31. :onClick => "javascript:window.open('#{bindings[:object].get_cover_img}')"})
  32. end
  33. end
  34. field :begin_time
  35. field :end_time
  36. field :show
  37. field :remark
  38. field :created_at
  39. end
  40. show do
  41. field :id
  42. field :title
  43. field :room_id
  44. field :cover do
  45. formatted_value do
  46. bindings[:view].tag(:img,{:src => bindings[:object].get_cover_img,
  47. :style => 'width: 100px;height: 100px;cursor: pointer;display: block;max-width: 100px',
  48. :onClick => "javascript:window.open('#{bindings[:object].get_cover_img}')"})
  49. end
  50. end
  51. field :begin_time
  52. field :end_time
  53. field :show
  54. field :remark
  55. field :created_at
  56. field :updated_at
  57. end
  58. edit do
  59. field :title
  60. field :room_id
  61. field :v_cover, :file_upload do
  62. pretty_value do
  63. bindings[:view].tag(:img, {:src => bindings[:object].get_cover_img, :class => 'preview'})
  64. end
  65. end
  66. field :begin_time
  67. field :end_time
  68. field :show
  69. field :remark
  70. end
  71. end
  72. def get_cover_img
  73. url = "http://#{Ali::Oss::CDN_URL_FOR_HOST}/#{self.cover}"
  74. return url
  75. end
  76. def v_cover=file
  77. unless file.blank?
  78. clear_cover_img
  79. file_name = "#{UUID.new.generate[0...8].downcase}.jpg"
  80. file_path = "#{IMG_STORE_PATH}/#{file_name}"
  81. Ali::Oss.store(Ali::Oss::BUCKET_NAME_PUBLIC_READ, file_path, file.read)
  82. self.cover = file_path
  83. self.save
  84. end
  85. end
  86. def clear_cover_img
  87. file_path = "#{self.cover}"
  88. Ali::Oss.delete_object(Ali::Oss::BUCKET_NAME_PUBLIC_READ, file_path)
  89. end
  90. def after_destroy
  91. clear_cover_img
  92. end
  93. end