Skip to content
This repository has been archived by the owner on Jun 22, 2020. It is now read-only.

Commit

Permalink
Initial checkin of code and patches spread across the interwebs
Browse files Browse the repository at this point in the history
  • Loading branch information
Alan Harper committed Aug 26, 2008
0 parents commit a71ce1f
Show file tree
Hide file tree
Showing 23 changed files with 736 additions and 0 deletions.
20 changes: 20 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Copyright (c) 2005 Matt McCray, based on code from Typo by Tobias Luetke

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWAR
202 changes: 202 additions & 0 deletions README
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
This theme_support version has been updated by Damien Le Berrigaud for Rails 2.0
http://www.webdrivenblog.com/

= Theme Support for Rails Applications

This plugin provides support for themes to the rails application environment.
It supports theme specific images, stylesheets, javascripts, and views. The
views can be in ERb (rhtml) or liquid formats. Optionally, you can configure
the theme system to ignore any templates except liquid ones.


== Usage

This plugin automatically makes any patches needed for theme support. You can
use the theme_generator to create the file structure needed, or create it
yourself.

It expects the following theme folder structure.

$app_root
themes/
[theme_name]
layouts/ <- layout .rhtml or .liquid templates
images/
stylesheets/
javascripts/
views/ <- you can override application views
about.markdown
preview.png

When run in production mode, it will automatically cache the theme files so that
the web-server will deliver them in subsequent requests.

It bears noting that, like Typo, this will mean your apache/fcgi process will need
write permissions. This could be a possible security vulnerability.

With that in mind, it is best to pre-cache all of the theme files by using the
included rake tasks:

$ rake theme_create_cache

The theme file cache generates the following file structure:

$app_root
public/
themes/
[theme_name]/
images/
stylesheets/
javascripts/

There are other rake tasks available:

- theme_create_cache
- theme_remove_cache
- theme_update_cache

You specify which theme to use in your controller by using the 'theme' helper.
It's used just like the 'layout' helper. In fact, you can use them both
simultaneously. The following will render actions using the 'default' layout
in the 'blue_bird' theme (theme/blue_bird/layouts/default.rhtml):

class ApplicationController < ActionController::Base
layout 'default'

theme 'blue_bird'

...
end

You can also defer the theme lookup to a controller method:

class ApplicationController < ActionController::Base
layout 'default'

theme :get_theme

def get_theme

# If you let the user choose their own theme, you might
# add a User#theme field...

current_user.theme
end

...
end


Note: By setting the theme in the ApplicationController you can set
the theme for the whole application.

In your application views, there are theme specific helper tags
available to you. For ERb templates they are:

- theme_image_tag
- theme_image_path
- theme_javascript_include_tag
- theme_javascript_path
- theme_stylesheet_link_tag
- theme_stylesheet_path

For liquid templates there is a single helper, themeitem, that will determine
the path base on the theme file extension. Here's how you'd use it:

<link rel="StyleSheet" href="{% themeitem %} default.css {% endthemeitem %}" />
...
<img src="{% themeitem %} logo.png {% endthemeitem %}" />

The output from those two calls are:

<link rel="StyleSheet" href="/themes/[current_theme]/stylesheets/default.css" />
...
<img src="/themes/[current_theme]/images/logo.png" />

New in version 1.4 is ActionMailer support. Note, this is still experimental. However,
if you would like your themes to be able to override your ActionMailer views, you can
send the theme in your deliver_* method call. For example, assuming we have an ActionMailer
class named Mailer, and have implemented theme_support as shown above, here's how you would
allowing themes in emails:


def send_email
Mailer.deliver_my_email( 'a variable', :theme=>get_theme )
end


== Contributors

The theme_support pluging includs patches from the following:

* agkr
* D.J. Vogel

Thanks guys!

== Changelog

1.4.0 - Better support for Rails 1.1+. Updated the liquid themeitem tag.
Liquid layouts are no longer generated by default.Added a couple
of patches. One allows theme sub-directories. For example, you
can have:

[theme_dir]
stylesheets/
admin/
main.css

Added experimental support for themeing ActionMailer classes.
They work as normal, if you want to all theme's to override the
.rhtml|.liquid views, send the theme in the deliver_* method. For
example:

Mailer.deliver_signup( user, :theme=>get_theme() )

In that example, `get_theme` is a method on the controller and at
the top we've used:

layout 'default'
theme :get_theme

1.3.0 - The theme_system component is no longer needed. All of the
theme support is driven by a single plugin named, oddly enough,
'theme_support'. Also improved theme route support. Instead of
overriding RouteSet#reload, RouteSet#draw is overridden, making
the theme support entirely transparent -- hopefully ;-)

1.2.2 - More Bug-fixes.

1.2.1 - Bug-fixes and documentation clean up.

1.2.0 - Updated actionview_ex with the new render_file additions from
Typo. Renamed the rake tasks so that they all start with
'theme' (theme_create_cache, theme_remove_cache,
theme_update_cache). You can update the system files by running:

$ ./script/generate theme _system_

Full support for Liquid templates, as well as an option to only
allow Liquid templates in a theme.

1.1.1 - Added rake tasks for pre-caching the themes, and removing the
cached themes.

1.1.0 - [Breaking] Abstraction of the Typo theme system. The themes are
now the same as is used in Typo. The theme engine itself is a
combination of plugins and a component. No more symlinks, thank
goodness.

