glebtv-rails-uploader 0.1.3 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (42) hide show
  1. checksums.yaml +7 -0
  2. data/README.md +17 -10
  3. data/{vendor → app}/assets/images/uploader/but_del_tag2.png +0 -0
  4. data/{vendor → app}/assets/images/uploader/ico_attach.png +0 -0
  5. data/{vendor → app}/assets/images/uploader/preloader.gif +0 -0
  6. data/{vendor → app}/assets/images/uploader/progressBarFillBg.png +0 -0
  7. data/app/assets/javascripts/uploader/application.js +2 -2
  8. data/app/assets/javascripts/uploader/canvas-to-blob.js +95 -0
  9. data/app/assets/javascripts/uploader/jquery.fileupload-angular.js +348 -0
  10. data/app/assets/javascripts/uploader/jquery.fileupload-process.js +158 -0
  11. data/app/assets/javascripts/uploader/jquery.fileupload-resize.js +212 -0
  12. data/{vendor → app}/assets/javascripts/uploader/jquery.fileupload-ui.js +265 -269
  13. data/app/assets/javascripts/uploader/jquery.fileupload-validate.js +116 -0
  14. data/{vendor → app}/assets/javascripts/uploader/jquery.fileupload.js +655 -258
  15. data/{vendor → app}/assets/javascripts/uploader/jquery.iframe-transport.js +29 -9
  16. data/app/assets/javascripts/uploader/jquery.ui.widget.js +530 -0
  17. data/app/assets/javascripts/uploader/load-image.js +381 -0
  18. data/{vendor → app}/assets/javascripts/uploader/locales/en.js +0 -0
  19. data/{vendor → app}/assets/javascripts/uploader/locales/ru.js +0 -0
  20. data/{vendor → app}/assets/javascripts/uploader/locales/uk.js +0 -0
  21. data/app/assets/javascripts/uploader/rails_admin.js +26 -24
  22. data/app/assets/javascripts/uploader/tmpl.js +86 -0
  23. data/{vendor → app}/assets/stylesheets/uploader/default.css +0 -0
  24. data/{vendor → app}/assets/stylesheets/uploader/jquery.fileupload-ui.css +0 -0
  25. data/app/controllers/uploader/attachments_controller.rb +13 -10
  26. data/app/views/rails_admin/main/_form_rails_uploader.haml +9 -9
  27. data/app/views/uploader/default/_container.html.erb +1 -2
  28. data/app/views/uploader/default/_download.html.erb +1 -1
  29. data/lib/uploader/asset.rb +2 -2
  30. data/lib/uploader/engine.rb +4 -4
  31. data/lib/uploader/fileuploads.rb +18 -18
  32. data/lib/uploader/helpers/field_tag.rb +16 -17
  33. data/lib/uploader/helpers/form_builder.rb +1 -1
  34. data/lib/uploader/helpers/form_tag_helper.rb +1 -1
  35. data/lib/uploader/hooks/formtastic.rb +1 -2
  36. data/lib/uploader/rails_admin/field.rb +27 -27
  37. data/lib/uploader/version.rb +1 -1
  38. metadata +115 -131
  39. data/vendor/assets/javascripts/uploader/jquery.fileupload-ip.js +0 -160
  40. data/vendor/assets/javascripts/uploader/jquery.ui.widget.js +0 -282
  41. data/vendor/assets/javascripts/uploader/load-image.min.js +0 -1
  42. data/vendor/assets/javascripts/uploader/tmpl.min.js +0 -1
@@ -2,17 +2,21 @@
2
2
  module Uploader
3
3
  class AttachmentsController < ActionController::Metal
4
4
  include AbstractController::Callbacks
5
-
5
+
6
6
  before_filter :find_klass
7
-
7
+
8
8
  def create
9
9
  @asset = @klass.new(params[:asset])
10
10
  @asset.uploader_create(params, request)
11
11
  render_resourse(@asset, 201)
12
12
  end
13
-
13
+
14
14
  def destroy
