class Disqussion::Widget

Disqus Widget generator.

All of the methods accept various options, and “account” is required for all of them. You can avoid having to pass in the account each time by setting it in the defaults like this:

Disqus::defaults[:account] = "my_account"

Constants

COMBO
RECENT
ROOT_PATH
THREAD
TOP
VALID_AVATAR_SIZES
VALID_COLORS
VALID_DEFAULT_TABS
VALID_NUM_ITEMS
VALID_ORIENTATIONS

Public Class Methods

combo(opts = {}) click to toggle source

Show the Disqus combo widget. This is a three-tabbed box with links popular threads, top posters, and recent threads. Options:

  • :account: Your Discus account (required).

  • :num_items: How many items to show.

  • :hide_mods: Don’t show moderators.

  • :default_tab: Should be ‘people’, ‘recent’, or ‘popular’.

# File lib/disqussion/widget.rb, line 161
def combo(opts = {})
  opts = Disqus::defaults.merge(opts)
  validate_opts!(opts)
  s = '<script type="text/javascript" src="'
  s << COMBO
  s << '&hide_mods=1' if opts[:hide_mods]
  s << '"></script>' 
  s % [opts[:account], opts[:num_items], opts[:color], opts[:default_tab]]
end
comment_counts(opts = {}) click to toggle source

Loads Javascript to show the number of comments for the page.

The Javascript sets the inner html to the comment count for any links on the page that have the anchor “disqus_thread”. For example, “View Comments” below would be replaced by “1 comment” or “23 comments” etc.

<a href="http://my.website/article-permalink#disqus_thread">View Comments</a>
<a href="http://my.website/different-permalink#disqus_thread">View Comments</a>

Options:

  • account: Your Discus account (required).

# File lib/disqussion/widget.rb, line 60
      def comment_counts(opts = {})
        opts = Disqus::defaults.merge(opts)        
        validate_opts!(opts)
        s = <<-WHIMPER
        <script type="text/javascript">
        //<![CDATA[
        (function() {
            var links = document.getElementsByTagName('a');
            var query = '?';
            for(var i = 0; i < links.length; i++) {
              if(links[i].href.indexOf('#disqus_thread') >= 0) {
                query += 'url' + i + '=' + encodeURIComponent(links[i].href) + '&';
              }
            }
            document.write('<' + 'script type="text/javascript" src="#{ROOT_PATH}get_num_replies.js' + query + '"></' + 'script>');
          })();
        //]]>
        </script>
        WHIMPER
        s % opts[:account]
      end
recent_comments(opts = {}) click to toggle source

Show the recent comments Disqus widget. Options:

  • account: Your Discus account (required).

  • header: HTML snipper with header (default h2) tag and text.

  • num_items:: How many items to show.

  • hide_avatars: Don’t show avatars.

  • avatar_size: Avatar size.

# File lib/disqussion/widget.rb, line 137
def recent_comments(opts = {})
  opts = Disqus::defaults.merge(opts)
  opts[:header] ||= '<h2 class="dsq-widget-title">Recent Comments</h2>'
  validate_opts!(opts)
  s = '<div id="dsq-recentcomments" class="dsq-widget">'
  s << opts[:header]
  s << '<script type="text/javascript" src="'
  s << RECENT 
  s << '&hide_avatars=1' if opts[:hide_avatars]
  s << '"></script>'
  s << '</div>'
  if opts[:show_powered_by]
    s << '<a href="http://disqus.com">Powered by Disqus</a>'
  end
  s % [opts[:account], opts[:num_items], opts[:avatar_size]]
end
thread(opts = {}) click to toggle source

Show the main Disqus thread widget. Options:

  • account: Your Discus account (required).

# File lib/disqussion/widget.rb, line 30
def thread(opts = {})
  opts = Disqus::defaults.merge(opts)
  opts[:view_thread_text] ||= "View the discussion thread"
  validate_opts!(opts)
  s = ''
  if opts[:developer]
    s << '<script type="text/javascript">var disqus_developer = 1;</script>'
  end
  s << '<div id="disqus_thread"></div>'
  s << '<script type="text/javascript" src="' + THREAD + '"></script>'
  s << '<noscript><a href="http://%s.disqus.com/?url=ref">'
  s << opts[:view_thread_text]
  s << '</a></noscript>'
  if opts[:show_powered_by]
    s << '<a href="http://disqus.com" class="dsq-brlink">blog comments '
    s << 'powered by <span class="logo-disqus">Disqus</span></a>'
  end
  s % [opts[:account], opts[:account]]
end
top_commenters(opts = {}) click to toggle source

Show the top commenters Disqus thread widget. Options:

  • account: Your Discus account (required).

  • header: HTML snipper with header (default h2) tag and text.

  • show_powered_by: Show or hide the powered by Disqus text.

  • num_items:: How many items to show.

  • hide_mods: Don’t show moderators.

  • hide_avatars: Don’t show avatars.

  • avatar_size: Avatar size.

# File lib/disqussion/widget.rb, line 91
def top_commenters(opts = {})
  opts = Disqus::defaults.merge(opts)
  opts[:header] ||= '<h2 class="dsq-widget-title">Top Commenters</h2>'
  validate_opts!(opts)        
  s = '<div id="dsq-topcommenters" class="dsq-widget">'
  s << opts[:header]
  s << '<script type="text/javascript" src="'
  s << TOP
  s << '&hide_avatars=1' if opts[:hide_avatars]
  s << '&hide_mods=1' if opts[:hide_mods]
  s << '"></script>'
  s << '</div>'
  if opts[:show_powered_by]
    s << '<a href="http://disqus.com">Powered by Disqus</a>'
  end
  s % [opts[:account], opts[:num_items], opts[:avatar_size], opts[:orientation]]
end

Private Class Methods

validate_opts!(opts) click to toggle source
# File lib/disqussion/widget.rb, line 173
def validate_opts!(opts)
  raise ArgumentError.new("You must specify an :account") if !opts[:account]
  raise ArgumentError.new("Invalid color") if opts[:color] && !VALID_COLORS.include?(opts[:color])
  raise ArgumentError.new("Invalid num_items") if opts[:num_items] && !VALID_NUM_ITEMS.include?(opts[:num_items])
  raise ArgumentError.new("Invalid default_tab") if opts[:default_tab] && !VALID_DEFAULT_TABS.include?(opts[:default_tab])
  raise ArgumentError.new("Invalid avatar size") if opts[:avatar_size] && !VALID_AVATAR_SIZES.include?(opts[:avatar_size])
  raise ArgumentError.new("Invalid orientation") if opts[:orientation] && !VALID_ORIENTATIONS.include?(opts[:orientation])
end