diff --git a/app/models/debate.rb b/app/models/debate.rb index d141e4330..f2723a864 100644 --- a/app/models/debate.rb +++ b/app/models/debate.rb @@ -122,6 +122,14 @@ class Debate < ActiveRecord::Base cached_votes_up/flags_count.to_f < 5 end + def after_hide + self.tags.each{ |t| t.decrement_custom_counter_for('Debate') } + end + + def after_restore + self.tags.each{ |t| t.increment_custom_counter_for('Debate') } + end + def self.title_max_length @@title_max_length ||= self.columns.find { |c| c.name == 'title' }.limit || 80 end diff --git a/app/models/proposal.rb b/app/models/proposal.rb index 24045e8f7..5d21be8d2 100644 --- a/app/models/proposal.rb +++ b/app/models/proposal.rb @@ -105,6 +105,14 @@ class Proposal < ActiveRecord::Base cached_votes_up) end + def after_hide + self.tags.each{ |t| t.decrement_custom_counter_for('Proposal') } + end + + def after_restore + self.tags.each{ |t| t.increment_custom_counter_for('Proposal') } + end + def self.title_max_length @@title_max_length ||= self.columns.find { |c| c.name == 'title' }.limit || 80 end diff --git a/lib/acts_as_paranoid_aliases.rb b/lib/acts_as_paranoid_aliases.rb index 6fd875ef6..7f2cfe4bd 100644 --- a/lib/acts_as_paranoid_aliases.rb +++ b/lib/acts_as_paranoid_aliases.rb @@ -4,6 +4,7 @@ module ActsAsParanoidAliases base.extend(ClassMethods) def hide + return false if hidden? update_attribute(:hidden_at, Time.now) after_hide end @@ -24,8 +25,13 @@ module ActsAsParanoidAliases end def restore(opts={}) + return false unless hidden? super(opts) update_attribute(:confirmed_hide_at, nil) + after_restore + end + + def after_restore end end @@ -48,12 +54,12 @@ module ActsAsParanoidAliases def hide_all(ids) return if ids.blank? - where(id: ids).update_all(hidden_at: Time.now) + where(id: ids).each(&:hide) end def restore_all(ids) return if ids.blank? - only_hidden.where(id: ids).update_all(hidden_at: nil) + only_hidden.where(id: ids).each(&:restore) end end end diff --git a/spec/models/debate_spec.rb b/spec/models/debate_spec.rb index 907bcef5b..1ab7b8a06 100644 --- a/spec/models/debate_spec.rb +++ b/spec/models/debate_spec.rb @@ -350,6 +350,20 @@ describe Debate do end end + describe "custom tag counters when hiding/restoring" do + it "decreases the tag counter when hiden, and increases it when restored" do + debate = create(:debate, tag_list: "foo") + tag = ActsAsTaggableOn::Tag.where(name: 'foo').first + expect(tag.debates_count).to eq(1) + + debate.hide + expect(tag.reload.debates_count).to eq(0) + + debate.restore + expect(tag.reload.debates_count).to eq(1) + end + end + describe "conflictive debates" do it "should return true when it has more than 1 flag for 5 positive votes" do diff --git a/spec/models/proposal_spec.rb b/spec/models/proposal_spec.rb index f713f3ce5..ff200fca9 100644 --- a/spec/models/proposal_spec.rb +++ b/spec/models/proposal_spec.rb @@ -193,6 +193,20 @@ describe Proposal do end end + describe "custom tag counters when hiding/restoring" do + it "decreases the tag counter when hiden, and increases it when restored" do + proposal = create(:proposal, tag_list: "foo") + tag = ActsAsTaggableOn::Tag.where(name: 'foo').first + expect(tag.proposals_count).to eq(1) + + proposal.hide + expect(tag.reload.proposals_count).to eq(0) + + proposal.restore + expect(tag.reload.proposals_count).to eq(1) + end + end + describe "#confidence_score" do it "takes into account votes" do