use _path for most things instead of _url

Instead of hard-coding the scheme and host everywhere, use _path
methods to show relative URLs.

Except that our previous setting of
Rails.application.routes.default_url_options in
config.after_initialize made this moot because Rails inserts that
host into all _path helpers for some reason.  So revert that
setting.

But then anything that wants an absolute URL doesn't know the
hostname and the root_url helper throws an exception.  So make a
Rails.application.root_url shortcut to pass the per-app settings in
Rails.application to root_url.

Now we can just use _path helpers most places but still use _url
ones where we need them, such as in RSS views and e-mail templates.
This commit is contained in:
joshua stein 2015-01-02 17:02:55 -06:00
parent ad272a8f11
commit 9f5e04bbba
25 changed files with 79 additions and 54 deletions

View file

@ -16,8 +16,4 @@ module ApplicationHelper
label_tag(nil, time_ago_in_words(*args),
:title => args.first.strftime("%F %T %z"))
end
def main_root_url
Rails.application.routes.url_helpers.root_url
end
end

View file

@ -325,6 +325,10 @@ class Comment < ActiveRecord::Base
"comment.#{short_id}.#{created_at.to_i}@#{Rails.application.domain}"
end
def path
self.story.comments_path + "/comments/#{self.short_id}#c_#{self.short_id}"
end
def plaintext_comment
# TODO: linkify then strip tags and convert entities back
comment
@ -341,6 +345,10 @@ class Comment < ActiveRecord::Base
self.upvotes - self.downvotes
end
def short_id_path
self.story.short_id_path + "/_/comments/#{self.short_id}#c_#{self.short_id}"
end
def short_id_url
self.story.short_id_url + "/_/comments/#{self.short_id}#c_#{self.short_id}"
end

View file

@ -75,6 +75,6 @@ class Message < ActiveRecord::Base
end
def url
Rails.application.routes.url_helpers.root_url + "messages/#{self.short_id}"
Rails.application.root_url + "messages/#{self.short_id}"
end
end

View file

@ -190,6 +190,10 @@ class Story < ActiveRecord::Base
end
end
def comments_path
"#{short_id_path}/#{self.title_as_url}"
end
def comments_url
"#{short_id_url}/#{self.title_as_url}"
end
@ -370,8 +374,12 @@ class Story < ActiveRecord::Base
self.user_id, nil, false)
end
def short_id_path
Rails.application.routes.url_helpers.root_path + "s/#{self.short_id}"
end
def short_id_url
Rails.application.routes.url_helpers.root_url + "s/#{self.short_id}"
Rails.application.root_url + "s/#{self.short_id}"
end
def score
@ -472,6 +480,10 @@ class Story < ActiveRecord::Base
end
end
def url_or_comments_path
self.url.blank? ? self.comments_path : self.url
end
def url_or_comments_url
self.url.blank? ? self.comments_url : self.url
end

View file

@ -12,7 +12,7 @@ class="comment <%= comment.current_vote ? (comment.current_vote[:vote] == 1 ?
<% if @user %>
<a class="upvoter"></a>
<% else %>
<%= link_to "", login_url, :class => "upvoter" %>
<%= link_to "", login_path, :class => "upvoter" %>
<% end %>
<div class="score"><%= comment.score %></div>
<% if @user && @user.can_downvote?(comment) %>
@ -90,7 +90,8 @@ class="comment <%= comment.current_vote ? (comment.current_vote[:vote] == 1 ?
<% if defined?(show_story) && show_story %>
| on:
<a href="<%= comment.story.comments_url %>"><%= comment.story.title %></a>
<a href="<%= comment.story.comments_path %>"><%= comment.story.title
%></a>
<% end %>
</div>
<div class="comment_text">

View file

@ -5,7 +5,7 @@
<title><%= Rails.application.name %><%= @title.present? ?
": " + h(@title) : "" %></title>
<description><%= @title %></description>
<link><%= root_url %>comments</link>
<link><%= Rails.application.root_url %>comments</link>
<% @comments.each do |comment| %>
<item>

View file

@ -5,7 +5,7 @@
<title><%= Rails.application.name %><%= @title.present? ?
": " + h(@title) : "" %></title>
<description><%= @title %></description>
<link><%= root_url + (@newest ? "newest" : "") %></link>
<link><%= Rails.application.root_url + (@newest ? "newest" : "") %></link>
<% @stories.each do |story| %>
<item>

View file

@ -1,7 +1,7 @@
Hello <%= @invitation.email %>,
The user <%= @invitation.user.username %> has invited you to <%= Rails.application.name %> (<%= root_url %>)<%= @invitation.memo.present? ? raw("\n\n #{@invitation.memo}") : "" %>
The user <%= @invitation.user.username %> has invited you to <%= Rails.application.name %> (<%= Rails.application.root_url %>)<%= @invitation.memo.present? ? raw("\n\n #{@invitation.memo}") : "" %>
To accept this invitation and create an account, visit the URL below:
<%= root_url %>invitations/<%= @invitation.code %>
<%= Rails.application.root_url %>invitations/<%= @invitation.code %>

