Skip to content
This repository has been archived by the owner on Aug 15, 2018. It is now read-only.

Commit

Permalink
modified savage beast to work like I needed it to...
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh Adams committed Jan 31, 2009
1 parent 2f66e86 commit 1f66ba6
Show file tree
Hide file tree
Showing 280 changed files with 9,556 additions and 2 deletions.
2 changes: 1 addition & 1 deletion config/environment.rb
Expand Up @@ -58,7 +58,7 @@
# Only load the plugins named here, in the order given. By default, all plugins
# in vendor/plugins are loaded in alphabetical order.
# :all can be used as a placeholder for all plugins not explicitly named
config.plugins = [ :has_settings, :stringex, :userstamp, :paperclip, :all ]
config.plugins = [ :has_settings, :stringex, :userstamp, :paperclip, :white_list, :white_list_formatted_content, :all ]

# Add additional load paths for your own custom dirs
# config.load_paths += %W( #{RAILS_ROOT}/extras )
Expand Down
2 changes: 1 addition & 1 deletion config/routes.rb
@@ -1,7 +1,7 @@
ActionController::Routing::Routes.draw do |map|
map.from_plugin :ansuz_photo_album
map.from_plugin :ansuz_blog
#map.from_plugin :ansuz_savage_beast
map.from_plugin :ansuz_savage_beast
map.from_plugin :ansuz_content_section
map.from_plugin :ansuz_user_manager
map.from_plugin :ansuz_menu_system
Expand Down
8 changes: 8 additions & 0 deletions vendor/plugins/ansuz_savage_beast/README
@@ -0,0 +1,8 @@
See http://www.williambharding.com/blog/?p=100 for install instructions
See http://code.google.com/p/savage-beast-2/ to grab the latest version

Please post any comments or suggestions to the blog post. If you would like
to make a contribution to improve this plugin, send me an email via the site.

And be sure you carefully follow the Engines installation instructions! That
line to add to your environment file is easily missed.
10 changes: 10 additions & 0 deletions vendor/plugins/ansuz_savage_beast/Rakefile
@@ -0,0 +1,10 @@
# Add your own tasks in files placed in lib/tasks ending in .rake,
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.

require(File.join(File.dirname(__FILE__), 'config', 'boot'))

require 'rake'
require 'rake/testtask'
require 'rake/rdoctask'

require 'tasks/rails'
@@ -0,0 +1,62 @@
class ForumsController < ApplicationController
before_filter :login_required, :except => [:index, :show]
before_filter :find_or_initialize_forum, :except => :index
before_filter :admin?, :except => [:show, :index]

cache_sweeper :posts_sweeper, :only => [:create, :update, :destroy]

def index
@forums = Forum.find_ordered
# reset the page of each forum we have visited when we go back to index
session[:forum_page] = nil
respond_to do |format|
format.html
format.xml { render :xml => @forums }
end
end

def show
respond_to do |format|
format.html do
# keep track of when we last viewed this forum for activity indicators
(session[:forums] ||= {})[@forum.id] = Time.now.utc if logged_in?
(session[:forum_page] ||= Hash.new(1))[@forum.id] = params[:page].to_i if params[:page]

@topics = @forum.topics.paginate :page => params[:page]
User.find(:all, :conditions => ['id IN (?)', @topics.collect { |t| t.replied_by }.uniq]) unless @topics.blank?
end
format.xml { render :xml => @forum }
end
end

# new renders new.html.erb
def create
@forum.attributes = params[:forum]
@forum.save!
respond_to do |format|
format.html { redirect_to @forum }
format.xml { head :created, :location => formatted_forum_url(@forum, :xml) }
end
end

def update
@forum.update_attributes!(params[:forum])
respond_to do |format|
format.html { redirect_to @forum }
format.xml { head 200 }
end
end

def destroy
@forum.destroy
respond_to do |format|
format.html { redirect_to forums_path }
format.xml { head 200 }
end
end

protected
def find_or_initialize_forum
@forum = params[:id] ? Forum.find(params[:id]) : Forum.new
end
end
@@ -0,0 +1,10 @@
class ModeratorsController < ApplicationController
before_filter :login_required

def destroy
Moderatorship.delete_all ['id = ?', params[:id]]
redirect_to user_path(params[:user_id])
end

alias authorized? admin?
end
@@ -0,0 +1,22 @@
class MonitorshipsController < ApplicationController
before_filter :login_required

cache_sweeper :monitorships_sweeper, :only => [:create, :destroy]

def create
@monitorship = Monitorship.find_or_initialize_by_user_id_and_topic_id(current_user.id, params[:topic_id])
@monitorship.update_attribute :active, true
respond_to do |format|
format.html { redirect_to forum_topic_path(params[:forum_id], params[:topic_id]) }
format.js
end
end

