From dbb66ace9022872791b72c2ca6fa2d4a1ece425a Mon Sep 17 00:00:00 2001 From: evazion Date: Wed, 23 Dec 2020 13:19:03 -0600 Subject: [PATCH] routes: replace hardcoded routes in models with route helpers. Add a Routes module that gives models access to route helpers outside of views, and use it to replace various hardcoded routes. --- app/logical/d_text.rb | 6 +++--- app/logical/routes.rb | 11 +++++++++++ app/logical/sources/strategies/pixiv.rb | 6 +++--- app/logical/upload_service/replacer.rb | 2 +- app/logical/user_promotion.rb | 6 +++--- app/models/bulk_update_request.rb | 2 +- app/models/comment.rb | 2 +- app/models/forum_post.rb | 2 +- app/models/post.rb | 6 ++++-- app/models/user_feedback.rb | 6 +++--- app/models/wiki_page.rb | 2 +- test/unit/sources/pixiv_test.rb | 2 +- test/unit/user_feedback_test.rb | 2 +- 13 files changed, 34 insertions(+), 21 deletions(-) create mode 100644 app/logical/routes.rb diff --git a/app/logical/d_text.rb b/app/logical/d_text.rb index d0982a6b8..f665c4923 100644 --- a/app/logical/d_text.rb +++ b/app/logical/d_text.rb @@ -109,11 +109,11 @@ class DText end if obj.is_approved? - "The \"bulk update request ##{obj.id}\":/bulk_update_requests/#{obj.id} has been approved by <@#{obj.approver.name}>.\n\n#{embedded_script}" + "The \"bulk update request ##{obj.id}\":#{Routes.bulk_update_request_path(obj)} has been approved by <@#{obj.approver.name}>.\n\n#{embedded_script}" elsif obj.is_pending? - "The \"bulk update request ##{obj.id}\":/bulk_update_requests/#{obj.id} is pending approval.\n\n#{embedded_script}" + "The \"bulk update request ##{obj.id}\":#{Routes.bulk_update_request_path(obj)} is pending approval.\n\n#{embedded_script}" elsif obj.is_rejected? - "The \"bulk update request ##{obj.id}\":/bulk_update_requests/#{obj.id} has been rejected.\n\n#{embedded_script}" + "The \"bulk update request ##{obj.id}\":#{Routes.bulk_update_request_path(obj)} has been rejected.\n\n#{embedded_script}" end end end diff --git a/app/logical/routes.rb b/app/logical/routes.rb new file mode 100644 index 000000000..8af010188 --- /dev/null +++ b/app/logical/routes.rb @@ -0,0 +1,11 @@ +# Allow Rails URL helpers to be used outside of views. +# Example: Routes.posts_path(tags: "touhou") => /posts?tags=touhou + +class Routes + include Singleton + include Rails.application.routes.url_helpers + + class << self + delegate_missing_to :instance + end +end diff --git a/app/logical/sources/strategies/pixiv.rb b/app/logical/sources/strategies/pixiv.rb index 25700d0fd..00d5ff86b 100644 --- a/app/logical/sources/strategies/pixiv.rb +++ b/app/logical/sources/strategies/pixiv.rb @@ -65,15 +65,15 @@ module Sources text = text.gsub(%r{https?://www\.pixiv\.net/member_illust\.php\?mode=medium&illust_id=([0-9]+)}i) do |_match| pixiv_id = $1 - %(pixiv ##{pixiv_id} "»":[/posts?tags=pixiv:#{pixiv_id}]) + %(pixiv ##{pixiv_id} "»":[#{Routes.posts_path(tags: "pixiv:#{pixiv_id}")}]) end text = text.gsub(%r{https?://www\.pixiv\.net/member\.php\?id=([0-9]+)}i) do |_match| member_id = $1 profile_url = "https://www.pixiv.net/users/#{member_id}" - search_params = {"search[url_matches]" => profile_url}.to_param + artist_search_url = Routes.artists_path(search: { url_matches: profile_url }) - %("user/#{member_id}":[#{profile_url}] "»":[/artists?#{search_params}]) + %("user/#{member_id}":[#{profile_url}] "»":[#{artist_search_url}]) end text = text.gsub(/\r\n|\r|\n/, "
") diff --git a/app/logical/upload_service/replacer.rb b/app/logical/upload_service/replacer.rb index f64198c5a..2a8d8b62b 100644 --- a/app/logical/upload_service/replacer.rb +++ b/app/logical/upload_service/replacer.rb @@ -11,7 +11,7 @@ class UploadService end def comment_replacement_message(post, replacement) - %("#{replacement.creator.name}":[/users/#{replacement.creator.id}] replaced this post with a new image:\n\n#{replacement_message(post, replacement)}) + %("#{replacement.creator.name}":[#{Routes.user_path(replacement.creator)}] replaced this post with a new image:\n\n#{replacement_message(post, replacement)}) end def replacement_message(post, replacement) diff --git a/app/logical/user_promotion.rb b/app/logical/user_promotion.rb index e9653bb2d..6420db5e3 100644 --- a/app/logical/user_promotion.rb +++ b/app/logical/user_promotion.rb @@ -32,16 +32,16 @@ class UserPromotion def create_mod_actions if old_can_approve_posts != user.can_approve_posts? - ModAction.log("\"#{promoter.name}\":/users/#{promoter.id} changed approval privileges for \"#{user.name}\":/users/#{user.id} from #{old_can_approve_posts} to [b]#{user.can_approve_posts?}[/b]", :user_approval_privilege) + ModAction.log("\"#{promoter.name}\":#{Routes.user_path(promoter)} changed approval privileges for \"#{user.name}\":#{Routes.user_path(user)} from #{old_can_approve_posts} to [b]#{user.can_approve_posts?}[/b]", :user_approval_privilege, promoter) end if old_can_upload_free != user.can_upload_free? - ModAction.log("\"#{promoter.name}\":/users/#{promoter.id} changed unlimited upload privileges for \"#{user.name}\":/users/#{user.id} from #{old_can_upload_free} to [b]#{user.can_upload_free?}[/b]", :user_upload_privilege) + ModAction.log("\"#{promoter.name}\":#{Routes.user_path(promoter)} changed unlimited upload privileges for \"#{user.name}\":#{Routes.user_path(user)} from #{old_can_upload_free} to [b]#{user.can_upload_free?}[/b]", :user_upload_privilege, promoter) end if user.level_changed? category = is_upgrade ? :user_account_upgrade : :user_level_change - ModAction.log(%{"#{user.name}":/users/#{user.id} level changed #{user.level_string_was} -> #{user.level_string}}, category) + ModAction.log(%{"#{user.name}":#{Routes.user_path(user)} level changed #{user.level_string_was} -> #{user.level_string}}, category, promoter) end end diff --git a/app/models/bulk_update_request.rb b/app/models/bulk_update_request.rb index d7afa226d..b40bcabca 100644 --- a/app/models/bulk_update_request.rb +++ b/app/models/bulk_update_request.rb @@ -89,7 +89,7 @@ class BulkUpdateRequest < ApplicationRecord end def bulk_update_request_link - %{"bulk update request ##{id}":/bulk_update_requests?search%5Bid%5D=#{id}} + %{"bulk update request ##{id}":#{Routes.bulk_update_requests_path(search: { id: id })}} end end diff --git a/app/models/comment.rb b/app/models/comment.rb index 2207706ef..c56271f56 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -21,7 +21,7 @@ class Comment < ApplicationRecord mentionable( :message_field => :body, :title => ->(user_name) {"#{creator.name} mentioned you in a comment on post ##{post_id}"}, - :body => ->(user_name) {"@#{creator.name} mentioned you in a \"comment\":/posts/#{post_id}#comment-#{id} on post ##{post_id}:\n\n[quote]\n#{DText.extract_mention(body, "@" + user_name)}\n[/quote]\n"} + :body => ->(user_name) {"@#{creator.name} mentioned you in a \"comment\":#{Routes.post_path(post, anchor: "comment-#{id}")} on post ##{post_id}:\n\n[quote]\n#{DText.extract_mention(body, "@" + user_name)}\n[/quote]\n"} ) module SearchMethods diff --git a/app/models/forum_post.rb b/app/models/forum_post.rb index c8aad7962..8cab6029b 100644 --- a/app/models/forum_post.rb +++ b/app/models/forum_post.rb @@ -28,7 +28,7 @@ class ForumPost < ApplicationRecord mentionable( :message_field => :body, :title => ->(user_name) {%{#{creator.name} mentioned you in topic ##{topic_id} (#{topic.title})}}, - :body => ->(user_name) {%{@#{creator.name} mentioned you in topic ##{topic_id} ("#{topic.title}":[/forum_topics/#{topic_id}?page=#{forum_topic_page}]):\n\n[quote]\n#{DText.extract_mention(body, "@" + user_name)}\n[/quote]\n}} + :body => ->(user_name) {%{@#{creator.name} mentioned you in topic ##{topic_id} ("#{topic.title}":[#{Routes.forum_topic_path(topic, page: forum_topic_page)}]):\n\n[quote]\n#{DText.extract_mention(body, "@" + user_name)}\n[/quote]\n}} ) module SearchMethods diff --git a/app/models/post.rb b/app/models/post.rb index 58e7079d3..e89d79e34 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -1390,7 +1390,8 @@ class Post < ApplicationRecord new_artist_tags.each do |tag| if tag.artist.blank? - warnings.add(:base, "Artist [[#{tag.name}]] requires an artist entry. \"Create new artist entry\":[/artists/new?artist%5Bname%5D=#{CGI.escape(tag.name)}]") + new_artist_path = Routes.new_artist_path(artist: { name: tag.name }) + warnings.add(:base, "Artist [[#{tag.name}]] requires an artist entry. \"Create new artist entry\":[#{new_artist_path}]") end end end @@ -1412,7 +1413,8 @@ class Post < ApplicationRecord return if tags.any?(&:artist?) return if Sources::Strategies.find(source).is_a?(Sources::Strategies::Null) - warnings.add(:base, "Artist tag is required. \"Create new artist tag\":[/artists/new?artist%5Bsource%5D=#{CGI.escape(source)}]. Ask on the forum if you need naming help") + new_artist_path = Routes.new_artist_path(artist: { source: source }) + warnings.add(:base, "Artist tag is required. \"Create new artist tag\":[#{new_artist_path}]. Ask on the forum if you need naming help") end def has_copyright_tag diff --git a/app/models/user_feedback.rb b/app/models/user_feedback.rb index cda52916c..75a0f7dc3 100644 --- a/app/models/user_feedback.rb +++ b/app/models/user_feedback.rb @@ -8,10 +8,10 @@ class UserFeedback < ApplicationRecord validates_inclusion_of :category, :in => %w(positive negative neutral) after_create :create_dmail, unless: :disable_dmail_notification after_update(:if => ->(rec) { CurrentUser.id != rec.creator_id}) do |rec| - ModAction.log(%{#{CurrentUser.name} updated user feedback for "#{rec.user.name}":/users/#{rec.user_id}}, :user_feedback_update) + ModAction.log(%{#{CurrentUser.name} updated user feedback for "#{rec.user.name}":#{Routes.user_path(rec.user)}}, :user_feedback_update) end after_destroy(:if => ->(rec) { CurrentUser.id != rec.creator_id}) do |rec| - ModAction.log(%{#{CurrentUser.name} deleted user feedback for "#{rec.user.name}":/users/#{rec.user_id}}, :user_feedback_delete) + ModAction.log(%{#{CurrentUser.name} deleted user feedback for "#{rec.user.name}":#{Routes.user_path(rec.user)}}, :user_feedback_delete) end deletable @@ -52,7 +52,7 @@ class UserFeedback < ApplicationRecord end def create_dmail - body = %{#{disclaimer}@#{creator.name} created a "#{category} record":/user_feedbacks?search[user_id]=#{user_id} for your account:\n\n#{self.body}} + body = %{#{disclaimer}@#{creator.name} created a "#{category} record":#{Routes.user_feedbacks_path(search: { user_id: user_id })} for your account:\n\n#{self.body}} Dmail.create_automated(:to_id => user_id, :title => "Your user record has been updated", :body => body) end diff --git a/app/models/wiki_page.rb b/app/models/wiki_page.rb index 236ba0410..a38f782d0 100644 --- a/app/models/wiki_page.rb +++ b/app/models/wiki_page.rb @@ -122,7 +122,7 @@ class WikiPage < ApplicationRecord broken_wikis = WikiPage.linked_to(title_was) if broken_wikis.count > 0 - broken_wiki_search = Rails.application.routes.url_helpers.wiki_pages_path(search: { linked_to: title_was }) + broken_wiki_search = Routes.wiki_pages_path(search: { linked_to: title_was }) warnings.add(:base, %!Warning: [[#{title_was}]] is still linked from "#{broken_wikis.count} #{"other wiki page".pluralize(broken_wikis.count)}":[#{broken_wiki_search}]. Update #{(broken_wikis.count > 1) ? "these wikis" : "this wiki"} to link to [[#{title}]] instead!) end end diff --git a/test/unit/sources/pixiv_test.rb b/test/unit/sources/pixiv_test.rb index a68d75ad0..a69106c3d 100644 --- a/test/unit/sources/pixiv_test.rb +++ b/test/unit/sources/pixiv_test.rb @@ -195,7 +195,7 @@ module Sources should "convert illust links and member links to dtext" do get_source("https://www.pixiv.net/member_illust.php?mode=medium&illust_id=63421642") - dtext_desc = %(foo 【pixiv #46337015 "»":[/posts?tags=pixiv:46337015]】bar 【pixiv #14901720 "»":[/posts?tags=pixiv:14901720]】\n\nbaz【"user/83739":[https://www.pixiv.net/users/83739] "»":[/artists?search%5Burl_matches%5D=https%3A%2F%2Fwww.pixiv.net%2Fusers%2F83739]】) + dtext_desc = %(foo 【pixiv #46337015 "»":[/posts?tags=pixiv%3A46337015]】bar 【pixiv #14901720 "»":[/posts?tags=pixiv%3A14901720]】\n\nbaz【"user/83739":[https://www.pixiv.net/users/83739] "»":[/artists?search%5Burl_matches%5D=https%3A%2F%2Fwww.pixiv.net%2Fusers%2F83739]】) assert_equal(dtext_desc, @site.dtext_artist_commentary_desc) end end diff --git a/test/unit/user_feedback_test.rb b/test/unit/user_feedback_test.rb index 516a79fd2..5f20faa66 100644 --- a/test/unit/user_feedback_test.rb +++ b/test/unit/user_feedback_test.rb @@ -7,7 +7,7 @@ class UserFeedbackTest < ActiveSupport::TestCase gold = FactoryBot.create(:gold_user) member = FactoryBot.create(:user) dmail = <<~EOS.chomp - @#{gold.name} created a "positive record":/user_feedbacks?search[user_id]=#{user.id} for your account: + @#{gold.name} created a "positive record":/user_feedbacks?search%5Buser_id%5D=#{user.id} for your account: good job! EOS