Posts

Embedding flash video in html

Mittwoch, 27. April 2011, 21:30 Uhr | roberto@vasquez-angel.de |

There is a flash video player for html embedding here.

A embed code generator for the player can be found here.

Installing rails on windows

Mittwoch, 27. April 2011, 16:50 Uhr | roberto@vasquez-angel.de |

Prerequisites

You’ll need an actual java version.

Install jruby

Download the ruby installer here and install to a directory of your choice (i.e. c:/rubies/jruby_xxx).

Install pik

Go get pik: Download site and run the installer.

Install rails

$> gem install rails

Done!

Rails3 TDD: Capybara != Webrat

Montag, 25. April 2011, 21:37 Uhr | roberto@vasquez-angel.de |

Capybara is not a drop-in replacement and does not have the same API. I’ll collect differences here:

Webrat Capybara
response.should page.should
contain have_content

Other gotchas: * fill_in does not work with symbols as the fieldname on capybara!

Rails3: form_for and namespaced controllers

Montag, 25. April 2011, 20:36 Uhr | roberto@vasquez-angel.de |

Assume you have a Customer model and a namespaced controller for handling customers:

class Platform::Backend::CustomersController < Platform::BackendController
  .
  .
  .
end  

Then, the right form_for tag is the following:

<%= form_for([:platform_backend, @customer]) do |f| %>
  .
  .
  .
<% end %>

Stack level too deep with cucumber and rspec

Montag, 25. April 2011, 12:35 Uhr | roberto@vasquez-angel.de |

When you get a “Stack level too deep” error, for all step definitions:

