carrierwave_ext 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,10 @@
1
+ require 'rake_ext'
2
+
3
+ project(
4
+ name: "carrierwave_ext",
5
+ gem: true,
6
+ summary: "Fixes and integration with MongoDB.",
7
+
8
+ author: "Alexey Petrushin",
9
+ homepage: "http://github.com/alexeypetrushin/carrierwave_ext"
10
+ )
@@ -0,0 +1,17 @@
1
+ require 'carrierwave'
2
+ require 'carrierwave/validations/active_model'
3
+ require 'carrierwave/orm/mongoid'
4
+
5
+ %w(
6
+ fixes
7
+ miscellaneous
8
+ mongoid_embedded
9
+ ).each{|f| require "carrierwave_ext/#{f}"}
10
+
11
+ CarrierWave::Uploader::Base.class_eval do
12
+ def name; model.send("#{mounted_as}_filename") end
13
+ end
14
+
15
+ Mongoid::Document.class_eval do
16
+ include CarrierWave::MongoidEmbedded
17
+ end
@@ -0,0 +1,12 @@
1
+ CarrierWave::SanitizedFile.class_eval do
2
+ def sanitize_regexp
3
+ /[^[:word:]\.\-\+\s_]/i
4
+ end
5
+ end
6
+
7
+ CarrierWave::Uploader::Cache.class_eval do
8
+ def original_filename=(filename)
9
+ raise CarrierWave::InvalidParameter, "invalid filename" unless filename =~ /\A[[:word:]\.\-\+\s_]+\z/i
10
+ @original_filename = filename
11
+ end
12
+ end
@@ -0,0 +1,26 @@
1
+ #
2
+ # Changing filename format from <version>_<filename_with_extension> to <name>.<version>.<extension>
3
+ #
4
+ CarrierWave::Uploader::Versions.class_eval do
5
+ def full_filename(for_file)
6
+ name = super
7
+ if version_name
8
+ ext = File.extname name
9
+ base = File.basename name, ext
10
+ "#{base}.#{version_name}#{ext}"
11
+ else
12
+ name
13
+ end
14
+ end
15
+
16
+ def full_original_filename
17
+ name = super
18
+ if version_name
19
+ ext = File.extname name
20
+ base = File.basename name, ext
21
+ "#{base}.#{version_name}#{ext}"
22
+ else
23
+ name
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,37 @@
1
+ #
2
+ # Because of Mongoid doesn't fires some events on embedded documents
3
+ # (it has 'smart' callback system and doesn't fires :save/:destroy if document don't really saved/destroyed)
4
+ # we need to do it manually.
5
+ #
6
+
7
+ Mongoid::Document.class_eval do
8
+ def each_embedded association_name, &b
9
+ Array(send(association_name)).each &b
10
+ end
11
+ end
12
+
13
+ module CarrierWave::MongoidEmbedded
14
+ extend ActiveSupport::Concern
15
+
16
+ module ClassMethods
17
+ def mount_embedded_uploader association_name, column
18
+ after_save do |doc|
19
+ doc.each_embedded(association_name) do |embedded|
20
+ embedded.send "store_#{column}!"
21
+ end
22
+ end
23
+
24
+ before_save do |doc|
25
+ doc.each_embedded(association_name) do |embedded|
26
+ embedded.send "write_#{column}_identifier"
27
+ end
28
+ end
29
+
30
+ after_destroy do |doc|
31
+ doc.each_embedded(association_name) do |embedded|
32
+ embedded.send "remove_#{column}!"
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,25 @@
1
+ require 'mongoid_misc/spec'
2
+
3
+ module CarrierWaveExtSpecHelper
4
+ TEST_PATH, TEST_CACHE_PATH = '/tmp/spec_fs', '/tmp/spec_fs_cache'
5
+
6
+ def with_files
7
+ before do
8
+ CarrierWave.configure do |config|
9
+ config.storage = :file
10
+ config.enable_processing = false
11
+
12
+ config.cache_dir = TEST_CACHE_PATH
13
+ config.root = TEST_PATH
14
+ end
15
+ end
16
+
17
+ before do
18
+ [TEST_PATH, TEST_CACHE_PATH].each{|p| FileUtils.rm_r(p) if File.exist?(p)}
19
+ end
20
+ before do
21
+ [TEST_PATH, TEST_CACHE_PATH].each{|p| FileUtils.rm_r(p) if File.exist?(p)}
22
+ end
23
+ end
24
+ end
25
+ rspec.extend CarrierWaveExtSpecHelper
File without changes
@@ -0,0 +1,158 @@
1
+ require 'carrierwave_ext/spec_helper'
2
+
3
+ describe "Mongoid & CarrierWave" do
4
+ with_tmp_spec_dir
5
+ before :each do
6
+ connection = Mongo::Connection.new
7
+ connection.db('test').collection('test').drop
8
+
9
+ Mongoid.configure do |config|
10
+ config.master = connection.db('test')
11
+ end
12
+ end
13
+
14
+ before :all do
15
+ class PlaneImageUploader < CarrierWave::Uploader::Base
16
+ require 'carrierwave/processing/mini_magick'
17
+
18
+ include CarrierWave::MiniMagick
19
+
20
+ storage :file
21
+
22
+ version :icon do
23
+ process convert: :png
24
+ process resize_to_fit: [50, 50]
25
+ end
26
+
27
+ def store_dir
28
+ PlaneImageUploader.store_dir
29
+ end
30
+
31
+ def root
32
+ PlaneImageUploader.store_dir
33
+ end
34
+
35
+ class << self
36
+ attr_accessor :store_dir
37
+ end
38
+ end
39
+ end
40
+ after(:all){remove_constants :PlaneImageUploader}
41
+
42
+ before do
43
+ PlaneImageUploader.store_dir = "#{spec_dir}/data"
44
+ @file = File.new "#{spec_dir}/plane.jpg"
45
+ end
46
+ after do
47
+ @file.close
48
+ @file2.close if @file2
49
+ end
50
+
51
+ it "should works without model" do
52
+ # writing
53
+ uploader = PlaneImageUploader.new
54
+ uploader.store!(@file)
55
+ uploader.identifier.should == 'plane.jpg'
56
+ uploader.url.should == '/plane.jpg'
57
+ uploader.icon.url.should == '/plane.icon.jpg'
58
+ File.should exist("#{spec_dir}/data/plane.icon.jpg")
59
+
60
+ # reading
61
+ uploader = PlaneImageUploader.new
62
+ uploader.retrieve_from_store! 'plane.jpg'
63
+ uploader.url.should == '/plane.jpg'
64
+ uploader.icon.url.should == '/plane.icon.jpg'
65
+
66
+ # destroying
67
+ uploader = PlaneImageUploader.new
68
+ uploader.retrieve_from_store! 'plane.jpg'
69
+ uploader.remove!
70
+ File.should_not exist("#{spec_dir}/data/plane.icon.jpg")
71
+ end
72
+
73
+ describe "Document" do
74
+ before :all do
75
+ class Plane
76
+ include Mongoid::Document
77
+
78
+ mount_uploader :image, PlaneImageUploader
79
+ end
80
+ end
81
+ after(:all){remove_constants :Plane}
82
+
83
+ it "basic" do
84
+ Plane.create! image: @file
85
+ Plane.first.image.current_path.should =~ /\/plane.jpg/
86
+ File.should exist("#{spec_dir}/data/plane.jpg")
87
+ end
88
+
89
+ it "path format" do
90
+ Plane.create! image: @file
91
+
92
+ plane = Plane.first
93
+ plane.image.url.should == '/plane.jpg'
94
+ plane.image.icon.url.should =~ /\/plane\.icon\.jpg/
95
+ plane.image.name.should == 'plane.jpg'
96
+
97
+ plane.image.icon.current_path.should =~ /\/plane\.icon\.jpg/
98
+ File.should exist("#{spec_dir}/data/plane.icon.jpg")
99
+ end
100
+ end
101
+
102
+ describe "EmbeddedDocument" do
103
+ before :all do
104
+ class PlaneImage
105
+ include Mongoid::Document
106
+ embedded_in :plane, class_name: 'Plane2'
107
+ mount_uploader :image, PlaneImageUploader
108
+ end
109
+
110
+ class Plane2
111
+ include Mongoid::Document
112
+
113
+ embeds_many :images, class_name: 'PlaneImage'
114
+ mount_embedded_uploader :images, :image
115
+ end
116
+ end
117
+ after(:all){remove_constants :Plane2, :PlaneImage}
118
+
119
+ it "basic" do
120
+ Plane2.create! images: [PlaneImage.new(image: @file)]
121
+ Plane2.first.images.first.image.current_path.should =~ /\/plane.jpg/
122
+ File.should exist("#{spec_dir}/data/plane.jpg")
123
+ end
124
+
125
+ it "CRUD" do
126
+ # create
127
+ Plane2.create! images: [PlaneImage.new(image: @file)]
128
+ File.should exist("#{spec_dir}/data/plane.jpg")
129
+
130
+ # update
131
+ plane = Plane2.first
132
+ @file2 = File.new "#{spec_dir}/plane2.jpg"
133
+ plane.images << PlaneImage.new(image: @file2)
134
+ plane.save!
135
+ File.should exist("#{spec_dir}/data/plane2.jpg")
136
+
137
+ # destroy embedded
138
+ plane.images.last.destroy
139
+ File.should exist("#{spec_dir}/data/plane.jpg")
140
+ File.should_not exist("#{spec_dir}/data/plane2.jpg")
141
+
142
+ # destroy parent
143
+ plane.destroy
144
+ File.should_not exist("#{spec_dir}/data/plane.jpg")
145
+ end
146
+
147
+ it "path format" do
148
+ Plane2.create! images: [PlaneImage.new(image: @file)]
149
+
150
+ plane_image = Plane2.first.images.first
151
+ plane_image.image.url.should == '/plane.jpg'
152
+ plane_image.image.icon.url.should =~ /\/plane\.icon\.jpg/
153
+
154
+ plane_image.image.icon.current_path.should =~ /\/plane\.icon\.jpg/
155
+ File.should exist("#{spec_dir}/data/plane.icon.jpg")
156
+ end
157
+ end
158
+ end
@@ -0,0 +1,7 @@
1
+ require "mongoid_misc"
2
+
3
+ require "carrierwave_ext"
4
+
5
+ require 'rspec_ext'
6
+ require 'mongoid_misc/spec'
7
+ require 'carrierwave_ext/spec'
@@ -0,0 +1,62 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+ require 'carrierwave/processing/mini_magick'
4
+
5
+ describe "Uploading" do
6
+ with_tmp_spec_dir
7
+ with_mongoid
8
+ with_files
9
+
10
+ before :all do
11
+ class ImageUploader < CarrierWave::Uploader::Base
12
+ include CarrierWave::MiniMagick
13
+
14
+ # def sanitize_regexp
15
+ # /[^[:word:]\.\-\+\s_]/i
16
+ # end
17
+
18
+ def file_path
19
+ model.id
20
+ end
21
+
22
+ def store_dir
23
+ "#{root}#{file_path}"
24
+ end
25
+
26
+ def extension_white_list
27
+ [/.*/]
28
+ end
29
+ end
30
+
31
+ class Post
32
+ include Mongoid::Document
33
+
34
+ field :name, type: String, default: ""
35
+ validates_uniqueness_of :name
36
+
37
+ mount_uploader :image, ImageUploader
38
+ end
39
+ end
40
+ after(:all){remove_constants :Post, :ImageUploader}
41
+
42
+ it "should upload images" do
43
+ post = nil
44
+ File.open "#{spec_dir}/ship.jpg" do |f|
45
+ post = Post.new image: f
46
+ post.save!
47
+ end
48
+ post.image.url.should =~ /\/ship\.jpg/
49
+ post.image_filename.should =~ /ship\.jpg/
50
+ post.image.path.should =~ /\/ship\.jpg/
51
+ end
52
+
53
+ it "should preserve spaces and unicode characters in filename" do
54
+ File.open "#{spec_dir}/файл с пробелами.txt" do |f|
55
+ post = Post.new image: f
56
+
57
+ post.image.url.should =~ /\/файл с пробелами\.txt/
58
+ post.image.filename =~ /файл с пробелами\.txt/
59
+ post.image.path =~ /\/файл с пробелами\.txt/
60
+ end
61
+ end
62
+ end
metadata ADDED
@@ -0,0 +1,58 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: carrierwave_ext
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Alexey Petrushin
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-08-23 00:00:00.000000000Z
13
+ dependencies: []
14
+ description:
15
+ email:
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - Rakefile
21
+ - readme.md
22
+ - lib/carrierwave_ext/fixes.rb
23
+ - lib/carrierwave_ext/miscellaneous.rb
24
+ - lib/carrierwave_ext/mongoid_embedded.rb
25
+ - lib/carrierwave_ext/spec.rb
26
+ - lib/carrierwave_ext.rb
27
+ - spec/mount_uploader_spec/plane.jpg
28
+ - spec/mount_uploader_spec/plane2.jpg
29
+ - spec/mount_uploader_spec.rb
30
+ - spec/spec_helper.rb
31
+ - spec/uploader_spec/ship.jpg
32
+ - spec/uploader_spec/файл с пробелами.txt
33
+ - spec/uploader_spec.rb
34
+ homepage: http://github.com/alexeypetrushin/carrierwave_ext
35
+ licenses: []
36
+ post_install_message:
37
+ rdoc_options: []
38
+ require_paths:
39
+ - lib
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ required_rubygems_version: !ruby/object:Gem::Requirement
47
+ none: false
48
+ requirements:
49
+ - - ! '>='
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ requirements: []
53
+ rubyforge_project:
54
+ rubygems_version: 1.8.6
55
+ signing_key:
56
+ specification_version: 3
57
+ summary: Fixes and integration with MongoDB.
58
+ test_files: []