| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- require 'uuid'
- class ProductPicture < ActiveRecord::Base
- has_paper_trail
- self.table_name = "product_pictures"
- after_destroy :after_destroy
- belongs_to :product
- validates :product_id, presence: true
- attr_accessor :v_img
- IMG_STORE_PATH = "product_pictures"
- PIC_TYPE_ENUM = [["详情图",1],["轮播图",0]]
- rails_admin do
- navigation_label '商品管理'
- weight -240
- list do
- filters [:id,:pic_type]
- field :id
- field :product
- field :img do
- formatted_value do
- bindings[:view].tag(:img,{:src => bindings[:object].get_img,
- :style => 'width: 100px;height: 100px;cursor: pointer;display: block;max-width: 100px;',
- :onClick => "javascript:window.open('#{bindings[:object].get_img}')"})
- end
- end
- field :pic_type,:enum do
- enum do
- PIC_TYPE_ENUM
- end
- end
- field :sort
- field :created_at
- field :updated_at
- end
- show do
- field :id
- field :product
- field :img do
- formatted_value do
- bindings[:view].tag(:img,{:src => bindings[:object].get_img,
- :style => 'width: 100px;height: 100px;cursor: pointer;display: block;max-width: 100px;',
- :onClick => "javascript:window.open('#{bindings[:object].get_img}')"})
- end
- end
- field :pic_type, :enum do
- enum do
- PIC_TYPE_ENUM
- end
- end
- field :sort
- field :created_at
- field :updated_at
- end
- edit do
- field :product
- field :v_img, :file_upload do
- pretty_value do
- bindings[:view].tag(:img,{:src => bindings[:object].get_img, :class => 'preview'})
- end
- end
- field :pic_type,:enum do
- enum do
- PIC_TYPE_ENUM
- end
- end
- field :sort
- end
- end
- def v_img=file
- unless file.blank?
- # clear_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.img = file_path
- self.save
- end
- end
- def clear_img
- file_path = "#{self.img}"
- Ali::Oss.delete_object(Ali::Oss::BUCKET_NAME_PUBLIC_READ,file_path)
- end
- def get_img
- url = "http://#{Ali::Oss::CDN_URL_FOR_HOST}/#{self.img}"
- return url
- end
- def after_destroy
- clear_img
- end
- end
|