file_upload 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (145) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/Rakefile +34 -0
  4. data/app/assets/javascripts/file_upload/application.js +13 -0
  5. data/app/assets/javascripts/file_upload.js.erb +148 -0
  6. data/app/assets/stylesheets/file_upload/application.css +13 -0
  7. data/app/assets/stylesheets/file_upload.css +3 -0
  8. data/app/controllers/file_upload/application_controller.rb +4 -0
  9. data/app/controllers/file_upload/redis_files_controller.rb +54 -0
  10. data/app/helpers/file_upload/application_helper.rb +4 -0
  11. data/app/helpers/file_upload/tag_helper.rb +25 -0
  12. data/app/models/file_upload/builder_mixin.rb +13 -0
  13. data/app/models/file_upload/redis_file.rb +159 -0
  14. data/app/models/file_upload/redis_file_readable.rb +21 -0
  15. data/app/models/file_upload/temp_file.rb +17 -0
  16. data/app/views/file_upload/redis_files/index.html.erb +1 -0
  17. data/app/views/file_upload/redis_files/new.html.erb +9 -0
  18. data/app/views/file_upload/redis_files/show.html.erb +15 -0
  19. data/app/views/file_upload/tags/_multiple.html.erb +44 -0
  20. data/app/views/file_upload/tags/_single.html.erb +46 -0
  21. data/app/views/layouts/file_upload/application.html.erb +14 -0
  22. data/config/routes.rb +6 -0
  23. data/lib/file_upload/engine.rb +40 -0
  24. data/lib/file_upload/version.rb +3 -0
  25. data/lib/file_upload.rb +7 -0
  26. data/lib/tasks/file_upload_tasks.rake +4 -0
  27. data/test/dummy/README.rdoc +28 -0
  28. data/test/dummy/Rakefile +6 -0
  29. data/test/dummy/app/assets/javascripts/application.js +15 -0
  30. data/test/dummy/app/assets/javascripts/jquery-1.10.2.min.js +6 -0
  31. data/test/dummy/app/assets/javascripts/jquery-ui.js +15003 -0
  32. data/test/dummy/app/assets/stylesheets/application.css +13 -0
  33. data/test/dummy/app/controllers/application_controller.rb +5 -0
  34. data/test/dummy/app/controllers/db_files_controller.rb +8 -0
  35. data/test/dummy/app/controllers/emails_controller.rb +43 -0
  36. data/test/dummy/app/controllers/users_controller.rb +42 -0
  37. data/test/dummy/app/helpers/application_helper.rb +2 -0
  38. data/test/dummy/app/models/db_file.rb +13 -0
  39. data/test/dummy/app/models/email.rb +10 -0
  40. data/test/dummy/app/models/user.rb +9 -0
  41. data/test/dummy/app/views/db_files/_db_file.html.erb +1 -0
  42. data/test/dummy/app/views/emails/_email.html.erb +3 -0
  43. data/test/dummy/app/views/emails/_form.html.erb +24 -0
  44. data/test/dummy/app/views/emails/edit.html.erb +3 -0
  45. data/test/dummy/app/views/emails/index.html.erb +5 -0
  46. data/test/dummy/app/views/emails/new.html.erb +3 -0
  47. data/test/dummy/app/views/emails/show.html.erb +14 -0
  48. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  49. data/test/dummy/app/views/users/_form.html.erb +20 -0
  50. data/test/dummy/app/views/users/_user.html.erb +1 -0
  51. data/test/dummy/app/views/users/edit.html.erb +3 -0
  52. data/test/dummy/app/views/users/index.html.erb +5 -0
  53. data/test/dummy/app/views/users/new.html.erb +3 -0
  54. data/test/dummy/app/views/users/show.html.erb +8 -0
  55. data/test/dummy/bin/bundle +3 -0
  56. data/test/dummy/bin/rails +4 -0
  57. data/test/dummy/bin/rake +4 -0
  58. data/test/dummy/config/application.rb +28 -0
  59. data/test/dummy/config/boot.rb +5 -0
  60. data/test/dummy/config/database.yml +31 -0
  61. data/test/dummy/config/environment.rb +5 -0
  62. data/test/dummy/config/environments/development.rb +29 -0
  63. data/test/dummy/config/environments/production.rb +80 -0
  64. data/test/dummy/config/environments/test.rb +36 -0
  65. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  66. data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  67. data/test/dummy/config/initializers/inflections.rb +16 -0
  68. data/test/dummy/config/initializers/mime_types.rb +5 -0
  69. data/test/dummy/config/initializers/secret_token.rb +12 -0
  70. data/test/dummy/config/initializers/session_store.rb +3 -0
  71. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  72. data/test/dummy/config/locales/en.yml +23 -0
  73. data/test/dummy/config/routes.rb +7 -0
  74. data/test/dummy/config.ru +4 -0
  75. data/test/dummy/db/development.sqlite3 +0 -0
  76. data/test/dummy/db/migrate/20130820025256_add_users.rb +21 -0
  77. data/test/dummy/db/schema.rb +34 -0
  78. data/test/dummy/log/development.log +15541 -0
  79. data/test/dummy/log/test.log +26004 -0
  80. data/test/dummy/public/404.html +58 -0
  81. data/test/dummy/public/422.html +58 -0
  82. data/test/dummy/public/500.html +57 -0
  83. data/test/dummy/public/favicon.ico +0 -0
  84. data/test/dummy/tmp/cache/assets/development/sprockets/1027254ad9f9317017cee3f9f844a862 +0 -0
  85. data/test/dummy/tmp/cache/assets/development/sprockets/13fe41fee1fe35b49d145bcc06610705 +0 -0
  86. data/test/dummy/tmp/cache/assets/development/sprockets/21e0ff88cf13ca60a86a9b629cdca847 +0 -0
  87. data/test/dummy/tmp/cache/assets/development/sprockets/24cf35cebd10e0755a5585f9e486aa35 +0 -0
  88. data/test/dummy/tmp/cache/assets/development/sprockets/27413a86db5db5977d5dd1d87c724907 +0 -0
  89. data/test/dummy/tmp/cache/assets/development/sprockets/2f5173deea6c795b8fdde723bb4b63af +0 -0
  90. data/test/dummy/tmp/cache/assets/development/sprockets/2f5c6d9c2c3107f94ad59710d3b31f75 +0 -0
  91. data/test/dummy/tmp/cache/assets/development/sprockets/3131541e8089635bc8595e2da83d1d81 +0 -0
  92. data/test/dummy/tmp/cache/assets/development/sprockets/32f1d616324cf52e9876bb83b9b0841a +0 -0
  93. data/test/dummy/tmp/cache/assets/development/sprockets/33474263b3841cd98ef63cde45dc85b2 +0 -0
  94. data/test/dummy/tmp/cache/assets/development/sprockets/357970feca3ac29060c1e3861e2c0953 +0 -0
  95. data/test/dummy/tmp/cache/assets/development/sprockets/3fa0d13a4820d4aa56f86ed4af4b88fc +0 -0
  96. data/test/dummy/tmp/cache/assets/development/sprockets/4e07f6f4475a0dca9066eec990db088a +0 -0
  97. data/test/dummy/tmp/cache/assets/development/sprockets/5512d0b5788ce193fd26bfadccd21304 +0 -0
  98. data/test/dummy/tmp/cache/assets/development/sprockets/5d5ab91fa7d1eee7502e703747d4e933 +0 -0
  99. data/test/dummy/tmp/cache/assets/development/sprockets/6b7e97595c37161f619adfd2b7724ea2 +0 -0
  100. data/test/dummy/tmp/cache/assets/development/sprockets/87fa3cc4e2e49762d146f3d1709b3fb0 +0 -0
  101. data/test/dummy/tmp/cache/assets/development/sprockets/8806d06f37e08b22f9546e50ab98d671 +0 -0
  102. data/test/dummy/tmp/cache/assets/development/sprockets/8cdad172c92eaf76e1b71c84f58a192e +0 -0
  103. data/test/dummy/tmp/cache/assets/development/sprockets/91e53bdf30a2b8e78bf99ca794addf70 +0 -0
  104. data/test/dummy/tmp/cache/assets/development/sprockets/96e2b2964d1d31d997a1af35ad0afed2 +0 -0
  105. data/test/dummy/tmp/cache/assets/development/sprockets/ab5c07d7e81fb5d5cf7460715f22bef6 +0 -0
  106. data/test/dummy/tmp/cache/assets/development/sprockets/b94837629e2fb2ef40ea66348764a9a2 +0 -0
  107. data/test/dummy/tmp/cache/assets/development/sprockets/c360bf7b66d29ebdf71b5814574b5fed +0 -0
  108. data/test/dummy/tmp/cache/assets/development/sprockets/cffd775d018f68ce5dba1ee0d951a994 +0 -0
  109. data/test/dummy/tmp/cache/assets/development/sprockets/d771ace226fc8215a3572e0aa35bb0d6 +0 -0
  110. data/test/dummy/tmp/cache/assets/development/sprockets/e046b0166566aeeb2da51f2bde543364 +0 -0
  111. data/test/dummy/tmp/cache/assets/development/sprockets/ec2d15616d5d09eb1693b761c72948ce +0 -0
  112. data/test/dummy/tmp/cache/assets/development/sprockets/f7cbd26ba1d28d48de824f0e94586655 +0 -0
  113. data/test/dummy/tmp/cache/assets/development/sprockets/fc0327db3eb7c687c31f2acf94871f86 +0 -0
  114. data/test/dummy/tmp/cache/assets/test/sprockets/1027254ad9f9317017cee3f9f844a862 +0 -0
  115. data/test/dummy/tmp/cache/assets/test/sprockets/13fe41fee1fe35b49d145bcc06610705 +0 -0
  116. data/test/dummy/tmp/cache/assets/test/sprockets/21e0ff88cf13ca60a86a9b629cdca847 +0 -0
  117. data/test/dummy/tmp/cache/assets/test/sprockets/24cf35cebd10e0755a5585f9e486aa35 +0 -0
  118. data/test/dummy/tmp/cache/assets/test/sprockets/27413a86db5db5977d5dd1d87c724907 +0 -0
  119. data/test/dummy/tmp/cache/assets/test/sprockets/2f5173deea6c795b8fdde723bb4b63af +0 -0
  120. data/test/dummy/tmp/cache/assets/test/sprockets/32f1d616324cf52e9876bb83b9b0841a +0 -0
  121. data/test/dummy/tmp/cache/assets/test/sprockets/357970feca3ac29060c1e3861e2c0953 +0 -0
  122. data/test/dummy/tmp/cache/assets/test/sprockets/3fa0d13a4820d4aa56f86ed4af4b88fc +0 -0
  123. data/test/dummy/tmp/cache/assets/test/sprockets/5d5ab91fa7d1eee7502e703747d4e933 +0 -0
  124. data/test/dummy/tmp/cache/assets/test/sprockets/6b7e97595c37161f619adfd2b7724ea2 +0 -0
  125. data/test/dummy/tmp/cache/assets/test/sprockets/87fa3cc4e2e49762d146f3d1709b3fb0 +0 -0
  126. data/test/dummy/tmp/cache/assets/test/sprockets/8806d06f37e08b22f9546e50ab98d671 +0 -0
  127. data/test/dummy/tmp/cache/assets/test/sprockets/ab5c07d7e81fb5d5cf7460715f22bef6 +0 -0
  128. data/test/dummy/tmp/cache/assets/test/sprockets/b94837629e2fb2ef40ea66348764a9a2 +0 -0
  129. data/test/dummy/tmp/cache/assets/test/sprockets/c360bf7b66d29ebdf71b5814574b5fed +0 -0
  130. data/test/dummy/tmp/cache/assets/test/sprockets/cffd775d018f68ce5dba1ee0d951a994 +0 -0
  131. data/test/dummy/tmp/cache/assets/test/sprockets/d771ace226fc8215a3572e0aa35bb0d6 +0 -0
  132. data/test/dummy/tmp/cache/assets/test/sprockets/e046b0166566aeeb2da51f2bde543364 +0 -0
  133. data/test/dummy/tmp/cache/assets/test/sprockets/ec2d15616d5d09eb1693b761c72948ce +0 -0
  134. data/test/dummy/tmp/cache/assets/test/sprockets/f7cbd26ba1d28d48de824f0e94586655 +0 -0
  135. data/test/dummy/tmp/cache/assets/test/sprockets/fc0327db3eb7c687c31f2acf94871f86 +0 -0
  136. data/test/fixtures/avatar.jpeg +0 -0
  137. data/test/fixtures/test_upload.txt +1 -0
  138. data/test/integration/multiple_upload_test.rb +131 -0
  139. data/test/integration/single_upload_test.rb +157 -0
  140. data/test/test_helper.rb +60 -0
  141. data/test/unit/redis_file_readable_test.rb +30 -0
  142. data/test/unit/redis_file_test.rb +30 -0
  143. data/vendor/assets/javascripts/jquery.fileupload.js +1113 -0
  144. data/vendor/assets/javascripts/load-image.js +232 -0
  145. metadata +401 -0
