Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

merge idearise digest_for routine #78

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,20 @@ GEM
activesupport (3.0.11)
arel (2.0.10)
builder (2.1.2)
contest (0.1.2)
contest (0.1.3)
erubis (2.6.6)
abstract (>= 1.0.0)
i18n (0.5.0)
json (1.6.5)
json (1.7.3)
mail (2.2.19)
activesupport (>= 2.3.6)
i18n (>= 0.4.0)
mime-types (~> 1.16)
treetop (~> 1.4.8)
mime-types (1.17.2)
mocha (0.9.12)
metaclass (0.0.1)
mime-types (1.18)
mocha (0.11.4)
metaclass (~> 0.0.1)
polyglot (0.3.3)
rack (1.2.5)
rack-mount (0.6.14)
Expand All @@ -71,21 +73,20 @@ GEM
rake (0.9.2.2)
rdoc (3.12)
json (~> 1.4)
sqlite3 (1.3.5)
test-unit (2.2.0)
sqlite3 (1.3.6)
test-unit (2.4.8)
thor (0.14.6)
treetop (1.4.10)
polyglot
polyglot (>= 0.3.1)
tzinfo (0.3.31)
tzinfo (0.3.33)

PLATFORMS
ruby

DEPENDENCIES
contest
mocha
rails (= 3.0.11)
sqlite3
test-unit
themes_for_rails!
26 changes: 15 additions & 11 deletions lib/themes_for_rails/action_view.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# encoding: utf-8
module ThemesForRails

module ActionView

extend ActiveSupport::Concern
Expand All @@ -10,35 +10,39 @@ module ActionView
end

def current_theme_stylesheet_path(asset)
base_theme_stylesheet_path(:theme => self.theme_name, :asset => "#{asset}.css")
base_theme_stylesheet_path(:theme => self.theme_name, :asset => digest_for_stylesheet("#{asset}.css", self.theme_name))
end

def current_theme_javascript_path(asset)
base_theme_javascript_path(:theme => self.theme_name, :asset => "#{asset}.js")
base_theme_javascript_path(:theme => self.theme_name, :asset => digest_for_javascript("#{asset}.js", self.theme_name))
end

def current_theme_image_path(asset)
base_theme_image_path(:theme => self.theme_name, :asset => asset)
image, extension = name_ext(asset)
base_theme_image_path(:theme => self.theme_name, :asset => digest_for_image("#{image}.#{extension}", self.theme_name))
end

def theme_stylesheet_path(asset, new_theme_name = self.theme_name)
base_theme_stylesheet_path(:theme => new_theme_name, :asset => "#{asset}.css")
base_theme_stylesheet_path(:theme => new_theme_name, :asset => digest_for_stylesheet("#{asset}.css", new_theme_name))
end

def theme_javascript_path(asset, new_theme_name = self.theme_name)
base_theme_javascript_path(:theme => new_theme_name, :asset => "#{asset}.js")
base_theme_javascript_path(:theme => new_theme_name, :asset => digest_for_javascript("#{asset}.js", new_theme_name))
end

def theme_image_path(asset, new_theme_name = self.theme_name)
base_theme_image_path(:theme => new_theme_name, :asset => asset)
image, extension = name_ext(asset)
base_theme_image_path(:theme => new_theme_name, :asset => digest_for_image("#{image}.#{extension}", new_theme_name))
end

def theme_image_tag(source, options = {})
image_tag(theme_image_path(source), options)
image, extension = name_ext(source)
image_tag(theme_image_path(digest_for_image("#{image}.#{extension}", self.theme_name)), options)
end

def theme_image_submit_tag(source, options = {})
image_submit_tag(theme_image_path(source), options)
image, extension = name_ext(source)
image_submit_tag(theme_image_path(digest_for_image("#{image}.#{extension}", self.theme_name)), options)
end

def theme_javascript_include_tag(*files)
Expand Down
43 changes: 37 additions & 6 deletions lib/themes_for_rails/common_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ module CommonMethods
def theme_name
@cached_theme_name ||= begin
case @theme_name
when Symbol then
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
Expand All @@ -26,23 +26,54 @@ def set_theme(name)
add_theme_view_path
end
end

public

def valid_theme?
!self.theme_name.nil?
end

# will add the view path for the current theme
def add_theme_view_path
add_theme_view_path_for(self.theme_name)
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(theme_view_path_for(name))
end

def digest_for_image(asset, theme_context)
if Rails.application.config.respond_to?('assets') && Rails.application.config.assets.digests
File.basename(Rails.application.config.assets.digests["#{theme_context}/images/#{asset}"])
else
asset
end
end

def digest_for_javascript(asset, theme_context)
if Rails.application.config.respond_to?('assets') && Rails.application.config.assets.digests
File.basename(Rails.application.config.assets.digests["#{theme_context}/javascripts/#{asset}"])
else
asset
end
end

def digest_for_stylesheet(asset, theme_context)
if Rails.application.config.respond_to?('assets') && Rails.application.config.assets.digests
File.basename(Rails.application.config.assets.digests["#{theme_context}/stylesheets/#{asset}"])
else
asset
end
end

def name_ext(file_name)
ext = File.extname(file_name)
name = File.basename(file_name, ext)
ext.slice!(0) if ext.length > 0
return name, ext
end

def public_theme_path
theme_view_path("/")
end
Expand Down
6 changes: 3 additions & 3 deletions lib/themes_for_rails/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ def themes_for_rails
constraints = { :theme => /[\w\.]*/ }

match "#{theme_dir}/:theme/stylesheets/*asset" => 'themes_for_rails/assets#stylesheets',
:as => :base_theme_stylesheet, :constraints => constraints
:as => :base_theme_stylesheet, :constraints => constraints, :via => [:get]
match "#{theme_dir}/:theme/javascripts/*asset" => 'themes_for_rails/assets#javascripts',
:as => :base_theme_javascript, :constraints => constraints
:as => :base_theme_javascript, :constraints => constraints, :via => [:get]
match "#{theme_dir}/:theme/images/*asset" => 'themes_for_rails/assets#images',
:as => :base_theme_image, :constraints => constraints
:as => :base_theme_image, :constraints => constraints, :via => [:get]
end

end
Expand Down
16 changes: 8 additions & 8 deletions lib/themes_for_rails/url_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,24 @@ module ThemesForRails
module UrlHelpers

extend ActiveSupport::Concern

included do
helper_method :current_theme_stylesheet_path,
:current_theme_javascript_path,
helper_method :current_theme_stylesheet_path,
:current_theme_javascript_path,
:current_theme_image_path
end

def current_theme_stylesheet_path(asset)
base_theme_stylesheet_path(:theme => self.theme_name, :asset => "#{asset}.css")
base_theme_stylesheet_path(:theme => self.theme_name, :asset => digest_for_stylesheet("#{asset}.css", self.theme_name))
end

def current_theme_javascript_path(asset)
base_theme_javascript_path(:theme => self.theme_name, :asset => "#{asset}.js")
base_theme_javascript_path(:theme => self.theme_name, :asset => digest_for_javascript("#{asset}.js", self.theme_name))
end

def current_theme_image_path(asset)
image, extension = asset.split(".")
base_theme_image_path(:theme => self.theme_name, :asset => "#{image}.#{extension}")
image, extension = name_ext(asset)
base_theme_image_path(:theme => self.theme_name, :asset => digest_for_image("#{image}.#{extension}", self.theme_name))
end

end
Expand Down
1 change: 0 additions & 1 deletion themes_for_rails.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,4 @@ Gem::Specification.new do |gem|
gem.add_development_dependency "test-unit"
gem.add_development_dependency "contest"
gem.add_development_dependency "mocha"
gem.add_development_dependency('rails', ["= 3.0.11"])
end