diff options
Diffstat (limited to 'app')
-rw-r--r-- | app/controllers/request_controller.rb | 4 | ||||
-rwxr-xr-x | app/helpers/link_to_helper.rb | 51 | ||||
-rw-r--r-- | app/models/incoming_message.rb | 31 | ||||
-rw-r--r-- | app/models/public_body_change_request.rb | 9 | ||||
-rw-r--r-- | app/views/comment/_single_comment.text.erb | 2 | ||||
-rw-r--r-- | app/views/request/_outgoing_correspondence.text.erb | 2 | ||||
-rw-r--r-- | app/views/request/_resent_outgoing_correspondence.text.erb | 2 | ||||
-rw-r--r-- | app/views/track_mailer/event_digest.text.erb | 2 |
8 files changed, 75 insertions, 28 deletions
diff --git a/app/controllers/request_controller.rb b/app/controllers/request_controller.rb index a94461758..6445dd685 100644 --- a/app/controllers/request_controller.rb +++ b/app/controllers/request_controller.rb @@ -447,9 +447,9 @@ class RequestController < ApplicationController flash[:notice] = case info_request.calculate_status when 'waiting_response' _("<p>Thank you! Hopefully your wait isn't too long.</p> <p>By law, you should get a response promptly, and normally before the end of <strong> -{{date_response_required_by}}</strong>.</p>",:date_response_required_by=>simple_date(info_request.date_response_required_by)) +{{date_response_required_by}}</strong>.</p>",:date_response_required_by=>view_context.simple_date(info_request.date_response_required_by)) when 'waiting_response_overdue' - _("<p>Thank you! Hope you don't have to wait much longer.</p> <p>By law, you should have got a response promptly, and normally before the end of <strong>{{date_response_required_by}}</strong>.</p>",:date_response_required_by=>simple_date(info_request.date_response_required_by)) + _("<p>Thank you! Hope you don't have to wait much longer.</p> <p>By law, you should have got a response promptly, and normally before the end of <strong>{{date_response_required_by}}</strong>.</p>",:date_response_required_by=>view_context.simple_date(info_request.date_response_required_by)) when 'waiting_response_very_overdue' _("<p>Thank you! Your request is long overdue, by more than {{very_late_number_of_days}} working days. Most requests should be answered within {{late_number_of_days}} working days. You might like to complain about this, see below.</p>", :very_late_number_of_days => AlaveteliConfiguration::reply_very_late_after_days, :late_number_of_days => AlaveteliConfiguration::reply_late_after_days) when 'not_held' diff --git a/app/helpers/link_to_helper.rb b/app/helpers/link_to_helper.rb index 405886a85..bf70f9140 100755 --- a/app/helpers/link_to_helper.rb +++ b/app/helpers/link_to_helper.rb @@ -279,13 +279,58 @@ module LinkToHelper end end - # Basic date format - def simple_date(date) + # Public: Usually-correct format for a DateTime-ish object + # To define a new new format define the `simple_date_{FORMAT}` method + # + # date - a DateTime, Date or Time + # opts - a Hash of options (default: { format: :html}) + # :format - :html returns a HTML <time> tag + # :text returns a plain String + # + # Examples + # + # simple_date(Time.now) + # # => "<time>..." + # + # simple_date(Time.now, :format => :text) + # # => "March 10, 2014" + # + # Returns a String + # Raises ArgumentError if the format is unrecognized + def simple_date(date, opts = {}) + opts = { :format => :html }.merge(opts) + date_formatter = "simple_date_#{ opts[:format] }" + + if respond_to?(date_formatter) + send(date_formatter, date) + else + raise ArgumentError, "Unrecognised format :#{ opts[:format] }" + end + end + + # Usually-correct HTML formatting of a DateTime-ish object + # Use LinkToHelper#simple_date with desired formatting options + # + # date - a DateTime, Date or Time + # + # Returns a String + def simple_date_html(date) + date = date.in_time_zone unless date.is_a? Date + time_tag date, simple_date_text(date), :title => date.to_s + end + + # Usually-correct plain text formatting of a DateTime-ish object + # Use LinkToHelper#simple_date with desired formatting options + # + # date - a DateTime, Date or Time + # + # Returns a String + def simple_date_text(date) date = date.in_time_zone.to_date unless date.is_a? Date date_format = _("simple_date_format") date_format = :long if date_format == "simple_date_format" - return I18n.l(date, :format => date_format) + I18n.l(date, :format => date_format) end def simple_time(date) diff --git a/app/models/incoming_message.rb b/app/models/incoming_message.rb index 59e61952e..6db145348 100644 --- a/app/models/incoming_message.rb +++ b/app/models/incoming_message.rb @@ -35,7 +35,7 @@ require 'htmlentities' require 'rexml/document' require 'zip/zip' -require 'iconv' unless RUBY_VERSION >= '1.9' +require 'iconv' unless String.method_defined?(:encode) class IncomingMessage < ActiveRecord::Base extend MessageProminence @@ -294,7 +294,7 @@ class IncomingMessage < ActiveRecord::Base emails = ascii_chars.scan(MySociety::Validate.email_find_regexp) # Convert back to UCS-2, making a mask at the same time - if RUBY_VERSION >= '1.9' + if String.method_defined?(:encode) emails.map! do |email| # We want the ASCII representation of UCS-2 [email[0].encode('UTF-16LE').force_encoding('US-ASCII'), @@ -520,7 +520,7 @@ class IncomingMessage < ActiveRecord::Base # should instead tell elinks to respect the source # charset use_charset = "utf-8" - if RUBY_VERSION.to_f >= 1.9 + if String.method_defined?(:encode) begin text.encode('utf-8') rescue Encoding::UndefinedConversionError, Encoding::InvalidByteSequenceError @@ -551,7 +551,7 @@ class IncomingMessage < ActiveRecord::Base end def _sanitize_text(text) - if RUBY_VERSION.to_f >= 1.9 + if String.method_defined?(:encode) begin # Test if it's good UTF-8 text.encode('utf-8') @@ -796,27 +796,28 @@ class IncomingMessage < ActiveRecord::Base return self.cached_attachment_text_clipped end - def _get_attachment_text_internal + def _extract_text # Extract text from each attachment - text = '' - attachments = self.get_attachments_for_display - for attachment in attachments - text += MailHandler.get_attachment_text_one_file(attachment.content_type, + self.get_attachments_for_display.reduce(''){ |memo, attachment| + memo += MailHandler.get_attachment_text_one_file(attachment.content_type, attachment.body, attachment.charset) - end + } + end + + def _get_attachment_text_internal + text = self._extract_text # Remove any bad characters - if RUBY_VERSION >= '1.9' - text.encode("utf-8", :invalid => :replace, - :undef => :replace, - :replace => "") + if String.method_defined?(:encode) + # handle "problematic" encoding + text.encode!('UTF-16', 'UTF-8', :invalid => :replace, :undef => :replace, :replace => '') + text.encode('UTF-8', 'UTF-16') else Iconv.conv('utf-8//IGNORE', 'utf-8', text) end end - # Returns text for indexing def get_text_for_indexing_full return get_body_for_quoting + "\n\n" + get_attachment_text_full diff --git a/app/models/public_body_change_request.rb b/app/models/public_body_change_request.rb index c1f395c0c..0e59cbecc 100644 --- a/app/models/public_body_change_request.rb +++ b/app/models/public_body_change_request.rb @@ -75,7 +75,8 @@ class PublicBodyChangeRequest < ActiveRecord::Base def thanks_notice if self.public_body - _("Your request to update the address for #{get_public_body_name} has been sent. Thank you for getting in touch! We'll get back to you soon.") + _("Your request to update the address for {{public_body_name}} has been sent. Thank you for getting in touch! We'll get back to you soon.", + :public_body_name => get_public_body_name) else _("Your request to add an authority has been sent. Thank you for getting in touch! We'll get back to you soon.") end @@ -89,12 +90,12 @@ class PublicBodyChangeRequest < ActiveRecord::Base end def comment_for_public_body - comments = [_("Requested by: #{get_user_name} (#{get_user_email})")] + comments = ["Requested by: #{get_user_name} (#{get_user_email})"] if !source_url.blank? - comments << _("Source URL: #{source_url}") + comments << "Source URL: #{source_url}" end if !notes.blank? - comments << _("Notes: #{notes}") + comments << "Notes: #{notes}" end comments.join("\n") end diff --git a/app/views/comment/_single_comment.text.erb b/app/views/comment/_single_comment.text.erb index 925e8b688..4932a7e4a 100644 --- a/app/views/comment/_single_comment.text.erb +++ b/app/views/comment/_single_comment.text.erb @@ -1,2 +1,2 @@ -<%= _("{{username}} left an annotation:", :username =>comment.user.name) %> (<%= simple_date(comment.created_at || Time.now) %>) +<%= _("{{username}} left an annotation:", :username =>comment.user.name) %> (<%= simple_date((comment.created_at || Time.now), :format => :text) %>) <%= comment.body.strip %> diff --git a/app/views/request/_outgoing_correspondence.text.erb b/app/views/request/_outgoing_correspondence.text.erb index 5375ef81b..221e359e2 100644 --- a/app/views/request/_outgoing_correspondence.text.erb +++ b/app/views/request/_outgoing_correspondence.text.erb @@ -3,6 +3,6 @@ <%- else %> <%= _('From:') %> <% if @info_request.user_name %><%= @info_request.user_name %><% else %><%= "[#{_('An anonymous user')}]"%><% end %> <%= _('To:') %> <%= @info_request.public_body.name %> - <%= _('Date:') %> <%= simple_date(info_request_event.created_at) %> + <%= _('Date:') %> <%= simple_date(info_request_event.created_at, :format => :text) %> <%= outgoing_message.get_body_for_text_display %> <%- end %> diff --git a/app/views/request/_resent_outgoing_correspondence.text.erb b/app/views/request/_resent_outgoing_correspondence.text.erb index d645e9488..d39f8395b 100644 --- a/app/views/request/_resent_outgoing_correspondence.text.erb +++ b/app/views/request/_resent_outgoing_correspondence.text.erb @@ -1,2 +1,2 @@ -<%= _('Date:') %> <%= simple_date(info_request_event.created_at) %> +<%= _('Date:') %> <%= simple_date(info_request_event.created_at, :format => :text) %> Sent <% if outgoing_message.message_type == 'initial_request' %> request <% elsif outgoing_message.message_type == 'followup' %> a follow up <% else %> <% raise "unknown message_type" %><% end %> to <%= public_body_link(@info_request.public_body) %> again<% if not info_request_event.same_email_as_previous_send? %>, using a new contact address<% end %>. diff --git a/app/views/track_mailer/event_digest.text.erb b/app/views/track_mailer/event_digest.text.erb index 8dbc7fe06..b83c184f0 100644 --- a/app/views/track_mailer/event_digest.text.erb +++ b/app/views/track_mailer/event_digest.text.erb @@ -32,7 +32,7 @@ else raise "unknown type in event_digest " + event.event_type end - main_text += " (" + simple_date(event.created_at) + ")\n" + main_text += " (" + simple_date(event.created_at, :format => :text) + ")\n" # Main text, wrapped, words highlighted with * and indented. if event.is_outgoing_message? |