Browse Source

Translated emails and added time ago JS method

development
Godwin 8 years ago
parent
commit
973ca46c08
  1. 44
      app/assets/javascripts/time.js
  2. 13
      app/controllers/application_controller.rb
  3. 3
      app/helpers/application_helper.rb
  4. 8
      app/mailers/user_mailer.rb
  5. 1
      app/views/layouts/application.html.haml
  6. 2
      config/initializers/assets.rb
  7. 2
      config/locales/es.yml

44
app/assets/javascripts/time.js

@ -0,0 +1,44 @@
(function() {
function updateTimes() {
var updateIn = 0;
forEachElement('time', function(time) {
var date = new Date(time.getAttribute('datetime'));
var timeAgo = ((new Date()) - date) / (1000);
var unit = "seconds";
var updateTime = 0;
if (timeAgo >= 31536000) {
timeAgo /= 31536000;
unit = "over_x_years";
} else if (timeAgo >= 172800) {
timeAgo /= 172800;
unit = "x_days";
} else if (timeAgo >= 3600) {
timeAgo /= 3600;
unit = "x_hours";
updateTime = 3600;
} else if (timeAgo >= 60) {
timeAgo /= 60;
unit = "x_minutes"
updateTime = 60;
} else {
timeAgo = 1;
unit = "less_than_x_minutes";
updateTime = 10;
}
if (updateTime > 0 && (updateIn < 1 || updateTime < updateIn)) {
updateIn = updateTime;
}
time.setAttribute("title", date);
time.innerHTML = I18n.t('datetime.distance_in_words.time_ago', {time: I18n.t('datetime.distance_in_words.' + unit, {count: Math.floor(timeAgo)})});
});
if (updateIn > 0) {
window.setTimeout(updateTimes, updateIn + 1000);
}
}
window.addEventListener("load", updateTimes, false);
})();

13
app/controllers/application_controller.rb

@ -241,6 +241,11 @@ class ApplicationController < LinguaFrancaApplicationController
expiry ||= (Time.now + 12.hours) expiry ||= (Time.now + 12.hours)
session[:confirm_uid] = user.id session[:confirm_uid] = user.id
unless user.locale.present?
user.locale = I18n.locale
user.save
end
# send the confirmation email and make sure it get sent as quickly as possible # send the confirmation email and make sure it get sent as quickly as possible
UserMailer.send_mail :email_confirmation do UserMailer.send_mail :email_confirmation do
EmailConfirmation.create(user_id: user.id, expiry: expiry, url: url) EmailConfirmation.create(user_id: user.id, expiry: expiry, url: url)
@ -324,12 +329,8 @@ class ApplicationController < LinguaFrancaApplicationController
end end
user = User.find_by_email(params[:email]) user = User.find_by_email(params[:email])
unless user unless user.present?
# not really a good UX so we should fix this later user = User.create(:email => params[:email], locale: I18n.locale)
#do_404
#return
user = User.new(:email => params[:email])
user.save!
end end
# generate the confirmation, send the email and show the 403 # generate the confirmation, send the email and show the 403

3
app/helpers/application_helper.rb

@ -1361,6 +1361,9 @@ module ApplicationHelper
end end
def comment(comment) def comment(comment)
add_inline_script :time
add_js_translation('datetime.distance_in_words')
content_tag(:div, class: 'comment-body') do content_tag(:div, class: 'comment-body') do
content_tag(:h4, comment.user.name, class: 'comment-title') + content_tag(:h4, comment.user.name, class: 'comment-title') +
content_tag(:time, time(comment.created_at, :default), datetime: comment.created_at.to_s) + content_tag(:time, time(comment.created_at, :default), datetime: comment.created_at.to_s) +

8
app/mailers/user_mailer.rb

