cirandas.net

ref: master

app/helpers/theme_loader_helper.rb


 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
module ThemeLoaderHelper
  def current_theme
    @current_theme ||=
      begin
        return session[:theme] if session[:theme]
        return session[:user_theme] if session[:user_theme]

        # utility for developers: set the theme to 'random' in development mode and
        # you will get a different theme every request. This is interesting for
        # testing
        if Rails.env.development? && environment.theme == 'random'
          @random_theme ||= Dir.glob('public/designs/themes/*').map { |f| File.basename(f) }.rand
          @random_theme
        elsif Rails.env.development? && respond_to?(:params) && params[:user_theme] && File.exists?(Rails.root.join('public/designs/themes', params[:user_theme]))
          params[:user_theme]
        else
          if profile && !profile.theme.nil?
            profile.theme
          elsif environment
            environment.theme
          else
            if logger
              logger.warn("No environment found. This is weird.")
              logger.warn("Request environment: %s" % request.env.inspect)
              logger.warn("Request parameters: %s" % params.inspect)
            end

            # could not determine the theme, so return the default one
            'default'
          end
        end
      end
  end

  def theme_path
    if session[:user_theme]
      '/user_themes/' + current_theme
    elsif session[:theme]
      '/designs/themes/' + session[:theme]
    else
      '/designs/themes/' + current_theme
    end
  end
end