1.0.2 - The current_theme is now retrieved from the controller. Where
symlinks are created on *nix systems, shortcuts are created
on Windows if Win32Utils is installed.

1.0.1 - Added 'themes' directory, theme definition file, and symlinks
to the appropriate directories on supported platforms.

1.0.0 - Initial release


---
Copyright (c) 2005 Matt McCray, based on code from Typo by Tobias Luetke
released under the MIT license
15 changes: 15 additions & 0 deletions generators/theme/USAGE
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
NAME
theme - Creates the folder structure for a new theme

SYNOPSIS
theme [theme_name]

DESCRIPTION
This generator creates the folder structure for a new theme. It creates all of
the folders for your theme content (images, stylesheets, javascripts, layouts,
and views).

EXAMPLE
./script/generate theme default

This will generate the file structure for a theme named 'default'.
5 changes: 5 additions & 0 deletions generators/theme/templates/about.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
### <%= class_name.underscore.humanize.titleize %>

Author: *Me*

This description can be found in themes/<%= file_name %>/about.markdown
11 changes: 11 additions & 0 deletions generators/theme/templates/layout.liquid
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title></title>
<link href="{% themeitem %} <%= file_name %>.css {% endthemeitem %}" media="screen" rel="Stylesheet" type="text/css" />
</head>
<body>
{{ content_for_layout }}
</body>
</html>
11 changes: 11 additions & 0 deletions generators/theme/templates/layout.rhtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title></title>
<%%= theme_stylesheet_link_tag '<%= file_name %>' %>
</head>
<body>
<%%= @content_for_layout %>
</body>
</html>
Binary file added generators/theme/templates/preview.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions generators/theme/templates/theme.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/*
Main StyleSheet for the '<%= class_name %>' theme
*/

BODY {

}
4 changes: 4 additions & 0 deletions generators/theme/templates/theme.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
enabled: true
title: <%= class_name.underscore.humanize.titleize %>
author: Matt McCray
description: This is the description... All light and fluffy.
13 changes: 13 additions & 0 deletions generators/theme/templates/views_readme
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Overriding Views

You can override views by putting custom `rhtml` or `liquid`
templates in this directory. You use the same folder structure
as Rails:

views/
[controller_name]/
_overriden_partial.rhtml
overriden_action.rhtml

*Note:* These are overrides! They will only work if they have
a matching view in the main rails `app/views` folder.
27 changes: 27 additions & 0 deletions generators/theme/theme_generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class ThemeGenerator < Rails::Generator::NamedBase

def manifest
record do |m|
# Theme folder(s)
m.directory File.join( "themes", file_name )
# theme content folders
m.directory File.join( "themes", file_name, "images" )
m.directory File.join( "themes", file_name, "javascript" )
m.directory File.join( "themes", file_name, "layouts" )
m.directory File.join( "themes", file_name, "views" )
m.directory File.join( "themes", file_name, "stylesheets" )
# Default files...
# about
m.template 'about.markdown', File.join( 'themes', file_name, 'about.markdown' )
# image
m.file 'preview.png', File.join( 'themes', file_name, 'images', 'preview.png' )
# stylesheet
m.template "theme.css", File.join( "themes", file_name, "stylesheets", "#{file_name}.css" )
# layouts
m.template 'layout.rhtml', File.join( 'themes', file_name, 'layouts', 'default.rhtml' )
#m.template 'layout.liquid', File.join( 'themes', file_name, 'layouts', 'default.liquid' )
# view readme
m.template 'views_readme', File.join( 'themes', file_name, 'views', 'views_readme.txt' )
end
end
end
18 changes: 18 additions & 0 deletions init.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Initializes theme support by extending some of the core Rails classes
require 'patches/actionview_ex'
require 'patches/actioncontroller_ex'
require 'patches/actionmailer_ex'
require 'patches/routeset_ex'

# Add the tag helpers for rhtml and, optionally, liquid templates
require 'helpers/rhtml_theme_tags'

# Commented out to remove the message
# "Liquid doesn't seem to be loaded... uninitialized constant Liquid"

#begin
# require 'helpers/liquid_theme_tags'
#rescue
# # I guess Liquid isn't being used...
# STDERR.puts "Liquid doesn't seem to be loaded... #{$!}"
#end
31 changes: 31 additions & 0 deletions lib/helpers/liquid_theme_tags.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# A Liquid Tag for retrieving path information for theme specific media
#
# Returns the path based on the file extension
#
class Themeitem < Liquid::Block

@@image_exts = %w( .png .jpg .jpeg .jpe .gif )
@@stylesheet_exts = %w( .css )
@@javascript_exts = %w( .js .htc )

def render(context)
# Which, if either, of these are correct?
base_url = context['request'].relative_url_root || ActionController::Base.asset_host.to_s
theme_name = @theme_name || context['active_theme']

filename = @nodelist.join('').strip
ext = File.extname( filename )

if @@image_exts.include?( ext )
"#{base_url}/themes/#{theme_name}/images/#{filename}"

elsif @@stylesheet_exts.include?( ext )
"#{base_url}/themes/#{theme_name}/stylesheets/#{filename}"

elsif @@javascript_exts.include?( ext )
"#{base_url}/themes/#{theme_name}/javascript/#{filename}"
end
end
end

Liquid::Template.register_tag( 'themeitem', Themeitem )
Loading

0 comments on commit a71ce1f

Please sign in to comment.