15
- @asset = @klass.find(params[:id])
15
+ begin
16
+ @asset = @klass.find(params[:id])
17
+ rescue
18
+ @asset = @klass.where(guid: params[:guid])
19
+ end
16
20
  @asset.uploader_destroy(params, request)
17
21
  render_resourse(@asset, 200)
18
22
  end
@@ -38,7 +42,7 @@ module Uploader
38
42
  self.content_type = "application/json"
39
43
  self.response_body = '{"ok": true}'
40
44
  end
41
-
45
+
42
46
  protected
43
47
 
44
48
 
@@ -53,20 +57,19 @@ module Uploader
53
57
  @klass = params[:klass].blank? ? nil : params[:klass].safe_constantize
54
58
  raise ActionController::RoutingError.new("Class not found #{params[:klass]}") if @klass.nil?
55
59
  end
56
-
60
+
57
61
  def render_resourse(record, status = 200)
58
62
  if record.errors.empty?
59
63
  if record.respond_to?(:to_jq_upload)
60
- render_json(record.to_jq_upload.to_json(:root => false), status)
64
+ render_json({'files' => Array.wrap(record.to_jq_upload)}.to_json, status)
61
65
  else
62
- render_json([record].to_json(:root => false), status)
66
+ render_json({'files' => Array.wrap(record)}.to_json, status)
63
67
  end
64
-
65
68
  else
66
69
  render_json([record.errors].to_json, 422)
67
70
  end
68
71
  end
69
-
72
+
70
73
  def render_json(body, status = 200)
71
74
  self.status = status
72
75
  self.content_type = "application/json"
