From 7b9f2c9695fee6f9766f1a26aae07dff030c6b15 Mon Sep 17 00:00:00 2001 From: rgarcia Date: Mon, 28 Sep 2015 15:06:22 +0200 Subject: [PATCH] refactors sanitizable behaviour --- app/models/concerns/sanitizable.rb | 19 +++++++++++++++++++ app/models/debate.rb | 22 +++++----------------- app/models/proposal.rb | 23 +++++------------------ 3 files changed, 29 insertions(+), 35 deletions(-) create mode 100644 app/models/concerns/sanitizable.rb diff --git a/app/models/concerns/sanitizable.rb b/app/models/concerns/sanitizable.rb new file mode 100644 index 000000000..65c8431ef --- /dev/null +++ b/app/models/concerns/sanitizable.rb @@ -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 diff --git a/app/models/debate.rb b/app/models/debate.rb index ee767cb46..e4854a4f0 100644 --- a/app/models/debate.rb +++ b/app/models/debate.rb @@ -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 diff --git a/app/models/proposal.rb b/app/models/proposal.rb index c0de5bace..b14d2e194 100644 --- a/app/models/proposal.rb +++ b/app/models/proposal.rb @@ -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