Class: BlogBasic::BlogComment

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/blog_basic/blog_comment.rb

Instance Method Summary collapse

Instance Method Details

#akismet_attributesObject



90
91
92
93
94
95
96
97
98
99
100
101
# File 'app/models/blog_basic/blog_comment.rb', line 90

def akismet_attributes
  {
    :key                  => BlogBasic::BlogConf.data['akismet_key'],
    :blog                 => BlogBasic::BlogConf.data['blog_url'],
    :user_ip              => user_ip,
    :user_agent           => user_agent,
    :comment_author       => name,
    :comment_author_email => email,
    :comment_author_url   => site_url,
    :comment_content      => body
  }
end

#check_for_spamObject



78
79
80
81
82
83
84
85
86
87
88
# File 'app/models/blog_basic/blog_comment.rb', line 78

def check_for_spam
  if BlogBasic::BlogConf.data['akismet_key'] && BlogBasic::BlogConf.data['blog_url']
    if Akismetor.spam?(akismet_attributes)
      self.errors.add_to_base('This comment has been detected as spam')
      return false
    else
      return true
    end
  end
  true
end

#formatted_created_atObject



47
48
49
# File 'app/models/blog_basic/blog_comment.rb', line 47

def formatted_created_at
  self.created_at.strftime(BlogBasic::BlogConf.data['post_date_format'] || '%m/%d/%Y at %I:%M%p')
end

#parsed_bodyObject



51
52
53
54
55
56
# File 'app/models/blog_basic/blog_comment.rb', line 51

def parsed_body
  # # Going to add markdown/html support later for comments
  # self.code_highlight_and_markdown(self.body, {:escape_html => true})

  self.body
end

#request=(request) ⇒ Object

Used to set more tracking for akismet



72
73
74
75
76
# File 'app/models/blog_basic/blog_comment.rb', line 72

def request=(request)
  self.user_ip    = request.remote_ip
  self.user_agent = request.env['HTTP_USER_AGENT']
  self.referrer   = request.env['HTTP_REFERER']
end

#siteObject



67
68
69
# File 'app/models/blog_basic/blog_comment.rb', line 67

def site
  self.site_url.blank? ? "" : self.site_url
end

#user_image_tagObject



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
# File 'app/models/blog_basic/blog_comment.rb', line 15

def user_image_tag
  if self.user && self.user.respond_to?(:blog_image_url) && self.user.blog_image_url
    # Load image from model
    ret = "<img src=\"#{self.user.blog_image_url}\" />"
  elsif BlogConf.data['gravatar']
    # Gravatar
    require 'digest/md5'
    if self.respond_to?(:email) && !self.email.blank?
      email = self.email
    elsif self.user && self.user.respond_to?(:email) && !self.user.email.blank?
      email = self.user.email
    else
      return ''
    end

    hash = Digest::MD5.hexdigest(email.downcase)
    ret = "<img src=\"http://www.gravatar.com/avatar/#{hash}.jpg\" />"
  else
    # No Image
    return ''
  end

  return ret.html_safe if ret.respond_to?(:html_safe)
  return ret
end

#user_nameObject



58
59
60
61
62
63
64
65
# File 'app/models/blog_basic/blog_comment.rb', line 58

def user_name
  name = self.user ? self.user.name : self.name
  if !self.site_url.blank? && false
    return "<a href=\"".html_safe << self.site_url << "\">".html_safe << name << "</a>".html_safe
  else
    return name
  end
end

#validateObject



41
42
43
44
45
# File 'app/models/blog_basic/blog_comment.rb', line 41

def validate
  if !self.user
    self.errors.add(:name, 'is required') if self.name.blank?
  end
end