Ruby on Rails 4 quickstart

This is a whistle-stop tour of the rooftop and rooftop-rails gems.

We use a lot of Ruby and Rails at Error Agency, where Rooftop was concieved. So there's a pretty thorough client implementation for both general Ruby (for use with Sinatra, Middleman etc.) and Rails apps.

🚧

Some assumptions

This tutorial assumes that you're familiar with the structure and function of a pretty standard Rails app. If you're not, dig into that first. We're also assuming you want to see something running. We'll gloss over any magic, and unpack the detail other tutorials.

There's a repo to accompany this guide: https://github.com/rooftopcms/rooftop-rails-demo-app. Each step has a tag.

1. Set up a new Rails project

We'll assume you're familiar enough with Rails to install it and get it running locally.

https://github.com/rooftopcms/rooftop-rails-demo-app/releases/tag/step_1

2. Add the Rooftop gems

There are 2 gems you need to add to your Gemfile.

gem 'rooftop'
gem 'rooftop-rails'

Don't forget to bundle after adding them.

https://github.com/rooftopcms/rooftop-rails-demo-app/releases/tag/step_2

3. Generate your Rooftop initializer

The connection to Rooftop is configured in an initializer. There's a Rails generator in the gem to make it easy to add.

There are optional switches to pass in your API token and site name. If you don't add them here, you can edit the initializer later in config/initializers/rooftop.rb.

rails g rooftop --api-token 7e4a13513520946cc96ce6f4068a1a93 --site-name rooftopdemo

❗️

API keys should be secret

You'll notice that if you follow this tutorial, the key will work. We periodically wipe and regenerate this demo site. You should keep your real API keys safe.

https://github.com/rooftopcms/rooftop-rails-demo-app/releases/tag/step_3

4. Add model classes

For models backed by Rooftop post types, just create normal Ruby classes with the appropriate Rooftop mixin. There are 2 mixins: Rooftop::Page includes stuff related to pages (including nesting), and Rooftop::Post includes stuff related to posts.

Here's the code for a Page class:

class Page
  include Rooftop::Page
end

And here's how you create the model for a custom post type - we've called ours 'News Article' in Rooftop:

class NewsArticle
  include Rooftop::Post
end

https://github.com/rooftopcms/rooftop-rails-demo-app/releases/tag/step_4

5. Try out your integration in the console

At this point, you're hooked up to Rooftop and you'll be able to get data using the Rails console.

rails c
#return the first page from the demo Rooftop site
Page.first
#return the first news article from the demo site
NewsArticle.first