dragonfly 0.9.12 → 0.9.13

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of dragonfly might be problematic. Click here for more details.

@@ -1,21 +1,15 @@
1
1
  module Dragonfly
2
2
  class UrlMapper
3
-
3
+
4
4
  # Exceptions
5
5
  class BadUrlFormat < StandardError; end
6
-
6
+
7
7
  class Segment < Struct.new(:param, :seperator, :pattern)
8
-
9
8
  def regexp_string
10
9
  @regexp_string ||= "(#{Regexp.escape(seperator)}#{pattern}+?)?"
11
10
  end
12
- #
13
- # def regexp
14
- # @regexp ||= Regexp.new(regexp_string)
15
- # end
16
-
17
11
  end
18
-
12
+
19
13
  def initialize(url_format, patterns={})
20
14
  @url_format = url_format
21
15
  raise BadUrlFormat, "bad url format #{url_format}" if url_format[/[\w_]:[\w_]/]
@@ -24,13 +18,13 @@ module Dragonfly
24
18
  end
25
19
 
26
20
  attr_reader :url_format, :url_regexp, :segments
27
-
21
+
28
22
  def params_for(path, query=nil)
29
23
  if path and md = path.match(url_regexp)
30
24
  params = Rack::Utils.parse_query(query)
31
25
  params_in_url.each_with_index do |var, i|
32
26
  value = md[i+1][1..-1] if md[i+1]
33
- params[var] = value && URI.unescape(value)
27
+ params[var] = value && Rack::Utils.unescape(value)
34
28
  end
35
29
  params
36
30
  end
@@ -39,21 +33,21 @@ module Dragonfly
39
33
  def params_in_url
40
34
  @params_in_url ||= url_format.scan(/\:[\w_]+/).map{|f| f.tr(':','') }
41
35
  end
42
-
36
+
43
37
  def url_for(params)
44
38
  params = params.dup
45
39
  url = url_format.dup
46
40
  segments.each do |seg|
47
41
  value = params[seg.param]
48
- value ? url.sub!(/:[\w_]+/, URI.escape(value.to_s)) : url.sub!(/.:[\w_]+/, '')
42
+ value ? url.sub!(/:[\w_]+/, Rack::Utils.escape(value.to_s)) : url.sub!(/.:[\w_]+/, '')
49
43
  params.delete(seg.param)
50
44
  end
51
45
  url << "?#{Rack::Utils.build_query(params)}" if params.any?
52
46
  url
53
47
  end
54
-
48
+
55
49
  private
56
-
50
+
57
51
  def init_segments(patterns)
58
52
  @segments = []
59
53
  url_format.scan(/([^\w_]):([\w_]+)/).each do |seperator, param|
@@ -64,7 +58,7 @@ module Dragonfly
64
58
  )
65
59
  end
66
60
  end
67
-
61
+
68
62
  def init_url_regexp
69
63
  i = -1
70
64
  regexp_string = url_format.gsub(/[^\w_]:[\w_]+/) do
@@ -73,6 +67,6 @@ module Dragonfly
73
67
  end
74
68
  @url_regexp = Regexp.new('^' + regexp_string + '$')
75
69
  end
76
-
70
+
77
71
  end
78
72
  end
@@ -0,0 +1,24 @@
1
+ require 'tempfile'
2
+
3
+ module Dragonfly
4
+ module Utils
5
+
6
+ module_function
7
+
8
+ def new_tempfile(ext=nil, content=nil)
9
+ tempfile = ext ? Tempfile.new(['dragonfly', ".#{ext}"]) : Tempfile.new('dragonfly')
10
+ tempfile.binmode
11
+ tempfile.write(content) if content
12
+ tempfile.close
13
+ tempfile
14
+ end
15
+
16
+ def symbolize_keys(hash)
17
+ hash.inject({}) do |new_hash, (key, value)|
18
+ new_hash[key.to_sym] = value
19
+ new_hash
20
+ end
21
+ end
22
+
23
+ end
24
+ end
@@ -27,7 +27,7 @@ describe Item do
27
27
  @classes = Item.dragonfly_attachment_classes
28
28
  @class1, @class2 = @classes
29
29
  end
30
-
30
+
31
31
  it "should return the attachment classes" do
32
32
  @class1.superclass.should == Dragonfly::ActiveModelExtensions::Attachment
