summaryrefslogtreecommitdiff
path: root/app/helpers/application_helper.rb
diff options
context:
space:
mode:
Diffstat (limited to 'app/helpers/application_helper.rb')
-rw-r--r--app/helpers/application_helper.rb91
1 files changed, 91 insertions, 0 deletions
diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb
new file mode 100644
index 0000000..903549b
--- /dev/null
+++ b/app/helpers/application_helper.rb
@@ -0,0 +1,91 @@
+module ApplicationHelper
+
+ def main_column_class
+ if has_side_column?
+ 'with_side_column'
+ else
+ 'full'
+ end
+ end
+
+ #
+ # two forms:
+ #
+ # (1) link('page-name')
+ # (2) link('label' => 'page-name')
+ #
+ # both accept optional options hash:
+ #
+ # (1) link('page-name', :class => 'x')
+ # (2) link('label' => 'page-name', :class => 'x')
+ #
+ def link(name, options=nil)
+ if name.is_a? Hash
+ klass = name.delete(:class)
+ label, name = name.to_a.first
+ if label.is_a? Symbol
+ label = I18n.t label
+ end
+ else
+ klass = options[:class] if options
+ end
+ if name.starts_with?('#')
+ path = name
+ else
+ page = StaticPage.find(name)
+ if page
+ label ||= page.title
+ path = page_path(page)
+ else
+ label = '[dead link]'
+ path = '/'
+ end
+ end
+ link_to label, path, {:class => klass}
+ end
+
+ def page_path(page)
+ '/' + ([I18n.locale.to_s] + page.path).join('/')
+ end
+
+ #
+ # allows a template to render a partial giving a relative path (relative to the calling template)
+ #
+ def render_local_template(arg, locals={})
+ caller_path = Pathname.new(caller.first.gsub(/^([^:]*).*$/, '\1'))
+ doc_root = Pathname.new(Rails.root + 'app/views')
+ template_path = File.dirname(caller_path.relative_path_from(doc_root).to_s) + '/' + arg
+ begin
+ render :template => template_path, :locals => locals
+ rescue ActionView::MissingTemplate => exc
+ "<!-- template missing %s -->" % template_path
+ end
+ end
+
+ def page_title
+ if @page
+ @page.props.title || @page.title
+ else
+ ""
+ end
+ end
+
+ #
+ # renders the content of a static page
+ # this is an ugly duplicate of the application controller render_page, because
+ # they call two different 'render' methods (controller and view renders behave differently).
+ # TODO: figure out how to combine into one helper_method.
+ #
+ def render_page(page)
+ begin
+ render :template => page.template_path
+ rescue ActionView::MissingTemplate => exc
+ begin
+ render :template => page.template_path(DEFAULT_LOCALE)
+ rescue
+ raise exc
+ end
+ end
+ end
+
+end