MacVim, NerdTree, Janus Annoyances
I’ve decided to use Vim for a few weeks at least and am enjoying it. There are plenty of posts on using Vim as a Ruby IDE and switching from your IDE of choice. Most of the TextMate to Vim posts seem to recommend Janus and for good reason.
Janus adds a number of plugins and settings to Macvim including NerdTree. NerdTree displays a project drawer in a window which is in a vertical split, normally alomgside your working buffer. The first problem is that if you close your working buffer with :bd or :bw, the NerdTree window will fill the MacVim tab, to cycle to the next or previous buffer then entails resizing splits which is not what I want.
Janus solves this with a function in .vimrc:
This function closes the MacVim tab (i.e. Cmd + W) if there is only one tab and it contains 2 windows, one of them with the NerdTree buffer. Unfortunately this doesn’t consider buffers that are not currently displayed. If I want to close MacVim or the MacVim current tab, I’ll use Cmd + Q or W, when I delete a buffer I want the window to display the next buffer.
To disable the Janus function add an override to .vimrc.local:
It’s better to make changes in .vimrc.local and .gvimrc.local as Janus can be updated to the latest version without losing your customisations.
Luckily there is a script on the vim wiki that keeps the window open when deleting a buffer. It can be amended to ignore the NerdTree buffer, if I want to close NerdTree I will use the key mapped to toggle it:
I would like to map :bd to :Kwbd but it seems it can’t be done (I’d be happy to be proved wrong here) so have mapped Ctrl + Shift + B in .vimrc.local:
Creating an HTML5 Boilerplate WordPress Theme
I’ve just had a logo and design done for Furld. I am pleased with the design but it was supplied in HTML and CSS rather than HTML5 and CSS3 so I want to convert it at the same time as I turn it into a WordPress theme.

I like HTML5 Boilerplate by Paul Irish and luckily there is a WordPress theme called Roots, which is designed as a jumping off point for your own HTML5 theme.
I have installed WordPress locally to use as a development environment so I just have to copy the roots theme to mysite/wp-content/themes/roots and I can activate it.
After activating the theme I can visit the home page and see this.

