Last active
August 29, 2015 14:01
-
-
Save vhyza/5da1b75a47f6fd600a0b to your computer and use it in GitHub Desktop.
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 characters
# -*- 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_development_dependency "rails" | |
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 characters
# 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 |
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 characters
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 |
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 characters
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