@ -10,6 +10,7 @@ class UserMailer < ActionMailer::Base
def email_confirmation(confirmation) def email_confirmation(confirmation)
@confirmation = EmailConfirmation.find(confirmation) if confirmation.present? @confirmation = EmailConfirmation.find(confirmation) if confirmation.present?
I18n.locale = @confirmation.user.locale if @confirmation.user.locale.present?
@subject = _'email.subject.confirm_email','Please confirm your email address' @subject = _'email.subject.confirm_email','Please confirm your email address'
mail to: @confirmation.user.named_email, subject: @subject mail to: @confirmation.user.named_email, subject: @subject
end end
@ -18,6 +19,7 @@ class UserMailer < ActionMailer::Base
@registration = ConferenceRegistration.find(registration) if registration.present? @registration = ConferenceRegistration.find(registration) if registration.present?
@conference = @registration.conference @conference = @registration.conference
@user = @registration.user @user = @registration.user
I18n.locale = @user.locale if @user.locale.present?
@subject = @conference.registration_status.to_sym == :pre ? @subject = @conference.registration_status.to_sym == :pre ?
_( _(
'email.subject.pre_registration_confirmed', 'email.subject.pre_registration_confirmed',
@ -47,6 +49,7 @@ class UserMailer < ActionMailer::Base
@workshop = Workshop.find(workshop) if workshop.present? @workshop = Workshop.find(workshop) if workshop.present?
@requester = User.find(requester) if requester.present? @requester = User.find(requester) if requester.present?
addresses = [] addresses = []
I18n.locale = @workshop.active_facilitators.first.locale if @workshop.active_facilitators.first.locale.present?
@workshop.active_facilitators.each do |f| @workshop.active_facilitators.each do |f|
addresses << f.named_email addresses << f.named_email
end end
@ -62,6 +65,7 @@ class UserMailer < ActionMailer::Base
@workshop = Workshop.find(workshop) if workshop.present? @workshop = Workshop.find(workshop) if workshop.present?
@conference = Conference.find(@workshop.conference_id) @conference = Conference.find(@workshop.conference_id)
@user = User.find(user) if user.present? @user = User.find(user) if user.present?
I18n.locale = @user.locale if @user.locale.present?
@subject = (_'email.subject.workshop_request_approved', @subject = (_'email.subject.workshop_request_approved',
"You have been added as a facilitator of #{@workshop.title}", "You have been added as a facilitator of #{@workshop.title}",
:vars => {:workshop_title => @workshop.title}) :vars => {:workshop_title => @workshop.title})
@ -72,6 +76,7 @@ class UserMailer < ActionMailer::Base
@workshop = Workshop.find(workshop) if workshop.present? @workshop = Workshop.find(workshop) if workshop.present?
@conference = @workshop.conference @conference = @workshop.conference
@user = User.find(user) if user.present? @user = User.find(user) if user.present?
I18n.locale = @user.locale if @user.present? && @user.locale.present?
@subject = (_'email.subject.workshop_request_denied', @subject = (_'email.subject.workshop_request_denied',
"Your request to facilitate #{@workshop.title} has been denied", "Your request to facilitate #{@workshop.title} has been denied",
:vars => {:workshop_title => @workshop.title}) :vars => {:workshop_title => @workshop.title})
@ -84,6 +89,7 @@ class UserMailer < ActionMailer::Base
@locale = locale @locale = locale
@locale_name = language_name(locale) @locale_name = language_name(locale)
@user = User.find(user) if user.present? @user = User.find(user) if user.present?
I18n.locale = @user.locale if @user.present? && @user.locale.present?
@translator = User.find(translator) if translator.present? @translator = User.find(translator) if translator.present?
@subject = (_'email.subject.workshop_translated', @subject = (_'email.subject.workshop_translated',
"The #{@locale_name} translation for #{@workshop.title} has been modified", "The #{@locale_name} translation for #{@workshop.title} has been modified",
@ -98,6 +104,7 @@ class UserMailer < ActionMailer::Base
@workshop = Workshop.find(workshop) if workshop.present? @workshop = Workshop.find(workshop) if workshop.present?
@data = data @data = data
@user = User.find(user) if user.present? @user = User.find(user) if user.present?
I18n.locale = @user.locale if @user.present? && @user.locale.present?
@translator = User.find(translator) if translator.present? @translator = User.find(translator) if translator.present?
@subject = (_'email.subject.workshop_original_content_changed', @subject = (_'email.subject.workshop_original_content_changed',
"Original content for #{@workshop.title} has been modified", "Original content for #{@workshop.title} has been modified",
@ -119,6 +126,7 @@ class UserMailer < ActionMailer::Base
@workshop = Workshop.find(workshop) if workshop.present? @workshop = Workshop.find(workshop) if workshop.present?
@comment = Comment.find(comment) if comment.present? @comment = Comment.find(comment) if comment.present?
@user = User.find(user) if user.present? @user = User.find(user) if user.present?
I18n.locale = @user.locale if @user.present? && @user.locale.present?
if @comment.reply? if @comment.reply?
@subject = (_'email.subject.workshop_comment.reply', vars: { user_name: @comment.user.name }) @subject = (_'email.subject.workshop_comment.reply', vars: { user_name: @comment.user.name })

1
app/views/layouts/application.html.haml

@ -82,3 +82,4 @@
%button.close-btn.subdued=_'forms.actions.generic.close' %button.close-btn.subdued=_'forms.actions.generic.close'
= yield :footer_scripts if content_for?(:footer_scripts) = yield :footer_scripts if content_for?(:footer_scripts)
= inline_scripts = inline_scripts
= emit_js_translations

2
config/initializers/assets.rb

@ -8,4 +8,4 @@ Rails.application.config.assets.version = '1.0'
# Precompile additional assets. # Precompile additional assets.
# application.js, application.css, and all non-JS/CSS in app/assets folder are already added. # application.js, application.css, and all non-JS/CSS in app/assets folder are already added.
Rails.application.config.assets.precompile += %w( user-mailer.css map.js pen.js editor.js markdown.js html2canvas.js main.js housing.js schedule.js favicon.ico ) Rails.application.config.assets.precompile += %w( user-mailer.css map.js pen.js time.js editor.js markdown.js html2canvas.js main.js housing.js schedule.js favicon.ico )

2
config/locales/es.yml

@ -1009,7 +1009,7 @@ es:
actions: actions:
generic: generic:
add_comment: Añadir comentario add_comment: Añadir comentario
agree: Estoy en acuerdo. agree: Estoy en acuerdo
register: Registrarse register: Registrarse
edit_registration: Cambiar mi registracion edit_registration: Cambiar mi registracion
custom_amount: Cantidad diferente custom_amount: Cantidad diferente

Loading…
Cancel
Save