live_broad.rb 3.5 KB

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