sequel_paperclip 0.3.0 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/sequel_paperclip/attachment.rb +16 -18
- data/lib/sequel_paperclip.rb +25 -38
- data/sequel_paperclip.gemspec +1 -1
- metadata +2 -2
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.4.0
|
@@ -40,36 +40,34 @@ module Sequel
|
|
40
40
|
@storage_updates = []
|
41
41
|
end
|
42
42
|
|
43
|
-
def
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
:path => path(style),
|
57
|
-
}
|
43
|
+
def update(file)
|
44
|
+
if file
|
45
|
+
unless file.is_a?(File) || file.is_a?(Tempfile)
|
46
|
+
raise ArgumentError, "#{name}: #{file} is not a File"
|
47
|
+
end
|
48
|
+
else
|
49
|
+
if exists?
|
50
|
+
options[:styles].each_pair do |style, style_options|
|
51
|
+
@storage_updates << {
|
52
|
+
:type => STORAGE_UPDATE_DELETE,
|
53
|
+
:path => path(style),
|
54
|
+
}
|
55
|
+
end
|
58
56
|
end
|
59
57
|
end
|
60
58
|
|
61
|
-
@queued_file =
|
59
|
+
@queued_file = file
|
62
60
|
end
|
63
61
|
|
64
62
|
def exists?
|
65
63
|
!!model.send("#{name}_basename")
|
66
64
|
end
|
67
65
|
|
68
|
-
def path(style)
|
66
|
+
def path(style = :original)
|
69
67
|
Interpolations.interpolate(options[:path], self, model, style)
|
70
68
|
end
|
71
69
|
|
72
|
-
def url(style)
|
70
|
+
def url(style = :original)
|
73
71
|
Interpolations.interpolate(options[:url], self, model, style)
|
74
72
|
end
|
75
73
|
|
data/lib/sequel_paperclip.rb
CHANGED
@@ -8,13 +8,11 @@ module Sequel
|
|
8
8
|
module Plugins
|
9
9
|
module Paperclip
|
10
10
|
def self.apply(model, opts={}, &block)
|
11
|
-
end
|
12
|
-
|
13
|
-
def self.configure(model, opts={}, &block)
|
14
11
|
model.class_inheritable_hash :attachments
|
15
12
|
model.attachments = {}
|
13
|
+
end
|
16
14
|
|
17
|
-
|
15
|
+
def self.configure(model, opts={}, &block)
|
18
16
|
end
|
19
17
|
|
20
18
|
module ClassMethods
|
@@ -65,25 +63,21 @@ module Sequel
|
|
65
63
|
end
|
66
64
|
end
|
67
65
|
|
68
|
-
define_method("#{name}_attachment_instance") do
|
69
|
-
self.attachment_instances ||= {}
|
70
|
-
self.attachment_instances[name] ||= Attachment.new(self, name, options)
|
71
|
-
end
|
72
|
-
|
73
66
|
define_method("#{name}") do
|
74
|
-
|
75
|
-
|
67
|
+
@attachment_instances ||= {}
|
68
|
+
@attachment_instances[name] ||= Attachment.new(self, name, options)
|
76
69
|
end
|
77
70
|
|
78
71
|
define_method("#{name}=") do |value|
|
79
|
-
attachment = send("#{name}
|
72
|
+
attachment = send("#{name}")
|
73
|
+
|
74
|
+
# queue destroy attachment, so all old files get deleted properly even
|
75
|
+
# if the basename/ filename changes
|
76
|
+
attachment.update(nil)
|
80
77
|
|
81
78
|
if value
|
82
|
-
basename =
|
83
|
-
|
84
|
-
basename = ActiveSupport::SecureRandom.hex(4).to_s
|
85
|
-
send("#{name}_basename=", basename)
|
86
|
-
end
|
79
|
+
basename = attachment_generate_basename(attachment)
|
80
|
+
send("#{name}_basename=", basename)
|
87
81
|
|
88
82
|
if respond_to?("#{name}_filename")
|
89
83
|
send("#{name}_filename=", basename+File.extname(file.original_filename).downcase)
|
@@ -96,39 +90,33 @@ module Sequel
|
|
96
90
|
if respond_to?("#{name}_originalname")
|
97
91
|
send("#{name}_originalname=", file.original_filename)
|
98
92
|
end
|
99
|
-
|
100
|
-
attachment.set(value)
|
101
93
|
else
|
102
|
-
attachment.destroy
|
103
|
-
|
104
94
|
send("#{name}_basename=", nil)
|
105
95
|
end
|
106
96
|
|
97
|
+
# now queue the real update
|
98
|
+
attachment.update(value)
|
99
|
+
|
107
100
|
# force sequel to call the hooks
|
108
101
|
modified!
|
109
102
|
end
|
110
103
|
|
111
104
|
define_method("#{name}?") do
|
112
|
-
attachment = send("#{name}
|
105
|
+
attachment = send("#{name}")
|
113
106
|
attachment.exists?
|
114
107
|
end
|
115
|
-
|
116
|
-
define_method("#{name}_url") do |style|
|
117
|
-
attachment = send("#{name}_attachment_instance")
|
118
|
-
attachment.url(style)
|
119
|
-
end
|
120
|
-
|
121
|
-
define_method("#{name}_path") do |style|
|
122
|
-
attachment = send("#{name}_attachment_instance")
|
123
|
-
attachment.path(style)
|
124
|
-
end
|
125
108
|
end
|
126
109
|
end
|
127
110
|
|
128
111
|
module InstanceMethods
|
112
|
+
def attachment_generate_basename(attachment)
|
113
|
+
basename = send("#{attachment.name}_basename")
|
114
|
+
basename.blank? ? ActiveSupport::SecureRandom.hex(4).to_s : basename
|
115
|
+
end
|
116
|
+
|
129
117
|
def after_save
|
130
|
-
if attachment_instances
|
131
|
-
attachment_instances.each_value do |attachment|
|
118
|
+
if @attachment_instances
|
119
|
+
@attachment_instances.each_value do |attachment|
|
132
120
|
attachment.process
|
133
121
|
attachment.update_storage
|
134
122
|
end
|
@@ -138,12 +126,11 @@ module Sequel
|
|
138
126
|
|
139
127
|
def after_destroy
|
140
128
|
self.class.attachments.each_key do |name|
|
141
|
-
|
142
|
-
attachment.destroy
|
129
|
+
send("#{name}=", nil)
|
143
130
|
end
|
144
131
|
|
145
|
-
if attachment_instances
|
146
|
-
attachment_instances.each_value do |attachment|
|
132
|
+
if @attachment_instances
|
133
|
+
@attachment_instances.each_value do |attachment|
|
147
134
|
attachment.update_storage
|
148
135
|
end
|
149
136
|
end
|
data/sequel_paperclip.gemspec
CHANGED