Module: ArticleHelper

Defined in:
app/helpers/article_helper.rb

Constant Summary collapse

MORE_CHARACTERS_TOLERANCE =
100
NUMBER_OF_CHARACTERS =
1200

Instance Method Summary collapse

Instance Method Details

#shorten_text(text, number_of_characters = NUMBER_OF_CHARACTERS, more_link = nil) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'app/helpers/article_helper.rb', line 6

def shorten_text(text, number_of_characters = NUMBER_OF_CHARACTERS, more_link = nil)
  if !(text.nil? || text.empty?)
    if text.length <= number_of_characters
      shorter_text = text
    else
      punction_char_after_end = \
        (text[number_of_characters..number_of_characters+MORE_CHARACTERS_TOLERANCE] =~ /\.|,|;/) || 0
      shorter_text = text[0..number_of_characters + punction_char_after_end]

      if more_link
        shorter_text += more_link
      end
    end
  else
    shorter_text = ''
  end

  shorter_text
end