Sha256: 8ac94438aa152f7026943318ce3f1c31da1ce3b6b00bd013c8cf6ed5dd5d8359

Contents?: true

Size: 1.6 KB

Versions: 2

Compression:

Stored size: 1.6 KB

Contents

require "action_controller/metal"

module ThemesForRails
  class AssetsController < ActionController::Base
    
    include ThemesForRails::CommonMethods
    include ThemesForRails::UrlHelpers
    
    def stylesheets
      filename, extension = extract_filename_and_extension_from(params[:asset])
      render_asset theme_stylesheet_path_for(params[:theme], filename), 'text/css'
    end
    
    def javascripts
      filename, extension = extract_filename_and_extension_from(params[:asset])
      render_asset theme_javascript_path_for(params[:theme], filename), 'text/javascript'
    end
    
    def images
      filename, extension = extract_filename_and_extension_from(params[:asset])
      render_asset theme_image_path_for(params[:theme], filename, extension), "image/#{extension.gsub('.', '')}"
    end
    
  private
  
    def extract_filename_and_extension_from(asset)
      extension = File.extname(asset)
      filename = params[:asset].gsub(extension, '')
      return filename, extension
    end
    
    def render_asset(asset, mime_type)
      unless File.exists?(asset)
        render :text => 'not found', :status => 404
      else
        send_file asset, :type => mime_type
      end
    end
    
    # Physical paths
    def theme_stylesheet_path_for(name, asset)
      File.join(theme_path_for(name), 'stylesheets', "#{asset}.css")
    end
    def theme_javascript_path_for(name, asset)
      File.join(theme_path_for(name), 'javascripts', "#{asset}.js")
    end
    def theme_image_path_for(name, asset, extension = ".png")
      File.join(theme_path_for(name), 'images', "#{asset}#{extension}")
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
themes_for_rails-0.3.1 lib/themes_for_rails/assets_controller.rb
themes_for_rails-0.3.0 lib/themes_for_rails/assets_controller.rb