layout: move header links logic to helper

This commit is contained in:
joshua stein 2017-07-13 15:56:32 -05:00
parent 0ad6dffbe7
commit b46640ca81
2 changed files with 78 additions and 34 deletions

View File

@ -33,6 +33,77 @@ module ApplicationHelper
raw(html)
end
def header_links
return @header_links if @header_links
@header_links = {
"/" => { :title => @cur_url == "/" ? Rails.application.name : "Home" },
"/recent" => { :title => "Recent" },
"/comments" => { :title => "Comments" },
}
if @user
@header_links.merge!({ "/threads" => { :title => "Your Threads" } })
end
if @user && @user.can_submit_stories?
@header_links.merge!({
"/stories/new" => { :title => "Submit Story" }
})
end
@header_links.merge!({ "/search" => { :title => "Search" } })
@header_links.each do |k,v|
v[:class] ||= []
if k == @cur_url
v[:class].push "cur_url"
end
end
@header_links
end
def right_header_links
return @right_header_links if @right_header_links
@right_header_links = {
"/filters" => { :title => "Filters" },
}
if @user
if (count = @user.unread_message_count) > 0
@right_header_links.merge!({ "/messages" => {
:class => [ "new_messages" ],
:title => "#{count} New Message#{count == 1 ? "" : "s"}",
} })
else
@right_header_links.merge!({
"/messages" => { :title => "Messages" }
})
end
@right_header_links.merge!({
"/settings" => { :title => "#{@user.username} (#{@user.karma})" }
})
else
@right_header_links.merge!({
"/login" => { :title => "Login" }
})
end
@right_header_links.each do |k,v|
v[:class] ||= []
if k == @cur_url
v[:class].push "cur_url"
end
end
@right_header_links
end
def page_numbers_for_pagination(max, cur)
if max <= MAX_PAGES
return (1 .. max).to_a

View File

@ -48,20 +48,7 @@
href="/" title="<%= Rails.application.name %> (Current traffic: <%=
@traffic.to_i %>)"></a>
<% links = {
"/" => @cur_url == "/" ? Rails.application.name : "Home",
"/recent" => "Recent",
"/comments" => "Comments",
} %>
<% if @user && @user.can_submit_stories? %>
<% links.merge!({ "/threads" => "Your Threads",
"/stories/new" => "Submit Story" }) %>
<% end %>
<% links.merge!({ "/search" => "Search" }) %>
<% if @cur_url.present? && !links.keys.include?(@cur_url) &&
<% if @cur_url.present? && !header_links.keys.include?(@cur_url) &&
@heading.present? %>
<span id="headertitle">
<a href="<%= @cur_url %>"><%= @heading %></a>
@ -69,33 +56,19 @@
<% end %>
<span class="headerlinks">
<% links.each do |u,v| %>
<a href="<%= u %>" <%= u == @cur_url ? raw("class=\"cur_url\"") :
"" %>><%= v %></a>
<% header_links.each do |u,v| %>
<a href="<%= u %>" class="<%= v[:class].join(" ") %>"><%=
v[:title] %></a>
<% end %>
</span>
</div>
<div id="headerright">
<span class="headerlinks">
<a href="/filters" <%= @cur_url == "/filters" ?
raw("class=\"cur_url\"") : "" %>>Filters</a>
<% if @user %>
<% if (count = @user.unread_message_count) > 0 %>
<a href="/messages" class="new_messages <%= @cur_url == "/messages" ?
"cur_url" : "" %>"><%= count %> New Message<%= count == 1 ? "" :
"s" %></a>
<% else %>
<a href="/messages" <%= @cur_url == "/messages" ?
raw("class=\"cur_url\"") : "" %>>Messages</a>
<% right_header_links.each do |u,v| %>
<a href="<%= u %>" class="<%= v[:class].join(" ") %>"><%=
v[:title] %></a>
<% end %>
<a href="/settings" <%= @cur_url == "/settings" ?
raw("class=\"cur_url\"") : "" %>><%= @user.username %>
(<%= @user.karma %>)</a>
<% else %>
<a href="/login">Login</a>
<% end %>
</span>
</div>