glebtv-rails-uploader 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (87) hide show
  1. data/MIT-LICENSE +20 -0
  2. data/README.md +241 -0
  3. data/Rakefile +26 -0
  4. data/app/assets/javascripts/uploader/application.js +9 -0
  5. data/app/assets/javascripts/uploader/rails_admin.js +25 -0
  6. data/app/assets/stylesheets/uploader/application.css +7 -0
  7. data/app/controllers/uploader/attachments_controller.rb +67 -0
  8. data/app/views/rails_admin/main/_form_rails_uploader.haml +10 -0
  9. data/app/views/uploader/default/_container.html.erb +67 -0
  10. data/app/views/uploader/default/_download.html.erb +17 -0
  11. data/app/views/uploader/default/_upload.html.erb +14 -0
  12. data/config/locales/en.yml +5 -0
  13. data/config/locales/ru.yml +5 -0
  14. data/config/locales/uk.yml +5 -0
  15. data/config/routes.rb +7 -0
  16. data/lib/file_size_validator.rb +66 -0
  17. data/lib/glebtv-rails-uploader.rb +3 -0
  18. data/lib/uploader.rb +35 -0
  19. data/lib/uploader/asset.rb +47 -0
  20. data/lib/uploader/engine.rb +25 -0
  21. data/lib/uploader/fileuploads.rb +109 -0
  22. data/lib/uploader/helpers/field_tag.rb +100 -0
  23. data/lib/uploader/helpers/form_builder.rb +16 -0
  24. data/lib/uploader/helpers/form_tag_helper.rb +16 -0
  25. data/lib/uploader/hooks/active_record.rb +5 -0
  26. data/lib/uploader/hooks/formtastic.rb +14 -0
  27. data/lib/uploader/hooks/simple_form.rb +9 -0
  28. data/lib/uploader/rails_admin/field.rb +28 -0
  29. data/lib/uploader/version.rb +3 -0
  30. data/spec/dummy/README.rdoc +261 -0
  31. data/spec/dummy/Rakefile +7 -0
  32. data/spec/dummy/app/assets/javascripts/application.js +15 -0
  33. data/spec/dummy/app/assets/stylesheets/application.css +13 -0
  34. data/spec/dummy/app/controllers/application_controller.rb +3 -0
  35. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  36. data/spec/dummy/app/models/article.rb +7 -0
  37. data/spec/dummy/app/models/asset.rb +27 -0
  38. data/spec/dummy/app/models/picture.rb +5 -0
  39. data/spec/dummy/app/uploaders/picture_uploader.rb +55 -0
  40. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  41. data/spec/dummy/config.ru +4 -0
  42. data/spec/dummy/config/application.rb +58 -0
  43. data/spec/dummy/config/boot.rb +10 -0
  44. data/spec/dummy/config/database.yml +25 -0
  45. data/spec/dummy/config/environment.rb +5 -0
  46. data/spec/dummy/config/environments/development.rb +37 -0
  47. data/spec/dummy/config/environments/production.rb +67 -0
  48. data/spec/dummy/config/environments/test.rb +37 -0
  49. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  50. data/spec/dummy/config/initializers/inflections.rb +15 -0
  51. data/spec/dummy/config/initializers/mime_types.rb +5 -0
  52. data/spec/dummy/config/initializers/secret_token.rb +7 -0
  53. data/spec/dummy/config/initializers/session_store.rb +8 -0
  54. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  55. data/spec/dummy/config/locales/en.yml +5 -0
  56. data/spec/dummy/config/routes.rb +3 -0
  57. data/spec/dummy/db/migrate/20120508093416_create_assets.rb +19 -0
  58. data/spec/dummy/db/migrate/20120508093830_create_articles.rb +10 -0
  59. data/spec/dummy/public/404.html +26 -0
  60. data/spec/dummy/public/422.html +26 -0
  61. data/spec/dummy/public/500.html +25 -0
  62. data/spec/dummy/public/favicon.ico +0 -0
  63. data/spec/dummy/script/rails +6 -0
  64. data/spec/factories/articles.rb +8 -0
  65. data/spec/factories/assets.rb +11 -0
  66. data/spec/factories/files/rails.png +0 -0
  67. data/spec/fileuploads_spec.rb +57 -0
  68. data/spec/requests/attachments_controller_spec.rb +38 -0
  69. data/spec/spec_helper.rb +52 -0
  70. data/spec/uploader_spec.rb +18 -0
  71. data/vendor/assets/images/uploader/but_del_tag2.png +0 -0
  72. data/vendor/assets/images/uploader/ico_attach.png +0 -0
  73. data/vendor/assets/images/uploader/preloader.gif +0 -0
  74. data/vendor/assets/images/uploader/progressBarFillBg.png +0 -0
  75. data/vendor/assets/javascripts/uploader/jquery.fileupload-ip.js +160 -0
  76. data/vendor/assets/javascripts/uploader/jquery.fileupload-ui.js +637 -0
  77. data/vendor/assets/javascripts/uploader/jquery.fileupload.js +904 -0
  78. data/vendor/assets/javascripts/uploader/jquery.iframe-transport.js +171 -0
  79. data/vendor/assets/javascripts/uploader/jquery.ui.widget.js +282 -0
  80. data/vendor/assets/javascripts/uploader/load-image.min.js +1 -0
  81. data/vendor/assets/javascripts/uploader/locales/en.js +27 -0
  82. data/vendor/assets/javascripts/uploader/locales/ru.js +27 -0
  83. data/vendor/assets/javascripts/uploader/locales/uk.js +27 -0
  84. data/vendor/assets/javascripts/uploader/tmpl.min.js +1 -0
  85. data/vendor/assets/stylesheets/uploader/default.css +214 -0
  86. data/vendor/assets/stylesheets/uploader/jquery.fileupload-ui.css +30 -0
  87. metadata +317 -0
