Contentful on Rails: two fresh gems

Blobpostheaders contentfulonrailsgems

We've reworked two gems which simplify building Contentful-powered Rails apps — contentful_model and contentful_rails — and are eager to tell you all about them.

First off, some history

Two years ago our friends from Error Studio created contentful_model and contentful_rails — two gems to make your Rails + Contentful development as easy as it can get, using what you already know from Rails to power your Contentful projects. Since then many many features have been added to the project, allowing Rails developers to fetch content directly from Contentful in the Rails way.

Revealing the hidden gems

Sadly, Error discontinued development in late 2015. We still wanted to continue providing support for the active users, so early this year we got the ownership of these gems and started building some long-requested features.

The future is green (just as our tests)

Today we're glad to relaunch (in a beta mode for now) the contentful_model gem, which now includes content migrations and entry editing via the CMA. The gem is extensively covered with tests, and the documentation has also been refined.

contentful_model features

Simple ActiveRecord-like API for defining content models

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class MyEntry < ContentfulModel::Base
  self.content_type_id = 'my_entry'

  has_many :other_entry
  has_one :some_cool_entry
  # .. and more ..
end

# Get all entries
MyEntry.all.load

# Get a single entry
MyEntry.find('some_id')

# Search by field
MyEntry.find_by(foo: 'some value').load

# Full Text Search [by field]
MyEntry.search(foo: 'some value inside my text').load

# Full Text Search [all fields]
MyEntry.search('some text inside any field').load

Content editing/publishing with the CMA integration

1
2
3
4
5
6
7
8
my_entry = MyEntry.find('some_id')
my_entry.foo = 'bar'
my_entry.save # Saves edits as a draft

my_entry.publish # Publishes entry

# You can also chain the commands
my_entry.save.publish # Will save and then publish

Content model migrations compatible with ActiveRecord::Migration

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class CreateFooContentType < ActiveRecord::Migration
  include ContentfulModel::Migrations::Migration

  def up
    create_content_type('foo') do |ct|
      ct.field('bar', :symbol)
      ct.field('baz', :date)
    end

    add_content_type_field 'foo', 'foobar', :integer
  end

  def down
    remove_content_type_field 'foo', 'foobar'
  end
end

What's coming next

As most things in life, these gems will always be getting better. We plan to extensively cover contentful_rails with tests and add some more helpers to help you build things faster.

Another aspect of our roadmap is the feedback-driven improvements — we're attentively reading your tickets and listening to everything you have to say to make these gems ready for a final release.

Your contributions are welcome

You can use these gems inside your apps and provide us feedback for contentful_model and for contentful_rails — just open an issue on GitHub or submit a pull request. We're looking forward to what you have to say!

There's more for the Rails crowd

If you're looking for more ways to integrate Contentful with Rails apps, there's a nice tutorial which will help you get started.

Blog posts in your inbox

Subscribe to receive most important updates. We send emails once a month.