Cucumber Table transformations with Factory Girl

Problem
After reading this post here, to be able to use the Cucumber table trasformatios feature to build objects in your cucumber tests, you would like to use FactoryGirl instead of the standard model.
The reason for using FactoryGirl could be that your model needs a few more attributes (mandatory fields), that you don’t want to specify in your cucumber table, but you want the factory to take care of them.

Solution
The only difference would take place in the transformation step.
So if you would originally have the following:

Transform /^table:Vehicle,User,Start,End$/ do |table|
  table.hashes.map do |hash|
    vehicle = Vehicle.create!({:regno => hash['Vehicle']})
    user = User.create!({:first_name => hash['User']})
    booking = Booking.create!({:start_at => hash['Start'],
                               :end_at => hash['End']})
    {:vehicle => vehicle, :user => user, :booking => booking}
  end
end

and for example your user has also fields like password, date of birth, that are mandatory in the model validations, you would put them in your factory define declaration like:

Factory.define(:user) do |u|
u.first_name "Name"
u.last_name "Lastname"
u.address1 "Address1"
u.address2 "Line 2"
u.address3 "Line 3"
u.zip "1111"
u.city "City"
u.sequence(:email) {|n|"teste#{n}@test.com"}
u.profile_picture File.open(File.join(Rails.root,"features/fixtures/user.png"))
u.password "password"
end

and replace the tranformation to:

 

Transform /^table:Vehicle,User,Start,End$/ do |table|
table.hashes.map do |hash|
vehicle = Factory(:vehicle, :regno => hash['Vehicle'])
user = Factory(:user,:first_name => hash['User'])
booking = Factory(:booking, :start_at => hash['Start'],
:end_at => hash['End'])

{:vehicle => vehicle, :user => user, :booking => booking}
end
end

The actual step would similar to the one in the blog article:

Given /^the following bookings?:$/ do |table|
# table is a Cucumber::Ast::Table
table.each do |group|
booking = group[:booking]
associations = {:vehicle => group[:vehicle], :user => group[:user]}
booking.update_attributes(associations)
end
end

Secure Random passwords in Ruby

Problem
You would like to use a random password in ruby. Most solutions describe using the Digest library (Digest::SHA1).

Solution
According to the post here, you can achieve the same by using ActiveSupport’s secure_random library.
To use it in Ruby outside Rails use:

require 'active_support/secure_random'
ActiveSupport::SecureRandom.hex(10)
ActiveSupport::SecureRandom.base64(10)


and inside rails:

SecureRandom.hex(10)

passenger ArgumentError (invalid byte sequence in US-ASCII)

Problem
After upgrading your server to use rvm and Ruby 1.9 you get the following error when you are trying to access certain parts of your redmine installation:

passenger ArgumentError (invalid byte sequence in US-ASCII)

Solution
If you follow the instruction posted here, you can add the following to your redmine’s “RAILS_ROOT/config/initializers/string_encodings.rb”:

Encoding.default_external = 'UTF-8'

and restart your application

Passenger (mod_rails) gem installation – Apache2 – Debian – rvm

Problem
You would like to (re)-install passenger after a system wide rvm installation, but you only have the mod_passenger.c file and not the .so after the gem installation.

Solution
Make sure that after using gem install passenger you also use the command to install the apache2 passenger module:

passenger-install-apache2-module

you should then be able to see the .so file and add it to your /etc/apache2/mods-available/passenger.load

Could not find generator jquery:install

Problem
When you try to replace prototype with jquery in your Rails 3.0.x application, by using the command described in the Agile Web development book:

rails generate jquery:install --ui --force

you get the following error:

Could not find generator jquery:install

Solution
You would need to follow the steps below:

  1. Add gem “jquery-rails”, “~> 1.0.13” in your Gemfile, and run bundle install
  2. run the command described above: rails generate jquery:install –ui –force

Now you should be able to see something like:

remove public/javascripts/prototype.js
remove public/javascripts/effects.js
remove public/javascripts/dragdrop.js
remove public/javascripts/controls.js
copying jQuery (1.6.2)
create public/javascripts/jquery.js
create public/javascripts/jquery.min.js
copying jQuery UI (1.8.14)
create public/javascripts/jquery-ui.js
create public/javascripts/jquery-ui.min.js
copying jQuery UJS adapter (cd619d)
remove public/javascripts/rails.js
create public/javascripts/jquery_ujs.js

Formatting edit field values in a Rails edit view

Problem
You have an edit form that displays a value in a format (ie dates) that you want to modify by using a helper method (ie display_datetime).

Solution
According to the suggestion here, you can use the following in your view:

<%= f.text_field :date_field_to_format, :value => display_datetime(f.object.date_field_to_format) %>

Testing associations in Rspec

Problem
You would like to test your model associations in an RSpec model spec, to make sure that they are correctly set up.

Solution
After some Google searching and coming across this article here, ended up using the second method described there with the reflect_on_association method, as it doesn’t need the inclusion of another gem.
So for example to to test multiple has_many or belongs_to associations in your model rspec you could use something like:
context "check associations" do
# belongs_to associations
%w{bel_to1 bel_to2}.each do |bt|
it "should belong to #{bt}" do
m = Model.reflect_on_association(:"#{bt}")
m.macro.should == :belongs_to
end
end
# has_many associations
%w{has_many1 has_many2 has_many3}.each do |hm|
it "should have many #{hm}" do
m = Model.reflect_on_association(:"#{hm}")
m.macro.should == :has_many
end
end
end

The single-table inheritance mechanism failed to locate the subclass:

Problem
You have correctly setup your Single Table Inheritance tables as in:

class Parent < ActieRecord::Base ; end class Child < Parent ; end

but everytime you are trying to use that in a cucumber feature, or in rails console you get the following error:

The single-table inheritance mechanism failed to locate the subclass: 'child'. This error is raised because the column 'type' is reserved for storing the class in case of inheritance. Please rename this column if you didn't intend it to be used for storing the inheritance class or overwrite Parent.inheritance_column to use another column for that information. (ActiveRecord::SubclassNotFound)

Solution
Make sure that when use the subclass name in a feature, or you create a record in the database the initial letter is in capital!
So make sure that in this case the type column contains 'Child' and not 'child'

Google-Maps-Rails (gmaps4rails) does not display markers when upgrading to 0.10.0

Problem
When upgrading the gmaps4rails gem to version 0.10.0 the markers are not displayed any more on the map.

Solution
There is an issue raised about the bug here.
To make the markers appear downgrade to version 0.9.1 of the gem.

update: or upgrade to 0.10.2 as the bug is fixed.