Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add basic support for file attachments
- Loading branch information
Showing
5 changed files
with
132 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.