aboutsummaryrefslogtreecommitdiffstats
path: root/app
diff options
context:
space:
mode:
authorRhenium <rhenium@rhe.jp>2014-02-11 00:50:49 +0900
committerRhenium <rhenium@rhe.jp>2014-02-11 00:50:49 +0900
commit33689cbce02d01aa59aab7d2aaba9e7127aacd9f (patch)
treef9642a7f081d4360b38e0d5ce842ee3c1c6d3670 /app
parent051a858e2cad2f09f70d0b221f508fc4f30db964 (diff)
downloadaclog-33689cbce02d01aa59aab7d2aaba9e7127aacd9f.tar.gz
show reply tree on tweets/show (single tweet page)
Diffstat (limited to 'app')
-rw-r--r--app/controllers/tweets_controller.rb4
-rw-r--r--app/models/tweet.rb24
-rw-r--r--app/views/tweets/show.html.haml4
3 files changed, 31 insertions, 1 deletions
diff --git a/app/controllers/tweets_controller.rb b/app/controllers/tweets_controller.rb
index 078830b..4a2590f 100644
--- a/app/controllers/tweets_controller.rb
+++ b/app/controllers/tweets_controller.rb
@@ -13,6 +13,8 @@ class TweetsController < ApplicationController
@tweet = Tweet.find(params[:id])
@user = @tweet.user
authorize_to_show_user! @user
+ @replies_before = @tweet.reply_ancestors(2)
+ @replies_after = @tweet.reply_descendants(2)
end
def best
@@ -58,7 +60,7 @@ class TweetsController < ApplicationController
authorize_to_show_user! @user
@source_user = User.find(id: params[:user_id_b], screen_name: params[:screen_name_b])
authorize_to_show_user! @source_user
- @tweets = paginate(@user.tweets.discovered_by(@source_user).order_by_id)
+ @tweets = paginate(@user.tweets).discovered_by(@source_user).order_by_id
end
def all_best
diff --git a/app/models/tweet.rb b/app/models/tweet.rb
index c9c9c14..a89288c 100644
--- a/app/models/tweet.rb
+++ b/app/models/tweet.rb
@@ -42,6 +42,30 @@ class Tweet < ActiveRecord::Base
end
end
+ def reply_ancestors(max_level = Float::INFINITY)
+ nodes = []
+ node = self
+ level = 0
+
+ while node.in_reply_to && level < max_level
+ nodes.unshift(node = node.in_reply_to)
+ level += 1
+ end
+ nodes
+ end
+
+ def reply_descendants(max_level = Float::INFINITY)
+ nodes = []
+ c_nodes = [self]
+ level = 0
+
+ while c_nodes.size > 0 && level < max_level
+ nodes.concat(c_nodes.map! {|node| node.replies }.flatten!)
+ level += 1
+ end
+ nodes.sort_by {|t| t.id }
+ end
+
def self.from_json(json)
find_by(id: json[:id]) || begin
user = User.from_json(json[:user])
diff --git a/app/views/tweets/show.html.haml b/app/views/tweets/show.html.haml
index b1184cb..c41ebf6 100644
--- a/app/views/tweets/show.html.haml
+++ b/app/views/tweets/show.html.haml
@@ -1,7 +1,11 @@
- title CGI.unescapeHTML(strip_tags(format_tweet_text(@tweet.text))) + " from @" + @user.screen_name
- caption @user.screen_name + "'s Tweet"
- sidebar :users
+= render partial: "tweet_without_stats", collection: @replies_before, as: :tweet
+%hr
= render @tweet
+%hr
+= render partial: "tweet_without_stats", collection: @replies_after, as: :tweet
- if logged_in?
.pull-right
%span.import= link_to "update", import_path(@tweet.id)