Not very pretty but it is HTML5 and now I can start customising it. Rather than customising the theme itself I am going to create a child theme:
- Create a directory mysite/wp-content/themes/roots-child/
- Create a style.css in the directory.
When the Roots Child theme is activated the site should look exactly the same but changes I make in the child theme will override the parent theme. This means the parent theme can be updated by the original author and I will not lose any customisations I make to the child theme.
Installing WordPress on your Mac for Development
The Stack
To run WordPress locally you will need Apache, MySQL, and PHP installed. Although OS X comes with some of these it is more convenient to use MAMP. MAMP comes in a free version and a Pro version, for my purposes the free version should be fine.
Installation is just copying a folder from the disc image to your applications folder and removal is just a case of deleting the folder. Once the folder has been copied just start MAMP which will start the servers and open the home page on port 8888.
From the preferences > Apache pane set the document root to wherever you want to put your sites. I set mine to ~/dev/sites/.
The Database
Click on open start page on the MAMP dialogue then click on phpMyAdmin, enter a database name for your WordPress database then click on create. Click on privileges and create a new user with full privileges for the database.
WordPress
Create a subdirectory for your new web site in your sites directory, download WordPress from wordpress.org and unzip the contents of the download into the subdirectory.
Rename the file wp-config-sample.php to wp-config.php then edit it to fill in the database details and change the salts and keys.
Navigate to http://localhost:8888/ and you should see the subdirectory, click the link and you should get taken to the WordPress install page (if you get error connecting to database it is likely that either your database server is not running of there is an error in your wp_config.php). Fill in the blog title, username, password, and email, hit submit and you are done.
Rails, Compass, HTML5 Boilerplate and Heroku
Posted by John in Ruby and Rails on April 14, 2011
Overview
HTML5 provides some great functionality but also, potentially, some headaches. HTML5 Boilerplate is a template which aims to solve the various cross-browser differences.
There is a gem to use HTML5 Boilerplate in a rails app which uses Compass to build the CSS. Using Compass on Heroku requires a small workaround as Heroku limits where an app can write to.
Setup
For a new Rails App: Follow the process in prior post on setting up a new project.
After the rspec and cucumber installs run
When asked, select yes for change of SASS location but no to change of CSS location
Carry on through prior post (but don’t install jquery as that is part of the HTML5 Boilerplate)
Create a home controller script
Delete the /public/index.html and the app/views/layouts/application.html.erb files
Set the root in the config/routes.rb
Fix the annoying Rails 3.0.5 bug by amending the javascript expansions line in config/application.rb to
The app should now run locally using hml5-boilerplate, compass, and haml.
Heroku
But not on Heroku, to run on Heroku the css files need to be written to a tmp directory as that is the only folder the app can write to.
In config/compass.rb change the css_dir line to
Change config/initializers/compass.rb to
This should allow the app to run locally and hosted on Heroku.
To push to Heroku follow these instructions.
In summary, once you have your SSH keys set up:
Rails – File, New Project…
Posted by John in Ruby and Rails on January 19, 2011
Even with RubyMine starting a new project is not simply File, New Project, like the rails new command, it can only take you so far.
It’s not complicated to set up a new Rails project but, as something you are likely to do farily infrequently, it is easy to skip or forget steps or commands. Charles Max Wood has posted a screencast of his Rails set up at what looks to be the start of a great screencast series.
Here is a terse run down of my setup:
rails new appname -J -T-J skips prototype files, -T skips testunit files.cd appnamervm --create --rvmrc 1.9.2@appnamervm rvmrc trustmvim .or mate . or whatever editor you use.- Amend the /Gemfile (Edit: I’ll update these files as I improve my process, add new gems to the default set and gems are updated/changed)
bundle installrails g rspec:installrails g cucumber:install --capybara --rspec --sporkspork --bootstraprails g jquery:install -uirake db:create- Amend /.gitignore
git flow initAccept the git-flow defaults. I like the structure provided by git-flow and will post on it in the future to explore it more.git add .git commit -m "Project skeleton"git remote add origin git@github.com:JohnPlummer/project_name.gitgit push -u origin developgit push -u origin master
Managing Ruby Gems
Posted by John in Ruby and Rails on December 29, 2010
Ruby gems are packages that add functionality to the core Ruby libraries. Each gem normally provides functionality in a specific area and can depend on other gems. Rails is a Ruby gem. Presumably, in the past, gem versioning caused problems for Ruby devs which has led to the development of a couple of tools:
RVM
RVM stands for Ruby Version Manager and allows a dev to switch betwen versions of Ruby. More importantly it can also sets of gems.
- Gemsets are associated with a particular version of Ruby.
- There is gemset named global (per Ruby version) and gems in this set are available to all other gemsets for that Ruby version.
- To associate a project with a particular gemset, include a .rvmrc file in the project root containing
rvm –create use version@project_name > /dev/null
This will create the gemset if it does not exist and switch to it automatically. - Useful commands to work with gemsets: rvm gemset [list|name|create|use|delete|empty|rename|copy]
Bundler
While RVM is used to manage and switch between installed gems, Bundler is a tool to install gems and associate them with a project.
- Bundler is itself a gem and is installed with Rails 3.
- Project dependencies are listed in a gemfile file in the project root.
- Gems versions can be specified in the gemfile but that is not really necessary with the gemfile.lock file which stores version numbers.
- bundle install ensures all gems listed in the gemfile are installed to the current gemset.
- Gems can be grouped by environment (development | test | production).
- Bundler ensures that the correct gems are ‘required’ by Ruby for the environment it is running under.
- The environment groups allow bundle install to be selective on what to install. e.g. for a production server
bundle install –without development test
RubyMine Gem Management
It looks as though RubyMine will be my environment of choice and it includes it’s own sdk (Ruby version) and gem management system that should integrate well with RVM’s gemsets and Bundler’s gemfile.
- A project can be associated with a particular version of Ruby and a gemset in RM’s settings pane.
- Once a gemset is selected, any gems installed by RM will be installed to this gemset.
- RM should also be able to read a .rvmrc file in the project root and switch automatically to the specified gemset. This currently (RM 3.0.1) seems not to work with newly created gemsets that have not yet had gems installed to them so best to check in the project settings to be sure.
- RM scans the gemfile, offering to install and attach and missing gems.
- Gems that are attached get scanned by RM to enable features such as autocomplete within the IDE.
Rails and Rake Commands and Rails Environments
Posted by John in Ruby and Rails on December 23, 2010
Rails
Rails seems to consist of two parts, a set of components to build an application on top of and a command line tool to help this process. The reason the rails command is so useful is the amount of convention in a Rails app: Certain files with certain names are expected in certain locations. When one file or class is created, the chances are that a number of further files and classes will be required. The most commonly used rails commands are:
rails new
The first command is rails new app_name. This can take a number of options to specify versions, templates, databases, or to skip certain parts of the application. View a full list with rails new –h. Rails new appname –t is possibly useful to skip creating the test folder if you intend to use rspec.
rails server
Runs the default rails dev server, WEBrick. which can be viewed on localhost:3000. Again use –h to view all the options. The –d option runs the server as a daemon and –u enables debugging. One option to be aware of is –e which allows you to specify an environment to run under, more on this later. rails s can be substituted for rails server.
rails generate
Creates boilerplate code for various parts of an application. Installing a gem (such as rspec) can add additional generators. The most common rails generators are probably scaffold, model, controller and migration. rails g can be used in place of rails generate.
- rails g controller controller_name action1 action2 … generates a controller with the specified actions and views to match the actions. e.g. rails generate controller Products buy sell will create app/controllers/products_controller.rb which will contain class ProductsController with methods buy and sell. It will also create the views app/views/products/buy.html.erb and app/views/products/sell.html.erb. The command will also create routes, test classes and helpers.
- rails g model model_name field1:type field2:type … creates a class for the model in app/models/model_name.rb and a database migration file to add a matching table to the database. It also creates a test class and a fixture file (to provide test data).
- rails g migration migration_name creates a migration file with the specified name (preceded by a timestamp). To add colums to an existing table rails g migration add_something_to_tablename field1:type field2:type can be used to create the correct migration automatically.
- rails g scaffold model_name field1:type field2:type … Combines all of the above, creating a model and migration, a controller with basic CRUD actions, matching views, tests, helpers etc. Although this would appear useful it is meant to be more of a learning tool (and useful for the various instant gratification demo’s of Rails).
rails console
Starts a console that can run ruby code. Like the rails server an environment can be specified to run under e.g. rails console test (note this is different to rails server).
Rake
Rake is a ruby command for ‘ruby make’ used for common administration tasks. The most common tasks are to administer databases and run tests.
rake test
Runs the applications tests, a number of options can be used to restrict the tests that are run.
rake db:command
There are a number of db commands but the most used is db:migrate. This command can take further options, one of which is used to specify the rails environment – rake db:migrate RAILS_ENV=test (and a third syntax to specify the environment.
Environments
After running rails new appname, three environments will be available, development, production, and test. The settings for the environments are specified in config/environments /*.rb and as seen above, various commands can specify the environment they run under. Further environments can be created if required e.g. staging.
The default environment for rails server, console and rake db:migrate is development. Tests run in the test environment. Running under other environments is normally done by specifying the environment in the server or hosting configuration.
The rails environment is useful in a number of other places such as:
Testing the environment from code -
<%= debug(params) if Rails.env.development? %>
Loading certain gems for specific environments with groups in the gemfile
source 'http://rubygems.org' gem 'rails', '3.0.3' gem 'sqlite3-ruby', '1.2.5', :require => 'sqlite3' gem 'gravatar_image_tag', '0.1.0' group :development do gem 'rspec-rails', '2.1.0' gem 'annotate-models', '1.0.4' end group :test do gem 'rspec', '2.1.0' gem 'webrat', '0.7.1' gem 'autotest', '4.3.2' gem 'autotest-fsevent', '0.2.2' gem 'autotest-growl', '0.2.8' gem 'autotest-rails-pure', '4.1.0' gem 'spork', '0.8.4' gem 'factory_girl_rails', '1.0' end


