From 175ff9d2b16120ce342c1dbdee24842d9a39ee87 Mon Sep 17 00:00:00 2001 From: joshua stein Date: Sun, 16 Dec 2012 20:00:41 -0600 Subject: [PATCH] add some custom as_json output to Story and User --- app/models/story.rb | 18 ++++++++++++++++-- app/models/user.rb | 18 ++++++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/app/models/story.rb b/app/models/story.rb index 32f4e37..8d40bef 100644 --- a/app/models/story.rb +++ b/app/models/story.rb @@ -107,6 +107,18 @@ class Story < ActiveRecord::Base end end + def as_json(options = {}) + h = super(:only => [ + :short_id, + :title, + :url, + :comments_url, + ]) + h[:submitter_user] = user + h[:description] = markeddown_description + h + end + def assign_short_id 10.times do |try| if try == 10 @@ -229,8 +241,6 @@ class Story < ActiveRecord::Base end def calculated_hotness - score = upvotes - downvotes - order = Math.log([ score.abs, 1 ].max, 10) if score > 0 sign = 1 @@ -245,6 +255,10 @@ class Story < ActiveRecord::Base return -(order + (sign * (self.created_at.to_f / window))).round(7) end + + def score + upvotes - downvotes + end def vote_summary r_counts = {} diff --git a/app/models/user.rb b/app/models/user.rb index 2a7ebf7..e18200a 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -30,6 +30,24 @@ class User < ActiveRecord::Base before_save :check_session_token after_create :create_default_tag_filters, :create_rss_token + def as_json(options = {}) + h = super(:only => [ + :id, + :username, + :is_admin, + :is_moderator, + ]) + h[:avatar_url] = avatar_url + h + end + + def avatar_url + "https://secure.gravatar.com/avatar/" << + Digest::MD5.hexdigest(self.email.strip.downcase) << "?r=pg&d=" << + CGI.escape(Rails.application.routes.url_helpers.root_url + + "images/1x1t.gif") << "&s=100" + end + def check_session_token if self.session_token.blank? self.session_token = Utils.random_str(60)