33
33
  @class2.superclass.should == Dragonfly::ActiveModelExtensions::Attachment
@@ -50,7 +50,7 @@ describe Item do
50
50
  end
51
51
 
52
52
  describe "included modules (e.g. Mongoid::Document)" do
53
-
53
+
54
54
  it "should work" do
55
55
  mongoid_document = Module.new
56
56
  app1.define_macro_on_include(mongoid_document, :dog_accessor)
@@ -239,7 +239,7 @@ describe Item do
239
239
  @app.datastore.should_receive(:destroy).with('some_uid')
240
240
  @item.save!
241
241
  end
242
-
242
+
243
243
  it "should not try to destroy the old data if saved again" do
244
244
  @app.datastore.should_receive(:destroy).with('some_uid')
245
245
  @item.save!
@@ -300,11 +300,11 @@ describe Item do
300
300
  @app.datastore.should_receive(:destroy).with('some_uid')
301
301
  @item.destroy
302
302
  end
303
-
303
+
304
304
  it 'should mark the attribute as changed' do
305
305
  @item.preview_image_uid_changed?.should be_true
306
306
  end
307
-
307
+
308
308
  end
309
309
 
310
310
  describe "destroy errors" do
@@ -568,7 +568,7 @@ describe Item do
568
568
  @item.should_not be_valid
569
569
  @item.errors[:otra_imagen].should == ["tipo de contenido incorrecto. Que chungo tio"]
570
570
  end
571
-
571
+
572
572
  it "should allow for custom messages including access to the property name and expected/allowed values" do
573
573
  @item.should_receive(:its_friday).and_return(false) # hack to get rid of other validation
574
574
  Item.class_eval do
@@ -648,14 +648,14 @@ describe Item do
648
648
  end
649
649
 
650
650
  describe "meta from magic attributes" do
651
-
651
+
652
652
  it "should store the meta for the original file name if it exists" do
653
653
  data = 'jasdlkf sadjl'
654
654
  data.stub!(:original_filename).and_return('hello.png')
655
655
  @item.preview_image = data
656
656
  @item.preview_image.meta[:name].should == 'hello.png'
657
657
  end
658
-
658
+
659
659
  it "should include magic attributes in the saved meta" do
660
660
  @item.preview_image = '123'
661
661
  @item.save!
@@ -735,7 +735,7 @@ describe Item do
735
735
  }.should raise_error(NoMethodError)
736
736
  end
737
737
  end
738
-
738
+
739
739
  describe "job shortcuts" do
740
740
  before(:each) do
741
741
  @app.job :bacon do
@@ -797,7 +797,7 @@ describe Item do
797
797
  it "should include meta info about the model" do
798
798
  @item.save!
799
799
  item = Item.find(@item.id)
800
- item.preview_image.meta.should include_hash(:model_class => 'Item', :model_attachment => :preview_image)
800
+ item.preview_image.meta.should include_hash(:model_class => 'Item', :model_attachment => :preview_image)
801
801
  end
802
802
  end
803
803
 
@@ -896,7 +896,7 @@ describe Item do
896
896
  @item = Item.new
897
897
  @item.preview_image = "something"
898
898
  end
