More Cucumber Common Steps and Love

You just have to love this “syntax” 😀
Story

#features/discussion.feature
Scenario: I add to an discussion
    Given I am logged in
    And a "Discussion" exists for "Festival" "1"
    And I am on "Festival" "1"
    When I fill in "Text" with "Halloo"
    And I press "Send"
    Then I should be on "Festival" "1"
    And I should see "Halloo"

Steps

#features/step_definitions/common_steps.rb 
# On page/record
Given /^I am on "([^"]*)"$/ do |path|
  visit path
end

Then /^I should be on "([^"]*)"$/ do |path|
  current_path.should == path
end

Given /^I am on "([^"]*)" "([^"]*)"$/ do |model,number|
  visit polymorphic_path(record_from_strings(model,number))
end

Then /^I should be on "([^"]*)" "([^"]*)"$/ do |model,number|
   current_path.should == polymorphic_path(record_from_strings(model,number))
end

# Existing
Given /^a "([^"]*)" exists for "([^"]*)" "([^"]*)"$/ do |associated,model,number|
  record = record_from_strings(model,number)
  record.send(associated.underscore+'=',valid(associated))
  record.save!
end


# Login
Then /^I should be logged in$/ do
  should be_logged_in
end

Given /^I am logged in$/ do
  visit 'login'
  fill_in 'email', :with=>'quentin@example.com'
  fill_in 'password', :with=>'test'
  click_button 'Login'
end


# Support
def current_path
  response.request.request_uri
end

def record_from_strings(model,number)
  model.constantize.find(:first,:offset=>number.to_i-1)
end

env.rb

#features/support/env.rb
#load all fixtures
include AuthenticatedTestHelper #restful_authentification
include ValidAttributes #http://github.com/grosser/valid_attributes

Save All Empty Strings as NIL

With the  clear_empty_attributes plugin, all empty string worries are gone!

  • Complicate queries for empty fields (WHERE field IS NULL OR field = '')
  • Use of unless field.blank? (opposed to only if field)
  • Late-detected bugs because most of the time columns were filled or '' and suddenly they are nil
  • Some validations do not support :allow_blank=>true
  • Datebases can handle NULL better & faster than '' (especially when using LIKE)

Install

script/plugin install git://github.com/grosser/clear_empty_attributes.git

Now all empty strings will be saves as NULL.

Migrate

To take care of all other/old models we have to run a migration.
Remove any blank strings/texts from your Models:

rake clear_empty_attributes:clear_all_blank_strings MODELS=User,Movie,...

No More Whitespace Worries — Use HAML for Emails!

Tired of all the erb whitespace and those <%-end-%> madness ?

Switch to HAML for emails!!

How to?

  • remove any leading whitespace with capture and gsub
  • use == for easy string replacement
  • use == for newlines

Example

==Hello #{@user},
Your order has been completed on #{Time.now}.
You may now access the bought items:
==
- content = capture do
  - for item in @order.items
    - if item.shipping?
      We will send it to you soon.
    - else
      please download it here:
      ==#{item.orderable} -- #{polymorphic_url(item.orderable)}
    ==

=content.gsub(/^(  )*/,'')
==
=mail_signature

MySql: DEFAULT does NOT prevent NULL

I just came across a serious bug/gotcha when using mysql.
Integer, default 0 -> set to NULL –> NULL and not 0

Remember
always set NOT_NULL and DEFAULT!

Example

change_column :order_items, :shipping, :boolean, :default=>false

#shipping is nil or false or true
OrderItem.find(:all,:conditions=>{:shipping=>false}) => [] 

change_column :order_items, :shipping, :boolean, :default=>false, :null=>false

OrderItem.find(:all,:conditions=>{:shipping=>false}) => [all]

Optional Atrributes for Partials

I just stumbled about a nice and only a little dirty approach to make partial attributes optional.

Helper

#app/helpers/application_helper.rb
def options;{};end
def item;nil;end

View

render :partial=>'xyz', :item=>@user, :options=>{:show_something=>true}
OR
render :partial=>'xyz'

Inside partial

...
text = options[:show_something] if item
...