clean some things up and add a readme

This commit is contained in:
John Bintz 2013-02-22 16:58:41 -05:00
parent ba98341916
commit 900238b1e1
8 changed files with 92 additions and 36 deletions

View File

@ -1,29 +1,24 @@
# Carrierwave::Mongoid::Media Make it very easy to accept Carrierwave uploads to a Mongoid GridFS-backed data store and then deliver them.
TODO: Write a gem description Add this to your `config/routes.rb`:
## Installation ``` ruby
My::Application.routes.draw do
CarrierWave::Mongoid::Media.routes(self)
end
```
Add this line to your application's Gemfile: And you'll have a `/media/*path` route that just delivers whatever Carrierwave uploads to this app, as long as
you're using [carrierwave-mongoid](https://github.com/jnicklas/carrierwave-mongoid) to do the uploads. CarrierWave's
`grid_fs_access_url` will be set correctly for you, too.
gem 'carrierwave-mongoid-media' You can specify if any of the paths should respond with `Content-Disposition: attachment` headers with an initializer:
And then execute: ``` ruby
# config/initializers/carrierwave_mongoid_media.rb
$ bundle CarrierwaveMongoidMedia.force_downloads_on do |path|
# return true if the file should get Content-Disposition: attachment
end
```
Or install it yourself as:
$ gem install carrierwave-mongoid-media
## Usage
TODO: Write usage instructions here
## Contributing
1. Fork it
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create new Pull Request

View File

@ -1,12 +1,4 @@
class MediaController < ApplicationController class MediaController < ApplicationController
def show include CarrierWave::Mongoid::Media::ControllerBehavior
path = params[:path] + '.' + params[:format]
obj = Mongoid::GridFS[path]
raise Mongoid::Errors::DocumentNotFound.new(Mongoid::GridFS, :path => path) if !obj
self.content_type = obj.content_type
self.response_body = obj
end
end end

View File

@ -0,0 +1,40 @@
require 'delegate'
class CarrierwaveMongoidMedia < SimpleDelegator
def self.find(path)
obj = Mongoid::GridFS[path]
raise Mongoid::Errors::DocumentNotFound.new(Mongoid::GridFS, :path => path) if !obj
new(obj)
end
def self.force_downloads_on(&block)
if block
@block = block
else
@block
end
end
def attachment_filename
File.basename(self.filename)
end
def initialize(gridfs_object)
@gridfs_object = gridfs_object
end
def __getobj__
@gridfs_object
end
def force_download?
if block = self.class.force_downloads_on
block.call(self.filename)
else
false
end
end
end

View File

@ -16,4 +16,7 @@ Gem::Specification.new do |gem|
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) } gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
gem.test_files = gem.files.grep(%r{^(test|spec|features)/}) gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
gem.require_paths = ["lib"] gem.require_paths = ["lib"]
gem.add_dependency 'carrierwave'
gem.add_dependency 'carrierwave-mongoid'
end end

View File

@ -1,3 +1,6 @@
require "carrierwave-mongoid-media/version" require "carrierwave-mongoid-media/version"
require 'carrierwave-mongoid-media/engine' if defined?(Rails::Engine)
if defined?(Rails::Engine)
require 'carrierwave-mongoid-media/engine'
require 'carrierwave/mongoid/media/controller_behavior'
end

View File

@ -13,15 +13,20 @@ module CarrierWave
@prefix ||= DEFAULT_PREFIX @prefix ||= DEFAULT_PREFIX
end end
def self.routes(router, prefix = :media) def self.routes(router, options = {})
self.prefix = prefix options = {
:prefix => :media,
:controller => :media
}.merge(options)
self.prefix = options[:prefix]
CarrierWave.configure do |c| CarrierWave.configure do |c|
c.grid_fs_access_url = "/#{prefix}" c.grid_fs_access_url = "/#{options[:prefix]}"
end end
router.instance_exec do router.instance_exec do
get "#{prefix}/*path" => "media#show", :as => :media get "#{options[:prefix]}/*path" => "#{options[:controller]}#show", :as => :media
end end
end end
end end

View File

@ -0,0 +1 @@
require 'carrierwave-mongoid-media'

View File

@ -0,0 +1,17 @@
module CarrierWave::Mongoid::Media::ControllerBehavior
def show
obj = CarrierwaveMongoidMedia.find(path)
self.content_type = obj.content_type
self.response_body = obj
if obj.force_download?
response.headers['Content-Disposition'] = "attachment; filename='#{obj.attachment_filename}'"
end
end
private
def path
@path ||= params[:path] + '.' + params[:format]
end
end