Module: PagesHelper
- Defined in:
- app/helpers/pages_helper.rb
Instance Method Summary collapse
-
#breadcrumbs(options = {}) ⇒ Object
Takes an array of pages which constitute the ancestors of the current page (page) and displays them in the requested format Options: * seperator: The text or character that will seperate the breadcrumbs.
-
#link_to_add_fields(name, f, association) ⇒ Object
Add extra fields for an object in a form, in this case page parts.
- #link_to_page(page, options = {}) ⇒ Object
-
#navigation(options = {}) ⇒ Object
Options: * root: The root of the navigation structure * current: The current_page if not @page * depth: The number of levels in the tree to traverse.
-
#page_content(options = {}) ⇒ Object
Return the specified page part content.
- #page_css(options = {}) ⇒ Object
- #page_js(options = {}) ⇒ Object
- #page_meta_description ⇒ Object
- #page_meta_keywords ⇒ Object
- #page_snippet(name, options = {}) ⇒ Object
-
#page_title(default_text) ⇒ Object
Set the page title.
-
#replace_title_for(page) ⇒ Object
Usage: You can use the attributes of any instance variables and insert them in the title For example: This is a page for {my_object:my_object_attribute} Requires that any object that will be used have a public whitelist method which returns an array of legal attributes (as this can be set by a user certain attributes should not be exposed).
Instance Method Details
#breadcrumbs(options = {}) ⇒ Object
Takes an array of pages which constitute the ancestors of the current page (page) and displays them in the requested format Options:
-
seperator: The text or character that will seperate the breadcrumbs. Defaults to “ » ”
-
format: choices are “ul” or “inline”. “ul” displays the breadcrumb links in an unordered list whilst “inline” displays them inline in a containing div. Defaults to “ul”
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'app/helpers/pages_helper.rb', line 144 def (={}) = { :breadcrumbs => @breadcrumbs, :page => @page, :seperator => ' » ', :format => :ul }.with_indifferent_access.merge() case [:format] when :inline content_tag :div, :class => :breadcrumbs do links = [:breadcrumbs].collect.with_index { |, i| link_to_page(, :class => "crumb_#{i}") + [:seperator].html_safe }.join().html_safe links += content_tag(:span, replace_title_for([:page]), :class => 'current_page') if [:page] end when :ul content_tag :ul, :class => :breadcrumbs do links = [:breadcrumbs].collect.with_index { |, i| content_tag(:li, link_to_page(, :class => "crumb_#{i}")) }.join().html_safe links += content_tag(:li, replace_title_for([:page]), :class => 'current_page') if [:page] end else 'Please choose one of :inline or :ul as a format' end end |
#link_to_add_fields(name, f, association) ⇒ Object
Add extra fields for an object in a form, in this case page parts
89 90 91 92 93 94 95 96 |
# File 'app/helpers/pages_helper.rb', line 89 def link_to_add_fields(name, f, association) new_object = f.object.class.reflect_on_association(association).klass.new fields = simple_fields_for(association, new_object, :child_index => "new_#{association}") do |builder| safe_concat(render(association.to_s.singularize + "_fields", :f => builder)) end link_to('Add', '#', :onclick => "add_fields(this, \"#{association}\", \"#{escape_javascript(fields)}\")", :class => 'icon add') end |
#link_to_page(page, options = {}) ⇒ Object
127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'app/helpers/pages_helper.rb', line 127 def link_to_page(page, ={}) # Arbitrarily chosen url to take precedence over controller and action if page.url.blank? if page.controller.blank? && page.action.blank? link_to replace_title_for(page), "/#{page.permalink}", else link_to replace_title_for(page), url_for(:controller => '/' + page.controller, :action => page.action), end else link_to replace_title_for(page), page.url, end end |
#navigation(options = {}) ⇒ Object
Options:
-
root: The root of the navigation structure
-
current: The current_page if not @page
-
depth: The number of levels in the tree to traverse. Defaults to 2
-
class: The class of the containing ul. Defaults to “”
-
id: The id of the containing id. Defaults to “”
-
link_current: Set to true if the current page should have a link. Defaults to false
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
# File 'app/helpers/pages_helper.rb', line 177 def (={}) = { :root => nil, :current => @page, :class => 'nav', :id => '', :include_root => false, :link_current => false, :depth => 2 }.with_indifferent_access.merge() current_user = nil unless defined?(current_user) if [:root].is_a?(NilClass) root_page = Page.published_or_hidden.viewable_by(current_user).root_only.first elsif [:root].is_a?(Page) root_page = [:root] elsif [:root].is_a?(String) root_page = Page.published_or_hidden.viewable_by(current_user).where('pages.url = :root or pages.permalink = :root', :root => [:root]).first elsif [:root].is_a?(Hash) [:root].stringify_keys! root_page = Page.published_or_hidden.viewable_by(current_user).where(:controller => [:root][:controller], :action => [:root][:action]).first else return "<p><em>Error:</em> Root must be a page, a permalink, a url or a hash containing the controller and action, got: #{root.class.to_s}.</p>".html_safe end return "<p><em>Error:</em> Root page not found: #{[:root]}</p>".html_safe unless root_page grouped_pages = root_page.self_and_descendants.viewable_by(current_user)..published.for_nav.group_by(&:parent_id) render 'pages/navigation', :options => , :root_page => root_page, :grouped_pages => grouped_pages, :level => 1 end |
#page_content(options = {}) ⇒ Object
Return the specified page part content
99 100 101 102 103 104 105 106 |
# File 'app/helpers/pages_helper.rb', line 99 def page_content(={}) = { :page => @page, :part => 'body' }.with_indifferent_access.merge() [:page].page_parts.detect { |p| p.title == [:part].to_s } if [:page] end |
#page_css(options = {}) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'app/helpers/pages_helper.rb', line 28 def page_css(={}) = { :with_tags => true }.with_indifferent_access.merge() if [:with_tags] if @page && !@page.css.blank? content_tag :style do @page.css end end else @page.css end end |
#page_js(options = {}) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'app/helpers/pages_helper.rb', line 12 def page_js(={}) = { :with_tags => true }.with_indifferent_access.merge() if [:with_tags] if @page && !@page.js.blank? javascript_tag do @page.js end end else @page.js end end |
#page_meta_description ⇒ Object
52 53 54 55 56 57 58 |
# File 'app/helpers/pages_helper.rb', line 52 def if @page && !@page..blank? content_tag :meta, :name => 'description' do @page. end end end |
#page_meta_keywords ⇒ Object
44 45 46 47 48 49 50 |
# File 'app/helpers/pages_helper.rb', line 44 def if @page && !@page..blank? content_tag :meta, :name => 'keywords' do @page. end end end |
#page_snippet(name, options = {}) ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'app/helpers/pages_helper.rb', line 108 def page_snippet(name, ={}) = { :default_text => 'Page snippet not found', :tag => nil, :class => ['page_snippet'], :id => nil }.with_indifferent_access.merge() page_snippet = PageSnippet.find_by_name(name.to_s) if [:tag] content_tag [:tag], :class => [:class], :id => [:id] do page_snippet ? page_snippet : [:default_text] end else page_snippet ? page_snippet : [:default_text] end end |
#page_title(default_text) ⇒ Object
Set the page title
4 5 6 7 8 9 10 |
# File 'app/helpers/pages_helper.rb', line 4 def page_title(default_text) if @page replace_title_for(@page) else default_text end end |
#replace_title_for(page) ⇒ Object
Usage: You can use the attributes of any instance variables and insert them in the title For example:
This is a page for {{my_object:my_object_attribute}}
Requires that any object that will be used have a public whitelist method which returns an array of legal attributes (as this can be set by a user certain attributes should not be exposed)
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'app/helpers/pages_helper.rb', line 66 def replace_title_for(page) return nil unless page page.title.scan(/\{\{(\w+):(\w+)\}\}/).uniq.flatten.in_groups_of(2).each do |klass, attribute| if self.instance_variable_defined? "@#{klass}" obj = self.instance_variable_get "@#{klass}" if obj.class.public_methods.include?(:whitelist) && obj.class.whitelist.is_a?(Array) if obj.class.whitelist.include?(attribute) page.title.gsub!("{{#{klass}:#{attribute}}}", obj.send(attribute).to_s) else page.title.gsub!("{{#{klass}:#{attribute}}}", "'#{attribute}' not in whitelist for #{obj.class}") end else page.title.gsub!("{{#{klass}:#{attribute}}}", "Whitelist not defined for #{obj.class}") end else page.title.gsub!("{{#{klass}:#{attribute}}}", "Not found") end end page.title end |