sequel_paperclip 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.1
1
+ 0.0.2
@@ -6,6 +6,9 @@ module Sequel
6
6
  attr_reader :options
7
7
  attr_accessor :processors
8
8
 
9
+ STORAGE_UPDATE_SAVE = 1
10
+ STORAGE_UPDATE_DELETE = 2
11
+
9
12
  def initialize(name, options = {})
10
13
  unless options[:styles]
11
14
  options[:styles] = {
@@ -25,22 +28,39 @@ module Sequel
25
28
  @options = options
26
29
  self.processors = []
27
30
  options[:processors].each do |processor|
28
- self.processors << "Sequel::Plugins::Paperclip::Processors::#{processor[:type].to_s.capitalize}".constantize.new(self, processor)
31
+ klass = "Sequel::Plugins::Paperclip::Processors::#{processor[:type].to_s.capitalize}"
32
+ self.processors << klass.constantize.new(self, processor)
29
33
  end
34
+ @storage_updates = []
30
35
  end
31
36
 
32
37
  def process(model, src_path)
33
- files_to_store = {}
34
38
  processors.each do |processor|
35
39
  processor.pre_runs(model, src_path)
36
40
  options[:styles].each_pair do |style, style_options|
37
- files_to_store[style] ||= Tempfile.new("paperclip")
41
+ tmp_file = Tempfile.new("paperclip")
38
42
  puts "processing #{name} for style #{style} with processor #{processor.name}"
39
- processor.run(style, style_options, files_to_store[style])
43
+ processor.run(style, style_options, tmp_file)
44
+ @storage_updates << {
45
+ :type => STORAGE_UPDATE_SAVE,
46
+ :src_file => tmp_file,
47
+ :dst_path => path(model, style),
48
+ }
40
49
  end
41
50
  processor.post_runs
42
51
  end
43
- files_to_store
52
+ end
53
+
54
+ def destroy(model)
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)
44
64
  end
45
65
 
46
66
  def exists?(model)
@@ -54,6 +74,26 @@ module Sequel
54
74
  def url(model, style)
55
75
  Interpolations.interpolate(options[:url], self, model, style)
56
76
  end
77
+
78
+ def update_storage(model)
79
+ @storage_updates.each do |update|
80
+ case update[:type]
81
+ when STORAGE_UPDATE_SAVE
82
+ puts "saving #{update[:dst_path]} (#{update[:src_file].size} bytes)"
83
+ FileUtils.mkdir_p(File.dirname(update[:dst_path]))
84
+ FileUtils.cp(update[:src_file].path, update[:dst_path])
85
+ update[:src_file].close!
86
+ when STORAGE_UPDATE_DELETE
87
+ puts "deleting #{update[:path]}"
88
+ begin
89
+ FileUtils.rm(update[:path])
90
+ rescue Errno::ENOENT => error
91
+ end
92
+ else
93
+ raise ArgumentError, "invalid type '#{update[:type]}'"
94
+ end
95
+ end
96
+ end
57
97
  end
58
98
  end
59
99
  end
@@ -8,7 +8,7 @@ module Sequel
8
8
  attr_reader :options
9
9
 
10
10
  def initialize(attachment, options)
11
- @name = self.class.name
11
+ @name = "dummy"
12
12
  end
13
13
 
14
14
  def pre_runs(model, src_path)
@@ -8,7 +8,7 @@ module Sequel
8
8
  attr_reader :options
9
9
 
10
10
  def initialize(attachment, options)
11
- @name = self.class.name
11
+ @name = "image"
12
12
  @attachment = attachment
13
13
  @options = options
14
14
  end
@@ -39,10 +39,24 @@ module Sequel
39
39
  end
40
40
 
41
41
  define_method("#{name}_basename=") do |basename|
42
- old_filename = send("#{name}_filename")
43
- extname = old_filename ? File.extname(old_filename) : ""
44
- send("#{name}_filename=", basename+extname)
42
+ if basename
43
+ old_filename = send("#{name}_filename")
44
+ extname = old_filename ? File.extname(old_filename) : ""
45
+ send("#{name}_filename=", basename+extname)
46
+ else
47
+ send("#{name}_filename=", nil)
48
+ end
49
+ end
50
+ end
51
+
52
+ define_method("#{name}=") do |value|
53
+ if !value && attachment.exists?(self)
54
+ attachment.destroy(self)
45
55
  end
