Blog tech

Manage your metas with metaslug gem

Rédigé par Martin Catty | 14 octobre 2014

Metaslug is a gem designed to let you easily manage your metas, based on a locale and an URL (a path to be more accurate).

Why metaslug ?

For quite some time we’ve been looking for a gem to let us manage metas of our applications outside the codebase with these simple requirements:

  • Metas are based on a path (slug)
  • Metas can be different according to the locale
  • Being a developer should not be a requirement

By metas we mean: title, description, opengraph tag…

We used metamagic which does the job but we still need to manage our metas inside the differents views and have to deal with the locale.

How it works

Our README is a good starting point.

After installing the gem with bundler (gem metaslug in your gemfile) you will generate your locales files based on your routes and use our helper metaslug in your layout.

Metaslug uses simple YAML file to store metas in config/metaslug/{en,fr…}.yml. Your locale file will contain a defaut section, allowing you to set default metas for every page without explicit ones.

The syntax is exactly the same as in the routes, with parameters like :id or so.

Dynamic metas

Of course your YAML file can have dynamic content. We use liquid to let you set your content dynamically.

In the YAML it will result in something like:

en:
  "/posts/:id/edit":
    title: "Edit post "

In your model you’ll have to define the methods accessible by liquid:

class Post < ActiveRecord::Base
  liquid_methods :title
end

And in your controllers you will define which ivar is accessible with metaslug_vars :post, only: :edit. As you can see metaslug_vars has the same syntax as a before_filter. In fact it is a before_filter.

It just marks these variables as accessible and will interpolate them after the action and before the rendering.

Please notice metas are reloaded for each request in development mode only.

Deep metas

There is no particular limitation for metas. This kind of metas:

<meta property="og:locale:alternate" content="fr_FR" />

Will result in:

"/awesome-page":
  og:
    locale:
      alternate: "fr_FR"

There is no control, you can even define your own meta if you want.

What if everything is dynamic?

So you’re app as a lot of dynamic content, or maybe you use a CMS ? Great! Metaslug let you write something like this:

# catch all slug
"/:slug":
  title: ""
  description: ""

Just remind that the keys are browsed sequentially, you have to put this generic expression at the bottom of your file.

Technical points and roadmap

We actually just compare the request.path to our YAML keys, converted to regexp. It may sound a good idea to reuse the rails router but our goal is to avoid deep coupling with rails.

For the next major version we want to be fully compatible with any rack-based framework.

You can view the YAML files as a current backend solution but it won’t be the only one. It’s still way to complicate for a customer to edit this kind of files and commit them to the repository. Plus it will require to reload the application.

Insteal, we will add a user interface to allow metas edition from the application. To do this we will need to introduce different storage like database, cache…

Conclusion

We really hope Metaslug will help you manage your metas more easily.

Feel free to suggest some feature or even code it and make a pull request.

The Synbioz Team