added custom as_json methods to models

This commit is contained in:
Daniel Vincent Grippi 2010-08-29 23:46:19 -07:00
parent de8450c7c5
commit 5e34506df6
6 changed files with 42 additions and 4 deletions

View file

@ -20,6 +20,8 @@ class PeopleController < ApplicationController
@latest_status_message = current_user.raw_visible_posts.find_all_by__type_and_person_id("StatusMessage", params[:id]).last
@post_count = @posts.count
respond_with @person
end
def destroy

View file

@ -2,7 +2,7 @@ class UsersController < ApplicationController
before_filter :authenticate_user!, :except => [:new, :create]
respond_to :html
respond_to :json, :only => [:index, :show]
respond_to :json, :only => :show
def show
@user = User.find_by_id params[:id]

View file

@ -27,9 +27,11 @@ class Group
def as_json(opts = {})
{
"name" => self.name,
"people" => self.people.each{|person| person.as_json},
"posts" => self.posts.each {|post| post.as_json },
:group => {
:name => self.name,
:people => self.people.each{|person| person.as_json},
:posts => self.posts.each {|post| post.as_json },
}
}
end
end

View file

@ -76,6 +76,18 @@ class Person
owner.nil?
end
def as_json(opts={})
{
:person => {
:id => self.id,
:name => self.real_name,
:email => self.email,
:url => self.url,
:exported_key => exported_key
}
}
end
protected
def clean_url
self.url ||= "http://localhost:3000/" if self.class == User

View file

@ -44,6 +44,15 @@ class Post
signable_accessors.collect{|accessor|
(self.send accessor.to_sym).to_s}.join ';'
end
def as_json(opts={})
{
:post => {
:id => self.id,
:person => self.person.as_json,
}
}
end
protected
def destroy_comments

View file

@ -281,6 +281,19 @@ class User
def all_group_ids
self.groups.all.collect{|x| x.id}
end
def as_json(opts={})
{
:user => {
:posts => self.raw_visible_posts.each{|post| post.as_json},
:friends => self.friends.each {|friend| friend.as_json},
:groups => self.groups.each {|group| group.as_json},
:pending_requests => self.pending_requests.each{|request| request.as_json},
}
}
end
protected
def self.generate_key