acts_as_graph_object 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +85 -0
- data/Rakefile +2 -0
- data/acts_as_graph_object.gemspec +20 -0
- data/lib/acts_as_graph_object/base.rb +59 -0
- data/lib/acts_as_graph_object/configuration.rb +29 -0
- data/lib/acts_as_graph_object/helpers.rb +19 -0
- data/lib/acts_as_graph_object/railite.rb +14 -0
- data/lib/acts_as_graph_object/version.rb +3 -0
- data/lib/acts_as_graph_object.rb +3 -0
- metadata +73 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Fred Kelly
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,85 @@
|
|
1
|
+
acts_as_graph_object
|
2
|
+
====================
|
3
|
+
|
4
|
+
ActiveRecord extension that maps models to Facebook Open Graph objects.
|
5
|
+
|
6
|
+
### Installation
|
7
|
+
```
|
8
|
+
gem install acts_as_graph_object
|
9
|
+
```
|
10
|
+
Then just add the dependancy to your `Gemfile`.
|
11
|
+
|
12
|
+
### Usage
|
13
|
+
#### Configuration
|
14
|
+
```ruby
|
15
|
+
# app/config/initializers/acts_as_graph_object.rb
|
16
|
+
ActsAsGraphObject.configure do |config|
|
17
|
+
config.namespace = 'my-app'
|
18
|
+
config.app_id = 12345
|
19
|
+
config.admins = [1245, 6789]
|
20
|
+
end
|
21
|
+
```
|
22
|
+
|
23
|
+
##### Default URL Method
|
24
|
+
In order to use the built in `@model.url` method you need to set the following config option:
|
25
|
+
```ruby
|
26
|
+
# app/config/environments/production.rb
|
27
|
+
routes.default_url_options[:host] = 'my-app.com'
|
28
|
+
```
|
29
|
+
|
30
|
+
#### Add acts_as_graph_object...
|
31
|
+
```ruby
|
32
|
+
# app/models/movie.rb
|
33
|
+
class Movie < ActiveRecord::Base
|
34
|
+
acts_as_graph_object :custom => [:director, :writer, :cast]
|
35
|
+
|
36
|
+
def cast
|
37
|
+
...
|
38
|
+
end
|
39
|
+
end
|
40
|
+
```
|
41
|
+
#### Outputting meta tags
|
42
|
+
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>`:
|
43
|
+
```html
|
44
|
+
# app/views/layouts/application.html.erb
|
45
|
+
<head>
|
46
|
+
<%= yield :meta_tags %>
|
47
|
+
</head>
|
48
|
+
|
49
|
+
# app/views/movies/show.html.erb
|
50
|
+
<% content_for :meta_tags, graph_object_tags_for(@movie) %>
|
51
|
+
```
|
52
|
+
#### Overriding from view
|
53
|
+
If you want to override a value from the view (for example to use a `url_for` helper):
|
54
|
+
```html
|
55
|
+
# app/views/movies/show.html.erb
|
56
|
+
<% content_for :meta_tags, graph_object_tags_for(@movie, :url => movie_url(@movie)) %>
|
57
|
+
```
|
58
|
+
|
59
|
+
#### Notes
|
60
|
+
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.
|
61
|
+
|
62
|
+
## Project Goals
|
63
|
+
1\. Find a way to provide model URLs using the url_for view/controller helpers.
|
64
|
+
|
65
|
+
2\. Map common attribute names to `og` meta tags (e.g. title, description, image etc).
|
66
|
+
|
67
|
+
3\. Provide helpers that allow resulting meta tags to be easily added to global layout in a standar Rails REST architecture.
|
68
|
+
i.e. for all /show actions, add meta tags to head if object has open graph attributes.
|
69
|
+
|
70
|
+
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.
|
71
|
+
|
72
|
+
5\. Automatically handle arrays, i.e. `:cast => ['Tom Cruise', 'Kelly McGillis', 'Val Kilmer']` becomes:
|
73
|
+
```html
|
74
|
+
<meta property="my-app:cast" content="Tom Cruise" />
|
75
|
+
<meta property="my-app:cast" content="Kelly McGillis" />
|
76
|
+
<meta property="my-app:cast" content="Val Kilmer" />
|
77
|
+
```
|
78
|
+
|
79
|
+
6\. Keep it unobtrusive! no heavy configuration in models, something simple, e.g.
|
80
|
+
```ruby
|
81
|
+
class Movie < ActiveRecord::Base
|
82
|
+
acts_as_graph_object :custom => [:director, :writer, :cast]
|
83
|
+
end
|
84
|
+
```
|
85
|
+
This would map all standard properties `title`, `description`, `image`, `app_id` etc along with the custom properties `director`, `writer` & `cast`.
|
data/Rakefile
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/acts_as_graph_object/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Fred Kelly"]
|
6
|
+
gem.email = ["me@fredkelly.net"]
|
7
|
+
gem.description = %q{ActiveRecord extension that maps models to Facebook Open Graph objects.}
|
8
|
+
gem.summary = %q{Facebook Open Graph object mapper.}
|
9
|
+
gem.homepage = "https://github.com/fredkelly/acts_as_graph_object"
|
10
|
+
|
11
|
+
# requires Rails!
|
12
|
+
gem.add_runtime_dependency "rails", "~> 3.1.0"
|
13
|
+
|
14
|
+
gem.files = `git ls-files`.split($\)
|
15
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
16
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
17
|
+
gem.name = "acts_as_graph_object"
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
gem.version = ActsAsGraphObject::VERSION
|
20
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
module ActsAsGraphObject
|
2
|
+
module Base
|
3
|
+
def acts_as_graph_object(options = {})
|
4
|
+
# url helpers for fallback url method
|
5
|
+
delegate :url_helpers, to: 'Rails.application.routes'
|
6
|
+
delegate :configuration, to: 'ActsAsGraphObject'
|
7
|
+
|
8
|
+
class_attribute :options
|
9
|
+
self.options = options
|
10
|
+
|
11
|
+
include InstanceMethods
|
12
|
+
end
|
13
|
+
|
14
|
+
module InstanceMethods
|
15
|
+
# requires routes.default_url_options[:host] to be set!
|
16
|
+
# TODO: add warning message if method is called?
|
17
|
+
def url
|
18
|
+
url_helpers.send("#{self.class}_url".downcase, self)
|
19
|
+
end
|
20
|
+
|
21
|
+
def type
|
22
|
+
[configuration.namespace, self.class.name.underscore].join(':')
|
23
|
+
end
|
24
|
+
|
25
|
+
def graph_properties
|
26
|
+
# standard object properties
|
27
|
+
default_properties = [:title, :type, :image, :url, :description, :site_name,
|
28
|
+
:latitude, :longitude, :street_address, :locality, :region,
|
29
|
+
:postal_code, :country_name, :email, :phone_number, :fax_number]
|
30
|
+
|
31
|
+
# property/content pairs
|
32
|
+
properties = {
|
33
|
+
:og => {},
|
34
|
+
:fb => {
|
35
|
+
:app_id => configuration.app_id,
|
36
|
+
:admins => configuration.admins.join(',')
|
37
|
+
}
|
38
|
+
}
|
39
|
+
|
40
|
+
# try all the default og properties first
|
41
|
+
default_properties.each do |property|
|
42
|
+
if self.respond_to?(property)
|
43
|
+
properties[:og][property] = self.send(property)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
# add any custom properties..
|
48
|
+
unless options[:custom].empty?
|
49
|
+
properties[configuration.namespace] ||= {}
|
50
|
+
Array(options[:custom]).each do |property|
|
51
|
+
properties[configuration.namespace][property] = self.send(property)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
properties
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module ActsAsGraphObject
|
2
|
+
# Configures ActsAsGraphObject.
|
3
|
+
def self.configure(configuration = ActsAsGraphObject::Configuration.new)
|
4
|
+
yield configuration if block_given?
|
5
|
+
@@configuration = configuration
|
6
|
+
end
|
7
|
+
|
8
|
+
def self.configuration # :nodoc:
|
9
|
+
@@configuration ||= ActsAsGraphObject::Configuration.new
|
10
|
+
end
|
11
|
+
|
12
|
+
# Can be configured using the ActsAsGraphObject.configure method. For example:
|
13
|
+
#
|
14
|
+
# ActsAsGraphObject.configure do |config|
|
15
|
+
# config.namespace = 'my-app'
|
16
|
+
# end
|
17
|
+
class Configuration
|
18
|
+
attr_accessor :namespace, :app_id, :admins
|
19
|
+
|
20
|
+
def initialize # :nodoc:
|
21
|
+
# set any defaults in here
|
22
|
+
end
|
23
|
+
|
24
|
+
# symbolize namespace
|
25
|
+
def namespace=(namespace)
|
26
|
+
@namespace = namespace.to_sym
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module ActsAsGraphObject
|
2
|
+
module ViewHelpers
|
3
|
+
def graph_object_tags_for(object, options = {})
|
4
|
+
raise "You need to add acts_as_graph_object to your #{object.class} model." unless object.respond_to?(:graph_properties)
|
5
|
+
meta_tags = []
|
6
|
+
object.graph_properties.each do |namespace, attributes|
|
7
|
+
attributes.each do |property, values|
|
8
|
+
# allow value overide from view..
|
9
|
+
values = options[property] || values
|
10
|
+
# in most cases 'values' will be a single value
|
11
|
+
Array(values).each do |content|
|
12
|
+
meta_tags << tag(:meta, :property => [namespace, property].join(':'), :content => content)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
meta_tags.join("\n").html_safe
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require "acts_as_graph_object"
|
2
|
+
require "rails"
|
3
|
+
|
4
|
+
module ActsAsGraphObject
|
5
|
+
class Railtie < Rails::Railtie
|
6
|
+
initializer 'acts_as_graph_object.ar_extensions' do
|
7
|
+
ActiveRecord::Base.extend ActsAsGraphObject::Base
|
8
|
+
end
|
9
|
+
|
10
|
+
initializer 'acts_as_graph_object.view_helpers' do
|
11
|
+
ActionView::Base.send :include, ActsAsGraphObject::ViewHelpers
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
metadata
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: acts_as_graph_object
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Fred Kelly
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-08-05 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rails
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 3.1.0
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 3.1.0
|
30
|
+
description: ActiveRecord extension that maps models to Facebook Open Graph objects.
|
31
|
+
email:
|
32
|
+
- me@fredkelly.net
|
33
|
+
executables: []
|
34
|
+
extensions: []
|
35
|
+
extra_rdoc_files: []
|
36
|
+
files:
|
37
|
+
- .gitignore
|
38
|
+
- Gemfile
|
39
|
+
- LICENSE
|
40
|
+
- README.md
|
41
|
+
- Rakefile
|
42
|
+
- acts_as_graph_object.gemspec
|
43
|
+
- lib/acts_as_graph_object.rb
|
44
|
+
- lib/acts_as_graph_object/base.rb
|
45
|
+
- lib/acts_as_graph_object/configuration.rb
|
46
|
+
- lib/acts_as_graph_object/helpers.rb
|
47
|
+
- lib/acts_as_graph_object/railite.rb
|
48
|
+
- lib/acts_as_graph_object/version.rb
|
49
|
+
homepage: https://github.com/fredkelly/acts_as_graph_object
|
50
|
+
licenses: []
|
51
|
+
post_install_message:
|
52
|
+
rdoc_options: []
|
53
|
+
require_paths:
|
54
|
+
- lib
|
55
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
56
|
+
none: false
|
57
|
+
requirements:
|
58
|
+
- - ! '>='
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '0'
|
61
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
62
|
+
none: false
|
63
|
+
requirements:
|
64
|
+
- - ! '>='
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: '0'
|
67
|
+
requirements: []
|
68
|
+
rubyforge_project:
|
69
|
+
rubygems_version: 1.8.24
|
70
|
+
signing_key:
|
71
|
+
specification_version: 3
|
72
|
+
summary: Facebook Open Graph object mapper.
|
73
|
+
test_files: []
|