Skip to content

Commit

Permalink
Merge pull request #272 from Retrospring/feature/social-post-tagging
Browse files Browse the repository at this point in the history
Add the ability to post to services with a tag
  • Loading branch information
raccube authored Jan 7, 2022
2 parents f80d189 + ac92a8b commit 136e2fa
Show file tree
Hide file tree
Showing 10 changed files with 101 additions and 6 deletions.
11 changes: 11 additions & 0 deletions app/controllers/services_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,17 @@ def create
end
end

def update
service = current_user.services.find(params[:id])
service.post_tag = params[:service][:post_tag].tr('@', '')
if service.save
flash[:success] = "Service updated successfully"
else
flash[:error] = "Failed to update service"
end
redirect_to services_path
end

def failure
Rails.logger.info "oauth error: #{params.inspect}"
flash[:error] = t('flash.service.failure')
Expand Down
14 changes: 11 additions & 3 deletions app/helpers/social_helper/twitter_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module SocialHelper::TwitterMethods
include MarkdownHelper

def prepare_tweet(answer)
def prepare_tweet(answer, post_tag = nil)
question_content = twitter_markdown answer.question.content.gsub(/\@(\w+)/, '\1')
original_question_length = question_content.length
answer_content = twitter_markdown answer.content
Expand All @@ -19,8 +19,16 @@ def prepare_tweet(answer)
tweet_text = ""

until parsed_tweet[:valid]
tweet_text = "#{question_content[0..122]}#{'…' if original_question_length > [123, question_content.length].min}" \
" — #{answer_content[0..123]}#{'…' if original_answer_length > [124, answer_content.length].min} #{answer_url}"
shortened_question = "#{question_content[0..122]}#{'…' if original_question_length > [123, question_content.length].min}"
shortened_answer = "#{answer_content[0..123]}#{'…' if original_answer_length > [124, answer_content.length].min}"
components = [
shortened_question,
'—',
shortened_answer,
post_tag,
answer_url
]
tweet_text = components.compact.join(' ')

parsed_tweet = Twitter::TwitterText::Validation::parse_tweet(tweet_text)

Expand Down
1 change: 1 addition & 0 deletions app/models/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ class Service < ApplicationRecord

belongs_to :user
validates_uniqueness_of :uid, scope: :type
validates_length_of :post_tag, maximum: 20

class << self

Expand Down
2 changes: 1 addition & 1 deletion app/models/services/twitter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ def client
end

def post_tweet(answer)
client.update! prepare_tweet(answer)
client.update! prepare_tweet(answer, self.post_tag)
end
end
6 changes: 6 additions & 0 deletions app/views/settings/_services.haml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,9 @@
service_path(service),
data: { confirm: t('views.settings.service.confirm', service: service.provider.capitalize) },
method: :delete

.col-md-6.mt-2
= bootstrap_form_for(service, as: 'service', url: update_service_path(service)) do |f|
= f.text_field :post_tag, class: 'form-control', label: 'tag', label_as_placeholder: true,
append: f.submit('Update', class: 'btn btn-primary'), maxlength: 20, pattern: '^[^@]*$',
help: "Automatically append a tag to your shared answers. #{'A # symbol is not automatically prepended.' if service.provider == 'twitter'}"
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@

# resources :services, only: [:index, :destroy]
match '/settings/services', to: 'services#index', via: 'get', as: :services
match '/settings/services/:id', to: 'services#update', via: 'patch', as: :update_service
match '/settings/services/:id', to: 'services#destroy', via: 'delete', as: :service
controller :services do
scope "/auth", as: "auth" do
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20220104180510_add_post_tag_to_services.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddPostTagToServices < ActiveRecord::Migration[5.2]
def change
add_column :services, :post_tag, :string, limit: 20
end
end
1 change: 1 addition & 0 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@
t.string "nickname"
t.datetime "created_at"
t.datetime "updated_at"
t.string "post_tag", limit: 20
t.index ["user_id"], name: "index_services_on_user_id"
end

Expand Down
41 changes: 41 additions & 0 deletions spec/controllers/services_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,45 @@
end
end
end

context '#update' do
subject { patch :update, params: params }

context 'not signed in' do
let(:params) { { id: 1 } }

it 'redirects to sign in page' do
subject
expect(response).to redirect_to(new_user_session_path)
end
end

context 'user with Twitter connection' do
before { sign_in user }

let(:user) { FactoryBot.create(:user) }
let(:service) { Services::Twitter.create(user: user, uid: 12) }
let(:params) { { id: service.id, service: { post_tag: post_tag } } }

context 'tag is valid' do
let(:post_tag) { '#askaraccoon' }

it 'updates a service connection' do
expect { subject }.to change { service.reload.post_tag }.to('#askaraccoon')
expect(response).to redirect_to(services_path)
expect(flash[:success]).to eq("Service updated successfully")
end
end

context 'tag is too long' do
let(:post_tag) { 'a' * 21 } # 1 character over the limit

it 'shows an error' do
subject
expect(response).to redirect_to(services_path)
expect(flash[:error]).to eq("Failed to update service")
end
end
end
end
end
25 changes: 23 additions & 2 deletions spec/helpers/social_helper/twitter_methods_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

describe SocialHelper::TwitterMethods, :type => :helper do
let(:user) { FactoryBot.create(:user) }
let(:question_content) { 'q' * 255 }
let(:answer_content) { 'a' * 255 }
let(:answer) { FactoryBot.create(:answer, user: user,
content: 'a' * 255,
question_content: 'q' * 255) }
content: answer_content,
question_content: question_content) }

before do
stub_const("APP_CONFIG", {
Expand All @@ -25,6 +27,25 @@
end
end

context 'when a suffix has been passed' do
let(:question_content) { 'question' }
let(:answer_content) { 'answer' }

subject { prepare_tweet(answer, '#askracc') }

it 'should include the suffix after the link' do
expect(subject).to eq("question — answer #askracc https://example.com/#{user.screen_name}/a/#{answer.id}")
end
end

context 'when a suffix has been passed and the tweet needs to be shortened' do
subject { prepare_tweet(answer, '#askracc') }

it 'should shorten the tweet while keeping the suffix intact' do
expect(subject).to eq("#{'q' * 120}… — #{'a' * 120}… #askracc https://example.com/#{user.screen_name}/a/#{answer.id}")
end
end

context 'when the question and answer are short' do
before do
answer.question.content = 'Why are raccoons so good?'
Expand Down

0 comments on commit 136e2fa

Please sign in to comment.