Home > Rails > Savage Beast 2.0 - A Rails 2.0 Message Forum Plugin

Savage Beast 2.0 - A Rails 2.0 Message Forum Plugin

January 30th, 2008

I’ve been working the last couple days on creating a new version of the original Savage Beast plugin that is Rails 2.0 compliant and integrates the changes to the Beast source code that have been added over the last year. The result has been an interesting trek through the ins and outs of Rails plugin writing, that has given birth to a new version of the Savage Beast plugin, re-ported from scratch from the Beast trunk.

Installation

Currently, the following is necessary to use the Savage Beast plugin:

  1. The Savage Beast 2.0 plugin. Go to your application root directory and:
    svn export http://savage-beast-2.googlecode.com/svn/trunk/ vendor/plugins/savage_beast

  2. Most of the stuff you need to run Beast
    • Redcloth: gem install Redcloth
    • A bunch of plugins (white_list, white_list_formatted_content, acts_as_list, gibberish, will_paginate, engines). The easiest way to install these en masse is just to copy the contents of savage_beast/tested_plugins to your standard Rails plugin directory (/vendor/plugins). If you already have versions of these plugins, you can just choose not to overwrite those versions
    • For the engines plugin to work, add this line to the top of your environment.rb, right after the require of boot: require File.join(File.dirname(__FILE__), ../vendor/plugins/engines/boot‘)
  3. Copy the migration in /vendor/plugins/savage_beast/db/migrate into your own migration directory (and run it)
  4. Implement in your User model the four methods in plugins/savage_beast/lib/savage_beast/user_init that are marked as "#implement in your user model
  5. Add the line “map.from_plugin :savage_beast” to your routes.rb. Location shouldn’t matter unless you intend to override it.
  6. Add the line “include SavageBeast::UserInit” to your User model. Location shouldn’t matter unless you intend to override it.
  7. Implement versions of the methods in SavageBeast::AuthenticationSystem (located in /plugins/savage_beast/lib) in your application controller if they aren’t already there (note: technically, I believe only “login_required” and “current_user” are necessary, the others give you more functionality). Helpful commenter Adam says that if you have the “helper :all” line in your application controller, be sure to add the “SavageBeast::AuthenticationSystem” line after that.

And off you go! When you visit your_site/forums something should happen. I’ve been creating new forums by visiting /forums/new. There’s probably a hidden admin view somewhere.

I’d like to remove some of these steps during the plugin install process with subsequent releases (is it possible to install a dependent plugin during your plugin install process?), but given that this is my first plugin project (and not a small one at that), I’m just trying to “get it done” before I “get it done beautifully.” I think DHH said somewhere that I should do that.

Implementing Your Own Views and Controllers

The engines plugin makes it eminently easy to mix in your own stuff as you see fit. Just create a new file in your /controllers or /views directories with the same name as the file you want to override in Savage Beast. If you just want to override a particular method in a controller, you can do that piecemeal if you just leave your XController empty except for the method you wanted to override.

If you’re integrating this into an existing site, I’d recommend you start by creating a forums layout page (/app/views/layouts/forums.html.erb). This will give you a taste of how easy it is to selectively override files from the plugin.

Demo

You can check out a (slightly-but-not-too-modified) version of Savage Beast online at Bonanzle.  The differences between our version and the version checked into Subversion are 1) addition of topic tagging (users can tag topics to get them removed, etc) 2) recent post list shows posts in unique topics, rather than showing posts from the same topic repeatedly (there’s another blog on here about the SQL I used to do that) and 3) skinning.  None of those changes feel intrinsic to what SB is “supposed to do,” which is why they aren’t checked in.

Differences from Savage Beast 1.0

The main difference is that this incorporates about a year’s worth of progress to the Beast source, and it actually takes that code a step further by being Rails 2.0.x compliant.

One thing Jodi seemed pretty excited about in the first Savage Beast was the ability to create forums off of models. Bonanzle doesn’t need that functionality, so I haven’t tested it, but I imagine it will probably work if you follow the steps that did it in the first Savage Beast.

TODO

Would be nice to have some help writing tests. Would also be great to figure out how to install this through the standard “ruby script/plugin install”.

Without Engines?

There are a couple means by which you can use this plugin without using the Engines plugin. I originally had intended to do this, suspecting that Engines would be a performance detriment. Turns out it’s not. But maybe you want to roll without Engines for some other reason.

The most dumb-easy way to get by without Engines would just be to copy the views, helpers, controllers, and models into your own project. Yes, it means having a lot more of all the above in your project, but it’s no worse than if you were coding the plugin from scratch yourself (actually, I’d argue it’s a lot better, since you didn’t have to do the work yourself :)).

Another way is to uncomment the code at the bottom of Savage Beast’s init.rb. Before I knew about the Engines plugin I created the code in there that simulates Engines without having Engines installed. The caveat is that you won’t get Engines’ ability to selectively override particular methods/views, and you need to copy all your helpers into the SB/lib directory, where they become global (yuck). If anyone out there has cycles to hone this Engines’-less approach, please email me and we can talk about getting those changes into the project.

Conclusion

Comments are most welcome. I’ll be checking in changes to the project as I find bugs and improvements in using it, but this is admittedly something I don’t have a lot of spare time to closely follow (see my other entries on the wonders of entrepreneurship). Hopefully others can contribute patches as they find time. If you like the plugin, feel free to stop by Agile Development and give it a rating so that others can find it in the future.

Bill Rails

  1. October 30th, 2008 at 08:19 | #1

    Hey Doug,

    Good question. I’ve added a “demo” section to the blog.

    Bill

  2. October 30th, 2008 at 08:20 | #2

    P.S. Judging by the fact that several thousand people have downloaded SB in its months of existence (at least 50 people visit this blog per day), I’m betting that it is probably online at many Rails sites that use forums.

  3. tom
    November 5th, 2008 at 12:09 | #3

    Hi all. No update for Rails 2.1 ? I’ve a lot of difficulties with routes :(
    undefined local variable or method `users_path’
    or
    undefined local variable or method `new_session_path’
    even my restful seems kaput:
    No route matches “/login” with {:method=>:get}
    :((((
    if someone has an idea…
    I’m running Rails 2.1.2 & engines 2.1.2

  4. tom
    November 5th, 2008 at 12:20 | #4

    mode autoresponse: It seems that ‘map.from_plugin :savage_beast’ is not working in my route.rb…
    If I take all routes defined in ‘vendor/plugins/savage_beast/routes.rb’ and put it directly in route.rb of my app, it’s working. cool.

  5. November 5th, 2008 at 17:25 | #5

    We use it on our site. http://www.rubysoccer.com

  6. November 19th, 2008 at 09:26 | #6

    @Ravi: The problem you mentioned about Reply to Topic link not working, is because in the layout application.html, “application.js” is wrongly included before “prototype”, “effects” (hence the show/hide/toggle functions weren’t working)

    Bill, please correct the actual plugin code. Loving the Savage Beast! I am about to launch it on my site: http://www.chictini.com

  7. December 4th, 2008 at 02:17 | #7

    Hello!

    Thanks, William, for a great Beast rewrite. We needed something like that and forked Savage Beast into Aep Beast, to have it Rails 2.1 - compatible and improve the code a bit:
    http://blog.aenima.pl/2008/12/4/aep-beast-a-modern-beast-forum-fork

    It wouldn’t be possible without your Savage Beast though :)

  8. January 22nd, 2009 at 07:53 | #8

    You might get this error after upgrading to Rails 2.2:

    uninitialized constant Rails::Plugin::Dependencies

    You need change savage_beast/init.rb:
    Rails::Plugin::Dependencies

    to

    ActiveSupport::Dependencies

    Zac

  9. Tyler
    January 28th, 2009 at 11:51 | #9

    I was ALSO having issues with a users_path variable not being found.

    Adding map.resources :users to routes.rb didn’t fix it, but when I moved the map.from_plugin :savage_beast so that it happened before the map.resources :users line, then everything started working.

    –Tyler

  10. Robert
    January 28th, 2009 at 17:32 | #10

    Does anyone know a good way to port the data/postings from an existing Beast forum to Savage beast?
    (is it even possible?)

    thanks.

  11. February 9th, 2009 at 03:37 | #11

    I am able to run it successfully with Rails 2.1.0 and 2.3.2 - see my script at http://bananacoding.com/oxite.aspx/Blog/Savage-Beast-Forum-Installation

    Hope this helps,
    -Jirapong

  12. Justin
    February 16th, 2009 at 11:04 | #12

    Note: In part 2.1, “Redcloth” should be “RedCloth” - with a capital ‘C’. The gem install could not find gem “Redcloth” otherwise.

    gem install RedCloth

  13. March 2nd, 2009 at 01:52 | #13

    Do you think that it will work without engines plugin on Rails 2.3. ?

  14. Sarah
    March 14th, 2009 at 20:32 | #14

    I’m also running into the uninitialized constant ForumsController problem…I do have that line added to environment.rb…feels like repeatedly slamming myself against a brick wall as a rails newbie sometimes!

  15. April 4th, 2009 at 23:35 | #15

    We have been using it on our site: http://www.cricketfantasies.com..Amazing plugin!

  16. Stephen
    April 7th, 2009 at 04:56 | #16

    Hi, nice plugin

    I think

    By the way, I had to do

    gem install RedCloth

    (not gem install Redcloth)

  17. April 22nd, 2009 at 02:27 | #17

    I installed everything as instructed above, and when i start server, or infact even when i run rake (for migrations) or generator (for scaffolding) it is breaking and throwing up this error : c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.1.0/lib/active_support/core_ext/module/aliasing.rb:31:in `alias_method’: undefined method `initialize_schema_information’ for module `ActiveRecord::ConnectionAdapters::SchemaStatements’ (NameError) Any clue why this mite be happening?

  18. Thomas
    April 22nd, 2009 at 21:05 | #18

    Hi Bill,

    I downloaded your Savage Beast to work with my Rails 2.3 project. Here are the changes I needed to make:
    * need a version of engines that’s compatible with the version of Rails, since a lot of code has been moving
    from Engines to Rails (git://github.com/lazyatom/engines.git)
    * the migration should be renamed before copying to the migration directory so it doesn’t conflict (I’m using timestamped migrations)
    * in init.rb, need to change “Dependencies.load_once_paths.delete(path)” to “ActiveSupport::Dependencies.load_once_paths.delete(path)”
    (dependencies are part of ActiveSupport now, according to http://sagarkulkarni.com/2009/03/02/getting-weborb-to-work-with-rails-22-const_missing-uninitia-lized-constant-railsplugindependencies/)
    * (note for Mac users, no RedCloth precompiled version for Mac, so you need XTools installed)
    * according to Rails 2.3 release notes, no need to “Add the line “map.from_plugin :savage_beast” to your routes.rb” as rails now automatically includes your mappings, but I still needed to add it.
    * I got the error message “Rails::Plugins::RedCloth not found”, even though I had installed RedCloth as a gem. To get around this, I had to add the line “config.gem “RedCloth”" in my environment.rb.
    * got this warning message “/Users/jobsearch/Documents/Rails/19gale/vendor/plugins/white_list_formatted_content/init.rb:5: warning: default `to_a’ will be obsolete”
    * this is because “Object.to_a” will dissapear in Ruby 1.9. Changed to Array(options)
    * got warning “DEPRECATION WARNING: formatted_forum_posts_path() has been deprecated. Please pass format to the standard forum_posts_path method instead.. (called from _run_erb_vendor47plugins47savage_beast47app47views47forums47show46html46erb at vendor/plugins/savage_beast/app/views/forums/show.html.erb:33)”
    * changed “formatted_forum_posts_path(@forum, :rss)” to “forum_posts_path(@forum, :format => :rss)” wherever found
    * got error “undefined method `to_i’ for {:page=>nil}:Hash” on line #25 of forums_controller.rb
    * paginate method is being called improperly, as it uses default arguments, not options.
    * changed “@forum.topics.paginate :page => params[:page]” to “@forum.topics.paginate( params[:page] ? params[:page] : 1 )” and fixed the problem.
    * (note if you pass 0 or nil as the page #, you get an array, not a collection back).
    * same issue (paginate method) for topics_controller, same fix
    * got error “undefined method `email’ for “#”:User”. Your documentation doesn’t mention you need an “email” method for user.
    * got warning “DEPRECATION WARNING: truncate takes an option hash instead of separate length and omission arguments.”
    * changed all instances of truncate(a,b) to truncate(a,:length=>b). Includes 2 instances in white_list_formatted_content/init.rb
    * when I click on “Reply to topic”, nothing happens. There doesn’t appear to be a function ReplyForm.init()
    * plugin javascript was not included. I added an extra “content_for :head” in topics/show.html.erb as I’m not sure if I want this script available for the rest of my application.
    * “\n \n”
    * Fixed typo “Administator” => “Administrator” in topics/show.html.erb (not related to port, but just to let you know…)

    Hope this information is useful,

    Thomas O’Dell

  19. April 24th, 2009 at 03:50 | #19

    Believe it or not, I actually created a Github project, moved Savage Beast into a project directory with Git, and started making the changes to get it Rails 2.3 compatible. I’m being contracted to make a 2.3-compatible (and fleshed out) version of Bloggity, so I’m going to try to kill two birds with one stone here.

    So this checklist will be extremely helpful in bringing SB in the 2009s. Thanks Thomas!

  20. April 29th, 2009 at 02:38 | #20

    William, that’s some great news. Care to share github repo address? Maybe you could use some work from our fork of SB:
    http://github.com/aenima/aep_beast/tree/master

  21. April 30th, 2009 at 00:51 | #21

    Actually, William, it would be cool if you contacted me (via email or any-method from my website) — we did some work with our fork of Savage Beast (named it Aep_Beast) to make it Rails 2.1-compliant (and clean up some code ;)) and I think you could use/merge our fork and start from that point onwards.

  22. Cory
    April 30th, 2009 at 09:38 | #22

    Hi Bill, looking forward to a 2.3 compatible Savage Beast. Let me know if/when code is pushed to GitHub. I would love to help out; I think it would be great for a current project I’m working on.

  23. April 30th, 2009 at 22:13 | #23

    Hey Tomash,

    Thanks for your generous offer! Hopefully you received my email today that I’d like to try to work with you over the long term to merge the two as best we can. For starters, though, I’d just like to get SB working with 2.3 via the changes suggested above. I don’t think this will tkae me more than a couple hours time, after I actually have those couple hours available (have been spending my open source time this week on Bloggity).

    You can watch the developments unfold at
    Savage Beast:
    http://github.com/wbharding/savage-beast/tree/master

    and Bloggity:
    http://github.com/wbharding/bloggity/commits/master

    I’ll make announcements to the blog when they get to the point of being consumable. Since I’ve been contracted to make progress on Bloggity, that’s getting first dibs; I have to be done with that by May 15. SB is going to be pretty easy to at least get to the point its running though, so I’m pretty sure I’ll be able to sneak in its updates within the next couple weeks as well.

  24. Andy
    May 14th, 2009 at 11:12 | #24

    I’ve implemented this using the steps above and implemented current_user and login_required.

    I’m seeing the forums index and I can create a new forum. But I don’t see a “new topic” button on the topics index page.

    Ideas?

  25. Andy
    May 14th, 2009 at 11:28 | #25

    Also, does anyone have an example of application.rb?

    What’s the actual code I put in application.rb to include the AuthenticationSystem? When it says override, do I write my specific code for those functions in the AuthenticationSystem file or in Application.rb?

  26. Andy
    May 14th, 2009 at 15:16 | #26

    I’ve gotten the New Topic button to display, but now when I go to start a new topic, I get this error:

    You have a nil object when you didn’t expect it!
    The error occurred while evaluating nil.display_name

    Line 8: 8:

    This is in my Application.rb:

    def current_user
    @current_user ||= ((session[:user_id] && User.find_by_id(session[:user_id])) || 0)
    end

    I also tried:

    def current_user
    User.find_by_id(session[:user_id])
    end

    Why is “current_user” nil in the New Topic page?

    Thanks

  27. May 17th, 2009 at 07:08 | #27

    @Andy
    Have you tried User.find(session[:user_id])?

Comment pages
  1. January 3rd, 2009 at 04:50 | #1