Skip to content

Instantly share code, notes, and snippets.

@karmi
Forked from vhyza/carrierwave-blob.gemspec
Last active August 29, 2015 14:01
Show Gist options
  • Save karmi/f1dc8c75d67b92b23a55 to your computer and use it in GitHub Desktop.
Save karmi/f1dc8c75d67b92b23a55 to your computer and use it in GitHub Desktop.

Revisions

  1. karmi revised this gist May 18, 2014. 1 changed file with 36 additions and 0 deletions.
    36 changes: 36 additions & 0 deletions README.markdown
    Original 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)
  2. karmi revised this gist May 18, 2014. 2 changed files with 27 additions and 1 deletion.
    20 changes: 20 additions & 0 deletions carrierwave-blob-generator.rb
    Original 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
    8 changes: 7 additions & 1 deletion carrierwave-blob.rb
    Original file line number Diff line number Diff line change
    @@ -10,7 +10,7 @@ def initialize(content)
    end

    def url(options={})
    @content
    @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
  3. @vhyza vhyza revised this gist May 17, 2014. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions carrierwave-blob.rb
    Original 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 picture: "data:#{type};base64,#{blob}"
    uploader.model.update_columns self.uploader.mounted_as => "data:#{type};base64,#{blob}"
    end

    def retrieve!(identifier=nil)
    Content.new uploader.model.read_attribute(:picture)
    Content.new uploader.model.read_attribute(self.uploader.mounted_as)
    end
    end

  4. @vhyza vhyza revised this gist May 17, 2014. 2 changed files with 52 additions and 1 deletion.
    19 changes: 18 additions & 1 deletion carrierwave-blob.gemspec
    Original file line number Diff line number Diff line change
    @@ -1 +1,18 @@
    TEST
    # -*- 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
    34 changes: 34 additions & 0 deletions carrierwave-blob.rb
    Original 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
  5. @vhyza vhyza created this gist May 17, 2014.
    1 change: 1 addition & 0 deletions carrierwave-blob.gemspec
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    TEST