Sha256: f8cf3a5ba2b9b7dcafe38a200df1c3404bcfbaea21836c03ed91d1d855a683d5

Contents?: true

Size: 1.79 KB

Versions: 1

Compression:

Stored size: 1.79 KB

Contents

require "action_controller/metal"

module ThemesForRails
  class AssetsController < ActionController::Base

    include ThemesForRails::CommonMethods
    include ThemesForRails::UrlHelpers

    def stylesheets
      handle_asset(params[:asset], params[:theme], "stylesheets")
    end

    def javascripts
      handle_asset(params[:asset], params[:theme], "javascripts")
    end

    def images
      handle_asset(params[:asset], params[:theme], "images")
    end

  private
    
    def handle_asset(asset, theme, prefix)
      find_themed_asset(asset, theme, prefix) do |path, mime_type|
        send_file path, :type => mime_type, :disposition => "inline"
      end
    end
    
    def find_themed_asset(asset_name, asset_theme, asset_prefix, &block)
      path = asset_path(asset_name, asset_theme, asset_prefix)
      if File.exists?(path)
        yield path, mime_type_for(request)
      elsif File.extname(path).blank?
        asset_name = "#{asset_name}.#{extension_from(request.path_info)}"
        return find_themed_asset(asset_name, asset_theme, asset_prefix, &block) 
      else
        render_not_found
      end
    end

    def asset_path(asset_name, asset_theme, asset_prefix)
      File.join(theme_path_for(asset_theme), asset_prefix, asset_name)
    end

    def render_not_found
      render :text => 'not found', :status => 404
    end
      
    def mime_type_for(request)
      existing_mime_type = mime_type_from_uri(request.path_info)
      unless existing_mime_type.nil? 
        existing_mime_type.to_s
      else
        "image/#{extension_from(request.path_info)}"
      end
    end
    
    def mime_type_from_uri(path)
      extension = extension_from(path)
      Mime::Type.lookup_by_extension(extension)
    end
    
    def extension_from(path)
      File.extname(path).to_s[1..-1]
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
themes_for_rails-0.4.3 lib/themes_for_rails/assets_controller.rb