added custom as_json methods to models
This commit is contained in:
parent
de8450c7c5
commit
5e34506df6
6 changed files with 42 additions and 4 deletions
|
|
@ -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
|
@latest_status_message = current_user.raw_visible_posts.find_all_by__type_and_person_id("StatusMessage", params[:id]).last
|
||||||
@post_count = @posts.count
|
@post_count = @posts.count
|
||||||
|
|
||||||
|
respond_with @person
|
||||||
end
|
end
|
||||||
|
|
||||||
def destroy
|
def destroy
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@ class UsersController < ApplicationController
|
||||||
before_filter :authenticate_user!, :except => [:new, :create]
|
before_filter :authenticate_user!, :except => [:new, :create]
|
||||||
|
|
||||||
respond_to :html
|
respond_to :html
|
||||||
respond_to :json, :only => [:index, :show]
|
respond_to :json, :only => :show
|
||||||
|
|
||||||
def show
|
def show
|
||||||
@user = User.find_by_id params[:id]
|
@user = User.find_by_id params[:id]
|
||||||
|
|
|
||||||
|
|
@ -27,9 +27,11 @@ class Group
|
||||||
|
|
||||||
def as_json(opts = {})
|
def as_json(opts = {})
|
||||||
{
|
{
|
||||||
"name" => self.name,
|
:group => {
|
||||||
"people" => self.people.each{|person| person.as_json},
|
:name => self.name,
|
||||||
"posts" => self.posts.each {|post| post.as_json },
|
:people => self.people.each{|person| person.as_json},
|
||||||
|
:posts => self.posts.each {|post| post.as_json },
|
||||||
|
}
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -76,6 +76,18 @@ class Person
|
||||||
owner.nil?
|
owner.nil?
|
||||||
end
|
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
|
protected
|
||||||
def clean_url
|
def clean_url
|
||||||
self.url ||= "http://localhost:3000/" if self.class == User
|
self.url ||= "http://localhost:3000/" if self.class == User
|
||||||
|
|
|
||||||
|
|
@ -45,6 +45,15 @@ class Post
|
||||||
(self.send accessor.to_sym).to_s}.join ';'
|
(self.send accessor.to_sym).to_s}.join ';'
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def as_json(opts={})
|
||||||
|
{
|
||||||
|
:post => {
|
||||||
|
:id => self.id,
|
||||||
|
:person => self.person.as_json,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
protected
|
protected
|
||||||
def destroy_comments
|
def destroy_comments
|
||||||
comments.each{|c| c.destroy}
|
comments.each{|c| c.destroy}
|
||||||
|
|
|
||||||
|
|
@ -281,6 +281,19 @@ class User
|
||||||
def all_group_ids
|
def all_group_ids
|
||||||
self.groups.all.collect{|x| x.id}
|
self.groups.all.collect{|x| x.id}
|
||||||
end
|
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
|
protected
|
||||||
|
|
||||||
def self.generate_key
|
def self.generate_key
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue