From ac1f2107366a9ca75900708795fda1d6dd1a4e4d Mon Sep 17 00:00:00 2001 From: Tom Hughes Date: Tue, 25 Jun 2013 21:54:02 +0100 Subject: [PATCH] Replace the spam observer with simple after_save callbacks --- app/models/diary_comment.rb | 5 +++++ app/models/diary_entry.rb | 5 +++++ app/models/spam_observer.rb | 15 --------------- app/models/user.rb | 9 +++++++++ config/application.rb | 5 ----- 5 files changed, 19 insertions(+), 20 deletions(-) delete mode 100644 app/models/spam_observer.rb diff --git a/app/models/diary_comment.rb b/app/models/diary_comment.rb index 075d288ed..bea1c7f0d 100644 --- a/app/models/diary_comment.rb +++ b/app/models/diary_comment.rb @@ -8,6 +8,7 @@ class DiaryComment < ActiveRecord::Base attr_accessible :body after_initialize :set_defaults + after_save :spam_check def body RichText.new(read_attribute(:body_format), read_attribute(:body)) @@ -27,4 +28,8 @@ private def set_defaults self.body_format = "markdown" unless self.attribute_present?(:body_format) end + + def spam_check + user.spam_check + end end diff --git a/app/models/diary_entry.rb b/app/models/diary_entry.rb index 99a56b2ce..de2a42ae3 100644 --- a/app/models/diary_entry.rb +++ b/app/models/diary_entry.rb @@ -27,6 +27,7 @@ class DiaryEntry < ActiveRecord::Base attr_accessible :title, :body, :language_code, :latitude, :longitude after_initialize :set_defaults + after_save :spam_check def body RichText.new(read_attribute(:body_format), read_attribute(:body)) @@ -37,4 +38,8 @@ private def set_defaults self.body_format = "markdown" unless self.attribute_present?(:body_format) end + + def spam_check + user.spam_check + end end diff --git a/app/models/spam_observer.rb b/app/models/spam_observer.rb deleted file mode 100644 index 07fa84ffd..000000000 --- a/app/models/spam_observer.rb +++ /dev/null @@ -1,15 +0,0 @@ -class SpamObserver < ActiveRecord::Observer - observe User, DiaryEntry, DiaryComment - - def after_save(record) - case - when record.is_a?(User) then user = record - when record.is_a?(DiaryEntry) then user = record.user - when record.is_a?(DiaryComment) then user = record.user - end - - if user.status == "active" and user.spam_score > SPAM_THRESHOLD - user.update_column(:status, "suspended") - end - end -end diff --git a/app/models/user.rb b/app/models/user.rb index 778afbf8a..6677d3b98 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -52,6 +52,7 @@ class User < ActiveRecord::Base after_initialize :set_defaults before_save :encrypt_password + after_save :spam_check has_attached_file :image, :default_url => "/assets/:class/:attachment/:style.png", @@ -216,6 +217,14 @@ class User < ActiveRecord::Base return score.to_i end + ## + # perform a spam check on a user + def spam_check + if status == "active" and spam_score > SPAM_THRESHOLD + update_column(:status, "suspended") + end + end + ## # return an oauth access token for a specified application def access_token(application_key) diff --git a/config/application.rb b/config/application.rb index 879f5714d..d510a6a7c 100644 --- a/config/application.rb +++ b/config/application.rb @@ -32,11 +32,6 @@ module OpenStreetMap # :all can be used as a placeholder for all plugins not explicitly named. # config.plugins = [ :exception_notification, :ssl_requirement, :all ] - # Activate observers that should always be running. - unless STATUS == :database_offline - config.active_record.observers = :spam_observer - end - # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone. # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC. # config.time_zone = 'Central Time (US & Canada)' -- 2.43.2