controller cleanup: using respond_to and respond_with.

This commit is contained in:
Daniel Grippi & Raphael Sofaer 2010-08-29 22:49:13 -07:00
parent 5babda044d
commit 7984756aa7
13 changed files with 139 additions and 178 deletions

View file

@ -1,20 +1,17 @@
class AlbumsController < ApplicationController class AlbumsController < ApplicationController
before_filter :authenticate_user! before_filter :authenticate_user!
respond_to :html
respond_to :json, :only => [:index, :show]
def index def index
@albums = Album.mine_or_friends(params[:friends], current_user).paginate :page => params[:page], :order => 'created_at DESC' @albums = Album.mine_or_friends(params[:friends], current_user).paginate :page => params[:page], :order => 'created_at DESC'
respond_with @albums
end end
def create def create
@album = current_user.post(:album, params[:album]) @album = current_user.post(:album, params[:album])
respond_with @album
if @album.created_at
flash[:notice] = "Successfully created album."
redirect_to @album
else
flash[:error] = "Successfully failed."
redirect_to albums_path
end
end end
def new def new
@ -22,30 +19,26 @@ class AlbumsController < ApplicationController
end end
def destroy def destroy
@album = Album.first(:id => params[:id]) @album = Album.find_by_id params[:id]
@album.destroy @album.destroy
flash[:notice] = "Successfully destroyed album." respond_with :location => albums_url
redirect_to albums_url
end end
def show def show
@photo = Photo.new @photo = Photo.new
@album = Album.first(:id => params[:id]) @album = Album.find_by_id params[:id]
@album_photos = @album.photos @album_photos = @album.photos
respond_with @album
end end
def edit def edit
@album = Album.first(:id => params[:id]) @album = Album.find_by_id params[:id]
end end
def update def update
@album = Album.first(:id => params[:id]) @album = Album.find_params_by_id params[:id]
if @album.update_attributes(params[:album]) respond_with @album
flash[:notice] = "Successfully updated album."
redirect_to @album
else
render :action => 'edit'
end
end end
end end

View file

@ -1,15 +1,20 @@
class CommentsController < ApplicationController class CommentsController < ApplicationController
before_filter :authenticate_user! before_filter :authenticate_user!
respond_to :html
respond_to :json, :only => :show
def create def create
target = Post.first(:id => params[:comment][:post_id]) target = Post.find_by_id params[:comment][:post_id]
text = params[:comment][:text] text = params[:comment][:text]
if current_user.comment text, :on => target @comment = current_user.comment text, :on => target
render :text => "Woo!" respond_with @comment
else
render :text => "Boo!"
end
end end
end def show
@comment = Comment.find_by_id params[:id]
respond_with @comment
end
end

View file

@ -10,7 +10,7 @@ class GroupsController < ApplicationController
end end
def create def create
@group = current_user.group(params[:group]) @group = current_user.group params[:group]
respond_with @group respond_with @group
end end
@ -19,13 +19,13 @@ class GroupsController < ApplicationController
end end
def destroy def destroy
@group = Group.find_by_id(params[:id]) @group = Group.find_by_id params[:id]
@group.destroy @group.destroy
respond_with :location => groups_url respond_with :location => groups_url
end end
def show def show
@group = Group.find_by_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'
@ -34,7 +34,7 @@ class GroupsController < ApplicationController
def edit def edit
@groups = current_user.groups @groups = current_user.groups
@group = Group.find_by_id(params[:id]) @group = Group.find_by_id params[:id]
end end
def update def update

View file

@ -1,14 +1,12 @@
class PeopleController < ApplicationController class PeopleController < ApplicationController
before_filter :authenticate_user! before_filter :authenticate_user!
respond_to :html
respond_to :json, :only => [:index, :show]
def index def index
unless params[:q] @people = Person.search params[:q]
@people = current_user.friends.paginate :page => params[:page], :order => 'created_at DESC' respond_with @people
render :index
else
@people = Person.search(params[:q])
render :json => @people.to_json(:only => :_id)
end
end end
def show def show
@ -26,8 +24,7 @@ class PeopleController < ApplicationController
def destroy def destroy
current_user.unfriend(current_user.visible_person_by_id(params[:id])) current_user.unfriend(current_user.visible_person_by_id(params[:id]))
flash[:notice] = "unfriended person." respond_with :location => people_url
redirect_to people_url
end end
end end

View file