899
-
899
+
900
900
  [
901
901
  1,
902
902
  "1",
@@ -933,24 +933,24 @@ describe Item do
933
933
  @item.remove_preview_image.should be_false
934
934
  end
935
935
  end
936
-
936
+
937
937
  it "should return false by default for the getter" do
938
938
  @item.remove_preview_image.should be_false
939
939
  end
940
-
940
+
941
941
  end
942
-
942
+
943
943
  describe "callbacks" do
944
944
 
945
945
  describe "after_assign" do
946
-
946
+
947
947
  before(:each) do
948
948
  @app = test_app
949
949
  @app.define_macro(MyModel, :image_accessor)
950
950
  end
951
951
 
952
952
  describe "as a block" do
953
-
953
+
954
954
  def set_after_assign(*args, &block)
955
955
  Item.class_eval do
956
956
  image_accessor :preview_image do
@@ -972,14 +972,14 @@ describe Item do
972
972
  Item.new.preview_image = nil
973
973
  x.should be_nil
974
974
  end
975
-
975
+
976
976
  it "should yield the attachment" do
977
977
  x = nil
978
978
  set_after_assign{|a| x = a.data }
979
979
  Item.new.preview_image = "discussion"
980
980
  x.should == "discussion"
981
981
  end
982
-
982
+
983
983
  it "should evaluate in the model context" do
984
984
  x = nil
985
985
  set_after_assign{ x = title.upcase }
@@ -988,7 +988,7 @@ describe Item do
988
988
  item.preview_image = "jobs"
989
989
  x.should == "BIG"
990
990
  end
991
-
991
+
992
992
  it "should allow passing a symbol for calling a model method" do
993
993
  set_after_assign :set_title
994
994
  item = Item.new
@@ -1005,7 +1005,7 @@ describe Item do
1005
1005
  item.preview_image = "jobs"
1006
1006
  item.title.should == "DOOBIE"
1007
1007
  end
1008
-
1008
+
1009
1009
  it "should not re-trigger callbacks (causing an infinite loop)" do
1010
1010
  set_after_assign{|a| self.preview_image = 'dogman' }
1011
1011
  item = Item.new
@@ -1013,9 +1013,9 @@ describe Item do
1013
1013
  end
1014
1014
 
1015
1015
  end
1016
-
1016
+
1017
1017
  end
1018
-
1018
+
1019
1019
  describe "after_unassign" do
1020
1020
  before(:each) do
1021
1021
  @app = test_app
@@ -1027,18 +1027,18 @@ describe Item do
1027
1027
  end
1028
1028
  @item = Item.new :title => 'yo'
1029
1029
  end
1030
-
1030
+
1031
1031
  it "should not call it after assign" do
1032
1032
  @item.preview_image = 'suggs'
1033
1033
  @item.title.should == 'yo'
1034
1034
  end
1035
-
1035
+
1036
1036
  it "should call it after unassign" do
1037
1037
  @item.preview_image = nil
1038
1038
  @item.title.should == 'unassigned'
1039
1039
  end
1040
1040
  end
1041
-
1041
+
1042
1042
  describe "copy_to" do
1043
1043
  before(:each) do
1044
1044
  @app = test_app
@@ -1056,7 +1056,7 @@ describe Item do
1056
1056
  end
1057
1057
  @item = Item.new :title => 'yo'
1058
1058
  end
1059
-
1059
+
1060
1060
  it "should copy to the other image when assigned" do
1061
1061
  @item.preview_image = 'hello bear'
1062
1062
  @item.other_image.data.should == 'hello bearyo'
@@ -1067,18 +1067,18 @@ describe Item do
1067
1067
  @item.preview_image = nil
1068
1068
  @item.other_image.should be_nil
1069
1069
  end
1070
-
1070
+
1071
1071
  it "should allow simply copying over without args" do
1072
1072
  @item.preview_image = 'hello bear'
1073
1073
  @item.yet_another_image.data.should == 'hello bear'
1074
1074
  end
1075
-
1075
+
1076
1076
  end
1077
-
1077
+
1078
1078
  end
1079
1079
 
1080
1080
  describe "storage_opts" do
1081
-
1081
+
1082
1082
  def set_storage_opts(*args, &block)
1083
1083
  Item.class_eval do
1084
1084
  image_accessor :preview_image do
@@ -1086,19 +1086,19 @@ describe Item do
1086
1086
  end
1087
1087
  end
1088
1088
  end
1089
-
1089
+
1090
1090
  before(:each) do
1091
1091
  @app = test_app
1092
1092
  @app.define_macro(MyModel, :image_accessor)
1093
1093
  end
1094
-
1094
+
1095
1095
  it "should send the specified options to the datastore on store" do
1096
1096
  set_storage_opts :egg => 'head'
1097
1097
  item = Item.new :preview_image => 'hello'
1098
1098
  @app.datastore.should_receive(:store).with(anything, hash_including(:egg => 'head'))
1099
1099
  item.save!
1100
1100
  end
1101
-
1101
+
1102
1102
  it "should allow putting in a proc" do
1103
1103
  set_storage_opts{ {:egg => 'numb'} }
1104
1104
  item = Item.new :preview_image => 'hello'
@@ -1120,7 +1120,7 @@ describe Item do
1120
1120
  @app.datastore.should_receive(:store).with(anything, hash_including(:a => 1))
1121
1121
  item.save!
1122
1122
  end
1123
-
1123
+
1124
1124
  it "should allow setting more than once" do
1125
1125
  Item.class_eval do
1126
1126
  image_accessor :preview_image do
@@ -1137,7 +1137,7 @@ describe Item do
1137
1137
  end
1138
1138
 
1139
1139
  describe "storage_path, etc." do
1140
-
1140
+
1141
1141
  def set_storage_path(path=nil, &block)
1142
1142
  Item.class_eval do
1143
1143
  image_accessor :preview_image do
@@ -1172,7 +1172,7 @@ describe Item do
1172
1172
  ))
