Skip to content

Commit

Permalink
reduced complexity of working with theme path
Browse files Browse the repository at this point in the history
  • Loading branch information
tanraya committed Oct 8, 2011
1 parent 4bb02a5 commit d3297d5
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 69 deletions.
3 changes: 2 additions & 1 deletion lib/themes_for_rails.rb
Expand Up @@ -19,7 +19,8 @@ def config
end

def available_themes(&block)
Dir.glob(File.join(config.themes_path, "*"), &block)
#Dir.glob(File.join(config.themes_path, "*"), &block)
Dir.glob(File.join(config.themes_dir, "*"), &block)
end

alias each_theme_dir available_themes
Expand Down
36 changes: 8 additions & 28 deletions lib/themes_for_rails/common_methods.rb
@@ -1,9 +1,5 @@
module ThemesForRails
module CommonMethods
def view_path_for(theme)
File.join(theme_path_for(theme), "views")
end

def theme_name
@cached_theme_name ||= begin
case @theme_name
Expand All @@ -25,45 +21,29 @@ def set_theme(name)

if valid_theme?
add_theme_view_path
add_theme_assets_path
end
end

public
protected

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

# will add the view path for the current theme
# Add view path for current theme
def add_theme_view_path
add_assets_path_for(self.theme_name)
add_theme_view_path_for(self.theme_name)
self.view_paths.insert 0, ActionView::FileSystemResolver.new("#{ThemesForRails.config.themes_dir}/#{self.theme_name}/views")
end

# will add the view path for a given theme name
def add_theme_view_path_for(name)
self.view_paths.insert 0, ActionView::FileSystemResolver.new(view_path_for(name))
end

# Add paths for Sprockets
def add_assets_path_for(name)
assets_dir = File.join(ThemesForRails.config.base_dir, ThemesForRails.config.themes_dir, name, 'assets')
# Add assets path for current theme
def add_theme_assets_path
assets_dir = File.join(Rails.root.to_s, ThemesForRails.config.themes_dir, self.theme_name, 'assets')

[:stylesheets, :javascripts, :images].each do |asset_type|
Rails.application.assets.append_path(File.join(assets_dir, asset_type.to_s))
end
end

def public_theme_path
theme_path("/")
end

def theme_path(base = ThemesForRails.config.base_dir)
theme_path_for(theme_name, base)
end

def theme_path_for(name, base = ThemesForRails.config.base_dir, theme_dir = ThemesForRails.config.themes_dir)
File.join(base, theme_dir, name)
end
end
end
27 changes: 10 additions & 17 deletions lib/themes_for_rails/config.rb
@@ -1,37 +1,30 @@
module ThemesForRails
class Config

attr_writer :base_dir, :themes_dir
attr_writer :themes_dir #:base_dir,
#attr_accessor :use_sass

def initialize(&block)
#@use_sass = true
yield if block_given?
end

def base_dir
@base_dir ||= Rails.root
end
#def base_dir
# @base_dir ||= Rails.root
#end

def themes_dir
@themes_dir ||= "app/themes"
@themes_dir = File.join(Rails.root, @themes_dir)
@themes_dir
end

def themes_path
File.join(base_dir, themes_dir)
end
#def themes_path
# File.join(Rails.root, themes_dir)
#end

def clear
@base_dir = nil
#@base_dir = nil
@themes_dir = nil
end

#def use_sass?
# @use_sass and sass_is_available?
#end

#def sass_is_available?
# !!defined?(Sass::Plugin)
#end
end
end
15 changes: 0 additions & 15 deletions spec/common_methods_spec.rb
Expand Up @@ -7,19 +7,4 @@
@common.theme_name = "awesome"
ThemesForRails.config.clear
end

it 'should use config base_dir to build theme path' do
ThemesForRails.config.base_dir ='some_path'
@common.theme_path.should match(/some_path/)
end

it 'should use config themes_dir to build theme path' do
ThemesForRails.config.themes_dir ='skinner'
@common.theme_path.should match(/skinner/)
end

it 'should use config base_dir to build theme path for theme' do
ThemesForRails.config.base_dir ='some_path'
@common.theme_path_for('doodley').should match(/some_path/)
end
end
8 changes: 0 additions & 8 deletions spec/config_spec.rb
Expand Up @@ -5,14 +5,6 @@
ThemesForRails.config.clear
end

it "should change the base directory" do
ThemesForRails.config do |config|
config.base_dir = 'app/empty_themes'
end

ThemesForRails.available_theme_names.should be_empty
end

it 'should change the directory to views' do
ThemesForRails.config do |config|
config.themes_dir = 'app/themes'
Expand Down

0 comments on commit d3297d5

Please sign in to comment.