Merge pull request #300 from AyuntamientoMadrid/flag-helper

Flag helper
This commit is contained in:
Raimond Garcia
2015-09-01 15:19:16 +02:00
19 changed files with 133 additions and 112 deletions

View File

@@ -68,7 +68,7 @@ FactoryGirl.define do
trait :flagged do
after :create do |debate|
Flag.flag!(FactoryGirl.create(:user), debate)
Flag.flag(FactoryGirl.create(:user), debate)
end
end
end
@@ -101,7 +101,7 @@ FactoryGirl.define do
trait :flagged do
after :create do |debate|
Flag.flag!(FactoryGirl.create(:user), debate)
Flag.flag(FactoryGirl.create(:user), debate)
end
end
end

View File

@@ -155,7 +155,7 @@ feature 'Comments' do
user = create(:user)
debate = create(:debate)
comment = create(:comment, commentable: debate)
Flag.flag!(user, comment)
Flag.flag(user, comment)
login_as(user)
visit debate_path(debate)

View File

@@ -335,7 +335,7 @@ feature 'Debates' do
scenario "Unflagging", :js do
user = create(:user)
debate = create(:debate)
Flag.flag!(user, debate)
Flag.flag(user, debate)
login_as(user)
visit debate_path(debate)
@@ -432,9 +432,9 @@ feature 'Debates' do
expect(current_url).to include('tag=Deporte')
expect(page).to have_selector('#debates .debate', count: 2)
expect(page).to_not have_content(debate3.title)
expect(page).to have_content(debate1.title)
expect(page).to have_content(debate2.title)
expect(page).to_not have_content(debate3.title)
expect(page).to have_content(debate1.title)
expect(page).to have_content(debate2.title)
end
end
end

View File

@@ -33,9 +33,10 @@ describe Ability do
describe 'flagging content' do
it { should be_able_to(:flag, debate) }
it { should_not be_able_to(:unflag, debate) }
it { should be_able_to(:unflag, debate) }
it { should be_able_to(:flag, comment) }
it { should_not be_able_to(:unflag, comment) }
it { should be_able_to(:unflag, comment) }
describe "own comments" do
let(:own_comment) { create(:comment, author: user) }
@@ -50,20 +51,6 @@ describe Ability do
it { should_not be_able_to(:flag, own_debate) }
it { should_not be_able_to(:unflag, own_debate) }
end
describe "already-flagged comments" do
before(:each) { Flag.flag!(user, comment) }
it { should_not be_able_to(:flag, comment) }
it { should be_able_to(:unflag, comment) }
end
describe "already-flagged debates" do
before(:each) { Flag.flag!(user, debate) }
it { should_not be_able_to(:flag, debate) }
it { should be_able_to(:unflag, debate) }
end
end
describe "other users" do

View File

@@ -5,41 +5,41 @@ describe Flag do
let(:user) { create(:user) }
let(:comment) { create(:comment) }
describe '.flag!' do
describe '.flag' do
it 'creates a flag when there is none' do
expect { described_class.flag!(user, comment) }.to change{ Flag.count }.by(1)
expect { described_class.flag(user, comment) }.to change{ Flag.count }.by(1)
expect(Flag.last.user).to eq(user)
expect(Flag.last.flaggable).to eq(comment)
end
it 'raises an error if the flag has already been created' do
described_class.flag!(user, comment)
expect { described_class.flag!(user, comment) }.to raise_error(Flag::AlreadyFlaggedError)
it 'does nothing if the flag already exists' do
described_class.flag(user, comment)
expect(described_class.flag(user, comment)).to eq(false)
expect(Flag.by_user_and_flaggable(user, comment).count).to eq(1)
end
it 'increases the flag count' do
expect { described_class.flag!(user, comment) }.to change{ comment.reload.flags_count }.by(1)
expect { described_class.flag(user, comment) }.to change{ comment.reload.flags_count }.by(1)
end
end
describe '.unflag!' do
describe '.unflag' do
it 'raises an error if the flag does not exist' do
expect { described_class.unflag!(user, comment) }.to raise_error(Flag::NotFlaggedError)
expect(described_class.unflag(user, comment)).to eq(false)
end
describe 'when the flag already exists' do
before(:each) { described_class.flag!(user, comment) }
before(:each) { described_class.flag(user, comment) }
it 'removes an existing flag' do
expect { described_class.unflag!(user, comment) }.to change{ Flag.count }.by(-1)
expect { described_class.unflag(user, comment) }.to change{ Flag.count }.by(-1)
end
it 'decreases the flag count' do
expect { described_class.unflag!(user, comment) }.to change{ comment.reload.flags_count }.by(-1)
expect { described_class.unflag(user, comment) }.to change{ comment.reload.flags_count }.by(-1)
end
end
end
describe '.flagged?' do
@@ -48,7 +48,7 @@ describe Flag do
end
it 'returns true when the user has flagged the comment' do
described_class.flag!(user, comment)
described_class.flag(user, comment)
expect(described_class.flagged?(user, comment)).to be
end
end

View File

@@ -24,6 +24,28 @@ describe User do
end
end
describe "#comment_flags" do
let(:user) { create(:user) }
it "returns {} if no comment" do
expect(user.comment_flags([])).to eq({})
end
it "returns a hash of flaggable_ids with 'true' if they were flagged by the user" do
comment1 = create(:comment)
comment2 = create(:comment)
comment3 = create(:comment)
Flag.flag(user, comment1)
Flag.flag(user, comment3)
flagged = user.comment_flags([comment1, comment2, comment3])
expect(flagged[comment1.id]).to be
expect(flagged[comment2.id]).to_not be
expect(flagged[comment3.id]).to be
end
end
subject { build(:user) }
it "is valid" do

View File

@@ -3,34 +3,31 @@ require 'rails_helper'
describe 'Vote' do
describe '#for_debates' do
it 'returns votes for debates' do
debate = create(:debate)
create(:vote, votable: debate)
expect(Vote.for_debates.count).to eq(1)
end
it 'does not returns votes for other votables' do
debate = create(:debate)
comment = create(:comment)
create(:vote, votable: comment)
expect(Vote.for_debates.count).to eq(0)
end
end
describe '#in' do
it 'returns debates send in parameters' do
debate = create(:debate)
create(:vote, votable: debate)
expect(Vote.in(debate).count).to eq(1)
expect(Vote.for_debates(debate).count).to eq(0)
end
it 'does not return debates not in parameters' do
debate = create(:debate)
create(:vote, votable: debate)
it 'returns votes only for debates in parameters' do
debate1 = create(:debate)
debate2 = create(:debate)
create(:vote, votable: debate1)
expect(Vote.in([]).count).to eq(0)
expect(Vote.for_debates(debate1).count).to eq(1)
expect(Vote.for_debates(debate2).count).to eq(0)
end
it 'accepts more than 1 debate' do
debate1 = create(:debate)
debate2 = create(:debate)
debate3 = create(:debate)
create(:vote, votable: debate1)
create(:vote, votable: debate3)
expect(Vote.for_debates([debate1, debate2]).count).to eq(1)
end
end
@@ -43,4 +40,4 @@ describe 'Vote' do
expect(vote.value).to eq(false)
end
end
end
end