@ -1,20 +1,28 @@
class PhotosController < ApplicationController class PhotosController < ApplicationController
before_filter :authenticate_user! before_filter :authenticate_user!
respond_to :html
respond_to :json, :only => :show
def create def create
album = Album.find_by_id params[:album_id]
begin begin
@photo = current_user.post(:photo, params) @photo = current_user.post(:photo, params)
render :nothing => true if @photo.created_at respond_with @photo
rescue TypeError rescue TypeError
flash[:error] = "Photo upload failed. Are you sure an image was added?" message = "Photo upload failed. Are you sure an image was added?"
redirect_to Album.first(:id => params[:album_id]) respond_with :location => album, :error => message
rescue CarrierWave::IntegrityError rescue CarrierWave::IntegrityError
flash[:error] = "Photo upload failed. Are you sure that was an image?" message = "Photo upload failed. Are you sure that was an image?"
redirect_to Album.first(:id => params[:album_id]) respond_with :location => album, :error => message
rescue RuntimeError => e rescue RuntimeError => e
flash[:error] = "Photo upload failed. Are you sure that your seatbelt is fastened?" message = "Photo upload failed. Are you sure that your seatbelt is fastened?"
redirect_to Album.first(:id => params[:album_id]) respond_with :location => album, :error => message
raise e raise e
end end
end end
@ -26,29 +34,27 @@ class PhotosController < ApplicationController
end end
def destroy def destroy
@photo = Photo.first(:id => params[:id]) @photo = Photo.find_by_id params[:id]
@photo.destroy @photo.destroy
flash[:notice] = "Successfully deleted photo." respond_with :location => @photo.album
redirect_to @photo.album
end end
def show def show
@photo = Photo.first(:id => params[:id]) @photo = Photo.find_by_id params[:id]
@album = @photo.album @album = @photo.album
respond_with @photo, @album
end end
def edit def edit
@photo= Photo.first(:id => params[:id]) @photo = Photo.find_by_id params[:id]
@album = @photo.album @album = @photo.album
end end
def update def update
@photo= Photo.first(:id => params[:id]) @photo = Photo.find_by_id params[:id]
if @photo.update_attributes(params[:photo]) @photo.update_attributes params[:photo]
flash[:notice] = "Successfully updated photo."
redirect_to @photo respond_with @photo
else
render :action => 'edit'
end
end end
end end

View file

@ -3,7 +3,7 @@ class PublicsController < ApplicationController
include Diaspora::Parser include Diaspora::Parser
def hcard def hcard
@person = Person.first(:_id => params[:id]) @person = Person.find_by_id params[:id]
unless @person.nil? || @person.owner.nil? unless @person.nil? || @person.owner.nil?
render 'hcard' render 'hcard'

View file

@ -1,29 +1,31 @@
class RequestsController < ApplicationController class RequestsController < ApplicationController
before_filter :authenticate_user! before_filter :authenticate_user!
include RequestsHelper include RequestsHelper
respond_to :html
respond_to :json, :only => :index
def index def index
@remote_requests = Request.for_user( current_user ) @remote_requests = Request.for_user current_user
@request = Request.new @request = Request.new
respond_with @remote_requests
end end
def destroy def destroy
if params[:accept] if params[:accept]
if params[:group_id] if params[:group_id]
@friend = current_user.accept_and_respond( params[:id], params[:group_id]) @friend = current_user.accept_and_respond( params[:id], params[:group_id])
flash[:notice] = "you are now friends" flash[:notice] = "you are now friends"
redirect_to current_user.group_by_id(params[:group_id]) respond_with :location => current_user.group_by_id(params[:group_id])
else else
flash[:error] = "please select a group!" flash[:error] = "please select a group!"
redirect_to requests_url respond_with :location => requests_url
end end
else else
current_user.ignore_friend_request params[:id] current_user.ignore_friend_request params[:id]
flash[:notice] = "ignored friend request" respond_with :location => requests_url, :notice => "Ignored friend request."
redirect_to requests_url
end end
end end
def new def new
@ -31,11 +33,12 @@ class RequestsController < ApplicationController
end end
def create def create
group = current_user.group_by_id(params[:request][:group_id])
begin begin
rel_hash = relationship_flow(params[:request][:destination_url]) rel_hash = relationship_flow(params[:request][:destination_url])
rescue Exception => e rescue Exception => e
flash[:error] = "no diaspora seed found with this email!" respond_with :location => group, :error => "No diaspora seed found with this email!"
redirect_to current_user.group_by_id(params[:request][:group_id])
return return
end end
@ -45,17 +48,17 @@ class RequestsController < ApplicationController
@request = current_user.send_request(rel_hash, params[:request][:group_id]) @request = current_user.send_request(rel_hash, params[:request][:group_id])
rescue Exception => e rescue Exception => e
raise e unless e.message.include? "already friends" raise e unless e.message.include? "already friends"
flash[:notice] = "You are already friends with #{params[:request][:destination_url]}!" message = "You are already friends with #{params[:request][:destination_url]}!"
redirect_to current_user.group_by_id(params[:request][:group_id]) respond_with :location => group, :notice => message
return return
end end
if @request if @request
flash[:notice] = "a friend request was sent to #{@request.destination_url}" message = "A friend request was sent to #{@request.destination_url}."
redirect_to current_user.group_by_id(params[:request][:group_id]) respond_with :location => group, :notice => message
else else
flash[:error] = "Something went horribly wrong..." message = "Something went horribly wrong."
redirect_to current_user.group_by_id(params[:request][:group_id]) respond_with :location => group, :error => message
end end
end end

View file

