refactors sanitizable behaviour

This commit is contained in:
rgarcia
2015-09-28 15:06:22 +02:00
parent 590c0b9242
commit 7b9f2c9695
3 changed files with 29 additions and 35 deletions

View File

@@ -0,0 +1,19 @@
module Sanitizable
extend ActiveSupport::Concern
included do
before_validation :sanitize_description
before_validation :sanitize_tag_list
end
protected
def sanitize_description
self.description = WYSIWYGSanitizer.new.sanitize(description)
end
def sanitize_tag_list
self.tag_list = TagSanitizer.new.sanitize_tag_list(self.tag_list)
end
end

View File

@@ -4,6 +4,7 @@ class Debate < ActiveRecord::Base
include Taggable
include Conflictable
include Measurable
include Sanitizable
apply_simple_captcha
@@ -23,9 +24,6 @@ class Debate < ActiveRecord::Base
validates :terms_of_service, acceptance: { allow_nil: false }, on: :create
before_validation :sanitize_description
before_validation :sanitize_tag_list
before_save :calculate_hot_score, :calculate_confidence_score
scope :for_render, -> { includes(:tags) }
@@ -39,6 +37,10 @@ class Debate < ActiveRecord::Base
# Ahoy setup
visitable # Ahoy will automatically assign visit_id on create
def description
super.try :html_safe
end
def likes
cached_votes_up
end
@@ -83,10 +85,6 @@ class Debate < ActiveRecord::Base
(cached_anonymous_votes_total.to_f / cached_votes_total) * 100
end
def description
super.try :html_safe
end
def after_commented
save # updates the hot_score because there is a before_save
end
@@ -120,14 +118,4 @@ class Debate < ActiveRecord::Base
self.tags.each{ |t| t.increment_custom_counter_for('Debate') }
end
protected
def sanitize_description
self.description = WYSIWYGSanitizer.new.sanitize(description)
end
def sanitize_tag_list
self.tag_list = TagSanitizer.new.sanitize_tag_list(self.tag_list)
end
end

View File

@@ -3,6 +3,7 @@ class Proposal < ActiveRecord::Base
include Taggable
include Conflictable
include Measurable
include Sanitizable
apply_simple_captcha
acts_as_votable
@@ -25,8 +26,6 @@ class Proposal < ActiveRecord::Base
validates :terms_of_service, acceptance: { allow_nil: false }, on: :create
before_validation :sanitize_description
before_validation :sanitize_tag_list
before_validation :set_responsible_name
before_save :calculate_hot_score, :calculate_confidence_score
@@ -39,18 +38,14 @@ class Proposal < ActiveRecord::Base
scope :sort_by_random, -> { order("RANDOM()") }
scope :sort_by_flags, -> { order(flags_count: :desc, updated_at: :desc) }
def description
super.try :html_safe
end
def total_votes
cached_votes_up
end
def description
super.try :html_safe
end
def description
super.try :html_safe
end
def editable?
total_votes <= Setting.value_for("max_votes_for_proposal_edit").to_i
end
@@ -107,14 +102,6 @@ class Proposal < ActiveRecord::Base
protected
def sanitize_description
self.description = WYSIWYGSanitizer.new.sanitize(description)
end
def sanitize_tag_list
self.tag_list = TagSanitizer.new.sanitize_tag_list(self.tag_list)
end
def set_responsible_name
if author && author.level_two_or_three_verified?
self.responsible_name = author.document_number