26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
# File 'lib/roachclip.rb', line 26
def roachclip name, options = {}
self.attachment name
raise InvalidAttachment unless attachment_names.include?(name)
path = options.delete(:path) || "/gridfs/fs/%s-%s"
self.roaches << {:name => name, :options => options}
options[:default_style] ||= :original
options[:styles] ||= {}
options[:styles].each { |k,v| self.attachment "#{name}_#{k}" unless k == options[:default_style] }
before_save :process_roaches
before_save :destroy_nil_roaches
self.send(:define_method, "#{name}_path") do
time = self.attributes['updated_at'] || Time.now
time = time.to_i
(path % [self.send(name).id.to_s, time]).chomp('-')
end
options[:styles].each do |k,v|
self.send(:define_method, "#{name}_#{k}_path") do
time = self.attributes['updated_at'] || Time.now
time = time.to_i
(path % [self.send("#{name}_#{k}").id.to_s, time]).chomp('-')
end
end
end
|