From dc8c5fbf824b73e5bf149579ef0e7c2fb7442abc Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Wed, 18 Oct 2023 20:01:07 +0200 Subject: [PATCH] Add option to omit URL to `prepare_tweet` helper --- app/helpers/social_helper/twitter_methods.rb | 4 ++-- spec/helpers/social_helper/twitter_methods_spec.rb | 13 ++++++++++++- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/app/helpers/social_helper/twitter_methods.rb b/app/helpers/social_helper/twitter_methods.rb index 7f025f71..716060f8 100644 --- a/app/helpers/social_helper/twitter_methods.rb +++ b/app/helpers/social_helper/twitter_methods.rb @@ -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 = "" diff --git a/spec/helpers/social_helper/twitter_methods_spec.rb b/spec/helpers/social_helper/twitter_methods_spec.rb index 7074f6e6..ad630bc4 100644 --- a/spec/helpers/social_helper/twitter_methods_spec.rb +++ b/spec/helpers/social_helper/twitter_methods_spec.rb @@ -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 \ No newline at end of file +end