1173
1173
  item.save!
1174
1174
  end
1175
-
1175
+
1176
1176
  it "should allow setting as a block" do
1177
1177
  set_storage_path{|a| "#{a.data}/megs/#{title}" }
1178
1178
  item = Item.new :title => 'billy'
@@ -1196,7 +1196,7 @@ describe Item do
1196
1196
  item.save!
1197
1197
  end
1198
1198
  end
1199
-
1199
+
1200
1200
  describe "unknown config method" do
1201
1201
  it "should raise an error" do
1202
1202
  lambda{
@@ -1208,18 +1208,18 @@ describe Item do
1208
1208
  }.should raise_error(NoMethodError)
1209
1209
  end
1210
1210
  end
1211
-
1211
+
1212
1212
  describe "changed?" do
1213
1213
  before(:each) do
1214
1214
  set_up_item_class
1215
1215
  @item = Item.new
1216
1216
  end
1217
-
1217
+
1218
1218
  it "should be changed when assigned" do
1219
1219
  @item.preview_image = 'ggg'
1220
1220
  @item.preview_image.should be_changed
1221
1221
  end
1222
-
1222
+
1223
1223
  it "should not be changed when saved" do
1224
1224
  @item.preview_image = 'ggg'
1225
1225
  @item.save!
@@ -1233,7 +1233,7 @@ describe Item do
1233
1233
  item.preview_image.should_not be_changed
1234
1234
  end
1235
1235
  end
1236
-
1236
+
1237
1237
  describe "retain and pending" do
1238
1238
  before(:each) do
1239
1239
  set_up_item_class(@app=test_app)
@@ -1251,13 +1251,13 @@ describe Item do
1251
1251
  @item.preview_image = 'hello'
1252
1252
  @item.retained_preview_image.should be_nil
1253
1253
  end
1254
-
1254
+
1255
1255
  it "should return nil if assigned and saved" do
1256
1256
  @item.preview_image = 'hello'
1257
1257
  @item.save!
1258
1258
  @item.retained_preview_image.should be_nil
1259
1259
  end
1260
-
1260
+
1261
1261
  it "should return the saved stuff if assigned and retained" do
1262
1262
  @item.preview_image = 'hello'
1263
1263
  @item.preview_image.name = 'dog.biscuit'
@@ -1272,14 +1272,14 @@ describe Item do
1272
1272
  }
1273
1273
  end.and_return('new/uid')
1274
1274
  @item.preview_image.retain!
