Sha256: 58efa9fa10779d4e8ddd2d3c486416fd083124c3306e73152d096307e0362e4d

Contents?: true

Size: 1.41 KB

Versions: 1

Compression:

Stored size: 1.41 KB

Contents

module ThemesForRails
  module CommonMethods
    def theme_name
      @cached_theme_name ||= begin
        case @theme_name
        when Symbol then
          self.respond_to?(@theme_name, true) ? self.send(@theme_name) : @theme_name.to_s
        when String then @theme_name
        else
          nil
        end
      end
    end

    def theme_name=(name)
      @theme_name = name
    end

    # This calls from ControllerMethods module
    def set_theme(name)
      self.theme_name = name

      if valid_theme?
        add_theme_view_path
      end
    end

  protected

    # Generate path for assets by theme name and asset type
    def assets_path_for(theme_name, asset_type)
      File.join(ThemesForRails.config.themes_dir, theme_name, 'assets', asset_type.to_s)
    end

    # Check theme is valid
    def valid_theme?
      !self.theme_name.nil?
    end

    # Add view path for current theme
    def add_theme_view_path
      prepend_view_path(ActionView::FileSystemResolver.new(views_path_for(self.theme_name)))
    end

    def views_path_for(theme_name)
      File.join(ThemesForRails.config.themes_dir, theme_name, 'views')
    end
    
    def add_theme_assets_path
      @@theme_assets_path_cache ||= begin
        [:stylesheets, :javascripts, :images].each do |asset_type|
          Rails.application.assets.prepend_path(assets_path_for(self.theme_name, asset_type.to_s))
        end
        true
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
themes_for_rails-1.0.0rc1 lib/themes_for_rails/common_methods.rb