sequel_paperclip 0.2.0 → 0.3.0
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.
- data/VERSION +1 -1
- data/lib/sequel_paperclip/attachment.rb +51 -34
- data/lib/sequel_paperclip.rb +60 -48
- data/sequel_paperclip.gemspec +2 -2
- metadata +3 -3
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.3.0
|
@@ -2,14 +2,15 @@ module Sequel
|
|
2
2
|
module Plugins
|
3
3
|
module Paperclip
|
4
4
|
class Attachment
|
5
|
+
attr_reader :model
|
5
6
|
attr_reader :name
|
6
7
|
attr_reader :options
|
7
|
-
|
8
|
+
attr_reader :queued_file
|
8
9
|
|
9
10
|
STORAGE_UPDATE_SAVE = 1
|
10
11
|
STORAGE_UPDATE_DELETE = 2
|
11
|
-
|
12
|
-
def
|
12
|
+
|
13
|
+
def self.preprocess_options(options = {})
|
13
14
|
unless options[:styles]
|
14
15
|
options[:styles] = {
|
15
16
|
:original => {}
|
@@ -24,58 +25,74 @@ module Sequel
|
|
24
25
|
]
|
25
26
|
end
|
26
27
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
self.processors << klass.constantize.new(self, processor)
|
28
|
+
options[:processors].each_with_index do |processor, i|
|
29
|
+
if processor.is_a?(Hash)
|
30
|
+
klass = "Sequel::Plugins::Paperclip::Processors::#{processor[:type].to_s.capitalize}"
|
31
|
+
options[:processors][i] = klass.constantize.new(self, processor)
|
32
|
+
end
|
33
33
|
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def initialize(model, name, preprocessed_options)
|
37
|
+
@model = model
|
38
|
+
@name = name
|
39
|
+
@options = preprocessed_options
|
34
40
|
@storage_updates = []
|
35
41
|
end
|
36
42
|
|
37
|
-
def
|
38
|
-
|
39
|
-
|
43
|
+
def set(file)
|
44
|
+
unless file.is_a?(File) || file.is_a?(Tempfile)
|
45
|
+
raise ArgumentError, "#{name}: #{file} is not a File"
|
46
|
+
end
|
47
|
+
|
48
|
+
@queued_file = file
|
49
|
+
end
|
50
|
+
|
51
|
+
def destroy
|
52
|
+
if exists?
|
40
53
|
options[:styles].each_pair do |style, style_options|
|
41
|
-
tmp_file = Tempfile.new("paperclip")
|
42
|
-
puts "processing #{name} for style #{style} with processor #{processor.name}"
|
43
|
-
processor.run(style, style_options, tmp_file)
|
44
54
|
@storage_updates << {
|
45
|
-
:type =>
|
46
|
-
:
|
47
|
-
:dst_path => path(model, style),
|
55
|
+
:type => STORAGE_UPDATE_DELETE,
|
56
|
+
:path => path(style),
|
48
57
|
}
|
49
58
|
end
|
50
|
-
processor.post_runs
|
51
59
|
end
|
52
|
-
end
|
53
60
|
|
54
|
-
|
55
|
-
return unless exists?(model)
|
56
|
-
|
57
|
-
options[:styles].each_pair do |style, style_options|
|
58
|
-
@storage_updates << {
|
59
|
-
:type => STORAGE_UPDATE_DELETE,
|
60
|
-
:path => path(model, style),
|
61
|
-
}
|
62
|
-
end
|
63
|
-
model.send("#{name}_basename=", nil)
|
61
|
+
@queued_file = nil
|
64
62
|
end
|
65
63
|
|
66
|
-
def exists?
|
64
|
+
def exists?
|
67
65
|
!!model.send("#{name}_basename")
|
68
66
|
end
|
69
67
|
|
70
|
-
def path(
|
68
|
+
def path(style)
|
71
69
|
Interpolations.interpolate(options[:path], self, model, style)
|
72
70
|
end
|
73
71
|
|
74
|
-
def url(
|
72
|
+
def url(style)
|
75
73
|
Interpolations.interpolate(options[:url], self, model, style)
|
76
74
|
end
|
77
75
|
|
78
|
-
def
|
76
|
+
def process
|
77
|
+
return unless @queued_file
|
78
|
+
src_path = @queued_file.path
|
79
|
+
options[:processors].each do |processor|
|
80
|
+
processor.pre_runs(model, src_path)
|
81
|
+
options[:styles].each_pair do |style, style_options|
|
82
|
+
tmp_file = Tempfile.new("paperclip")
|
83
|
+
puts "processing #{name} for style #{style} with processor #{processor.name}"
|
84
|
+
processor.run(style, style_options, tmp_file)
|
85
|
+
@storage_updates << {
|
86
|
+
:type => STORAGE_UPDATE_SAVE,
|
87
|
+
:src_file => tmp_file,
|
88
|
+
:dst_path => path(style),
|
89
|
+
}
|
90
|
+
end
|
91
|
+
processor.post_runs
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
def update_storage
|
79
96
|
@storage_updates.each do |update|
|
80
97
|
case update[:type]
|
81
98
|
when STORAGE_UPDATE_SAVE
|
data/lib/sequel_paperclip.rb
CHANGED
@@ -13,14 +13,16 @@ module Sequel
|
|
13
13
|
def self.configure(model, opts={}, &block)
|
14
14
|
model.class_inheritable_hash :attachments
|
15
15
|
model.attachments = {}
|
16
|
+
|
17
|
+
model.send(:attr_accessor, :attachment_instances)
|
16
18
|
end
|
17
19
|
|
18
20
|
module ClassMethods
|
19
21
|
def attachment(name, options)
|
20
22
|
attr_accessor name
|
21
23
|
|
22
|
-
|
23
|
-
attachments[name] =
|
24
|
+
Attachment.preprocess_options(options)
|
25
|
+
self.attachments[name] = options
|
24
26
|
|
25
27
|
columns = db_schema.keys
|
26
28
|
unless columns.include?(:"#{name}_filename") || columns.include?(:"#{name}_basename")
|
@@ -63,77 +65,87 @@ module Sequel
|
|
63
65
|
end
|
64
66
|
end
|
65
67
|
|
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
|
+
define_method("#{name}") do
|
74
|
+
attachment = send("#{name}_attachment_instance")
|
75
|
+
attachment.exists? ? attachment : nil
|
76
|
+
end
|
77
|
+
|
66
78
|
define_method("#{name}=") do |value|
|
67
|
-
|
68
|
-
|
79
|
+
attachment = send("#{name}_attachment_instance")
|
80
|
+
|
81
|
+
if value
|
82
|
+
basename = send("#{name}_basename")
|
83
|
+
if basename.blank?
|
84
|
+
basename = ActiveSupport::SecureRandom.hex(4).to_s
|
85
|
+
send("#{name}_basename=", basename)
|
86
|
+
end
|
87
|
+
|
88
|
+
if respond_to?("#{name}_filename")
|
89
|
+
send("#{name}_filename=", basename+File.extname(file.original_filename).downcase)
|
90
|
+
end
|
91
|
+
|
92
|
+
if respond_to?("#{name}_filesize")
|
93
|
+
send("#{name}_filesize=", file.size)
|
94
|
+
end
|
95
|
+
|
96
|
+
if respond_to?("#{name}_originalname")
|
97
|
+
send("#{name}_originalname=", file.original_filename)
|
98
|
+
end
|
99
|
+
|
100
|
+
attachment.set(value)
|
101
|
+
else
|
102
|
+
attachment.destroy
|
103
|
+
|
104
|
+
send("#{name}_basename=", nil)
|
69
105
|
end
|
70
|
-
instance_variable_set("@#{name}", value);
|
71
106
|
|
72
107
|
# force sequel to call the hooks
|
73
108
|
modified!
|
74
109
|
end
|
75
110
|
|
76
111
|
define_method("#{name}?") do
|
77
|
-
attachment
|
112
|
+
attachment = send("#{name}_attachment_instance")
|
113
|
+
attachment.exists?
|
78
114
|
end
|
79
115
|
|
80
116
|
define_method("#{name}_url") do |style|
|
81
|
-
attachment
|
117
|
+
attachment = send("#{name}_attachment_instance")
|
118
|
+
attachment.url(style)
|
82
119
|
end
|
83
120
|
|
84
121
|
define_method("#{name}_path") do |style|
|
85
|
-
attachment
|
122
|
+
attachment = send("#{name}_attachment_instance")
|
123
|
+
attachment.path(style)
|
86
124
|
end
|
87
125
|
end
|
88
126
|
end
|
89
127
|
|
90
|
-
module InstanceMethods
|
91
|
-
def before_save
|
92
|
-
self.class.attachments.each_value do |attachment|
|
93
|
-
file = send(attachment.name)
|
94
|
-
if file
|
95
|
-
unless file.is_a?(File) || file.is_a?(Tempfile)
|
96
|
-
raise ArgumentError, "#{attachment.name} is not a File"
|
97
|
-
end
|
98
|
-
|
99
|
-
basename = send("#{attachment.name}_basename")
|
100
|
-
if basename.blank?
|
101
|
-
basename = ActiveSupport::SecureRandom.hex(4).to_s
|
102
|
-
send("#{attachment.name}_basename=", basename)
|
103
|
-
end
|
104
|
-
|
105
|
-
if respond_to?("#{attachment.name}_filename")
|
106
|
-
send("#{attachment.name}_filename=", basename+File.extname(file.original_filename).downcase)
|
107
|
-
end
|
108
|
-
|
109
|
-
if respond_to?("#{attachment.name}_filesize")
|
110
|
-
send("#{attachment.name}_filesize=", file.size)
|
111
|
-
end
|
112
|
-
|
113
|
-
if respond_to?("#{attachment.name}_originalname")
|
114
|
-
send("#{attachment.name}_originalname=", file.original_filename)
|
115
|
-
end
|
116
|
-
end
|
117
|
-
end
|
118
|
-
super
|
119
|
-
end
|
120
|
-
|
128
|
+
module InstanceMethods
|
121
129
|
def after_save
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
attachment.
|
130
|
+
if attachment_instances
|
131
|
+
attachment_instances.each_value do |attachment|
|
132
|
+
attachment.process
|
133
|
+
attachment.update_storage
|
126
134
|
end
|
127
|
-
|
128
|
-
attachment.update_storage(self)
|
129
135
|
end
|
130
136
|
super
|
131
137
|
end
|
132
138
|
|
133
139
|
def after_destroy
|
134
|
-
self.class.attachments.
|
135
|
-
send("#{
|
136
|
-
attachment.
|
140
|
+
self.class.attachments.each_key do |name|
|
141
|
+
attachment = send("#{name}_attachment_instance")
|
142
|
+
attachment.destroy
|
143
|
+
end
|
144
|
+
|
145
|
+
if attachment_instances
|
146
|
+
attachment_instances.each_value do |attachment|
|
147
|
+
attachment.update_storage
|
148
|
+
end
|
137
149
|
end
|
138
150
|
super
|
139
151
|
end
|
data/sequel_paperclip.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{sequel_paperclip}
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "0.3.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Corin Langosch"]
|
12
|
-
s.date = %q{2010-09-
|
12
|
+
s.date = %q{2010-09-21}
|
13
13
|
s.description = %q{Sequel plugin which provides Paperclip (attachments, thumbnail resizing, etc.) functionality for model.}
|
14
14
|
s.email = %q{info@netskin.com}
|
15
15
|
s.extra_rdoc_files = [
|
metadata
CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
6
|
- 0
|
7
|
-
-
|
7
|
+
- 3
|
8
8
|
- 0
|
9
|
-
version: 0.
|
9
|
+
version: 0.3.0
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Corin Langosch
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-09-
|
17
|
+
date: 2010-09-21 00:00:00 +01:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|