Tuesday, May 22, 2012

Cucumber on rails

The purpose of this document is the following

  • Setup cucumber with rails
  • Define basic features
  • To test ajax response
The version for gems that are used to create the documents are,
  • cucumber (1.2.0)
  • cucumber-rails (1.3.0)

Setup

Cucumber-rails gem makes it easier to install cucumber in rails, but I faced some issues which I would like to document. The following is from the cucumber-rails document,
group :test do
  gem 'cucumber-rails', :require => false
  # database_cleaner is not required, but highly recommended
  gem 'database_cleaner'
  gem 'cucumber-rails-training-wheels'
  gem 'capybara'
  gem 'rspec-expectations'
  gem 'fabrication' #for test fixture creation
end
Then run,
bundle install
rails generate cucumber:install
This will generate the necessary file for cucumber.
  create  config/cucumber.yml
  create  script/cucumber
   chmod  script/cucumber
  create  features/step_definitions
  create  features/support
  create  features/support/env.rb
   exist  lib/tasks
  create  lib/tasks/cucumber.rake
If you are using mongo, then it should give you a warning that you can not use 'transaction' strategy for mongo. Then you can change that to 'truncation' in your 'features/support/env.rb' file. Change the env.rb as following,
DatabaseCleaner.strategy = :truncation
Now it is time to write some features to test. cucumber-rails-training-wheels gem help you with generators, so that you can generate features quickly. To generate use the command as follows,
rails generate cucumber_rails_training_wheels:feature post title:string body:text number:integer published:boolean
With this plug-in you can generate cucumber feature when you are using scaffold generator. For details please see the reference.
I am working on a sample cucumber project that can facilitate newbies to get started with cucumber and get to know the best practices. Any contribution is very welcome.

Global fixture setup and tear down

If you have some global data that is needed to be loaded for every test then you can use cucumber hooks. You can create the fixtures that you always need, for an example users for login or initialize admin global settings.

Reference

Wednesday, May 2, 2012

Parameterizing translation files in rails I18n

In en.yml,

delete_confirm_with_title: 'Are you sure, you want to delete "%{title}"?'

In erb
<%= link_to 'X', resource, confirm: t('delete_confirm_with_title', {:title => resource.name}), method: :delete %>

Friday, April 27, 2012

Date time stamp using jquery ui framework

If you are using jquery ui framework then you can show you date time in a cool way. The final result will look like this,




It will of course depend on what theme you are using. Here it goes.

HTML

Apr ´10
25th

1:05 pm


CSS

.date {
 width: 55px;
 text-align:center;
 font-family: cursive;
}

.date .ui-widget-header {
 padding: 2px 5px 2px 5px;
 font-size: .9em !important;
}

.day, .time {
 font-size: .8em !important;
}

.date hr {
 margin: 0px;
 border: 0;
 height: 1px;
 background-image: -webkit-linear-gradient(left, rgba(0,0,0,0), rgba(31,52,60,0.75), rgba(0,0,0,0)); 
 background-image:    -moz-linear-gradient(left, rgba(0,0,0,0), rgba(31,52,60,0.75), rgba(0,0,0,0)); 
 background-image:     -ms-linear-gradient(left, rgba(0,0,0,0), rgba(31,52,60,0.75), rgba(0,0,0,0)); 
 background-image:      -o-linear-gradient(left, rgba(0,0,0,0), rgba(31,52,60,0.75), rgba(0,0,0,0));
}

sup, sub {
 vertical-align: baseline;
 position: relative;
 top: -0.4em;
}
sub { top: 0.4em; }

The style of hr element will of course vary for different theme.

Thursday, April 12, 2012

Using helper method in Rials 3 controllers

Here is a rails 3 way to access you helper methods from rails3 controllers.

# app/controllers/posts_controller.rb
class PostsController < ActionController::Base
  def show
    # ...
    tags = view_context.generate_tags(@post)
    email_link = view_context.mail_to(@user.email)
    # ...
  end 
end

# app/helpers/posts_helper.rb
module PostsHelper
  def generate_tags(post)
    "Generate Tags"
  end
end

Found the solution from the blog of Samnang Chhun.

Reference

Friday, March 9, 2012

Rails JSON mapping

Rails to_json is great when you are using web service to talk to your own application. But sometimes when you expose service for some third party application, then you need to export you own data in their format. In my can I had to expose my data to be able to use fullcalendar. And fullcalendar expects data in their format. So thanks to the discussion here, I found a way to represent your in the following way,

  def story_feed
    @stories = @project.stories
    render :json => @stories.map { |story| 
      { :id => story.id,
        :title => story.title,
        :start => story.start_at || Date.today,
        :end => story.complete_at || Date.today }
    }
  end