Module: Ramaze::Helper::Navigation

Defined in:
lib/cortex_reaver/helper/navigation.rb

Overview

Provides navigation rendering shortcuts

Instance Method Summary collapse

Instance Method Details

#author_info(model) ⇒ Object

An HTML author/creation line for a model



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/cortex_reaver/helper/navigation.rb', line 7

def author_info(model)
  s = '<span class="author-info">'
  if model.respond_to? :creator and creator = model.creator
    c = creator
  end
  if model.respond_to? :updater and updater = model.updater
    u = updater
  end

  s << '<span class="creator">'
  s << user_link(model, :creator)
  s << '</span>'
  s << ' on <span class="date">'
  s << model.created_on.strftime('%e %B %Y')
  s << '</span>'

  ct = model.created_on
  ut = model.updated_on
  unless ut.year == ct.year and ut.month == ct.month and ut.day == ct.day
    # Show the update time as well
    if u.nil? or c == u
      s << ' (updated'
    else
      s << ' (updated by '
      s << '<span class="updater">'
      s << user_link(model, :updater)
      s << '</span>'
    end
    s << ' on <span class="date">'
    s << ut.strftime('%e %B %Y')
    s << '</span>)'
  end
  s << '</span>'
end

#full_url(relative) ⇒ Object

Returns a full URL from a relative one.



43
44
45
# File 'lib/cortex_reaver/helper/navigation.rb', line 43

def full_url(relative)
  "#{CortexReaver.config.site.url.chomp('/')}#{relative}"
end

#model_nav(model) ⇒ Object

Returns a list with next/up/previous links for the record.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/cortex_reaver/helper/navigation.rb', line 48

def model_nav(model)
  if not (model.respond_to? :next and model.respond_to? :absolute_window_url)
    # Not applicable
    return
  elsif CortexReaver::User === model or CortexReaver::Page === model
    return
  end

  n = '<ul class="' + model.class.to_s.underscore + ' navigation actions">'
  if model.previous
    n << '  <li><a class="previous" href="' + model.previous.url + '">&laquo; Previous ' + model.class.to_s.demodulize + '</a></li>'
  end
  n << '  <li><a class="up" href="' + model.absolute_window_url + '">Back to ' + model.class.to_s.demodulize.pluralize + '</a></li>'
  if user.can_edit? model
    n << '  <li><a class="edit" href="' + r(:edit, model.id).to_s + '">Edit</a></li>'
  end
  if model.next
    n << '  <li><a class="next" href="' + model.next.url + '">Next ' + model.class.to_s.demodulize + ' &raquo;</a></li>'
  end
  n << '</ul>'
end

Generate pagination links from a Sequenceable class and index. The index can be :first or :last for the corresponding pages, an instance of the class (in which case the page which would contain that instance is highlighted, or a page number. Limit determines how many numeric links to include–use :all to include all pages.



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/cortex_reaver/helper/navigation.rb', line 75

def page_nav(klass, index = nil, limit = 15)
  # Translate :first, :last into corresponding windows.
  case index
  when :first
    page = 0
  when :last
    page = klass.window_count - 1
  when klass
    # Index is actually an instance of the target class
    page = index.window_absolute_index
  else
    # Index is a page number
    page = index.to_i
  end

  pages = Array.new
  links = '<ol class="pagination actions">'
  window_count = klass.window_count

  # Determine which pages to create links to
  if limit.kind_of? Integer and window_count > limit
    # There are more pages than we can display.

    # The desired number of previous or next pages
    previous_pages = (Float(limit - 1) / 2).floor
    next_pages = (Float(limit - 1) / 2).ceil

    # Shift window near edges
    first = page - previous_pages
    last = page + next_pages
    if first < 0
      last -= first
      first = 0
    elsif last > (window_count - 1)
      first -= last - (window_count - 1)
      last = window_count - 1
    end

    # Build pages
    pages = (first .. last).to_a

    # Check for elided segments
    if pages[0] != 0
      pages[0] = 0
      pages[1] = :elided
    end
    if pages[-1] != window_count - 1
      pages[-1] = window_count - 1
      pages[-2] = :elided
    end
  else
    # The window encompasses the entire set of pages
    pages = (0 ... window_count).to_a
  end

  if page > 0
    # Add "previous page" link.
    links << "<li><a class=\"previous\" href=\"#{klass.url}/page/#{page - 1}\">&laquo;</a></li>"
  else
    links << "<li class=\"placeholder\"><span class=\"previous\"></span></li>"
  end

  # Convert pages to links
  unless pages.empty?
    pages.inject(pages.first - 1) do |previous, i|
      if i == :elided
        # These pages are not side-by-side.
        links << '<li class="elided"><span>&hellip;</span></li>'
      elsif i == page
        # This is a link to the current page.
        links << "<li class=\"current\"><span>#{i + 1}</span></li>"
      else
        # This is a link to a different page.
        links << "<li><a href=\"#{klass.url}/page/#{i}\">#{i + 1}</a></li>"
      end

      # Remember this as the previous page.
      i
    end
  end

  if page < klass.window_count - 1
    # Add "next page" link.
    links << "<li><a class=\"next\" href=\"#{klass.url}/page/#{page + 1}\">&raquo;</a></li>"
  else
    links << "<li class=\"placeholder\"><span class=\"next\"></span></li>"
  end

  links << '</ol>'
end

#section_nav(sections) ⇒ Object

Produces a section navigation list from an array of titles to urls.



167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/cortex_reaver/helper/navigation.rb', line 167

def section_nav(sections)
  s = "<ul class=\"sections actions\">\n"
  sections.each do |section|
    title = section.first
    url = section.last.to_s
    klass = url.gsub(/\//, '').gsub(/_/, '-')
    s << '<li class="' + klass
    s << ' current' if request.request_uri[url]
    s << '"><a href="' + attr_h(url) + '">'
    s << title
    s << "</a></li>\n"
  end
  s << "\n</ul>"
end

Returns a link to a user.



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
209
210
211
212
213
214
215
216
217
# File 'lib/cortex_reaver/helper/navigation.rb', line 183

def user_link(x, who=:creator)
  case x
  when CortexReaver::User
    name = x.name || x.
    "<a href=\"#{attr_h x.url}\">#{h name}</a>"
  when CortexReaver::Comment
    if x.send(who)
      # Use attached creator/whoever
      user_link x.send(who)
    else
      # Use anonymous info
      name = x.name || x.email || 'Anonymous'
      if x.email
        s = "<a href=\"mailto:#{attr_h x.email}\">#{h name}</a>"
        if x.http
          s << " (<a href=\"#{attr_h x.http}\" rel=\"nofollow\">#{h x.http}</a>)"
        end
        s
      elsif x.http
        "<a href=\"#{attr_h x.http}\" rel=\"nofollow\">#{h name}</a>"
      else
        h name
      end
    end
  else
    if x.respond_to? who
      user_link x.send(who)
    elsif x.nil?
      # Hmm.
      ''
    else
      raise ArgumentError.new("don't know how to make a user link to #{x.inspect}")
    end
  end
end