def destroy
Monitorship.update_all ['active = ?', false], ['user_id = ? and topic_id = ?', current_user.id, params[:topic_id]]
respond_to do |format|
format.html { redirect_to forum_topic_path(params[:forum_id], params[:topic_id]) }
format.js
end
end
end
134 changes: 134 additions & 0 deletions vendor/plugins/ansuz_savage_beast/app/controllers/posts_controller.rb
@@ -0,0 +1,134 @@
class PostsController < ApplicationController
before_filter :find_post, :except => [:index, :create, :monitored, :search]
before_filter :login_required, :except => [:index, :monitored, :search, :show]
@@query_options = { :select => "#{Post.table_name}.*, #{Topic.table_name}.title as topic_title, #{Forum.table_name}.name as forum_name", :joins => "inner join #{Topic.table_name} on #{Post.table_name}.topic_id = #{Topic.table_name}.id inner join #{Forum.table_name} on #{Topic.table_name}.forum_id = #{Forum.table_name}.id" }

# @WBH@ TODO: This uses the caches_formatted_page method. In the main Beast project, this is implemented via a Config/Initializer file. Not
# sure what analogous place to put it in this plugin. It don't work in the init.rb
#caches_formatted_page :rss, :index, :monitored
cache_sweeper :posts_sweeper, :only => [:create, :update, :destroy]

def index
conditions = []
[:user_id, :forum_id, :topic_id].each { |attr| conditions << Post.send(:sanitize_sql, ["#{Post.table_name}.#{attr} = ?", params[attr]]) if params[attr] }
conditions = conditions.empty? ? nil : conditions.collect { |c| "(#{c})" }.join(' AND ')
@posts = Post.paginate @@query_options.merge(:conditions => conditions, :page => params[:page], :count => {:select => "#{Post.table_name}.id"}, :order => post_order)
@users = User.find(:all, :select => 'distinct *', :conditions => ['id in (?)', @posts.collect(&:user_id).uniq]).index_by(&:id)
render_posts_or_xml
end

def search
conditions = params[:q].blank? ? nil : Post.send(:sanitize_sql, ["LOWER(#{Post.table_name}.body) LIKE ?", "%#{params[:q]}%"])
@posts = Post.paginate @@query_options.merge(:conditions => conditions, :page => params[:page], :count => {:select => "#{Post.table_name}.id"}, :order => post_order)
@users = User.find(:all, :select => 'distinct *', :conditions => ['id in (?)', @posts.collect(&:user_id).uniq]).index_by(&:id)
render_posts_or_xml :index
end

def monitored
@user = User.find params[:user_id]
options = @@query_options.merge(:conditions => ["#{Monitorship.table_name}.user_id = ? and #{Post.table_name}.user_id != ? and #{Monitorship.table_name}.active = ?", params[:user_id], @user.id, true])
options[:order] = post_order
options[:joins] += " inner join #{Monitorship.table_name} on #{Monitorship.table_name}.topic_id = #{Topic.table_name}.id"
options[:page] = params[:page]
options[:count] = {:select => "#{Post.table_name}.id"}
@posts = Post.paginate options
render_posts_or_xml
end

def show
respond_to do |format|
format.html { redirect_to forum_topic_path(@post.forum_id, @post.topic_id) }
format.xml { render :xml => @post.to_xml }
end
end

def create
@topic = Topic.find_by_id_and_forum_id(params[:topic_id],params[:forum_id])
if @topic.locked?
respond_to do |format|
format.html do
flash[:notice] = 'This topic is locked.'[:locked_topic]
redirect_to(forum_topic_path(:forum_id => params[:forum_id], :id => params[:topic_id]))
end
format.xml do
render :text => 'This topic is locked.'[:locked_topic], :status => 400
end
end
return
end
@forum = @topic.forum
@post = @topic.posts.build(params[:post])
@post.user = current_user
@post.save!
respond_to do |format|
format.html do
redirect_to forum_topic_path(:forum_id => params[:forum_id], :id => params[:topic_id], :anchor => @post.dom_id, :page => params[:page] || '1')
end
format.xml { head :created, :location => formatted_post_url(:forum_id => params[:forum_id], :topic_id => params[:topic_id], :id => @post, :format => :xml) }
end
rescue ActiveRecord::RecordInvalid
flash[:bad_reply] = 'Please post something at least...'[:post_something_message]
respond_to do |format|
format.html do
redirect_to forum_topic_path(:forum_id => params[:forum_id], :id => params[:topic_id], :anchor => 'reply-form', :page => params[:page] || '1')
end
format.xml { render :xml => @post.errors.to_xml, :status => 400 }
end
end

def edit
respond_to do |format|
format.html
format.js
end
end

def update
@post.attributes = params[:post]
@post.save!
rescue ActiveRecord::RecordInvalid
flash[:bad_reply] = 'An error occurred'[:error_occured_message]
ensure
respond_to do |format|
format.html do
redirect_to forum_topic_path(:forum_id => params[:forum_id], :id => params[:topic_id], :anchor => @post.dom_id, :page => params[:page] || '1')
end
format.js
format.xml { head 200 }
end
end

