Thursday, August 8, 2013

Rails 4.0.0, local CI with RSpec, Zeus, and Guard, using PostgreSQL, deployed to Heroku

This post is an initial rambling about what I've been doing on some side projects. Aside from learning about Sinatra, and remembering my SQL-fu commands, I'm getting well acclimated to Heroku, re-enjoying Git, and having a blast working around concepts in RoR.

This is just a mini post about my experience with Hartl's exercises, but using Ruby 1.9.3-p194, rvm, Rails 4.0.0 to build the micro-post blogging engine.

Really, in summation, there were a few tricks that made this experience more painful than necessary.

I should also mention that due to other client work, I'm now on Xcode5 DP3. This is only relevant for the time being. Once iOS7 is out of beta and Xcode5 is RC, then maybe the command line tools will work better. For now, the nokogiri gem refused to install, and that's because the libxml2 and libxlts gems refused to install because something in the gcc from Xcode5 DP3 did not like playing well. That said, I could not run capybara or webrat to utilize have_selector("attribute", "value") in any of my rspec's. It was rather unfortunate, but kept me on my toes.

Basically, my gemfile (after the jump), has a couple of gems commented out b/c of the aforementioned Xcode differences. I did not use webrat (or capybara) or annotate-models. I did use PostgreSQL, which I found easier to manipulate, and though it required some config changes, allowed me to deploy to Heroku with no problems. Because of Heroku, and resetting the db, I had to put the faker gem in the normal list, and not development, b/c Heroku tried to use Faker for resetting the db :(

The one gem I did not anticipate ever having to add was the protected_attributes. Rails 4 removed the mass assignment of attributes for security, and there is plenty of good reason for it (just google 'rails protected_attributes, or 'rails 4 attr_accessible'). To follow Hartl's Rails 3 example in Rails 4, I used the protected_attributes and devise gems.

source ''

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.0.0'

# Faker for dev and heroku
gem 'faker'

# Strong protection
gem 'protected_attributes'
gem 'devise', '3.0.0.rc'

# gravatars
gem 'gravatar_image_tag'

# Pagination
gem 'will_paginate'

# Use postgres as the database for Active Record
gem 'pg'

# Use SCSS for stylesheets
gem 'sass-rails', '~> 4.0.0'

# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'

# Use CoffeeScript for assets and views
gem 'coffee-rails', '~> 4.0.0'

# See for more supported runtimes
# gem 'therubyracer', platforms: :ruby

# Use jquery as the JavaScript library
gem 'jquery-rails'

# Turbolinks makes following links in your web application faster. Read more:
gem 'turbolinks'

# Build JSON APIs with ease. Read more:
gem 'jbuilder', '~> 1.2'

group :doc do
  # bundle exec rake doc:rails generates the API under doc/api.
  gem 'sdoc', require: false

# Use ActiveModel has_secure_password
# gem 'bcrypt-ruby', '~> 3.0.0'

# Use unicorn as the app server
# gem 'unicorn'

# Use Capistrano for deployment

group :development do
  gem 'rspec-rails'
  gem 'guard'
  gem 'guard-rspec'
  gem 'zeus'
  gem 'factory_girl_rails'
  #gem 'annotate-models'
  #gem 'webrat'

group :test do
  gem 'rspec'
  gem 'zeus'
  gem 'factory_girl_rails'
  #gem 'webrat'
# gem 'capistrano', group: :development

# Use debugger
# gem 'debugger', group: [:development, :test]
The other intricate issue I had was FactoryGirl. Using the latest gem with Rails 4 meant that factories have a different flavor. Instead of:

@user = Factory(:user)

it now is:

@user = FactoryGirl.create(:user)

FactoryGirl has some pretty awesome methods, and can do a lot, the only thing I struggled with was putting multiple factories inside the same file. The definitions by Hartl made sense, but needed to be massaged in order to fit. For example, my factories.rb user/admin definitions now look like this:

FactoryGirl.define do
  factory :user do
      sequence(:name) {|n|  "Person #{n}"}
      sequence(:email) {|n| "person_#{n}"}
      password               "foobar"
      password_confirmation  "foobar"

  factory :admin, class: User do
    admin true

Hope these help anyone else using an outdated book to brush up on some skills!

No comments:

Post a Comment