From 8e42055ece6002d6ff6fe3437e654fe3d753a4a0 Mon Sep 17 00:00:00 2001 From: rgarcia Date: Mon, 28 Sep 2015 13:33:57 +0200 Subject: [PATCH] refactors taggable behaviour --- app/models/concerns/taggable.rb | 20 ++++++++++++++++++++ app/models/debate.rb | 16 ++-------------- app/models/proposal.rb | 16 ++-------------- 3 files changed, 24 insertions(+), 28 deletions(-) create mode 100644 app/models/concerns/taggable.rb diff --git a/app/models/concerns/taggable.rb b/app/models/concerns/taggable.rb new file mode 100644 index 000000000..1cce0152d --- /dev/null +++ b/app/models/concerns/taggable.rb @@ -0,0 +1,20 @@ +module Taggable + extend ActiveSupport::Concern + + included do + acts_as_taggable + end + + def tag_list_with_limit(limit = nil) + return tags if limit.blank? + + tags.sort{|a,b| b.taggings_count <=> a.taggings_count}[0, limit] + end + + def tags_count_out_of_limit(limit = nil) + return 0 unless limit + + count = tags.size - limit + count < 0 ? 0 : count + end +end diff --git a/app/models/debate.rb b/app/models/debate.rb index 721dfb9cc..4e495281f 100644 --- a/app/models/debate.rb +++ b/app/models/debate.rb @@ -1,10 +1,11 @@ require 'numeric' class Debate < ActiveRecord::Base include Flaggable + include Taggable + apply_simple_captcha acts_as_votable - acts_as_taggable acts_as_paranoid column: :hidden_at include ActsAsParanoidAliases @@ -84,19 +85,6 @@ class Debate < ActiveRecord::Base super.try :html_safe end - def tag_list_with_limit(limit = nil) - return tags if limit.blank? - - tags.sort{|a,b| b.taggings_count <=> a.taggings_count}[0, limit] - end - - def tags_count_out_of_limit(limit = nil) - return 0 unless limit - - count = tags.size - limit - count < 0 ? 0 : count - end - def after_commented save # updates the hot_score because there is a before_save end diff --git a/app/models/proposal.rb b/app/models/proposal.rb index 5d21be8d2..f2693d8ad 100644 --- a/app/models/proposal.rb +++ b/app/models/proposal.rb @@ -1,9 +1,10 @@ class Proposal < ActiveRecord::Base include Flaggable + include Taggable + apply_simple_captcha acts_as_votable - acts_as_taggable acts_as_paranoid column: :hidden_at include ActsAsParanoidAliases @@ -50,19 +51,6 @@ class Proposal < ActiveRecord::Base super.try :html_safe end - def tag_list_with_limit(limit = nil) - return tags if limit.blank? - - tags.sort{|a,b| b.taggings_count <=> a.taggings_count}[0, limit] - end - - def tags_count_out_of_limit(limit = nil) - return 0 unless limit - - count = tags.size - limit - count < 0 ? 0 : count - end - def description super.try :html_safe end