View file

@ -10,6 +10,6 @@ Someone at <%= @invitation_request.ip_address %> has submitted an invitation req
If this is you, visit the URL below to confirm your request and
display it to other logged-in users.
<%= root_url %>invitations/confirm/<%= @invitation_request.code %>
<%= Rails.application.root_url %>invitations/confirm/<%= @invitation_request.code %>
If this is not you, you can delete this message.

View file

@ -17,7 +17,7 @@
</p>
<%= form_for @invitation_request,
:url => create_invitation_by_request_url do |f| %>
:url => create_invitation_by_request_path do |f| %>
<p>
<%= f.label :name, "Name:" %>
<%= f.text_field :name, :size => 30 %>

View file

@ -29,13 +29,13 @@
<em><%= ir.email %></em>
<% end %></td>
<td><%= raw ir.markeddown_memo %></td>
<td><%= form_tag send_invitation_for_request_url do %>
<td><%= form_tag send_invitation_for_request_path do %>
<%= hidden_field_tag "code", ir.code %>
<%= submit_tag "Send Invitation", :data => { :confirm => "Are " <<
"you sure you want to invite this person and remove this request?" } %>
<% end %></td>
<% if @user.is_moderator? %>
<td><%= form_tag delete_invitation_request_url do %>
<td><%= form_tag delete_invitation_request_path do %>
<%= hidden_field_tag "code", ir.code %>
<%= submit_tag "Delete", :data => { :confirm => "Are you sure " <<
"you want to delete this request?" } %>

View file

@ -8,7 +8,7 @@
below and instructions will be e-mailed to you.
</p>
<%= form_tag reset_password_url do %>
<%= form_tag reset_password_path do %>
<%= label_tag :email, "E-mail or Username:" %>
<%= text_field_tag :email, "", :size => 30 %>
<br />

View file

@ -3,7 +3,7 @@
Login
</div>
<%= form_tag login_url do %>
<%= form_tag login_path do %>
<p>
<%= label_tag :email, "E-mail or Username:" %>
<%= text_field_tag :email, "", :size => 30, :autofocus => "autofocus" %>
@ -20,7 +20,7 @@
<p>
Forgot your password? <%= link_to "Reset your password",
forgot_password_url %>.
forgot_password_path %>.
</p>
<p>

View file

@ -3,7 +3,7 @@
Set New Password
</div>
<%= form_tag set_new_password_url, { :autocomplete => "off" } do %>
<%= form_tag set_new_password_path, { :autocomplete => "off" } do %>
<%= error_messages_for(@reset_user) %>
<%= hidden_field_tag "token", params[:token] %>

View file

@ -23,7 +23,7 @@
</div>
<% if @messages.any? %>
<%= form_tag batch_delete_messages_url do %>
<%= form_tag batch_delete_messages_path do %>
<table class="data zebra" width="100%" cellspacing=0>
<tr>
<th width="3%"><%= check_box_tag "delete_all",

View file

@ -33,13 +33,13 @@
<div class="boxline">
<div style="float: left;">
<%= form_tag message_url(@message.short_id), :method => :delete do %>
<%= form_tag message_path(@message.short_id), :method => :delete do %>
<%= submit_tag "Delete Message" %>
<% end %>
</div>
<div style="float: left; padding-left: 1em;">
<%= form_tag message_url(@message.short_id) + "/keep_as_new",
<%= form_tag message_path(@message.short_id) + "/keep_as_new",
:method => :post do %>
<%= submit_tag "Keep As New" %>
<% end %>

View file

@ -16,7 +16,7 @@
<td><a href="/messages?to=<%= mod.moderator.try(:username) %>"><%=
mod.moderator.try(:username) %></a></td>
<td><% if mod.story %>
<a href="<%= mod.story.comments_url %>">Story: <%= mod.story.title
<a href="<%= mod.story.comments_path %>">Story: <%= mod.story.title
%></a>
<% elsif mod.comment %>
<a href="<%= mod.comment.url %>">Comment on <%=

View file

@ -4,4 +4,4 @@ Someone at <%= @ip %> requested to reset your account password
on <%= Rails.application.name %>. If you submitted this request, visit the link below to
set a new password. If not, you can disregard this e-mail.
<%= root_url %>login/set_new_password?token=<%= @user.password_reset_token %>
<%= Rails.application.root_url %>login/set_new_password?token=<%= @user.password_reset_token %>

View file