1275
- Dragonfly::Serializer.marshal_decode(@item.retained_preview_image).should == {
1275
+ Dragonfly::Serializer.json_decode(@item.retained_preview_image, :symbolize_keys => true).should == {
1276
1276
  :uid => 'new/uid',
1277
1277
  :some_analyser_method => 'HELLO',
1278
1278
  :size => 5,
1279
1279
  :name => 'dog.biscuit'
1280
1280
  }
1281
1281
  end
1282
-
1282
+
1283
1283
  it "should return nil if assigned, retained and saved" do
1284
1284
  @item.preview_image = 'hello'
1285
1285
  @item.preview_image.retain!
@@ -1302,14 +1302,14 @@ describe Item do
1302
1302
  item.retained_preview_image.should be_nil
1303
1303
  end
1304
1304
  end
1305
-
1305
+
1306
1306
  describe "assigning from a pending state" do
1307
1307
  before(:each) do
1308
1308
  set_up_item_class(@app=test_app)
1309
1309
  @app.analyser.add :some_analyser_method do |temp_object|
1310
1310
  temp_object.data.upcase
1311
1311
  end
1312
- @pending_string = Dragonfly::Serializer.marshal_encode(
1312
+ @pending_string = Dragonfly::Serializer.json_encode(
1313
1313
  :uid => 'new/uid',
1314
1314
  :some_analyser_method => 'HELLO',
1315
1315
  :size => 5,
@@ -1322,7 +1322,7 @@ describe Item do
1322
1322
  @item.dragonfly_attachments[:preview_image].should_receive(:retain!)
1323
1323
  @item.retained_preview_image = @pending_string
1324
1324
  end
1325
-
1325
+
1326
1326
  it "should update the attributes" do
1327
1327
  @item.retained_preview_image = @pending_string
1328
1328
  @item.preview_image_uid.should == 'new/uid'
@@ -1330,7 +1330,7 @@ describe Item do
1330
1330
  @item.preview_image_size.should == 5
1331
1331
  @item.preview_image_name.should == 'dog.biscuit'
1332
1332
  end
1333
-
1333
+
1334
1334
  it "should be a normal fetch job" do
1335
1335
  @item.retained_preview_image = @pending_string
1336
1336
  @app.datastore.should_receive(:retrieve).with('new/uid').and_return(Dragonfly::TempObject.new('retrieved yo'))
@@ -1341,9 +1341,9 @@ describe Item do
1341
1341
  @item.retained_preview_image = @pending_string
1342
1342
  @item.preview_image.url.should =~ %r{^/\w+/dog.biscuit$}
1343
1343
  end
1344
-
1344
+
1345
1345
  it "should raise an error if the pending string contains a non-magic attr method" do
1346
- pending_string = Dragonfly::Serializer.marshal_encode(
1346
+ pending_string = Dragonfly::Serializer.json_encode(
1347
1347
  :uid => 'new/uid',
1348
1348
  :some_analyser_method => 'HELLO',
1349
1349
  :size => 5,
@@ -1355,20 +1355,20 @@ describe Item do
1355
1355
  item.retained_preview_image = pending_string
1356
1356
  }.should raise_error(Dragonfly::ActiveModelExtensions::Attachment::BadAssignmentKey)
1357
1357
  end
1358
-
1358
+
1359
1359
  [nil, "", "asdfsad"].each do |value|
1360
1360
  it "should do nothing if assigned with #{value}" do
1361
1361
  @item.retained_preview_image = value
1362
1362
  @item.preview_image_uid.should be_nil
1363
1363
  end
1364
1364
  end
1365
-
1365
+
1366
1366
  it "should return the pending string again" do
1367
1367
  @item.retained_preview_image = @pending_string
1368
- Dragonfly::Serializer.marshal_decode(@item.retained_preview_image).should ==
1369
- Dragonfly::Serializer.marshal_decode(@pending_string)
1368
+ Dragonfly::Serializer.json_decode(@item.retained_preview_image).should ==
1369
+ Dragonfly::Serializer.json_decode(@pending_string)
1370
1370
  end
1371
-
1371
+
1372
1372
  it "should destroy the old one on save" do
1373
1373
  @item.preview_image = 'oldone'
1374
1374
  @app.datastore.should_receive(:store).with(a_temp_object_with_data('oldone'), anything).and_return('old/uid')
@@ -1403,7 +1403,7 @@ describe Item do
1403
1403
  @app.datastore.should_receive(:destroy).with('new/uid')
1404
1404
  @item.retained_preview_image = @pending_string
1405
1405
  end
1406
-
1406
+
1407
1407
  describe "automatically retaining (hack to test for existence of hidden form field)" do
1408
1408
  it "should automatically retain if set as an empty string then changed" do
1409
1409
  @item.retained_preview_image = ""
@@ -1432,7 +1432,7 @@ describe Item do
1432
1432
  end
1433
1433
 
1434
1434
  end
1435
-
1435
+
1436
1436
  describe "format and mime type" do
1437
1437
  before(:each) do
1438
1438
  @app = test_app
@@ -1463,7 +1463,7 @@ describe Item do
1463
1463
  @item.preview_image.mime_type.should == 'some/type'
1464
1464
  end
1465
1465
  end
1466
-
1466
+
1467
1467
  describe "inspect" do
1468
1468
  before(:each) do
1469
1469
  set_up_item_class
@@ -1474,5 +1474,5 @@ describe Item do
1474
1474
  @item.preview_image.inspect.should =~ %r{^<Dragonfly Attachment uid="[^"]+", app=:test[_\w]*>$}
1475
1475
  end
1476
1476
  end
1477
-
1477
+
1478
1478
  end