rails-uploader 0.4.1 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (61) hide show
  1. checksums.yaml +5 -5
  2. data/README.md +3 -1
  3. data/Rakefile +7 -14
  4. data/app/assets/javascripts/uploader/jquery.uploader.js.coffee +1 -0
  5. data/app/controllers/uploader/attachments_controller.rb +5 -3
  6. data/config/locales/ru.yml +1 -1
  7. data/config/locales/uk.yml +1 -1
  8. data/config/routes.rb +2 -0
  9. data/lib/generators/uploader/install/install_generator.rb +28 -0
  10. data/lib/generators/uploader/install/templates/20130905144515_create_assets.rb +34 -0
  11. data/lib/rails-uploader.rb +2 -1
  12. data/lib/uploader.rb +3 -1
  13. data/lib/uploader/asset.rb +19 -2
  14. data/lib/uploader/authorization.rb +4 -0
  15. data/lib/uploader/authorization_adapter.rb +2 -0
  16. data/lib/uploader/chunked_uploads.rb +3 -1
  17. data/lib/uploader/engine.rb +2 -0
  18. data/lib/uploader/file_part.rb +2 -0
  19. data/lib/uploader/fileupload_glue.rb +10 -3
  20. data/lib/uploader/fileuploads.rb +3 -1
  21. data/lib/uploader/helpers/field_tag.rb +5 -3
  22. data/lib/uploader/helpers/form_builder.rb +2 -0
  23. data/lib/uploader/helpers/form_tag_helper.rb +2 -0
  24. data/lib/uploader/hooks/active_record.rb +2 -0
  25. data/lib/uploader/hooks/formtastic.rb +2 -0
  26. data/lib/uploader/hooks/simple_form.rb +2 -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 +2 -0
  35. data/vendor/assets/stylesheets/uploader/default.scss +5 -6
  36. metadata +35 -95
  37. data/spec/dummy/db/migrate/20120508093416_create_assets.rb +0 -21
  38. data/spec/dummy/db/test.sqlite3 +0 -0
  39. data/spec/dummy/log/test.log +0 -4325
  40. data/spec/dummy/public/uploads/picture/data/1/rails.png +0 -0
  41. data/spec/dummy/public/uploads/picture/data/1/thumb_rails.png +0 -0
  42. data/spec/dummy/public/uploads/picture/data/3/rails.png +0 -0
  43. data/spec/dummy/public/uploads/picture/data/3/thumb_rails.png +0 -0
  44. data/spec/dummy/public/uploads/tmp/1455878520-6456-6895/rails.png +0 -0
  45. data/spec/dummy/public/uploads/tmp/1455878520-6456-6895/thumb_rails.png +0 -0
  46. data/spec/dummy/public/uploads/tmp/20130905-1539-33540-9729/rails.png +0 -0
  47. data/spec/dummy/public/uploads/tmp/20130905-1539-33540-9729/thumb_rails.png +0 -0
  48. data/spec/dummy/public/uploads/tmp/20130905-1542-33540-2188/rails.png +0 -0
  49. data/spec/dummy/public/uploads/tmp/20130905-1542-33540-2188/thumb_rails.png +0 -0
  50. data/spec/dummy/public/uploads/tmp/20130905-1542-33540-8490/rails.png +0 -0
  51. data/spec/dummy/public/uploads/tmp/20130905-1542-33540-8490/thumb_rails.png +0 -0
  52. data/spec/dummy/public/uploads/tmp/20130905-1722-34183-6034/rails.png +0 -0
  53. data/spec/dummy/public/uploads/tmp/20130905-1722-34183-6034/thumb_rails.png +0 -0
  54. data/spec/dummy/public/uploads/tmp/20130905-1723-34214-7553/rails.png +0 -0
  55. data/spec/dummy/public/uploads/tmp/20130905-1723-34214-7553/thumb_rails.png +0 -0
  56. data/spec/dummy/public/uploads/tmp/20130905-1726-34214-2817/rails.png +0 -0
  57. data/spec/dummy/public/uploads/tmp/20130905-1726-34214-2817/thumb_rails.png +0 -0
  58. data/spec/dummy/public/uploads/tmp/20130905-1726-34214-5471/rails.png +0 -0
  59. data/spec/dummy/public/uploads/tmp/20130905-1726-34214-5471/thumb_rails.png +0 -0
  60. data/spec/dummy/public/uploads/tmp/20130905-1726-34214-9281/rails.png +0 -0
  61. 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: 5b4b00fbcd6be9841837569fcbd89b22d291b035