@ -1,31 +1,23 @@
class StatusMessagesController < ApplicationController class StatusMessagesController < ApplicationController
before_filter :authenticate_user! before_filter :authenticate_user!
respond_to :html
respond_to :json, :only => :show
def create def create
params[:status_message][:to] = params[:group_ids] params[:status_message][:to] = params[:group_ids]
@status_message = current_user.post(:status_message, params[:status_message]) @status_message = current_user.post(:status_message, params[:status_message])
respond_with @status_message
if @status_message.created_at
render :nothing => true
else
redirect_to root_url
end
end end
def destroy def destroy
@status_message = StatusMessage.where(:id => params[:id]).first @status_message = StatusMessage.find_by_id params[:id]
@status_message.destroy @status_message.destroy
flash[:notice] = "Successfully destroyed status message." respond_with :location => root_url
redirect_to root_url
end end
def show def show
@status_message = StatusMessage.where(:id => params[:id]).first @status_message = StatusMessage.find_by_id params[:id]
respond_with @status_message
respond_to do |format|
format.html
format.xml { render :xml => @status_message.build_xml_for }
format.json { render :json => @status_message }
end
end end
end end

View file

@ -1,37 +1,26 @@
class UsersController < ApplicationController class UsersController < ApplicationController
before_filter :authenticate_user!, :except => [:new, :create] before_filter :authenticate_user!, :except => [:new, :create]
def index
@groups_array = current_user.groups.collect{|x| [x.to_s, x.id]}
unless params[:q] respond_to :html
@people = Person.all respond_to :json, :only => [:index, :show]
render :index
else
@people = Person.search(params[:q])
end
end
def show def show
@user= User.first(:id => params[:id]) @user = User.find_by_id params[:id]
@user_profile = @user.person.profile @user_profile = @user.person.profile
respond_with @user
end end
def edit def edit
@user = current_user @user = current_user
@person = @user.person @person = @user.person
@profile = @user.profile @profile = @user.profile
@photos = Photo.where(:person_id => @person.id).paginate :page => params[:page], :order => 'created_at DESC' @photos = Photo.find_all_by_person_id(@person.id).paginate :page => params[:page], :order => 'created_at DESC'
end end
def update def update
@user = User.where(:id => params[:id]).first @user = User.find_by_id params[:id]
@user.update_profile params[:user]
if @user.update_profile(params[:user]) respond_with @user
flash[:notice] = "Successfully updated your profile"
redirect_to @user.person
else
render :action => 'edit'
end
end end
end end

View file

@ -39,7 +39,7 @@
#session_action #session_action
#global_search #global_search
= form_tag(users_path, :method => 'get') do = form_tag(people_path, :method => 'get') do
%label{:for => 'q'} Search %label{:for => 'q'} Search
= text_field_tag 'q' = text_field_tag 'q'

View file

@ -1,24 +1,42 @@
/ %h1.big_text %h1.big_text
/ .back .back
/ = link_to '⇧ home', root_path = link_to "⇧ home", root_path
/ Friends
/ .button.right Search
/ = link_to 'Add Friend', requests_path
= @people.count.to_s + search_or_index %p
=form_tag '/users', :method => "get" do
= text_field_tag :q
= submit_tag "search"
= link_to "reset", users_path
= (@people.count).to_s + search_or_index
%table %table
%tr %tr
%th real name %th real name
%th email %th email
%th url %th url
- for person in @people - for person in @people
%tr %tr
%td= person.real_name %td= person.real_name
%td= person.email %td= person.email
%td= person.url %td= person.url
%td= link_to 'Show', person
%td= link_to 'Destroy', person, :confirm => 'Are you sure?', :method => :delete -if current_user.friends.include? person
%p= link_to "Add a friend", requests_path - elsif person.id == current_user.person.id
%td
#pagination %td thats you!
= will_paginate @people -elsif current_user.pending_requests.find_by_person_id(person.id)
%td
%td ^-you have a friend request from this person
-elsif current_user.pending_requests.find_by_url(person.receive_url)
%td
%td friend request pending
-else
%td
%td
= form_for Request.new do |f|
= f.select(:group_id, @groups_array)
= f.hidden_field :destination_url, :value => person.email
= f.submit "add friend"

View file

@ -1,42 +0,0 @@
%h1.big_text
.back
= link_to "⇧ home", root_path
Search
%p
=form_tag '/users', :method => "get" do
= text_field_tag :q
= submit_tag "search"
= link_to "reset", users_path
= (@people.count).to_s + search_or_index
%table
%tr
%th real name
%th email
%th url
- for person in @people
%tr
%td= person.real_name
%td= person.email
%td= person.url
-if current_user.friends.include? person
- elsif person.id == current_user.person.id
%td
%td thats you!
-elsif current_user.pending_requests.find_by_person_id(person.id)
%td
%td ^-you have a friend request from this person
-elsif current_user.pending_requests.find_by_url(person.receive_url)
%td
%td friend request pending
-else
%td
%td
= form_for Request.new do |f|
= f.select(:group_id, @groups_array)
= f.hidden_field :destination_url, :value => person.email
= f.submit "add friend"

View file

@ -10,8 +10,8 @@ describe PeopleController do
end end
it "index should yield search results for substring of person name" do it "index should yield search results for substring of person name" do
pending "wait, what???"
Person.should_receive(:search) Person.should_receive(:search)
get :index, :q => "Eu" get :index, :q => "Eu"
end end