Skip to content

Commit

Permalink
general refactoring and cleanup, adding a few tests for avatars along…
Browse files Browse the repository at this point in the history
… the way
  • Loading branch information
Trevor Turk committed May 21, 2008
1 parent d8a556b commit 5d1ecbb
Show file tree
Hide file tree
Showing 25 changed files with 98 additions and 86 deletions.
7 changes: 2 additions & 5 deletions app/controllers/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ class ApplicationController < ActionController::Base
rescue_from ActionController::InvalidAuthenticityToken, :with => :generic_error
rescue_from ActionView::MissingTemplate, :with => :not_found
rescue_from WillPaginate::InvalidPage, :with => :invalid_page
rescue_from Errno::ETIMEDOUT, :with => :generic_eror
rescue_from Timeout::Error, :with => :generic_eror

def redirect_home
redirect_to root_path and return false
Expand All @@ -40,7 +38,6 @@ def get_layout_vars
def update_online_at
return unless logged_in?
session[:online_at] = current_user.online_at.utc if current_user.online_at.utc + 10.minutes < Time.now.utc
User.update_all ['online_at = ?', Time.now.utc], ['id = ?', current_user.id]
current_user.update_attribute(:online_at, Time.now.utc)
end

end
end
13 changes: 5 additions & 8 deletions app/controllers/avatars_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,26 @@ class AvatarsController < ApplicationController

before_filter :redirect_home, :only => [:show, :edit, :update]
before_filter :require_login, :except => [:index]
before_filter :can_edit, :only => [:destroy]

def index
@avatars = Avatar.paginate(:page => params[:page], :order => 'updated_at desc')
end

def new
end

def create
@avatar = current_user.avatars.build params[:avatar]
@avatar = current_user.avatars.build(params[:avatar])
if @avatar.save
redirect_to avatars_path
else
render :action => "new"
end
end

def destroy
@avatar = Avatar.find(params[:id])
redirect_to root_path and return false unless admin? || (current_user == @avatar.user)
@user = User.find_by_id(@avatar.current_user_id)
@user.update_attributes(:avatar => nil) if @user
@avatar.destroy
redirect_to avatars_url
end
Expand All @@ -46,5 +44,4 @@ def deselect
current_user.update_attributes(:avatar => nil)
redirect_to current_user
end

end
10 changes: 6 additions & 4 deletions app/controllers/categories_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,19 @@ def show
@category = Category.find(params[:id], :include => :forums)
@forums = @category.forums
@topics = Topic.paginate(:page => params[:page], :include => [:user, :forum, :last_poster], :order => 'topics.last_post_at desc', :conditions => ["forum_id in (?)", @forums.collect(&:id)])
render(:template => "topics/index")
render :template => 'topics/index'
end

def new
end

def create
@category = Category.new(params[:category])
render :action => 'new' and return false unless @category.save
redirect_to forum_root_path
if @category.save
redirect_to forum_root_path
else
render :action => 'new'
end
end

def edit
Expand Down Expand Up @@ -49,5 +52,4 @@ def destroy
def confirm_delete
@category = Category.find(params[:id])
end

end
8 changes: 5 additions & 3 deletions app/controllers/events_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@ def new

def create
@event = current_user.events.build(params[:event])
redirect_to @event and return true if @event.save
render :action => "new"
if @event.save
redirect_to @event and return true
else
render :action => "new"
end
end

def edit
Expand All @@ -39,5 +42,4 @@ def destroy
@event.destroy
redirect_to events_url
end

end
10 changes: 6 additions & 4 deletions app/controllers/forums_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def index
def show
@forum = Forum.find(params[:id], :include => :category)
@topics = @forum.topics.paginate(:page => params[:page], :include => [:user, :last_poster], :order => 'topics.sticky desc, topics.last_post_at desc')
render(:template => "topics/index")
render :template => 'topics/index'
end

def new
Expand All @@ -21,8 +21,11 @@ def new

