From 9f5e04bbbaeaca90540dd1f2ea360be5e656d9fe Mon Sep 17 00:00:00 2001 From: joshua stein Date: Fri, 2 Jan 2015 17:02:55 -0600 Subject: [PATCH] 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. --- app/helpers/application_helper.rb | 4 --- app/models/comment.rb | 8 ++++++ app/models/message.rb | 2 +- app/models/story.rb | 14 +++++++++- app/views/comments/_comment.html.erb | 5 ++-- app/views/comments/index.rss | 2 +- app/views/home/rss.erb | 2 +- .../invitation_mailer/invitation.text.erb | 4 +-- .../invitation_request.text.erb | 2 +- app/views/invitations/build.html.erb | 2 +- app/views/invitations/index.html.erb | 4 +-- app/views/login/forgot_password.html.erb | 2 +- app/views/login/index.html.erb | 4 +-- app/views/login/set_new_password.html.erb | 2 +- app/views/messages/index.html.erb | 2 +- app/views/messages/show.html.erb | 4 +-- app/views/moderations/index.html.erb | 2 +- .../password_reset_link.text.erb | 2 +- app/views/settings/index.html.erb | 4 +-- app/views/signup/invited.html.erb | 2 +- app/views/stories/_form.html.erb | 6 ++--- app/views/stories/_listdetail.html.erb | 26 +++++++++---------- app/views/stories/edit.html.erb | 4 +-- app/views/users/show.html.erb | 2 +- config/application.rb | 22 +++++++++++----- 25 files changed, 79 insertions(+), 54 deletions(-) diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 00ce8b8..2a451b9 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -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 diff --git a/app/models/comment.rb b/app/models/comment.rb index d59a4a4..27ce824 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -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 diff --git a/app/models/message.rb b/app/models/message.rb index 6a783c1..0e78b9c 100644 --- a/app/models/message.rb +++ b/app/models/message.rb @@ -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 diff --git a/app/models/story.rb b/app/models/story.rb index 6836115..3754004 100644 --- a/app/models/story.rb +++ b/app/models/story.rb @@ -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 diff --git a/app/views/comments/_comment.html.erb b/app/views/comments/_comment.html.erb index c9cf25f..5bf877f 100644 --- a/app/views/comments/_comment.html.erb +++ b/app/views/comments/_comment.html.erb @@ -12,7 +12,7 @@ class="comment <%= comment.current_vote ? (comment.current_vote[:vote] == 1 ? <% if @user %> <% else %> - <%= link_to "", login_url, :class => "upvoter" %> + <%= link_to "", login_path, :class => "upvoter" %> <% end %>
<%= comment.score %>
<% 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: - <%= comment.story.title %> + <%= comment.story.title + %> <% end %>
diff --git a/app/views/comments/index.rss b/app/views/comments/index.rss index d124d1f..3bae518 100644 --- a/app/views/comments/index.rss +++ b/app/views/comments/index.rss @@ -5,7 +5,7 @@ <%= Rails.application.name %><%= @title.present? ? ": " + h(@title) : "" %> <%= @title %> - <%= root_url %>comments + <%= Rails.application.root_url %>comments <% @comments.each do |comment| %> diff --git a/app/views/home/rss.erb b/app/views/home/rss.erb index 7d65b52..8fc3226 100644 --- a/app/views/home/rss.erb +++ b/app/views/home/rss.erb @@ -5,7 +5,7 @@ <%= Rails.application.name %><%= @title.present? ? ": " + h(@title) : "" %> <%= @title %> - <%= root_url + (@newest ? "newest" : "") %> + <%= Rails.application.root_url + (@newest ? "newest" : "") %> <% @stories.each do |story| %> diff --git a/app/views/invitation_mailer/invitation.text.erb b/app/views/invitation_mailer/invitation.text.erb index 318c7e2..ca68232 100644 --- a/app/views/invitation_mailer/invitation.text.erb +++ b/app/views/invitation_mailer/invitation.text.erb @@ -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 %> diff --git a/app/views/invitation_request_mailer/invitation_request.text.erb b/app/views/invitation_request_mailer/invitation_request.text.erb index 423d5c4..f7d4c53 100644 --- a/app/views/invitation_request_mailer/invitation_request.text.erb +++ b/app/views/invitation_request_mailer/invitation_request.text.erb @@ -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. diff --git a/app/views/invitations/build.html.erb b/app/views/invitations/build.html.erb index 1945ebf..8e07a7b 100644 --- a/app/views/invitations/build.html.erb +++ b/app/views/invitations/build.html.erb @@ -17,7 +17,7 @@

