Skip to content

ActiveRecord extension that turns your models into Facebook's Open Graph objects.

License

Notifications You must be signed in to change notification settings

rubenrails/acts_as_opengraph

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

acts_as_opengraph

ActiveRecord / Mongoid extension that turns your models into facebook opengraph objects.

Installation

gem install acts_as_opengraph

Now just add the gem dependency in your projects configuration.

Usage

Adding acts_as_opengraph for ActiveRecord

# app/models/movie.rb
class Movie < ActiveRecord::Base
	acts_as_opengraph
end

Adding acts_as_opengraph for Mongoid

# app/models/movie.rb
class Movie
  include Mongoid::Acts::Opengraph

	acts_as_opengraph # This should be placed after model fields declaration
end

Generating the opengraph meta tags

# app/views/layouts/application.html.erb	
<head>
	<%= yield :opengraph_meta_tags %>
</head>

# app/views/movies/show.html.erb
<% content_for :opengraph_meta_tags, opengraph_meta_tags_for(@movie) %>

Displaying the Like Button

# app/views/movies/show.html.erb
<%= like_button_for @movie %>

* Notice that the Like Button will retrieve the required href attribute by calling @movie.opengraph_url. Read below for more options.

Options

Database columns

Even when the names of these columns can be changed with configuration, acts_as_opengraph tries to guess these names by checking for the existence of common names. Chances are that your model already has some of the opengraph defined properties.

This is the list of supported opengraph protocol properties and their possible column names (in precedence order):

  • title - og_title, title, name
  • type - og_type, kind, category
  • image - og_image, image, photo, picture, thumb, thumbnail
  • url - og_url, url, uri, link
  • description - og_description, description, summary
  • site_name - og_site, website, web
  • latitude - og_latitude, latitude
  • longitude - og_longitude, longitude
  • street_address - og_street_address, street_address, address, street
  • locality - og_locality, locality
  • region - og_region, region
  • postal_code - og_postal_code, postal_code, zip_code, zip
  • country_name - og_country_name, country_name, country
  • email - og_email, email, mail
  • phone_number - og_phone_number, phone_number, phone
  • fax_number - og_fax_number, fax_number, fax

Using a different column name

If you need to use a different column then use the columns option. For example, if you store the url of your movies using the imdb_url column in your movies table, then do this:

# app/models/movie.rb
acts_as_opengraph :columns => { :url => :imdb_url }

What about using a custom method?

If you wish to use a custom method for some opengraph field, then all you need to do is to define a method with the prefix opengraph_. For example, if you are using Paperclip for your image attachments, you can do this:

# app/models/movie.rb
class Movie < ActiveRecord::Base

	has_attached_file :picture, :styles => { :small => "160x130>"}

	acts_as_opengraph
	
	def opengraph_image
		picture.url(:small)
	end
	
end

Default values

Use the values option for passing default opengraph values. For our Movie example we can specify that all of our records are movies by doing this:

acts_as_opengraph :values => { :type => "movie" }

* Notice that acts_as_opengraph only accepts an options hash argument, so if you want to combine default values and column names you'd do this:

acts_as_opengraph :columns => { :url => :imdb_url, :email => :contact }, 
                  :values => { :type => "movie", :site_name => "http://example.com" }

Like Button options

Along with the object for which you want to display the Like button, you can pass an options hash to configure its appearance:

# app/views/layouts/application.html.erb	
<%= like_button_for @movie, :layout => :box_count, :show_faces => true  %>

Using url helpers

By default, acts_as_opengraph will try to retrieve your object's url by calling opengraph_url on it. You could override it by defining a custom method, like this:

# app/models/movie.rb
def opengraph_url
	"http://example.com/movies/#{self.id}"
end

But that's not the Rails way, so instead of doing that, you can pass an href option from your views, which means you can easily take advantage of the url helpers, like this:

# app/views/movies/show.html.erb
<%= like_button_for @movie, :href => movie_path(@movie) %>

See the complete list of allowed attributes and options here.

Using the XFBML version

The XFBML version is more versatile, but requires use of the JavaScript SDK.

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

# You can use the following helper method to load the JavaScript SDK
<%= fb_javascript_include_tag YOUR_APP_ID %>

# Now you can pass the :xfbml option to the like button helper
<%= like_button_for @movie, :xfbml => true %>

Note on Patches/Pull Requests

  • Fork the project.
  • Make your feature addition or bug fix.
  • Add tests for it. This is important so I don’t break it in a future version unintentionally.
  • Send me a pull request. Bonus points for topic branches.

Contributors

Copyright

Copyright © 2011 Ruben Ascencio, released under the MIT license

About

ActiveRecord extension that turns your models into Facebook's Open Graph objects.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages