duck_punching

The monkeys will have their revenge!

ar_mailer gem forked on GitHub, goodies added

Now that GitHub has a gem server it allows the process of managing your own fork or customisation of a gem ridiculously easy. No need for your own gem server or shipping around gem files, just put it up on github and nominate it as a gem in the project config. GitHub will automatically compile the gems using a gemspec file if present. All the info you need is here so I won't recap it any further.

A gem which I find essential for Rails apps which send out emails is ar_mailer by Eric Hodel of Seattle Ruby Brigade and Ruby Hitsquad fame and now the maintainer of RubyGems. But there a couple of things missing. One is the need for pid files to be generated when running as a daemon to allow effective management with monitoring tools and such.

Well there just so happened to be a patch by Dylan Egan in the ar_mailer forums to add this feature, but it has been overlooked for a long time. So I thought this would be a good chance to fork it and use the GitHub gem system as well. Its now forked it and patched with some tweaks to get it working and a few fixes for the tests which broke due to changes in the latest ZenTest gem.

The project for the gem is here. To use the new gem you need to remove the old one because of the way github names your gems. It uses github username as a prefix to the gem name. This means the old gem will be loaded instead of the new gem when required in your app. But the binary will be used from the new gem causing nasty problems.

To install, first add GitHub as a gem source

 
sudo gem sources -a http://gems.github.com
 

Then

 
sudo gem install adzap-ar_mailer
 

Now when the binary is run in daemon mode you will get the pid file you need. You can specify the pid file location in the command options like so

 
ar_sendmail -d -p ./log/ar_sendmail.pid
 

Any relative paths are expanded from the app directory. You can of course use an absolute path if you wish. The file gets removed on exit as you would expect.

The other major addition is not as visible. Its a Ruby based init.d script for managing mailer startup for one or more apps. It uses a yaml based config file where you specify the app settings for each app that needs a mailer instance running. See the README and demo config for more info.

Ruby, my hammer, is starting to make everything look a nail!

Remember this is GitHub, don't like what I have done, then fork off!

UPDATE:
If you are getting an error running the ar_sendmail binary, this is due to GitHub to setting the permission on gem binary files correctly. The error would be

/usr/bin/ar_sendmail:19:in `load': no such file to load -- ar_sendmail (LoadError)
	from /usr/bin/ar_sendmail:19

To fix it you need to run this ugly (please let me know a better way to get a gem path!!)

GEM=`ruby -e "require 'rubygems'; puts Gem.source_index.search('adzap-ar_mailer').last.full_gem_path + '/bin/ar_sendmail'"`
sudo chmod 755 $GEM

Now you should be able to execute it as a normal user.

No comments

date_time_text_field_helpers plugin moved to GitHub

My date_time_text_field_helpers plugin allows you to use text input fields for dates and times instead of the Rails standard select dropdowns. The plugin was up on Google Code but since I am mostly using git now, I just had to bring it over GitHub.

Updates will only be pushed to git from now on. The Google Code page will stay up for a while, until such time that I remember to pull it down.

No comments

Google-by-keyboard Firefox search plugin for Australia

If you haven't seen it yet, Google has a project in its labs for the navigation of search results with keyboard shortcuts. Its great for geeks who love the keyboard.

Well a reader at LifeHacker knocked out the search plugin for Firefox. I took that and tweaked it for the Australian search engine for those times you need sumthin a little more local. So download it and stick it in your Firefox profile searchplugins folder. Then change the search engine in the search bar to be 'Google by keyboard (AU)'.

Remember it doesn't search in au domain by default, it just means the search results will have the 'pages from Australia' radio button under the Google search box.

No comments

Using ActionView in standalone tests

When writing Rails plugins I like to be able to test them as a stand-alone library rather than inside a specific Rails project. I find it more productive and it avoids clashes with other plugins. It also allows you to test it against multiple rails versions.

One frustration I have had is when just want to use ActionView in your tests, because you might be writing a view plugin, if you require 'action_view' you get the following error:

MissingSourceFile: no such file to load -- html/document

This is because the path to the html-scanner library has not been added to the loadpath. This only happens in ActionController. But if we don't need or want the overhead of ActionController the following line will include html-scanner in the loadpath and all will be well.

$:.unshift Gem.searcher.find('actionpack').full_gem_path
  + '/lib/action_controller/vendor/html-scanner'

This finds the actionpack gem to get the full path. Then with the path to the html-scanner folder, adds it to the loadpath ($:). Now no more pesky load errors.

If there is a cleaner way, let me know.

No comments

calendar_helper plugin forked

I have been using Geoffrey Grosenbach's calendar_helper plugin and needed a few changes. I am also using git for projects now and finding the submodule a workable strategy for plugins. So I imported it into git using git-svn and created a new project on GitHub. I plan to do some refactoring to make it a little cleaner but mostly will just tweak for my own usage.

My first changes were to remove the requirement for the year and month to be passed in as options. These now default to the current month and year if not included in the options hash.

I have also added the :month_name_text option to override the default text for the month name. The month name is basically the calendar header so this makes it possible to include the year in the header for clarity.

Now that I have forked it, I removed the gem spec files as it will not be published as a gem as that would confuse everyone and just isn't the done thing.

Feel free to use and fork of course, these are just my preferences and maybe more to come. Thanks of course go to the original authors and contributors of the plugin.

No comments

Passing a block to ActiveRecord create

I submitted a patch for Rails, which has been committed, to allow you to pass a block to the AR create class method. It got a mention on the RailsEnvy podcast in the Living on the Edge section.

It mind sound boring, but it allows you to pretty up the regular code such as creating a new record from the parameters hash to then save it.

Often you might

 
@person = Person.new(params[:person])
@person.set_status :alive
@person.group = "default"
@person.save

now you can pretty it up with

 
@person = Person.create(params[:person]) do |p|
  p.set_status :alive
  p.group = "default"
end

The inner workings are that it creates a new object using the hash and then passes the object into the block for further manipulation. After the block is executed the record is saved and returned.

It can be particularly useful if you have a few protected attributes that you need to set or some instance method you need to execute when you are creating a record. Admittedly the set_status method might best be run as a callback in the model, but you get the idea.

It also works for the array variant of create when you pass in an array of hashes. The code in the block is executed for each record.

So look for it in Rails 2.1 or use it now with Edge.

Footnote: The original patch was actually larger and included the update method as well. Though DHH was not in favour of it, mentioning that the class update method itself may be of dubious value.

No comments

Jeez, where’s your blog dude?

Its time! As Gough Whitlam said. I am finally getting round to starting this blog of mine. At least there are few things which I can actually post about and possibly get some feedback from folks.

This will be my tech blog focussing on, guess what, Ruby and Rails and probably other stuff too. If the name of the blog sounds like I am promoting animal cruelty, fear not, it has its roots in the Ruby community from the mind of the inimitable Why the lucky stiff. And besides I like all the little, little duckies in the pond.

Anyway welcome and I hope to see you round these parts.

PS. If anyone is handy with graphics type software (I am definitely not) and happy to donate time (I know, I'm dreamin) I was thinking of a silhouetted monkey wearing boxing gloves, punching a duck as the main graphic for this blog. Just putting it out there.

No comments

Mexico