aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--app/helpers/application_helper.rb3
-rw-r--r--app/helpers/date_time_helper.rb69
-rwxr-xr-xapp/helpers/link_to_helper.rb83
-rw-r--r--app/mailers/request_mailer.rb2
-rw-r--r--app/views/track_mailer/event_digest.text.erb6
-rw-r--r--app/views/user/_user_listing_single.html.erb2
-rw-r--r--app/views/user/show.html.erb2
-rw-r--r--spec/helpers/date_time_helper_spec.rb71
-rw-r--r--spec/helpers/link_to_helper_spec.rb123
9 files changed, 245 insertions, 116 deletions
diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb
index 33525cb3d..45b042354 100644
--- a/app/helpers/application_helper.rb
+++ b/app/helpers/application_helper.rb
@@ -13,6 +13,9 @@ module ApplicationHelper
# all of all.
include LinkToHelper
+ # Some extra date and time formatters
+ include DateTimeHelper
+
# Site-wide access to configuration settings
include ConfigHelper
diff --git a/app/helpers/date_time_helper.rb b/app/helpers/date_time_helper.rb
new file mode 100644
index 000000000..5f129e590
--- /dev/null
+++ b/app/helpers/date_time_helper.rb
@@ -0,0 +1,69 @@
+module DateTimeHelper
+ # 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, "Unrecognized 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'
+ I18n.l(date, :format => date_format)
+ end
+
+ # Strips the date from a DateTime
+ #
+ # date - a DateTime, Date or Time
+ #
+ # Examples
+ #
+ # simple_time(Time.now)
+ # # => "10:46:54"
+ #
+ # Returns a String
+ def simple_time(date)
+ date.strftime("%H:%M:%S").strip
+ end
+end
diff --git a/app/helpers/link_to_helper.rb b/app/helpers/link_to_helper.rb
index dd6ffa805..3709469cf 100755
--- a/app/helpers/link_to_helper.rb
+++ b/app/helpers/link_to_helper.rb
@@ -28,19 +28,19 @@ module LinkToHelper
# Incoming / outgoing messages
def incoming_message_url(incoming_message, options = {})
- return request_url(incoming_message.info_request, options.merge(:anchor => "incoming-#{incoming_message.id}"))
+ message_url(incoming_message, options)
end
def incoming_message_path(incoming_message)
- incoming_message_url(incoming_message, :only_path => true)
+ message_path(incoming_message)
end
def outgoing_message_url(outgoing_message, options = {})
- request_url(outgoing_message.info_request, options.merge(:anchor => "outgoing-#{outgoing_message.id}"))
+ message_url(outgoing_message, options)
end
def outgoing_message_path(outgoing_message)
- outgoing_message_url(outgoing_message, :only_path => true)
+ message_path(outgoing_message)
end
def comment_url(comment, options = {})
@@ -279,73 +279,30 @@ module LinkToHelper
end
end
- # 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
+ #I18n locale switcher
- # 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
+ def locale_switcher(locale, params)
+ params['locale'] = locale
+ return url_for(params)
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
+ private
- date_format = _("simple_date_format")
- date_format = :long if date_format == "simple_date_format"
- I18n.l(date, :format => date_format)
- end
+ # Private: Generate a request_url linking to the new correspondence
+ def message_url(message, options = {})
+ message_type = message.class.to_s.gsub('Message', '').downcase
- def simple_time(date)
- return date.strftime("%H:%M:%S").strip
- end
+ default_options = { :anchor => "#{ message_type }-#{ message.id }" }
- def year_from_date(date)
- return date.strftime("%Y").strip
- end
+ if options.delete(:cachebust)
+ default_options.merge!(:nocache => "#{ message_type }-#{ message.id }")
+ end
- #I18n locale switcher
+ request_url(message.info_request, options.merge(default_options))
+ end
- def locale_switcher(locale, params)
- params['locale'] = locale
- return url_for(params)
+ def message_path(message)
+ message_url(message, :only_path => true)
end
end
diff --git a/app/mailers/request_mailer.rb b/app/mailers/request_mailer.rb
index 1fd5b9ba7..dd6472c1d 100644
--- a/app/mailers/request_mailer.rb
+++ b/app/mailers/request_mailer.rb
@@ -71,7 +71,7 @@ class RequestMailer < ApplicationMailer
def new_response(info_request, incoming_message)
# Don't use login link here, just send actual URL. This is
# because people tend to forward these emails amongst themselves.
- @url = incoming_message_url(incoming_message)
+ @url = incoming_message_url(incoming_message, :cachebust => true)
@incoming_message, @info_request = incoming_message, info_request
headers('Return-Path' => blackhole_email,
diff --git a/app/views/track_mailer/event_digest.text.erb b/app/views/track_mailer/event_digest.text.erb
index b83c184f0..a154f430f 100644
--- a/app/views/track_mailer/event_digest.text.erb
+++ b/app/views/track_mailer/event_digest.text.erb
@@ -17,14 +17,14 @@
# e.g. Julian Burgess sent a request to Royal Mail Group (15 May 2008)
if event.event_type == 'response'
- url = incoming_message_url(event.incoming_message)
+ url = incoming_message_url(event.incoming_message, :cachebust => true)
main_text += _("{{public_body}} sent a response to {{user_name}}", :public_body => event.info_request.public_body.name, :user_name => event.info_request.user_name)
elsif event.event_type == 'followup_sent'
- url = outgoing_message_url(event.outgoing_message)
+ url = outgoing_message_url(event.outgoing_message, :cachebust => true)
main_text += _("{{user_name}} sent a follow up message to {{public_body}}", :user_name => event.info_request.user_name, :public_body => event.info_request.public_body.name)
elsif event.event_type == 'sent'
# this is unlikely to happen in real life, but happens in the test code
- url = outgoing_message_url(event.outgoing_message)
+ url = outgoing_message_url(event.outgoing_message, :cachebust => true)
main_text += _("{{user_name}} sent a request to {{public_body}}", :user_name => event.info_request.user_name, :public_body => event.info_request.public_body.name)
elsif event.event_type == 'comment'
url = comment_url(event.comment)
diff --git a/app/views/user/_user_listing_single.html.erb b/app/views/user/_user_listing_single.html.erb
index ed1b95718..3cb0d283f 100644
--- a/app/views/user/_user_listing_single.html.erb
+++ b/app/views/user/_user_listing_single.html.erb
@@ -18,7 +18,7 @@ end %>
<span class="bottomline">
<%= pluralize(display_user.info_requests.size, "request") %> <%= _('made.')%>
<%= pluralize(display_user.visible_comments.size, "annotation") %> <%= _('made.')%>
- <%= _('Joined in')%> <%= year_from_date(display_user.created_at) %>.
+ <%= _('Joined in')%> <%= display_user.created_at.year %>.
</span>
</div>
diff --git a/app/views/user/show.html.erb b/app/views/user/show.html.erb
index ce328b46f..7ae577565 100644
--- a/app/views/user/show.html.erb
+++ b/app/views/user/show.html.erb
@@ -64,7 +64,7 @@
<h1> <%= h(@display_user.name) + (@is_you ? _(" (you)") : "") %></h1>
<p class="subtitle">
- <%= _('Joined {{site_name}} in', :site_name=>site_name) %> <%= year_from_date(@display_user.created_at) %>
+ <%= _('Joined {{site_name}} in', :site_name=>site_name) %> <%= @display_user.created_at.year %>
<% if !@user.nil? && @user.admin_page_links? %>
(<%= link_to "admin", admin_user_show_path(@display_user) %>)
<% end %>
diff --git a/spec/helpers/date_time_helper_spec.rb b/spec/helpers/date_time_helper_spec.rb
new file mode 100644
index 000000000..c4fdee1d1
--- /dev/null
+++ b/spec/helpers/date_time_helper_spec.rb
@@ -0,0 +1,71 @@
+require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
+
+describe DateTimeHelper do
+
+ include DateTimeHelper
+
+ describe :simple_date do
+
+ it 'formats a date in html by default' do
+ time = Time.utc(2012, 11, 07, 21, 30, 26)
+ self.should_receive(:simple_date_html).with(time)
+ simple_date(time)
+ end
+
+ it 'formats a date in the specified format' do
+ time = Time.utc(2012, 11, 07, 21, 30, 26)
+ self.should_receive(:simple_date_text).with(time)
+ simple_date(time, :format => :text)
+ end
+
+ it 'raises an argument error if given an unrecognized format' do
+ time = Time.utc(2012, 11, 07, 21, 30, 26)
+ expect { simple_date(time, :format => :unknown) }.to raise_error(ArgumentError)
+ end
+
+ end
+
+ describe :simple_date_html do
+
+ it 'formats a date in a time tag' do
+ Time.use_zone('London') do
+ time = Time.utc(2012, 11, 07, 21, 30, 26)
+ expected = %Q(<time datetime="2012-11-07T21:30:26+00:00" title="2012-11-07 21:30:26 +0000">November 07, 2012</time>)
+ simple_date_html(time).should == expected
+ end
+ end
+
+ end
+
+ describe :simple_date_text do
+
+ it 'should respect time zones' do
+ Time.use_zone('Australia/Sydney') do
+ simple_date_text(Time.utc(2012, 11, 07, 21, 30, 26)).should == 'November 08, 2012'
+ end
+ end
+
+ it 'should handle Date objects' do
+ simple_date_text(Date.new(2012, 11, 21)).should == 'November 21, 2012'
+ end
+
+ end
+
+ describe :simple_time do
+
+ it 'returns 00:00:00 for a date' do
+ simple_time(Date.new(2012, 11, 21)).should == '00:00:00'
+ end
+
+ it 'returns the time component of a datetime' do
+ date = DateTime.new(2012, 11, 21, 10, 34, 56)
+ simple_time(date).should == '10:34:56'
+ end
+
+ it 'returns the time component of a time' do
+ time = Time.utc(2000, 'jan', 1, 20, 15, 1)
+ simple_time(time).should == '20:15:01'
+ end
+
+ end
+end
diff --git a/spec/helpers/link_to_helper_spec.rb b/spec/helpers/link_to_helper_spec.rb
index 4a01ec683..b11c35056 100644
--- a/spec/helpers/link_to_helper_spec.rb
+++ b/spec/helpers/link_to_helper_spec.rb
@@ -20,6 +20,82 @@ describe LinkToHelper do
end
+ describe 'when linking to new incoming messages' do
+
+ before do
+ @info_request = mock_model(InfoRequest, :id => 123, :url_title => 'test_title')
+ @incoming_message = mock_model(IncomingMessage, :id => 32, :info_request => @info_request)
+ end
+
+ context 'for external links' do
+
+ it 'generates the url to the info request of the message' do
+ incoming_message_url(@incoming_message).should include('http://test.host/request/test_title')
+ end
+
+ it 'includes an anchor to the new message' do
+ incoming_message_url(@incoming_message).should include('#incoming-32')
+ end
+
+ it 'includes does not cache by default' do
+ incoming_message_url(@incoming_message).should_not include('nocache=incoming-32')
+ end
+
+ it 'includes a cache busting parameter if set' do
+ incoming_message_url(@incoming_message, :cachebust => true).should include('nocache=incoming-32')
+ end
+
+ end
+
+ context 'for internal links' do
+
+ it 'generates the incoming_message_url with the path only' do
+ expected = '/request/test_title#incoming-32'
+ incoming_message_path(@incoming_message).should == expected
+ end
+
+ end
+
+ end
+
+ describe 'when linking to new outgoing messages' do
+
+ before do
+ @info_request = mock_model(InfoRequest, :id => 123, :url_title => 'test_title')
+ @outgoing_message = mock_model(OutgoingMessage, :id => 32, :info_request => @info_request)
+ end
+
+ context 'for external links' do
+
+ it 'generates the url to the info request of the message' do
+ outgoing_message_url(@outgoing_message).should include('http://test.host/request/test_title')
+ end
+
+ it 'includes an anchor to the new message' do
+ outgoing_message_url(@outgoing_message).should include('#outgoing-32')
+ end
+
+ it 'includes does not cache by default' do
+ outgoing_message_url(@outgoing_message).should_not include('nocache=outgoing-32')
+ end
+
+ it 'includes a cache busting parameter if set' do
+ outgoing_message_url(@outgoing_message, :cachebust => true).should include('nocache=outgoing-32')
+ end
+
+ end
+
+ context 'for internal links' do
+
+ it 'generates the outgoing_message_url with the path only' do
+ expected = '/request/test_title#outgoing-32'
+ outgoing_message_path(@outgoing_message).should == expected
+ end
+
+ end
+
+ end
+
describe 'when displaying a user link for a request' do
context "for external requests" do
@@ -69,51 +145,4 @@ describe LinkToHelper do
end
- describe 'simple_date' do
-
- it 'formats a date in html by default' do
- time = Time.utc(2012, 11, 07, 21, 30, 26)
- self.should_receive(:simple_date_html).with(time)
- simple_date(time)
- end
-
- it 'formats a date in the specified format' do
- time = Time.utc(2012, 11, 07, 21, 30, 26)
- self.should_receive(:simple_date_text).with(time)
- simple_date(time, :format => :text)
- end
-
- it 'raises an argument error if given an unrecognized format' do
- time = Time.utc(2012, 11, 07, 21, 30, 26)
- expect { simple_date(time, :format => :unknown) }.to raise_error(ArgumentError)
- end
-
- end
-
- describe 'simple_date_html' do
-
- it 'formats a date in a time tag' do
- Time.use_zone('London') do
- time = Time.utc(2012, 11, 07, 21, 30, 26)
- expected = "<time datetime=\"2012-11-07T21:30:26+00:00\" title=\"2012-11-07 21:30:26 +0000\">November 07, 2012</time>"
- simple_date_html(time).should == expected
- end
- end
-
- end
-
- describe 'simple_date_text' do
-
- it 'should respect time zones' do
- Time.use_zone('Australia/Sydney') do
- simple_date_text(Time.utc(2012, 11, 07, 21, 30, 26)).should == 'November 08, 2012'
- end
- end
-
- it 'should handle Date objects' do
- simple_date_text(Date.new(2012, 11, 21)).should == 'November 21, 2012'
- end
-
- end
-
end