-
-
Save karmi/f1dc8c75d67b92b23a55 to your computer and use it in GitHub Desktop.
Revisions
-
karmi revised this gist
May 18, 2014 . 1 changed file with 36 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,36 @@ # Store file uploads in the database This gem allows to store [`carrierwave`](https://github.com/carrierwaveuploader/carrierwave) uploads in the database, eg. on platforms with ephemeral filesystems like Heroku. ## Installation ```ruby # In: Gemfile gem 'carrierwave-blob', git: "https://gist.github.com/f1dc8c75d67b92b23a55.git" ``` ## Usage 1. Setup your uploader to use the _blob_ storage: ```ruby # In: app/uploaders/picture_uploader.rb storage CarrierWave::Storage::Blob ``` 2. Generate a migration for your model: ```ruby rails generate carrier_wave_blob_migration Idea ``` 3. Execute the migrations: ```ruby rake db:migrate ``` ----- Created during [RailsGirls Prague](http://railsgirls.cz) -
karmi revised this gist
May 18, 2014 . 2 changed files with 27 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,20 @@ class CarrierWaveBlobMigrationGenerator < Rails::Generators::NamedBase desc "Creates a migration to update column type to text" argument :fieldname, default: 'picture' check_class_collision def create_migration_file migration_nr = ActiveRecord::Migration.next_migration_number(Time.now.to_i) create_file "db/migrate/#{migration_nr}_update_#{name.downcase}_#{fieldname.downcase}_column.rb", <<-CONTENT.gsub(/^\s{6}/, '') class Update#{name.capitalize}#{fieldname.capitalize}Column < ActiveRecord::Migration def change change_column :#{name.tableize}, :#{fieldname}, :text, limit: nil end end CONTENT say "Don't forget to run `rake db:migrate`!", :yellow end end This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -10,7 +10,7 @@ def initialize(content) end def url(options={}) @content end def to_s @@ -32,3 +32,9 @@ def retrieve!(identifier=nil) end end class CarrierWaveBlobRailtie < Rails::Railtie generators do require 'carrierwave-blob-generator' end end -
vhyza revised this gist
May 17, 2014 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -22,11 +22,11 @@ def store!(file) # require "pry"; binding.pry type = uploader.file.content_type blob = Base64.encode64(self.uploader.file.read) uploader.model.update_columns self.uploader.mounted_as => "data:#{type};base64,#{blob}" end def retrieve!(identifier=nil) Content.new uploader.model.read_attribute(self.uploader.mounted_as) end end -
vhyza revised this gist
May 17, 2014 . 2 changed files with 52 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1 +1,18 @@ # -*- encoding: utf-8 -*- Gem::Specification.new do |s| s.name = "carrierwave-blob" s.version = "0.0.1" s.platform = Gem::Platform::RUBY s.summary = "BLOB support for Carrierwave" s.authors = [ 'Karel Minarik', 'Vojtech Hyza' ] s.email = [ '[email protected]', '[email protected]' ] s.files = `git ls-files`.split("\n") s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n") s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) } s.require_paths = ["."] s.add_dependency "carrierwave" end This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,34 @@ # encoding: utf-8 module CarrierWave module Storage class Blob < Abstract class Content def initialize(content) @content = content end def url(options={}) @content end def to_s @content end end def store!(file) # require "pry"; binding.pry type = uploader.file.content_type blob = Base64.encode64(self.uploader.file.read) uploader.model.update_columns picture: "data:#{type};base64,#{blob}" end def retrieve!(identifier=nil) Content.new uploader.model.read_attribute(:picture) end end end end -
vhyza created this gist
May 17, 2014 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1 @@ TEST