Placement of Dummy App when Creating a Rails Engine

Posted on

Building a Rails Engine and prefer to use RSpec over TestUnit? I do, but I constantly forget how to generate the plugin so that I don’t have to manually move the Dummy App. Here’s the command:

rails plugin new foobar --skip-test-unit --dummy-path=spec/dummy

Rails, Bundler, Gems and Local Git Repositories

Posted on

So your developing Rails project which contains gem dependencies that are also in development and you’re using a path to point to the development gem. Your Gemfile looks something like this:

gem 'GEM_NAME', path: '../GEM_NAME' 

Once you go to deploy, you quickly find out that you have to change all of your paths to map to git repositories. Here’s a trick that will greatly help you out. Bundler allows you to work against local git repositories locally instead of using the remote version. Here’s how you do it.

First, you need to tell bundler where you local gem is:

bundle config local.GEM_NAME /path/to/local/git/repository

Then create an entry in your Gemfile which looks like any other remote gem in a git repository

gem 'GEM_NAME', :github => 'my_git_repo/GEM_NAME', :branch => 'master'

Now you have no more changing of paths when deploying.

Running Migrations From Within Rails Engines

Posted on

We have multiple applications that share a common backend database for one of our clients. We wanted a simple way which we could run migrations across Rails applications without having to copy the migrations in to each application.

Our simple solution to this problem was adding the db/migrate folder of the engine to the config paths of the parent Rails application through an initializer in the Engine. Here is the code:

module Bootstrap
  class Engine < ::Rails::Engine
    initializer "bootstrap.load_app_instance_data" do |app|
      app.class.configure do 
        config.paths['db/migrate'] += Bootstrap::Engine.paths['db/migrate'].existent       

Using ssh-copy-id with a different port than 22

Posted on

All of my deploys are done doing SSH/SCP. I typically authenticate with these servers via a public/private key combination. Furthermore, non of my servers use port 22 for SSH connections. When I started using ssh-copy-id, i needed to specify the port to connect and I did something like this:

ssh-copy-id -p 5555
Bad port 'umask 077; test -d ~/.ssh || mkdir ~/.ssh ; cat >> ~/.ssh/authorized_keys'

Ouch. Its not working the way I expected it to. Here’s how you get it to work:

ssh-copy-id '-p 5555'

Thats much better.

JRuby, Asset Pre-compilation and Threadsafe!

Posted on

Do you have use ERB within your assets? Are you using JRuby and have config.threadsafe! enabled? Well, if you’re getting errors during deployment because the ERB can’t be evaluated correctly make this change to production.rb:

# Enable threaded mode
 config.threadsafe! unless $rails_rake_task

$rails_rake_task will returns true if this is being executed by a rake task. As such, config.threadsafe! will not be turned on when compiling assets.

Setting Up PostgreSQL on Lion (with some newbie info)

Posted on

Due to the benefits of using PostgreSQL over MySQL in Rails 3.1 and above, we have recently made the switch to PostgreSQL where performance is a concern. What I have found is that MySQL is way more familiar to me, and that Postgres introduced some challenges. In this post, I’ll detail how I set up my environment and got my application working.

By default PostgreSQL comes pre-loaded on Macs with Lion. While this is great, you will not have the “latest and greatest” release of PostgreSQL. This will also provide us with some hoops to jump through before our environment is up an running perfectly. We’re going to use Homebrew to install PostgreSQL, as it makes the initial installation dead simple.

homebrew install postgresql

In order to get our new install of PostgreSQL to take precedence over Apple’s installation, we must make adjustments to our PATH. I’ll use Textmate to make the adjustments

mate ~/.bash_profile

Then I’ll make the necessary adjustments to the file.

export PATH=/usr/local/bin:$PATH

The order of statement is important. “/usr/local/bin” must be put before path, otherwise Apple’s installation of PostgreSQL will take precedence over ours. At this point, PostgreSQL should be happily working on our machine.

At this point, we are ready to create a server instance. To do this, we’re going to run the following command:

initdb /usr/local/var/postgres

This is going to create an instance called postgres at the location /usr/local/var/postgres. Before we can start creating databases, we must first start the server. To do this, run the following command:

pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start

This will start PostgreSQL as a daemon in the background. If you only want it to run in a given console instance, you can use “postgres -D /usr/local/var/postgres”

I like to create a user for each development database on my machine. Because this is on my development machine, I’ve create a user without specifying a password. To do this, run the following command in terminal:

create user app_name

The command is going to ask you a few questions, I answer no to all of them.

Finally, we need to create a database. We’re going to specify the user we just created as the owner of the database.

createdb -O app_name app_name_development

Finally, lets make sure you can get in to the database:

psql -U app_name app_name_development

Library not loaded: libmysqlclient.18.dylib (LoadError) on Lion

Posted on

So, I’ve run in to this a few times already. You’ve created a new rails application, added the mysql gem to your bundle file, and managed to get it to install. Then you start up the rails server, and you get an error something like:

Library not loaded: libmysqlclient.18.dylib (LoadError)

Fortunately there is an easy fix. Just run the following from the command prompt:

export DYLD_LIBRARY_PATH=/usr/local/mysql/lib/

Crisis averted.

RIP Steve Jobs

Posted on

“If Apple becomes a place where computers are a commodity item, where the romance is gone, and where people forget that computers are the most incredible invention that an has ever invented, I’ll feel I have lost Apple. But if I’m a million miles away, and all those people still feel those things… then I will feel that my genes are still there.” — Steve Jobs, Newsweek, September 29, 1985

Working With Upstream Git Repositories

Posted on

Because I always forget, here is my simple guide to getting the latest from an upstream repository (one you have forked) on GitHub. Obviously, you first need to fork a project within GitHub, then clone it. For example, here I’ll clone my Bootstrapped gem.

git clone

Next, we’ve got to add a remote site to map to the upstream repository.

git add remote upstream

Finally, when you want to pull down the latest from the upstream repository, run the following command.

git fetch upstream

Bootstrapped-Rails: My First Published Gem

Posted on

A few weeks ago, some fellow developers stumbled upon Twitter’s Bootstrap CSS project on GitHub. All the sudden, it became crazy simple to develop a good looking interface to show as prototypes or to customize and make your own.

During the development of a simple application @justindarc said “Wouldn’t it be cool if Rails just generated Bootstrap compatible code? I’m getting tired of styling each form”. So started the development of Bootstrapped-Rails. Not only does the gem contain the latest version of Bootstrap, but it also has generators based of Ryan Bates’s Nifty Generators to easily generate a basic Bootstrap a layout file and scaffolded forms (sorry, it only supports ERB, no HAML).

Finally, I pulled in jquery.bootstrap.js from @justindarc. This JQuery plugin will add CSS3 animations, close events for modals and other functionally.

To use it, add the following to your Gemfile

gem "bootstrapped-rails", :git => ''

Currently, you need to add the following to application.css in /app/assets/stylesheets

//= require bootstrap
//= require jquery.bootstrap

And finally you need to add the following to application.js in /app/assets/javascript

//= require jquery.bootstrap
//= require jquery.tablesorter

Now your setup and ready to scaffold. Create a layout:

rails g bootstrapped:layout

And create a scaffold:

rails g bootstrapped:scaffold post title:string body:text

And there you have it, Bootstrapped-Rails.