Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

file 133 lines (109 sloc) 3.311 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
require 'active_support/core_ext/module/delegation'
#
# convention over configuration.
#
# Rails application example:
# app/
# - themes
# - [theme_name]
# |- assets
# |- images
# |- javascripts
# |- stylesheets
# |- compiled -> Compiled static files
# |- views
# |- layouts
#
module ThemePark
  autoload :Configuration, 'theme_park/configuration'
  autoload :Version, 'theme_park/version'
  autoload :Server, 'theme_park/server'

  module Rails
    autoload :ActionController, 'theme_park/rails/action_controller'
    autoload :AssetTagHelper, 'theme_park/rails/asset_tag_helper'
    autoload :Server, 'theme_park/rails/server'
  end

  class << self

    delegate :base, :root, :base_root, :prefix, :handlers, :to => :config

    def config
      @@config ||= ThemePark::Configuration.new
    end

    def setup
      yield config if block_given?
      self
    end

    def env
      @@env ||= Sprockets::Environment.new(base.to_s) do |env|
        env.version = ::Rails.env
        env.cache = Sprockets::Cache::FileStore.new "#{base}/tmp/cache/assets/#{::Rails.env}"
      end
    end

    def assets(name)
      env.clear_paths
      assets_path(name).each{|path| env.append_path path }
      env
    end

    def version
      ThemePark::Version::STRING
    end

    # Return the path of given theme name.
    def path(theme_name)
      File.join base_root, theme_name
    end

    def exist?(theme_name)
      Dir.exist? path(theme_name)
    end

    def interpolate(pattern, theme_name)
      pattern.gsub(":root", base_root).gsub(":name", theme_name.to_s)
    end
    
    ##
    # :method: images_path
    #
    # :call-seq: images_path(theme_name)
    #
    # Return images path for the theme name

    ##
    # :method: javascripts_path
    #
    # :call-seq: javascripts_path(theme_name)
    #
    # Return javascripts path for the theme name

    ##
    # :method: stylesheets_path
    #
    # :call-seq: stylesheets_path(theme_name)
    #
    # Return stylesheets path for the theme name

    ##
    # :method: compiled_path
    #
    # :call-seq: compiled_path(theme_name)
    #
    # Return compiled file path for the theme name

    ##
    # :method: views_path
    #
    # :call-seq: views_path(theme_name)
    #
    # Return views path for the theme name

    [ :images, :javascripts, :stylesheets, :compiled, :views ].each do |dir|
      class_eval <<-DIR_EVAL, __FILE__, __LINE__ + 1
def #{dir}_path(theme_name)
pattern = config.send :#{dir}_path
interpolate(pattern, theme_name)
end

DIR_EVAL
    end

    # The theme assets path is a array contains images, javascripts and stylesheets path.
    def assets_path(name)
      [ images_path(name), javascripts_path(name), stylesheets_path(name) ]
    end

    # The assets path contains all themes' images, javascripts and stylesheets path.
    def all_assets_path
      Dir.glob( File.join(self.base_root, "*") ).map do |theme_name|
        assets_path( File.basename(theme_name) )
      end.flatten
    end

  end

end

require 'theme_park/railtie' if defined?(Rails)
Something went wrong with that request. Please try again.