Merge pull request #409 from AyuntamientoMadrid/conflictive_debates

Conflictive debates
This commit is contained in:
Juanjo Bazán
2015-09-07 17:29:05 +02:00
6 changed files with 85 additions and 6 deletions

View File

@@ -134,6 +134,11 @@ class Debate < ActiveRecord::Base
terms.present? ? where("title ILIKE ? OR description ILIKE ?", "%#{terms}%", "%#{terms}%") : none
end
def conflictive?
return false unless flags_count > 0 && cached_votes_up > 0
flags_count / cached_votes_up.to_f > 0.2
end
protected
def sanitize_description

View File

@@ -1,6 +1,6 @@
class Flag < ActiveRecord::Base
belongs_to :user
belongs_to :flaggable, polymorphic: true, counter_cache: true
belongs_to :flaggable, polymorphic: true, counter_cache: true, touch: true
scope(:by_user_and_flaggable, lambda do |user, flaggable|
where(user_id: user.id,

View File

@@ -13,11 +13,11 @@
<% end %>
<h1><%= @debate.title %></h1>
<!-- SI HAY 1 DENUNCIA POR CADA 5 VOTOS POSITIVOS MUESTRA ESTE MENSAJE -->
<div class="alert-box alert radius margin-top">
<strong><%= t("debates.show.flag") %></strong>
</div>
<!-- /. SI HAY 1 DENUNCIA POR CADA 5 VOTOS POSITIVOS MUESTRA ESTE MENSAJE -->
<% if @debate.conflictive? %>
<div class="alert-box alert radius margin-top">
<strong><%= t("debates.show.flag") %></strong>
</div>
<% end %>
<div class="debate-info">
<%= avatar_image(@debate.author, seed: @debate.author_id, size: 32, class: 'author-photo') %>

View File

@@ -77,6 +77,13 @@ FactoryGirl.define do
trait :with_hot_score do
before(:save) { |d| d.calculate_hot_score }
end
trait :conflictive do
after :create do |debate|
Flag.flag(FactoryGirl.create(:user), debate)
4.times { create(:vote, votable: debate) }
end
end
end
factory :vote do
@@ -88,6 +95,11 @@ FactoryGirl.define do
end
end
factory :flag do
association :flaggable, factory: :debate
association :user, factory: :user
end
factory :comment do
association :commentable, factory: :debate
user

View File

@@ -453,4 +453,15 @@ feature 'Debates' do
expect(page).to_not have_content(debate3.title)
end
end
scenario 'Conflictive' do
good_debate = create(:debate)
conflictive_debate = create(:debate, :conflictive)
visit debate_path(conflictive_debate)
expect(page).to have_content "This debate has been flag as innapropiate for some users."
visit debate_path(good_debate)
expect(page).to_not have_content "This debate has been flag as innapropiate for some users."
end
end

View File

@@ -281,6 +281,11 @@ describe Debate do
.to change { debate.updated_at }
end
it "should expire cache when it has a new flag", :focus do
expect { create(:flag, flaggable: debate) }
.to change { debate.reload.updated_at }
end
it "should expire cache when it has a new tag" do
expect { debate.update(tag_list: "new tag") }
.to change { debate.updated_at }
@@ -308,4 +313,50 @@ describe Debate do
end
end
describe "conflictive debates" do
it "should return true when it has more than 1 flag for 5 positive votes" do
debate.update(flags_count: 1)
debate.update(cached_votes_up: 4)
expect(debate).to be_conflictive
debate.update(flags_count: 2)
debate.update(cached_votes_up: 9)
expect(debate).to be_conflictive
debate.update(flags_count: 3)
debate.update(cached_votes_up: 14)
expect(debate).to be_conflictive
debate.update(flags_count: 20)
debate.update(cached_votes_up: 2)
expect(debate).to be_conflictive
end
it "should return false when it has less than or equal to 1 flag for 5 positive votes" do
debate.update(flags_count: 1)
debate.update(cached_votes_up: 5)
expect(debate).to_not be_conflictive
debate.update(flags_count: 2)
debate.update(cached_votes_up: 10)
expect(debate).to_not be_conflictive
debate.update(flags_count: 2)
debate.update(cached_votes_up: 100)
expect(debate).to_not be_conflictive
end
it "should return false when it has no flags" do
debate.update(flags_count: 0)
expect(debate).to_not be_conflictive
end
it "should return false when it has not votes up" do
debate.update(cached_votes_up: 0)
expect(debate).to_not be_conflictive
end
end
end