Add option to omit URL to `prepare_tweet` helper

This commit is contained in:
Andreas Nedbal 2023-10-18 20:01:07 +02:00
parent bdedd92817
commit dc8c5fbf82
2 changed files with 14 additions and 3 deletions

View File

@ -3,7 +3,7 @@ require 'cgi'
module SocialHelper::TwitterMethods
include MarkdownHelper
def prepare_tweet(answer, post_tag = nil)
def prepare_tweet(answer, post_tag = nil, omit_url = false)
question_content = twitter_markdown answer.question.content.gsub(/\@(\w+)/, '\1')
original_question_length = question_content.length
answer_content = twitter_markdown answer.content
@ -13,7 +13,7 @@ module SocialHelper::TwitterMethods
username: answer.user.screen_name,
host: APP_CONFIG['hostname'],
protocol: (APP_CONFIG['https'] ? :https : :http)
)
) unless omit_url
parsed_tweet = { :valid => false }
tweet_text = ""

View File

@ -38,6 +38,17 @@ describe SocialHelper::TwitterMethods, :type => :helper do
end
end
context "when the url should be omitted" do
let(:question_content) { "question" }
let(:answer_content) { "answer" }
subject { prepare_tweet(answer, nil, true) }
it "should include the suffix after the link" do
expect(subject).to eq("question — answer")
end
end
context 'when a suffix has been passed and the tweet needs to be shortened' do
subject { prepare_tweet(answer, '#askracc') }
@ -69,4 +80,4 @@ describe SocialHelper::TwitterMethods, :type => :helper do
expect(subject).to eq("https://twitter.com/intent/tweet?text=#{CGI.escape(prepare_tweet(answer))}")
end
end
end
end