Commit ae38c043 authored by azul's avatar azul

feature: Post.with_link? to detect possible spam posts

parent 3c0d07be
......@@ -194,6 +194,12 @@ class Post < ActiveRecord::Base
"post_#{id}_body"
end
def with_link?
format_body
body_html.gsub(/<(\/*)a\s([^>]*?)>/) { |_m| return true }
return false
end
protected
def post_created
......
......@@ -37,5 +37,7 @@ purple_orange:
commentable_type: "User"
<% end %>
on_blues_page:
commentable_id: 1002
commentable_type: "Page"
......@@ -8,3 +8,29 @@ gerrard_to_blue:
<% updated = created = rand(46080) %>
updated_at: "<%= 10.minutes.ago.to_s(:db) %>"
created_at: "<%= 10.minutes.ago.to_s(:db) %>"
html_link:
user_id: 4
discussion: on_blues_page
body: |
Post with a html link
<a href="http://crabgrass.example">link</a>
auto_link:
user_id: 4
discussion: on_blues_page
body: |
Post with an automatic link:
https://crabgrass.example
greencloth_link:
user_id: 4
discussion: on_blues_page
body: |
Post with a [greencloth link]
no_link:
user_id: 4
discussion: on_blues_page
body: |
Post without any link
require 'test_helper'
class PostTest < ActiveSupport::TestCase
fixtures :posts
def test_with_link
[:greencloth_link, :auto_link, :html_link].each do |fixture_name|
assert posts(fixture_name).with_link?,
"Post fixture '#{fixture_name}' has a link but with_link? says it doesn't."
end
fixture_name = :no_link
refute posts(fixture_name).with_link?,
"Post fixture '#{fixture_name}' has no link but with_link? says it does."
end
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment