clean up and rework a ton of stuff

This commit is contained in:
John Bintz 2013-03-11 16:21:59 -04:00
parent 900238b1e1
commit 669be8430d
10 changed files with 131 additions and 66 deletions

View File

@ -8,7 +8,7 @@ My::Application.routes.draw do
end end
``` ```
And you'll have a `/media/*path` route that just delivers whatever Carrierwave uploads to this app, as long as And you'll have a `/media/:id` 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 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. `grid_fs_access_url` will be set correctly for you, too.
@ -17,7 +17,7 @@ You can specify if any of the paths should respond with `Content-Disposition: at
``` ruby ``` ruby
# config/initializers/carrierwave_mongoid_media.rb # config/initializers/carrierwave_mongoid_media.rb
CarrierwaveMongoidMedia.force_downloads_on do |path| CarrierwaveMongoidMedium.force_downloads_on do |path|
# return true if the file should get Content-Disposition: attachment # return true if the file should get Content-Disposition: attachment
end end
``` ```

View File

@ -0,0 +1,29 @@
module CarrierwaveMongoidMediaController
def show
resource.as_response_for(self)
end
def create
with_ok { create! }
end
def create!
resource.update_attributes(params)
end
def destroy
with_ok { destroy! }
end
def destroy!
resource.destroy
end
private
def with_ok
yield
head :ok
end
end

View File

@ -0,0 +1,9 @@
class Carrierwave::MongoidMediaController < ApplicationController
include CarrierwaveMongoidMediaController
private
def resource
@carrierwave_mongoid_media ||= CarrierwaveMongoidMedium.find(params[:id])
end
end

View File

@ -1,4 +0,0 @@
class MediaController < ApplicationController
include CarrierWave::Mongoid::Media::ControllerBehavior
end

View File

@ -1,40 +0,0 @@
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

@ -0,0 +1,86 @@
require 'virtus'
class CarrierwaveMongoidMedium
include Virtus
attribute :id
attribute :data
def self.find(id)
medium = new(:id => id)
raise_not_found(id) if !medium.gridfs_object
medium
end
def self.raise_not_found(id)
raise Mongoid::Errors::DocumentNotFound.new(Mongoid::GridFS, :filename => id)
end
def raise_not_found
self.class.raise_not_found(id)
end
def self.force_downloads_on(&block)
if block
@block = block
else
@block
end
end
def update_attributes(new_attributes = {})
self.attributes = new_attributes
save
end
def content_type
gridfs_object.content_type
end
def self.create(attributes = {})
new(attributes).save
end
def destroy
gridfs_object.destroy
@gridfs_object = nil
end
def save
attrs = attributes.dup
file = attrs.delete(:data)
filename = attrs.delete(:id) || attrs.delete(:filename)
@gridfs_object = Mongoid::GridFS.put(file, attrs.merge(:filename => filename))
end
def attachment_filename
::File.basename(id)
end
def gridfs_object
@gridfs_object ||= Mongoid::GridFS[id]
end
def force_download?
if block = self.class.force_downloads_on
block.call self
else
false
end
end
def as_response_for(controller_instance)
controller_instance.content_type = self.content_type
controller_instance.response_body = self.gridfs_object
if self.force_download?
controller_instance.response.headers['Content-Disposition'] = "attachment; filename='#{self.attachment_filename}'"
end
end
end

View File

@ -19,4 +19,5 @@ Gem::Specification.new do |gem|
gem.add_dependency 'carrierwave' gem.add_dependency 'carrierwave'
gem.add_dependency 'carrierwave-mongoid' gem.add_dependency 'carrierwave-mongoid'
gem.add_dependency 'virtus'
end end

View File

@ -2,5 +2,4 @@ require "carrierwave-mongoid-media/version"
if defined?(Rails::Engine) if defined?(Rails::Engine)
require 'carrierwave-mongoid-media/engine' require 'carrierwave-mongoid-media/engine'
require 'carrierwave/mongoid/media/controller_behavior'
end end

View File

@ -5,6 +5,8 @@ module CarrierWave
class Media < ::Rails::Engine class Media < ::Rails::Engine
DEFAULT_PREFIX = :media DEFAULT_PREFIX = :media
config.autoload_paths << File.expand_path("../../../app/behaviors", __FILE__)
class << self class << self
attr_writer :prefix attr_writer :prefix
end end
@ -16,7 +18,7 @@ module CarrierWave
def self.routes(router, options = {}) def self.routes(router, options = {})
options = { options = {
:prefix => :media, :prefix => :media,
:controller => :media :controller => "carrierwave/mongoid_media"
}.merge(options) }.merge(options)
self.prefix = options[:prefix] self.prefix = options[:prefix]
@ -26,7 +28,7 @@ module CarrierWave
end end
router.instance_exec do router.instance_exec do
get "#{options[:prefix]}/*path" => "#{options[:controller]}#show", :as => :media get "#{options[:prefix]}/:id" => "#{options[:controller]}#show", :as => :media, :id => /.*/
end end
end end
end end

View File

@ -1,17 +0,0 @@
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