Code Monkey home page Code Monkey logo

contentful_model's Introduction

ContentfulModel

This is a thin wrapper around the Contentful.rb api client library.

It allows you to inherit from ContentfulModel::Base and specify the content type id, and optionally, fields to coerce in a specific way.

Note that this library doesn't allow you to save changes to your models back to Contentful. We need to use the Contentful Management API for that. Pull requests welcome!

Usage

Configure ContentfulModel

Configure ContentfulModel with a block. In a Rails app this is best done in an initializer:

ContentfulModel.configure do |config|
  config.access_token = "your access token in here"
  config.preview_access_token = "your access token in here"
  config.space = "your space id in here"
  config.options = {
    #extra options to send to the Contentful::Client
  }
end

Create a model class

Create a class which inherits from ContentfulModel::Base.

class Foo < ContentfulModel::Base
   self.content_type_id = "content type id for this model"
end

ContentfulModel takes care of setting instance variables for each field in your model. You can optionally coerce fields to the right format - for example dates:

class Foo < ContentfulModel::Base
   self.content_type = "content type id for this model"

   coerce_field birthday: :date
   coerce_field store_id: :integer
end

Queries and Searching

ContentfulModel allows you to chain queries, like ActiveRecord. The options are as follows.

all()

Returns all entries of a particular content type. Requires load() to be called at the end of the chain.

Foo.all.load

offset([integer])

(Also aliased as skip()). Allows you to specify an offset from the start of the returned set. Requires load() at the end of the chain.

Foo.all.offset(2).load

find([id]

Returns the entry of the content type you've called, matching the id you passed into the find() method. Does not require load().

Foo.find("someidfromcontentful")

find_by([hash])

Accepts a hash of options to include in the search, as similar as possible to ActiveRecord version. Note that this doesn't work (and will throw an error) on fields which need the full-text search. This needs fixing.

Foo.find_by(someField: [searchquery1, searchquery2], someOtherField: "bar")

You'll see from the example above that it accepts an array of search terms which will invoke an 'in' query.

Associations

You can specify associations between models in a similar way to ActiveRecord. There are some differences, though, so read on.

There's no belongs_to

Contentful allows you to create relationships between content types - so a parent content type can have one or many child entries. You can (and probably should) validate the content types in your child field - for example, a 'page' might have one entry of content type 'template'.

However: it's not possible to assign an entry only once - so that means that every child entry might belong to more than one parent. So there isn't a belongs_to method because it wouldn't make any sense. Use belongs_to_many instead.

(If you happen to accidentally declare belongs_to on a model, you'll get a NotImplementedError.

### has_one Define a has_one relationship on a parent model, just like you would for ActiveRecord. For example:

A simple example:

class Article < ContentfulModel::Base
    has_one :author #author is the name of the field in contentful
end

A more complex example, with a child model class that doesn't follow the name of the field on the parent.

class Page < ContentfulModel::Base
    has_one :template, class_name: "PageTemplate", inverse_of: :page #template is the name of the field in contentful
end

Page will now have a method called template which returns the PageTemplate you have assigned in Contentful. Similarly, Article will have a method called author which returns the Author.

Provided you've properly set up belongs_to_many on the other end of the relationship, you'll have a utility method on the child model called page(). This is the entity which loaded the child. In most cases this is pretty useful.

### has_many Using has_many is conceptually identical to has_one, so there isn't much to say here.

belongs_to_many

Use belongs_to_many on the other end of a has_one or has_many relationship.

Our Article class above has a child called Author. The author will probably belong to many articles.

(note: you could model this particular example the other way around)

class Author < ContentfulModel::Base
    belongs_to_many :articles
end

Our Page class above has a method called template. This returns a PageTemplate class; we set the inverse here for clarity and to help with setting up some utility methods.

class PageTemplate < ContentfulModel::Base
    belongs_to_many :pages, inverse_of :template
end

Using belongs_to_many gives you a couple of useful methods on the child. Using the Article example above:

  • article() - this is the parent article which called the child author. If you call Author.find() explicitly, this will be nil
  • articles() - this requires an API call, and will return a collection of articles which has this author as a child

has_many_nested - using self-referential content types to create a tree

This is a departure from the classic ActiveRecord syntax here, but pretty useful. There are probably situations where you want to have a content type which has children of the same type. For example, a Error we often has a tree of pages for a website.

In this example let's assume you have a Page content type, which has a field called childPages in Contentful - this is referenced as child_pages in ContentfulModel.

Here's how you'd set it up:

class Page < ContentfulModel::Base
    has_many_nested :child_pages
end

This calls has_many and belongs_to_many on the Page model, and gives you some useful instance methods.

  • parent() - the parent of the current Page
  • children() - the children of the current Page
  • has_children?() - returns true or false depending on whether this page has children
  • root?() - returns true or false depending on whether this Page is a root page (i.e. no parents)
  • root() - returns the root page, from this page's point of view
  • ancestors() - an Enumerable you can iterate over to get all the ancestors. Surprisingly quick.
  • nested_children - returns a nested hash of children
  • nested_children_by(:field) - takes the name of the field you want to return, and returns a hash of nested children by the field you specify. E.g. nested_children_by(:slug).
  • find_child_path_by(:field, "thing-to-search") - returns an array of the child's parents. Useful for determining the ancestors of an entity you've called directly.
  • all_child_paths_by(:field) - return a 2d array of paths for all children. One of a couple of ways you can set up navigation.

From this, you can:

  • Build up a tree from calls to the top-level entity (e.g. a navigation tree)
  • Reverse-iterate up the tree from a given page (e.g. breadcrumbs)

Defining a root page

You can pass an optional second parameter into has_many_nested which means the class knows how to find its root:

class Page < ContentfulModel::Base
    has_many_nested :child_pages, root: -> { Page.find("some_id").first }
end

Adding this second parameter defines a method called root_page on the class, so you can get the root easily. Your proc needs to return one object.

An aside on the Contentful UI

There isn't a way to see the parent of a child entity, when you're looking at the child entity. This is something the Contentful gang are thinking about solving, we hear.

Preview mode

You might want to hit the preview API. Our contentful_rails gem uses it, for example.

Provided you've set a preview_api_token in the configuration block, it's dead easy. Just set ContentfulModel.use_preview_api = true before making calls.

Suppressing unvalidated content in preview

There's a slightly weird piece of logic in the Contentful API when you're using preview mode. It returns content which has failed its own validation!

This is something Contentful are planning to address, but for now, we need to filter these out to avoid breaking stuff on the client side. We've added a validator for 'required', using syntax similar to ActiveRecord:

class Page
    validates_presence_of :slug, :title, :some_other_vital_field
end

If you've defined this in a class, any queries to the API will filter out entities which aren't valid. This applies to both relations (where you might get a collection), or searches.

## Returning nil for fields which aren't defined If an object is valid, but has content missing from a field, the Contentful API simply doesn't return the field, which is frustrating. That means that you have to check manually for its existence to avoid raising a ContentfulModel::AttributeNotFoundError.

We decided it would be nice to be able to declare that certain fields should return nil, rather than raising an error. You can do that as follows:

class Page
    return_nil_for_empty :content, :excerpt
end

This means you can check for content.nil? instead of rescueing from an error. Much nicer.

To Do

There are quite a few outstanding tasks:

Licence

MIT - please see MIT-LICENCE in this repo.

Contributing

Please feel free to contribute. We would love your input.

  • Fork the repo
  • Make changes
  • Commit and make a PR :-)

contentful_model's People

Contributors

martinholmin avatar

Watchers

James Cloos avatar  avatar

Forkers

marklazz

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.