adds editable methods to Debate

This commit is contained in:
Juanjo Bazán
2015-07-19 14:46:21 +02:00
parent bb5dc339fd
commit df1a800d94
2 changed files with 47 additions and 2 deletions

View File

@@ -3,7 +3,7 @@ class Debate < ActiveRecord::Base
acts_as_votable
acts_as_commentable
acts_as_taggable
belongs_to :author, class_name: 'User', foreign_key: 'author_id'
validates :title, presence: true
@@ -24,4 +24,12 @@ class Debate < ActiveRecord::Base
votes_for.size
end
def editable?
total_votes == 0
end
def editable_by?(user)
editable? && author == user
end
end

View File

@@ -29,5 +29,42 @@ describe Debate do
@debate.terms_of_service = nil
expect(@debate).to_not be_valid
end
describe "#editable?" do
before(:each) do
@debate = create(:debate)
end
it "should be true if debate has no votes yet" do
expect(@debate.total_votes).to eq(0)
expect(@debate.editable?).to be true
end
it "should be false if debate has votes" do
create(:vote, votable: @debate)
expect(@debate.total_votes).to eq(1)
expect(@debate.editable?).to be false
end
end
describe "#editable_by?" do
before(:each) do
@debate = create(:debate)
end
it "should be true if user is the author and debate is editable" do
expect(@debate.editable_by?(@debate.author)).to be true
end
it "should be false if debate is not editable" do
create(:vote, votable: @debate)
expect(@debate.editable_by?(@debate.author)).to be false
end
it "should be false if user is not the author" do
expect(@debate.editable_by?(create(:user))).to be false
end
end
end