Using Shell aliases to speed up your Rails development (on OSX)

Tired of writing

script/server

?

How about saving yourself from carpal tunnel with handy aliases like these? ;

 
# Rails 
alias r='touch tmp/restart.txt' 
alias ss='script/server' 

Grab the source over at http://gist.github.com/218795 and whack it in your .bash_profile.

(Dont forget to set up git autocompletion first : http://blog.ericgoodwin.com/2008/4/10/auto-completion-with-git )

Multiple ActionMailer delivery methods

Looking for a way to deliver some of your ActionMailer notifications immediately (e.g. via :smtp) and others using a delayed/queued method (e.g. :activerecord using AR-Mailer (http://github.com/adzap/ar_mailer)?

Here's how I did it.

My default delivery method in my application was immedate using ActionMailer with SMTP.



# set delivery method to :smtp, :sendmail or :test


# or :activerecord for ar-mailer
 config.action_mailer.delivery_method = :smtp 

A corresponding model method action would look something like this;

 def deliver_new_registration_notice! 
  Notifier.deliver_new_registration_notice(self) 
 end

And for delayed (queued) delivery, you need to temporarily override the ActionMailer::Base.delivery_method;

 
 def deliver_existing_member_activation! 
   delivery_method = ActionMailer::Base.delivery_method 
   ActionMailer::Base.delivery_method = :activerecord #needed for bulk sending 
   reset_perishable_token! 
   Notifier.deliver_existing_member_activation(self) 
   ActionMailer::Base.delivery_method = delivery_method #reset it 
 end 

Rails guys: If you haven't tried Sass, just do it. Do it now.

On a recent project, our designer resisted our attempts to refactor their CSS into something that didn't resemble a dog's breakfast. What resulted was about 9000 lines of CSS spread across 8 files. For a simple site too! (I've only myself to blame for letting it into the codebase.) So yesterday I did something about it.

  * Grabbed HAML/Sass @gem install haml@  (well I was using actually haml already)

  * Created dir for .sass files @mkdir public/stylesheets/sass@

  * Migrate css 2 sass @cd public/stylesheets/@ @css2sass blah.css > sass/blah.sass@

  * remove all .css from my git repo @git rm blah.css@

  * Ignore all .css during checkins Insert this into your .gitignore @public/stylesheets/*.css@

  * Make sure you've got the right stylesheet includes E.g. @= stylesheet_link_tag "blah.css", :media => "all"@

  * Test Fire up your app, and point your browser at it. The .css files will be automagically generated.

  * Refactor the .sass, mercilessly (Go read up on Sass) I saved about 8000K lines of CSS code. Smack that!

RSpec, Authlogic, Factory_girl - TDD goodness

Whilst trying out Authlogic for a new project, I realized I was scooting ahead with a lot of *gasp* untested code. What would Kent Beck say? A quick google search somewhere along the lines of "rails rspec authlogic OMGWTFBBQ" netted this great tutorial from Mathieu. I didnt bother with the resource_controller, but I can confirm the thoughtbot-factory_girl / authlogic / rspec / rspec-rails combo works nicely.

10 Reasons why PHP is Still Better than Ruby ;-)

This post brought a smile to my face. Unless you're workin on a project of your own, producing as little and maintainable code as possible will mainly DRY up your paycheck. You spend less hours copying and pasting, less hours fixing the same bug on ten different lines and less time finding bugs in the first place. But remember: Time equals cash when you're contracted by the hour. So we should be grateful for PHP and frameworks like Zend which make it so ridiculously easy to write the most cumbersome code full of duplications.

(BDD) Testing with Cucumber/Selenium/Webrat

Grappling with the best way to capture our client's requirements in an expressive (and agile way), we are giving Cucumber a serious consideration. I wont go over the ins & outs of Cucumber, as there are plenty of resources already available. Here's a brief of the steps that I followed (and some of the issues I ran into), which should prove useful if you are doing something similar. h4. Install the Cucumber gem @sudo gem install cucumber@ If you're also having trouble with "gem install" (mirror server timeouts), wget the .gem, and build the gem yourself
wget <http://server/path/to/cucumber-0.2.3.1.gem>
git clone git://github.com/aslakhellesoy/cucumber.git
cd cucumber
rake install_gem
h4. Getting Cucumber to talk to Selenium Follow the steps in "Aslaks Setup":http://wiki.github.com/aslakhellesoy/cucumber/setting-up-selenium h4. Install Webrat @sudo gem install webrat@ h4. Start the Selenium RC server selenium Cucumber comes with 2 trivial examples, which you can run; h4. Running the sample Cucumber + Selenium test cucumber ../selenium/features/ Ensure this works first before trying the Webrat test below; h4. Running the sample Cucumber + Selenium + Webrat test To run the example test which includes Webrat, you can also use the rakefile supplied; @../examples/selenium_webrat]$ rake features --trace@ Jackpot Winner! or not. For me at least. I hit the wall with a timeout error, which is probably some missing Webrat config;
23:59:22.555 INFO - Command request: type[webrat=q, rspec] on session fadcc5971c4d47e5aae10c41e03af3b2
23:59:22.578 INFO - Got result: OK on session fadcc5971c4d47e5aae10c41e03af3b2
23:59:22.581 INFO - Command request: waitForCondition[          var element;

          try {
            element = selenium.browserbot.findElement('button=evalregex:/btnG/');
          } catch(e) {
            element = null;
          }
element != null;, 10000] on session fadcc5971c4d47e5aae10c41e03af3b2
23:59:32.607 INFO - Got result: Timed out after 10000ms on session fadcc5971c4d47e5aae10c41e03af3b2
23:59:32.611 INFO - Command request: testComplete[, ] on session fadcc5971c4d47e5aae10c41e03af3b2
Note: if you are also having trouble with timeouts, at time of writing there was an "unpatched bug":https://webrat.lighthouseapp.com/projects/10503/tickets/205-patch-timeout-parameter-passed-incorrectly-to-selenium in Webrat, which can be fixed by a monkey patch. For me this solved one issue, but the above remained. But I personally prefer the syntactic sugar that Webrat gives me, so its worth getting this working. click_button('btnG') versus @browser.click 'btnG' h4. Other Methods In my quest for Cucumber Glory, I did stumble across these useful, related resources; # "How To Setup RSpec, Cucumber, Webrat, RCov and Autotest on Leopard":http://www.claytonlz.com/index.php/2009/04/how-to-setup-rspec-cucumber-webrat-rcov-and-autotest-on-leopard/ # "Testing Non-Ruby Applications with Cucumber":http://www.vitarara.org/cms/testing_non-ruby_applications_with_cucumber # "Using Cucumber for Acceptance Testing":http://www.pathf.com/blogs/2009/03/using-cucumber-for-acceptance-testing/ # "Cucumber...with webrat mechanized sessions":http://www.pathf.com/blogs/2009/03/using-cucumber-for-acceptance-testing/comment-page-1/#comment-6101 ## And oelmekkis "env.rb":http://gist.github.com/82813 h4. My currently installed Gems FYI my gem versions at time of writing;
*** LOCAL GEMS ***

actionmailer (2.3.2, 2.2.2)
actionpack (2.3.2, 2.2.2, 1.13.6)
actionwebservice (1.2.6)
activerecord (2.3.2, 2.2.2, 1.15.6)
activeresource (2.3.2, 2.2.2)
activesupport (2.3.2, 2.2.2, 1.4.4)
builder (2.1.2)
cucumber (0.2.3.1, 0.1.12)
diff-lcs (1.1.2)
fastercsv (1.4.0, 1.2.3)
hoe (1.12.1)
mechanize (0.9.2)
nokogiri (1.2.3)
polyglot (0.2.5)
rails (2.3.2, 2.2.2)
rake (0.8.4)
rspec (1.1.12)
ruby-oci8 (1.0.4)
rubyforge (1.0.3)
Selenium (1.1.14)
selenium-client (1.2.14)
syntax (1.0.0)
term-ansicolor (1.0.3)
treetop (1.2.5)
webrat (0.4.3)