lib/tophat/opengraph.rb in tophat-2.0.0 vs lib/tophat/opengraph.rb in tophat-2.1.0

- old
+ new

@@ -16,18 +16,18 @@ @app_id = options.delete(:app_id) if options && options.has_key?(:app_id) @admins = options.delete(:admins) if options && options.has_key?(:admins) end def app_id - tag(:meta, :property => 'fb:app_id', :content => @app_id) + "\n".html_safe if @app_id + tag(:meta, :property => 'fb:app_id', :content => @app_id) + "\n".html_safe end def admins - tag(:meta, :property => 'fb:admins', :content => [*@admins].join(',')) + "\n".html_safe if @admins + tag(:meta, :property => 'fb:admins', :content => [*@admins].join(',')) + "\n".html_safe end - def render_graph_data + def graph_data output = ActiveSupport::SafeBuffer.new @graph_data.each do |key, value| output << tag(:meta, :property => "og:#{key}", :content => value) output << "\n".html_safe if @graph_data.size > 1 end @@ -46,10 +46,18 @@ def method_missing(method, *args, &block) #:nodoc @graph_data ||= {} @graph_data[method] = args.shift end + def to_html + output = ActiveSupport::SafeBuffer.new + output << app_id if defined?(@app_id) && @app_id + output << admins if defined?(@admins) && @admins + output << graph_data if has_graph_data? + output + end + end HTML4_XMLNS = [ 'http://www.w3.org/1999/xhtml', { :prefix => 'og', :url => 'http://ogp.me/ns#' }, @@ -75,22 +83,17 @@ html_tag end end def opengraph(options=nil, &block) - if options.kind_of? Hash - TopHat.current['open_graph_defaults'] = options - end + TopHat.current['open_graph_defaults'] = options if options.kind_of? Hash + if block_given? TopHat.current['open_graph_generator'] = OpenGraphGenerator.new(TopHat.current['open_graph_defaults'], &block) else TopHat.current['open_graph_generator'] ||= OpenGraphGenerator.new TopHat.current['open_graph_generator'].merge(TopHat.current['open_graph_defaults']) - output = ActiveSupport::SafeBuffer.new - output << TopHat.current['open_graph_generator'].app_id - output << TopHat.current['open_graph_generator'].admins - output << TopHat.current['open_graph_generator'].render_graph_data if TopHat.current['open_graph_generator'].has_graph_data? - output + TopHat.current['open_graph_generator'].to_html end end end end