Merge pull request #2707 from eyalev/rtl-email

Enable RTL direction in emails.
This commit is contained in:
Sam 2014-08-29 09:54:57 +10:00
commit 8ab9c57ca5
6 changed files with 94 additions and 18 deletions

View File

@ -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]

View File

@ -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}"

29
app/models/rtl.rb Normal file
View File

@ -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

View File

@ -1,4 +1,4 @@
<div id='main'>
<div id='main' class=<%= classes %>>
<% if top.present? %>
<div><%= top %></div>

View File

@ -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")

53
spec/models/rtl_spec.rb Normal file
View File

@ -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