roachclip 0.3.0 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/roachclip.rb +16 -6
- data/roachclip.gemspec +1 -1
- data/test/unit/test_roachclip.rb +51 -0
- metadata +2 -2
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.3.
|
1
|
+
0.3.1
|
data/lib/roachclip.rb
CHANGED
@@ -31,8 +31,10 @@ module Roachclip
|
|
31
31
|
path = options.delete(:path) || "/gridfs/fs/%s-%s"
|
32
32
|
self.roaches << {:name => name, :options => options}
|
33
33
|
|
34
|
+
options[:default_style] ||= :original
|
35
|
+
|
34
36
|
options[:styles] ||= {}
|
35
|
-
options[:styles].each { |k,v| self.attachment "#{name}_#{k}"}
|
37
|
+
options[:styles].each { |k,v| self.attachment "#{name}_#{k}" unless k == options[:default_style] }
|
36
38
|
|
37
39
|
before_save :process_roaches
|
38
40
|
before_save :destroy_nil_roaches
|
@@ -62,7 +64,8 @@ module Roachclip
|
|
62
64
|
roaches.each do |img|
|
63
65
|
name = img[:name]
|
64
66
|
styles = img[:options][:styles]
|
65
|
-
|
67
|
+
default_style = img[:options][:default_style]
|
68
|
+
|
66
69
|
return unless assigned_attachments[name]
|
67
70
|
|
68
71
|
src = Tempfile.new ["roachclip", name.to_s].join('-')
|
@@ -75,8 +78,14 @@ module Roachclip
|
|
75
78
|
thumbnail = Paperclip::Thumbnail.new src, styles[style_key]
|
76
79
|
tmp_file_name = thumbnail.make
|
77
80
|
stored_file_name = send("#{name}_name").gsub(/\.(\w*)\Z/) { "_#{style_key}.#{$1}" }
|
78
|
-
|
79
|
-
|
81
|
+
|
82
|
+
if style_key == default_style
|
83
|
+
send "#{name}=", tmp_file_name
|
84
|
+
send "#{name}_name=", stored_file_name
|
85
|
+
else
|
86
|
+
send "#{name}_#{style_key}=", tmp_file_name
|
87
|
+
send "#{name}_#{style_key}_name=", stored_file_name
|
88
|
+
end
|
80
89
|
end
|
81
90
|
end
|
82
91
|
end
|
@@ -85,11 +94,12 @@ module Roachclip
|
|
85
94
|
roaches.each do |img|
|
86
95
|
name = img[:name]
|
87
96
|
styles = img[:options][:styles]
|
88
|
-
|
97
|
+
default_style = img[:options][:default_style]
|
98
|
+
|
89
99
|
return unless @nil_attachments && @nil_attachments.include?(name)
|
90
100
|
|
91
101
|
styles.keys.each do |style_key|
|
92
|
-
send "#{name}_#{style_key}=", nil
|
102
|
+
send "#{name}_#{style_key}=", nil unless style_key == default_style
|
93
103
|
end
|
94
104
|
end
|
95
105
|
end
|
data/roachclip.gemspec
CHANGED
data/test/unit/test_roachclip.rb
CHANGED
@@ -121,6 +121,57 @@ class RoachclipTest < Test::Unit::TestCase
|
|
121
121
|
end
|
122
122
|
end
|
123
123
|
end
|
124
|
+
|
125
|
+
context "with a default style" do
|
126
|
+
setup do
|
127
|
+
Doc.roachclip :image, { :styles => { :default => { :geometry => '100x100' }, :thumb => { :geometry => '50x50' } }, :default_style => :default }
|
128
|
+
@doc = Doc.new
|
129
|
+
end
|
130
|
+
|
131
|
+
should "not add an attachment for the default style" do
|
132
|
+
assert !@doc.respond_to?(:image_default)
|
133
|
+
assert !@doc.respond_to?(:image_default=)
|
134
|
+
end
|
135
|
+
|
136
|
+
should "add attachment for thumb style" do
|
137
|
+
assert @doc.respond_to?(:image_thumb)
|
138
|
+
assert @doc.respond_to?(:image_thumb=)
|
139
|
+
end
|
140
|
+
|
141
|
+
should "process all styles" do
|
142
|
+
Paperclip::Thumbnail.any_instance.expects(:make).twice
|
143
|
+
@doc.image = File.open(test_file_path)
|
144
|
+
@doc.save!
|
145
|
+
end
|
146
|
+
|
147
|
+
should "not destroy the default style roach" do
|
148
|
+
@doc.image = File.open(test_file_path)
|
149
|
+
@doc.save!
|
150
|
+
|
151
|
+
@doc.expects(:image_thumb=).with(nil).at_least_once
|
152
|
+
@doc.expects(:image_default=).never
|
153
|
+
@doc.image = nil
|
154
|
+
@doc.save!
|
155
|
+
end
|
156
|
+
end
|
157
|
+
|
158
|
+
context "with the :original style" do
|
159
|
+
setup do
|
160
|
+
Doc.roachclip :image, { :styles => { :original => { :geometry => '100x100' } } }
|
161
|
+
@doc = Doc.new
|
162
|
+
end
|
163
|
+
|
164
|
+
should "behave as the default style" do
|
165
|
+
assert !@doc.respond_to?(:image_original)
|
166
|
+
assert !@doc.respond_to?(:image_original=)
|
167
|
+
end
|
168
|
+
|
169
|
+
should "process the original image" do
|
170
|
+
Paperclip::Thumbnail.any_instance.expects(:make).once
|
171
|
+
@doc.image = File.open(test_file_path)
|
172
|
+
@doc.save!
|
173
|
+
end
|
174
|
+
end
|
124
175
|
end
|
125
176
|
|
126
177
|
def fname
|