Giter VIP home page Giter VIP logo

acts_as_graph_object's Introduction

acts_as_graph_object Gem Version

ActiveRecord extension that maps models to Facebook Open Graph objects.

Links

Installation

gem install acts_as_graph_object

Then just add the dependancy to your Gemfile.

Usage

Configuration

# app/config/initializers/acts_as_graph_object.rb
ActsAsGraphObject.configure do |config|
  config.namespace = 'my-app'
  config.app_id = 12345
  config.admins = [1245, 6789]
end

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] = 'my-app.com'

Add acts_as_graph_object...

# app/models/movie.rb
class Movie < ActiveRecord::Base
  acts_as_graph_object :custom => [:director, :writer, :cast]

  def cast
  	...
  end
end

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    
<head>
    <%= yield :meta_tags %>
</head>

# 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]
}

Notes

This is my first gem so things are a bit rough around the edges, all feedback is happily welcomed :) - please fork/fix to your heart's content.

Project Goals

1. Find a way to provide model URLs using the url_for view/controller helpers.

2. Map common attribute names to og meta tags (e.g. title, description, image etc).

3. Provide helpers that allow resulting meta tags to be easily added to global layout in a standar Rails REST architecture. i.e. for all /show actions, add meta tags to head if object has open graph attributes.

4. Allow for easy configuration for constants such as fb:app_id & fb:admins as well as an app namespace to be used in og:type and any custom attributes.

5. Automatically handle arrays, i.e. :cast => ['Tom Cruise', 'Kelly McGillis', 'Val Kilmer'] becomes:

<meta property="my-app:cast" content="Tom Cruise" />
<meta property="my-app:cast" content="Kelly McGillis" />
<meta property="my-app:cast" content="Val Kilmer" />

6. Keep it unobtrusive! no heavy configuration in models, something simple, e.g.

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

This would map all standard properties title, description, image, app_id etc along with the custom properties director, writer & cast.

7. Write proper tests and documentation!

acts_as_graph_object's People

Contributors

fredkelly avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

kylemac

acts_as_graph_object's Issues

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.