Class: Comments

Inherits:
Dynasnip show all
Defined in:
lib/vanilla/dynasnips/comments.rb

Instance Attribute Summary

Attributes inherited from Vanilla::Renderers::Base

#app

Instance Method Summary collapse

Methods inherited from Dynasnip

all, attribute, build_snip, #method_missing, persist!, persist_all!, snip_attributes, snip_name, usage

Methods inherited from Vanilla::Renderers::Base

escape_curly_braces, #include_snips, #initialize, #prepare, #process_text, #raw_content, render, #render, #render_without_including_snips, snip_regexp

Constructor Details

This class inherits a constructor from Vanilla::Renderers::Base

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Dynasnip

Instance Method Details

#check_for_spam(comment) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/vanilla/dynasnips/comments.rb', line 67

def check_for_spam(comment)
  snip_date = Date.parse(Soup[app.request.params[:snip]].updated_at)
  Defensio.configure(app.config[:defensio])
  defensio_params = {
    :comment_author_email => comment[:email], 
    :comment_author => comment[:author], 
    :comment_author_url => comment[:website],
    :comment_content => comment[:content],
    :comment_type => "comment", 
    :user_ip => app.request.ip, 
    :article_date => snip_date.strftime("%Y/%m/%d")
  }
  audit = Defensio.audit_comment(defensio_params)
  
  # Augment the comment hash
  comment[:user_ip] = app.request.ip
  comment[:spamminess] = audit["defensio_result"]["spaminess"]
  comment[:spam] = audit["defensio_result"]["spam"]
  comment[:defensio_signature] = audit["defensio_result"]["signature"]
  comment[:defensio_message] = audit["defensio_result"]["message"] if audit["defensio_result"]["message"]
  comment[:defensio_status] = audit["defensio_result"]["status"]
  comment
end

#get(snip_name = nil, disable_new_comments = false) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/vanilla/dynasnips/comments.rb', line 16

def get(snip_name=nil, disable_new_comments=false)
  snip_name = snip_name || app.request.params[:snip]
  return usage if self.class.snip_name == snip_name
  comments = Soup.sieve(:commenting_on => snip_name)
  comments_html = if app.request.snip_name == snip_name
    rendered_comments = render_comments(comments)
    rendered_comments += comment_form.gsub('SNIP_NAME', snip_name) unless disable_new_comments
    rendered_comments
  else
   %{<a href="#{Vanilla::Routes.url_to(snip_name)}">#{comments.length} comments for #{snip_name}</a>}
  end
  return comments_html
end

#post(*args) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/vanilla/dynasnips/comments.rb', line 30

def post(*args)
  snip_name = app.request.params[:snip]
  existing_comments = Soup.sieve(:commenting_on => snip_name)
  comment = app.request.params.reject { |k,v| ![:author, :email, :website, :content].include?(k) }
  
  return "You need to add some details!" if comment.empty?
  
  comment = check_for_spam(comment)
  
  if comment[:spam]
    "Sorry - your comment looks like spam, according to Defensio :("
  else
    return "No spam today, thanks anyway" unless app.request.params[:human] == 'human'
    Soup << comment.merge({
     :name => "#{snip_name}-comment-#{existing_comments.length + 1}", 
     :commenting_on => snip_name,
     :created_at => Time.now
    })
    "Thanks for your comment! Back to {link_to #{snip_name}}"
  end
end

#render_comments(comments) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/vanilla/dynasnips/comments.rb', line 52

def render_comments(comments)
  "<h2>Comments</h2><ol class='comments'>" + comments.map do |comment|
    rendered_comment = comment_template.gsub('COMMENT_CONTENT', app.render(comment)).
                                        gsub('COMMENT_DATE', comment.created_at)
    author = comment.author
    author = "Anonymous" unless author && author != ""
    if comment.website && comment.website != ""
      rendered_comment.gsub!('COMMENT_AUTHOR', "<a href=\"#{comment.website}\">#{author}</a>")
    else
      rendered_comment.gsub!('COMMENT_AUTHOR', author)
    end
    rendered_comment
  end.join + "</ol>"    
end