Code Monkey home page Code Monkey logo

acts_as_opengraph's Introduction

acts_as_opengraph

ActiveRecord 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

# app/models/movie.rb
class Movie < ActiveRecord::Base
	acts_as_opengraph
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

  • Eric Hill - Updated the meta attribute to reflect the current OG spec
  • Timo Göllner - Integrated Like Button via facebook XFBML tag

Copyright

Copyright © 2011 Ruben Ascencio, released under the MIT license

acts_as_opengraph's People

Contributors

rubenrails avatar

Stargazers

Rafe Hatfield avatar

Watchers

Rafe Hatfield avatar James Cloos avatar

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.