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 CHANGED
@@ -1 +1 @@
1
- 0.3.0
1
+ 0.4.0
@@ -40,36 +40,34 @@ module Sequel
40
40
  @storage_updates = []
41
41
  end
42
42
 
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?
53
- options[:styles].each_pair do |style, style_options|
54
- @storage_updates << {
55
- :type => STORAGE_UPDATE_DELETE,
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 = nil
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
 
@@ -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
- model.send(:attr_accessor, :attachment_instances)
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
- attachment = send("#{name}_attachment_instance")
75
- attachment.exists? ? attachment : nil
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}_attachment_instance")
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 = send("#{name}_basename")
83
- if basename.blank?
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}_attachment_instance")
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
- attachment = send("#{name}_attachment_instance")
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
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{sequel_paperclip}
8
- s.version = "0.3.0"
8
+ s.version = "0.4.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"]
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 3
7
+ - 4
8
8
  - 0
9
- version: 0.3.0
9
+ version: 0.4.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Corin Langosch