@@ -0,0 +1,26 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>The page you were looking for doesn't exist (404)</title>
5
+ <style type="text/css">
6
+ body { background-color: #fff; color: #666; text-align: center; font-family: arial, sans-serif; }
7
+ div.dialog {
8
+ width: 25em;
9
+ padding: 0 4em;
10
+ margin: 4em auto 0 auto;
11
+ border: 1px solid #ccc;
12
+ border-right-color: #999;
13
+ border-bottom-color: #999;
14
+ }
15
+ h1 { font-size: 100%; color: #f00; line-height: 1.5em; }
16
+ </style>
17
+ </head>
18
+
19
+ <body>
20
+ <!-- This file lives in public/404.html -->
21
+ <div class="dialog">
22
+ <h1>The page you were looking for doesn't exist.</h1>
23
+ <p>You may have mistyped the address or the page may have moved.</p>
24
+ </div>
25
+ </body>
26
+ </html>
@@ -0,0 +1,26 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>The change you wanted was rejected (422)</title>
5
+ <style type="text/css">
6
+ body { background-color: #fff; color: #666; text-align: center; font-family: arial, sans-serif; }
7
+ div.dialog {
8
+ width: 25em;
9
+ padding: 0 4em;
10
+ margin: 4em auto 0 auto;
11
+ border: 1px solid #ccc;
12
+ border-right-color: #999;
13
+ border-bottom-color: #999;
14
+ }
15
+ h1 { font-size: 100%; color: #f00; line-height: 1.5em; }
16
+ </style>
17
+ </head>
18
+
19
+ <body>
20
+ <!-- This file lives in public/422.html -->
21
+ <div class="dialog">
22
+ <h1>The change you wanted was rejected.</h1>
23
+ <p>Maybe you tried to change something you didn't have access to.</p>
24
+ </div>
25
+ </body>
26
+ </html>
@@ -0,0 +1,25 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>We're sorry, but something went wrong (500)</title>
5
+ <style type="text/css">
6
+ body { background-color: #fff; color: #666; text-align: center; font-family: arial, sans-serif; }
7
+ div.dialog {
8
+ width: 25em;
9
+ padding: 0 4em;
10
+ margin: 4em auto 0 auto;
11
+ border: 1px solid #ccc;
12
+ border-right-color: #999;
13
+ border-bottom-color: #999;
14
+ }
15
+ h1 { font-size: 100%; color: #f00; line-height: 1.5em; }
16
+ </style>
17
+ </head>
18
+
19
+ <body>
20
+ <!-- This file lives in public/500.html -->
21
+ <div class="dialog">
22
+ <h1>We're sorry, but something went wrong.</h1>
23
+ </div>
24
+ </body>
25
+ </html>
File without changes
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+ # This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application.
3
+
4
+ APP_PATH = File.expand_path('../../config/application', __FILE__)
5
+ require File.expand_path('../../config/boot', __FILE__)
6
+ require 'rails/commands'
@@ -0,0 +1,8 @@
1
+ # Read about factories at https://github.com/thoughtbot/factory_girl
2
+
3
+ FactoryGirl.define do
4
+ factory :article do
5
+ title "MyString"
6
+ content "MyText"
7
+ end
8
+ end
@@ -0,0 +1,11 @@
1
+ # Read about factories at https://github.com/thoughtbot/factory_girl
2
+
3
+ FactoryGirl.define do
4
+ factory :picture, :class => Picture do
5
+ data File.open('spec/factories/files/rails.png')
6
+ data_content_type "image/png"
7
+ data_file_name "rails.png"
8
+
9
+ association :assetable, :factory => :article
10
+ end
11
+ end
Binary file
@@ -0,0 +1,57 @@
1
+ require 'spec_helper'
2
+
3
+ describe Uploader::Fileuploads do
4
+ before(:all) do
5
+ @picture = FactoryGirl.create(:picture)
6
+ end
7
+
8
+ it "should be a Module" do
9
+ Uploader::Fileuploads.should be_a(Module)
10
+ end
11
+
12
+ it "should return asset class" do
13
+ Article.fileupload_klass("picture").should == Picture
14
+ end
15
+
16
+ it "should find asset by guid" do
17
+ asset = Article.fileupload_find("picture", @picture.guid)
18
+ asset.should == @picture
19
+ end
20
+
21
+ it "should update asset target_id by guid" do
22
+ Article.fileupload_update(1000, @picture.guid, :picture)
23
+ @picture.reload
24
+ @picture.assetable_id.should == 1000
25
+ @picture.guid.should be_nil
26
+ end
27
+
28
+ context "instance methods" do
29
+ before(:each) do
30
+ @article = FactoryGirl.build(:article)
31
+ end
32
+
33
+ it "should generate guid" do
34
+ @article.fileupload_guid.should_not be_blank
35
+ end
36
+
37
+ it "should change guid" do
38
+ @article.fileupload_guid = "other guid"
39
+ @article.fileupload_changed?.should be_true
40
+ @article.fileupload_guid.should == "other guid"
41
+ end
42
+
43
+ it "should not multiplay upload" do
44
+ @article.fileupload_multiple?("picture").should be_false
45
+ end
46
+
47
+ it "should find uploaded asset or build new record" do
48
+ picture = @article.fileupload_asset(:picture)
49
+ picture.should_not be_nil
50
+ picture.should be_new_record
51
+ end
52
+
53
+ it "should return fileuploads columns" do
54
+ @article.fileuploads_columns.should include(:picture)
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,38 @@
1
+ require 'spec_helper'
2
+
3
+ describe Uploader::AttachmentsController do
4
+ include Rack::Test::Methods
5
+
6
+ def app
7
+ Dummy::Application
8
+ end
9
+
10
+ it "should create new asset" do
11
+ file = Rack::Test::UploadedFile.new('spec/factories/files/rails.png', "image/png")
12
+ post "/uploader/attachments", {
13
+ :klass => "Picture",
14
+ :assetable_id => "1",
15
+ :assetable_type => "Article",
16
+ :guid => "SOMESTRING",
17
+ :asset => {:data => file}
18
+ }
19
+
20
+ last_response.body.should include("assetable_type")
21
+ last_response.body.should include("SOMESTRING")
22
+ last_response.body.should include("data")
23
+ end
24
+
25
+ it "should destroy asset" do
26
+ @asset = FactoryGirl.create(:picture)
27
+
28
+ lambda {
29
+ delete "/uploader/attachments/#{@asset.id}", {:klass => "Picture"}
30
+ }.should change { Picture.count }.by(-1)
31
+ end
32
+
33
+ it "should raise 404 error with wrong class" do
34
+ lambda {
35
+ post "/uploader/attachments", {:klass => "wrong"}
36
+ }.should raise_error(ActionController::RoutingError)
37
+ end
38
+ end
@@ -0,0 +1,52 @@
1
+ # Configure Rails Envinronment
2
+ ENV["RAILS_ENV"] = "test"
3
+
4
+ require File.expand_path("../dummy/config/environment.rb", __FILE__)
5
+ require "rails/test_help"
6
+ require "rspec/rails"
7
+ require "database_cleaner"
8
+
9
+ # Fixtures replacement with a straightforward definition syntax
10
+ require 'factory_girl'
11
+ FactoryGirl.definition_file_paths = [ File.expand_path("../factories/", __FILE__) ]
12
+ FactoryGirl.find_definitions
13
+
14
+ ActionMailer::Base.delivery_method = :test
15
+ ActionMailer::Base.perform_deliveries = true
16
+ ActionMailer::Base.default_url_options[:host] = "test.com"
17
+
18
+ Rails.backtrace_cleaner.remove_silencers!
19
+
20
+ # Run any available migration
21
+ ActiveRecord::Migrator.migrate File.expand_path("../dummy/db/migrate/", __FILE__)
22
+
23
+ require 'carrierwave'
24
+ CarrierWave.configure do |config|
25
+ config.storage = :file
26
+ config.enable_processing = true
27
+ end
28
+
29
+ # Load support files
30
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
31
+
32
+ RSpec.configure do |config|
33
+ # Remove this line if you don't want RSpec's should and should_not
34
+ # methods or matchers
35
+ require 'rspec/expectations'
36
+ config.include RSpec::Matchers
37
+
38
+ # == Mock Framework
39
+ config.mock_with :rspec
40
+
41
+ config.before(:suite) do
42
+ DatabaseCleaner.strategy = :truncation
43
+ end
44
+
45
+ config.before(:all) do
46
+ DatabaseCleaner.start
47
+ end
48
+
49
+ config.after(:all) do
50
+ DatabaseCleaner.clean
51
+ end
52
+ end
@@ -0,0 +1,18 @@
1
+ require 'spec_helper'
2
+
3
+ describe Uploader do
4
+ it "should be a Module" do
5
+ Uploader.should be_a(Module)
6
+ end
7
+
8
+ it "should generate random string" do
9
+ value = Uploader.guid
10
+ value.should_not be_blank
11
+ value.size.should == 10
12
+ end
13
+
14
+ it "should find all precompile assets" do
15
+ Uploader.assets.should_not be_nil
16
+ Uploader.assets.should include('uploader/jquery.fileupload.js')
17
+ end
18
+ end
@@ -0,0 +1,160 @@
1
+ /*
2
+ * jQuery File Upload Image Processing Plugin 1.0.6
3
+ * https://github.com/blueimp/jQuery-File-Upload
4
+ *
5
+ * Copyright 2012, Sebastian Tschan
6
+ * https://blueimp.net
7
+ *
8
+ * Licensed under the MIT license:
9
+ * http://www.opensource.org/licenses/MIT
10
+ */
11
+
12
+ /*jslint nomen: true, unparam: true, regexp: true */
13
+ /*global define, window, document */
14
+
15
+ (function (factory) {
16
+ 'use strict';
17
+ if (typeof define === 'function' && define.amd) {
18
+ // Register as an anonymous AMD module:
19
+ define([
20
+ 'jquery',
21
+ 'load-image',
22
+ 'canvas-to-blob',
23
+ './jquery.fileupload'
24
+ ], factory);
25
+ } else {
26
+ // Browser globals:
27
+ factory(
28
+ window.jQuery,
29
+ window.loadImage,
30
+ window.canvasToBlob
31
+ );
32
+ }
33
+ }(function ($, loadImage, canvasToBlob) {
34
+ 'use strict';
35
+
36
+ // The File Upload IP version extends the basic fileupload widget
37
+ // with image processing functionality:
38
+ $.widget('blueimpIP.fileupload', $.blueimp.fileupload, {
39
+
40
+ options: {
41
+ // The regular expression to define which image files are to be
42
+ // resized, given that the browser supports the operation:
43
+ resizeSourceFileTypes: /^image\/(gif|jpeg|png)$/,
44
+ // The maximum file size of images that are to be resized:
45
+ resizeSourceMaxFileSize: 20000000, // 20MB
46
+ // The maximum width of the resized images:
47
+ resizeMaxWidth: undefined,
48
+ // The maximum height of the resized images:
49
+ resizeMaxHeight: undefined,
50
+ // The minimum width of the resized images:
51
+ resizeMinWidth: undefined,
52
+ // The minimum height of the resized images:
53
+ resizeMinHeight: undefined,
54
+
55
+ // The add callback is invoked as soon as files are added to the fileupload
56
+ // widget (via file input selection, drag & drop or add API call).
57
+ // See the basic file upload widget for more information:
58
+ add: function (e, data) {
59
+ $(this).fileupload('resize', data).done(function () {
60
+ data.submit();
61
+ });
62
+ }
63
+ },
64
+
65
+ // Resizes the image file at the given index and stores the created blob
66
+ // at the original position of the files list, returns a Promise object:
67
+ _resizeImage: function (files, index, options) {
68
+ var that = this,
69
+ file = files[index],
70
+ deferred = $.Deferred(),
71
+ canvas,
72
+ blob;
73
+ options = options || this.options;
74
+ loadImage(
75
+ file,
76
+ function (img) {
77
+ var width = img.width,
78
+ height = img.height;
79
+ canvas = loadImage.scale(img, {
80
+ maxWidth: options.resizeMaxWidth,
81
+ maxHeight: options.resizeMaxHeight,
82
+ minWidth: options.resizeMinWidth,
83
+ minHeight: options.resizeMinHeight,
84
+ canvas: true
85
+ });
86
+ if (width !== canvas.width || height !== canvas.height) {
87
+ canvasToBlob(canvas, function (blob) {
88
+ if (!blob.name) {
89
+ if (file.type === blob.type) {
90
+ blob.name = file.name;
91
+ } else if (file.name) {
92
+ blob.name = file.name.replace(
93
+ /\..+$/,
94
+ '.' + blob.type.substr(6)
95
+ );
96
+ }
97
+ }
98
+ files[index] = blob;
99
+ deferred.resolveWith(that);
100
+ }, file);
101
+ } else {
102
+ deferred.resolveWith(that);
103
+ }
104
+ }
105
+ );
106
+ return deferred.promise();
107
+ },
108
+
109
+ // Resizes the images given as files property of the data parameter,
110
+ // returns a Promise object that allows to bind a done handler, which
111
+ // will be invoked after processing all images is done:
112
+ resize: function (data) {
113
+ var that = this,
114
+ options = $.extend({}, this.options, data),
115
+ resizeAll = $.type(options.resizeSourceMaxFileSize) !== 'number',
116
+ isXHRUpload = this._isXHRUpload(options);
117
+ $.each(data.files, function (index, file) {
118
+ if (isXHRUpload && that._resizeSupport &&
119
+ (options.resizeMaxWidth || options.resizeMaxHeight ||
120
+ options.resizeMinWidth || options.resizeMinHeight) &&
121
+ (resizeAll || file.size < options.resizeSourceMaxFileSize) &&
122
+ options.resizeSourceFileTypes.test(file.type)) {
123
+ that._processing += 1;
124
+ if (that._processing === 1) {
125
+ that.element.addClass('fileupload-processing');
126
+ }
127
+ that._processingQueue = that._processingQueue.pipe(function () {
128
+ var deferred = $.Deferred();
129
+ that._resizeImage(
130
+ data.files,
131
+ index,
132
+ options
133
+ ).done(function () {
134
+ that._processing -= 1;
135
+ if (that._processing === 0) {
136
+ that.element
137
+ .removeClass('fileupload-processing');
138
+ }
139
+ deferred.resolveWith(that);
140
+ });
141
+ return deferred.promise();
142
+ });
143
+ }
144
+ });
145
+ return this._processingQueue;
146
+ },
147
+
148
+ _create: function () {
149
+ $.blueimp.fileupload.prototype._create.call(this);
150
+ this._processing = 0;
151
+ this._processingQueue = $.Deferred().resolveWith(this).promise();
152
+ this._resizeSupport = canvasToBlob && canvasToBlob(
153
+ document.createElement('canvas'),
154
+ $.noop
155
+ );
156
+ }
157
+
158
+ });
159
+
160
+ }));