Skip to content

Commit

Permalink
Merge pull request #397 from alphagov/fix-social-meta-tags-caching
Browse files Browse the repository at this point in the history
Fix caching of social meta tags
  • Loading branch information
alanth committed Sep 23, 2015
2 parents 36382a8 + ead5493 commit 6e45634
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 16 deletions.
5 changes: 5 additions & 0 deletions app/helpers/cache_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class Keys
delegate :last_signature_at, to: :template
delegate :petition_page?, to: :template
delegate :page_title, to: :template
delegate :request, to: :template

def initialize(template)
@template = template
Expand Down Expand Up @@ -68,6 +69,10 @@ def site_updated_at
Site.updated_at
end

def url
request.original_url
end

def for(keys)
keys.map{ |key| [key, value_for(key)] }.uniq
end
Expand Down
21 changes: 10 additions & 11 deletions app/views/application/_social_meta.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,31 @@
<%= open_graph_tag 'site_name', :site_name %>
<%= open_graph_tag 'locale', 'en_GB' %>
<%= open_graph_tag 'image', 'os-social/opengraph-image.png' %>
<% if petition_page? %>
<%= open_graph_tag 'url', petition_url(@petition) %>
<%= open_graph_tag 'type', 'article' %>
<%= open_graph_tag 'title', :title, petition: @petition.action %>
<%= open_graph_tag 'description', @petition.background %>
<% elsif archived_petition_page? %>
<% if archived_petition_page? %>
<%= open_graph_tag 'url', archived_petition_url(@petition) %>
<%= open_graph_tag 'type', 'article' %>
<%= open_graph_tag 'title', :archived_title, petition: @petition.title %>
<%= open_graph_tag 'description', @petition.description %>
<% elsif defined?(@petition) %>
<%= open_graph_tag 'url', petition_url(@petition) %>
<%= open_graph_tag 'type', 'article' %>
<%= open_graph_tag 'title', :title, petition: @petition.action %>
<%= open_graph_tag 'description', @petition.background %>
<% else %>
<%= open_graph_tag 'url', request.original_url %>
<%= open_graph_tag 'type', 'website' %>
<%= open_graph_tag 'title', page_title %>
<% end %>

<!-- Twitter -->
<%= twitter_card_tag 'card', 'summary' %>
<%= twitter_card_tag 'site', '@hocpetitions' %>
<%= twitter_card_tag 'image', 'os-social/opengraph-image.png' %>
<% if petition_page? %>
<%= twitter_card_tag 'title', :title, petition: @petition.action %>
<%= twitter_card_tag 'description', @petition.background %>
<% elsif archived_petition_page? %>
<% if archived_petition_page? %>
<%= twitter_card_tag 'title', :title, petition: @petition.title %>
<%= twitter_card_tag 'description', @petition.description %>
<% elsif defined?(@petition) %>
<%= twitter_card_tag 'title', :title, petition: @petition.action %>
<%= twitter_card_tag 'description', @petition.background %>
<% else %>
<%= twitter_card_tag 'title', page_title %>
<% end %>
6 changes: 1 addition & 5 deletions config/fragments.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
head:
keys:
- :site_updated_at
- :last_signature_at
- :petition_page
- :archived_petition_page
- :page_title
- :petition
- :url
options:
expires_in: 300

Expand Down
9 changes: 9 additions & 0 deletions spec/helpers/cache_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,15 @@
end
end

describe "#url" do
let(:request) { double(:request, original_url: "/petitions/123") }

it "delegates to the request's original_url method" do
expect(helper).to receive(:request).and_return(request)
expect(keys.url).to eq("/petitions/123")
end
end

describe "#method_missing" do
it "returns an assigned variable in the template context" do
assign('signature_count', 32)
Expand Down

0 comments on commit 6e45634

Please sign in to comment.