@@ -0,0 +1,58 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>The page you were looking for doesn't exist (404)</title>
5
+ <style>
6
+ body {
7
+ background-color: #EFEFEF;
8
+ color: #2E2F30;
9
+ text-align: center;
10
+ font-family: arial, sans-serif;
11
+ }
12
+
13
+ div.dialog {
14
+ width: 25em;
15
+ margin: 4em auto 0 auto;
16
+ border: 1px solid #CCC;
17
+ border-right-color: #999;
18
+ border-left-color: #999;
19
+ border-bottom-color: #BBB;
20
+ border-top: #B00100 solid 4px;
21
+ border-top-left-radius: 9px;
22
+ border-top-right-radius: 9px;
23
+ background-color: white;
24
+ padding: 7px 4em 0 4em;
25
+ }
26
+
27
+ h1 {
28
+ font-size: 100%;
29
+ color: #730E15;
30
+ line-height: 1.5em;
31
+ }
32
+
33
+ body > p {
34
+ width: 33em;
35
+ margin: 0 auto 1em;
36
+ padding: 1em 0;
37
+ background-color: #F7F7F7;
38
+ border: 1px solid #CCC;
39
+ border-right-color: #999;
40
+ border-bottom-color: #999;
41
+ border-bottom-left-radius: 4px;
42
+ border-bottom-right-radius: 4px;
43
+ border-top-color: #DADADA;
44
+ color: #666;
45
+ box-shadow:0 3px 8px rgba(50, 50, 50, 0.17);
46
+ }
47
+ </style>
48
+ </head>
49
+
50
+ <body>
51
+ <!-- This file lives in public/404.html -->
52
+ <div class="dialog">
53
+ <h1>The page you were looking for doesn't exist.</h1>
54
+ <p>You may have mistyped the address or the page may have moved.</p>
55
+ </div>
56
+ <p>If you are the application owner check the logs for more information.</p>
57
+ </body>
58
+ </html>
@@ -0,0 +1,58 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>The change you wanted was rejected (422)</title>
5
+ <style>
6
+ body {
7
+ background-color: #EFEFEF;
8
+ color: #2E2F30;
9
+ text-align: center;
10
+ font-family: arial, sans-serif;
11
+ }
12
+
13
+ div.dialog {
14
+ width: 25em;
15
+ margin: 4em auto 0 auto;
16
+ border: 1px solid #CCC;
17
+ border-right-color: #999;
18
+ border-left-color: #999;
19
+ border-bottom-color: #BBB;
20
+ border-top: #B00100 solid 4px;
21
+ border-top-left-radius: 9px;
22
+ border-top-right-radius: 9px;
23
+ background-color: white;
24
+ padding: 7px 4em 0 4em;
25
+ }
26
+
27
+ h1 {
28
+ font-size: 100%;
29
+ color: #730E15;
30
+ line-height: 1.5em;
31
+ }
32
+
33
+ body > p {
34
+ width: 33em;
35
+ margin: 0 auto 1em;
36
+ padding: 1em 0;
37
+ background-color: #F7F7F7;
38
+ border: 1px solid #CCC;
39
+ border-right-color: #999;
40
+ border-bottom-color: #999;
41
+ border-bottom-left-radius: 4px;
42
+ border-bottom-right-radius: 4px;
43
+ border-top-color: #DADADA;
44
+ color: #666;
45
+ box-shadow:0 3px 8px rgba(50, 50, 50, 0.17);
46
+ }
47
+ </style>
48
+ </head>
49
+
50
+ <body>
51
+ <!-- This file lives in public/422.html -->
52
+ <div class="dialog">
53
+ <h1>The change you wanted was rejected.</h1>
54
+ <p>Maybe you tried to change something you didn't have access to.</p>
55
+ </div>
56
+ <p>If you are the application owner check the logs for more information.</p>
57
+ </body>
58
+ </html>
@@ -0,0 +1,57 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>We're sorry, but something went wrong (500)</title>
5
+ <style>
6
+ body {
7
+ background-color: #EFEFEF;
8
+ color: #2E2F30;
9
+ text-align: center;
10
+ font-family: arial, sans-serif;
11
+ }
12
+
13
+ div.dialog {
14
+ width: 25em;
15
+ margin: 4em auto 0 auto;
16
+ border: 1px solid #CCC;
17
+ border-right-color: #999;
18
+ border-left-color: #999;
19
+ border-bottom-color: #BBB;
20
+ border-top: #B00100 solid 4px;
21
+ border-top-left-radius: 9px;
22
+ border-top-right-radius: 9px;
23
+ background-color: white;
24
+ padding: 7px 4em 0 4em;
25
+ }
26
+
27
+ h1 {
28
+ font-size: 100%;
29
+ color: #730E15;
30
+ line-height: 1.5em;
31
+ }
32
+
33
+ body > p {
34
+ width: 33em;
35
+ margin: 0 auto 1em;
36
+ padding: 1em 0;
37
+ background-color: #F7F7F7;
38
+ border: 1px solid #CCC;
39
+ border-right-color: #999;
40
+ border-bottom-color: #999;
41
+ border-bottom-left-radius: 4px;
42
+ border-bottom-right-radius: 4px;
43
+ border-top-color: #DADADA;
44
+ color: #666;
45
+ box-shadow:0 3px 8px rgba(50, 50, 50, 0.17);
46
+ }
47
+ </style>
48
+ </head>
49
+
50
+ <body>
51
+ <!-- This file lives in public/500.html -->
52
+ <div class="dialog">
53
+ <h1>We're sorry, but something went wrong.</h1>
54
+ </div>
55
+ <p>If you are the application owner check the logs for more information.</p>
56
+ </body>
57
+ </html>
File without changes
Binary file
@@ -0,0 +1 @@
1
+ Some upload text!!
@@ -0,0 +1,131 @@
1
+ require 'test_helper'
2
+
3
+ class MultipleUploadTest < CapybaraTest
4
+
5
+ teardown do
6
+ Email.destroy_all
7
+ DbFile.destroy_all
8
+ end
9
+
10
+ test "can attach attachments on create" do
11
+ email_count = Email.count
12
+ file_count = DbFile.count
13
+
14
+ visit new_email_path
15
+ assert has_selector?("h1", text: "New Email")
16
+
17
+ attach_file "email_attachments_file", File.expand_path("../../fixtures/avatar.jpeg", __FILE__)
18
+ assert has_selector?(".file_upload ul li input[type=checkbox]", count: 1)
19
+ attach_file "email_attachments_file", File.expand_path("../../fixtures/avatar.jpeg", __FILE__)
20
+ assert has_selector?(".file_upload ul li input[type=checkbox]", count: 2)
21
+
22
+ click_on "Create Email"
23
+
24
+ # should have validation error on subject/message
25
+ assert has_selector?(".error li")
26
+
27
+ fill_in "Subject", with: "Hi there"
28
+ fill_in "Message", with: "What are you up to?"
29
+
30
+ click_on "Create Email"
31
+ assert has_selector?("p", text: "Hi there")
32
+
33
+ assert_equal(email_count + 1, Email.count)
34
+ assert_equal(file_count + 2, DbFile.count)
35
+
36
+ email = Email.last
37
+ assert_equal(2, email.attachments.count)
38
+ end
39
+
40
+ test "can attach attachments, then change on create" do
41
+ email_count = Email.count
42
+ file_count = DbFile.count
43
+
44
+ visit new_email_path
45
+ assert has_selector?("h1", text: "New Email")
46
+
47
+ fill_in "Subject", with: "Hi there"
48
+ fill_in "Message", with: "What are you up to?"
49
+ attach_file "email_attachments_file", File.expand_path("../../fixtures/avatar.jpeg", __FILE__)
50
+ assert has_selector?(".file_upload ul li input[type=checkbox]", count: 1)
51
+
52
+ attach_file "email_attachments_file", File.expand_path("../../fixtures/test_upload.txt", __FILE__)
53
+ assert has_selector?(".file_upload ul li input[type=checkbox]", count: 2)
54
+ uncheck 'avatar.jpeg'
55
+
56
+ click_on "Create Email"
57
+ assert has_selector?("p", text: "Hi there")
58
+
59
+ assert_equal(email_count + 1, Email.count)
60
+ assert_equal(file_count + 1, DbFile.count)
61
+
62
+ email = Email.last
63
+ assert_equal(1, email.attachments.count)
64
+ file = email.attachments.first
65
+ assert_equal("test_upload.txt", file.name)
66
+ end
67
+
68
+ test "can remove attachments" do
69
+ email = Email.create({
70
+ subject: "Fixture email subject",
71
+ message: "Fixture email message",
72
+ attachments: [
73
+ DbFile.new({
74
+ name: "pic.jpg",
75
+ size: 112,
76
+ content_type: "image/jpeg",
77
+ data: "blah"
78
+ })
79
+ ]
80
+ })
81
+ assert email.persisted?, "sanity check"
82
+
83
+ email_count = Email.count
84
+ file_count = DbFile.count
85
+
86
+ visit edit_email_path(email)
87
+ assert has_selector?("h1", text: "Edit Email")
88
+
89
+ uncheck "pic.jpg"
90
+ click_on "Update Email"
91
+
92
+ assert has_selector?("h2", text: "Attachments (0)")
93
+ assert_equal(email_count, Email.count)
94
+ assert_equal(file_count - 1, DbFile.count, "should have removed the file record")
95
+ end
96
+
97
+ test "can change attachments" do
98
+ email = Email.create({
99
+ subject: "Fixture email subject",
100
+ message: "Fixture email message",
101
+ attachments: [
102
+ DbFile.new({
103
+ name: "pic.jpg",
104
+ size: 112,
105
+ content_type: "image/jpeg",
106
+ data: "blah"
107
+ })
108
+ ]
109
+ })
110
+ assert email.persisted?, "sanity check"
111
+
112
+ email_count = Email.count
113
+ file_count = DbFile.count
114
+
115
+ visit edit_email_path(email)
116
+ assert has_selector?("h1", text: "Edit Email")
117
+
118
+ uncheck "pic.jpg"
119
+ attach_file "email_attachments_file", File.expand_path("../../fixtures/test_upload.txt", __FILE__)
120
+ assert has_selector?(".file_upload ul li input[type=checkbox]", count: 2)
121
+ click_on "Update Email"
122
+
123
+ assert has_selector?("h2", text: "Attachments (1)")
124
+ assert_equal(email_count, Email.count)
125
+ assert_equal(file_count, DbFile.count, "should have removed the file record and created a new one")
126
+
127
+ file = DbFile.last
128
+ assert_equal("test_upload.txt", file.name)
129
+ end
130
+
131
+ end
@@ -0,0 +1,157 @@
1
+ require 'test_helper'
2
+
3
+ class SingleUploadTest < CapybaraTest
4
+
5
+ teardown do
6
+ User.destroy_all
7
+ DbFile.destroy_all
8
+ end
9
+
10
+ test "can upload a single file" do
11
+ user_count = User.count
12
+ file_count = DbFile.count
13
+
14
+ visit new_user_path
15
+ assert has_selector?(".file_upload .inputs")
16
+ assert has_no_selector?(".file_upload .inputs input[type=hidden]")
17
+
18
+ attach_file "user_avatar_file", File.expand_path("../../fixtures/avatar.jpeg", __FILE__)
19
+ assert has_selector?(".file_upload .inputs input[type=checkbox]")
20
+
21
+ click_on "Create User"
22
+
23
+ # should have validation error on name
24
+ assert has_selector?(".error li")
25
+ assert has_selector?(".file_upload .inputs input[type=checkbox]"), "upload file should still be present"
26
+
27
+ fill_in "Name", with: "John Smith"
28
+ click_on "Create User"
29
+
30
+ assert has_selector?("h1", text: "User: John Smith")
31
+
32
+ assert_equal(user_count + 1, User.count, "should have created a user")
33
+ assert_equal(file_count + 1, DbFile.count, "should have created a db file")
34
+
35
+ file = DbFile.last
36
+ assert_equal("avatar.jpeg", file.name)
37
+ end
38
+
39
+ test "can change file on create" do
40
+ user_count = User.count
41
+ file_count = DbFile.count
42
+
43
+ visit new_user_path
44
+ assert has_selector?(".file_upload .inputs")
45
+ assert has_no_selector?(".file_upload .inputs input[type=hidden]")
46
+
47
+ attach_file "user_avatar_file", File.expand_path("../../fixtures/avatar.jpeg", __FILE__)
48
+ assert has_selector?(".file_upload .inputs input[type=checkbox]", count: 1)
49
+ fill_in "Name", with: "John Smith"
50
+
51
+ attach_file "user_avatar_file", File.expand_path("../../fixtures/test_upload.txt", __FILE__)
52
+ assert has_selector?(".file_upload .inputs input[type=checkbox]", count: 1)
53
+ click_on "Create User"
54
+
55
+ assert has_selector?("h1", text: "User: John Smith")
56
+
57
+ assert_equal(user_count + 1, User.count, "should have created a user")
58
+ assert_equal(file_count + 1, DbFile.count, "should have created a db file")
59
+
60
+ file = DbFile.last
61
+ assert_equal("test_upload.txt", file.name)
62
+ end
63
+
64
+ test "can change file on edit" do
65
+ user = User.create({
66
+ name: "Bob",
67
+ avatar: DbFile.new({
68
+ name: "pic.jpg",
69
+ size: 112,
70
+ content_type: "image/jpeg",
71
+ data: "blah"
72
+ })
73
+ })
74
+ assert user.persisted?, "sanity check"
75
+
76
+ user_count = User.count
77
+ file_count = DbFile.count
78
+
79
+ visit edit_user_path(user)
80
+ assert has_selector?("h1", text: "Edit Bob")
81
+ assert has_selector?(".file_upload .inputs input[type=checkbox]", count: 1)
82
+
83
+ attach_file "user_avatar_file", File.expand_path("../../fixtures/avatar.jpeg", __FILE__)
84
+ assert has_selector?(".file_upload .inputs", text: "avatar.jpeg", count: 1)
85
+ fill_in "Name", with: "Bob2"
86
+
87
+ click_on "Update User"
88
+ assert has_selector?("h1", text: "User: Bob2")
89
+
90
+ assert_equal(user_count, User.count)
91
+ assert_equal(file_count, DbFile.count, "should not create a new record")
92
+
93
+ user.reload
94
+ file = user.avatar
95
+ assert_equal("avatar.jpeg", file.name)
96
+ end
97
+
98
+ test "can remove file on edit" do
99
+ user = User.create({
100
+ name: "Bob",
101
+ avatar: DbFile.new({
102
+ name: "pic.jpg",
103
+ size: 112,
104
+ content_type: "image/jpeg",
105
+ data: "blah"
106
+ })
107
+ })
108
+ assert user.persisted?, "sanity check"
109
+
110
+ user_count = User.count
111
+ file_count = DbFile.count
112
+
113
+ visit edit_user_path(user)
114
+ assert has_selector?("h1", text: "Edit Bob")
115
+ assert has_selector?(".file_upload .inputs input[type=checkbox]", count: 1)
116
+
117
+ uncheck "pic.jpg"
118
+ click_on "Update User"
119
+
120
+ assert has_selector?("h1", text: "User: Bob")
121
+ assert_equal(user_count, User.count)
122
+ assert_equal(file_count - 1, DbFile.count, "should have removed the file record")
123
+ end
124
+
125
+ test "can remove file on edit after changing" do
126
+ user = User.create({
127
+ name: "Bob",
128
+ avatar: DbFile.new({
129
+ name: "pic.jpg",
130
+ size: 112,
131
+ content_type: "image/jpeg",
132
+ data: "blah"
133
+ })
134
+ })
135
+ assert user.persisted?, "sanity check"
136
+
137
+ user_count = User.count
138
+ file_count = DbFile.count
139
+
140
+ visit edit_user_path(user)
141
+ assert has_selector?("h1", text: "Edit Bob")
142
+ assert has_selector?(".file_upload .inputs input[type=checkbox]", count: 1)
143
+
144
+ # choose a file
145
+ attach_file "user_avatar_file", File.expand_path("../../fixtures/avatar.jpeg", __FILE__)
146
+ assert has_selector?(".file_upload .inputs", text: "avatar.jpeg", count: 1)
147
+
148
+ # now remove it
149
+ uncheck "avatar.jpeg"
150
+ click_on "Update User"
151
+
152
+ assert has_selector?("h1", text: "User: Bob")
153
+ assert_equal(user_count, User.count)
154
+ assert_equal(file_count - 1, DbFile.count, "should have removed the file record")
155
+ end
156
+
157
+ end
@@ -0,0 +1,60 @@
1
+ # Configure Rails Environment
2
+ ENV["RAILS_ENV"] = "test"
3
+
4
+ require File.expand_path("../dummy/config/environment.rb", __FILE__)
5
+ require "rails/test_help"
6
+ require 'capybara/rails'
7
+
8
+ Rails.backtrace_cleaner.remove_silencers!
9
+
10
+ # Load support files
11
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
12
+
13
+ # Load fixtures from the engine
14
+ if ActiveSupport::TestCase.method_defined?(:fixture_path=)
15
+ ActiveSupport::TestCase.fixture_path = File.expand_path("../fixtures", __FILE__)
16
+ end
17
+
18
+ class CapybaraTest < ActionDispatch::IntegrationTest
19
+ include Capybara::DSL
20
+
21
+ setup do
22
+ Capybara.current_driver = :webkit
23
+ Capybara.javascript_driver = :webkit
24
+ end
25
+
26
+ teardown do
27
+ Capybara.reset_sessions!
28
+ Capybara.use_default_driver
29
+ end
30
+
31
+ def screenshot(name = 'test_screenshot')
32
+ page.driver.render "tmp/#{name}.png"
33
+ `open tmp/#{name}.png`
34
+ end
35
+ end
36
+
37
+ # Force shared connections for capybara-webkit
38
+ class ActiveRecord::Base
39
+ mattr_accessor :shared_connection
40
+ @@shared_connection = nil
41
+
42
+ def self.connection
43
+ @@shared_connection || ConnectionPool::Wrapper.new(:size => 1, :timeout => 15) { retrieve_connection }
44
+ end
45
+
46
+ def self.clear_all_connections!
47
+ @@shared_connection = nil
48
+ end
49
+
50
+ # Hack to get DatabaseCleaner working
51
+ def self.increment_open_transactions
52
+ connection.increment_open_transactions
53
+ end
54
+
55
+ def self.decrement_open_transactions
56
+ connection.decrement_open_transactions
57
+ end
58
+ end
59
+
60
+ ActiveRecord::Base.shared_connection = ActiveRecord::Base.connection