rails_admin_multiple_file_upload 0.4.0.1 → 0.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/app/assets/javascripts/rails_admin/rails_admin_multiple_file_upload.coffee +36 -25
- data/app/views/rails_admin/main/multiple_file_upload.html.slim +2 -3
- data/app/views/rails_admin/main/multiple_file_upload_collection.html.slim +97 -0
- data/config/locales/ru.rails_admin_multiple_file_upload.yml +8 -0
- data/lib/rails_admin_multiple_file_upload/action.rb +84 -3
- data/lib/rails_admin_multiple_file_upload/configuration.rb +23 -0
- data/lib/rails_admin_multiple_file_upload/helper.rb +58 -4
- data/lib/rails_admin_multiple_file_upload/model.rb +3 -0
- data/lib/rails_admin_multiple_file_upload/version.rb +1 -1
- data/release.sh +1 -2
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 71baa0168d48e55231b5a83d645455aac98fc3b1
|
4
|
+
data.tar.gz: 4623d5dd0f1872fc9eed8a555ac393c7c0d12d81
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9065d7d7a1c5ebfca8b07b38f6635792344c20070fe4480d0cbbd59dd90811f22f9c30dd0d02f2fd0b796a15b954d19235fe4a778def714ce661583c92189201
|
7
|
+
data.tar.gz: 08a171131f5d5bc847a46753d3507e3b7b1ad137c5f339b75d455e3406a6b84571b6dcf8e74ab2f3a192943d072ed1544f7ab063e02fd90efec1ce6fc4160f61
|
@@ -24,28 +24,39 @@ link_regexp = /^\s*https?:\/\/[^\s]+\s*$/
|
|
24
24
|
|
25
25
|
if window.FileReader
|
26
26
|
window.setCopyAndPasteFor = (el)->
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
if items[i].
|
38
|
-
items[i].
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
27
|
+
unless el.has_clipboard_callback
|
28
|
+
_pasteCallback = (event)->
|
29
|
+
dropzone = $(el).find(".rails_admin_multiple_file_upload_block .dropzone:visible")
|
30
|
+
return if dropzone.length == 0
|
31
|
+
dropzone = dropzone[0].dropzone
|
32
|
+
|
33
|
+
items = (event.clipboardData or event.originalEvent.clipboardData).items
|
34
|
+
blob = null
|
35
|
+
i = 0
|
36
|
+
while i < items.length
|
37
|
+
if items[i].type.indexOf('image') == 0
|
38
|
+
blob = items[i].getAsFile()
|
39
|
+
|
40
|
+
else
|
41
|
+
if items[i].kind == "string"
|
42
|
+
items[i].getAsString (text)->
|
43
|
+
if link_regexp.test(text)
|
44
|
+
xhr = new XMLHttpRequest()
|
45
|
+
xhr.open("GET", text)
|
46
|
+
xhr.responseType = "blob"
|
47
|
+
xhr.onload = ()->
|
48
|
+
_blob = xhr.response
|
49
|
+
if _blob != null
|
50
|
+
dropzone.addFile(_blob)
|
51
|
+
xhr.send()
|
52
|
+
|
53
|
+
i++
|
54
|
+
if blob != null
|
55
|
+
dropzone.addFile(blob)
|
56
|
+
|
57
|
+
el.addEventListener 'paste', _pasteCallback
|
58
|
+
|
59
|
+
el.has_clipboard_callback = true
|
60
|
+
|
61
|
+
|
62
|
+
window.setCopyAndPasteFor(document.body)
|
@@ -0,0 +1,97 @@
|
|
1
|
+
= stylesheet_link_tag 'rails_admin/rails_admin_multiple_file_upload'
|
2
|
+
= javascript_include_tag 'rails_admin/rails_admin_multiple_file_upload'
|
3
|
+
|
4
|
+
.rails_admin_multiple_file_upload_block
|
5
|
+
.controls
|
6
|
+
ul.nav.nav-tabs
|
7
|
+
- if multiple_file_upload_fields.blank?
|
8
|
+
ruby:
|
9
|
+
upload_field = "image"
|
10
|
+
i18n_path = "rails_admin.multiple_file_upload_collection.#{@model.name.tableize}.#{upload_field}"
|
11
|
+
i18n_path_default = "rails_admin.multiple_file_upload_collection.#{@model.name.tableize}"
|
12
|
+
|
13
|
+
link = "#multiple_file_upload_collection_#{@model.name.tableize}_#{upload_field}"
|
14
|
+
title = I18n.t(i18n_path, default: I18n.t(i18n_path_default))
|
15
|
+
li
|
16
|
+
= link_to I18n.t(i18n_path, default: I18n.t(i18n_path_default)), link, title: title, "data-toggle" => "tab"
|
17
|
+
|
18
|
+
- else
|
19
|
+
- multiple_file_upload_fields.each do |f|
|
20
|
+
ruby:
|
21
|
+
upload_field = f
|
22
|
+
|
23
|
+
i18n_path = "rails_admin.multiple_file_upload_collection.#{@model.name.tableize}.#{upload_field}"
|
24
|
+
i18n_path_default = "rails_admin.multiple_file_upload_collection.#{@model.name.tableize}"
|
25
|
+
|
26
|
+
link = "#multiple_file_upload_collection_#{@model.name.tableize}_#{upload_field}"
|
27
|
+
title = I18n.t(i18n_path, default: I18n.t(i18n_path_default))
|
28
|
+
li
|
29
|
+
= link_to I18n.t(i18n_path, default: I18n.t(i18n_path_default)), link, title: title, "data-toggle" => "tab"
|
30
|
+
|
31
|
+
.tab-content
|
32
|
+
- if multiple_file_upload_fields.blank?
|
33
|
+
- upload_field = "image"
|
34
|
+
- if @model.respond_to?("sorted")
|
35
|
+
- objects = @model.sorted.to_a
|
36
|
+
- elsif child_objects.respond_to?("ordered")
|
37
|
+
- objects = @model.ordered.to_a
|
38
|
+
-else
|
39
|
+
- objects = @model.all.to_a
|
40
|
+
|
41
|
+
.tab-pane{id="multiple_file_upload_collection_#{@model.name.tableize}_#{upload_field}"}
|
42
|
+
.row-fluid
|
43
|
+
- block_id = "rails_admin_multiple_upload_collection_#{@model.name.tableize}_#{upload_field}"
|
44
|
+
- button_id = "rails_admin_multiple_upload_collection_#{@model.name.tableize}_#{upload_field}_button"
|
45
|
+
.span12.rails_admin_multiple_upload_collection{id="#{block_id}"}
|
46
|
+
= rails_admin_multiple_file_upload_collection objects, {upload_field: upload_field}
|
47
|
+
|
48
|
+
= button_tag "Загрузить", id: button_id
|
49
|
+
javascript:
|
50
|
+
$("##{block_id} .dropzone").dropzone({
|
51
|
+
paramName: "#{upload_field}",
|
52
|
+
autoProcessQueue: false,
|
53
|
+
addRemoveLinks: true
|
54
|
+
});
|
55
|
+
$("##{block_id} .dropzone")[0].dropzone.on('success', function(){
|
56
|
+
$("##{button_id}").click();
|
57
|
+
});
|
58
|
+
$("##{button_id}").click(function(e){
|
59
|
+
e.preventDefault();
|
60
|
+
$("##{block_id} .dropzone")[0].dropzone.processQueue();
|
61
|
+
return false
|
62
|
+
});
|
63
|
+
//setCopyAndPasteFor($("##{block_id}")[0]);
|
64
|
+
|
65
|
+
- else
|
66
|
+
- multiple_file_upload_fields.each do |f|
|
67
|
+
- upload_field = f
|
68
|
+
- if @model.respond_to?("sorted")
|
69
|
+
- objects = @model.sorted.to_a
|
70
|
+
- elsif child_objects.respond_to?("ordered")
|
71
|
+
- objects = @model.ordered.to_a
|
72
|
+
-else
|
73
|
+
- objects = @model.all.to_a
|
74
|
+
|
75
|
+
.tab-pane{id="rails_admin_multiple_upload_collection_#{@model.name.tableize}_#{upload_field}"}
|
76
|
+
.row-fluid
|
77
|
+
- block_id = "rails_admin_multiple_upload_collection_#{@model.name.tableize}_#{upload_field}"
|
78
|
+
- button_id = "rails_admin_multiple_upload_collection_#{@model.name.tableize}_#{upload_field}_button"
|
79
|
+
.span12.rails_admin_multiple_upload{id="#{block_id}"}
|
80
|
+
= rails_admin_multiple_file_upload_collection objects, {upload_field: upload_field}
|
81
|
+
|
82
|
+
= button_tag "Загрузить", id: button_id
|
83
|
+
javascript:
|
84
|
+
$("##{block_id} .dropzone").dropzone({
|
85
|
+
paramName: "#{upload_field}",
|
86
|
+
autoProcessQueue: false,
|
87
|
+
addRemoveLinks: true
|
88
|
+
});
|
89
|
+
$("##{block_id} .dropzone")[0].dropzone.on('success', function(){
|
90
|
+
$("##{button_id}").click();
|
91
|
+
});
|
92
|
+
$("##{button_id}").click(function(e){
|
93
|
+
e.preventDefault();
|
94
|
+
$("##{block_id} .dropzone")[0].dropzone.processQueue();
|
95
|
+
return false
|
96
|
+
});
|
97
|
+
//setCopyAndPasteFor($("##{block_id}")[0]);
|
@@ -8,3 +8,11 @@ ru:
|
|
8
8
|
save_error: "Ошибка при сохранении"
|
9
9
|
success: "Успешно"
|
10
10
|
title: "Загрузка файлов"
|
11
|
+
|
12
|
+
multiple_file_upload_collection:
|
13
|
+
menu: "Загрузка файлов"
|
14
|
+
breadcrumb: "Загрузка файлов"
|
15
|
+
error: "Ошибка"
|
16
|
+
save_error: "Ошибка при сохранении"
|
17
|
+
success: "Успешно"
|
18
|
+
title: "Загрузка файлов"
|
@@ -42,13 +42,16 @@ module RailsAdmin
|
|
42
42
|
|
43
43
|
_file = params[child_field][child_model_upload_field]
|
44
44
|
if ["undefined", "blob", '', nil].include?(_file.original_filename)
|
45
|
-
ext = _file.content_type.split("/").last
|
46
|
-
_file.original_filename = "#{(Time.new.to_f*1_000_000).to_i}
|
45
|
+
ext = ".#{_file.content_type.split("/").last}"
|
46
|
+
_file.original_filename = "#{(Time.new.to_f*1_000_000).to_i}#{ext}"
|
47
|
+
else
|
48
|
+
ext = File.extname(_file.original_filename)
|
47
49
|
end
|
48
50
|
|
49
51
|
main_obj = @object
|
50
52
|
child = main_obj.send(child_field).new
|
51
|
-
child.
|
53
|
+
child.name = File.basename(_file.original_filename, ext) if @conf.options[:name_from_filename]
|
54
|
+
child.send(child_model_upload_field + "=", _file)
|
52
55
|
if child.save
|
53
56
|
message = "<strong>#{I18n.t('admin.actions.multiple_file_upload.success')}!</strong>"
|
54
57
|
else
|
@@ -76,3 +79,81 @@ module RailsAdmin
|
|
76
79
|
end
|
77
80
|
end
|
78
81
|
end
|
82
|
+
|
83
|
+
|
84
|
+
module RailsAdmin
|
85
|
+
module Config
|
86
|
+
module Actions
|
87
|
+
class MultipleFileUploadCollection < Base
|
88
|
+
RailsAdmin::Config::Actions.register(self)
|
89
|
+
|
90
|
+
# Is the action acting on the root level (Example: /admin/contact)
|
91
|
+
register_instance_option :root? do
|
92
|
+
false
|
93
|
+
end
|
94
|
+
|
95
|
+
register_instance_option :collection? do
|
96
|
+
true
|
97
|
+
end
|
98
|
+
|
99
|
+
# Is the action on an object scope (Example: /admin/team/1/edit)
|
100
|
+
register_instance_option :member? do
|
101
|
+
false
|
102
|
+
end
|
103
|
+
|
104
|
+
register_instance_option :route_fragment do
|
105
|
+
'multiple_file_upload_collection'
|
106
|
+
end
|
107
|
+
|
108
|
+
register_instance_option :controller do
|
109
|
+
Proc.new do |klass|
|
110
|
+
@conf = ::RailsAdminMultipleFileUploadCollection::Configuration.new @abstract_model
|
111
|
+
@model = @abstract_model.model
|
112
|
+
|
113
|
+
if request.get?
|
114
|
+
# @nodes = list_entries(@model_config, :index, nil, nil).sort { |a,b| a.lft <=> b.lft }
|
115
|
+
@files = []
|
116
|
+
render action: @action.template_name
|
117
|
+
|
118
|
+
elsif request.post?
|
119
|
+
begin
|
120
|
+
upload_field = params[:upload_field].to_s
|
121
|
+
upload_field = "image" if upload_field.blank?
|
122
|
+
|
123
|
+
_file = params[upload_field]
|
124
|
+
if ["undefined", "blob", '', nil].include?(_file.original_filename)
|
125
|
+
ext = ".#{_file.content_type.split("/").last}"
|
126
|
+
_file.original_filename = "#{(Time.new.to_f*1_000_000).to_i}#{ext}"
|
127
|
+
else
|
128
|
+
ext = File.extname(_file.original_filename)
|
129
|
+
end
|
130
|
+
|
131
|
+
@object = @model.new
|
132
|
+
@object.name = File.basename(_file.original_filename, ext) if @conf.options[:name_from_filename]
|
133
|
+
@object.send(upload_field + "=", _file)
|
134
|
+
if @object.save
|
135
|
+
message = "<strong>#{I18n.t('admin.actions.multiple_file_upload_collection.success')}!</strong>"
|
136
|
+
else
|
137
|
+
message = "<strong>#{I18n.t('admin.actions.multiple_file_upload_collection.save_error')}</strong>: #{@object.errors.full_messages}"
|
138
|
+
end
|
139
|
+
|
140
|
+
rescue Exception => e
|
141
|
+
message = "<strong>#{I18n.t('admin.actions.multiple_file_upload_collection.error')}</strong>: #{e}"
|
142
|
+
end
|
143
|
+
|
144
|
+
render text: message
|
145
|
+
end
|
146
|
+
end
|
147
|
+
end
|
148
|
+
|
149
|
+
register_instance_option :link_icon do
|
150
|
+
'icon-upload'
|
151
|
+
end
|
152
|
+
|
153
|
+
register_instance_option :http_methods do
|
154
|
+
[:get, :post]
|
155
|
+
end
|
156
|
+
end
|
157
|
+
end
|
158
|
+
end
|
159
|
+
end
|
@@ -9,6 +9,7 @@ module RailsAdminMultipleFileUpload
|
|
9
9
|
fields: [{}],
|
10
10
|
thumbnail_size: :thumb,
|
11
11
|
upload_gem: :paperclip,
|
12
|
+
name_from_filename: true
|
12
13
|
}.merge(config || {})
|
13
14
|
end
|
14
15
|
|
@@ -18,3 +19,25 @@ module RailsAdminMultipleFileUpload
|
|
18
19
|
end
|
19
20
|
end
|
20
21
|
end
|
22
|
+
|
23
|
+
module RailsAdminMultipleFileUploadCollection
|
24
|
+
class Configuration
|
25
|
+
def initialize(abstract_model)
|
26
|
+
@abstract_model = abstract_model
|
27
|
+
end
|
28
|
+
|
29
|
+
def options
|
30
|
+
@options ||= {
|
31
|
+
fields: [],
|
32
|
+
thumbnail_size: :thumb,
|
33
|
+
upload_gem: :paperclip,
|
34
|
+
name_from_filename: true
|
35
|
+
}.merge(config || {})
|
36
|
+
end
|
37
|
+
|
38
|
+
protected
|
39
|
+
def config
|
40
|
+
::RailsAdmin::Config.model(@abstract_model.model).multiple_file_upload_collection || {}
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -1,7 +1,7 @@
|
|
1
1
|
module RailsAdminMultipleFileUpload
|
2
2
|
module Helper
|
3
3
|
def rails_admin_multiple_file_upload(files, opts= {})
|
4
|
-
# backward compatibility
|
4
|
+
# backward compatibility
|
5
5
|
opts[:child_field] ||= opts[:embedded_field]
|
6
6
|
opts[:child_model_order_field] ||= opts[:embedded_model_order_field]
|
7
7
|
opts[:child_model_upload_field] ||= opts[:embedded_model_upload_field]
|
@@ -28,7 +28,7 @@ module RailsAdminMultipleFileUpload
|
|
28
28
|
def rails_admin_multiple_file_upload_builder(files, config)
|
29
29
|
ret = []
|
30
30
|
_ret = []
|
31
|
-
|
31
|
+
files.each do |ef|
|
32
32
|
if multiple_file_upload_paperclip?
|
33
33
|
if ef.send(config[:child_model_upload_field] + "_content_type") =~ /\Aimage/
|
34
34
|
file_url = ef.send(config[:child_model_upload_field]).url(multiple_file_upload_thumbnail_size)
|
@@ -37,9 +37,8 @@ module RailsAdminMultipleFileUpload
|
|
37
37
|
else
|
38
38
|
if ef.respond_to?(:name)
|
39
39
|
file_name = ef.name
|
40
|
-
else
|
41
|
-
file_name = ef.send(config[:child_model_upload_field] + "_file_name")
|
42
40
|
end
|
41
|
+
file_name = ef.send(config[:child_model_upload_field] + "_file_name") if file_name.blank?
|
43
42
|
_ret << content_tag(:span, link_to(ef.name), class: "file_block_load_already")
|
44
43
|
end
|
45
44
|
end
|
@@ -63,6 +62,57 @@ module RailsAdminMultipleFileUpload
|
|
63
62
|
|
64
63
|
|
65
64
|
|
65
|
+
|
66
|
+
def rails_admin_multiple_file_upload_collection(files, opts= {})
|
67
|
+
|
68
|
+
id = "ns_#{rand(100_000_000..999_999_999)}"
|
69
|
+
config = {
|
70
|
+
update_url: multiple_file_upload_collection_path(model_name: @abstract_model),
|
71
|
+
upload_field: opts[:upload_field]
|
72
|
+
}
|
73
|
+
content_tag(:div, rails_admin_multiple_file_upload_collection_builder(files, config), id: id, class: 'rails_admin_multiple_file_upload_collection')
|
74
|
+
end
|
75
|
+
|
76
|
+
def rails_admin_multiple_file_upload_collection_builder(files, config)
|
77
|
+
ret = []
|
78
|
+
_ret = []
|
79
|
+
_upload_field = (config[:upload_field].blank? ? "image" : config[:upload_field])
|
80
|
+
files.each do |ef|
|
81
|
+
if multiple_file_upload_paperclip?
|
82
|
+
if ef.send(_upload_field + "_content_type") =~ /\Aimage/
|
83
|
+
file_url = ef.send(_upload_field).url(multiple_file_upload_thumbnail_size)
|
84
|
+
_ret << content_tag(:span, image_tag(file_url), class: "file_block_load_already")
|
85
|
+
|
86
|
+
else
|
87
|
+
if ef.respond_to?(:name)
|
88
|
+
file_name = ef.name
|
89
|
+
end
|
90
|
+
file_name = ef.send(_upload_field + "_file_name") if file_name.blank?
|
91
|
+
_ret << content_tag(:span, link_to(ef.name), class: "file_block_load_already")
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
ret << content_tag(:div, _ret.join.html_safe)
|
96
|
+
ret << content_tag(:div, "", class: "clearfix")
|
97
|
+
@object = @abstract_model.model.new
|
98
|
+
ret << rails_admin_form_for(@object, url: config[:update_url], html: {method: :post, multipart: true, class: "form-horizontal denser dropzone"})do |f|
|
99
|
+
_ret = []
|
100
|
+
_ret << hidden_field_tag(:upload_field, _upload_field)
|
101
|
+
_ret.join.html_safe
|
102
|
+
# f.fields_for config[:child_field] do |ef|
|
103
|
+
# @object.send(config[:child_field]).each do |field|
|
104
|
+
# ef.input_for field
|
105
|
+
# end
|
106
|
+
# end
|
107
|
+
# f.submit
|
108
|
+
end
|
109
|
+
ret.join.html_safe
|
110
|
+
end
|
111
|
+
|
112
|
+
|
113
|
+
|
114
|
+
|
115
|
+
|
66
116
|
def multiple_file_upload_fields
|
67
117
|
@conf.options[:fields]
|
68
118
|
end
|
@@ -77,5 +127,9 @@ module RailsAdminMultipleFileUpload
|
|
77
127
|
@conf.options[:thumbnail_size]
|
78
128
|
end
|
79
129
|
|
130
|
+
def set_name_from_filename?
|
131
|
+
@conf.options[:name_from_filename]
|
132
|
+
end
|
133
|
+
|
80
134
|
end
|
81
135
|
end
|
data/release.sh
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rails_admin_multiple_file_upload
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
4
|
+
version: 0.4.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alexander Kiseliev
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-08-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -83,6 +83,7 @@ files:
|
|
83
83
|
- app/assets/javascripts/rails_admin/rails_admin_multiple_file_upload.coffee
|
84
84
|
- app/assets/stylesheets/rails_admin/rails_admin_multiple_file_upload.sass
|
85
85
|
- app/views/rails_admin/main/multiple_file_upload.html.slim
|
86
|
+
- app/views/rails_admin/main/multiple_file_upload_collection.html.slim
|
86
87
|
- config/locales/ru.rails_admin_multiple_file_upload.yml
|
87
88
|
- lib/rails_admin_multiple_file_upload.rb
|
88
89
|
- lib/rails_admin_multiple_file_upload/action.rb
|