acts_as_opengraph 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +6 -0
- data/Gemfile +4 -0
- data/LICENSE +19 -0
- data/README.markdown +137 -0
- data/Rakefile +2 -0
- data/acts_as_opengraph.gemspec +23 -0
- data/lib/acts_as_opengraph/active_record/acts/opengraph.rb +121 -0
- data/lib/acts_as_opengraph/helper/acts_as_opengraph_helper.rb +48 -0
- data/lib/acts_as_opengraph/version.rb +3 -0
- data/lib/acts_as_opengraph.rb +10 -0
- data/test/opengraph_test.rb +109 -0
- data/test/test_helper.rb +48 -0
- metadata +93 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
Copyright (c) 2011 Ruben Ascencio
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
5
|
+
in the Software without restriction, including without limitation the rights
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
8
|
+
furnished to do so, subject to the following conditions:
|
9
|
+
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
11
|
+
all copies or substantial portions of the Software.
|
12
|
+
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
THE SOFTWARE.
|
data/README.markdown
ADDED
@@ -0,0 +1,137 @@
|
|
1
|
+
# acts\_as\_opengraph
|
2
|
+
|
3
|
+
ActiveRecord extension that turns your models into [facebook opengraph](http://developers.facebook.com/docs/opengraph/) objects.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
gem install acts_as_opengraph
|
8
|
+
|
9
|
+
Now just add the gem dependency in your projects configuration.
|
10
|
+
|
11
|
+
## Usage
|
12
|
+
|
13
|
+
### Adding acts\_as\_opengraph
|
14
|
+
|
15
|
+
# app/models/movie.rb
|
16
|
+
class Movie < ActiveRecord::Base
|
17
|
+
acts_as_opengraph
|
18
|
+
end
|
19
|
+
|
20
|
+
### Generating the opengraph meta tags
|
21
|
+
|
22
|
+
# app/views/layouts/application.html.erb
|
23
|
+
<head>
|
24
|
+
<%= yield :opengraph_meta_tags %>
|
25
|
+
</head>
|
26
|
+
|
27
|
+
# app/views/movies/show.html.erb
|
28
|
+
<% content_for :opengraph_meta_tags, opengraph_meta_tags_for(@movie) %>
|
29
|
+
|
30
|
+
### Displaying the Like Button
|
31
|
+
# app/views/movies/show.html.erb
|
32
|
+
<%= like_button_for @movie %>
|
33
|
+
|
34
|
+
\* Notice that the Like Button will retrieve the required `href` attribute by calling `@movie.opengraph_url`. Read below for more options.
|
35
|
+
|
36
|
+
|
37
|
+
## Options
|
38
|
+
|
39
|
+
### Database columns
|
40
|
+
|
41
|
+
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.
|
42
|
+
|
43
|
+
This is the list of supported opengraph protocol properties and their possible column names (in precedence order):
|
44
|
+
|
45
|
+
* __title__ - og\_title, title, name
|
46
|
+
* __type__ - og\_type, kind, category
|
47
|
+
* __image__ - og\_image, image, photo, picture, thumb, thumbnail
|
48
|
+
* __url__ - og\_url, url, uri, link
|
49
|
+
* __description__ - og\_description, description, summary
|
50
|
+
* __site\_name__ - og\_site, website, web
|
51
|
+
* __latitude__ - og\_latitude, latitude
|
52
|
+
* __longitude__ - og\_longitude, longitude
|
53
|
+
* __street\_address__ - og\_street\_address, street_address, address, street
|
54
|
+
* __locality__ - og\_locality, locality
|
55
|
+
* __region__ - og\_region, region
|
56
|
+
* __postal\_code__ - og\_postal\_code, postal\_code, zip\_code, zip
|
57
|
+
* __country\_name__ - og\_country_name, country\_name, country
|
58
|
+
* __email__ - og\_email, email, mail
|
59
|
+
* __phone\_number__ - og\_phone\_number, phone\_number, phone
|
60
|
+
* __fax\_number__ - og\_fax\_number, fax\_number, fax
|
61
|
+
|
62
|
+
### Using a different column name
|
63
|
+
|
64
|
+
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:
|
65
|
+
|
66
|
+
# app/models/movie.rb
|
67
|
+
acts_as_opengraph :columns => { :url => :imdb_url }
|
68
|
+
|
69
|
+
### What about using a custom method?
|
70
|
+
|
71
|
+
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_`.
|
72
|
+
For example, if you are using [Paperclip](https://github.com/thoughtbot/paperclip) for your image attachments, you can do this:
|
73
|
+
|
74
|
+
# app/models/movie.rb
|
75
|
+
class Movie < ActiveRecord::Base
|
76
|
+
|
77
|
+
has_attached_file :picture, :styles => { :small => "160x130>"}
|
78
|
+
|
79
|
+
acts_as_opengraph
|
80
|
+
|
81
|
+
def opengraph_image
|
82
|
+
picture.url(:small)
|
83
|
+
end
|
84
|
+
|
85
|
+
end
|
86
|
+
|
87
|
+
### Default values
|
88
|
+
|
89
|
+
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:
|
90
|
+
|
91
|
+
acts_as_opengraph :values => { :type => "movie" }
|
92
|
+
|
93
|
+
\* 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:
|
94
|
+
|
95
|
+
acts_as_opengraph :columns => { :url => :imdb_url, :email => :contact },
|
96
|
+
:values => { :type => "movie", :site_name => "http://example.com" }
|
97
|
+
|
98
|
+
## Like Button options
|
99
|
+
|
100
|
+
Along with the object for which you want to display the Like button, you can pass an options hash to configure its appearance:
|
101
|
+
|
102
|
+
# app/views/layouts/application.html.erb
|
103
|
+
<%= like_button_for @movie, :layout => :box_count, :show_faces => true %>
|
104
|
+
|
105
|
+
### Using url helpers
|
106
|
+
|
107
|
+
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:
|
108
|
+
|
109
|
+
# app/models/movie.rb
|
110
|
+
def opengraph_url
|
111
|
+
"http://example.com/movies/#{self.id}"
|
112
|
+
end
|
113
|
+
|
114
|
+
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:
|
115
|
+
|
116
|
+
# app/views/movies/show.html.erb
|
117
|
+
<%= like_button_for @movie, :href => movie_path(@movie) %>
|
118
|
+
|
119
|
+
See the complete list of allowed attributes and options [here](http://developers.facebook.com/docs/reference/plugins/like/).
|
120
|
+
|
121
|
+
## Note on Patches/Pull Requests
|
122
|
+
|
123
|
+
* Fork the project.
|
124
|
+
* Make your feature addition or bug fix.
|
125
|
+
* Add tests for it. This is important so I don’t break it in a future version unintentionally.
|
126
|
+
* Send me a pull request. Bonus points for topic branches.
|
127
|
+
|
128
|
+
|
129
|
+
## Copyright
|
130
|
+
|
131
|
+
Copyright © 2011 Ruben Ascencio, released under the MIT license
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
|
136
|
+
|
137
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "acts_as_opengraph/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "acts_as_opengraph"
|
7
|
+
s.version = ActsAsOpengraph::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Ruben Ascencio"]
|
10
|
+
s.email = ["galateaweb@gmail.com"]
|
11
|
+
s.homepage = "https://github.com/rubenrails/acts_as_opengraph"
|
12
|
+
s.summary = %q{ActiveRecord extension that turns your models into graph objects}
|
13
|
+
s.description = %q{ActiveRecord extension that turns your models into graph objects. Includes helper methods for adding <meta> tags and the Like Button to your views.}
|
14
|
+
|
15
|
+
s.rubyforge_project = "acts_as_opengraph"
|
16
|
+
|
17
|
+
s.add_development_dependency('sqlite3')
|
18
|
+
|
19
|
+
s.files = `git ls-files`.split("\n")
|
20
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
21
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
22
|
+
s.require_paths = ["lib"]
|
23
|
+
end
|
@@ -0,0 +1,121 @@
|
|
1
|
+
module ActiveRecord
|
2
|
+
module Acts
|
3
|
+
module Opengraph
|
4
|
+
|
5
|
+
def self.included(base)
|
6
|
+
base.extend ActMethods
|
7
|
+
end
|
8
|
+
|
9
|
+
module ActMethods
|
10
|
+
def acts_as_opengraph(options = {})
|
11
|
+
# don't allow multiple calls
|
12
|
+
return if included_modules.include? InstanceMethods
|
13
|
+
|
14
|
+
extend ClassMethods
|
15
|
+
|
16
|
+
opengraph_atts = %w(title type image url description site_name latitude longitude street_address locality region postal_code country_name email phone_number fax_number)
|
17
|
+
|
18
|
+
options[:columns] ||= {}
|
19
|
+
options[:values] ||= {}
|
20
|
+
|
21
|
+
opengraph_atts.each do |att_name|
|
22
|
+
options[:columns]["#{att_name}".to_sym] ||= alternative_column_name_for("og_#{att_name}".to_sym)
|
23
|
+
end
|
24
|
+
|
25
|
+
write_inheritable_attribute :opengraph_atts, opengraph_atts
|
26
|
+
class_inheritable_reader :opengraph_atts
|
27
|
+
|
28
|
+
write_inheritable_attribute :options, options
|
29
|
+
class_inheritable_reader :options
|
30
|
+
|
31
|
+
opengraph_atts.each do |att_name|
|
32
|
+
define_method "opengraph_#{att_name}" do
|
33
|
+
return_value_or_default att_name.to_sym
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
include InstanceMethods
|
38
|
+
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
|
43
|
+
module ClassMethods
|
44
|
+
|
45
|
+
private
|
46
|
+
|
47
|
+
# Returns a list of possible column names for a given attribute.
|
48
|
+
#
|
49
|
+
# @param [Symbol] att_name An opengraph attribute name prefixed with 'og_', i.e. :og_title, :og_type, etc
|
50
|
+
# @return [Array] A list of possible names for the given opengraph attribute
|
51
|
+
def alternative_names_for(att_name)
|
52
|
+
case att_name
|
53
|
+
when :og_title then [:title, :name]
|
54
|
+
when :og_type then [:kind, :category]
|
55
|
+
when :og_image then [:image, :photo, :picture, :thumb, :thumbnail]
|
56
|
+
when :og_url then [:url, :uri, :link]
|
57
|
+
when :og_description then [:description, :summary]
|
58
|
+
when :og_site_name then [:site, :website, :web]
|
59
|
+
when :og_latitude then [:latitude]
|
60
|
+
when :og_longitude then [:longitude]
|
61
|
+
when :og_street_address then [:street_address, :address, :street]
|
62
|
+
when :og_locality then [:locality]
|
63
|
+
when :og_region then [:region]
|
64
|
+
when :og_postal_code then [:postal_code, :zip_code, :zip]
|
65
|
+
when :og_country_name then [:country_name, :country]
|
66
|
+
when :og_email then [:email, :mail]
|
67
|
+
when :og_phone_number then [:phone_number, :phone]
|
68
|
+
when :og_fax_number then [:fax_number, :fax]
|
69
|
+
else []
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
# Tries to guess the column name for the given attribute. If it can't find any column (or similar) then it will create a virtual attribute
|
74
|
+
# for the object called: ATT_NAME_placeholder, so the object responds to that column.
|
75
|
+
#
|
76
|
+
# @param [Symbol] att_name An opengraph attribute name prefixed with 'og_', i.e. :og_title, :og_type, etc
|
77
|
+
# @return [String] The final name (found or created) for the opengraph attribute
|
78
|
+
def alternative_column_name_for(att_name)
|
79
|
+
alt_names = alternative_names_for(att_name)
|
80
|
+
columns_to_check = [att_name] + alt_names
|
81
|
+
columns_to_check.each do |column_name|
|
82
|
+
return column_name.to_sym if column_names.include?(column_name.to_s)
|
83
|
+
end
|
84
|
+
|
85
|
+
# Define placeholder method
|
86
|
+
ph_method_name = "#{alt_names.first}_placeholder"
|
87
|
+
define_method(ph_method_name) { "" }
|
88
|
+
ph_method_name
|
89
|
+
end
|
90
|
+
|
91
|
+
end
|
92
|
+
|
93
|
+
module InstanceMethods
|
94
|
+
# Returns an array of hashes representing the opengraph attribute/values for the Object.
|
95
|
+
#
|
96
|
+
# @return [Array] List of hashes representing opengraph attribute/values
|
97
|
+
# @example
|
98
|
+
# @movie.opengraph_data #=> {name=> "og:title", :value => "The Rock"}, {:name => "og:type", :value=> "movie"}
|
99
|
+
def opengraph_data
|
100
|
+
data_list = opengraph_atts.map do |att_name|
|
101
|
+
{:name => "og:#{att_name}", :value => self.send("opengraph_#{att_name}")}
|
102
|
+
end
|
103
|
+
data_list.delete_if{ |el| el[:value].blank? }
|
104
|
+
end
|
105
|
+
|
106
|
+
|
107
|
+
private
|
108
|
+
|
109
|
+
def return_value_or_default(att_name)
|
110
|
+
if options[:values].has_key?(att_name.to_sym)
|
111
|
+
options[:values][att_name]
|
112
|
+
else
|
113
|
+
self.send options[:columns]["#{att_name}".to_sym]
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
end
|
118
|
+
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
module ActsAsOpengraphHelper
|
2
|
+
# Generates the opengraph meta tags for your views
|
3
|
+
#
|
4
|
+
# @param [Object, #opengraph_data] obj An instance of your ActiveRecord model that responds to opengraph_data
|
5
|
+
# @return [String] A set of meta tags describing your graph object based on the {http://ogp.me/ opengraph protocol}
|
6
|
+
# @raise [ArgumentError] When you pass an instance of an object that doesn't responds to opengraph_data (maybe you forgot to add acts_as_opengraph in your model)
|
7
|
+
# @example
|
8
|
+
# opengraph_meta_tags_for(@movie)
|
9
|
+
def opengraph_meta_tags_for(obj)
|
10
|
+
raise(ArgumentError.new, "You need to call acts_as_opengraph on your #{obj.class} model") unless obj.respond_to?(:opengraph_data)
|
11
|
+
tags = obj.opengraph_data.map do |att|
|
12
|
+
%(<meta name="#{att[:name].dasherize}" content="#{Rack::Utils.escape_html(att[:value])}"/>)
|
13
|
+
end
|
14
|
+
tags = tags.join("\n")
|
15
|
+
tags.respond_to?(:html_safe) ? tags.html_safe : tags
|
16
|
+
end
|
17
|
+
|
18
|
+
# Displays the Facebook Like Button in your views.
|
19
|
+
#
|
20
|
+
# @param [Object, #opengraph_data] obj An instance of your ActiveRecord model that responds to opengraph_data
|
21
|
+
# @param [Hash] options A Hash of {http://developers.facebook.com/docs/reference/plugins/like/ supported attributes}. Defaults to { :layout => :standard, :show_faces => false, :width => 450, :action => :like, :colorscheme => :light }
|
22
|
+
# @return [String] An iFrame version of the Facebook Like Button
|
23
|
+
# @raise [ArgumentError] When you pass an instance of an object that doesn't responds to opengraph_data (maybe you forgot to add acts_as_opengraph in your model)
|
24
|
+
# @example
|
25
|
+
# like_button_for(@movie)
|
26
|
+
# like_button_for(@movie, :layout => :button_count, :display_faces => true)
|
27
|
+
# @example Specifying href using rails helpers
|
28
|
+
# like_button_for(@movie, :href => movie_url(@movie))
|
29
|
+
def like_button_for(obj, options = {})
|
30
|
+
raise(ArgumentError.new, "You need to call acts_as_opengraph on your #{obj.class} model") unless obj.respond_to?(:opengraph_data)
|
31
|
+
href = options[:href] ? options[:href] : obj.opengraph_url
|
32
|
+
return unless href.present?
|
33
|
+
|
34
|
+
config = { :layout => :standard, :show_faces => false, :width => 450, :action => :like, :colorscheme => :light }
|
35
|
+
config.update(options) if options.is_a?(Hash)
|
36
|
+
|
37
|
+
o_layout = config[:layout].to_sym
|
38
|
+
if o_layout == :standard
|
39
|
+
config[:height] = config[:show_faces].to_s.to_sym == :true ? 80 : 35
|
40
|
+
elsif o_layout == :button_count
|
41
|
+
config[:height] = 21
|
42
|
+
elsif o_layout == :box_count
|
43
|
+
config[:height] = 65
|
44
|
+
end
|
45
|
+
|
46
|
+
%(<iframe src="http://www.facebook.com/plugins/like.php?href=#{CGI.escape(href)}&layout=#{config[:layout]}&show_faces=#{config[:show_faces]}&width=#{config[:width]}&action=#{config[:action]}&colorscheme=#{config[:colorscheme]}&height=#{config[:height]}" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:#{config[:width]}px; height:#{config[:height]}px;" allowTransparency="true"></iframe>)
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
if defined? ActiveRecord::Base
|
2
|
+
require File.join(File.dirname(__FILE__), 'acts_as_opengraph', 'active_record', 'acts', 'opengraph')
|
3
|
+
ActiveRecord::Base.send :include, ActiveRecord::Acts::Opengraph
|
4
|
+
end
|
5
|
+
|
6
|
+
if defined? ActionView::Base
|
7
|
+
require File.join(File.dirname(__FILE__), 'acts_as_opengraph', 'helper', 'acts_as_opengraph_helper')
|
8
|
+
ActionView::Base.send :include, ActsAsOpengraphHelper
|
9
|
+
end
|
10
|
+
|
@@ -0,0 +1,109 @@
|
|
1
|
+
require File.join File.dirname(__FILE__), 'test_helper'
|
2
|
+
|
3
|
+
class Book < ActiveRecord::Base
|
4
|
+
acts_as_opengraph
|
5
|
+
end
|
6
|
+
|
7
|
+
class Movie < ActiveRecord::Base
|
8
|
+
acts_as_opengraph :values => {:type => "movie", :site_name => "IMDb"}, :columns => {:url => :imdb}
|
9
|
+
|
10
|
+
def opengraph_image
|
11
|
+
"http://ia.media-imdb.com/rock.jpg"
|
12
|
+
end
|
13
|
+
|
14
|
+
end
|
15
|
+
|
16
|
+
class Song < ActiveRecord::Base
|
17
|
+
# This model doesn't uses acts_as_opengraph
|
18
|
+
end
|
19
|
+
|
20
|
+
class MovieTest < Test::Unit::TestCase
|
21
|
+
|
22
|
+
include ActsAsOpengraphHelper
|
23
|
+
|
24
|
+
MOVIE_NAME = "The Rock"
|
25
|
+
MOVIE_DESCRIPTION = "A renegade general and his group of U.S. Marines take over Alcatraz and threaten San Francisco Bay with biological weapons."
|
26
|
+
MOVIE_URL = "http://www.imdb.com/title/tt0117500/"
|
27
|
+
|
28
|
+
GENERATED_OPENGRAPH_DATA = [
|
29
|
+
{:value=> MOVIE_NAME, :name=> "og:title"},
|
30
|
+
{:value=> "movie", :name=> "og:type"},
|
31
|
+
{:value=> "http://ia.media-imdb.com/rock.jpg", :name=> "og:image"},
|
32
|
+
{:value=> MOVIE_URL, :name=> "og:url"},
|
33
|
+
{:value=> MOVIE_DESCRIPTION, :name=> "og:description"},
|
34
|
+
{:value=> "IMDb", :name=> "og:site_name"}
|
35
|
+
]
|
36
|
+
|
37
|
+
GENERATED_META_TAGS = %(<meta name="og:title" content="#{MOVIE_NAME}"/>
|
38
|
+
<meta name="og:type" content="movie"/>
|
39
|
+
<meta name="og:image" content="http://ia.media-imdb.com/rock.jpg"/>
|
40
|
+
<meta name="og:url" content="#{MOVIE_URL}"/>
|
41
|
+
<meta name="og:description" content="#{MOVIE_DESCRIPTION}"/>
|
42
|
+
<meta name="og:site-name" content="IMDb"/>)
|
43
|
+
|
44
|
+
GENERATED_LIKE_BUTTON = %(<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.imdb.com%2Ftitle%2Ftt0117500%2F&layout=standard&show_faces=false&width=450&action=like&colorscheme=light&height=35" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:450px; height:35px;" allowTransparency="true"></iframe>)
|
45
|
+
GENERATED_LIKE_BUTTON_CUSTOM_URL = %(<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fexample.com%2Fmovies%2F6&layout=standard&show_faces=false&width=450&action=like&colorscheme=light&height=35" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:450px; height:35px;" allowTransparency="true"></iframe>)
|
46
|
+
def setup
|
47
|
+
setup_db
|
48
|
+
assert @movie = Movie.create!(:title => MOVIE_NAME, :description => MOVIE_DESCRIPTION, :imdb => MOVIE_URL)
|
49
|
+
assert @song = Song.create!(:title => "Yellow Submarine")
|
50
|
+
assert @book = Book.create!(:title => "Getting real")
|
51
|
+
end
|
52
|
+
|
53
|
+
def teardown
|
54
|
+
teardown_db
|
55
|
+
end
|
56
|
+
|
57
|
+
def test_respond_to_opengraph_methods
|
58
|
+
assert_respond_to @movie, :opengraph_title
|
59
|
+
assert_respond_to @movie, :opengraph_type
|
60
|
+
assert_respond_to @movie, :opengraph_image
|
61
|
+
assert_respond_to @movie, :opengraph_url
|
62
|
+
assert_respond_to @movie, :opengraph_description
|
63
|
+
assert_respond_to @movie, :opengraph_site_name
|
64
|
+
assert_respond_to @movie, :opengraph_latitude
|
65
|
+
assert_respond_to @movie, :opengraph_longitude
|
66
|
+
assert_respond_to @movie, :opengraph_street_address
|
67
|
+
assert_respond_to @movie, :opengraph_locality
|
68
|
+
assert_respond_to @movie, :opengraph_region
|
69
|
+
assert_respond_to @movie, :opengraph_postal_code
|
70
|
+
assert_respond_to @movie, :opengraph_country_name
|
71
|
+
assert_respond_to @movie, :opengraph_email
|
72
|
+
assert_respond_to @movie, :opengraph_phone_number
|
73
|
+
assert_respond_to @movie, :opengraph_fax_number
|
74
|
+
assert_respond_to @movie, :opengraph_data
|
75
|
+
end
|
76
|
+
|
77
|
+
def test_opengraph_data
|
78
|
+
assert_equal GENERATED_OPENGRAPH_DATA, @movie.opengraph_data
|
79
|
+
end
|
80
|
+
|
81
|
+
def test_default_values
|
82
|
+
assert_equal "IMDb", @movie.opengraph_site_name
|
83
|
+
end
|
84
|
+
|
85
|
+
def test_method_overriding
|
86
|
+
assert_equal "http://ia.media-imdb.com/rock.jpg", @movie.opengraph_image
|
87
|
+
end
|
88
|
+
|
89
|
+
def test_different_column_name
|
90
|
+
assert_equal MOVIE_URL, @movie.opengraph_url
|
91
|
+
end
|
92
|
+
|
93
|
+
def test_meta_tags_helper
|
94
|
+
assert_equal GENERATED_META_TAGS, opengraph_meta_tags_for(@movie)
|
95
|
+
assert_raise(ArgumentError) { opengraph_meta_tags_for(@song) }
|
96
|
+
end
|
97
|
+
|
98
|
+
def test_like_button_helper
|
99
|
+
assert_equal GENERATED_LIKE_BUTTON, like_button_for(@movie)
|
100
|
+
assert_equal GENERATED_LIKE_BUTTON_CUSTOM_URL, like_button_for(@movie, :href => "http://example.com/movies/6")
|
101
|
+
|
102
|
+
# There's no way of getting the href attribute for this Book, so it returns nil
|
103
|
+
assert_nil like_button_for(@book)
|
104
|
+
|
105
|
+
# We aren't using acts_as_opengraph for this model, so it should let us know
|
106
|
+
assert_raise(ArgumentError) { like_button_for(@song) }
|
107
|
+
end
|
108
|
+
|
109
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
plugin_path = File.join File.dirname(__FILE__), '..'
|
2
|
+
|
3
|
+
require 'test/unit'
|
4
|
+
|
5
|
+
require 'rubygems'
|
6
|
+
require 'active_record'
|
7
|
+
require 'action_view'
|
8
|
+
|
9
|
+
|
10
|
+
|
11
|
+
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
|
12
|
+
$stdout = StringIO.new
|
13
|
+
|
14
|
+
def setup_db
|
15
|
+
ActiveRecord::Base.logger
|
16
|
+
ActiveRecord::Schema.define(:version => 1) do
|
17
|
+
|
18
|
+
create_table :books, :force => true do |t|
|
19
|
+
t.string :title
|
20
|
+
end
|
21
|
+
|
22
|
+
create_table :movies, :force => true do |t|
|
23
|
+
t.string :title
|
24
|
+
t.string :description
|
25
|
+
t.string :imdb
|
26
|
+
end
|
27
|
+
|
28
|
+
create_table :songs, :force => true do |t|
|
29
|
+
t.string :title
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def teardown_db
|
36
|
+
ActiveRecord::Base.connection.tables.each do |table|
|
37
|
+
ActiveRecord::Base.connection.drop_table(table)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
setup_db
|
42
|
+
|
43
|
+
|
44
|
+
$:.unshift File.join plugin_path, 'lib'
|
45
|
+
|
46
|
+
require 'acts_as_opengraph/active_record/acts/opengraph'
|
47
|
+
require 'acts_as_opengraph/helper/acts_as_opengraph_helper'
|
48
|
+
require 'acts_as_opengraph'
|
metadata
ADDED
@@ -0,0 +1,93 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: acts_as_opengraph
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Ruben Ascencio
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-02-22 00:00:00 -08:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: sqlite3
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 3
|
30
|
+
segments:
|
31
|
+
- 0
|
32
|
+
version: "0"
|
33
|
+
type: :development
|
34
|
+
version_requirements: *id001
|
35
|
+
description: ActiveRecord extension that turns your models into graph objects. Includes helper methods for adding <meta> tags and the Like Button to your views.
|
36
|
+
email:
|
37
|
+
- galateaweb@gmail.com
|
38
|
+
executables: []
|
39
|
+
|
40
|
+
extensions: []
|
41
|
+
|
42
|
+
extra_rdoc_files: []
|
43
|
+
|
44
|
+
files:
|
45
|
+
- .gitignore
|
46
|
+
- Gemfile
|
47
|
+
- LICENSE
|
48
|
+
- README.markdown
|
49
|
+
- Rakefile
|
50
|
+
- acts_as_opengraph.gemspec
|
51
|
+
- lib/acts_as_opengraph.rb
|
52
|
+
- lib/acts_as_opengraph/active_record/acts/opengraph.rb
|
53
|
+
- lib/acts_as_opengraph/helper/acts_as_opengraph_helper.rb
|
54
|
+
- lib/acts_as_opengraph/version.rb
|
55
|
+
- test/opengraph_test.rb
|
56
|
+
- test/test_helper.rb
|
57
|
+
has_rdoc: true
|
58
|
+
homepage: https://github.com/rubenrails/acts_as_opengraph
|
59
|
+
licenses: []
|
60
|
+
|
61
|
+
post_install_message:
|
62
|
+
rdoc_options: []
|
63
|
+
|
64
|
+
require_paths:
|
65
|
+
- lib
|
66
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
67
|
+
none: false
|
68
|
+
requirements:
|
69
|
+
- - ">="
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
hash: 3
|
72
|
+
segments:
|
73
|
+
- 0
|
74
|
+
version: "0"
|
75
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
76
|
+
none: false
|
77
|
+
requirements:
|
78
|
+
- - ">="
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
hash: 3
|
81
|
+
segments:
|
82
|
+
- 0
|
83
|
+
version: "0"
|
84
|
+
requirements: []
|
85
|
+
|
86
|
+
rubyforge_project: acts_as_opengraph
|
87
|
+
rubygems_version: 1.5.0
|
88
|
+
signing_key:
|
89
|
+
specification_version: 3
|
90
|
+
summary: ActiveRecord extension that turns your models into graph objects
|
91
|
+
test_files:
|
92
|
+
- test/opengraph_test.rb
|
93
|
+
- test/test_helper.rb
|