cleaned up group controller. added custom as_json to group model.

This commit is contained in:
Daniel Grippi & Raphael Sofaer 2010-08-29 20:59:58 -07:00
parent b85333e52b
commit 5babda044d
2 changed files with 29 additions and 29 deletions

View file

@ -1,6 +1,9 @@
class GroupsController < ApplicationController class GroupsController < ApplicationController
before_filter :authenticate_user! before_filter :authenticate_user!
respond_to :html
respond_to :json, :only => :show
def index def index
@posts = current_user.visible_posts(:by_members_of => :all).paginate :page => params[:page], :order => 'created_at DESC' @posts = current_user.visible_posts(:by_members_of => :all).paginate :page => params[:page], :order => 'created_at DESC'
@group = :all @group = :all
@ -8,13 +11,7 @@ class GroupsController < ApplicationController
def create def create
@group = current_user.group(params[:group]) @group = current_user.group(params[:group])
respond_with @group
if @group.created_at
flash[:notice] = "Successfully created group."
redirect_to @group
else
render :action => 'new'
end
end end
def new def new
@ -22,36 +19,31 @@ class GroupsController < ApplicationController
end end
def destroy def destroy
@group = Group.first(:id => params[:id]) @group = Group.find_by_id(params[:id])
@group.destroy @group.destroy
flash[:notice] = "Successfully destroyed group." respond_with :location => groups_url
redirect_to groups_url
end end
def show def show
@group = Group.first(:id => params[:id]) @group = Group.find_by_id(params[:id])
@friends = @group.people @friends = @group.people
@posts = current_user.visible_posts( :by_members_of => @group ).paginate :order => 'created_at DESC' @posts = current_user.visible_posts( :by_members_of => @group ).paginate :order => 'created_at DESC'
respond_with @group
end end
def edit def edit
@groups = current_user.groups @groups = current_user.groups
@group = Group.first(:id => params[:id]) @group = Group.find_by_id(params[:id])
end end
def update def update
@group = Group.first(:id => params[:id]) @group = Group.find_by_id(params[:id])
if @group.update_attributes(params[:group]) @group.update_attributes(params[:group])
#flash[:notice] = "Successfully updated group." respond_with @group
redirect_to @group
else
render :action => 'edit'
end
end end
def move_friends def move_friends
pp params
params[:moves].each{ |move| params[:moves].each{ |move|
move = move[1] move = move[1]
unless current_user.move_friend(move) unless current_user.move_friend(move)
@ -63,8 +55,8 @@ class GroupsController < ApplicationController
flash[:notice] = "Groups edited successfully." flash[:notice] = "Groups edited successfully."
redirect_to Group.first, :action => "edit" redirect_to Group.first, :action => "edit"
end end
def move_friend def move_friend
unless current_user.move_friend( :friend_id => params[:friend_id], :from => params[:from], :to => params[:to][:to]) unless current_user.move_friend( :friend_id => params[:friend_id], :from => params[:from], :to => params[:to][:to])
flash[:error] = "didn't work #{params.inspect}" flash[:error] = "didn't work #{params.inspect}"

View file

@ -2,18 +2,18 @@ class Group
include MongoMapper::Document include MongoMapper::Document
key :name, String key :name, String
validates_presence_of :name key :person_ids, Array
key :person_ids, Array
key :request_ids, Array key :request_ids, Array
key :post_ids, Array key :post_ids, Array
many :people, :in => :person_ids, :class_name => 'Person' many :people, :in => :person_ids, :class_name => 'Person'
many :requests, :in => :request_ids, :class_name => 'Request' many :requests, :in => :request_ids, :class_name => 'Request'
many :posts, :in => :post_ids, :class_name => 'Post' many :posts, :in => :post_ids, :class_name => 'Post'
belongs_to :user, :class_name => 'User' belongs_to :user, :class_name => 'User'
validates_presence_of :name
timestamps! timestamps!
def to_s def to_s
@ -24,5 +24,13 @@ class Group
id = id.to_id id = id.to_id
posts.detect{|x| x.person.id == id } posts.detect{|x| x.person.id == id }
end end
def as_json(opts = {})
{
"name" => self.name,
"people" => self.people.each{|person| person.as_json},
"posts" => self.posts.each {|post| post.as_json },
}
end
end end