Module: ActiveRecord::Acts::Disqusable::ClassMethods

Defined in:
lib/acts_as_disqusable.rb

Instance Method Summary collapse

Instance Method Details

#acts_as_disqusable(options = {}) ⇒ Object

Macro that adds theme for object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/acts_as_disqusable.rb', line 12

def acts_as_disqusable(options = {})
  @@configuration = YAML::load(File.open("#{RAILS_ROOT}/config/disqus.yml"))
  @@configuration.update(options) if options.is_a?(Hash)
  include ActiveRecord::Acts::Disqusable::InstanceMethods
  
  after_create :create_thread

  def thread_column
    @@configuration[:thread_column] || 'thread_id'
  end          
  
  def title_column
    @@configuration[:title_column] || 'title'
  end
  
  def slug_column
    @@configuration[:slug_column] || 'slug'
  end
  
  def prefix
    if @@configuration[:prefix].nil?
      return self.to_s.downcase + '-'
    elsif @@configuration[:prefix]
      return @@configuration[:prefix] + '-'
    else
      return ''
    end
  end

  def forum_id
    @@configuration[:forum_id].to_s || nil
  end
  
  def forum_api_key
    @@configuration[:forum_api_key] || nil
  end
  
  def forum_shortname
    @@configuration[:forum_shortname] || nil
  end
  
  def threads
    Disqus::Forum.find(self.forum_id).threads
  end        
  
  def find_thread_by_url(url)
    Disqus::Forum.get_thread_by_url(url, self.forum_api_key)
  end
  
  def comment_count(ids={})
    if self.column_names.include? self.thread_column
      if !ids.is_a? Hash
        c = "id IN (" + ids.join(',')+ ")"
      else
        c = nil
      end
      thread_ids = Post.all(:conditions => c, :select => :thread_id).collect(&:thread_id).compact.join(',')
      if thread_ids.length > 0
        Disqus::Forum.posts_count(thread_ids, self.forum_api_key)
      else 
        Hash.new
      end
    else
      'This operation is not supported without a thread_column.'
    end
  end
  
  # limit — Number of entries that should be included in the response. Default is 25.
  # start — Starting point for the query. Default is 0.
  # filter — Type of entries that should be returned (new, spam or killed).
  # exclude — Type of entries that should be excluded from the response (new, spam or killed).
  def comments(opts={})
    Disqus::Forum.posts(self.forum_api_key, opts)
  end
  
  def comment_permalink(comment, opts={})
    title = comment.thread['title']
    if comment.thread['url']
      permalink = comment.thread['url']
    else
      url = comment.thread['identifier'].first.sub(self.prefix, '')
      path = opts[:path] || self.to_s.pluralize.downcase
      permalink = "/#{path}/#{url}"
    end
    return "<a href=\"#{permalink}\" class=\"#{opts[:class]}\" title=\"#{opts[:title]}\">#{title}</a>"
  end
end

#comment_count(ids = {}) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/acts_as_disqusable.rb', line 61

def comment_count(ids={})
  if self.column_names.include? self.thread_column
    if !ids.is_a? Hash
      c = "id IN (" + ids.join(',')+ ")"
    else
      c = nil
    end
    thread_ids = Post.all(:conditions => c, :select => :thread_id).collect(&:thread_id).compact.join(',')
    if thread_ids.length > 0
      Disqus::Forum.posts_count(thread_ids, self.forum_api_key)
    else 
      Hash.new
    end
  else
    'This operation is not supported without a thread_column.'
  end
end


87
88
89
90
91
92
93
94
95
96
97
# File 'lib/acts_as_disqusable.rb', line 87

def comment_permalink(comment, opts={})
  title = comment.thread['title']
  if comment.thread['url']
    permalink = comment.thread['url']
  else
    url = comment.thread['identifier'].first.sub(self.prefix, '')
    path = opts[:path] || self.to_s.pluralize.downcase
    permalink = "/#{path}/#{url}"
  end
  return "<a href=\"#{permalink}\" class=\"#{opts[:class]}\" title=\"#{opts[:title]}\">#{title}</a>"
end

#comments(opts = {}) ⇒ Object

limit — Number of entries that should be included in the response. Default is 25. start — Starting point for the query. Default is 0. filter — Type of entries that should be returned (new, spam or killed). exclude — Type of entries that should be excluded from the response (new, spam or killed).



83
84
85
# File 'lib/acts_as_disqusable.rb', line 83

def comments(opts={})
  Disqus::Forum.posts(self.forum_api_key, opts)
end

#find_thread_by_url(url) ⇒ Object



57
58
59
# File 'lib/acts_as_disqusable.rb', line 57

def find_thread_by_url(url)
  Disqus::Forum.get_thread_by_url(url, self.forum_api_key)
end

#forum_api_keyObject



45
46
47
# File 'lib/acts_as_disqusable.rb', line 45

def forum_api_key
  @@configuration[:forum_api_key] || nil
end

#forum_idObject



41
42
43
# File 'lib/acts_as_disqusable.rb', line 41

def forum_id
  @@configuration[:forum_id].to_s || nil
end

#forum_shortnameObject



49
50
51
# File 'lib/acts_as_disqusable.rb', line 49

def forum_shortname
  @@configuration[:forum_shortname] || nil
end

#prefixObject



31
32
33
34
35
36
37
38
39
# File 'lib/acts_as_disqusable.rb', line 31

def prefix
  if @@configuration[:prefix].nil?
    return self.to_s.downcase + '-'
  elsif @@configuration[:prefix]
    return @@configuration[:prefix] + '-'
  else
    return ''
  end
end

#slug_columnObject



27
28
29
# File 'lib/acts_as_disqusable.rb', line 27

def slug_column
  @@configuration[:slug_column] || 'slug'
end

#thread_columnObject



19
20
21
# File 'lib/acts_as_disqusable.rb', line 19

def thread_column
  @@configuration[:thread_column] || 'thread_id'
end

#threadsObject



53
54
55
# File 'lib/acts_as_disqusable.rb', line 53

def threads
  Disqus::Forum.find(self.forum_id).threads
end

#title_columnObject



23
24
25
# File 'lib/acts_as_disqusable.rb', line 23

def title_column
  @@configuration[:title_column] || 'title'
end