Module: ActsAsOpengraphHelper

Defined in:
lib/acts_as_opengraph/helper/acts_as_opengraph_helper.rb

Instance Method Summary collapse

Instance Method Details

#like_button_for(obj, options = {}) ⇒ String

Displays the Facebook Like Button in your views.

Examples:

like_button_for(@movie)
like_button_for(@movie, :layout => :button_count, :display_faces => true)

Specifying href using rails helpers

like_button_for(@movie, :href => movie_url(@movie))

Parameters:

  • obj (Object, #opengraph_data)

    An instance of your ActiveRecord model that responds to opengraph_data

  • options (Hash) (defaults to: {})

    A Hash of / supported attributes. Defaults to { :layout => :standard, :show_faces => false, :width => 450, :action => :like, :colorscheme => :light }

Returns:

  • (String)

    An iFrame version of the Facebook Like Button

Raises:

  • (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)



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/acts_as_opengraph/helper/acts_as_opengraph_helper.rb', line 29

def like_button_for(obj, options = {})
  raise(ArgumentError.new, "You need to call acts_as_opengraph on your #{obj.class} model") unless obj.respond_to?(:opengraph_data)
  href = options[:href] ? options[:href] : obj.opengraph_url
  return unless href.present?
  
  config = { :layout => :standard, :show_faces => false, :width => 450, :action => :like, :colorscheme => :light }
  config.update(options) if options.is_a?(Hash)
  
  o_layout = config[:layout].to_sym
  if o_layout == :standard
    config[:height] = config[:show_faces].to_s.to_sym == :true ? 80 : 35
  elsif o_layout == :button_count
    config[:height] = 21
  elsif o_layout == :box_count
    config[:height] = 65
  end
  
  %(<iframe src="http://www.facebook.com/plugins/like.php?href=#{CGI.escape(href)}&amp;layout=#{config[:layout]}&amp;show_faces=#{config[:show_faces]}&amp;width=#{config[:width]}&amp;action=#{config[:action]}&amp;colorscheme=#{config[:colorscheme]}&amp;height=#{config[:height]}" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:#{config[:width]}px; height:#{config[:height]}px;" allowTransparency="true"></iframe>)
end

#opengraph_meta_tags_for(obj) ⇒ String

Generates the opengraph meta tags for your views

Examples:

opengraph_meta_tags_for(@movie)

Parameters:

  • obj (Object, #opengraph_data)

    An instance of your ActiveRecord model that responds to opengraph_data

Returns:

Raises:

  • (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)



9
10
11
12
13
14
15
16
# File 'lib/acts_as_opengraph/helper/acts_as_opengraph_helper.rb', line 9

def opengraph_meta_tags_for(obj)
  raise(ArgumentError.new, "You need to call acts_as_opengraph on your #{obj.class} model") unless obj.respond_to?(:opengraph_data)
  tags = obj.opengraph_data.map do |att|
    %(<meta property="#{att[:name].dasherize}" content="#{Rack::Utils.escape_html(att[:value])}"/>)
  end
  tags = tags.join("\n")
  tags.respond_to?(:html_safe) ? tags.html_safe : tags
end