Module: PagesHelper

Defined in:
app/helpers/pages_helper.rb

Instance Method Summary collapse

Instance Method Details

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(options={})
  default_options = {
    'breadcrumbs' => @breadcrumbs,
    'page' => @page,
    'seperator' => ' » ',
    'format' => 'ul'
  }.merge!(options.stringify_keys)

  case default_options['format'].to_s
    when 'inline'
       :div, :class => 'breadcrumbs' do
        links = default_options['breadcrumbs'].collect.with_index { |breadcrumb, i| link_to_page(breadcrumb, :class => "crumb_#{i}") + default_options['seperator'].html_safe }.join().html_safe
        links += (:span, replace_title_for(default_options['page']), :class => 'current_page') if default_options['page']
      end
      
    when 'ul'
       :ul, :class => 'breadcrumbs' do
        links = default_options['breadcrumbs'].collect.with_index { |breadcrumb, i| (:li, link_to_page(breadcrumb, :class => "crumb_#{i}")) }.join().html_safe
        links += (:li, replace_title_for(default_options['page']), :class => 'current_page') if default_options['page']
      end
      
    else
      'Please choose one of \'inline\' or \'ul\' as a format'
  end
end

#filter_select(target, options = {}) ⇒ Object



203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'app/helpers/pages_helper.rb', line 203

def filter_select(target, options={})
  default_options = {
    'builder' => nil,
    'object' => nil,
    'attribute' => nil  
  }.merge!(options.stringify_keys)
  
  if default_options['builder']
    default_options['builder'].select(default_options['attribute'], Page.filters, {}, { :class => 'filter', :rel => target })
  else
    select_tag(default_options['object'], default_options['attribute'], Page.filters, {}, { :class => 'filter', :rel => target })
  end
end

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


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, options={})
  # 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}", options
    else
      link_to replace_title_for(page), url_for(:controller => '/' + page.controller, :action => page.action), options
    end
  else
    link_to replace_title_for(page), page.url, options
  end
end

Options:

  • 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



176
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
# File 'app/helpers/pages_helper.rb', line 176

def navigation(permalink, options={})
  default_options = {
    'current' => @page,
    'class' => 'nav', 
    'id' => '',
    'include_root' => false,
    'link_current' => false,
    'depth' => 2
  }.merge!(options.stringify_keys)

  current_user = nil unless defined?(current_user)

  root_page = Page.published_or_hidden.viewable_by(current_user)
  
  if permalink
    root_page = root_page.find_by_permalink(permalink.to_s)
  else
    root_page = root_page.first
  end
  
  return "<p><em>Error:</em> Root page not found</p>".html_safe unless root_page

  grouped_pages = root_page.self_and_descendants.viewable_by(current_user).shown_in_menu.published.group_by(&:parent_id)

  render 'pages/navigation', :options => default_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(options={})
  default_options = {
    'page' => @page,
    'part' => 'body'
  }.merge!(options.stringify_keys)
  
  default_options['page'].page_parts.detect { |p| p.title == default_options['part'].to_s } if default_options['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(options={})
  default_options = {
    'with_tags' => true
  }.merge(options.stringify_keys)

  if default_options['with_tags']
    if @page && !@page.css.blank?
       :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(options={})
  default_options = {
    'with_tags' => true
  }.merge(options.stringify_keys)
  
  if default_options['with_tags']
    if @page && !@page.js.blank?
      javascript_tag do 
        @page.js
      end
    end
  else
    @page.js
  end
end

#page_meta_descriptionObject



52
53
54
55
56
57
58
# File 'app/helpers/pages_helper.rb', line 52

def page_meta_description
  if @page && !@page.meta_description.blank?
     :meta, :name => 'description' do 
      @page.meta_description
    end
  end
end

#page_meta_keywordsObject



44
45
46
47
48
49
50
# File 'app/helpers/pages_helper.rb', line 44

def page_meta_keywords
  if @page && !@page.meta_keywords.blank?
     :meta, :name => 'keywords' do 
      @page.meta_keywords
    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, options={})
  default_options = {
    'default_text' => 'Page snippet not found',
    'tag' => nil,
    'class' => ['page_snippet'],
    'id' => nil
  }.merge(options.stringify_keys)

  page_snippet = PageSnippet.find_by_name(name.to_s)
  
  if default_options['tag']
     default_options['tag'], :class => default_options['class'], :id => default_options['id'] do 
      page_snippet ? page_snippet : default_options['default_text']
    end
  else
    page_snippet ? page_snippet : default_options['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