rails-uploader 0.4.3 → 0.5.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (60) hide show
  1. checksums.yaml +5 -5
  2. data/README.md +3 -1
  3. data/Rakefile +8 -14
  4. data/app/assets/javascripts/uploader/jquery.uploader.js.coffee +1 -0
  5. data/app/controllers/uploader/attachments_controller.rb +5 -9
  6. data/app/views/uploader/default/_container.html.erb +1 -0
  7. data/config/routes.rb +2 -0
  8. data/lib/generators/uploader/install/install_generator.rb +28 -0
  9. data/lib/generators/uploader/install/templates/20130905144515_create_assets.rb +34 -0
  10. data/lib/rails-uploader.rb +2 -1
  11. data/lib/uploader.rb +3 -1
  12. data/lib/uploader/asset.rb +19 -2
  13. data/lib/uploader/authorization.rb +8 -2
  14. data/lib/uploader/authorization_adapter.rb +2 -0
  15. data/lib/uploader/chunked_uploads.rb +2 -0
  16. data/lib/uploader/engine.rb +2 -0
  17. data/lib/uploader/file_part.rb +2 -0
  18. data/lib/uploader/fileupload_glue.rb +10 -3
  19. data/lib/uploader/fileuploads.rb +2 -0
  20. data/lib/uploader/helpers/field_tag.rb +5 -3
  21. data/lib/uploader/helpers/form_builder.rb +2 -0
  22. data/lib/uploader/helpers/form_tag_helper.rb +2 -0
  23. data/lib/uploader/hooks/active_record.rb +2 -0
  24. data/lib/uploader/hooks/formtastic.rb +2 -0
  25. data/lib/uploader/hooks/simple_form.rb +2 -0
  26. data/lib/uploader/json_rendering.rb +11 -0
  27. data/lib/uploader/upload_request.rb +5 -2
  28. data/lib/uploader/version.rb +3 -1
  29. data/spec/dummy/app/assets/config/manifest.js +3 -0
  30. data/spec/dummy/config/environments/production.rb +1 -1
  31. data/spec/dummy/config/environments/test.rb +1 -1
  32. data/spec/requests/attachments_controller_spec.rb +39 -3
  33. data/spec/spec_helper.rb +2 -0
  34. data/vendor/assets/javascripts/uploader/jquery.fileupload-ui.js +7 -0
  35. metadata +39 -98
  36. data/spec/dummy/db/migrate/20120508093416_create_assets.rb +0 -21
  37. data/spec/dummy/db/test.sqlite3 +0 -0
  38. data/spec/dummy/log/test.log +0 -4325
  39. data/spec/dummy/public/uploads/picture/data/1/rails.png +0 -0
  40. data/spec/dummy/public/uploads/picture/data/1/thumb_rails.png +0 -0
  41. data/spec/dummy/public/uploads/picture/data/3/rails.png +0 -0
  42. data/spec/dummy/public/uploads/picture/data/3/thumb_rails.png +0 -0
  43. data/spec/dummy/public/uploads/tmp/1455878520-6456-6895/rails.png +0 -0
  44. data/spec/dummy/public/uploads/tmp/1455878520-6456-6895/thumb_rails.png +0 -0
  45. data/spec/dummy/public/uploads/tmp/20130905-1539-33540-9729/rails.png +0 -0
  46. data/spec/dummy/public/uploads/tmp/20130905-1539-33540-9729/thumb_rails.png +0 -0
  47. data/spec/dummy/public/uploads/tmp/20130905-1542-33540-2188/rails.png +0 -0
  48. data/spec/dummy/public/uploads/tmp/20130905-1542-33540-2188/thumb_rails.png +0 -0
  49. data/spec/dummy/public/uploads/tmp/20130905-1542-33540-8490/rails.png +0 -0
  50. data/spec/dummy/public/uploads/tmp/20130905-1542-33540-8490/thumb_rails.png +0 -0
  51. data/spec/dummy/public/uploads/tmp/20130905-1722-34183-6034/rails.png +0 -0
  52. data/spec/dummy/public/uploads/tmp/20130905-1722-34183-6034/thumb_rails.png +0 -0
  53. data/spec/dummy/public/uploads/tmp/20130905-1723-34214-7553/rails.png +0 -0
  54. data/spec/dummy/public/uploads/tmp/20130905-1723-34214-7553/thumb_rails.png +0 -0
  55. data/spec/dummy/public/uploads/tmp/20130905-1726-34214-2817/rails.png +0 -0
  56. data/spec/dummy/public/uploads/tmp/20130905-1726-34214-2817/thumb_rails.png +0 -0
  57. data/spec/dummy/public/uploads/tmp/20130905-1726-34214-5471/rails.png +0 -0
  58. data/spec/dummy/public/uploads/tmp/20130905-1726-34214-5471/thumb_rails.png +0 -0
  59. data/spec/dummy/public/uploads/tmp/20130905-1726-34214-9281/rails.png +0 -0
  60. data/spec/dummy/public/uploads/tmp/20130905-1726-34214-9281/thumb_rails.png +0 -0
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: af962edb962aca8edc7a8996727d3a95fa41c843
4
- data.tar.gz: 5bf8074ec2db68e2956f89b9ab817d6c80df36e7
2
+ SHA256:
3
+ metadata.gz: 0ee8765eed0acd43fa3f0298b920818c61ce98dd87489357589617c253e08b78
4
+ data.tar.gz: 700490dfe078e643704fd6c08a6bb4d619ce62eaa776493f4ad11c26ee294851
5
5
  SHA512:
