Acts as Graph Object

ActiveRecord extension that maps models to Facebook Open Graph objects (by placing the relevant meta tags into your HTML).

Download .zip Download .tar.gz View on GitHub


gem install acts_as_graph_object

Then just add the dependancy to your Gemfile.



# app/config/initializers/acts_as_graph_object.rb

ActsAsGraphObject.configure do |config|
  config.namespace = 'my-app'
  config.app_id = 12345
  config.admins = [1245, 6789]

Default URL Method

In order to use the built in @model.url method you need to set the following config option:

# app/config/environments/production.rb

routes.default_url_options[:host] = ''

Add acts_as_graph_object...

# app/models/movie.rb

class Movie < ActiveRecord::Base
  acts_as_graph_object :custom => [:director, :writer, :cast]
  def cast

Outputting meta tags

Use the graph_object_tags_for(@movie) helper to output the resulting <meta> tags. You can use this in combination with content_for to push the results into your <head>:

# app/views/layouts/application.html.erb

  <%= yield :meta_tags %>
# app/views/movies/show.html.erb

<% content_for :meta_tags, graph_object_tags_for(@movie) %>

Overriding from view

If you want to override a value from the view (for example to use a url_for helper):

# app/views/movies/show.html.erb

<% content_for :meta_tags, graph_object_tags_for(@movie, :url => movie_url(@movie)) %>

Alternative attribute names (v0.0.7)

You can now use non-default names for the standard Open Graph properties and the Gem will attempt to pick these up. Here are the properties and their alternative names:

# standard object properties & alternative names

default_properties = {
  :title           => [:name, :label],
  :type            => [:kind, :group, :class],
  :image           => [:picture, :photo],
  :url             => [:permalink, :link],
  :description     => [:info, :details],
  :site_name       => [:site],
  :latitude        => [:lat],
  :longitude       => [:lng],
  :street_address  => [:address],
  :locality        => [:locale, :area],
  :region          => [:province, :territory],
  :postal_code     => [:post_code, :zip_code, :zip],
  :country_name    => [:country],
  :email           => [:email_address],
  :phone_number    => [:phone],
  :fax_number      => [:fax]