<%= form_for @invitation_request, - :url => create_invitation_by_request_url do |f| %> + :url => create_invitation_by_request_path do |f| %>

<%= f.label :name, "Name:" %> <%= f.text_field :name, :size => 30 %> diff --git a/app/views/invitations/index.html.erb b/app/views/invitations/index.html.erb index 35c13ca..9d43eed 100644 --- a/app/views/invitations/index.html.erb +++ b/app/views/invitations/index.html.erb @@ -29,13 +29,13 @@ <%= ir.email %> <% end %> <%= raw ir.markeddown_memo %> - <%= form_tag send_invitation_for_request_url do %> + <%= 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 %> <% if @user.is_moderator? %> - <%= form_tag delete_invitation_request_url do %> + <%= 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?" } %> diff --git a/app/views/login/forgot_password.html.erb b/app/views/login/forgot_password.html.erb index fa3f4a2..0f7653e 100644 --- a/app/views/login/forgot_password.html.erb +++ b/app/views/login/forgot_password.html.erb @@ -8,7 +8,7 @@ below and instructions will be e-mailed to you.

- <%= 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 %>
diff --git a/app/views/login/index.html.erb b/app/views/login/index.html.erb index 9c22a5f..fbab6cf 100644 --- a/app/views/login/index.html.erb +++ b/app/views/login/index.html.erb @@ -3,7 +3,7 @@ Login
- <%= form_tag login_url do %> + <%= form_tag login_path do %>

<%= label_tag :email, "E-mail or Username:" %> <%= text_field_tag :email, "", :size => 30, :autofocus => "autofocus" %> @@ -20,7 +20,7 @@

Forgot your password? <%= link_to "Reset your password", - forgot_password_url %>. + forgot_password_path %>.

diff --git a/app/views/login/set_new_password.html.erb b/app/views/login/set_new_password.html.erb index 291d5aa..dd9925f 100644 --- a/app/views/login/set_new_password.html.erb +++ b/app/views/login/set_new_password.html.erb @@ -3,7 +3,7 @@ Set New Password - <%= 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] %> diff --git a/app/views/messages/index.html.erb b/app/views/messages/index.html.erb index d36a5b1..cd0a8d3 100644 --- a/app/views/messages/index.html.erb +++ b/app/views/messages/index.html.erb @@ -23,7 +23,7 @@ <% if @messages.any? %> - <%= form_tag batch_delete_messages_url do %> + <%= form_tag batch_delete_messages_path do %>
<%= check_box_tag "delete_all", diff --git a/app/views/messages/show.html.erb b/app/views/messages/show.html.erb index 8d67bea..cc8cf40 100644 --- a/app/views/messages/show.html.erb +++ b/app/views/messages/show.html.erb @@ -33,13 +33,13 @@
- <%= 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 %>
- <%= 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 %> diff --git a/app/views/moderations/index.html.erb b/app/views/moderations/index.html.erb index 97e371d..ed161ba 100644 --- a/app/views/moderations/index.html.erb +++ b/app/views/moderations/index.html.erb @@ -16,7 +16,7 @@
<%= mod.moderator.try(:username) %> <% if mod.story %> - Story: <%= mod.story.title + Story: <%= mod.story.title %> <% elsif mod.comment %> Comment on <%= diff --git a/app/views/password_reset/password_reset_link.text.erb b/app/views/password_reset/password_reset_link.text.erb index e3ea49b..f95bb09 100644 --- a/app/views/password_reset/password_reset_link.text.erb +++ b/app/views/password_reset/password_reset_link.text.erb @@ -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 %> diff --git a/app/views/settings/index.html.erb b/app/views/settings/index.html.erb index e3b12fa..f496db1 100644 --- a/app/views/settings/index.html.erb +++ b/app/views/settings/index.html.erb @@ -6,7 +6,7 @@ Account Settings - <%= 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 @@