def create
@forum = Forum.new(params[:forum])
render :action => 'new' and return false unless @forum.save
redirect_to @forum
if @forum.save
redirect_to @forum
else
render :action => 'new'
end
end

def edit
Expand Down Expand Up @@ -52,5 +55,4 @@ def destroy
def confirm_delete
@forum = Forum.find(params[:id])
end

end
7 changes: 3 additions & 4 deletions app/controllers/headers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def create
if @header.save
redirect_to @header
else
render :action => "new"
render :action => 'new'
end
end

Expand All @@ -32,7 +32,7 @@ def update
if @header.update_attributes(params[:header])
redirect_to @header
else
render :action => "edit"
render :action => 'edit'
end
end

Expand All @@ -52,6 +52,5 @@ def vote_down
@header = Header.find(params[:id])
@header.vote_down
render :partial => 'votes.html.erb'
end

end
end
1 change: 0 additions & 1 deletion app/controllers/home_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,4 @@ def index
@uploads = Upload.find(:all, :limit => 5, :include => :user, :order => 'uploads.updated_at desc')
@users = User.find(:all, :limit => 3, :order => 'profile_updated_at desc')
end

end
11 changes: 5 additions & 6 deletions app/controllers/posts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def create
redirect_to root_path and return false unless @topic
@post = current_user.posts.build(params[:post])
if @topic.locked
redirect_to root_path and return false unless admin? || (current_user == @topic.user)
redirect_to root_path and return false unless admin? || (current_user == @topic.user) # TODO can this be a conditional before_filter?
end
@topic.posts_count += 1 # hack to set last_page correctly
if (@topic.posts << @post)
Expand All @@ -40,7 +40,7 @@ def update
if @post.update_attributes(params[:post])
redirect_to topic_post_path(@post)
else
render :action => :edit
render :action => 'edit'
end
end

Expand All @@ -56,13 +56,12 @@ def topic
def quote
@body = "[quote=#{@post.user.login}]#{@post.body}[/quote]"
@post = nil # clear post so form with create a new one
render :template => "posts/new"
render :template => 'posts/new'
end

def find_topic_and_post
@post = Post.find(params[:id])
@topic = Topic.find(@post.topic.id)
redirect_to topics_url unless @topic
end

end
end
end
8 changes: 5 additions & 3 deletions app/controllers/ranks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@ def new

def create
@rank = Rank.new(params[:rank])
render :action => 'new' and return false unless @rank.save
redirect_to ranks_path
if @rank.save
redirect_to ranks_path
else
render :action => 'new'
end
end

def edit
Expand All @@ -34,5 +37,4 @@ def destroy
@rank.destroy
redirect_to ranks_path
end

end
3 changes: 1 addition & 2 deletions app/controllers/search_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,5 @@ def index
@users = User.paginate(:page => params[:page], :order => 'created_at desc', :conditions => ['login LIKE ?', '%' + params[:query] + '%'])
render :template => 'users/index'
end
end

end
end
1 change: 0 additions & 1 deletion app/controllers/settings_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,4 @@ def update
@setting.update_attributes(params[:setting])
redirect_to settings_path
end

end
8 changes: 3 additions & 5 deletions app/controllers/themes_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class ThemesController < ApplicationController

before_filter :redirect_home, :only => [:show, :edit, :update]
before_filter :require_admin
before_filter :require_admin

def index
@themes = Theme.paginate(:page => params[:page])
Expand All @@ -12,17 +12,16 @@ def new
end

def create
@theme = current_user.themes.build params[:theme]
@theme = current_user.themes.build(params[:theme])
if @theme.save
redirect_to themes_path
else
render :action => "new"
render :action => 'new'
end
end

def destroy
@theme = Theme.find(params[:id])
redirect_to themes_path and return false unless can_edit?(@theme)
@settings.update_attributes(:theme => nil) if @settings.theme == @theme.filename
@theme.destroy
redirect_to themes_path
Expand All @@ -38,5 +37,4 @@ def deselect
@settings.update_attributes(:theme => nil)
redirect_to themes_path
end