Feature: Create a customer
  
  So that a new customer can access his archive
  As a platform admin
  I want to be able to create a new customer

  Scenario: Create a customer                                   # features/platform_backend/create_a_customer.feature:7
    Given I am logged in                                        # features/platform_backend/step_definitions/create_a_customer_steps.rb:1
    When I create the customer John Doe Company                 # features/platform_backend/step_definitions/create_a_customer_steps.rb:8
      stack level too deep (SystemStackError)
      features/platform_backend/create_a_customer.feature:9:in `When I create the customer John Doe Company'
    Then I should see the John Doe Company in the customer list # features/platform_backend/step_definitions/create_a_customer_steps.rb:15

Failing Scenarios:
cucumber features/platform_backend/create_a_customer.feature:7 # Scenario: Create a customer

1 scenario (1 failed)
3 steps (1 failed, 1 skipped, 1 passed)
0m2.483s

you have been bitten by a bug in rspec v2.5. You can solve this, by specifying a later version in your Rails.root/Gemfile:

group :test, :development do
  gem 'rspec', ">= 2.6.0.rc2"
  gem 'rspec-rails'
end  

Rails: routing root to a placeholder without controller

Sonntag, 24. April 2011, 12:53 Uhr | roberto@vasquez-angel.de |

In your Rails.root/config/routes.rb:

Archive::Application.routes.draw do
  .
  .
  .
  root :to => proc { |env| [200, {}, ["Welcome"]] }
end

Rails3: Applying an application template

Samstag, 23. April 2011, 14:06 Uhr | roberto@vasquez-angel.de |

Applying a rails application template after having created the applicaton:

rake rails:template LOCATION=/path/to/template

Cucumber: organizing features and steps

Samstag, 23. April 2011, 13:54 Uhr | roberto@vasquez-angel.de |

I’ve just written my first feature in cucumber and I’m thinking about the best way to organize features and steps.

I’ll give a try at following structure:

Rails.root
|-features
  |-[business_need]
    |-[feature_n].feature
      |- step_definitions
        |- [feature_n]_steps.rb

In reality, it looks like this:

Rails.root
|-features
  |- platform_backend
    |-create_a_customer.feature
      |- step_definitions
        |- create_a_customer_steps.rb

Cucumber and autotest

Samstag, 23. April 2011, 12:05 Uhr | roberto@vasquez-angel.de |

To have autotest run your cucumber features, you habe to set AUTOFEATURE to true, before running it:

$> AUTOFEATURE=true
$> autotest

Rails3: Installing cucumber

Samstag, 23. April 2011, 11:50 Uhr | roberto@vasquez-angel.de |

Modify your Rails.root/Gemfile:

group :test do
  gem 'cucumber-rails'
  gem 'capybara'
  gem 'database_cleaner'
end

Install your bundle:

$> bundle install

Install cucumber with rspec and capybara:

$> rails g cucumber:install --capybara --rspec

Japanese candlestick chart cheat sheet

Mittwoch, 20. April 2011, 23:49 Uhr | roberto@vasquez-angel.de |

babypips.com has a japanese candlestick chart cheat sheet here

Ruby: Handling zip files

Mittwoch, 20. April 2011, 11:00 Uhr | roberto@vasquez-angel.de |

There is a zip gem for Ruby. You can find it here

Creating zip files

First, you have to install the zip gem:

$> gem install zip

or, when using rails, you can add it to your Rails.root/Gemfile:

gem 'zip'

Then you can do something like this:

require 'zip/zip'

Zip::ZipFile.open("output.zip", Zip::ZipFile::CREATE) do |zip_file|
  zip_file.add "filename_in_the_zipfile.txt", "some_source_file.txt"
end

Ruby: Line based file access

Mittwoch, 20. April 2011, 07:18 Uhr | roberto@vasquez-angel.de |

Counting lines

I did find some fancy examples, but I wanted to have it look nice, so I came up with this:

File.new('some_file.txt', "r").lines.count

Access a specific line

File.open('some_file.txt', "r") do |file|
  nth_line_content = file.lines.entries[n]
end

Creating a rails3 application with jruby

Dienstag, 19. April 2011, 12:15 Uhr | roberto@vasquez-angel.de |

Create the application:

$> rails new [APP_NAME] -d [DB_ADAPTER] -m http://jruby.org/rails3.rb

Add warbler to your app. Edit Rails.root/Gemfile:

group :development do
  platforms :jruby do
    gem 'warbler'
  end
end

Install your bundle:

$> bundle install

Check the installation:

$> rails server

Open http://localhost:3000.

Deploying to Tomcat

Using sqlite3

If you are using sqlite for production, you may want to include the Rails.root/db to the war file. To do this, you’ll have to create a warble config file:

$> warble config

Then, edit Rails.root/config/warble.rb:

Warbler::Config.new do |config|
  config.dirs = %w(app config db lib log vendor tmp)
  .
  .
  .
end

Packiging and move to production

Convert the application to a .war file:

$> warble

Install Tomcat. Copy the .war file to the tomcat/webapps folder. Open a browser at http://localhost:8080/manager (admin:admin) and enable your application.

Open http://localhost:8080/[APP_NAME]

Gotchas

  • The production log is written to [TOMCAT]/log/localhost.YYYY-MM-DD.log

Hausverbot gegen die GEZ rechtens

Dienstag, 19. April 2011, 07:30 Uhr | roberto@vasquez-angel.de |

Wie golem.de berichtet ist ein ausgestelltes Hausverbot gegen die GEZ rechtens und wirksam.

Das ist doch mal eine gute Nachricht. Leider kommt das Urteil (und auch die Klage) etwas spät, da die Zeiten der GEZ-Belästigungen sowieso bald vorbei sein sollten, da bald jeder Haushalt pauschal zahlen “darf”.

Blueprint CSS Generator

Montag, 18. April 2011, 12:41 Uhr | roberto@vasquez-angel.de |

There is a Blueprint CSS generator at ianli.com

Update #3: Ostern, *Feier*-Tage und das Frankfurter Ordnungsamt

Montag, 18. April 2011, 10:09 Uhr | roberto@vasquez-angel.de |

Es gibt einen neuen Bericht der FR zu diesem Thema: Kirche für strengere Kontrollen zum Tanzverbot

Rails3: nested_scaffold gem

Sonntag, 17. April 2011, 22:52 Uhr | roberto@vasquez-angel.de |

I often use nested resources in rails. And I like to use scaffolds to have a start, when developing. But there is no nested scaffold in rails 3 by default.

Akira Matsuda has written one

FactoryGirl: Defining associations

Sonntag, 17. April 2011, 22:00 Uhr | roberto@vasquez-angel.de |

Assume you have projects that belongs to a customer. You can define a factory like that:

Factory.define(:customer) do |customer|
  customer.email    "customer@example.com"
  customer.password "foobar"
end

Factory.define(:project do |project|
  project.name "Example project"
  project.description "This is an example project"
  project.targeted_at DateTime.now.advance(:days => 7)
  project.association :customer, :factory => :customer
end

Adding pretty URIs to your blog

Samstag, 16. April 2011, 13:18 Uhr | roberto@vasquez-angel.de |

To add pretty urls to your blogs posts, you can use the friendly_id gem, that can be found here.

The documentation is really good, and installing was straight:

1.Installation

Add the gem to your application. In your Rails.root/Gemfile:

gem "friendly_id", "~> 3.2.1"

Install your bundle:

$> bundle install

Run the friendly id installer:

$> rails g friendly_id

Migrate:

$> rake db:migrate

2.Adding slugs to the post model

Edit Rails.root/app/models/post.rb:

class Post < ActiveRecord::Base
  has_friendly_id :title, :use_slug                    => true, 
                          :approximate_ascii           => true,
                          :ascii_approximation_options => :german # support for german umlauts

Generate the slugs for existing posts:

$> rake friendly_id:make_slugs MODEL=Post

3.Making your posts accessible through nice URIs

There is no step three :)

Rails3: Switch between locales

Freitag, 15. April 2011, 11:47 Uhr | roberto@vasquez-angel.de |

Assume you have a i18n toolbar on your page. To switch between locales, without switching the current page, you can link to the locale without setting any path:

  <%= link_to image_tag("application/icons/german.jpg"), url_for(:locale => :de) %>
  <%= link_to image_tag("application/icons/english.jpg"), url_for(:locale => :en) %>

Ruby: The set union operator for arrays

Freitag, 15. April 2011, 11:02 Uhr | roberto@vasquez-angel.de |

I hereby promise, that i’ll never forget the “Set Union” operator again.

The Ruby API says: Set Union—Returns a new array by joining this array with other_ary, removing duplicates.

   [ "a", "b", "c" ] | [ "c", "d", "a" ]
          #=> [ "a", "b", "c", "d" ]

</cite>

Rails3: How to hightlight the current link

Freitag, 15. April 2011, 10:21 Uhr | roberto@vasquez-angel.de |

Highlighting the current link is a pattern, that you’ll need on almost all pages.

I googled the problem and got to this page. Some guy posted a comment with a link to here. I liked the approach from the moment i saw it. Unfortunately it didn’t work “out-of-the-clipboard” (seems to be for rails 2.x), so I had tweak it.

Here is what I came up with:

Rails.root/app/helpers/application_helpers.rb:

module ApplicationHelper
  class CurrentPageDecorator
    def initialize(helper,options)
      @helper = helper
      @html_class = options[:class] || 'active'
    end
    
    def link_to(*args,&blk)
      name = args.first
      options = args.second || { }
      html_options = args.third || { }
      if @helper.current_page?(options)
        html_options[:class] = (html_options.has_key?(:class)) ? "#{html_options[:class]} #{@html_class}" : @html_class
      end   
      @helper.link_to(name,options,html_options,blk)
    end
  end

   
  def highlight_current_link(options = { },&blk)
    raise ArgumentError unless block_given?
    yield CurrentPageDecorator.new(self,options)
    nil
  end  
end

In your views:

<ul>
<%= highlight_current_link do |n| %>
  <li class="span-6" id="clinic">
    <%= n.link_to 'Klinik', template_path("praxis/clinic"), :id => 'clinic_link' %>
  </li>
  
  <li class="span-6" id="office_tour">
    <%= n.link_to 'Office Tour', template_path("praxis/office_tour"), :id => 'office_tour_link' %>
  </li>
<% end %>  
</ul>

To pass a custom html class for the active link you can do: <%= highlight_current_link(:class => ‘on’) do |n| %> . . . <% end %>

Programming language popularity Index

Donnerstag, 14. April 2011, 09:47 Uhr | roberto@vasquez-angel.de |

There is a programming language popularity index @ TIOBE Software

The Ruby Toolbox

Donnerstag, 14. April 2011, 09:31 Uhr | roberto@vasquez-angel.de |

Sometimes it’s difficult to find the best solution or gem for a specific problem in ruby. The Ruby Toolbox has statistics about the popularity of gems orderes in categories:

The Ruby Toolbox