- <%= 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| %>
Delete Account diff --git a/app/views/signup/invited.html.erb b/app/views/signup/invited.html.erb index d248014..74fe4dc 100644 --- a/app/views/signup/invited.html.erb +++ b/app/views/signup/invited.html.erb @@ -3,7 +3,7 @@ Create an Account
- <%= 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 %> diff --git a/app/views/stories/_form.html.erb b/app/views/stories/_form.html.erb index 45f9b62..7870f48 100644 --- a/app/views/stories/_form.html.erb +++ b/app/views/stories/_form.html.erb @@ -3,7 +3,7 @@

Error: This story was already submitted <%= time_ago_in_words(f.object.already_posted_story.created_at) %> ago

- Please view the previous discussion for this story.

@@ -15,7 +15,7 @@ time_ago_in_words(f.object.already_posted_story.created_at) %> ago, but may be submitted again.

- Please view the previous discussion for this story first. If the content has changed or warrants new discussion, you may submit it again.

@@ -71,7 +71,7 @@
">
- Submit to <%= Rails.application.name %> diff --git a/app/views/stories/_listdetail.html.erb b/app/views/stories/_listdetail.html.erb index 0d5218e..54bed76 100644 --- a/app/views/stories/_listdetail.html.erb +++ b/app/views/stories/_listdetail.html.erb @@ -7,14 +7,14 @@ class="story <%= story.vote == 1 ? "upvoted" : "" %> <%= story.vote == -1 ? <% if @user %> <% else %> - <%= link_to "", login_url, :class => "upvoter" %> + <%= link_to "", login_path, :class => "upvoter" %> <% end %>
<%= story.score %>
<% if story.can_be_seen_by_user?(@user) %> - <%= story.title %> + <%= story.title %> <% 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 ? <% story.taggings.sort_by{|t| t.tag.tag }.sort_by{|t| t.tag.is_media?? -1 : 0 }.each do |tagging| %> - <%= tagging.tag.tag %> <% end %> @@ -37,12 +37,12 @@ class="story <%= story.vote == 1 ? "upvoted" : "" %> <%= story.vote == -1 ?
- <%= ms.title %> + <%= ms.title %> <% ms.taggings.sort_by{|t| t.tag.tag }.sort_by{|t| t.tag.tag == "pdf" ? -1 : 0 }.each do |tagging| %> - <%= tagging.tag.tag %> <% end %> @@ -75,21 +75,21 @@ class="story <%= story.vote == 1 ? "upvoted" : "" %> <%= story.vote == -1 ? <% if story.is_editable_by_user?(@user) %> | - edit + edit <% 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 ? | flag <% 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) %> | - <%= story.comments_count == 0 ? + <%= story.comments_count == 0 ? "discuss" : "#{story.comments_count} comment" << (story.comments_count == 1 ? "" : "s") %> @@ -130,6 +130,6 @@ class="story <%= story.vote == 1 ? "upvoted" : "" %> <%= story.vote == -1 ?
diff --git a/app/views/stories/edit.html.erb b/app/views/stories/edit.html.erb index a84ec0a..4221259 100644 --- a/app/views/stories/edit.html.erb +++ b/app/views/stories/edit.html.erb @@ -3,7 +3,7 @@ Edit Story
- <%= 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 @@ <%= submit_tag "Save" %> -  or cancel +  or cancel editing
diff --git a/app/views/users/show.html.erb b/app/views/users/show.html.erb index 17b43a8..0bd0e96 100644 --- a/app/views/users/show.html.erb +++ b/app/views/users/show.html.erb @@ -92,7 +92,7 @@ <%= @showing_user.stories_submitted_count %><%= tag ? ", " : "" %> <% if tag %> - most commonly tagged <%= tag.tag %> <% end %> diff --git a/config/application.rb b/config/application.rb index 606146b..c7c0022 100644 --- a/config/application.rb +++ b/config/application.rb @@ -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"