scinote-web/app/helpers/activity_helper.rb

71 lines
2.9 KiB
Ruby
Raw Normal View History

2016-07-21 19:11:15 +08:00
module ActivityHelper
# constants for correct truncation length
TAGS_LENGTH = 4
TRUNCATE_OFFSET = 3
def activity_truncate(message, len = Constants::NAME_TRUNCATION_LENGTH)
2017-06-01 21:09:01 +08:00
activity_titles = message.scan(/<strong>(.*?)<\/strong>/)
activity_titles.each do |activity_title|
activity_title = activity_title[0]
# find first closing tag of smart annotation
closing_tag_sa = activity_title.index('</a>')
unless closing_tag_sa.nil?
opening_tag_sa = activity_title.index('<img')
opening_temp = activity_title.index('<span')
# depending on user/experiment set the first opening tag
opening_tag_sa = opening_temp if opening_tag_sa.nil? ||
opening_tag_sa > opening_temp
end
len_temp = len
# check until we run out of smart annotations in message
while !opening_tag_sa.nil? && !closing_tag_sa.nil? &&
opening_tag_sa < len_temp
stripped = strip_tags(activity_title[opening_tag_sa...closing_tag_sa])
.length
len_temp += (activity_title[opening_tag_sa...closing_tag_sa +
TAGS_LENGTH]).length - stripped
len = len_temp + TRUNCATE_OFFSET + TAGS_LENGTH if len <= closing_tag_sa
closing_temp = closing_tag_sa + 1
closing_tag_sa = activity_title.index('</a>', closing_temp)
unless closing_tag_sa.nil?
# find next smart annotation
opening_tag_sa = activity_title.index('<img', closing_temp)
opening_temp = activity_title.index('<span', closing_temp)
# depending on user/experiment set the next opening tag
opening_tag_sa = opening_temp if opening_tag_sa.nil? ||
opening_tag_sa > opening_temp
end
end
# adjust truncation length according to smart annotations length
len = activity_title.length if len > activity_title.length &&
len != Constants::NAME_TRUNCATION_LENGTH
if activity_title.length > len
2017-06-05 22:07:21 +08:00
title = "<div class='modal-tooltip'>
#{truncate(activity_title, length: len, escape: false)}
2017-06-05 22:07:21 +08:00
<span class='modal-tooltiptext'>
#{activity_title}
</span>
</div>"
2017-06-01 21:09:01 +08:00
else
title = truncate(activity_title, length: len, escape: false)
2017-06-01 21:09:01 +08:00
end
message = message.gsub(/#{Regexp.escape(activity_title)}/, title)
2016-07-21 19:11:15 +08:00
end
sanitize_input(message) if message
2016-07-21 19:11:15 +08:00
end
def days_since_1970(dt)
dt.to_i / 86400
end
2018-01-06 00:08:00 +08:00
def calculate_previous_date(activities,
index,
previus_batch_last_activitiy_date)
if index == 1 && !activities.first_page?
return previus_batch_last_activitiy_date
end
activity = activities[index - 1]
return activity.created_at.to_date if activity
Date.new(1901, 1, 1)
end
2016-07-21 19:11:15 +08:00
end