live_broad.rb 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. self.show=true
  17. end
  18. end
  19. IMG_STORE_PATH = "live_broads"
  20. rails_admin do
  21. navigation_label '直播管理'
  22. weight -100
  23. list do
  24. filters [:id,:title]
  25. field :id
  26. field :title
  27. field :room_id
  28. field :cover do
  29. formatted_value do
  30. bindings[:view].tag(:img,{:src => bindings[:object].get_cover_img,
  31. :style => 'width: 100px;height: 100px;cursor: pointer;display: block;max-width: 100px',
  32. :onClick => "javascript:window.open('#{bindings[:object].get_cover_img}')"})
  33. end
  34. end
  35. field :begin_time
  36. field :end_time
  37. field :show
  38. field :remark
  39. field :created_at
  40. end
  41. show do
  42. field :id
  43. field :title
  44. field :room_id
  45. field :cover do
  46. formatted_value do
  47. bindings[:view].tag(:img,{:src => bindings[:object].get_cover_img,
  48. :style => 'width: 100px;height: 100px;cursor: pointer;display: block;max-width: 100px',
  49. :onClick => "javascript:window.open('#{bindings[:object].get_cover_img}')"})
  50. end
  51. end
  52. field :begin_time
  53. field :end_time
  54. field :show
  55. field :remark
  56. field :created_at
  57. field :updated_at
  58. end
  59. edit do
  60. field :title
  61. field :room_id
  62. field :v_cover, :file_upload do
  63. pretty_value do
  64. bindings[:view].tag(:img, {:src => bindings[:object].get_cover_img, :class => 'preview'})
  65. end
  66. end
  67. field :begin_time
  68. field :end_time
  69. field :show
  70. field :remark
  71. end
  72. end
  73. def get_cover_img
  74. url = "http://#{Ali::Oss::CDN_URL_FOR_HOST}/#{self.cover}"
  75. return url
  76. end
  77. def v_cover=file
  78. unless file.blank?
  79. clear_cover_img
  80. file_name = "#{UUID.new.generate[0...8].downcase}.jpg"
  81. file_path = "#{IMG_STORE_PATH}/#{file_name}"
  82. Ali::Oss.store(Ali::Oss::BUCKET_NAME_PUBLIC_READ, file_path, file.read)
  83. self.cover = file_path
  84. self.save
  85. end
  86. end
  87. def clear_cover_img
  88. file_path = "#{self.cover}"
  89. Ali::Oss.delete_object(Ali::Oss::BUCKET_NAME_PUBLIC_READ, file_path)
  90. end
  91. def after_destroy
  92. clear_cover_img
  93. end
  94. end