Correction so that the tweet url is actually well generated

This commit is contained in:
echarp 2015-10-23 16:21:42 +02:00
parent 31d64d636a
commit 791b9d58b0
3 changed files with 17 additions and 13 deletions

View File

@ -103,12 +103,16 @@ class Event < ActiveRecord::Base
"#{start_time.to_date} #{city}: #{title} #{hashtags.join(' ')}"
end
def to_tweet(url)
tweet = to_s
if (tweet.size + url.size >= 140)
tweet = tweet[0, tweet[0, 140 - url.size].rindex(/\s/)]
def to_tweet
url = Rails.application.routes.url_helpers.event_url(
self,
host: ActionMailer::Base.default_url_options[:host])
tweet = "#{self} #{url}"
if (tweet.size >= 140)
tweet = "#{tweet[0, tweet.rindex(/\s/, 140 - url.size)]} #{url}"
end
"#{tweet} #{url}"
tweet
end
private

View File

@ -33,6 +33,6 @@ class EventCallbacks
config.access_token = ENV['TWITTER_ACCESS_TOKEN']
config.access_token_secret = ENV['TWITTER_ACCESS_SECRET']
end
client.update "#{event.to_tweet event_url(event)}" if client.consumer_key
client.update event.to_tweet if client.consumer_key
end
end

View File

@ -22,7 +22,7 @@
%h3= t '.tweet_helper'
= @event.to_tweet event_url @event
= @event.to_tweet
%fieldset
%legend