diff options
Diffstat (limited to 'app/helpers')
-rw-r--r-- | app/helpers/application_helper.rb | 91 | ||||
-rw-r--r-- | app/helpers/blog_helper.rb | 15 | ||||
-rw-r--r-- | app/helpers/haml_helper.rb | 54 | ||||
-rw-r--r-- | app/helpers/navigation_helper.rb | 84 |
4 files changed, 244 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 diff --git a/app/helpers/blog_helper.rb b/app/helpers/blog_helper.rb new file mode 100644 index 0000000..20e4113 --- /dev/null +++ b/app/helpers/blog_helper.rb @@ -0,0 +1,15 @@ +module BlogHelper + + def recent_blog_summaries(path) + root = StaticPage.find(path) + if root + pages = root.all_children.order_by(:posted_at).limit(PAGINATION_SIZE) + haml do + pages.each do |page| + haml render(:partial => 'layouts/blog/summary', :locals => {:page => page}) + end + end + end + end + +end diff --git a/app/helpers/haml_helper.rb b/app/helpers/haml_helper.rb new file mode 100644 index 0000000..dae0149 --- /dev/null +++ b/app/helpers/haml_helper.rb @@ -0,0 +1,54 @@ +require 'pathname' + +module HamlHelper + + # + # acts like haml_tag, capture_haml, or haml_concat, depending on how it is called. + # + # two or more args --> like haml_tag + # one arg and a block --> like haml_tag + # zero args and a block --> like capture_haml + # one arg and no block --> like haml_concat + # + # additionally, we allow the use of more than one class. + # + # some examples of these usages: + # + # def display_robot(robot) + # haml do # like capture_haml + # haml '.head', robot.head_html # like haml_tag + # haml '.head' do # same + # haml robot.head_html + # end + # haml '.body.metal', robot.body_html # like haml_tag, but with multiple classes + # haml '<a href="/x">link</a>' # like haml_concat + # end + # end + # + # wrapping the helper in a capture_haml call is very useful, because then + # the helper can be used wherever a normal helper would be. + # + def haml(name=nil, *args, &block) + if name + if args.empty? and block.nil? + haml_concat name + else + if name =~ /^(.*?\.[^\.]*)(\..*)$/ + # allow chaining of classes if there are multiple '.' in the first arg + name = $1 + classes = $2.gsub('.',' ') + hsh = args.detect{|i| i.is_a?(Hash)} + unless hsh + hsh = {} + args << hsh + end + hsh[:class] = classes + end + haml_tag(name, *args, &block) + end + else + capture_haml(&block) + end + end + +end
\ No newline at end of file diff --git a/app/helpers/navigation_helper.rb b/app/helpers/navigation_helper.rb new file mode 100644 index 0000000..8582391 --- /dev/null +++ b/app/helpers/navigation_helper.rb @@ -0,0 +1,84 @@ +module NavigationHelper + + def has_side_column? + second_level_children_count = Menu.menu.submenu(current_page_path.first).try(:size) + if second_level_children_count.nil? + false + else + second_level_children_count > 1 + end + end + + def top_level_navigation_links + haml do + first = 'first' + Menu.menu.each do |item| + active = current_page_path.first == item.name ? 'active' : '' + haml 'li.tab', :class => first do + haml 'a.tab', I18n.t('pages.' + item.name), :href => menu_item_path(item), :class => active + end + first = '' + end + end + end + + def side_column_navigation_links + if menu = Menu.menu.submenu(current_page_path.first) + haml do + haml 'ul.nav.nav-tabs.nav-stacked' do + display_menu(menu, 1) + end + end + end + end + + def act_as(page) + page = StaticPage.find(page) + @current_page_path = page.path + render_page(page) + end + + private + + def menu_item_path(item) + "/#{I18n.locale}/#{item.path.join('/')}" + end + + def display_menu(menu, level=0) + menu.each do |item| + haml 'li', :class => path_active(current_page_path, item) do + haml 'a', I18n.t('pages.'+item.name), :href => menu_item_path(item), :class => "level#{level}" + end + display_menu(item.submenu, level+1) + end + end + + def path_active(page_path, menu_item) + array_starts_with(page_path, menu_item.path) ? 'active' : '' + end + + def current_page_path + @current_page_path ||= begin + if @page + @page.path + elsif params[:page].is_a? String + params[:page].split('/') + else + [] + end + end + end + + # + # returns true if first part of array_long contains array_short + # + def array_starts_with(array_long, array_short) + array_short.length.times do |i| + if array_short[i] != array_long[i] + return false + end + end + return true + end + +end |