glebtv-rails-uploader 0.0.8 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -25,6 +25,9 @@ a little howto for mongoid / carrierwave:
25
25
  # DO NOT add this!
26
26
  # belongs_to :post
27
27
 
28
+ # optional built-in sorting for rails_admin
29
+ field :sort, type: Integer
30
+
28
31
  # field name must be 'data'
29
32
  mount_uploader :data, CoverUploader
30
33
 
@@ -50,6 +53,9 @@ a little howto for mongoid / carrierwave:
50
53
  # models/post.rb
51
54
  class Post
52
55
  include Mongoid::Document
56
+
57
+ field :fileupload_guid, type: String
58
+
53
59
  include Uploader::Fileuploads
54
60
  has_one :cover, as: :assetable
55
61
  fileuploads :cover
@@ -60,16 +66,6 @@ a little howto for mongoid / carrierwave:
60
66
 
61
67
  ```ruby
62
68
  class Album
63
- # optional built-in sorting for rails_admin
64
- def sort_assets!(order)
65
- pos = 0
66
- order.each do |pic_id|
67
- pic = self.pictures.find(pic_id)
68
- pic.sort = (pos+=1)
69
- pic.save!
70
- end
71
- end
72
-
73
69
  has_many :pictures, as: :assetable, dependent: :destroy
74
70
  fileuploads :pictures
75
71
 
@@ -78,6 +74,7 @@ class Album
78
74
  rails_admin do
79
75
  edit do
80
76
  ...
77
+ field :fileupload_guid, :hidden # this is needed or else rails_admin sanitizes it away
81
78
  field :pictures, :rails_uploader
82
79
  end
83
80
  end
@@ -110,6 +107,7 @@ end
110
107
  rails_admin do
111
108
  edit do
112
109
  ...
110
+ field :fileupload_guid, :hidden # this is needed or else rails_admin sanitizes it away
113
111
  field :pictures, :rails_uploader
114
112
  end
115
113
  end
@@ -5,9 +5,7 @@
5
5
  //= require uploader/jquery.fileupload
6
6
  //= require uploader/jquery.fileupload-ui
7
7
 
8
- $('.uploader-dnd-area input[type=file]').trigger("init.uploader");
9
-
10
- $('.uploader-files').each(function() {
8
+ $(document).off('init.sort').on('init.sort', '.uploader-files', function() {
11
9
  var $t = $(this);
12
10
  $t.sortable({
13
11
  update: function(event, ui) {
@@ -22,4 +20,6 @@ $('.uploader-files').each(function() {
22
20
  })
23
21
  }
24
22
  })
25
- });
23
+ });
24
+
25
+ $('.uploader-dnd-area input[type=file]').trigger("init.uploader");
@@ -19,10 +19,19 @@ module Uploader
19
19
 
20
20
  def sort
21
21
  @model = params[:assetable_type].safe_constantize
22
- @object = @model.find(params[:assetable_id])
23
22
 
24
- if @object.respond_to?(:sort_assets!)
25
- @object.sort_assets!(params[:sort].split('|'))
23
+ sort = params[:sort].split('|')
24
+ if params[:assetable_id].blank?
25
+ @finder = @klass.where(guid: params[:guid])
26
+ else
27
+ @finder = @klass.where(assetable_id: params[:assetable_id])
28
+ end
29
+
30
+ @finder.each do |asset|
31
+ if asset.respond_to?(:sort=)
32
+ asset.sort = sort.index(asset.id.to_s)
33
+ asset.save!
34
+ end
26
35
  end
27
36
 
28
37
  self.status = 200
@@ -60,6 +60,7 @@
60
60
  $(this).data("fileupload")
61
61
  ._renderDownload(<%=raw fv.to_json(:root => false) %>)
62
62
  .appendTo(container);
63
+ container.trigger('init.sort');
63
64
  <% end -%>
64
65
  }).trigger("init.uploader");
65
66
  });
@@ -1,4 +1,4 @@
1
- # lib/file_size_validator.rb
1
+ # file_size_validator.rb
2
2
  # Based on: https://gist.github.com/795665
3
3
 
4
4
  class FileSizeValidator < ActiveModel::EachValidator
@@ -15,7 +15,7 @@ module Uploader
15
15
  def uploader_create(params, request = nil)
16
16
  self.guid = params[:guid]
17
17
  self.assetable_type = params[:assetable_type]
18
- if self.class.respond_to?(:collection)
18
+ if self.class.respond_to?(:collection) && !params[:assetable_id].blank?
19
19
  self.assetable_id = Moped::BSON::ObjectId.from_string(params[:assetable_id])
20
20
  else
21
21
  self.assetable_id = params[:assetable_id]
@@ -27,7 +27,7 @@ module Uploader
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 }
@@ -49,7 +49,7 @@ module Uploader
49
49
  end
50
50
 
51
51
  def values
52
- if value.first.respond_to?(:sort)
52
+ if !value.nil? && value.first.respond_to?(:sort)
53
53
  Array.wrap(value).sort_by(&:sort)
54
54
  else
55
55
  Array.wrap(value)
@@ -1,3 +1,3 @@
1
1
  module Uploader
2
- VERSION = "0.0.8".freeze
2
+ VERSION = "0.1.1".freeze
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: glebtv-rails-uploader
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.8
4
+ version: 0.1.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: