Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add current theme function

  • Loading branch information...
commit 1f7d22f1b0a52ce4b9ca3bd72a73e2f51a4332ed 1 parent 3a03b49
@zires authored
View
13 lib/theme_park/rails/action_view/template_renderer.rb
@@ -0,0 +1,13 @@
+# encoding: utf-8
+class ActionView::TemplateRenderer
+
+ alias_method :_render_template_original, :render_template
+
+ def render_and_assign_template(template, layout_name = nil, locals = {})
+ @view.controller.current_template = template.identifier
+ _render_template_original(template, layout_name, locals)
+ end
+
+ alias_method :render_template, :render_and_assign_template
+
+end
View
28 lib/theme_park/rails/asset_tag_helper.rb
@@ -4,13 +4,11 @@ module Rails
module AssetTagHelper
extend ActiveSupport::Concern
- #include Sprockets::Helpers::RailsHelper
-
#
def theme_stylesheet_link_tag(*sources)
options = sources.extract_options!
theme = options.delete(:theme)
- if serve_static_assets? or !sprockets?
+ if serve_theme_compiled?
sources.collect do |source|
stylesheet_link_tag theme_compiled_path(source, theme)
end.join("\n").html_safe
@@ -23,7 +21,7 @@ def theme_stylesheet_link_tag(*sources)
def theme_javascript_include_tag(*sources)
options = sources.extract_options!
theme = options.delete(:theme)
- if serve_static_assets? or !sprockets?
+ if serve_theme_compiled?
sources.collect do |source|
javascript_include_tag theme_compiled_path(source, theme)
end.join("\n").html_safe
@@ -47,17 +45,31 @@ def theme_compiled_path(source, theme)
# sprocket assets pipeline enabled or not.
def assets_enabled?
- Rails.application.config.assets.enabled
+ ::Rails.application.config.assets.enabled
end
# serve static assets or not.
def serve_static_assets?
- Rails.application.config.serve_static_assets
+ ::Rails.application.config.serve_static_assets
end
# use sprockets or not.
- def sprockets?
- assets_enabled? && Rails.application.config.assets.digest.present?
+ def assets_digest?
+ ::Rails.application.config.assets.digest.present?
+ end
+
+ def serve_theme_compiled?
+ serve_static_assets? and assets_digest?
+ end
+
+ # Note that this method return the theme name may not correct.
+ # In some use case, you need override this method.
+ def current_theme
+ if view_paths.first.to_s.gsub(::Rails.root.to_s, '') =~ /#{ThemePark.root.chomp('/')}\/(.+)\/views/
+ $1
+ else
+ ''
+ end
end
end
View
12 lib/theme_park/railtie.rb
@@ -20,11 +20,21 @@ class Railtie < ::Rails::Railtie
end
ActiveSupport.on_load(:action_view) do
- ActionView::Helpers::AssetTagHelper.send :include, ThemePark::Rails::AssetTagHelper
+ include ThemePark::Rails::AssetTagHelper
+
+ # Make active_view can get current template's identifier.
+ # Uncomment when it is stable.
+ # require 'theme_park/rails/action_view/template_renderer'
end
ActiveSupport.on_load(:action_controller) do
include ThemePark::Rails::ActionController
+
+ # Make active_view can get current template's identifier.
+ # Uncomment when it is stable.
+ # ActionController::Base.class_eval do
+ # attr_accessor :current_template
+ # end
end
config.to_prepare do
Please sign in to comment.
Something went wrong with that request. Please try again.