def destroy
@post.destroy
flash[:notice] = "Post of '{title}' was deleted."[:post_deleted_message, @post.topic.title]
respond_to do |format|
format.html do
redirect_to(@post.topic.frozen? ?
forum_path(params[:forum_id]) :
forum_topic_path(:forum_id => params[:forum_id], :id => params[:topic_id], :page => params[:page]))
end
format.xml { head 200 }
end
end

protected
def authorized?
action_name == 'create' || @post.editable_by?(current_user)
end

def post_order
"#{Post.table_name}.created_at#{params[:forum_id] && params[:topic_id] ? nil : " desc"}"
end

def find_post
@post = Post.find_by_id_and_topic_id_and_forum_id(params[:id], params[:topic_id], params[:forum_id]) || raise(ActiveRecord::RecordNotFound)
end

def render_posts_or_xml(template_name = action_name)
respond_to do |format|
format.html { render :action => template_name }
format.rss { render :action => template_name, :layout => false }
format.xml { render :xml => @posts.to_xml }
end
end
end
110 changes: 110 additions & 0 deletions vendor/plugins/ansuz_savage_beast/app/controllers/topics_controller.rb
@@ -0,0 +1,110 @@
class TopicsController < ApplicationController
before_filter :find_forum_and_topic, :except => :index
before_filter :login_required, :only => [:new, :create, :edit, :update, :destroy]

# @WBH@ TODO: This uses the caches_formatted_page method. In the main Beast project, this is implemented via a Config/Initializer file. Not
# sure what analogous place to put it in this plugin. It don't work in the init.rb
#caches_formatted_page :rss, :show
cache_sweeper :posts_sweeper, :only => [:create, :update, :destroy]

def index
respond_to do |format|
format.html { redirect_to forum_path(params[:forum_id]) }
format.xml do
@topics = Topic.paginate_by_forum_id(params[:forum_id], :order => 'sticky desc, replied_at desc', :page => params[:page])
render :xml => @topics.to_xml
end
end
end

def new
@topic = Topic.new
end

def show
respond_to do |format|
format.html do
# see notes in application.rb on how this works
update_last_seen_at
# keep track of when we last viewed this topic for activity indicators
(session[:topics] ||= {})[@topic.id] = Time.now.utc if logged_in?
# authors of topics don't get counted towards total hits
@topic.hit! unless logged_in? and @topic.user == current_user
@posts = @topic.posts.paginate :page => params[:page]
User.find(:all, :conditions => ['id IN (?)', @posts.collect { |p| p.user_id }.uniq]) unless @posts.blank?
@post = Post.new
end
format.xml do
render :xml => @topic.to_xml
end
format.rss do
@posts = @topic.posts.find(:all, :order => 'created_at desc', :limit => 25)
render :action => 'show', :layout => false
end
end
end

def create
topic_saved, post_saved = false, false
# this is icky - move the topic/first post workings into the topic model?
Topic.transaction do
@topic = @forum.topics.build(params[:topic])
assign_protected
@post = @topic.posts.build(params[:topic])
@post.topic = @topic
@post.user = current_user
# only save topic if post is valid so in the view topic will be a new record if there was an error
@topic.body = @post.body # incase save fails and we go back to the form
topic_saved = @topic.save if @post.valid?
post_saved = @post.save
end

if topic_saved && post_saved
respond_to do |format|
format.html { redirect_to forum_topic_path(@forum, @topic) }
format.xml { head :created, :location => formatted_topic_url(:forum_id => @forum, :id => @topic, :format => :xml) }
end
else
render :action => "new"
end
end

def update
@topic.attributes = params[:topic]
assign_protected
@topic.save!
respond_to do |format|
format.html { redirect_to forum_topic_path(@forum, @topic) }
format.xml { head 200 }
end
end

def destroy
@topic.destroy
flash[:notice] = "Topic '{title}' was deleted."[:topic_deleted_message, @topic.title]
respond_to do |format|
format.html { redirect_to forum_path(@forum) }
format.xml { head 200 }
end
end

protected
def assign_protected
@topic.user = current_user if @topic.new_record?
# admins and moderators can sticky and lock topics
return unless admin? or current_user.moderator_of?(@topic.forum)
@topic.sticky, @topic.locked = params[:topic][:sticky], params[:topic][:locked]
# only admins can move
return unless admin?
@topic.forum_id = params[:topic][:forum_id] if params[:topic][:forum_id]
end

def find_forum_and_topic
@forum = Forum.find(params[:forum_id])
@topic = @forum.topics.find(params[:id]) if params[:id]
end

def authorized?
%w(new create).include?(action_name) || @topic.editable_by?(current_user)
end
end

0 comments on commit 1f66ba6

Please sign in to comment.