Commit a1e33538 by Adarsh Pandit

Refactor Paperclip::Attachment#assign

* Extract private methods
* Remove trailing conditionals
* Remove explicit returns
* Remove `.self`
parent 41da3a3c
...@@ -87,37 +87,29 @@ module Paperclip ...@@ -87,37 +87,29 @@ module Paperclip
end end
# What gets called when you call instance.attachment = File. It clears # What gets called when you call instance.attachment = File. It clears
# errors, assigns attributes, and processes the file. It # errors, assigns attributes, and processes the file. It also queues up the
# also queues up the previous file for deletion, to be flushed away on # previous file for deletion, to be flushed away on #save of its host. In
# #save of its host. In addition to form uploads, you can also assign # addition to form uploads, you can also assign another Paperclip
# another Paperclip attachment: # attachment:
# new_user.avatar = old_user.avatar # new_user.avatar = old_user.avatar
def assign uploaded_file def assign(uploaded_file)
@file = Paperclip.io_adapters.for(uploaded_file)
ensure_required_accessors! ensure_required_accessors!
ensure_required_validations! ensure_required_validations!
file = Paperclip.io_adapters.for(uploaded_file)
return nil if not file.assignment?
self.clear(*only_process)
return nil if file.nil?
@queued_for_write[:original] = file
instance_write(:file_name, cleanup_filename(file.original_filename))
instance_write(:content_type, file.content_type.to_s.strip)
instance_write(:file_size, file.size)
instance_write(:fingerprint, file.fingerprint) if instance_respond_to?(:fingerprint)
instance_write(:created_at, Time.now) if has_enabled_but_unset_created_at?
instance_write(:updated_at, Time.now)
@dirty = true
post_process(*only_process) if post_processing if @file.assignment?
clear(*only_process)
# Reset the file size if the original file was reprocessed. if @file.nil?
instance_write(:file_size, @queued_for_write[:original].size) nil
instance_write(:fingerprint, @queued_for_write[:original].fingerprint) if instance_respond_to?(:fingerprint) else
updater = :"#{name}_file_name_will_change!" assign_attributes
instance.send updater if instance.respond_to? updater post_process_file
reset_file_if_original_reprocessed
end
else
nil
end
end end
# Returns the public URL of the attachment with a given style. This does # Returns the public URL of the attachment with a given style. This does
...@@ -142,9 +134,8 @@ module Paperclip ...@@ -142,9 +134,8 @@ module Paperclip
# #
# +#new(Paperclip::Attachment, options_hash)+ # +#new(Paperclip::Attachment, options_hash)+
# +#for(style_name, options_hash)+ # +#for(style_name, options_hash)+
def url(style_name = default_style, options = {})
default_options = {:timestamp => @options[:use_timestamp], :escape => @options[:escape_url]}
def url(style_name = default_style, options = {})
if options == true || options == false # Backwards compatibility. if options == true || options == false # Backwards compatibility.
@url_generator.for(style_name, default_options.merge(:timestamp => options)) @url_generator.for(style_name, default_options.merge(:timestamp => options))
else else
...@@ -152,6 +143,13 @@ module Paperclip ...@@ -152,6 +143,13 @@ module Paperclip
end end
end end
def default_options
{
:timestamp => @options[:use_timestamp],
:escape => @options[:escape_url]
}
end
# Alias to +url+ that allows using the expiring_url method provided by the cloud # Alias to +url+ that allows using the expiring_url method provided by the cloud
# storage implementations, but keep using filesystem storage for development and # storage implementations, but keep using filesystem storage for development and
# testing. # testing.
...@@ -419,6 +417,61 @@ module Paperclip ...@@ -419,6 +417,61 @@ module Paperclip
self.extend(storage_module) self.extend(storage_module)
end end
def assign_attributes
@queued_for_write[:original] = @file
assign_file_information
assign_fingerprint(@file.fingerprint)
assign_timestamps
end
def assign_file_information
instance_write(:file_name, cleanup_filename(@file.original_filename))
instance_write(:content_type, @file.content_type.to_s.strip)
instance_write(:file_size, @file.size)
end
def assign_fingerprint(fingerprint)
if instance_respond_to?(:fingerprint)
instance_write(:fingerprint, fingerprint)
end
end
def assign_timestamps
if has_enabled_but_unset_created_at?
instance_write(:created_at, Time.now)
end
instance_write(:updated_at, Time.now)
end
def post_process_file
dirty!
if post_processing
post_process(*only_process)
end
end
def dirty!
@dirty = true
end
def reset_file_if_original_reprocessed
instance_write(:file_size, @queued_for_write[:original].size)
assign_fingerprint(@queued_for_write[:original].fingerprint)
reset_updater
end
def reset_updater
if instance.respond_to?(updater)
instance.send(updater)
end
end
def updater
:"#{name}_file_name_will_change!"
end
def extra_options_for(style) #:nodoc: def extra_options_for(style) #:nodoc:
process_options(:convert_options, style) process_options(:convert_options, style)
end end
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment