summaryrefslogtreecommitdiff
path: root/app/models/concerns
diff options
context:
space:
mode:
authorPatricio Cano <suprnova32@gmail.com>2016-08-05 17:10:08 -0500
committerPatricio Cano <suprnova32@gmail.com>2016-08-15 13:18:15 -0500
commit43e756d4eafd79f4d2f366b646ebb94af78b5a4c (patch)
tree07949d3368affcda301fd266e1e5bf0649474b23 /app/models/concerns
parent7179165af7553720089a0b7e7024374c371e2f90 (diff)
downloadgitlab-ce-43e756d4eafd79f4d2f366b646ebb94af78b5a4c.tar.gz
Refactored AkismetHelper into AkismetService and cleaned up `Spammable`
- Refactored SpamCheckService into SpamService
Diffstat (limited to 'app/models/concerns')
-rw-r--r--app/models/concerns/spammable.rb68
1 files changed, 28 insertions, 40 deletions
diff --git a/app/models/concerns/spammable.rb b/app/models/concerns/spammable.rb
index f272e7c5a55..694e2efcade 100644
--- a/app/models/concerns/spammable.rb
+++ b/app/models/concerns/spammable.rb
@@ -1,55 +1,36 @@
module Spammable
extend ActiveSupport::Concern
- include Gitlab::AkismetHelper
module ClassMethods
- def attr_spammable(*attrs)
- attrs.each do |attr|
- spammable_attrs << attr.to_s
- end
+ def attr_spammable(attr, options = {})
+ spammable_attrs << [attr.to_s, options]
end
end
included do
has_one :user_agent_detail, as: :subject, dependent: :destroy
attr_accessor :spam
- after_validation :check_for_spam, on: :create
+ after_validation :spam_detected?, on: :create
cattr_accessor :spammable_attrs, instance_accessor: false do
[]
end
+ delegate :submitted?, to: :user_agent_detail, allow_nil: true
end
def can_be_submitted?
if user_agent_detail
- user_agent_detail.submittable? && akismet_enabled?
+ user_agent_detail.submittable?
else
false
end
end
- def submit_spam
- return unless akismet_enabled? && can_be_submitted?
- spam!(user_agent_detail, spammable_text, owner)
- end
-
- def spam_detected?(env)
- @spam = is_spam?(env, owner, spammable_text)
- end
-
def spam?
@spam
end
- def submitted?
- if user_agent_detail
- user_agent_detail.submitted
- else
- false
- end
- end
-
- def check_for_spam
+ def spam_detected?
self.errors.add(:base, "Your #{self.class.name.underscore} has been recognized as spam and has been discarded.") if spam?
end
@@ -61,34 +42,41 @@ module Spammable
end
end
- def to_ability_name
- self.class.to_s.underscore
- end
-
- # Override this method if an additional check is needed before calling Akismet
- def check_for_spam?
- akismet_enabled?
+ def owner
+ User.find(owner_id)
end
def spam_title
- raise NotImplementedError
+ attr = self.class.spammable_attrs.select do |_, options|
+ options.fetch(:spam_title, false)
+ end
+
+ attr = attr[0].first
+
+ public_send(attr) if respond_to?(attr.to_sym)
end
def spam_description
- raise NotImplementedError
- end
+ attr = self.class.spammable_attrs.select do |_, options|
+ options.fetch(:spam_description, false)
+ end
- private
+ attr = attr[0].first
+
+ public_send(attr) if respond_to?(attr.to_sym)
+ end
def spammable_text
result = []
- self.class.spammable_attrs.each do |entry|
- result << self.send(entry)
+ self.class.spammable_attrs.map do |attr|
+ result << public_send(attr.first)
end
+
result.reject(&:blank?).join("\n")
end
- def owner
- User.find(owner_id)
+ # Override in Spammable if further checks are necessary
+ def check_for_spam?
+ current_application_settings.akismet_enabled
end
end