lib/attachs/storages/base.rb in attachs-0.4.5 vs lib/attachs/storages/base.rb in attachs-4.0.0.0

- old
+ new

@@ -1,73 +1,24 @@ module Attachs module Storages class Base - def initialize(attachment) - @attachment = attachment - end + private - protected - - attr_reader :attachment - - def template - @template ||= begin - if attachment.exists? - (attachment.options[:path] || Attachs.config.default_path).dup - else - attachment.options[:default_path].dup - end.tap do |path| - path.scan(/:([a-zA-Z0-9_]+)/).flatten.uniq.map(&:to_sym).each do |name| - if name != :style - path.gsub! ":#{name}", interpolate(name) - end - end - path.squeeze! '/' - path.slice! 0 if path[0] == '/' - end - end + def detect_content_type(path) + MIME::Types.type_for(path).first.to_s end - def path(style=:original) - template.gsub(':style', style.to_s) + def processable?(path) + detect_content_type(path) =~ /^image\// end - def interpolate(name) - if interpolation = Attachs.config.interpolations[name] - interpolation.call(attachment).to_s.parameterize - else - case name - when :basename - attachment.basename.parameterize - when :filename - "#{attachment.basename.parameterize}.#{attachment.extension}" - when :size - attachment.size - when :extension - attachment.extension - when :type - attachment.content_type.split('/').first.parameterize - when :timestamp - attachment.updated_at.to_i - when :class - attachment.record.class.name.parameterize - when :id - attachment.record.id - when :attribute - attachment.attribute.to_s.parameterize - end.to_s + def build_processor(path) + case detect_content_type(path) + when /^image\// + klass = Processors::Image end - end - - def find_option(options, name, default) - if options.has_key?(name) - options[name] - elsif attachment.options.has_key?(name) - attachment.options[name] - else - default - end + klass.new path end end end end