Sorry the the confusion, anyone who has been checking out the the Mephisto Contact Form Plugin from the old SVN repository. The latest version with an update for Rails 2.3 is at:
Category: Mephisto
Mephisto Drax (version 0.8) introduces breaking changes for plugins. I’ve just finished updating the contact / feedback form plugin. It’s now working fine and tests passing.
Installation instructions are the same as before except that the ‘contact_notifier’ has moved from the ‘lib’ directory to the ‘app’ directory. It still needs to be updated to include your destination email address for contact mails.
If you’re a Mephisto plug-in developer, you might be interested in checking out my post on migrating Mephisto plugins to Drax. It’s based on my experiences with the contact_form.
There have been some major changes to Mephisto in the latest release (0.8 Drax) that break existing plugins. If you’re interested in migrating your existing plugin(s) over to Drax, read on.
Repository Move
First thing to note is that the Mephisto code base has moved from SVN to github.
Plugin Architecture Changes
There is no longer a base class for Mephisto plugins. Instead, you create Mephisto plugins using Rails Engines. If you’re migrating a pre-Drax plugin to Drax and Rails engines, you’ll most likely need to:
- Remove your plugin file – there’s no base class for it any more so you’ll get errors like: ‘superclass must be a Class (Module given) (TypeError)’
- Move your routes into a ‘routes.rb’ file in your plugin root directory.
- In your plugin root directory, create an ‘app’ directory, with ‘views’, ‘models’ and ‘controllers’ sub-directories. Move your code files into the appropriate folders in the ‘app’ directory. These will be auto-loaded.
- Remove various lines in your init.rb which manually add your plugin file directories to the load paths, if you have these.
- If you inherit from the ApplicationController, add ‘unloadable‘ to your controller class. This will fix errors in development mode like ‘A copy of ApplicationController has been removed from the module tree but is still active!’
An example
You can have a look at my contact_form plugin code. Revision 18 is before Drax and engines and uses the old approach. Revisions 19 and later are using Rails engines and will work with Drax.
UPDATE: Latest code at GitHub
http://github.com/jcrisp/mephisto_contact_form/tree/master
When I was writing a contact form plugin for Mephisto, I had a lot of trouble finding documentation and ended up reading lots of code and experimenting. That was fun, but fairly slow, so I hope this post can save future plugin developers time, and help them avoid some of the gotchas I stumbled over.
Repository Directory Structure
At the most macro level, your repository needs to have a ‘plugins’ directory, and then a directory named after your plugin. Eg,
…/plugins/my_new_plugin/…
If this is not set up correctly, your plugin will not be able to be installed via ‘ruby script/install plugin
Liquid Plugins Directory and Init.rb
As you probably know, Mephisto uses Liquid for page templates. Liquid can be extended with new tags/blocks. The way to do this in a plugin is to set up a ‘mephisto/liquid’ directory with your extensions in it. See example here. So that’s great, but you also need to register it in init.rb. Here’s the contact form’s init.rb – check out the line about ‘register_tag’.
Mephisto Plugin Class
Mephisto trunk now has a base class for plugins – Mephisto::Plugin. Inheriting from this allows you to set up routes to brand new controllers you create. See contact form example here. This opens the door to writing Mephisto plugins which do postback and processing. It is also possible to add in tabs and forms in the administration interface. Rick‘s feedback plugin shows how to do this.
Using Liquid Templates from Your Plugin
One of the trickiest bits was getting the plug-in controller to render a liquid template. This is important if you want your additions to Mephisto to have the same layout and colours as the rest of the site. The way I’ll outline below works fine, but it is not ideal. Hopefully there is a better way to do this (eg, some sort of Liquid API for Mephisto plugins).. if you know how a better way, please let me know!
I had my plugin controller inherit from the Mephisto ApplicationController to gain access to the method ‘render_liquid_template_for’. You can see the code here. However, this led to thorny problems where the plug-in classes were getting loaded only once when the server started, but Mephisto (and the ApplicationController) were getting reloaded for every request. First request worked fine, but nasty errors were spat out on the second and subsequent requests. To resolve this, I removed the plug-in from the ‘load_once_paths’. You can see how to do this in the init.rb.
Models, Views & Controllers Directories and Init.rb
Okay, this is open to personal taste. I like to have similar directories in my plugin to a normal app. Eg, separate directories for controllers, model, etc. This causes a bit more work, as you need to add the extra directories to various global path variables. For an example of how to do this, take a look at ‘models_path’ and ‘controllers_path’ in this init.rb and the physical directory structure of the contact form’s lib directory.
UPDATE
More info about Mephisto plugins with Drax 0.8.
Introduction
If you use Mephisto, a content management / blogging system written in Rails, you may well be interested in using this new plug-in. It provides a form that lets visitors to your site leave their contact details and send you messages or feedback via email.
UPDATE
Please check out information about using the contact form plugin with Mephisto Drax 0.8.
License
This plug-in was developed for the new ThoughtWorks Studios site. As I wrote it at and for work, it is copyright ThoughtWorks, 2007. However, ThoughtWorks, being generous souls, is happy for me to open source it under the Apache 2.0 licence, which pretty much means you have free reign to use it as you want.
Requirements
- Mephisto Edge (the latest stable 0.7.3 release does not have support for Mephisto plugins)
- Rails Edge (required by Mephisto edge)
- ActionMailer (comes with Rails) correctly configured with SMTP server etc, so that emails can be delivered. See “Configuration” section here for more details.
Installation
ruby script/plugin install http://github.com/jcrisp/mephisto_contact_form/tree/master
or in your vendor/plugins directory for Mephisto:
git clone http://github.com/jcrisp/mephisto_contact_form/tree/master mephisto_contact_form
Make sure you restart your web server at this point so that the plugin is loaded.
Setup
1. Create a new template called ‘contact_us.liquid’ though the admin web interface (under the ‘Design’ tab).
Paste in the following code:
<H1>Contact Us</H1> {% contactform %} <p>{{ form.name }}<label for="author"><small>Your name</small></label></p> <p>{{ form.email }}<label for="email"><small>Email address</small></label></p> <p>{{ form.phone}}<label for="phone"><small>Phone number (optional)</small></label></p> <p>{{ form.subject}}<label for="subject"><small>Subject</small></label></p> <p>{{ form.body }}</p> <p>{{ form.submit }}</p> {% endcontactform %}
Feel free to modify labels, layout etc.
2. Edit
{MEPHISTO_ROOT}/vendor/plugins/mephisto_contact_form/lib/contact_notifier.rb
and put in the email address you want contact form submissions to go to.
3. Link to “/contact_form” from your site.
Any issues / questions / suggestions?
Best to post comments on this blog.
Technical Info
The contact form plugin is actually a combination of a rails plugin, a liquid block plugin and a Mephisto plugin. See this post about developing Mephisto plugins for more information.