changeset 94:0c8a8b390145

Now with epic caching And kicking out builder because I can't figure out how to cache it ;_;
author nanaya <me@myconan.net>
date Thu, 28 Jul 2016 03:49:08 +0900
parents 5cc4aab05c55
children dfd7daac110e
files app/helpers/application_helper.rb app/views/tweets/_tweet.atom.erb app/views/tweets/show.atom.builder app/views/tweets/show.atom.erb config/initializers/monkeypatch_collection_cache_key_callable.rb
diffstat 5 files changed, 58 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/app/helpers/application_helper.rb	Thu Jul 28 01:54:54 2016 +0900
+++ b/app/helpers/application_helper.rb	Thu Jul 28 03:49:08 2016 +0900
@@ -8,4 +8,8 @@
       "#{text.first(limit)}..."
     end
   end
+
+  def atom_id(id)
+    "tag:#{request.host_with_port},2005:#{id}"
+  end
 end
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/app/views/tweets/_tweet.atom.erb	Thu Jul 28 03:49:08 2016 +0900
@@ -0,0 +1,13 @@
+<entry>
+  <id><%= atom_id "#{tweet.user.id}/#{tweet.id}" %></id>
+  <published><%= tweet.created_at %></published>
+  <updated><%= tweet.created_at %></updated>
+  <link rel="alternate" type="text/html" href="<%= tweet.uri %>"/>
+  <title><%= ellipsize tweet.text %></title>
+  <content type="html">
+    <%= render(:partial => "tweet", :formats => :html, :locals => { :tweet => tweet }).to_str %>
+  </content>
+  <author>
+    <name><%= tweet.user.screen_name %></name>
+  </author>
+</entry>
--- a/app/views/tweets/show.atom.builder	Thu Jul 28 01:54:54 2016 +0900
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,15 +0,0 @@
-atom_feed do |feed|
-  feed.title "#{@user.name} (@#{@user.screen_name})"
-  feed.icon @user.profile_image_url_https
-  feed.updated @tweets.first.try(:created_at) || Time.at(0)
-
-  @tweets.each do |tweet|
-    feed.entry tweet, :url => tweet.uri, :updated => tweet.created_at do |entry|
-      entry.title ellipsize(tweet.text)
-      entry.content render(:partial => "tweet", :formats => :html, :locals => { :tweet => tweet }), :type => "html"
-      entry.author do |author|
-        author.name tweet.user.screen_name
-      end
-    end
-  end
-end
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/app/views/tweets/show.atom.erb	Thu Jul 28 03:49:08 2016 +0900
@@ -0,0 +1,21 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
+  <id>
+    tag:<%= request.host_with_port %>,2005:<%= @user.id %>
+  </id>
+
+  <link rel="alternate" type="text/html" href="<%= request.protocol %><%= request.host_with_port %>" />
+  <link rel="self" type="application/atom+xml" href="<%= request.url %>" />
+
+  <title>
+    <%= @user.name %> (<%= @user.screen_name %>)
+  </title>
+  <icon>
+    <%= @user.profile_image_url_https %>
+  </icon>
+  <updated>
+    <%= @tweets.first.try(:created_at) || Time.at(0) %>
+  </updated>
+
+  <%= render(:partial => "tweet", :collection => @tweets, :cached => ->(f) { f.id }) %>
+</feed>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/config/initializers/monkeypatch_collection_cache_key_callable.rb	Thu Jul 28 03:49:08 2016 +0900
@@ -0,0 +1,20 @@
+# stolen from https://github.com/rails/rails/pull/25616
+
+if Gem::Version.new(Rails.version) >= Gem::Version.new("5.1.0")
+  raise "remove this monkey patch!"
+end
+
+module ActionView::CollectionCaching
+  private def collection_by_cache_keys
+    seed =
+      if @options[:cached].respond_to?(:call)
+        @options[:cached]
+      else
+        ->(i) { i }
+      end
+
+    @collection.each_with_object({}) do |item, hash|
+      hash[expanded_cache_key(seed.call(item))] = item
+    end
+  end
+end