Archive for the ‘rails’ Category

showing session data with exception notification

June 15, 2009 , Written By  Surendra Singhi.

We have been using Exception Notification in many of our Rails Project. After upgrading to Rails 2.3 and using our own version of optimized session class, we have noticed that the error emails generated by this plugin were not sending session information. To fix this the below patch can be used.

  1. Index: vendor/plugins/exception_notification/views/exception_notifier/_session.rhtml
  2. ===================================================================
  3. — vendor/plugins/exception_notification/views/exception_notifier/_session.rhtml
  4. +++ vendor/plugins/exception_notification/views/exception_notifier/_session.rhtml       (working copy)
  5. @@ -1,2 +1,2 @@
  6. -*session id: <%= @request.session.instance_variable_get(:@session_id).inspect %>
  7. -*data: <%= PP.pp(@request.session.instance_variable_get(:@data), "").gsub(/\n/, "\n  ").strip %>
  8. +*session id: <%= @request.session_options[:id].inspect %>
  9. +*data: <%= @request.session.to_hash.inspect.to_s.gsub(/\n/, "\n  ").strip %>

Locally generating rails documentation in mac & linux.

January 30, 2009 , Written By  sdhar.

It is very easy to build a local copy of rails documentation and guides. Open terminal and type these commands.

  1. $ cd /tmp
  2. $ rails doc
  3. $ cd doc
  4. $ rake rails:freeze:gems
  5. $ rake doc:rails
  6. $ rake doc:guides
  7. $ sudo mv doc /opt/rails-doc

After completing these steps open browser and type

  1. file:///opt/rails-doc/api/index.html //api.
  2. file:///opt/rails-doc/guides/index.html // guide related to rails development.

organizing mailers and mailer templates

January 24, 2009 , Written By  Surendra Singhi.

When working on a large Rails project, it can be a kludge having the mailers present among the views and other model files. A nice technique to clean up things is to put all the mailers and associated views in the same folder. If you don’t know about this technique then go read it first, and then come back here.

The benefit of having the ApplicationMailer is that you can define the host for the mailers, include ActionController::UrlWriter, other setup methods in a single file and have all the mailers inherit them.

The patch written for action mailer to not use layouts for text/plain email was causing problem with exception notification plugin, so I have modified it slightly so that it checks whether the file parameter passed to it responds to content_type or not.

  1. def candidate_for_layout?(options)
  2.   (!options[:file] || !options[:file].respond_to?(:content_type) ||
  3.      options[:file].content_type != ‘text/plain’) &&
  4.      !@template.send(:_exempt_from_layout?, default_template_name)
  5. end

Further to clean things up this patch can be moved to ApplicationMailer to ensure that it is only applied for all our mailers and not for other mailers which may be used elsewhere.

Rails 2.2.2 - exception notification and spawn plugin

January 1, 2009 , Written By  Surendra Singhi.

In Rails 2.2.2 the send! method from ActiveSupport has been removed,this broke the exception notification plugin. So, get the latest release from the github.

Also, the introduction of database connection pools breaks the spawn plugin. So, switch to the latest release of that as well.

switch from enhanced migrations plugin to rails timestamped migrations

December 27, 2008 , Written By  Surendra Singhi.

On our rails 1.2.* project we were using enhanced migrations plugin. Recently we decided to make a switch to rails 2.2.* and wanted to use default UTC based migrations versioning which is a part of the rails core now.

Enhanced migrations plugin uses the table migrations_info to store the migrations information but rails 2.2 uses the table schema_migrations by default. When one does rake db:migrate, the rails code checks whether the schema_migrations table exists or not, if the table is not there, then it is created, and if the schema_info table is present, then this table is populated with all migrations upto the version in the schema_info table. But, since we were using the enhanced migrations plugin, the table schema_info was present but wasn’t being used.

So, to make the switch to rails 2.2.* timestamped migrations before doing regular rake db:migrate the following sql code had to be run, to ensure that schema_migrations table is pre-created, and has the correct list of all migrations which have been run.

create table schema_migrations (version varchar(255) not null primary key);
INSERT INTO schema_migrations select id from migrations_info;
drop table if exists schema_info;

multipart emails with mailer templates

December 23, 2008 , Written By  Surendra Singhi.

Update: The code given below here causes the exception notification plugin to stop working, get the fixed code from this post. It also contains a better technique to organize the mailers.

Rails 2.2 has added a very cool new feature which allows the use of layouts for mailer-templates, just like the way layouts can be used for views. This is great, but there is one gotcha, when you try sending a multi-part email (both text and html, as you should be), it wraps the mail content with the same layout for both the text/plain version and the text/html version.

After digging around a bit in the ActionMailer code, I came up with the following monkey patch, which will use the layout only for the text/html version of the email. You can put this code in your environment.rb file, it won’t use layout when the mail-template content type is ‘text/plain’.

  1. # monkey patch action mailer to not use layouts for text/plain emails
  2. module ActionMailer
  3.   class Base
  4.     private
  5.     def candidate_for_layout?(options)
  6.       (!options[:file] || options[:file].content_type != ‘text/plain’) && !@template.send(:_exempt_from_layout?, default_template_name)
  7.     end
  8.   end
  9. end

If you can come up with some other better solution than this, then let us know.