Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Implement initial version.

Add Ripple ORM adapter.
Add Riak storage adapter.
  • Loading branch information...
commit e099fa281c2013a3a0354e64b86532dc864ed3a8 1 parent 516ec24
@tylerhunt authored
Showing with 123 additions and 1 deletion.
  1. +44 −1 lib/carrierwave/ripple.rb
  2. +79 −0 lib/carrierwave/storage/riak.rb
View
45 lib/carrierwave/ripple.rb
@@ -1,6 +1,49 @@
require 'carrierwave/ripple/version'
+require 'carrierwave/validations/active_model'
+require 'ripple/document'
-module Carrierwave
+module CarrierWave
module Ripple
+ include CarrierWave::Mount
+
+ def mount_uploader(column, uploader=nil, options={}, &block)
+ property column, String
+
+ super
+
+ alias_method :read_uploader, :[]
+ alias_method :write_uploader, :[]=
+
+ include CarrierWave::Validations::ActiveModel
+
+ if uploader_option(column.to_sym, :validate_integrity)
+ validates_integrity_of column
+ end
+
+ if uploader_option(column.to_sym, :validate_processing)
+ validates_processing_of column
+ end
+
+ after_save :"store_#{column}!"
+ before_save :"write_#{column}_identifier"
+ after_destroy :"remove_#{column}!"
+ before_update :"store_previous_model_for_#{column}"
+ after_save :"remove_previously_stored_#{column}"
+
+ class_eval <<-RUBY, __FILE__, __LINE__ + 1
+ def #{column}=(new_file)
+ column = _mounter(:#{column}).serialization_column
+
+ if new_file.is_a?(String)
+ write_uploader(column, new_file)
+ else
+ send(:"\#{column}_will_change!")
+ super
+ end
+ end
+ RUBY
+ end
end
end
+
+Ripple::Document::ClassMethods.send(:include, CarrierWave::Ripple)
View
79 lib/carrierwave/storage/riak.rb
@@ -0,0 +1,79 @@
+module CarrierWave
+ module Storage
+ class Riak < Abstract
+ class File
+ attr_reader :path
+
+ def initialize(uploader, path)
+ @uploader = uploader
+ @path = path
+ end
+
+ def write(file)
+ robject = bucket.new(key)
+ robject.content_type = file.content_type
+ robject.raw_data = file.read
+ robject.store
+ end
+
+ def read
+ @read ||= robject.raw_data
+ end
+
+ def delete
+ robject.delete
+ end
+
+ def content_type
+ robject.content_type
+ end
+
+ def size
+ read.length
+ end
+
+ def url
+ robject.url
+ end
+
+ def riak
+ @uploader.riak_client
+ end
+ private :riak
+
+ def bucket
+ riak.bucket(@uploader.riak_bucket_name)
+ end
+ private :bucket
+
+ def key
+ CGI.escape(path)
+ end
+ private :key
+
+ def robject
+ @robject ||= bucket.get(key)
+ end
+ private :robject
+ end
+
+ def store!(file)
+ File.new(uploader, uploader.store_path).tap do |stored|
+ stored.write(file)
+ end
+ end
+
+ def retrieve!(identifier)
+ File.new(uploader, uploader.store_path(identifier))
+ end
+ end
+ end
+
+ Uploader::Base.add_config :riak_client
+ Uploader::Base.add_config :riak_bucket_name
+
+ configure do |config|
+ config.storage_engines[:riak] = 'CarrierWave::Storage::Riak'
+ config.riak_bucket_name = 'uploads'
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.