Skip to content

Commit

Permalink
add basic support for file attachments
Browse files Browse the repository at this point in the history
  • Loading branch information
al6x committed May 18, 2011
1 parent 09744b8 commit 6571f1a
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 2 deletions.
2 changes: 2 additions & 0 deletions lib/mongo_mapper_ext.rb
@@ -1,5 +1,6 @@
require 'mongo_mapper_ext/gems'
require 'mongo_mapper'
require 'carrierwave'

[
'hacks/fixes',
Expand All @@ -15,6 +16,7 @@
'plugins/default_scope',
# 'plugins/db_config',
'plugins/attributes_cache',
'plugins/carrierwave',
'plugins/micelaneous',
].each do |file|
require "mongo_mapper_ext/#{file}"
Expand Down
9 changes: 7 additions & 2 deletions lib/mongo_mapper_ext/gems.rb
@@ -1,3 +1,8 @@
# core gems
gem 'mongo_mapper', '0.9.0'

gem 'carrierwave', '0.5.3'

# dependencies for core gems
gem 'i18n', '0.5.0'
gem 'activesupport', '3.0.7'
Expand All @@ -8,5 +13,5 @@
gem 'plucky', '0.3.6'
gem 'jnunemaker-validatable', '1.8.4'

# core gems
gem 'mongo_mapper', '0.9.0'
gem 'mini_magick', '3.2.1'
gem 'subexec', '0.0.4'
71 changes: 71 additions & 0 deletions lib/mongo_mapper_ext/plugins/carrierwave.rb
@@ -0,0 +1,71 @@
require 'carrierwave/validations/active_model'

#
# Hacks
#
CarrierWave::SanitizedFile.class_eval do
def sanitize_regexp
/[^[:word:]\.\-\+\s_]/i
end
end

CarrierWave::Uploader::Cache.class_eval do
def original_filename=(filename)
raise CarrierWave::InvalidParameter, "invalid filename" unless filename =~ /\A[[:word:]\.\-\+\s_]+\z/i
@original_filename = filename
end
end


#
# mount_uploader
#
module MongoMapper
module Plugins
module CarrierWave
extend ActiveSupport::Concern

module ClassMethods
include ::CarrierWave::Mount
##
# See +CarrierWave::Mount#mount_uploader+ for documentation
#
def mount_uploader(column, uploader, options={}, &block)
define_key column, uploader, options

super

alias_method :read_uploader, :read_attribute
alias_method :write_uploader, :write_attribute

include ::CarrierWave::Validations::ActiveModel

validates_integrity_of column if uploader_option(column.to_sym, :validate_integrity)
validates_processing_of column if uploader_option(column.to_sym, :validate_processing)

after_save "store_#{column}!".to_sym
before_save "write_#{column}_identifier".to_sym
after_destroy "remove_#{column}!".to_sym
end

protected
def define_key column, uploader, options
options[:mount_on] ||= "#{column}_filename"
name = options[:mount_on]
key name

# # not allowin
# before_save do |model|
# filename = model.send(name).filename
#
# end
# rad.extension(:kit_carrierwave, self){validates_uniqueness_of name}
#
# ensure_index name
end
end
end
end
end

MongoMapper::Document.include MongoMapper::Plugins::CarrierWave
52 changes: 52 additions & 0 deletions spec/carrierwave_spec.rb
@@ -0,0 +1,52 @@
require 'spec_helper'

require "mongo_mapper_ext"
require "mongo_mapper_ext/spec"

describe "MongoMapper & CarrierWave" do
with_mongo_mapper
with_test_database
with_tmp_spec_dir

before :all do
MongoMapper.database = 'test'

class PlaneUploader < CarrierWave::Uploader::Base
storage :file

class << self
attr_accessor :store_dir
end

def store_dir
self.class.store_dir
end
end

class Plane
include MongoMapper::Document

# key :image, String
mount_uploader :image, PlaneUploader
end
end

before do
PlaneUploader.store_dir = "#{spec_dir}/data"
end

after :all do
remove_constants :Plane, :PlaneUploader
end

it "basic" do
plane = Plane.new
File.open "#{spec_dir}/plane.jpg" do |f|
plane.image = f
plane.save!
end

plane.image.current_path.should == '/tmp/carrierwave_spec/data/plane.jpg'
File.should exist("#{spec_dir}/data/plane.jpg") #.to_file.exist?.should be_true
end
end
Binary file added spec/carrierwave_spec/plane.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 6571f1a

Please sign in to comment.