end
12 changes: 7 additions & 5 deletions app/controllers/topics_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def show
@posts = @topic.posts.paginate(:page => params[:page], :include => :user)
redirect_to @topic if @posts.blank? # if params[:page] is too big, no posts will be found
@page = params[:page] ? params[:page] : 1
@padding = ((@page.to_i - 1) * Topic::PER_PAGE) # to get post #s w/ pagination
@padding = ((@page.to_i - 1) * 30) # to get post #s w/ pagination
@topic.hit!
end

Expand All @@ -23,8 +23,11 @@ def new
def create
@topic = current_user.topics.build(params[:topic])
@post = @topic.posts.build(params[:topic]) ; @post.user = current_user
redirect_to @topic and return true if @topic.save && @post.save
render :action => "new"
if @topic.save && @post.save
redirect_to @topic
else
render :action => 'new'
end
end

def edit
Expand Down Expand Up @@ -71,5 +74,4 @@ def unknown_request
end
redirect_to root_path
end

end
end
7 changes: 4 additions & 3 deletions app/controllers/uploads_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ class UploadsController < ApplicationController
before_filter :can_edit, :only => [:destroy]

rescue_from Errno::ENOENT, :with => :url_upload_not_found
rescue_from Errno::ETIMEDOUT, :with => :url_upload_not_found
rescue_from OpenURI::HTTPError, :with => :url_upload_not_found

rescue_from Timeout::Error, :with => :url_upload_not_found

def index
@uploads = Upload.paginate(:page => params[:page], :order => 'updated_at desc')
end
Expand All @@ -34,5 +36,4 @@ def destroy
@upload.destroy
redirect_to files_path
end

end
end
2 changes: 1 addition & 1 deletion app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,4 @@ def do_login(user)
user.save!
redirect_to root_path
end
end
end
8 changes: 7 additions & 1 deletion app/models/avatar.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
class Avatar < ActiveRecord::Base

belongs_to :user
belongs_to :current_avatar_user, :foreign_key => "current_user_id", :class_name => "User"
belongs_to :current_avatar_user, :foreign_key => 'current_user_id', :class_name => 'User'

has_attachment :storage => :file_system, :max_size => 500.kilobytes, :content_type => :image
include AttachmentFuExtensions

after_destroy :nullify_current_avatar_user

def nullify_current_avatar_user
self.current_avatar_user.update_attribute(:avatar, nil) rescue nil
end

def to_s
filename
end
Expand Down
7 changes: 1 addition & 6 deletions app/models/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@ class Message < ActiveRecord::Base
belongs_to :user

validates_presence_of :body

# TODO can remove w/ rails 2.1
def self.last
find(:first, :order => 'id desc')
end

def self.get
find(:all, :limit => 50, :order => 'messages.id desc', :include => :user)
Expand All @@ -24,4 +19,4 @@ def self.refresh(id, current_user)
def to_s
body
end
end
end
2 changes: 1 addition & 1 deletion app/models/post.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Post < ActiveRecord::Base
def page
posts = Post.find_all_by_topic_id(self.topic_id, :select => 'id', :order => 'created_at').map(&:id)
post_number = posts.rindex(self.id) + 1
(post_number.to_f / Topic::PER_PAGE).ceil
(post_number.to_f / 30).ceil
end

def after_create
Expand Down
4 changes: 1 addition & 3 deletions app/models/topic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ class Topic < ActiveRecord::Base

attr_accessor :body

PER_PAGE = 30

def before_update
@old_forum = Topic.find(id).forum
end
Expand All @@ -35,7 +33,7 @@ def replies
end

def last_page
[(posts_count.to_f / PER_PAGE).ceil.to_i, 1].max
[(posts_count.to_f / 30).ceil.to_i, 1].max
end

def update_cached_fields
Expand Down
Loading

0 comments on commit 5d1ecbb

Please sign in to comment.