6
- metadata.gz: 4bf1c5436485ad70c9c45e4f5a02fe6cb043d3a8da26dc09fa1b2583102f863782559092d6ab79fd703ff0c821e233733b3fa250fd9f4aa1fda879d578ee0983
7
- data.tar.gz: fce54a2f91480eca89f22b435862d15ad6f765e085c8b1425376b90bda1456562ec1dd0628542345892a0495d9752ae347f37e73b20372b399b4460cd20d5b90
6
+ metadata.gz: 02c90581ef78112caa2392c0257c200714ef64b2266f4b851ec651a0dd45d4e6c73000bc7111b160f125f27c796dd8120a6205f29b45afe5f6c0de3797c11bb0
7
+ data.tar.gz: ec0e0cc1ab8417f7930b9decd0610fde73163568a49c75e8dc3565c3ccb6dad770d5968c1780d35ff5b83cab3db4f27e434c1789c2978efdd7114bf1c705a80a
data/README.md CHANGED
@@ -1,3 +1,5 @@
1
+ [![Build Status](https://travis-ci.org/superp/rails-uploader.svg?branch=master)](https://travis-ci.org/superp/rails-uploader)
2
+
1
3
  # HTML5 File uploader for rails
2
4
 
3
5
  This gem use https://github.com/blueimp/jQuery-File-Upload for upload files.
@@ -19,7 +21,7 @@ mount Uploader::Engine => '/uploader'
19
21
  Migration for ActiveRecord:
20
22
 
21
23
  ```bash
22
- $> rake uploader:install:migrations
24
+ $ bundle exec rails g uploader:install
23
25
  ```
24
26
 
25
27
  ## Usage
data/Rakefile CHANGED
@@ -1,18 +1,8 @@
1
- # encoding: utf-8
2
- require 'rake'
3
- require 'rake/testtask'
4
- require 'rake/rdoctask'
1
+ # frozen_string_literal: true
5
2
 
6
- desc 'Default: run unit tests.'
7
- task :default => :test
8
-
9
- desc 'Test the rails-uploader plugin.'
10
- Rake::TestTask.new(:test) do |t|
11
- t.libs << 'lib'
12
- t.libs << 'test'
13
- t.pattern = 'test/**/*_test.rb'
14
- t.verbose = true
15
- end
3
+ require 'rdoc/task'
4
+ require 'bundler/gem_tasks'
5
+ require 'rspec/core/rake_task'
16
6
 
17
7
  desc 'Generate documentation for the rails-uploader plugin.'
18
8
  Rake::RDocTask.new(:rdoc) do |rdoc|
@@ -22,3 +12,7 @@ Rake::RDocTask.new(:rdoc) do |rdoc|
22
12
  rdoc.rdoc_files.include('README.rdoc')
23
13
  rdoc.rdoc_files.include('lib/**/*.rb')
24
14
  end
15
+
16
+ RSpec::Core::RakeTask.new(:spec)
17
+
18
+ task default: :spec
@@ -34,6 +34,7 @@ class UploaderWidget
34
34
  @options['filesContainer'] = @container
35
35
  @options['uploadTemplateId'] += @template
36
36
  @options['downloadTemplateId'] += @template
37
+ @options.singular = !@input.prop('multiple')
37
38
 
38
39
  this._initFileupload()
39
40
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Uploader
2
4
  class AttachmentsController < ActionController::Metal
3
5
  include AbstractController::Callbacks
@@ -5,8 +7,8 @@ module Uploader
5
7
  include Uploader::ChunkedUploads
6
8
 
7
9
  before_action :find_klass
8
- before_action :build_asset, only: [:create]
9
- before_action :find_asset, only: [:destroy]
10
+ before_action :build_asset, only: :create
11
+ before_action :find_asset, only: :destroy
10
12
 
11
13
  def index
12
14
  authorize!(:index, @klass)
@@ -46,7 +48,7 @@ module Uploader
46
48
 
47
49
  def find_klass
48
50
  @klass = Uploader.constantize(params[:klass])
49
- raise ActionController::RoutingError, "Class not found #{params[:klass]}" if @klass.nil?
51
+ raise ActionController::ParameterMissing.new("klass parameter isn't set but #{params[:klass].inspect}") if @klass.nil?
50
52
  end
51
53
 
52
54
  def build_asset
@@ -67,12 +69,6 @@ module Uploader
67
69
  end
68
70
  end
69
71
 
70
- def render_json(hash_or_object, status = 200)
71
- self.status = status
72
- self.content_type = request.format
73
- self.response_body = hash_or_object.to_json(root: false)
74
- end
75
-
76
72
  def asset_params
77
73
  ActionController::Parameters.new(params).require(:asset).permit(:data)
78
74
  end
@@ -1,6 +1,7 @@
1
1
  <%= content_tag(:div, id: field.id, class: 'uploader-dnd-area', data: { tpml: field.klass.to_s, exists: field.exists? }) do -%>
2
2
  <%= hidden_field(field.object_name, :fileupload_guid, object: field.object) if field.object.new_record? %>
3
3
 
4
+ <p class='uploader-errors'></p>
4
5
  <div class="uploader-files"></div>
5
6
 
6
7
  <div class="uploader-dnd-hints">
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  Uploader::Engine.routes.draw do
2
4
  resources :attachments, only: [:index, :create, :update, :destroy]
3
5
  end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails/generators/migration'
4
+
5
+ module Uploader
6
+ module Generators
7
+ class InstallGenerator < ::Rails::Generators::Base
8
+ include Rails::Generators::Migration
9
+
10
+ source_root File.expand_path('templates', __dir__)
11
+
12
+ desc 'add the migrations'
13
+ def self.next_migration_number(_path)
14
+ if @prev_migration_nr
15
+ @prev_migration_nr += 1
16
+ else
17
+ @prev_migration_nr = Time.now.utc.strftime('%Y%m%d%H%M%S').to_i
18
+ end
19
+
20
+ @prev_migration_nr.to_s
21
+ end
22
+
23
+ def copy_migrations
24
+ migration_template '20130905144515_create_assets.rb', 'db/migrate/create_assets.rb'
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ class CreateAssets < ActiveRecord::Migration
4
+ def self.up
5
+ create_table :assets do |t|
6
+ t.string :data_file_name, null: false
7
+ t.string :data_content_type
8
+ t.integer :data_file_size
9
+
10
+ t.integer :assetable_id, null: false
11
+ t.string :assetable_type, limit: 30, null: false
12
+ t.string :type, limit: 30
13
+
14
+ t.string :guid, limit: 30
15
+ t.string :public_token, limit: 30
16
+
17
+ t.integer :user_id
18
+ t.integer :sort_order, default: 0
19
+ t.integer :width
20
+ t.integer :height
21
+
22
+ t.timestamps
23
+ end
24
+
25
+ add_index :assets, [:assetable_type, :type, :assetable_id]
26
+ add_index :assets, :user_id
27
+ add_index :assets, :guid
28
+ add_index :assets, :public_token
29
+ end
30
+
31
+ def self.down
32
+ drop_table :assets
33
+ end
34
+ end
@@ -1,2 +1,3 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
+
2
3
  require 'uploader'
@@ -1,4 +1,5 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
+
2
3
  require 'securerandom'
3
4
  require 'uploader/version'
4
5
 
@@ -64,6 +65,7 @@ module Uploader
64
65
 
65
66
  def self.constantize(klass)
66
67
  return if klass.blank?
68
+
67
69
  klass.safe_constantize
68
70
  end
69
71
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'active_support/concern'
2
4
 
3
5
  module Uploader
@@ -49,7 +51,22 @@ module Uploader
49
51
  #
50
52
  def fileupload_create(params, _request = nil)
51
53
  self[Uploader.guid_column] = params[:guid]
52
- update_attributes(self.class.fileupload_assetable_options(params))
54
+ return false unless update_attributes(self.class.fileupload_assetable_options(params))
55
+
56
+ if fileupload_destroy_other_on_singular?(params)
57
+ self.class.fileupload_find_assets(params).where.not(id: id).destroy_all
58
+ end
59
+ true
60
+ end
61
+
62
+ def fileupload_destroy_other_on_singular?(params)
63
+ return unless params[:singular].to_s.downcase == 'true'
64
+ return true if params[:guid].present?
65
+
66
+ [
67
+ params["#{Uploader.assetable_column}_id"],
68
+ params["#{Uploader.assetable_column}_type"]
69
+ ].all?(&:present?)
53
70
  end
54
71
 
55
72
  # Destroy asset
@@ -80,7 +97,7 @@ module Uploader
80
97
  name: filename,
81
98
  content_type: content_type,
82
99
  size: size,
83
- url: url,
100
+ url: url,
84
101
  thumb_url: thumb_url
85
102
  }
86
103
  end
@@ -1,11 +1,15 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'active_support/concern'
4
+ require_relative 'json_rendering'
2
5
 
3
6
  module Uploader
4
7
  module Authorization
5
8
  extend ActiveSupport::Concern
6
9
 
7
10
  included do
8
- include ActiveSupport::Rescuable
11
+ include JsonRendering
12
+ include ActionController::Rescue
9
13
 
10
14
  rescue_from Uploader::AccessDenied, with: :dispatch_uploader_access_denied
11
15
  end
@@ -21,6 +25,7 @@ module Uploader
21
25
  # If the action is not allowd, it raises an Uploader::AccessDenied exception.
22
26
  def authorize!(action, subject = nil)
23
27
  return if authorized?(action, subject)
28
+
24
29
  raise Uploader::AccessDenied.new(current_uploader_user, action, subject)
25
30
  end
26
31
 
@@ -41,11 +46,12 @@ module Uploader
41
46
  end
42
47
 
43
48
  def dispatch_uploader_access_denied(exception)
44
- render json: { message: exception.message }, status: 403
49
+ render_json({ message: exception.message }, 403)
45
50
  end
46
51
 
47
52
  def current_uploader_user
48
53
  return if Uploader.current_user_proc.nil?
54
+
49
55
  @current_uploader_user ||= Uploader.current_user_proc.call(request)
50
56
  end
51
57
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Uploader
2
4
  class AuthorizationAdapter
3
5
  attr_reader :user
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Uploader
2
4
  module ChunkedUploads
3
5
  extend ActiveSupport::Concern
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'rails'
2
4
 
3
5
  module Uploader
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Uploader
2
4
  class FilePart < File
3
5
  def initialize(path, filename)
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Uploader
2
4
  class FileuploadGlue
3
5
  attr_reader :record, :record_klass
@@ -5,8 +7,8 @@ module Uploader
5
7
  delegate :reflect_on_association, to: :record_klass
6
8
  delegate :fileupload_guid, :new_record?, to: :record
7
9
 
8
- TARGET_TYPE = '_type'.freeze
9
- TARGET_ID = '_id'.freeze
10
+ TARGET_TYPE = '_type'
11
+ TARGET_ID = '_id'
10
12
 
11
13
  def initialize(record)
12
14
  @record = record
@@ -36,6 +38,7 @@ module Uploader
36
38
 
37
39
  def asset(method_name)
38
40
  return unless available_fileuploads.include?(method_name.to_sym)
41
+
39
42
  find_asset_by_fileupload_guid(method_name, fileupload_guid) || build_asset(method_name)
40
43
  end
41
44
 
@@ -47,6 +50,7 @@ module Uploader
47
50
  # Find class by reflection
48
51
  def klass(method_name)
49
52
  return if association(method_name).nil?
53
+
50
54
  association(method_name).klass
51
55
  end
52
56
 
@@ -62,6 +66,7 @@ module Uploader
62
66
 
63
67
  def available_fileuploads
64
68
  return [] if @record_klass.fileupload_options.nil?
69
+
65
70
  @available_fileuploads ||= @record_klass.fileupload_options.keys
66
71
  end
67
72
 
@@ -97,7 +102,9 @@ module Uploader
97
102
  end
98
103
 
99
104
  def record_klass_type
100
- if @record_klass.respond_to?(:base_class)
105
+ if @record_klass.respond_to?(:polymorphic_name)
106
+ @record_klass.polymorphic_name
107
+ elsif @record_klass.respond_to?(:base_class)
101
108
  @record_klass.base_class.name
102
109
  else
103
110
  @record_klass.name
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'active_support/concern'
2
4
 
3
5
  module Uploader
@@ -1,7 +1,9 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Uploader
2
4
  module Helpers
3
5
  class FieldTag
4
- RESERVED_OPTIONS_KEYS = %(method_name object_name theme value object sortable).freeze
6
+ RESERVED_OPTIONS_KEYS = %w[method_name object_name theme value object sortable].freeze
5
7
 
6
8
  attr_reader :template, :object, :theme
7
9
 
@@ -73,12 +75,12 @@ module Uploader
73
75
  def input_html
74
76
  @input_html ||= { multiple: multiple?, class: 'uploader' }.merge(input_html_options)
75
77
  @input_html[:data] ||= {}
76
- @input_html[:data][:url] ||= attachments_path
78
+ @input_html[:data][:url] ||= attachments_path(singular: !multiple?)
77
79
  @input_html
78
80
  end
79
81
 
80
82
  def input_html_options
81
- @options.select { |key, _value| !RESERVED_OPTIONS_KEYS.include?(key.to_s) }
83
+ @options.reject { |key, _value| RESERVED_OPTIONS_KEYS.include?(key.to_s) }
82
84
  end
83
85
  end
84
86
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Uploader
2
4
  module Helpers
3
5
  module FormBuilder
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Uploader
2
4
  module Helpers
3
5
  module FormTagHelper
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'active_record'
2
4
 
3
5
  ActiveSupport.on_load :active_record do
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'formtastic'
2
4
 
3
5
  module Formtastic
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'simple_form'
2
4
 
3
5
  module SimpleForm
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Uploader
4
+ module JsonRendering
5
+ def render_json(hash_or_object, status = 200)
6
+ self.status = status
7
+ self.content_type = request.format
8
+ self.response_body = hash_or_object.to_json(root: false)
9
+ end
10
+ end
11
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'rack/request'
2
4
  require 'fileutils'
3
5
  require 'digest/sha1'
@@ -5,7 +7,7 @@ require 'uri'
5
7
 
6
8
  module Uploader
7
9
  class UploadRequest < Rack::Request
8
- SPLITTER = '/'.freeze
10
+ SPLITTER = '/'
9
11
 
10
12
  attr_reader :file
11
13
 
@@ -30,6 +32,7 @@ module Uploader
30
32
 
31
33
  def total_file_length
32
34
  return content_length.to_i unless chunked?
35
+
33
36
  @env['HTTP_CONTENT_RANGE'].split(SPLITTER).last.to_i
34
37
  end
35
38
 
@@ -73,7 +76,7 @@ module Uploader
73
76
 
74
77
  def extract_filename(value)
75
78
  value = value.match(/filename\s?=\s?\"?([^;"]+)\"?/i)[1]
76
- URI.decode(value.force_encoding('binary'))
79
+ URI.decode_www_form(value.force_encoding('binary'))
77
80
  end
78
81
  end
79
82
  end