about summary refs log tree commit diff
path: root/app/helpers
diff options
context:
space:
mode:
authorJoël Quenneville <joel.quen@gmail.com>2017-04-21 18:13:37 -0400
committerEugen <eugen@zeonfederated.com>2017-04-22 00:13:37 +0200
commit974ac467de5a60a30dc3d19b8ced732e04395879 (patch)
treea2acf21b97956ffd047a2fdab93c1ce687848f12 /app/helpers
parentaf7d02da5d2ec014fb87765b65b23d7b3ed6c90c (diff)
Add tests for StreamEntriesHelper#rtl? (#2286)
We used some random Arabic characters to test that the various RTL
conditions got triggered.
Diffstat (limited to 'app/helpers')
-rw-r--r--app/helpers/stream_entries_helper.rb20
1 files changed, 11 insertions, 9 deletions
diff --git a/app/helpers/stream_entries_helper.rb b/app/helpers/stream_entries_helper.rb
index 00a01df44..daa4a48e4 100644
--- a/app/helpers/stream_entries_helper.rb
+++ b/app/helpers/stream_entries_helper.rb
@@ -37,20 +37,22 @@ module StreamEntriesHelper
   end
 
   def rtl?(text)
-    return false if text.empty?
+    rtl_characters = /[\p{Hebrew}|\p{Arabic}|\p{Syriac}|\p{Thaana}|\p{Nko}]+/m.match(text)
 
-    matches = /[\p{Hebrew}|\p{Arabic}|\p{Syriac}|\p{Thaana}|\p{Nko}]+/m.match(text)
-
-    return false unless matches
-
-    rtl_size = matches.to_a.reduce(0) { |acc, elem| acc + elem.size }.to_f
-    ltr_size = text.strip.size.to_f
-
-    rtl_size / ltr_size > 0.3
+    if rtl_characters.present?
+      total_size = text.strip.size.to_f
+      rtl_size(rtl_characters.to_a) / total_size > 0.3
+    else
+      false
+    end
   end
 
   private
 
+  def rtl_size(characters)
+    characters.reduce(0) { |acc, elem| acc + elem.size }.to_f
+  end
+
   def embedded_view?
     params[:controller] == 'stream_entries' && params[:action] == 'embed'
   end