lib/attached/attachment.rb in attached-0.1.4 vs lib/attached/attachment.rb in attached-0.1.5

- old
+ new

@@ -10,33 +10,38 @@ attr_reader :file attr_reader :name attr_reader :instance - attr_reader :options attr_reader :queue attr_reader :path attr_reader :styles attr_reader :default attr_reader :medium attr_reader :credentials attr_reader :processors attr_reader :processor + attr_reader :aliases + attr_reader :alias + attr_reader :storage + attr_reader :host # A default set of options that can be extended to customize the path, storage or credentials. # # Usage: # # Attached::Attachment.options = { :storage => :fs, :path => "/:name/:style/:identifier:extension" } def self.options @options ||= { - :path => "/:name/:style/:identifier:extension", - :default => :original, - :styles => {}, - :processors => [], + :path => "/:name/:style/:identifier:extension", + :default => :original, + :credentials => {}, + :styles => {}, + :processors => [], + :aliases => [], } end # Initialize a new attachment by providing a name and the instance the attachment is associated with. @@ -52,25 +57,33 @@ # * :storage - The storage medium represented as a symbol such as ':s3' # * :credentials - A file, hash, or path used to authenticate with the specified storage medium # * :styles - A hash containing optional parameters including extension and identifier def initialize(name, instance, options = {}) + options = self.class.options.merge(options) + @name = name @instance = instance - @options = self.class.options.merge(options) @queue = {} - @path = @options[:path] - @styles = @options[:styles] - @default = @options[:default] - @medium = @options[:medium] - @credentials = @options[:credentials] - @processors = @options[:processors] - @processor = @options[:processor] + @path = options[:path] + @styles = options[:styles] + @default = options[:default] + @medium = options[:medium] + @credentials = options[:credentials] + @processors = options[:processors] + @processor = options[:processor] + @aliases = options[:aliases] + @alias = options[:alias] - @processors = @processors + [@processor] if @processor + @processors = self.processors + [self.processor] if self.processor + @aliases = self.aliases + [self.alias] if self.alias + + @storage = Attached::Storage.storage(self.medium, self.credentials) + + @host = self.storage.host end # Check if an attachment has been modified. # @@ -80,11 +93,15 @@ def changed? instance.changed.include? "#{name}_identifier" end + def file? + not identifier.blank? + end + # Assign an attachment to a file. # # Usage: # # @object.avatar.assign(...) @@ -109,12 +126,10 @@ # Usage: # # @object.avatar.save def save - @storage ||= Attached::Storage.storage(self.medium, self.credentials) - @queue.each do |style, file| @storage.save(file, self.path(style)) if file and self.path(style) end @queue = {} @@ -126,12 +141,10 @@ # Usage: # # @object.avatar.destroy def destroy - @storage ||= Attached::Storage.storage(self.medium, self.credentials) - @storage.destroy(self.path) if self.path end # Acesss the URL for an attachment. @@ -141,12 +154,15 @@ # @object.avatar.url # @object.avatar.url(:small) # @object.avatar.url(:large) def url(style = self.default) - @storage ||= Attached::Storage.storage(self.medium, self.credentials) + path = self.path(style) - return "#{@storage.host}#{path(style)}" + host = self.host + host = self.aliases[path.hash % self.aliases.count] unless self.aliases.empty? + + return "#{host}#{path}" end # Access the path for an attachment. # \ No newline at end of file