Add upvote:/downvote: editing metatags.
This commit is contained in:
@@ -680,7 +680,7 @@ class Post < ActiveRecord::Base
|
||||
|
||||
def filter_metatags(tags)
|
||||
@pre_metatags, tags = tags.partition {|x| x =~ /\A(?:rating|parent|-parent|source|-?locked):/i}
|
||||
@post_metatags, tags = tags.partition {|x| x =~ /\A(?:-pool|pool|newpool|fav|-fav|child|-favgroup|favgroup):/i}
|
||||
@post_metatags, tags = tags.partition {|x| x =~ /\A(?:-pool|pool|newpool|fav|-fav|child|-favgroup|favgroup|upvote|downvote):/i}
|
||||
apply_pre_metatags
|
||||
return tags
|
||||
end
|
||||
@@ -719,6 +719,9 @@ class Post < ActiveRecord::Base
|
||||
when /^-fav:(.+)$/i
|
||||
remove_favorite!(CurrentUser.user)
|
||||
|
||||
when /^(up|down)vote:(.+)$/i
|
||||
vote!($1)
|
||||
|
||||
when /^child:(.+)$/i
|
||||
child = Post.find($1)
|
||||
child.parent_id = id
|
||||
@@ -1030,12 +1033,16 @@ class Post < ActiveRecord::Base
|
||||
end
|
||||
|
||||
def vote!(score)
|
||||
if can_be_voted_by?(CurrentUser.user)
|
||||
vote = PostVote.create(:post_id => id, :score => score)
|
||||
self.score += vote.score
|
||||
else
|
||||
unless CurrentUser.is_voter?
|
||||
raise PostVote::Error.new("You do not have permission to vote")
|
||||
end
|
||||
|
||||
unless can_be_voted_by?(CurrentUser.user)
|
||||
raise PostVote::Error.new("You have already voted for this post")
|
||||
end
|
||||
|
||||
PostVote.create(:post_id => id, :score => score)
|
||||
reload # PostVote.create modifies our score. Reload to get the new score.
|
||||
end
|
||||
|
||||
def unvote!
|
||||
|
||||
@@ -7,6 +7,7 @@ FactoryGirl.define do
|
||||
default_image_size "large"
|
||||
base_upload_limit 10
|
||||
level 20
|
||||
created_at {Time.now}
|
||||
last_logged_in_at {Time.now}
|
||||
favorite_count 0
|
||||
bit_prefs 0
|
||||
|
||||
@@ -800,6 +800,50 @@ class PostTest < ActiveSupport::TestCase
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context "of" do
|
||||
setup do
|
||||
@gold = FactoryGirl.build(:gold_user)
|
||||
end
|
||||
|
||||
context "upvote:self or downvote:self" do
|
||||
context "by a member" do
|
||||
should "not upvote the post" do
|
||||
assert_raises PostVote::Error do
|
||||
@post.update(:tag_string => "upvote:self")
|
||||
end
|
||||
|
||||
assert_equal(0, @post.score)
|
||||
end
|
||||
|
||||
should "not downvote the post" do
|
||||
assert_raises PostVote::Error do
|
||||
@post.update(:tag_string => "downvote:self")
|
||||
end
|
||||
|
||||
assert_equal(0, @post.score)
|
||||
end
|
||||
end
|
||||
|
||||
context "by a gold user" do
|
||||
should "upvote the post" do
|
||||
CurrentUser.scoped(FactoryGirl.create(:gold_user)) do
|
||||
@post.update(:tag_string => "tag1 tag2 upvote:self")
|
||||
assert_equal(false, @post.errors.any?)
|
||||
assert_equal(1, @post.score)
|
||||
end
|
||||
end
|
||||
|
||||
should "downvote the post" do
|
||||
CurrentUser.scoped(FactoryGirl.create(:gold_user)) do
|
||||
@post.update(:tag_string => "tag1 tag2 downvote:self")
|
||||
assert_equal(false, @post.errors.any?)
|
||||
assert_equal(-1, @post.score)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context "tagged with a negated tag" do
|
||||
@@ -1733,8 +1777,16 @@ class PostTest < ActiveSupport::TestCase
|
||||
end
|
||||
end
|
||||
|
||||
should "not allow members to vote" do
|
||||
@user = FactoryGirl.create(:user)
|
||||
@post = FactoryGirl.create(:post)
|
||||
CurrentUser.scoped(@user) do
|
||||
assert_raises(PostVote::Error) { @post.vote!("up") }
|
||||
end
|
||||
end
|
||||
|
||||
should "not allow duplicate votes" do
|
||||
user = FactoryGirl.create(:user)
|
||||
user = FactoryGirl.create(:gold_user)
|
||||
post = FactoryGirl.create(:post)
|
||||
CurrentUser.scoped(user, "127.0.0.1") do
|
||||
assert_nothing_raised {post.vote!("up")}
|
||||
@@ -1746,7 +1798,7 @@ class PostTest < ActiveSupport::TestCase
|
||||
end
|
||||
|
||||
should "allow undoing of votes" do
|
||||
user = FactoryGirl.create(:user)
|
||||
user = FactoryGirl.create(:gold_user)
|
||||
post = FactoryGirl.create(:post)
|
||||
|
||||
# We deliberately don't call post.reload until the end to verify that
|
||||
|
||||
Reference in New Issue
Block a user