4
- data.tar.gz: 528c4ad6195d462a333439e477456562a1fd4c65
2
+ SHA256:
3
+ metadata.gz: b602b1677568ea5d878174c76224c6a61d431efdb93046f8bd3b897dbed63f55
4
+ data.tar.gz: e2464535aa4a6f329bee939b98a32fba4193f472111bda3c669c5f8465d1ac62
5
5
  SHA512:
6
- metadata.gz: 88594863634e997ce0d3b0997b8345ff75a0c7495a0dc6ac1bd08995267122207bda33ba3099b7b7201c92ce486f18a7010e02dc54607eaaf78c63efe3789f1b
7
- data.tar.gz: 1f6d1b8e0304f6dac05886493ae5c2aad2d1a8baec701664d3aa18ad8afac2ddff01dd3aa7b24230f35f015418b7421aa2791a263a75c92257dfaad52b4baf54
6
+ metadata.gz: 194158c8d9f7540ed210e11b3cb1b98ff346c35562b960365dba01113caea71b808363832b613a804bcd45b2df2f815506d8c74c25532011384dd027f38f9660
7
+ data.tar.gz: d66527e6277747de70d148af352391998c83a75e08418e0a665c31130c1ea5117e06c2c039ce4b8fad73930e75fe5cfe45c77da72e509ee4da6fdb34ed72c5ee
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,7 @@
1
1
  # encoding: utf-8
2
- require 'rake'
3
- require 'rake/testtask'
4
- require 'rake/rdoctask'
5
-
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
2
+ require 'rdoc/task'
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
16
5
 
17
6
  desc 'Generate documentation for the rails-uploader plugin.'
18
7
  Rake::RDocTask.new(:rdoc) do |rdoc|
@@ -22,3 +11,7 @@ Rake::RDocTask.new(:rdoc) do |rdoc|
22
11
  rdoc.rdoc_files.include('README.rdoc')
23
12
  rdoc.rdoc_files.include('lib/**/*.rb')
24
13
  end
14
+
15
+ RSpec::Core::RakeTask.new(:spec)
16
+
17
+ 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
@@ -1,6 +1,6 @@
1
1
  ru:
2
2
  uploader:
3
- button: "Выберите файл на компьютере"
3
+ button: "Выберите файл"
4
4
  drop: "Перетащите файл сюда"
5
5
  or: "или"
6
6
  confirm_delete: "Are you sure?"
@@ -1,6 +1,6 @@
1
1
  uk:
2
2
  uploader:
3
- button: "Виберіть файл на комп'ютері"
3
+ button: "Оберіть файл"
4
4
  drop: "Перетягніть файл сюди"
5
5
  or: "або"
6
6
  confirm_delete: "Are you sure?"
@@ -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,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'active_support/concern'
2
4
 
3
5
  module Uploader
@@ -21,6 +23,7 @@ module Uploader
21
23
  # If the action is not allowd, it raises an Uploader::AccessDenied exception.
22
24
  def authorize!(action, subject = nil)
23
25
  return if authorized?(action, subject)
26
+
24
27
  raise Uploader::AccessDenied.new(current_uploader_user, action, subject)
25
28
  end
26
29
 
@@ -46,6 +49,7 @@ module Uploader
46
49
 
47
50
  def current_uploader_user
48
51
  return if Uploader.current_user_proc.nil?
52
+
49
53
  @current_uploader_user ||= Uploader.current_user_proc.call(request)
50
54
  end
51
55
  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
@@ -5,7 +7,7 @@ module Uploader
5
7
  protected
6
8
 
7
9
  def with_chunked_upload(file_or_part)
8
- uploader = UploadRequest.new(env, file_or_part)
10
+ uploader = UploadRequest.new(request.env, file_or_part)
9
11
  return unless uploader.completed?
10
12
 
11
13
  yield uploader.file
@@ -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
@@ -40,7 +42,7 @@ module Uploader
40
42
  end
41
43
 
42
44
  def fileupload_guid=(value)
43
- @fileupload_changed = (@fileupload_guid != value)
45
+ @fileupload_changed = (@fileupload_guid != value) if @fileupload_changed.nil?
44
46
  @fileupload_guid = value.blank? ? nil : value
45
47
  end
46
48
 
@@ -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
@@ -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