add some custom as_json output to Story and User

This commit is contained in:
joshua stein 2012-12-16 20:00:41 -06:00
parent 6b0a965c66
commit 175ff9d2b1
2 changed files with 34 additions and 2 deletions

View file

@ -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 = {}

View file

@ -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)