@@ -1,10 +1,10 @@
1
- = stylesheet_link_tag "uploader/application.css"
2
- = form.send 'uploader_field', field.method_name, field.html_attributes
3
- :javascript
4
- (function(d, s, id) {
5
- var js, fjs = d.getElementsByTagName(s)[0];
6
- if (d.getElementById(id)) return;
7
- js = d.createElement(s); js.id = id;
8
- js.src = "/assets/uploader/rails_admin.js";
9
- fjs.parentNode.appendChild(js);
1
+ = stylesheet_link_tag "uploader/application.css"
2
+ = form.send 'uploader_field', field.method_name, field.html_attributes
3
+ :javascript
4
+ (function(d, s, id) {
5
+ var js, fjs = d.getElementsByTagName(s)[0];
6
+ if (d.getElementById(id)) return;
7
+ js = d.createElement(s); js.id = id;
8
+ js.src = "/assets/uploader/rails_admin.js";
9
+ fjs.parentNode.appendChild(js);
10
10
  }(document, 'script', 'uploader-jssdk'));
@@ -37,7 +37,6 @@
37
37
 
38
38
  $('#<%= field.id %> input[type="file"]').attr('name', 'uploader_field').bind("init.uploader", function (e) {
39
39
  if (!$.fn.fileupload) {
40
- // not yet loaded
41
40
  return false;
42
41
  }
43
42
 
@@ -56,7 +55,7 @@
56
55
  });
57
56
 
58
57
  <% if field.exists? -%>
59
- <% fv = field.values.map { |v| v.respond_to?(:to_jq_upload) ? v.to_jq_upload.first : v } %>
58
+ <% fv = field.values.map { |v| v.respond_to?(:to_jq_upload) ? v.to_jq_upload : v } %>
60
59
  $(this).data("fileupload")
61
60
  ._renderDownload(<%=raw fv.to_json(:root => false) %>)
62
61
  .appendTo(container);
@@ -3,7 +3,7 @@
3
3
  {% for (var i=0, file; file=o.files[i]; i++) { %}
4
4
  <div class="attach_item template-download" data-id="{%=file.id%}">
5
5
  <div class="delete">
6
- <a href="/" class="del_btn" data-type="DELETE" data-url="/uploader/attachments/{%=file.id%}?klass=<%= field.klass %>"></a>
6
+ <a href="/" class="del_btn delete" data-type="DELETE" data-url="/uploader/attachments/{%=file.id%}?guid={%= file.guid %}&klass=<%= field.klass %>"></a>
7
7
  </div>
8
8
  <div class="thumbnail preview">
9
9
  <a href="{%=file.url%}"><img src="{%=file.thumb_url%}" title="{%=file.filename%}" rel="gallery"></a>
@@ -5,7 +5,7 @@ module Uploader
5
5
  #
6
6
  # class Asset < ActiveRecord::Base
7
7
  # include Uploader::Asset
8
- #
8
+ #
9
9
  # def uploader_create(params, request = nil)
10
10
  # self.user = request.env['warden'].user
11
11
  # super
@@ -29,7 +29,7 @@ module Uploader
29
29
  #
30
30
  # class Asset < ActiveRecord::Base
31
31
  # include Uploader::Asset
32
- #
32
+ #
33
33
  # def uploader_destroy(params, request = nil)
34
34
  # ability = Ability.new(request.env['warden'].user)
35
35
  # if ability.can? :delete, self
@@ -4,18 +4,18 @@ require 'uploader'
4
4
  module Uploader
5
5
  class Engine < ::Rails::Engine
6
6
  isolate_namespace Uploader
7
-
7
+
8
8
  initializer "uploader.assets_precompile" do |app|
9
9
  app.config.assets.precompile += Uploader.assets
10
10
  end
11
-
12
- initializer "uploader.helpers" do
11
+
12
+ initializer "uploader.helpers" do
13
13
  ActiveSupport.on_load :action_view do
14
14
  ActionView::Base.send(:include, Uploader::Helpers::FormTagHelper)
15
15
  ActionView::Helpers::FormBuilder.send(:include, Uploader::Helpers::FormBuilder)
16
16
  end
17
17
  end
18
-
18
+
19
19
  initializer "uploader.hooks" do
20
20
  require "uploader/hooks/active_record" if Object.const_defined?("ActiveRecord")
21
21
  require "uploader/hooks/formtastic" if Object.const_defined?("Formtastic")
@@ -3,11 +3,11 @@ module Uploader
3
3
  def self.included(base)
4
4
  base.send :extend, SingletonMethods
5
5
  end
6
-
6
+
7
7
  module SingletonMethods
8
8
  # Join ActiveRecord object with uploaded file
9
9
  # Usage:
10
- #
10
+ #
11
11
  # class Article < ActiveRecord::Base
12
12
  # has_one :picture, :as => :assetable, :dependent => :destroy
13
13
  #
@@ -17,37 +17,38 @@ module Uploader
17
17
  #
18
18
  def fileuploads(*args)
19
19
  options = args.extract_options!
20
-
20
+
21
21
  class_attribute :fileuploads_options, :instance_writer => false
22
22
  self.fileuploads_options = options
23
-
23
+
24
24
  class_attribute :fileuploads_columns, :instance_writer => false
25
25
  self.fileuploads_columns = args.map(&:to_sym)
26
-
26
+
27
27
  unless self.is_a?(ClassMethods)
28
28
  include InstanceMethods
29
29
  extend ClassMethods
30
30
 
31
31
  after_save :fileuploads_update, :if => :fileupload_changed?
32
-
32
+
33
33
  fileuploads_columns.each { |asset| accepts_nested_attributes_for asset, :allow_destroy => true }
34
34
  end
35
35
  end
36
36
  end
37
-
37
+
38
38
  module ClassMethods
39
39
  # Update reflection klass by guid
40
40
  def fileupload_update(record_id, guid, method)
41
41
  query = fileupload_klass(method).where(:guid => guid, :assetable_type => base_class.name.to_s)
42
+ record_id = Moped::BSON::ObjectId.from_string(record_id) unless record_id.class.name == "Moped::BSON::ObjectId"
42
43
  query.update_all(:assetable_id => record_id, :guid => nil)
43
44
  end
44
-
45
+
45
46
  # Find asset by guid
46
47
  def fileupload_find(method, guid)
47
48
  klass = fileupload_klass(method)
48
- klass.where(:guid => guid).first
49
+ klass.where(:guid => guid)
49
50
  end
50
-
51
+
51
52
  # Find class by reflection
52
53
  def fileupload_klass(method)
53
54
  reflect_on_association(method.to_sym).klass
@@ -59,22 +60,22 @@ module Uploader
59
60
  end
60
61
  end
61
62
  end
62
-
63
+
63
64
  module InstanceMethods
64
65
  # Generate unique key
65
66
  def fileupload_guid
66
67
  @fileupload_guid ||= Uploader.guid
67
68
  end
68
-
69
+
69
70
  def fileupload_guid=(value)
70
71
  @fileupload_changed = true unless value.blank?
71
72
  @fileupload_guid = value.blank? ? nil : value
72
73
  end
73
-
74
+
74
75
  def fileupload_changed?
75
76
  @fileupload_changed === true
76
77
  end
77
-
78
+
78
79
  def fileupload_multiple?(method)
79
80
  association = self.class.reflect_on_association(method.to_sym)
80
81
  if association.respond_to?(:collection?)
@@ -83,8 +84,8 @@ module Uploader
83
84
  association.macro == :has_many
84
85
  end
85
86
  end
86
-
87
87
  # Find or build new asset object
88
+
88
89
  def fileupload_asset(method)
89
90
  if fileuploads_columns.include?(method.to_sym)
90
91
  asset = new_record? ? self.class.fileupload_find(method, fileupload_guid) : send(method)
@@ -92,13 +93,12 @@ module Uploader
92
93
  asset
93
94
  end
94
95
  end
95
-
96
+
96
97
  def fileuploads_columns
97
98
  self.class.fileuploads_columns
98
99
  end
99
-
100
+
100
101
  protected
101
-
102
102
  def fileuploads_update
103
103
  fileuploads_columns.each do |method|
104
104
  self.class.fileupload_update(id, fileupload_guid, method)
@@ -3,8 +3,8 @@ module Uploader
3
3
  class FieldTag
4
4
  attr_reader :template, :object, :theme
5
5
 
6
- delegate :uploader, :to => :template
7
-
6
+ delegate :uploader, :to => :template
7
+
8
8
  # Wrapper for render uploader field
9
9
  # Usage:
10
10
  #
@@ -13,12 +13,11 @@ module Uploader
13
13
  #
14
14
  def initialize(object_name, method_name, template, options = {}) #:nodoc:
15
15
  options = { :object_name => object_name, :method_name => method_name }.merge(options)
16
-
17
16
  @template, @options = template, options.dup
18
-
17
+
19
18
  @theme = (@options.delete(:theme) || "default")
20
19
  @value = @options.delete(:value) if @options.key?(:value)
21
-
20
+
22
21
  @object = @options.delete(:object) if @options.key?(:object)
23
22
  @object ||= @template.instance_variable_get("@#{object_name}")
24
23
  end
@@ -27,27 +26,27 @@ module Uploader
27
26
  locals = { :field => self }.merge(locals)
28
27
  @template.render :partial => "uploader/#{@theme}/container", :locals => locals
29
28
  end
30
-
29
+
31
30
  def id
32
31
  @id ||= @template.dom_id(@object, [method_name, 'uploader', @object.fileupload_guid].join('_'))
33
32
  end
34
-
33
+
35
34
  def method_name
36
35
  @options[:method_name]
37
36
  end
38
-
37
+
39
38
  def object_name
40
39
  @options[:object_name]
41
40
  end
42
-
41
+
43
42
  def multiple?
44
43
  @object.fileupload_multiple?(method_name)
45
44
  end
46
-
45
+
47
46
  def value
48
47
  @value ||= @object.fileupload_asset(method_name)
49
48
  end
50
-
49
+
51
50
  def values
52
51
  if !value.nil? && value.respond_to?(:first) && value.first.respond_to?(:sort)
53
52
  Array.wrap(value).sort_by(&:sort)
@@ -55,24 +54,24 @@ module Uploader
55
54
  Array.wrap(value)
56
55
  end
57
56
  end
58
-
57
+
59
58
  def exists?
60
59
  values.map(&:persisted?).any?
61
60
  end
62
-
61
+
63
62
  def klass
64
63
  @klass ||= @object.class.fileupload_klass(method_name)
65
64
  end
66
-
65
+
67
66
  def attachments_path(options = {})
68
67
  options = {
69
68
  :guid => @object.fileupload_guid,
70
69
  :assetable_type => @object.class.base_class.name.to_s,
71
70
  :klass => klass.to_s
72
71
  }.merge(options)
73
-
72
+
74
73
  options[:assetable_id] = @object.id if @object.persisted?
75
-
74
+
76
75
  uploader.attachments_path(options)
77
76
  end
78
77
 
@@ -87,7 +86,7 @@ module Uploader
87
86
 
88
87
  uploader.sort_attachments_path(options)
89
88
  end
90
-
89
+
91
90
  def input_html
92
91
  @input_html ||= {
93
92
  :"data-url" => attachments_path,
@@ -3,7 +3,7 @@ module Uploader
3
3
  module FormBuilder
4
4
  # Render uploader field
5
5
  # Usage:
6
- #
6
+ #
7
7
  # <%= form_for @article do |f| %>
8
8
  # <%= f.uploader_field :picture %>
9
9
  # <%= end %>
@@ -2,7 +2,7 @@ module Uploader
2
2
  module Helpers
3
3
  module FormTagHelper
4
4
  include ActionView::Helpers::JavaScriptHelper
5
-
5
+
6
6
  # A helper that renders file upload container
7
7
  #
8
8
  # <%= uploader_field_tag :article, :photo %>
@@ -7,8 +7,7 @@ class UploaderInput
7
7
 
8
8
  def to_html
9
9
  input_wrapping do
10
- label_html <<
11
- builder.uploader_field(method, input_html_options)
10
+ label_html << builder.uploader_field(method, input_html_options)
12
11
  end
13
12
  end
14
13
  end
@@ -1,28 +1,28 @@
1
- #require 'rails_admin/config/fields/base'
2
- module RailsAdmin::Config::Fields::Types
3
- class Uploader < RailsAdmin::Config::Fields::Types::Text
4
- RailsAdmin::Config::Fields::Types::register(:rails_uploader, self)
5
-
6
- register_instance_option(:config) do
7
- {}
8
- end
9
-
10
- register_instance_option(:partial) do
11
- :form_rails_uploader
12
- end
13
-
14
- def scope_type
15
- # Use association name if existent
16
- if bindings[:form].object_name.scan(/.*\[(.*)_attributes\]/).nil?
17
- bindings[:form].object_name
18
- else
19
- bindings[:form].object_name.scan(/.*\[(.*)_attributes\]/).to_s
20
- end
21
- end
22
-
23
- def scope_id
24
- bindings[:object].id
25
- end
26
-
27
- end
1
+ #require 'rails_admin/config/fields/base'
2
+ module RailsAdmin::Config::Fields::Types
3
+ class Uploader < RailsAdmin::Config::Fields::Types::Text
4
+ RailsAdmin::Config::Fields::Types::register(:rails_uploader, self)
5
+
6
+ register_instance_option(:config) do
7
+ {}
8
+ end
9
+
10
+ register_instance_option(:partial) do
11
+ :form_rails_uploader
12
+ end
13
+
14
+ def scope_type
15
+ # Use association name if existent
16
+ if bindings[:form].object_name.scan(/.*\[(.*)_attributes\]/).nil?
17
+ bindings[:form].object_name
18
+ else
19
+ bindings[:form].object_name.scan(/.*\[(.*)_attributes\]/).to_s
20
+ end
21
+ end
22
+
23
+ def scope_id
24
+ bindings[:object].id
25
+ end
26
+
27
+ end
28
28
  end