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

View file

@ -1,15 +1,20 @@
class CommentsController < ApplicationController
before_filter :authenticate_user!
respond_to :html
respond_to :json, :only => :show
def create
target = Post.first(:id => params[:comment][:post_id])
target = Post.find_by_id params[:comment][:post_id]
text = params[:comment][:text]
if current_user.comment text, :on => target
render :text => "Woo!"
else
render :text => "Boo!"
@comment = current_user.comment text, :on => target
respond_with @comment
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
def create
@group = current_user.group(params[:group])
@group = current_user.group params[:group]
respond_with @group
end
@ -19,13 +19,13 @@ class GroupsController < ApplicationController
end
def destroy
@group = Group.find_by_id(params[:id])
@group = Group.find_by_id params[:id]
@group.destroy
respond_with :location => groups_url
end
def show
@group = Group.find_by_id(params[:id])
@group = Group.find_by_id params[:id]
@friends = @group.people
@posts = current_user.visible_posts( :by_members_of => @group ).paginate :order => 'created_at DESC'
@ -34,7 +34,7 @@ class GroupsController < ApplicationController
def edit
@groups = current_user.groups
@group = Group.find_by_id(params[:id])
@group = Group.find_by_id params[:id]
end
def update

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -1,10 +1,16 @@
/ %h1.big_text
/ .back
/ = link_to '⇧ home', root_path
/ Friends
/ .button.right
/ = link_to 'Add Friend', requests_path
= @people.count.to_s + search_or_index
%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
@ -15,10 +21,22 @@
%td= person.real_name
%td= person.email
%td= person.url
%td= link_to 'Show', person
%td= link_to 'Destroy', person, :confirm => 'Are you sure?', :method => :delete
%p= link_to "Add a friend", requests_path
-if current_user.friends.include? person
#pagination
= will_paginate @people
- 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

@ -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
it "index should yield search results for substring of person name" do
pending "wait, what???"
Person.should_receive(:search)
get :index, :q => "Eu"
end