Guest User

Untitled

a guest
Aug 15th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.84 KB | None | 0 0
  1. anex = Anex.new(file_data_uri: file) # file is a base64 string
  2. resource.anexes << anex
  3. resource.save!
  4.  
  5. require "shrine"
  6. require "shrine/storage/file_system"
  7.  
  8. Shrine.storages = {
  9. cache: Shrine::Storage::FileSystem.new("public", prefix: "uploads/cache"), # temporary
  10. store: Shrine::Storage::FileSystem.new("public", prefix: "uploads/store"), # permanent
  11. }
  12.  
  13. Shrine.plugin :activerecord
  14. Shrine.plugin :cached_attachment_data # for forms
  15. Shrine.plugin :data_uri # for base_64
  16. Shrine.plugin :infer_extension
  17. # Shrine.plugin :rack_file # for non-Rails apps
  18.  
  19. class FileUploader < Shrine
  20. # plugins and uploading logic
  21.  
  22. end
  23.  
  24. class Anex < ApplicationRecord
  25. belongs_to :anexable, polymorphic: true
  26.  
  27. include FileUploader::Attachment.new(:file) # adds an `file` virtual attribute
  28.  
  29. def url
  30. "#{ENV['FILE_STORAGE_URI']}#{self.file.url}"
  31. end
  32. end
Add Comment
Please, Sign In to add comment