From 973ca46c083d071dbe8479aa6b18604d67daadd7 Mon Sep 17 00:00:00 2001 From: Godwin Date: Fri, 5 Aug 2016 22:31:25 -0700 Subject: [PATCH] Translated emails and added time ago JS method --- app/assets/javascripts/time.js | 44 +++++++++++++++++++++++ app/controllers/application_controller.rb | 13 +++---- app/helpers/application_helper.rb | 3 ++ app/mailers/user_mailer.rb | 8 +++++ app/views/layouts/application.html.haml | 1 + config/initializers/assets.rb | 2 +- config/locales/es.yml | 2 +- 7 files changed, 65 insertions(+), 8 deletions(-) create mode 100644 app/assets/javascripts/time.js diff --git a/app/assets/javascripts/time.js b/app/assets/javascripts/time.js new file mode 100644 index 0000000..0149572 --- /dev/null +++ b/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); +})(); \ No newline at end of file diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 2bed874..d768b45 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -241,6 +241,11 @@ class ApplicationController < LinguaFrancaApplicationController expiry ||= (Time.now + 12.hours) 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 UserMailer.send_mail :email_confirmation do EmailConfirmation.create(user_id: user.id, expiry: expiry, url: url) @@ -324,12 +329,8 @@ class ApplicationController < LinguaFrancaApplicationController end user = User.find_by_email(params[:email]) - unless user - # not really a good UX so we should fix this later - #do_404 - #return - user = User.new(:email => params[:email]) - user.save! + unless user.present? + user = User.create(:email => params[:email], locale: I18n.locale) end # generate the confirmation, send the email and show the 403 diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 537b8a7..b199e1d 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -1361,6 +1361,9 @@ module ApplicationHelper end def comment(comment) + add_inline_script :time + add_js_translation('datetime.distance_in_words') + content_tag(:div, class: 'comment-body') do content_tag(:h4, comment.user.name, class: 'comment-title') + content_tag(:time, time(comment.created_at, :default), datetime: comment.created_at.to_s) + diff --git a/app/mailers/user_mailer.rb b/app/mailers/user_mailer.rb index cbab8b2..579d977 100644 --- a/app/mailers/user_mailer.rb +++ b/app/mailers/user_mailer.rb @@ -10,6 +10,7 @@ class UserMailer < ActionMailer::Base def email_confirmation(confirmation) @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' mail to: @confirmation.user.named_email, subject: @subject end @@ -18,6 +19,7 @@ class UserMailer < ActionMailer::Base @registration = ConferenceRegistration.find(registration) if registration.present? @conference = @registration.conference @user = @registration.user + I18n.locale = @user.locale if @user.locale.present? @subject = @conference.registration_status.to_sym == :pre ? _( 'email.subject.pre_registration_confirmed', @@ -47,6 +49,7 @@ class UserMailer < ActionMailer::Base @workshop = Workshop.find(workshop) if workshop.present? @requester = User.find(requester) if requester.present? addresses = [] + I18n.locale = @workshop.active_facilitators.first.locale if @workshop.active_facilitators.first.locale.present? @workshop.active_facilitators.each do |f| addresses << f.named_email end @@ -62,6 +65,7 @@ class UserMailer < ActionMailer::Base @workshop = Workshop.find(workshop) if workshop.present? @conference = Conference.find(@workshop.conference_id) @user = User.find(user) if user.present? + I18n.locale = @user.locale if @user.locale.present? @subject = (_'email.subject.workshop_request_approved', "You have been added as a facilitator of #{@workshop.title}", :vars => {:workshop_title => @workshop.title}) @@ -72,6 +76,7 @@ class UserMailer < ActionMailer::Base @workshop = Workshop.find(workshop) if workshop.present? @conference = @workshop.conference @user = User.find(user) if user.present? + I18n.locale = @user.locale if @user.present? && @user.locale.present? @subject = (_'email.subject.workshop_request_denied', "Your request to facilitate #{@workshop.title} has been denied", :vars => {:workshop_title => @workshop.title}) @@ -84,6 +89,7 @@ class UserMailer < ActionMailer::Base @locale = locale @locale_name = language_name(locale) @user = User.find(user) if user.present? + I18n.locale = @user.locale if @user.present? && @user.locale.present? @translator = User.find(translator) if translator.present? @subject = (_'email.subject.workshop_translated', "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? @data = data @user = User.find(user) if user.present? + I18n.locale = @user.locale if @user.present? && @user.locale.present? @translator = User.find(translator) if translator.present? @subject = (_'email.subject.workshop_original_content_changed', "Original content for #{@workshop.title} has been modified", @@ -119,6 +126,7 @@ class UserMailer < ActionMailer::Base @workshop = Workshop.find(workshop) if workshop.present? @comment = Comment.find(comment) if comment.present? @user = User.find(user) if user.present? + I18n.locale = @user.locale if @user.present? && @user.locale.present? if @comment.reply? @subject = (_'email.subject.workshop_comment.reply', vars: { user_name: @comment.user.name }) diff --git a/app/views/layouts/application.html.haml b/app/views/layouts/application.html.haml index a09c671..1bd7edb 100644 --- a/app/views/layouts/application.html.haml +++ b/app/views/layouts/application.html.haml @@ -82,3 +82,4 @@ %button.close-btn.subdued=_'forms.actions.generic.close' = yield :footer_scripts if content_for?(:footer_scripts) = inline_scripts + = emit_js_translations diff --git a/config/initializers/assets.rb b/config/initializers/assets.rb index 9472591..bf4b408 100644 --- a/config/initializers/assets.rb +++ b/config/initializers/assets.rb @@ -8,4 +8,4 @@ Rails.application.config.assets.version = '1.0' # Precompile additional assets. # 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 ) diff --git a/config/locales/es.yml b/config/locales/es.yml index e31c030..094a971 100644 --- a/config/locales/es.yml +++ b/config/locales/es.yml @@ -1009,7 +1009,7 @@ es: actions: generic: add_comment: AƱadir comentario - agree: Estoy en acuerdo. + agree: Estoy en acuerdo register: Registrarse edit_registration: Cambiar mi registracion custom_amount: Cantidad diferente