@ -6,7 +6,7 @@
Account Settings
</div>
<%= form_for @edit_user, :url => settings_url, :method => :post,
<%= form_for @edit_user, :url => settings_path, :method => :post,
:html => { :id => "edit_user" } do |f| %>
<%= error_messages_for f.object %>
@ -179,7 +179,7 @@
<br>
<br>
<%= form_for @edit_user, :url => delete_account_url, :method => :post,
<%= form_for @edit_user, :url => delete_account_path, :method => :post,
:html => { :id => "delete_user" } do |f| %>
<div class="legend">
Delete Account

View file

@ -3,7 +3,7 @@
Create an Account
</div>
<%= form_for @new_user, { :url => signup_url,
<%= form_for @new_user, { :url => signup_path,
:autocomplete => "off" } do |f| %>
<%= hidden_field_tag "invitation_code", @invitation.code %>

View file

@ -3,7 +3,7 @@
<h2>Error: This story was already submitted <%=
time_ago_in_words(f.object.already_posted_story.created_at) %> ago</h2>
<p>
Please view the <a href="<%= f.object.already_posted_story.comments_url %>"
Please view the <a href="<%= f.object.already_posted_story.comments_path %>"
target="_blank">previous discussion</a> for this story.
</p>
</div>
@ -15,7 +15,7 @@
time_ago_in_words(f.object.already_posted_story.created_at) %> ago, but may
be submitted again.</h2>
<p>
Please view the <a href="<%= f.object.already_posted_story.comments_url %>"
Please view the <a href="<%= f.object.already_posted_story.comments_path %>"
target="_blank">previous discussion</a> for this story first. If the content
has changed or warrants new discussion, you may submit it again.
</p>
@ -71,7 +71,7 @@
<div id="story_guidelines" style="<%= show_guidelines?? "" :
"display: none;" %>">
<div style="float: right;">
<a href="javascript:window.location=%22<%= root_url %>stories/new?url=%22+encodeURIComponent(document.location)+%22&title=%22+encodeURIComponent(document.title)"
<a href="javascript:window.location=%22<%= Rails.application.root_url %>stories/new?url=%22+encodeURIComponent(document.location)+%22&title=%22+encodeURIComponent(document.title)"
style="border: 1px solid #ddd; padding: 0.5em; background-color:
#f8f8f8; line-height: 1.5em; margin-left: 1em;">Submit to
<%= Rails.application.name %></a>

View file

@ -7,14 +7,14 @@ class="story <%= story.vote == 1 ? "upvoted" : "" %> <%= story.vote == -1 ?
<% if @user %>
<a class="upvoter"></a>
<% else %>
<%= link_to "", login_url, :class => "upvoter" %>
<%= link_to "", login_path, :class => "upvoter" %>
<% end %>
<div class="score"><%= story.score %></div>
</div>
<div class="details">
<span class="link">
<% if story.can_be_seen_by_user?(@user) %>
<a href="<%= story.url_or_comments_url %>"><%= story.title %></a>
<a href="<%= story.url_or_comments_path %>"><%= story.title %></a>
<% end %>
<% if story.is_gone? %>
[Story removed by <%= story.is_moderated? ? "moderator" :
@ -25,7 +25,7 @@ class="story <%= story.vote == 1 ? "upvoted" : "" %> <%= story.vote == -1 ?
<span class="tags">
<% story.taggings.sort_by{|t| t.tag.tag }.sort_by{|t|
t.tag.is_media?? -1 : 0 }.each do |tagging| %>
<a href="<%= tag_url(tagging.tag.tag) %>"
<a href="<%= tag_path(tagging.tag.tag) %>"
class="<%= tagging.tag.css_class %>"
title="<%= tagging.tag.description %>"><%= tagging.tag.tag %></a>
<% end %>
@ -37,12 +37,12 @@ class="story <%= story.vote == 1 ? "upvoted" : "" %> <%= story.vote == -1 ?
<br>
<span class="merge"></span>
<span class="link">
<a href="<%= ms.url_or_comments_url %>"><%= ms.title %></a>
<a href="<%= ms.url_or_comments_path %>"><%= ms.title %></a>
</span>
<span class="tags">
<% ms.taggings.sort_by{|t| t.tag.tag }.sort_by{|t|
t.tag.tag == "pdf" ? -1 : 0 }.each do |tagging| %>
<a href="<%= tag_url(tagging.tag.tag) %>"
<a href="<%= tag_path(tagging.tag.tag) %>"
class="<%= tagging.tag.css_class %>"
title="<%= tagging.tag.description %>"><%= tagging.tag.tag %></a>
<% end %>
@ -75,21 +75,21 @@ class="story <%= story.vote == 1 ? "upvoted" : "" %> <%= story.vote == -1 ?
<% if story.is_editable_by_user?(@user) %>
|
<a href="<%= edit_story_url(story.short_id) %>">edit</a>
<a href="<%= edit_story_path(story.short_id) %>">edit</a>
<% if story.is_gone? && story.is_undeletable_by_user?(@user) %>
|
<%= link_to "undelete", story_undelete_url(story.short_id),
<%= link_to "undelete", story_undelete_path(story.short_id),
:method => :post, :data => {
:confirm => "Are you sure you want to undelete this story?" } %>
<% elsif !story.is_gone? %>
|
<% if story.user_id != @user.try(:id) &&
@user.try(:is_moderator?) %>
<%= link_to "delete", story_url(story.short_id),
<%= link_to "delete", story_path(story.short_id),
:method => :delete, :class => "mod_story_link" %>
<% else %>
<%= link_to "delete", story_url(story.short_id),
<%= link_to "delete", story_path(story.short_id),
:method => :delete, :data => {
:confirm => "Are you sure you want to delete this story?" } %>
<% end %>
@ -100,10 +100,10 @@ class="story <%= story.vote == 1 ? "upvoted" : "" %> <%= story.vote == -1 ?
| <a class="flagger">flag</a>
<% end %>
<% if story.vote == 0 %>
| <%= link_to "unhide", story_unhide_url(story.short_id),
| <%= link_to "unhide", story_unhide_path(story.short_id),
:class => "hider" %>
<% else %>
| <%= link_to "hide", story_hide_url(story.short_id),
| <%= link_to "hide", story_hide_path(story.short_id),
:class => "hider" %>
<% end %>
<% if defined?(single_story) && single_story && story.hider_count > 0 %>
@ -113,7 +113,7 @@ class="story <%= story.vote == 1 ? "upvoted" : "" %> <%= story.vote == -1 ?
<% if !story.is_gone? && (@user || story.comments_count > 0) %>
<span class="comments_label">
|
<a href="<%= story.comments_url %>"><%= story.comments_count == 0 ?
<a href="<%= story.comments_path %>"><%= story.comments_count == 0 ?
"discuss" : "#{story.comments_count} comment" <<
(story.comments_count == 1 ? "" : "s") %></a>
</span>
@ -130,6 +130,6 @@ class="story <%= story.vote == 1 ? "upvoted" : "" %> <%= story.vote == -1 ?
</div>
<div class="mobile_comments <%= story.comments_count == 0 ? "zero" : "" %>"
style="display: none;">
<a href="<%= story.comments_url %>"><%= story.comments_count %></a>
<a href="<%= story.comments_path %>"><%= story.comments_count %></a>
</div>
</li>

View file

@ -3,7 +3,7 @@
Edit Story
</div>
<%= form_for @story, :url => story_url(@story.short_id),
<%= form_for @story, :url => story_path(@story.short_id),
:method => :put, :html => { :id => "edit_story" } do |f| %>
<%= render :partial => "stories/form", :locals => { :story => @story,
:f => f } %>
@ -36,7 +36,7 @@
</div>
<%= submit_tag "Save" %>
&nbsp;or <a href="<%= story_url(@story.short_id) %>">cancel
&nbsp;or <a href="<%= story_path(@story.short_id) %>">cancel
editing</a>
<div style="clear: both;"></div>

View file

@ -92,7 +92,7 @@
<a href="/newest/<%= @showing_user.username %>"><%=
@showing_user.stories_submitted_count %></a><%= tag ? ", " : "" %>
<% if tag %>
most commonly tagged <a href="<%= tag_url(tag.tag) %>"
most commonly tagged <a href="<%= tag_path(tag.tag) %>"
class="<%= tag.css_class %>" title="<%= tag.description %>"><%=
tag.tag %></a>
<% end %>

View file

@ -27,11 +27,6 @@ module Lobsters
config.action_controller.action_on_unpermitted_parameters = :raise
config.cache_store = :file_store, "#{config.root}/tmp/cache/"
config.after_initialize do
Rails.application.routes.default_url_options[:host] =
Rails.application.domain
end
end
end
@ -40,8 +35,8 @@ silence_warnings do
ActionDispatch::ParamsParser::DEFAULT_PARSERS = {}
end
# define site name and domain to be used globally, can be overridden in
# config/initializers/production.rb
# define site name and domain to be used globally, should be overridden in a
# local file such as config/initializers/production.rb
class << Rails.application
def allow_invitation_requests?
true
@ -55,10 +50,23 @@ class << Rails.application
"Example News"
end
def root_url
Rails.application.routes.url_helpers.root_url({
:host => Rails.application.domain,
:protocol => Rails.application.ssl? ? "https" : "http",
})
end
# used as mailing list prefix and countinual prefix, cannot have spaces
def shortname
name.downcase.gsub(/[^a-z]/, "")
end
# whether absolute URLs should include https (does not require that
# config.force_ssl be on)
def ssl?
true
end
end
require "#{Rails.root}/lib/monkey"