Thursday, March 26, 2015

Using local assets with save_and_open_page

Previously I talked about how to use save_and_open_page with Cucumber. Unfortunately, when you do this it renders an unstyled page. If you add

Capybara.asset_host = 'http://localhost:3000'

to your spec_helper.rb or Capybara config file, save_and_open_page will use your local server's assets - making it much easier to diagnose issues.

Monday, March 23, 2015

undefined method `capture_haml' for RSpec

If you're using Haml in a Rails project, testing with RSpec, you might encounter the "undefined method 'capture_haml' for ..." error while trying to run your specs. I encountered this error in a view spec when trying to use Simple Form along with Haml. Fortunately, like many other obscure bugs there is a quick and dirty solution: The init_haml_helpers section is necessary to prevent other errors from happening.

Note that all lines are required. If you drop the init_haml_helpers line, you'll get the following error:
ActionView::Template::Error: undefined method `capture_position=' for nil:NilClass
and of course if you don't have any of this, you will get:
ActionView::Template::Error: undefined method `capture_haml' for #<RSpec::ExampleGroups::...

Thursday, March 19, 2015

Using save_and_open_page with Cucumber

Capybara has a nice utility method called save_and_open_page that will save the current page's HTML as a temp file and open it in your browser. This method is available by default in Ruby code (for example in an RSpec feature spec). As Cucumber is not direct Ruby, you can't directly say that. Fortunately, you can say

 Then show me the page  

and you'll get the same effect.

Wednesday, March 18, 2015

How to Validate Regular Expressions in Ruby on Rails Models

ActiveRecord has many awesome validators for common things like presence, numericality, etc. Unfortunately, it is missing one common format - regex, or regular expressions. These are normally saved as string fields and, if not validated, can lead to page errors when your app tries to use a field that contains an invalid regular expression. This is especially the case if you allow the user to input a regular expression. With the addition of one small validation class, you will be able to prevent these sorts of bugs with your models.

The above code adds a validator called RegularExpressionValidator to app/validators/regular_expression_validator.rb. It then adds the validation to the Bear model on the field my_regex_field. With this, if you try and create a Bear with an invalid regular expression, an error will be thrown and the save will not complete.

Monday, March 16, 2015

undefined method `fa_icon` for #<#Class:...

If you're trying to use the Font-Awesome gem in your Rails engine, you may run into this "undefined method `fa_icon for #<#Class:.." error. Fortunately, there is a pretty simple fix.

Add

 include FontAwesome::Rails::IconHelper  

to

 engine_name/app/helpers/engine_name/application_helper.rb  

and the error should go away.

Thanks to Github for the solution.

Thursday, March 12, 2015

How to Kill an "open" Rails Server

If you've force-closed your Rails server instance for whatever reason (like a bug that froze up the server), you might run into issues when you try and restart it. For example, you might get the error "WARN TCPServer Error: Address already in use - bind(2) Exiting" What do you do?

Assuming that you're running on port 3000, find the PID of the process with

lsof -wni tcp:3000

and kill the process with

kill -9 PID

Monday, March 9, 2015

Testing with a Dummy Controller in a Rails Engine

If you have a controller in your dummy app (spec/dummy/app/controllers/dummy_controller.rb) for your Rails engine, you might need to access the list of routes within your spec code. You can do this, surprisingly, with Rails.application.routes.routes!

Thursday, March 5, 2015

Using Bundler in a Rake Task

If you want to use Bundler in a rake task to automate something like bundle install, you can't simply call bundle install in a system call. Fortunately, Bundler provides a method called Bundler.with_clean_env. Using that, you can do something like
 require 'bundler'  
 Bundler.with_clean_env  
  system 'bundle install'  
 end  
to run your task.

Monday, March 2, 2015

AssetFilteredError: Assets filtered out and will not be served

If you've decided to use the Teaspoon gem for running your JS tests, you may have encountered this error when trying to navigate to your test suite:
 Error: Sprockets::Rails::Helper::AssetFilteredError: Asset filtered out and will not be served:   
 add `Rails.application.config.assets.precompile += %w( teaspoon.css )` to `config/initializers/assets.rb` and restart your server  
Unfortunately, this is an issue with Teaspoon and newer (>4.0) versions of Rails. For some reason, the asset paths for Teaspoon are no longer included in the list of assets that are automatically precompiled. Therefore, you must add them to your config/initializers/assets.rb file like so:
 unless Rails.env.production?  
  Rails.application.config.assets.precompile += %w(  
   teaspoon.css  
   teaspoon-teaspoon.js  
   teaspoon-mocha.js  
   # other assets  
  )  
Note: I added a surrounding check for production. Since Teaspoon is a testing gem, you shouldn't be using it in production, nor should you need its assets. If you have assets in this file that you need for production, you can separately add them with another addition to Rails.application.config.assets.precompile.