attache_rails 0.1.0 → 0.1.2
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
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 71bdf794a1a45cfb4a1c5a1f009a6b0986c2f036
|
4
|
+
data.tar.gz: aa0fd8c6977c2e78ae9114d3e53aa9a24ac3972e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f83391852c79448f278812211b2fb9dbc9f61302da3049e3e9f1a80e346c515c0bcbe4ab4fd12e859f2d0df9c246c3692d296bd320268a3a613a36f3d3afcee0
|
7
|
+
data.tar.gz: d6a3d6c02e9d3aaf54beb9567da08238b91464270d4daf4edd8ce6602c4d279d8284e88f836e309b166135e221e22f67355190af8919356dfdb4deddc88fd194
|
@@ -24,7 +24,7 @@ var AttacheFileInput = React.createClass({displayName: "AttacheFileInput",
|
|
24
24
|
onChange: function() {
|
25
25
|
var file_element = this.getDOMNode().firstChild;
|
26
26
|
// user cancelled file chooser dialog. ignore
|
27
|
-
if (file_element.files.length == 0) return;
|
27
|
+
if (! file_element || file_element.files.length == 0) return;
|
28
28
|
if (! this.props.multiple) this.state.files = {};
|
29
29
|
|
30
30
|
this.setState(this.state);
|
@@ -24,7 +24,7 @@ var AttacheFileInput = React.createClass({
|
|
24
24
|
onChange: function() {
|
25
25
|
var file_element = this.getDOMNode().firstChild;
|
26
26
|
// user cancelled file chooser dialog. ignore
|
27
|
-
if (file_element.files.length == 0) return;
|
27
|
+
if (! file_element || file_element.files.length == 0) return;
|
28
28
|
if (! this.props.multiple) this.state.files = {};
|
29
29
|
|
30
30
|
this.setState(this.state);
|
data/lib/attache_rails/model.rb
CHANGED
@@ -1,9 +1,18 @@
|
|
1
|
-
require "
|
1
|
+
require "cgi"
|
2
2
|
require "uri"
|
3
|
+
require "httpclient"
|
3
4
|
|
4
5
|
module AttacheRails
|
5
6
|
module Utils
|
6
7
|
class << self
|
8
|
+
def attache_upload_and_get_json(readable)
|
9
|
+
uri = URI.parse(ATTACHE_UPLOAD_URL)
|
10
|
+
uri.query = { file: (readable.try(:original_filename) || 'noname'), **attache_auth_options }.collect {|k,v|
|
11
|
+
CGI.escape(k.to_s) + "=" + CGI.escape(v.to_s)
|
12
|
+
}.join('&')
|
13
|
+
HTTPClient.post(uri, readable, {'Content-Type' => 'binary/octet-stream'}).body
|
14
|
+
end
|
15
|
+
|
7
16
|
def attache_url_for(json_string, geometry)
|
8
17
|
JSON.parse(json_string).tap do |attrs|
|
9
18
|
prefix, basename = File.split(attrs['path'])
|
@@ -47,13 +56,18 @@ module AttacheRails
|
|
47
56
|
end
|
48
57
|
|
49
58
|
def attaches_discard!(files = attaches_discarded)
|
59
|
+
files.reject! &:blank?
|
60
|
+
files.uniq!
|
50
61
|
if files.present?
|
51
62
|
logger.info "DELETE #{files.inspect}"
|
52
|
-
|
63
|
+
HTTPClient.post_content(
|
53
64
|
URI.parse(ATTACHE_DELETE_URL),
|
54
65
|
Utils.attache_auth_options.merge(paths: files.join("\n"))
|
55
66
|
)
|
56
67
|
end
|
68
|
+
rescue Exception
|
69
|
+
raise if ENV['ATTACHE_DISCARD_FAILURE_RAISE_ERROR']
|
70
|
+
logger.warn [$!, $@]
|
57
71
|
end
|
58
72
|
|
59
73
|
module ClassMethods
|
@@ -62,6 +76,18 @@ module AttacheRails
|
|
62
76
|
define_method "#{name}_options", -> (geometry, options = {}) { Utils.attache_options(geometry, [self.send("#{name}_attributes", geometry)], multiple: false, **options) }
|
63
77
|
define_method "#{name}_url", -> (geometry) { self.send("#{name}_attributes", geometry).try(:[], 'url') }
|
64
78
|
define_method "#{name}_attributes", -> (geometry) { str = self.send(name); Utils.attache_url_for(str, geometry) if str; }
|
79
|
+
define_method "#{name}=", -> (value) {
|
80
|
+
new_value = (value.respond_to?(:read) ? Utils.attache_upload_and_get_json(value) : value)
|
81
|
+
super(new_value)
|
82
|
+
}
|
83
|
+
define_method "#{name}_discard_was",-> do
|
84
|
+
new_value = self.send("#{name}")
|
85
|
+
old_value = self.send("#{name}_was")
|
86
|
+
obsoleted = [*old_value].collect {|x| JSON.parse(x)['path'] } - [*new_value].collect {|x| JSON.parse(x)['path'] }
|
87
|
+
self.attaches_discarded ||= []
|
88
|
+
self.attaches_discarded.push(*obsoleted)
|
89
|
+
end
|
90
|
+
after_update "#{name}_discard_was"
|
65
91
|
define_method "#{name}_discard", -> do
|
66
92
|
self.attaches_discarded ||= []
|
67
93
|
self.attaches_discarded.push(self.send("#{name}_attributes", 'original')['path'])
|
@@ -78,6 +104,18 @@ module AttacheRails
|
|
78
104
|
sum + (str.blank? ? [] : [Utils.attache_url_for(str, geometry)])
|
79
105
|
end
|
80
106
|
}
|
107
|
+
define_method "#{name}=", -> (array) {
|
108
|
+
new_value = ((array || []).reject(&:blank?).collect {|value| value.respond_to?(:read) ? Utils.attache_upload_and_get_json(value) : value })
|
109
|
+
super(new_value)
|
110
|
+
}
|
111
|
+
define_method "#{name}_discard_was",-> do
|
112
|
+
new_value = self.send("#{name}")
|
113
|
+
old_value = [*self.send("#{name}_was")]
|
114
|
+
obsoleted = old_value.collect {|x| JSON.parse(x)['path'] } - new_value.collect {|x| JSON.parse(x)['path'] }
|
115
|
+
self.attaches_discarded ||= []
|
116
|
+
obsoleted.each {|path| self.attaches_discarded.push(path) }
|
117
|
+
end
|
118
|
+
after_update "#{name}_discard_was"
|
81
119
|
define_method "#{name}_discard", -> do
|
82
120
|
self.attaches_discarded ||= []
|
83
121
|
self.send("#{name}_attributes", 'original').each {|attrs| self.attaches_discarded.push(attrs['path']) }
|
metadata
CHANGED
@@ -1,15 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: attache_rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- choonkeat
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
12
|
-
dependencies:
|
11
|
+
date: 2015-04-01 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: httpclient
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
13
27
|
description:
|
14
28
|
email:
|
15
29
|
- choonkeat@gmail.com
|