From e0c2f3df3af259c4e6be9059494773a4cd9d6681 Mon Sep 17 00:00:00 2001
From: Eyal Levin <eyalev@gmail.com>
Date: Wed, 27 Aug 2014 14:38:03 +0300
Subject: [PATCH] Enable RTL direction in emails.

---
 app/helpers/application_helper.rb     | 21 +++--------
 app/mailers/user_notifications.rb     |  6 ++-
 app/models/rtl.rb                     | 29 +++++++++++++++
 app/views/email/notification.html.erb |  2 +-
 lib/email/styles.rb                   |  1 +
 spec/models/rtl_spec.rb               | 53 +++++++++++++++++++++++++++
 6 files changed, 94 insertions(+), 18 deletions(-)
 create mode 100644 app/models/rtl.rb
 create mode 100644 spec/models/rtl_spec.rb

diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb
index 06496381339..efa4d2cbf01 100644
--- a/app/helpers/application_helper.rb
+++ b/app/helpers/application_helper.rb
@@ -32,7 +32,11 @@ module ApplicationHelper
   end
 
   def html_classes
-    "#{mobile_view? ? 'mobile-view' : 'desktop-view'} #{mobile_device? ? 'mobile-device' : 'not-mobile-device'} #{rtl_view? ? 'rtl' : ''}"
+    "#{mobile_view? ? 'mobile-view' : 'desktop-view'} #{mobile_device? ? 'mobile-device' : 'not-mobile-device'} #{rtl_class}"
+  end
+
+  def rtl_class
+    RTL.new(current_user).css_class
   end
 
   def escape_unicode(javascript)
@@ -131,21 +135,6 @@ module ApplicationHelper
     MobileDetection.mobile_device?(request.user_agent)
   end
 
-  def rtl_view?
-     site_default_rtl? || current_user_rtl?
-  end
-
-  def current_user_rtl?
-    SiteSetting.allow_user_locale && current_user.try(:locale).in?(rtl_locales)
-  end
-
-  def site_default_rtl?
-    !SiteSetting.allow_user_locale && SiteSetting.default_locale.in?(rtl_locales)
-  end
-
-  def rtl_locales
-    %w(he ar)
-  end
 
   def customization_disabled?
     controller.class.name.split("::").first == "Admin" || session[:disable_customization]
diff --git a/app/mailers/user_notifications.rb b/app/mailers/user_notifications.rb
index 6ada3e2e4dd..d1e5e584591 100644
--- a/app/mailers/user_notifications.rb
+++ b/app/mailers/user_notifications.rb
@@ -198,7 +198,11 @@ class UserNotifications < ActionMailer::Base
     html = UserNotificationRenderer.new(Rails.configuration.paths["app/views"]).render(
       template: 'email/notification',
       format: :html,
-      locals: { context_posts: context_posts, post: post, top: top ? PrettyText.cook(top).html_safe : nil }
+      locals: { context_posts: context_posts,
+                post: post,
+                top: top ? PrettyText.cook(top).html_safe : nil,
+                classes: RTL.new(user).css_class
+      }
     )
 
     template = "user_notifications.user_#{notification_type}"
diff --git a/app/models/rtl.rb b/app/models/rtl.rb
new file mode 100644
index 00000000000..d4857c1fb91
--- /dev/null
+++ b/app/models/rtl.rb
@@ -0,0 +1,29 @@
+class RTL
+
+  attr_reader :user
+
+  def initialize(user)
+    @user = user
+  end
+
+  def enabled?
+    site_locale_rtl? || current_user_rtl?
+  end
+
+  def current_user_rtl?
+    SiteSetting.allow_user_locale && user.try(:locale).in?(rtl_locales)
+  end
+
+  def site_locale_rtl?
+    !SiteSetting.allow_user_locale && SiteSetting.default_locale.in?(rtl_locales)
+  end
+
+  def rtl_locales
+    %w(he ar)
+  end
+
+  def css_class
+    enabled? ? 'rtl' : ''
+  end
+
+end
diff --git a/app/views/email/notification.html.erb b/app/views/email/notification.html.erb
index 8108cc693f6..363f2ae46d4 100644
--- a/app/views/email/notification.html.erb
+++ b/app/views/email/notification.html.erb
@@ -1,4 +1,4 @@
-<div id='main'>
+<div id='main' class=<%= classes %>>
 
 <% if top.present? %>
   <div><%= top %></div>
diff --git a/lib/email/styles.rb b/lib/email/styles.rb
index ea7744edd18..ecb7afb0b9c 100644
--- a/lib/email/styles.rb
+++ b/lib/email/styles.rb
@@ -58,6 +58,7 @@ module Email
       style('.user-avatar', 'vertical-align:top;width:55px;')
       style('.user-avatar img', nil, width: '45', height: '45')
       style('hr', 'background-color: #ddd; height: 1px; border: 1px;')
+      style('.rtl', 'direction: rtl;')
       # we can do this but it does not look right
       # style('#main', 'font-family:"Helvetica Neue", Helvetica, Arial, sans-serif')
       style('td.body', 'padding-top:5px;', colspan: "2")
diff --git a/spec/models/rtl_spec.rb b/spec/models/rtl_spec.rb
new file mode 100644
index 00000000000..0d69a858844
--- /dev/null
+++ b/spec/models/rtl_spec.rb
@@ -0,0 +1,53 @@
+require 'spec_helper'
+
+describe RTL do
+
+  let(:user) { Fabricate.build(:user) }
+
+  describe '.css_class' do
+
+    context 'user locale is allowed' do
+      before { SiteSetting.stubs(:allow_user_locale).returns(true) }
+
+      context 'user locale is RTL' do
+        before { user.stubs(:locale).returns('he') }
+
+        it 'returns rtl class' do
+          RTL.new(user).css_class.should == 'rtl'
+        end
+      end
+
+      context 'user locale is not RTL' do
+        it 'returns empty class' do
+          RTL.new(user).css_class.should == ''
+        end
+      end
+
+    end
+
+    context 'user locale is not allowed' do
+      before { SiteSetting.stubs(:allow_user_locale).returns(false) }
+
+      context 'site default locale is RTL' do
+        before { SiteSetting.stubs(:default_locale).returns('he') }
+
+        it 'returns rtl class' do
+          RTL.new(user).css_class.should == 'rtl'
+        end
+      end
+
+      context 'site default locale is LTR' do
+        before { SiteSetting.stubs(:default_locale).returns('en') }
+
+        context 'user locale is RTL' do
+          before { user.stubs(:locale).returns('he') }
+
+          it 'returns empty class' do
+            RTL.new(user).css_class.should == ''
+          end
+        end
+      end
+    end
+
+  end
+end