class LiveBroad < ActiveRecord::Base has_paper_trail self.table_name = "live_broads" validates :room_id,:title, presence: true attr_accessor :v_cover before_save :before_save def before_save #自动取消其他置顶项 if self.show live_broads = LiveBroad.find_by_sql("select * from live_broads ") # 创建商品图片 live_broads.each do |l| if l.id != self.id l.show=false l.save end end end end IMG_STORE_PATH = "live_broads" rails_admin do navigation_label '直播管理' weight -100 list do filters [:id,:title] field :id field :title field :room_id field :cover do formatted_value do bindings[:view].tag(:img,{:src => bindings[:object].get_cover_img, :style => 'width: 100px;height: 100px;cursor: pointer;display: block;max-width: 100px', :onClick => "javascript:window.open('#{bindings[:object].get_cover_img}')"}) end end field :begin_time field :end_time field :show field :remark field :created_at end show do field :id field :title field :room_id field :cover do formatted_value do bindings[:view].tag(:img,{:src => bindings[:object].get_cover_img, :style => 'width: 100px;height: 100px;cursor: pointer;display: block;max-width: 100px', :onClick => "javascript:window.open('#{bindings[:object].get_cover_img}')"}) end end field :begin_time field :end_time field :show field :remark field :created_at field :updated_at end edit do field :title field :room_id field :v_cover, :file_upload do pretty_value do bindings[:view].tag(:img, {:src => bindings[:object].get_cover_img, :class => 'preview'}) end end field :begin_time field :end_time field :show field :remark end end def get_cover_img url = "http://#{Ali::Oss::CDN_URL_FOR_HOST}/#{self.cover}" return url end def v_cover=file unless file.blank? clear_cover_img file_name = "#{UUID.new.generate[0...8].downcase}.jpg" file_path = "#{IMG_STORE_PATH}/#{file_name}" Ali::Oss.store(Ali::Oss::BUCKET_NAME_PUBLIC_READ, file_path, file.read) self.cover = file_path self.save end end def clear_cover_img file_path = "#{self.cover}" Ali::Oss.delete_object(Ali::Oss::BUCKET_NAME_PUBLIC_READ, file_path) end def after_destroy clear_cover_img end end