56
+ instance_variable_set("@#{name}", value);
57
+
58
+ # force sequel to call the hooks
59
+ modified!
46
60
  end
47
61
 
48
62
  define_method("#{name}?") do
@@ -59,67 +73,45 @@ module Sequel
59
73
  end
60
74
  end
61
75
 
62
- module InstanceMethods
76
+ module InstanceMethods
63
77
  def before_save
64
78
  self.class.attachments.each_value do |attachment|
65
79
  file = send(attachment.name)
66
- next unless file
80
+ if file
81
+ unless file.is_a?(File) || file.is_a?(Tempfile)
82
+ raise ArgumentError, "#{attachment.name} is not a File"
83
+ end
67
84
 
68
- unless file.is_a?(File) || file.is_a?(Tempfile)
69
- raise ArgumentError, "#{attachment.name} is not a File"
70
- end
85
+ basename = send("#{attachment.name}_basename")
86
+ if basename.blank?
87
+ basename = ActiveSupport::SecureRandom.hex(4).to_s
88
+ send("#{attachment.name}_basename=", basename)
89
+ end
71
90
 
72
- basename = send("#{attachment.name}_basename")
73
- if basename.blank?
74
- basename = ActiveSupport::SecureRandom.hex(4).to_s
75
- send("#{attachment.name}_basename=", basename)
76
- end
91
+ if respond_to?("#{attachment.name}_filename")
92
+ send("#{attachment.name}_filename=", basename+File.extname(file.original_filename).downcase)
93
+ end
77
94
 
78
- if respond_to?("#{attachment.name}_filename")
79
- send("#{attachment.name}_filename=", basename+File.extname(file.original_filename).downcase)
80
- end
95
+ if respond_to?("#{attachment.name}_filesize")
96
+ send("#{attachment.name}_filesize=", file.size)
97
+ end
81
98
 
82
- if respond_to?("#{attachment.name}_filesize")
83
- send("#{attachment.name}_filesize=", file.size)
84
- end
99
+ if respond_to?("#{attachment.name}_originalname")
100
+ send("#{attachment.name}_originalname=", file.original_filename)
101
+ end
85
102
 
86
- if respond_to?("#{attachment.name}_originalname")
87
- send("#{attachment.name}_originalname=", file.original_filename)
103
+ attachment.process(self, file.path)
88
104
  end
89
- end
90
- super
91
- end
92
-
93
- def after_save
94
- self.class.attachments.each_value do |attachment|
95
- file = send(attachment.name)
96
- next unless file
97
-
98
- files_to_store = attachment.process(self, file.path)
99
- attachment.options[:styles].each_key do |style|
100
- src_file = files_to_store[style]
101
- dst_path = attachment.path(self, style)
102
- puts "saving #{dst_path} (#{src_file.size} bytes)"
103
- FileUtils.mkdir_p(File.dirname(dst_path))
104
- FileUtils.cp(src_file.path, dst_path)
105
- src_file.close!
106
- end
105
+
106
+ attachment.update_storage(self)
107
107
  end
108
108
  super
109
109
  end
110
110
 
111
111
  def after_destroy
112
112
  self.class.attachments.each_value do |attachment|
113
- attachment.options[:styles].each_key do |style|
114
- next unless attachment.exists?(self)
115
- dst_path = attachment.path(self, style)
116
-
117
- puts "deleting #{dst_path}"
118
- begin
119
- FileUtils.rm(dst_path)
120
- rescue Errno::ENOENT => error
121
- end
122
- end
113
+ send("#{attachment.name}=", nil)
114
+ attachment.update_storage(self)
123
115
  end
124
116
  super
125
117
  end
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{sequel_paperclip}
8
- s.version = "0.0.1"
8
+ s.version = "0.0.2"
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-08-31}
12
+ s.date = %q{2010-09-09}
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
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 1
9
- version: 0.0.1
8
+ - 2
9
+ version: 0.0.2
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-08-31 00:00:00 +02:00
17
+ date: 2010-09-09 00:00:00 +01:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency