views:

382

answers:

3

What would be the best approach to develop an "extension" system for a Rails application?

I mean the same kind of system that WordPress has, like registering functions to hooks, etc. Then plugins could be activated/deactivated from an admin panel.

Are there some books/online material about implementing this kind of functionality in RoR software?

A: 

Typo does this using regular rails plugins

Folder containing plugin src:

http://github.com/fdv/typo/tree/812c0fab156c09b7bf00040b047bb59bf586f806/vendor/plugins

List of plugins:

http://wiki.github.com/fdv/typo/typo-plugins-list

Admin Interface for choosing sidebar plugins:

http://github.com/fdv/typo/blob/812c0fab156c09b7bf00040b047bb59bf586f806/app/controllers/admin/sidebar_controller.rb

I'd check out that implementation for some ideas.

Ben Hughes
+2  A: 

There are some implementations that were mentioned by Ben Hughes, which are good examples and would make good launching points. If you wanted to roll your own solution from scratch, I'd suggest something like the following:

Create two tables/models, Widgets and UserWidgets:

Widgets:
  ID (Primary Key)
  Name
  Description

UserWidgets:
  ID (Primary Key)
  User_ID (Foreign Key -> Users.ID)
  Widget_ID (Foreign Key -> Widget.ID)

Now you have a table for associating a user to widget. I'm assuming your widgets will be handled primarily via JavaScript, so add a new subdirectory, like so...

/public/javascripts/widgets

Now, for every widget in your Widgets table, add the appropriate JS file to this subdirectory. If you have a widget with the name "Clock", add "Clock.js".

Add the proper associations to the User Model.

has_many :user_widgets, :dependent => :destroy
has_many :widgets, :through -> :user_widgets

Then in a view somewhere, you can do:

<% @user.widgets.each do |w| %>
  <%= javascript_include_tag "widgets/#{w}" -%>
<% end %>

This would likely be put into the layout for whatever page(s) a widget would be appearing on. If you don't want to use dynamic widgets via JavaScript, you can use very similar view code anywhere in a view/partial.

Mike Trpcic
A: 

The following suggestion goes for a Ruby Application but can be adjusted for your purpose: Plugins in your Ruby Application

arnklint