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.
This commit is contained in:
@@ -109,11 +109,11 @@ class DText
|
|||||||
end
|
end
|
||||||
|
|
||||||
if obj.is_approved?
|
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?
|
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?
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
11
app/logical/routes.rb
Normal file
11
app/logical/routes.rb
Normal file
@@ -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
|
||||||
@@ -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|
|
text = text.gsub(%r{https?://www\.pixiv\.net/member_illust\.php\?mode=medium&illust_id=([0-9]+)}i) do |_match|
|
||||||
pixiv_id = $1
|
pixiv_id = $1
|
||||||
%(pixiv ##{pixiv_id} "»":[/posts?tags=pixiv:#{pixiv_id}])
|
%(pixiv ##{pixiv_id} "»":[#{Routes.posts_path(tags: "pixiv:#{pixiv_id}")}])
|
||||||
end
|
end
|
||||||
|
|
||||||
text = text.gsub(%r{https?://www\.pixiv\.net/member\.php\?id=([0-9]+)}i) do |_match|
|
text = text.gsub(%r{https?://www\.pixiv\.net/member\.php\?id=([0-9]+)}i) do |_match|
|
||||||
member_id = $1
|
member_id = $1
|
||||||
profile_url = "https://www.pixiv.net/users/#{member_id}"
|
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
|
end
|
||||||
|
|
||||||
text = text.gsub(/\r\n|\r|\n/, "<br>")
|
text = text.gsub(/\r\n|\r|\n/, "<br>")
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ class UploadService
|
|||||||
end
|
end
|
||||||
|
|
||||||
def comment_replacement_message(post, replacement)
|
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
|
end
|
||||||
|
|
||||||
def replacement_message(post, replacement)
|
def replacement_message(post, replacement)
|
||||||
|
|||||||
@@ -32,16 +32,16 @@ class UserPromotion
|
|||||||
|
|
||||||
def create_mod_actions
|
def create_mod_actions
|
||||||
if old_can_approve_posts != user.can_approve_posts?
|
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
|
end
|
||||||
|
|
||||||
if old_can_upload_free != user.can_upload_free?
|
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
|
end
|
||||||
|
|
||||||
if user.level_changed?
|
if user.level_changed?
|
||||||
category = is_upgrade ? :user_account_upgrade : :user_level_change
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -89,7 +89,7 @@ class BulkUpdateRequest < ApplicationRecord
|
|||||||
end
|
end
|
||||||
|
|
||||||
def bulk_update_request_link
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ class Comment < ApplicationRecord
|
|||||||
mentionable(
|
mentionable(
|
||||||
:message_field => :body,
|
:message_field => :body,
|
||||||
:title => ->(user_name) {"#{creator.name} mentioned you in a comment on post ##{post_id}"},
|
: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
|
module SearchMethods
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ class ForumPost < ApplicationRecord
|
|||||||
mentionable(
|
mentionable(
|
||||||
:message_field => :body,
|
:message_field => :body,
|
||||||
:title => ->(user_name) {%{#{creator.name} mentioned you in topic ##{topic_id} (#{topic.title})}},
|
: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
|
module SearchMethods
|
||||||
|
|||||||
@@ -1390,7 +1390,8 @@ class Post < ApplicationRecord
|
|||||||
|
|
||||||
new_artist_tags.each do |tag|
|
new_artist_tags.each do |tag|
|
||||||
if tag.artist.blank?
|
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
|
end
|
||||||
end
|
end
|
||||||
@@ -1412,7 +1413,8 @@ class Post < ApplicationRecord
|
|||||||
return if tags.any?(&:artist?)
|
return if tags.any?(&:artist?)
|
||||||
return if Sources::Strategies.find(source).is_a?(Sources::Strategies::Null)
|
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
|
end
|
||||||
|
|
||||||
def has_copyright_tag
|
def has_copyright_tag
|
||||||
|
|||||||
@@ -8,10 +8,10 @@ class UserFeedback < ApplicationRecord
|
|||||||
validates_inclusion_of :category, :in => %w(positive negative neutral)
|
validates_inclusion_of :category, :in => %w(positive negative neutral)
|
||||||
after_create :create_dmail, unless: :disable_dmail_notification
|
after_create :create_dmail, unless: :disable_dmail_notification
|
||||||
after_update(:if => ->(rec) { CurrentUser.id != rec.creator_id}) do |rec|
|
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
|
end
|
||||||
after_destroy(:if => ->(rec) { CurrentUser.id != rec.creator_id}) do |rec|
|
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
|
end
|
||||||
|
|
||||||
deletable
|
deletable
|
||||||
@@ -52,7 +52,7 @@ class UserFeedback < ApplicationRecord
|
|||||||
end
|
end
|
||||||
|
|
||||||
def create_dmail
|
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)
|
Dmail.create_automated(:to_id => user_id, :title => "Your user record has been updated", :body => body)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -122,7 +122,7 @@ class WikiPage < ApplicationRecord
|
|||||||
|
|
||||||
broken_wikis = WikiPage.linked_to(title_was)
|
broken_wikis = WikiPage.linked_to(title_was)
|
||||||
if broken_wikis.count > 0
|
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!)
|
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
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -195,7 +195,7 @@ module Sources
|
|||||||
should "convert illust links and member links to dtext" do
|
should "convert illust links and member links to dtext" do
|
||||||
get_source("https://www.pixiv.net/member_illust.php?mode=medium&illust_id=63421642")
|
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)
|
assert_equal(dtext_desc, @site.dtext_artist_commentary_desc)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ class UserFeedbackTest < ActiveSupport::TestCase
|
|||||||
gold = FactoryBot.create(:gold_user)
|
gold = FactoryBot.create(:gold_user)
|
||||||
member = FactoryBot.create(:user)
|
member = FactoryBot.create(:user)
|
||||||
dmail = <<~EOS.chomp
|
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!
|
good job!
|
||||||
EOS
|
EOS
|
||||||
|
|||||||
Reference in New Issue
Block a user