Permalink
Browse files

Changes to get tests passing

  • Loading branch information...
1 parent 3d0b499 commit 59aad341c598c2f63caa796ebfe8011e17bfe8e0 @jyurek jyurek committed May 4, 2009
View
@@ -188,7 +188,7 @@ def has_attached_file name, options = {}
include InstanceMethods
write_inheritable_attribute(:attachment_definitions, {}) if attachment_definitions.nil?
- attachment_definitions[name] = {:validations => {}}.merge(options)
+ attachment_definitions[name] = Paperclip::Definition.new(options)
after_save :save_attached_files
before_destroy :destroy_attached_files
@@ -285,7 +285,7 @@ def attachment_definitions
module InstanceMethods #:nodoc:
def attachment_for name
@_paperclip_attachments ||= {}
- @_paperclip_attachments[name] ||= Attachment.new(name, self, self.class.attachment_definitions[name])
+ @_paperclip_attachments[name] ||= Attachment.new(name, self, self.class.attachment_definitions[name].clone)
end
def each_attachment
View
@@ -16,39 +16,30 @@ def self.default_options
}
end
- attr_reader :name, :instance, :styles, :default_style, :convert_options, :queued_for_write
+ attr_reader :name, :instance, :styles, :default_style, :convert_options, :queued_for_write, :definition
# Creates an Attachment object. +name+ is the name of the attachment,
# +instance+ is the ActiveRecord object instance it's attached to, and
# +options+ is the same as the hash passed to +has_attached_file+.
- def initialize name, instance, options = {}
- @name = name
- @instance = instance
-
- options = self.class.default_options.merge(options)
-
- @url = options[:url]
- @url = @url.call(self) if @url.is_a?(Proc)
- @path = options[:path]
- @path = @path.call(self) if @path.is_a?(Proc)
- @styles = options[:styles]
- @styles = @styles.call(self) if @styles.is_a?(Proc)
- @default_url = options[:default_url]
- @validations = options[:validations]
- @default_style = options[:default_style]
- @storage = options[:storage]
- @whiny = options[:whiny_thumbnails]
- @convert_options = options[:convert_options] || {}
- @processors = options[:processors] || [:thumbnail]
- @options = options
- @queued_for_delete = []
- @queued_for_write = {}
- @errors = {}
- @validation_errors = nil
- @dirty = false
-
- normalize_style_definition
- initialize_storage
+ def initialize name, instance, definition
+ @name = name
+ @instance = instance
+ @definition = definition
+ @definition.payload = self
+
+ @url = definition.url
+ @path = definition.path
+ @default_url = definition.default_url
+ @validations = definition.validations
+ @default_style = definition.default_style
+ @storage = definition.storage
+ @whiny = definition.whiny_processing
+
+ @queued_for_delete = []
+ @queued_for_write = {}
+ @errors = {}
+ @validation_errors = nil
+ @dirty = false
end
# What gets called when you call instance.attachment = File. It clears
@@ -60,16 +51,7 @@ def initialize name, instance, options = {}
# If the file that is assigned is not valid, the processing (i.e.
# thumbnailing, etc) will NOT be run.
def assign uploaded_file
- %w(file_name).each do |field|
- unless @instance.class.column_names.include?("#{name}_#{field}")
- raise PaperclipError.new("#{@instance.class} model does not have required column '#{name}_#{field}'")
- end
- end
-
- if uploaded_file.is_a?(Paperclip::Attachment)
- uploaded_file = uploaded_file.to_file(:original)
- close_uploaded_file = uploaded_file.respond_to?(:close)
- end
+ verify_columns!
return nil unless valid_assignment?(uploaded_file)
@@ -91,7 +73,6 @@ def assign uploaded_file
# Reset the file size if the original file was reprocessed.
instance_write(:file_size, @queued_for_write[:original].size.to_i)
ensure
- uploaded_file.close if close_uploaded_file
validate
end
@@ -150,7 +131,7 @@ def save
end
end
- # Clears out the attachment. Has the same effect as previously assigning
+ # Clears out the attachment. Has the same effect as assigning
# nil to the attachment. Does NOT save. If you wish to clear AND save,
# use #destroy.
def clear
@@ -159,7 +140,7 @@ def clear
@validation_errors = nil
end
- # Destroys the attachment. Has the same effect as previously assigning
+ # Destroys the attachment. Has the same effect as assigning
# nil to the attachment *and saving*. This is permanent. If you wish to
# wipe out the existing attachment but not save, use #clear.
def destroy
@@ -208,7 +189,7 @@ def self.interpolations
attachment.original_filename.gsub(/#{File.extname(attachment.original_filename)}$/, "")
end,
:extension => lambda do |attachment,style|
- ((style = attachment.styles[style]) && style[:format]) ||
+ ((style = attachment.definition.style(style)) && style[:format]) ||
File.extname(attachment.original_filename).gsub(/^\.+/, "")
end,
:id => lambda{|attachment,style| attachment.instance.id },
@@ -267,10 +248,22 @@ def instance_read(attr)
instance.send(getter) if responds || attr.to_s == "file_name"
end
+ def to_file(style = default_style)
+ @storage.to_file(path(style))
+ end
+
private
+ def verify_columns!
+ %w(file_name).each do |field|
+ unless @instance.class.column_names.include?("#{name}_#{field}")
+ raise PaperclipError.new("#{@instance.class} model does not have required column '#{name}_#{field}'")
+ end
+ end
+ end
+
def logger #:nodoc:
- instance.logger
+ @logger ||= instance.respond_to?(:logger) ? instance.logger : Logger.new(STDOUT)
end
def log message #:nodoc:
@@ -298,52 +291,8 @@ def validate #:nodoc:
@validation_errors
end
- def normalize_style_definition #:nodoc:
- @styles.each do |name, args|
- unless args.is_a? Hash
- dimensions, format = [args, nil].flatten[0..1]
- format = nil if format.blank?
- @styles[name] = {
- :processors => @processors,
- :geometry => dimensions,
- :format => format,
- :whiny => @whiny,
- :convert_options => extra_options_for(name)
- }
- else
- @styles[name] = {
- :processors => @processors,
- :whiny => @whiny,
- :convert_options => extra_options_for(name)
- }.merge(@styles[name])
- end
- end
- end
-
- def solidify_style_definitions #:nodoc:
- @styles.each do |name, args|
- @styles[name][:geometry] = @styles[name][:geometry].call(instance) if @styles[name][:geometry].respond_to?(:call)
- @styles[name][:processors] = @styles[name][:processors].call(instance) if @styles[name][:processors].respond_to?(:call)
- end
- end
-
- def initialize_storage #:nodoc:
- @storage_module = Paperclip::Storage.const_get(@storage.to_s.capitalize)
- self.extend(@storage_module)
- end
-
- def extra_options_for(style) #:nodoc:
- all_options = convert_options[:all]
- all_options = all_options.call(instance) if all_options.respond_to?(:call)
- style_options = convert_options[style]
- style_options = style_options.call(instance) if style_options.respond_to?(:call)
-
- [ style_options, all_options ].compact.join(" ")
- end
-
def post_process #:nodoc:
return if @queued_for_write[:original].nil?
- solidify_style_definitions
return if fire_events(:before)
post_process_styles
return if fire_events(:after)
@@ -359,10 +308,10 @@ def callback which #:nodoc:
end
def post_process_styles
- @styles.each do |name, args|
+ definition.each_style do |name, args|
begin
- raise RuntimeError.new("Style #{name} has no processors defined.") if args[:processors].blank?
- @queued_for_write[name] = args[:processors].inject(@queued_for_write[:original]) do |file, processor|
+ raise RuntimeError.new("Style #{name} has no processors defined.") if args.processors.blank?
+ @queued_for_write[name] = args.processors.inject(@queued_for_write[:original]) do |file, processor|
Paperclip.processor(processor).make(file, args, self)
end
rescue PaperclipError => e
@@ -384,7 +333,7 @@ def interpolate pattern, style = default_style #:nodoc:
def queue_existing_for_delete #:nodoc:
return unless file?
- @queued_for_delete += [:original, *@styles.keys].uniq.map do |style|
+ @queued_for_delete += [:original, *definition.styles].uniq.map do |style|
path(style) if exists?(style)
end.compact
instance_write(:file_name, nil)
@@ -398,6 +347,20 @@ def flush_errors #:nodoc:
[message].flatten.each {|m| instance.errors.add(name, m) }
end
end
+
+ def flush_writes
+ @queued_for_write.each do |style, file|
+ @storage.write(path(style), file, self)
+ end
+ @queued_for_write = {}
+ end
+
+ def flush_deletes
+ @queued_for_delete.each do |path|
+ @storage.delete(path)
+ end
+ @queued_for_delete = []
+ end
end
end
@@ -1,10 +1,28 @@
module Paperclip
class Definition
- attr_accessor :url, :path, :default_style, :default_url, :whiny_processing,
- :storage, :storage_method
+ def self.attr_latebind *fields
+ fields.each do |field|
+ attr_writer :field
+ define_method field do
+ value = instance_variable_get("@#{field}")
+ value.respond_to?(:call) ? value.call(payload) : value
+ end
+ end
+ end
+
+ attr_latebind :url, :path, :default_style, :default_url, :whiny_processing
+ attr_accessor :storage, :storage_method, :validations, :payload
def self.default_options
- @default_options ||= {}
+ @default_options ||= {
+ :url => "/system/:attachment/:id/:style/:basename.:extension",
+ :path => ":rails_root/public/system/:attachment/:id/:style/:basename.:extension",
+ :default_url => "/:attachment/:style/missing.png",
+ :default_style => :original,
+ :validations => {},
+ :processors => [:thumbnail],
+ :storage => :filesystem
+ }
end
def initialize options = {}
@@ -13,16 +31,40 @@ def initialize options = {}
@path = options[:path] || defaults[:path]
@default_style = options[:default_style] || defaults[:default_style]
@default_url = options[:default_url] || defaults[:default_url]
+ @validations = options[:validations] || defaults[:validations]
@whiny_processing = options[:whiny_processing] || defaults[:whiny_processing]
@storage_method = options[:storage] || :filesystem
@all_styles = options[:all_styles] || {}
- @style_hashes = options[:styles]
+ @style_hashes = options[:styles] || {}
@styles = {}
- @storage = Storage.for(@storage_method, options)
+
+ @options = options
+
+ @all_styles[:processors] = options[:processors]
+ @all_styles = Style.new(@all_styles)
+ @style_hashes.each do |name, opts|
+ @styles[name] = @all_styles.merge(Style.new(opts))
+ end
+
+ @styles.each do |name, style|
+ style.processors ||= @all_styles.processors || defaults[:processors]
+ end
+ end
+
+ def storage
+ Storage.for(@storage_method, @options)
end
def style name
- @styles[name] ||= Style.new(@all_styles.merge(@style_hashes[name]))
+ @styles[name]
+ end
+
+ def styles
+ @styles.keys
+ end
+
+ def each_style &block
+ @styles.each(&block)
end
class Options
@@ -33,6 +75,8 @@ def initialize options = {}
end
end
+ attr_accessor :payload
+
def option_methods
@methods
end
@@ -41,6 +85,10 @@ def methodize method, value
(@methods ||= []) << method.to_sym
(class << self; self; end).class_eval do
attr_accessor method
+ define_method method do
+ value = instance_variable_get("@#{method}")
+ value.respond_to?(:call) ? value.call(payload) : value
+ end
end
instance_variable_set("@#{method}", value)
end
@@ -115,13 +163,17 @@ def credentials= yaml
class Style < Options
def initialize options = {}, format = nil
- if options.is_a? String
+ case options
+ when String
geometry = options
options = {}
+ when Array
+ geometry, format = options
+ options = {}
end
self.geometry = options.delete(:geomtry) || geometry
self.format = options.delete(:format) || format
- self.processors = options.delete(:processors) || [:thumbnail]
+ self.processors = options.delete(:processors)
self.convert_options = options.delete(:convert_options)
self.whiny_processing = options.delete(:whiny_processing)
self.whiny_processing = whiny_processing.nil? ? true : whiny_processing
Oops, something went wrong.

0 comments on commit 59aad34

Please sign in to comment.