Skip to content

Commit da84ed7

Browse files
committed
refactor SpamFilter
1 parent e3cf03e commit da84ed7

File tree

7 files changed

+64
-110
lines changed

7 files changed

+64
-110
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
.rspec-local
1515
.yardoc
1616
/.bundle
17-
/config/application.yml
17+
config/application.yml
18+
config/database.yml
1819
/db/*.sqlite3
1920
/db/*.sqlite3-journal
2021
/log/*.log

Gemfile

+1-2
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,6 @@ source 'https://rubygems.org' do
145145
gem 'annotate'
146146
gem 'fabrication-rails'
147147
gem 'ffaker'
148-
gem 'jazz_hands', github: 'nixme/jazz_hands', branch: 'bring-your-own-debugger'
149148
gem 'launchy'
150149
gem 'pry-byebug'
151150
#gem 'pry-rescue'
@@ -171,7 +170,7 @@ source 'https://rubygems.org' do
171170
gem 'timecop'
172171
gem 'vcr'
173172
gem 'webmock', '<1.16'
174-
gem 'stripe-ruby-mock', git: 'https://github.com/rebelidealist/stripe-ruby-mock', branch: 'live-tests'
173+
gem 'stripe-ruby-mock'
175174
end
176175

177176
group :production do

Gemfile.lock

+28-78
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,3 @@
1-
GIT
2-
remote: git://github.com/nixme/jazz_hands.git
3-
revision: 5e4b48f145883ecb14b55bf04eacc28ac9662676
4-
branch: bring-your-own-debugger
5-
specs:
6-
jazz_hands (0.5.2)
7-
awesome_print (~> 1.2)
8-
coolline (>= 0.4.2)
9-
hirb (~> 0.7.1)
10-
pry (~> 0.9.12)
11-
pry-doc (~> 0.4.6)
12-
pry-git (~> 0.2.3)
13-
pry-rails (~> 0.3.2)
14-
pry-remote (>= 0.1.7)
15-
pry-stack_explorer (~> 0.4.9)
16-
railties (>= 3.0, < 5.0)
17-
18-
GIT
19-
remote: https://github.com/rebelidealist/stripe-ruby-mock
20-
revision: f603978fd1a38b1c27ff67cfa7d6ac95b71abe5c
21-
branch: live-tests
22-
specs:
23-
stripe-ruby-mock (2.0.0)
24-
dante (>= 0.2.0)
25-
jimson-temp
26-
stripe (>= 1.15.0)
27-
281
GEM
292
remote: https://rubygems.org/
303
remote: https://rails-assets.org/
@@ -150,8 +123,6 @@ GEM
150123
sprockets (< 2.13)
151124
connection_pool (2.2.0)
152125
cookiejar (0.3.2)
153-
coolline (0.5.0)
154-
unicode_utils (~> 1.4)
155126
crack (0.4.2)
156127
safe_yaml (~> 1.0.0)
157128
crass (1.0.2)
@@ -167,7 +138,6 @@ GEM
167138
descendants_tracker (0.0.4)
168139
thread_safe (~> 0.3, >= 0.3.1)
169140
diff-lcs (1.2.5)
170-
diffy (3.0.7)
171141
docile (1.1.5)
172142
domain_name (0.5.24)
173143
unf (>= 0.0.5, < 1.0.0)
@@ -342,10 +312,6 @@ GEM
342312
json
343313
mime-types
344314
oauth
345-
grit (2.5.0)
346-
diff-lcs (~> 1.1)
347-
mime-types (~> 1.15)
348-
posix-spawn (~> 0.3.6)
349315
guard (2.12.5)
350316
formatador (>= 0.2.4)
351317
listen (~> 2.7)
@@ -368,7 +334,6 @@ GEM
368334
rack (>= 1.4.5)
369335
highline (1.7.2)
370336
hike (1.2.3)
371-
hirb (0.7.3)
372337
hitimes (1.2.2)
373338
http (0.5.1)
374339
http_parser.rb
@@ -493,7 +458,6 @@ GEM
493458
multi_json (~> 1.0)
494459
websocket-driver (>= 0.2.0)
495460
polyglot (0.3.5)
496-
posix-spawn (0.3.11)
497461
postgres_ext (1.0.0)
498462
activerecord (~> 3.2.0)
499463
pg_array_parser (~> 0.0.9)
@@ -505,21 +469,6 @@ GEM
505469
pry-byebug (1.3.2)
506470
byebug (~> 2.7)
507471
pry (~> 0.9.12)
508-
pry-doc (0.4.6)
509-
pry (>= 0.9)
510-
yard (>= 0.8)
511-
pry-git (0.2.3)
512-
diffy
513-
grit
514-
pry (>= 0.9.8)
515-
pry-rails (0.3.4)
516-
pry (>= 0.9.10)
517-
pry-remote (0.1.8)
518-
pry (~> 0.9)
519-
slop (~> 3.0)
520-
pry-stack_explorer (0.4.9.2)
521-
binding_of_caller (>= 0.7)
522-
pry (>= 0.9.11)
523472
pubnub (0.1.9)
524473
em-http-request (>= 1.0.2)
525474
json
@@ -707,6 +656,10 @@ GEM
707656
stripe (1.21.0)
708657
json (~> 1.8.1)
709658
rest-client (~> 1.4)
659+
stripe-ruby-mock (2.1.0)
660+
dante (>= 0.2.0)
661+
jimson-temp
662+
stripe (>= 1.20.1)
710663
strong_parameters (0.2.3)
711664
actionpack (~> 3.0)
712665
activemodel (~> 3.0)
@@ -767,7 +720,6 @@ GEM
767720
unf (0.1.4)
768721
unf_ext
769722
unf_ext (0.0.7.1)
770-
unicode_utils (1.4.0)
771723
vcr (2.9.3)
772724
webmock (1.15.2)
773725
addressable (>= 2.2.7)
@@ -778,7 +730,6 @@ GEM
778730
websocket-extensions (0.1.2)
779731
xpath (2.0.0)
780732
nokogiri (~> 1.3)
781-
yard (0.8.7.6)
782733

783734
PLATFORMS
784735
ruby
@@ -792,19 +743,19 @@ DEPENDENCIES
792743
awesome_print!
793744
better_errors!
794745
binding_of_caller!
795-
bugsnag
796-
capybara
797-
capybara-screenshot
746+
bugsnag!
747+
capybara!
748+
capybara-screenshot!
798749
carrierwave!
799750
carrierwave_backgrounder!
800751
chronic!
801752
clockwork!
802-
codeclimate-test-reporter
753+
codeclimate-test-reporter!
803754
coffee-rails!
804755
color!
805756
compass-rails!
806757
createsend!
807-
database_cleaner
758+
database_cleaner!
808759
dotenv-rails!
809760
elasticsearch-model!
810761
elasticsearch-rails!
@@ -817,20 +768,19 @@ DEPENDENCIES
817768
foreman!
818769
friendly_id (= 4.0.10.1)!
819770
fukuzatsu!
820-
fuubar
771+
fuubar!
821772
geocoder!
822773
github-markdown!
823774
grackle!
824775
guard-rspec!
825776
haml!
826777
hashie!
827-
heroku-deflater
828-
jazz_hands!
778+
heroku-deflater!
829779
jbuilder!
830780
jquery-rails (= 2.0.3)!
831781
kaminari!
832782
kramdown!
833-
launchy
783+
launchy!
834784
linkedin!
835785
local_time!
836786
mail_view (~> 2.0.4)!
@@ -839,7 +789,7 @@ DEPENDENCIES
839789
mixpanel!
840790
multi_json!
841791
never_wastes!
842-
newrelic_rpm
792+
newrelic_rpm!
843793
nokogiri!
844794
octokit!
845795
oj!
@@ -849,37 +799,37 @@ DEPENDENCIES
849799
omniauth-linkedin (~> 0.0.6)!
850800
omniauth-twitter (~> 0.0.16)!
851801
pg!
852-
poltergeist
802+
poltergeist!
853803
postgres_ext!
854-
pry-byebug
804+
pry-byebug!
855805
pubnub (= 0.1.9)!
856-
puma
806+
puma!
857807
querystring!
858-
quiet_assets
859-
rack_session_access
808+
quiet_assets!
809+
rack_session_access!
860810
rails (~> 3.2)!
861811
rails-assets-font-awesome!
862812
rails-assets-jquery-cookie (= 1.4.0)!
863813
rails-assets-jquery-dropdown!
864814
rails-erd!
865-
rails_12factor
815+
rails_12factor!
866816
rails_autolink!
867817
rakismet!
868818
redcarpet!
869819
redis-rails (= 3.2.4)!
870820
rest-client!
871-
rspec-rails
821+
rspec-rails!
872822
rubocop!
873823
ruby-progressbar!
874824
sanitize!
875825
sass!
876826
sass-rails!
877827
selectize-rails!
878-
selenium-webdriver
879-
shoulda-matchers
828+
selenium-webdriver!
829+
shoulda-matchers!
880830
sidekiq!
881831
simple_form!
882-
simplecov
832+
simplecov!
883833
sinatra!
884834
sitemap_generator!
885835
slim-rails!
@@ -888,13 +838,13 @@ DEPENDENCIES
888838
stripe!
889839
stripe-ruby-mock!
890840
strong_parameters!
891-
syntax
892-
timecop
841+
syntax!
842+
timecop!
893843
tire!
894844
travis!
895-
turnip
845+
turnip!
896846
tweet-button!
897847
twitter!
898848
uglifier!
899-
vcr
900-
webmock (< 1.16)
849+
vcr!
850+
webmock (< 1.16)!

app/models/comment.rb

+1-14
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,11 @@
1717

1818
class Comment < ActiveRecord::Base
1919
include ActsAsCommentable::Comment
20-
include Rakismet::Model
20+
include SpamFilter
2121

2222
belongs_to :commentable, polymorphic: true
2323
has_many :likes, as: :likable, dependent: :destroy
24-
has_one :spam_report, as: :spammable
2524
after_create :generate_event
26-
after_save :analyze_spam
2725
after_save :commented_callback
2826

2927
default_scope order: 'likes_cache DESC, created_at ASC'
@@ -33,13 +31,6 @@ class Comment < ActiveRecord::Base
3331
alias_method :author, :user
3432
alias_attribute :body, :comment
3533

36-
rakismet_attrs author: :user_name,
37-
author_email: :user_email,
38-
content: :comment,
39-
blog: ENV['AKISMET_URL'],
40-
user_ip: :remote_ip,
41-
user_agent: :user_agent
42-
4334
validates :comment, length: { minimum: 2 }
4435

4536
def self.latest_comments_as_strings(count=5)
@@ -148,8 +139,4 @@ def event_type(options={})
148139
:new_comment
149140
end
150141
end
151-
152-
def analyze_spam
153-
AnalyzeSpamJob.perform_async({ id: id, klass: self.class.name })
154-
end
155142
end

app/models/concerns/spam_filter.rb

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
module SpamFilter
2+
extend ActiveSupport::Concern
3+
4+
included do
5+
after_save :analyze_spam
6+
has_one :spam_report, as: :spammable
7+
include Rakismet::Model
8+
9+
rakismet_attrs author: :user_name,
10+
author_email: :user_email,
11+
content: :body,
12+
blog: ENV['AKISMET_URL'],
13+
user_ip: :remote_ip,
14+
user_agent: :user_agent
15+
16+
def analyze_spam
17+
AnalyzeSpamJob.perform_async({ id: id, klass: self.class.name })
18+
end
19+
20+
end
21+
end

app/models/protip.rb

+3-15
Original file line numberDiff line numberDiff line change
@@ -40,27 +40,20 @@ class Protip < ActiveRecord::Base
4040
include Tire::Model::Search
4141
include Scoring::HotStream
4242
include SearchModule
43-
include Rakismet::Model
44-
4543
acts_as_commentable
4644

4745
include ProtipMapping
46+
include SpamFilter
4847

4948
paginates_per(PAGESIZE = 18)
5049

5150
URL_REGEX = /(?i)\b((?:[a-z][\w-]+:(?:\/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?]))/
5251

5352
has_many :likes, as: :likable, dependent: :destroy, after_add: :reset_likes_cache, after_remove: :reset_likes_cache
5453
has_many :protip_links, autosave: true, dependent: :destroy, after_add: :reset_links_cache, after_remove: :reset_links_cache
55-
has_one :spam_report, as: :spammable
5654
belongs_to :user , autosave: true
5755

58-
rakismet_attrs author: proc { self.user.name },
59-
author_email: proc { self.user.email },
60-
content: :body,
61-
blog: ENV['AKISMET_URL'],
62-
user_ip: proc { self.user.last_ip },
63-
user_agent: proc { self.user.last_ua }
56+
6457

6558
acts_as_taggable_on :topics, :users
6659
attr_accessor :upvotes
@@ -108,7 +101,7 @@ class Protip < ActiveRecord::Base
108101
after_save :index_search
109102
after_destroy :index_search_after_destroy
110103
after_create :update_network
111-
after_save :analyze_spam
104+
112105
# End of test failing lines
113106

114107
attr_accessor :upvotes_value
@@ -999,9 +992,4 @@ def need_to_extract_data_from_links
999992
def adjust_like_value(user, like_value)
1000993
user.is_a?(User) && self.author.team_member_of?(user) ? [like_value/2, 1].max : like_value
1001994
end
1002-
1003-
def analyze_spam
1004-
AnalyzeSpamJob.perform_async({ id: id, klass: self.class.name })
1005-
end
1006-
1007995
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class AddAuthorDetailsToProtip < ActiveRecord::Migration
2+
def change
3+
add_column :protips, :user_name, :string
4+
add_column :protips, :user_email, :string
5+
add_column :protips, :user_agent, :string
6+
add_column :protips, :user_ip, :inet
7+
end
8+
end

0 commit comments

Comments
 (0)