Sha256: 43862d6268ab073cad75b1b987a39afb05184bf98d7d60d07998655c6641257a

Contents?: true

Size: 1.67 KB

Versions: 2

Compression:

Stored size: 1.67 KB

Contents

require "uri"

module RailsImager::ImagesHelper
  def rails_imager_p(path, args = {})
    if path.class.name == "Paperclip::Attachment"
      raise "Paperclip path does not start with public path: #{path.path}" unless path.path.to_s.start_with?(Rails.public_path.to_s)
      path_without_public = path.path.to_s.gsub("#{Rails.public_path}/", "")
      raise "Path didn't change '#{path.path}' - '#{path_without_public}'." if path.path.to_s == path_without_public
      path = path_without_public
    end
    
    newpath = ""
    
    if args[:url]
      args.delete(:url)
      newpath << "#{request.protocol}#{request.host_with_port}"
    elsif args[:mailer]
      args.delete(:mailer)
      
      if ActionMailer::Base.default_url_options[:protocol]
        newpath << ActionMailer::Base.default_url_options[:protocol]
      else
        newpath << "http://"
      end
      
      newpath << ActionMailer::Base.default_url_options[:host]
      
      if ActionMailer::Base.default_url_options[:port]
        newpath << ":#{ActionMailer::Base.default_url_options[:port]}"
      end
    end
    
    # Check for invalid parameters.
    args.each do |key, val|
      raise ArgumentError, "Invalid parameter: '#{key}'." unless RailsImager::ImageHandler::PARAMS_ARGS.include?(key)
    end
    
    
    
    newpath << "/rails_imager/images/"
    newpath << URI.encode(path)
    newpath << "/?"
    
    first = true
    args.each do |key, val|
      if first
        first = false
      else
        newpath << "&"
      end
      
      realkey = "image[#{key}]"
      
      newpath << URI.encode(realkey.to_s)
      newpath << "="
      newpath << URI.encode(val.to_s)
    end
    
    return newpath
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rails_imager-0.0.16 app/helpers/rails_imager/images_helper.rb
rails_imager-0.0.15 app/helpers/rails_imager/images_helper.rb