Skip to content

Instantly share code, notes, and snippets.

@vhyza
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save vhyza/5da1b75a47f6fd600a0b to your computer and use it in GitHub Desktop.
Save vhyza/5da1b75a47f6fd600a0b to your computer and use it in GitHub Desktop.
# -*- 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 = [ 'karmi@karmi.cz', 'vhyza@vhyza.eu' ]
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_development_dependency "rails"
s.add_dependency "carrierwave"
end
# encoding: utf-8
require 'railtie' if defined?(Rails)
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 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
end
end
require 'rails/generators/base'
class CarrierwaveBlobGenerator < Rails::Generators::NamedBase
desc "Create migration changing carrierwave column to text."
argument :column, default: 'picture'
def create_migration_file
create_file "db/migrate/#{ActiveRecord::Migration.next_migration_number(1)}_update_carrierwave_column.rb", <<-CONTENT
class UpdateCarrierwaveColumn < ActiveRecord::Migration
def change
change_column(:#{name.tableize}, :#{column}, :text, limit: nil)
end
end
CONTENT
say "Run `bundle exec rake db:migrate` to apply generated migration."
end
end
require 'rails'
module CarrierWave
class Railtie < ::Rails::Railtie
generators do
require "carrierwave_blob_generator"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment