Mephisto Contact form plugin

February 23rd, 2009

Being very much into Mephisto lately, I came across James Crisp’s contact form plugin. As my needs are multisite-oriented, I forked and tweaked it as needed. You can find it here

UPDATE March 15, 2009: Good news, Eric Kidd informed me that the patch has been applied to mephisto edge, commit

Currently in mephisto edge, which is maintained and developed by Eric Kidd (aka emk) article versions are broken. More precisely, the bug comes up ONLY when the installation operates in multisite mode (or even more precisely, when there are at least two articles belonging to two different sites).

After reporting the ticket on lighthouse I found out what the bug was. So..

The problem is how acts_as_versioned is being used. An acts_as_versioned record has among others an “id” column (the default id that ActiveRecord requires) and a “version” column.

Currently Mephisto falsely does the following inside \app\controllers\admin\articles_controller.rb on line 38 (edit action)...

@version = params[:version] ? @article.versions.find(params[:version]) : @article or raise(ActiveRecord::RecordNotFound)

the whole problem is the find(params[:version]) . What happens here is, that we lookup an article’s version by searching for its id instead of for its version column (even though we do use the correct :version parameter.)

So this has to change to find_by_version(params[:version]) and thus become..

@version = params[:version] ? @article.versions.find_by_version(params[:version]) : @article or raise(ActiveRecord::RecordNotFound)

Notice though that this doesn’t break in a single-site installation, because in this case id and version bot get the same (concurrent) increment. That is because all articles belong the same one and only Site instance.

‘nough said, voila le patch

While working on the unit tests of Lele I had the situation where I wanted to differentiate among several validation errors a specific field might have.

Since I didn’t want to start comparing error messages contained in model.errors I came up with the following petit hack

For each validation error I cared about detecting (mostly the custom ones) I did the following…

inside say model Private.rb
def validate
  errors.add(:joined, "Run for your life!!!"+ercode(123)) if  grenade.released? 
end

Now what’s ercode(123) ? The argument is arbitrary, ercode is the following method monkey patch of ActiveRecord inside /config/initializer/application.rb

class ActiveRecord::Base
  def ercode(code)

    #turn an integer like '1' into a string like '01'
    str_code = (code <10 ? "0"+ code.to_s : code.to_s)    

    ENV['RAILS_ENV'] == "test" ?  str_code : "" # I only want this when testing
  end
end

Given this, inside private_test.rb I can check whether a particular validation has been triggered by detecting its error code inside the models error strings.

For example…

def test_custom_validations
  ...
  assert_error_code(private, 123)
end

def assert_error_code(model, code)
  assert model.errors.full_messages.join("").include?(code.to_s) # test wether too long left  
end

Here is my vhost file for using a multisite installation (default nowdays with Mephisto 0.8.1) being served by apache 2 and passenger. Don’t forget to a2ensite foo.com your domain, for apache to pick it up.


NameVirtualHost *:80

<VirtualHost *:80>
 ServerName coderado.com 
 ServerAlias www.coderado.com
 RailsEnv production
 DirectorySlash Off

 DocumentRoot /var/apps/m8/public
 ErrorLog /var/apps/m8/log/apache.log
 CustomLog /var/apps/m8/log/access.log combined

 <Directory /var/apps/m8/public>
  Options FollowSymLinks
  AllowOverride None
  Order allow,deny
  Allow from all
 </Directory>

 RailsAllowModRewrite on
 RewriteEngine On
 # Rewrite / to index.html

 RewriteCond %{REQUEST_URI} ^/assets/.*$
 RewriteCond %{DOCUMENT_ROOT}/assets/%{HTTP_HOST}/$1 -f
 RewriteRule ^/assets/(.*)$ /assets/%{HTTP_HOST}/$1 [QSA,L]

 RewriteCond %{REQUEST_URI} ^/$
 RewriteCond %{DOCUMENT_ROOT}/cache/%{HTTP_HOST}/index.html -f
 RewriteRule ^/(.*)$ /cache/%{HTTP_HOST}/index.html [QSA,L]

 RewriteCond %{REQUEST_URI} ^/[^.]+$
 RewriteCond %{DOCUMENT_ROOT}/cache/%{HTTP_HOST}%{REQUEST_FILENAME}.html -f
 RewriteRule ^/(.*)$ /cache/%{HTTP_HOST}%{REQUEST_FILENAME}.html [QSA,L]

 RewriteCond %{REQUEST_URI} ^/.+$
 RewriteCond %{DOCUMENT_ROOT}/cache/%{HTTP_HOST}%{REQUEST_FILENAME} -f
 RewriteRule ^/(.*)$ /cache/%{HTTP_HOST}%{REQUEST_FILENAME} [QSA,L]

</VirtualHost>

Mephisto 0.8 requires nokogiri. Apparently a simple gem install nokogiri is not enough.

here’s what is…

  • sudo apt-get install libxml2-dev libxslt1-dev
  • sudo gem install nokogiri