Sha256: 3c2f501772da5d71a3019696dece7cbd336485df39755aa1dae52c3a9fc787fb

Contents?: true

Size: 1.34 KB

Versions: 1

Compression:

Stored size: 1.34 KB

Contents

module TopHat
  module TwitterCardHelper

    class TwitterCardGenerator
      include ActionView::Helpers

      attr_reader :card_data

      def initialize(type, &block)
        @type = type
        @card_data = {}

        yield self if block_given?
      end

      def render
        output = ActiveSupport::SafeBuffer.new
        output << tag(:meta, :name => 'twitter:card', :value => @type)
        @card_data.each do |key, value|
          output << "\n".html_safe
          output << tag(:meta, :name => "twitter:#{key}", :value => value)
        end
        output << "\n".html_safe unless @card_data.empty?
        output
      end

      def add_nested_attributes(method, &block)
        image_generator = TwitterCardGenerator.new(method, &block)
        image_generator.card_data.each do |key, value|
          @card_data["#{method}:#{key}"] = value
        end
      end

      def method_missing(method, *args, &block) #:nodoc
        @card_data ||= {}
        @card_data[method] = args.shift
        add_nested_attributes(method, &block) if block_given?
      end
    end

    def twitter_card(type=nil, &block)
      if type.nil?
        if TopHat.current['twitter_card']
          TopHat.current['twitter_card'].render
        end
      else
        TopHat.current['twitter_card'] = TwitterCardGenerator.new(type, &block)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
tophat-2.0.0 lib/tophat/twitter_card.rb