2
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2025-09-06 09:10:25 +08:00

Add rubocop to our build. (#5004)

This commit is contained in:
Guo Xiang Tan 2017-07-28 10:20:09 +09:00 committed by GitHub
parent ff4e295c4f
commit 5012d46cbd
871 changed files with 5480 additions and 6056 deletions

View file

@ -5,11 +5,11 @@ class MergePollsVotes < ActiveRecord::Migration
polls_votes = {}
PostCustomField.where(post_id: post_id).where("name LIKE 'polls-votes-%'").find_each do |pcf|
user_id = pcf.name["polls-votes-".size..-1]
polls_votes["#{user_id}"] = ::JSON.parse(pcf.value||"{}")
polls_votes["#{user_id}"] = ::JSON.parse(pcf.value || "{}")
end
pcf = PostCustomField.find_or_create_by(name: "polls-votes", post_id: post_id)
pcf.value = ::JSON.parse(pcf.value||"{}").merge(polls_votes).to_json
pcf.value = ::JSON.parse(pcf.value || "{}").merge(polls_votes).to_json
pcf.save
end
end

View file

@ -2,9 +2,9 @@ class ClosePollsInClosedTopics < ActiveRecord::Migration
def up
PostCustomField.joins(post: :topic)
.where("post_custom_fields.name = 'polls'")
.where("topics.closed")
.find_each do |pcf|
.where("post_custom_fields.name = 'polls'")
.where("topics.closed")
.find_each do |pcf|
polls = ::JSON.parse(pcf.value || "{}")
polls.values.each { |poll| poll["status"] = "closed" }
pcf.value = polls.to_json

View file

@ -101,7 +101,7 @@ module DiscoursePoll
post.save_custom_fields(true)
# publish the changes
MessageBus.publish("/polls/#{post.topic_id}", { post_id: post.id, polls: polls })
MessageBus.publish("/polls/#{post.topic_id}", post_id: post.id, polls: polls)
end
end

View file

@ -27,9 +27,9 @@ desc "Migrate old polls to new syntax"
task "poll:migrate_old_polls" => :environment do
# iterate over all polls
PluginStoreRow.where(plugin_name: "poll")
.where("key LIKE 'poll_options_%'")
.pluck(:key)
.each do |poll_options_key|
.where("key LIKE 'poll_options_%'")
.pluck(:key)
.each do |poll_options_key|
# extract the post_id
post_id = poll_options_key["poll_options_".length..-1].to_i
# load the post from the db
@ -56,9 +56,9 @@ task "poll:migrate_old_polls" => :environment do
options = post.custom_fields["polls"]["poll"]["options"]
# iterate over all votes
PluginStoreRow.where(plugin_name: "poll")
.where("key LIKE 'poll_vote_#{post_id}_%'")
.pluck(:key, :value)
.each do |poll_vote_key, vote|
.where("key LIKE 'poll_vote_#{post_id}_%'")
.pluck(:key, :value)
.each do |poll_vote_key, vote|
# extract the user_id
user_id = poll_vote_key["poll_vote_#{post_id}_%".length..-1].to_i
# find the selected option

View file

@ -136,7 +136,7 @@ after_initialize do
post.save_custom_fields(true)
MessageBus.publish("/polls/#{post.topic_id}", {post_id: post.id, polls: polls })
MessageBus.publish("/polls/#{post.topic_id}", post_id: post.id, polls: polls)
polls[poll_name]
end
@ -230,7 +230,7 @@ after_initialize do
end
next unless option["voter_ids"]
user_ids << option["voter_ids"].slice((params[:offset].to_i || 0) * 25, 25)
user_ids << option["voter_ids"].slice((params[:offset].to_i || 0) * 25, 25)
end
user_ids.flatten!
@ -295,7 +295,7 @@ after_initialize do
end
end
validate(:post, :validate_polls) do |force=nil|
validate(:post, :validate_polls) do |force = nil|
return if !SiteSetting.poll_enabled? && (self.user && !self.user.staff?)
# only care when raw has changed!
@ -360,9 +360,8 @@ after_initialize do
# tells the front-end we have a poll for that post
on(:post_created) do |post|
next if post.is_first_post? || post.custom_fields[DiscoursePoll::POLLS_CUSTOM_FIELD].blank?
MessageBus.publish("/polls/#{post.topic_id}", {
post_id: post.id,
polls: post.custom_fields[DiscoursePoll::POLLS_CUSTOM_FIELD]})
MessageBus.publish("/polls/#{post.topic_id}", post_id: post.id,
polls: post.custom_fields[DiscoursePoll::POLLS_CUSTOM_FIELD])
end
add_to_serializer(:post, :polls, false) do

View file

@ -13,7 +13,7 @@ describe ::DiscoursePoll::PollsController do
it "works" do
MessageBus.expects(:publish)
xhr :put, :vote, { post_id: poll.id, poll_name: "poll", options: ["5c24fc1df56d764b550ceae1b9319125"] }
xhr :put, :vote, post_id: poll.id, poll_name: "poll", options: ["5c24fc1df56d764b550ceae1b9319125"]
expect(response).to be_success
json = ::JSON.parse(response.body)
@ -23,7 +23,7 @@ describe ::DiscoursePoll::PollsController do
end
it "requires at least 1 valid option" do
xhr :put, :vote, { post_id: poll.id, poll_name: "poll", options: ["A", "B"] }
xhr :put, :vote, post_id: poll.id, poll_name: "poll", options: ["A", "B"]
expect(response).not_to be_success
json = ::JSON.parse(response.body)
@ -31,10 +31,10 @@ describe ::DiscoursePoll::PollsController do
end
it "supports vote changes" do
xhr :put, :vote, { post_id: poll.id, poll_name: "poll", options: ["5c24fc1df56d764b550ceae1b9319125"] }
xhr :put, :vote, post_id: poll.id, poll_name: "poll", options: ["5c24fc1df56d764b550ceae1b9319125"]
expect(response).to be_success
xhr :put, :vote, { post_id: poll.id, poll_name: "poll", options: ["e89dec30bbd9bf50fabf6a05b4324edf"] }
xhr :put, :vote, post_id: poll.id, poll_name: "poll", options: ["e89dec30bbd9bf50fabf6a05b4324edf"]
expect(response).to be_success
json = ::JSON.parse(response.body)
expect(json["poll"]["voters"]).to eq(1)
@ -44,13 +44,13 @@ describe ::DiscoursePoll::PollsController do
it "works even if topic is closed" do
topic.update_attribute(:closed, true)
xhr :put, :vote, { post_id: poll.id, poll_name: "poll", options: ["5c24fc1df56d764b550ceae1b9319125"] }
xhr :put, :vote, post_id: poll.id, poll_name: "poll", options: ["5c24fc1df56d764b550ceae1b9319125"]
expect(response).to be_success
end
it "ensures topic is not archived" do
topic.update_attribute(:archived, true)
xhr :put, :vote, { post_id: poll.id, poll_name: "poll", options: ["A"] }
xhr :put, :vote, post_id: poll.id, poll_name: "poll", options: ["A"]
expect(response).not_to be_success
json = ::JSON.parse(response.body)
expect(json["errors"][0]).to eq(I18n.t("poll.topic_must_be_open_to_vote"))
@ -58,21 +58,21 @@ describe ::DiscoursePoll::PollsController do
it "ensures post is not trashed" do
poll.trash!
xhr :put, :vote, { post_id: poll.id, poll_name: "poll", options: ["A"] }
xhr :put, :vote, post_id: poll.id, poll_name: "poll", options: ["A"]
expect(response).not_to be_success
json = ::JSON.parse(response.body)
expect(json["errors"][0]).to eq(I18n.t("poll.post_is_deleted"))
end
it "ensures polls are associated with the post" do
xhr :put, :vote, { post_id: Fabricate(:post).id, poll_name: "foobar", options: ["A"] }
xhr :put, :vote, post_id: Fabricate(:post).id, poll_name: "foobar", options: ["A"]
expect(response).not_to be_success
json = ::JSON.parse(response.body)
expect(json["errors"][0]).to eq(I18n.t("poll.no_polls_associated_with_this_post"))
end
it "checks the name of the poll" do
xhr :put, :vote, { post_id: poll.id, poll_name: "foobar", options: ["A"] }
xhr :put, :vote, post_id: poll.id, poll_name: "foobar", options: ["A"]
expect(response).not_to be_success
json = ::JSON.parse(response.body)
expect(json["errors"][0]).to eq(I18n.t("poll.no_poll_with_this_name", name: "foobar"))
@ -80,7 +80,7 @@ describe ::DiscoursePoll::PollsController do
it "ensures poll is open" do
closed_poll = create_post(raw: "[poll status=closed]\n- A\n- B\n[/poll]")
xhr :put, :vote, { post_id: closed_poll.id, poll_name: "poll", options: ["5c24fc1df56d764b550ceae1b9319125"] }
xhr :put, :vote, post_id: closed_poll.id, poll_name: "poll", options: ["5c24fc1df56d764b550ceae1b9319125"]
expect(response).not_to be_success
json = ::JSON.parse(response.body)
expect(json["errors"][0]).to eq(I18n.t("poll.poll_must_be_open_to_vote"))
@ -88,9 +88,9 @@ describe ::DiscoursePoll::PollsController do
it "doesn't discard anonymous votes when someone votes" do
default_poll = poll.custom_fields["polls"]["poll"]
add_anonymous_votes(poll, default_poll, 17, {"5c24fc1df56d764b550ceae1b9319125" => 11, "e89dec30bbd9bf50fabf6a05b4324edf" => 6})
add_anonymous_votes(poll, default_poll, 17, "5c24fc1df56d764b550ceae1b9319125" => 11, "e89dec30bbd9bf50fabf6a05b4324edf" => 6)
xhr :put, :vote, { post_id: poll.id, poll_name: "poll", options: ["5c24fc1df56d764b550ceae1b9319125"] }
xhr :put, :vote, post_id: poll.id, poll_name: "poll", options: ["5c24fc1df56d764b550ceae1b9319125"]
expect(response).to be_success
json = ::JSON.parse(response.body)
@ -148,7 +148,7 @@ describe ::DiscoursePoll::PollsController do
it "works for OP" do
MessageBus.expects(:publish)
xhr :put, :toggle_status, { post_id: poll.id, poll_name: "poll", status: "closed" }
xhr :put, :toggle_status, post_id: poll.id, poll_name: "poll", status: "closed"
expect(response).to be_success
json = ::JSON.parse(response.body)
expect(json["poll"]["status"]).to eq("closed")
@ -158,7 +158,7 @@ describe ::DiscoursePoll::PollsController do
log_in(:moderator)
MessageBus.expects(:publish)
xhr :put, :toggle_status, { post_id: poll.id, poll_name: "poll", status: "closed" }
xhr :put, :toggle_status, post_id: poll.id, poll_name: "poll", status: "closed"
expect(response).to be_success
json = ::JSON.parse(response.body)
expect(json["poll"]["status"]).to eq("closed")
@ -166,7 +166,7 @@ describe ::DiscoursePoll::PollsController do
it "ensures post is not trashed" do
poll.trash!
xhr :put, :toggle_status, { post_id: poll.id, poll_name: "poll", status: "closed" }
xhr :put, :toggle_status, post_id: poll.id, poll_name: "poll", status: "closed"
expect(response).not_to be_success
json = ::JSON.parse(response.body)
expect(json["errors"][0]).to eq(I18n.t("poll.post_is_deleted"))

View file

@ -12,7 +12,7 @@ describe PostsController do
describe "polls" do
it "works" do
xhr :post, :create, { title: title, raw: "[poll]\n- A\n- B\n[/poll]" }
xhr :post, :create, title: title, raw: "[poll]\n- A\n- B\n[/poll]"
expect(response).to be_success
json = ::JSON.parse(response.body)
expect(json["cooked"]).to match("data-poll-")
@ -21,7 +21,7 @@ describe PostsController do
it "works on any post" do
post = Fabricate(:post)
xhr :post, :create, { topic_id: post.topic.id, raw: "[poll]\n- A\n- B\n[/poll]" }
xhr :post, :create, topic_id: post.topic.id, raw: "[poll]\n- A\n- B\n[/poll]"
expect(response).to be_success
json = ::JSON.parse(response.body)
expect(json["cooked"]).to match("data-poll-")
@ -29,14 +29,14 @@ describe PostsController do
end
it "should have different options" do
xhr :post, :create, { title: title, raw: "[poll]\n- A\n- A\n[/poll]" }
xhr :post, :create, title: title, raw: "[poll]\n- A\n- A\n[/poll]"
expect(response).not_to be_success
json = ::JSON.parse(response.body)
expect(json["errors"][0]).to eq(I18n.t("poll.default_poll_must_have_different_options"))
end
it "should have at least 2 options" do
xhr :post, :create, { title: title, raw: "[poll]\n- A\n[/poll]" }
xhr :post, :create, title: title, raw: "[poll]\n- A\n[/poll]"
expect(response).not_to be_success
json = ::JSON.parse(response.body)
expect(json["errors"][0]).to eq(I18n.t("poll.default_poll_must_have_at_least_2_options"))
@ -47,7 +47,7 @@ describe PostsController do
(SiteSetting.poll_maximum_options + 1).times { |n| raw << "\n- #{n}" }
raw << "\n[/poll]"
xhr :post, :create, { title: title, raw: raw }
xhr :post, :create, title: title, raw: raw
expect(response).not_to be_success
json = ::JSON.parse(response.body)
@ -55,14 +55,14 @@ describe PostsController do
end
it "should have valid parameters" do
xhr :post, :create, { title: title, raw: "[poll type=multiple min=5]\n- A\n- B\n[/poll]" }
xhr :post, :create, title: title, raw: "[poll type=multiple min=5]\n- A\n- B\n[/poll]"
expect(response).not_to be_success
json = ::JSON.parse(response.body)
expect(json["errors"][0]).to eq(I18n.t("poll.default_poll_with_multiple_choices_has_invalid_parameters"))
end
it "prevents self-xss" do
xhr :post, :create, { title: title, raw: "[poll name=<script>alert('xss')</script>]\n- A\n- B\n[/poll]" }
xhr :post, :create, title: title, raw: "[poll name=<script>alert('xss')</script>]\n- A\n- B\n[/poll]"
expect(response).to be_success
json = ::JSON.parse(response.body)
expect(json["cooked"]).to match("data-poll-")
@ -71,7 +71,7 @@ describe PostsController do
end
it "also works whe there is a link starting with '[poll'" do
xhr :post, :create, { title: title, raw: "[Polls are awesome](/foobar)\n[poll]\n- A\n- B\n[/poll]" }
xhr :post, :create, title: title, raw: "[Polls are awesome](/foobar)\n[poll]\n- A\n- B\n[/poll]"
expect(response).to be_success
json = ::JSON.parse(response.body)
expect(json["cooked"]).to match("data-poll-")
@ -79,7 +79,7 @@ describe PostsController do
end
it "prevents pollception" do
xhr :post, :create, { title: title, raw: "[poll name=1]\n- A\n[poll name=2]\n- B\n- C\n[/poll]\n- D\n[/poll]" }
xhr :post, :create, title: title, raw: "[poll name=1]\n- A\n[poll name=2]\n- B\n- C\n[/poll]\n- D\n[/poll]"
expect(response).to be_success
json = ::JSON.parse(response.body)
expect(json["cooked"]).to match("data-poll-")
@ -93,13 +93,13 @@ describe PostsController do
let(:post_id) do
freeze_time(4.minutes.ago) do
xhr :post, :create, { title: title, raw: "[poll]\n- A\n- B\n[/poll]" }
xhr :post, :create, title: title, raw: "[poll]\n- A\n- B\n[/poll]"
::JSON.parse(response.body)["id"]
end
end
it "can be changed" do
xhr :put, :update, { id: post_id, post: { raw: "[poll]\n- A\n- B\n- C\n[/poll]" } }
xhr :put, :update, id: post_id, post: { raw: "[poll]\n- A\n- B\n- C\n[/poll]" }
expect(response).to be_success
json = ::JSON.parse(response.body)
expect(json["post"]["polls"]["poll"]["options"][2]["html"]).to eq("C")
@ -107,7 +107,7 @@ describe PostsController do
it "resets the votes" do
DiscoursePoll::Poll.vote(post_id, "poll", ["5c24fc1df56d764b550ceae1b9319125"], user)
xhr :put, :update, { id: post_id, post: { raw: "[poll]\n- A\n- B\n- C\n[/poll]" } }
xhr :put, :update, id: post_id, post: { raw: "[poll]\n- A\n- B\n- C\n[/poll]" }
expect(response).to be_success
json = ::JSON.parse(response.body)
expect(json["post"]["polls_votes"]).to_not be
@ -123,7 +123,7 @@ describe PostsController do
let(:post_id) do
freeze_time(6.minutes.ago) do
xhr :post, :create, { title: title, raw: poll }
xhr :post, :create, title: title, raw: poll
::JSON.parse(response.body)["id"]
end
end
@ -137,7 +137,7 @@ describe PostsController do
describe "with no vote" do
it "OP can change the options" do
xhr :put, :update, { id: post_id, post: { raw: new_option } }
xhr :put, :update, id: post_id, post: { raw: new_option }
expect(response).to be_success
json = ::JSON.parse(response.body)
expect(json["post"]["polls"]["poll"]["options"][1]["html"]).to eq("C")
@ -145,14 +145,14 @@ describe PostsController do
it "staff can change the options" do
log_in_user(Fabricate(:moderator))
xhr :put, :update, { id: post_id, post: { raw: new_option } }
xhr :put, :update, id: post_id, post: { raw: new_option }
expect(response).to be_success
json = ::JSON.parse(response.body)
expect(json["post"]["polls"]["poll"]["options"][1]["html"]).to eq("C")
end
it "support changes on the post" do
xhr :put, :update, { id: post_id, post: { raw: updated } }
xhr :put, :update, id: post_id, post: { raw: updated }
expect(response).to be_success
json = ::JSON.parse(response.body)
expect(json["post"]["cooked"]).to match("before")
@ -167,7 +167,7 @@ describe PostsController do
end
it "OP cannot change the options" do
xhr :put, :update, { id: post_id, post: { raw: new_option } }
xhr :put, :update, id: post_id, post: { raw: new_option }
expect(response).not_to be_success
json = ::JSON.parse(response.body)
expect(json["errors"][0]).to eq(I18n.t(
@ -178,7 +178,7 @@ describe PostsController do
it "staff can change the options and votes are merged" do
log_in_user(Fabricate(:moderator))
xhr :put, :update, { id: post_id, post: { raw: new_option } }
xhr :put, :update, id: post_id, post: { raw: new_option }
expect(response).to be_success
json = ::JSON.parse(response.body)
expect(json["post"]["polls"]["poll"]["options"][1]["html"]).to eq("C")
@ -190,10 +190,10 @@ describe PostsController do
it "staff can change the options and anonymous votes are merged" do
post = Post.find_by(id: post_id)
default_poll = post.custom_fields["polls"]["poll"]
add_anonymous_votes(post, default_poll, 7, {"5c24fc1df56d764b550ceae1b9319125" => 7})
add_anonymous_votes(post, default_poll, 7, "5c24fc1df56d764b550ceae1b9319125" => 7)
log_in_user(Fabricate(:moderator))
xhr :put, :update, { id: post_id, post: { raw: new_option } }
xhr :put, :update, id: post_id, post: { raw: new_option }
expect(response).to be_success
json = ::JSON.parse(response.body)
@ -204,7 +204,7 @@ describe PostsController do
end
it "support changes on the post" do
xhr :put, :update, { id: post_id, post: { raw: updated } }
xhr :put, :update, id: post_id, post: { raw: updated }
expect(response).to be_success
json = ::JSON.parse(response.body)
expect(json["post"]["cooked"]).to match("before")
@ -221,14 +221,14 @@ describe PostsController do
describe "named polls" do
it "should have different options" do
xhr :post, :create, { title: title, raw: "[poll name=""foo""]\n- A\n- A\n[/poll]" }
xhr :post, :create, title: title, raw: "[poll name=""foo""]\n- A\n- A\n[/poll]"
expect(response).not_to be_success
json = ::JSON.parse(response.body)
expect(json["errors"][0]).to eq(I18n.t("poll.named_poll_must_have_different_options", name: "foo"))
end
it "should have at least 2 options" do
xhr :post, :create, { title: title, raw: "[poll name='foo']\n- A\n[/poll]" }
xhr :post, :create, title: title, raw: "[poll name='foo']\n- A\n[/poll]"
expect(response).not_to be_success
json = ::JSON.parse(response.body)
expect(json["errors"][0]).to eq(I18n.t("poll.named_poll_must_have_at_least_2_options", name: "foo"))
@ -239,7 +239,7 @@ describe PostsController do
describe "multiple polls" do
it "works" do
xhr :post, :create, { title: title, raw: "[poll]\n- A\n- B\n[/poll]\n[poll name=foo]\n- A\n- B\n[/poll]" }
xhr :post, :create, title: title, raw: "[poll]\n- A\n- B\n[/poll]\n[poll name=foo]\n- A\n- B\n[/poll]"
expect(response).to be_success
json = ::JSON.parse(response.body)
expect(json["cooked"]).to match("data-poll-")
@ -248,14 +248,14 @@ describe PostsController do
end
it "should have a name" do
xhr :post, :create, { title: title, raw: "[poll]\n- A\n- B\n[/poll]\n[poll]\n- A\n- B\n[/poll]" }
xhr :post, :create, title: title, raw: "[poll]\n- A\n- B\n[/poll]\n[poll]\n- A\n- B\n[/poll]"
expect(response).not_to be_success
json = ::JSON.parse(response.body)
expect(json["errors"][0]).to eq(I18n.t("poll.multiple_polls_without_name"))
end
it "should have unique name" do
xhr :post, :create, { title: title, raw: "[poll name=foo]\n- A\n- B\n[/poll]\n[poll name=foo]\n- A\n- B\n[/poll]" }
xhr :post, :create, title: title, raw: "[poll name=foo]\n- A\n- B\n[/poll]\n[poll name=foo]\n- A\n- B\n[/poll]"
expect(response).not_to be_success
json = ::JSON.parse(response.body)
expect(json["errors"][0]).to eq(I18n.t("poll.multiple_polls_with_same_name", name: "foo"))

View file

@ -13,10 +13,8 @@ describe "DiscoursePoll endpoints" do
user
)
get "/polls/voters.json", {
post_id: post.id,
poll_name: DiscoursePoll::DEFAULT_POLL_NAME
}
get "/polls/voters.json", post_id: post.id,
poll_name: DiscoursePoll::DEFAULT_POLL_NAME
expect(response.status).to eq(200)
@ -36,11 +34,9 @@ describe "DiscoursePoll endpoints" do
user
)
get "/polls/voters.json", {
post_id: post.id,
poll_name: DiscoursePoll::DEFAULT_POLL_NAME,
option_id: 'e89dec30bbd9bf50fabf6a05b4324edf'
}
get "/polls/voters.json", post_id: post.id,
poll_name: DiscoursePoll::DEFAULT_POLL_NAME,
option_id: 'e89dec30bbd9bf50fabf6a05b4324edf'
expect(response.status).to eq(200)
@ -57,7 +53,7 @@ describe "DiscoursePoll endpoints" do
describe 'when post_id is blank' do
it 'should raise the right error' do
expect { get "/polls/voters.json", { poll_name: DiscoursePoll::DEFAULT_POLL_NAME } }
expect { get "/polls/voters.json", poll_name: DiscoursePoll::DEFAULT_POLL_NAME }
.to raise_error(ActionController::ParameterMissing)
end
end
@ -65,17 +61,15 @@ describe "DiscoursePoll endpoints" do
describe 'when post_id is not valid' do
it 'should raise the right error' do
expect do
get "/polls/voters.json", {
post_id: -1,
poll_name: DiscoursePoll::DEFAULT_POLL_NAME
}
get "/polls/voters.json", post_id: -1,
poll_name: DiscoursePoll::DEFAULT_POLL_NAME
end.to raise_error(Discourse::InvalidParameters, 'post_id is invalid')
end
end
describe 'when poll_name is blank' do
it 'should raise the right error' do
expect { get "/polls/voters.json", { post_id: post.id } }
expect { get "/polls/voters.json", post_id: post.id }
.to raise_error(ActionController::ParameterMissing)
end
end
@ -101,10 +95,8 @@ describe "DiscoursePoll endpoints" do
user
)
get "/polls/voters.json", {
post_id: post.id,
poll_name: DiscoursePoll::DEFAULT_POLL_NAME
}
get "/polls/voters.json", post_id: post.id,
poll_name: DiscoursePoll::DEFAULT_POLL_NAME
expect(response.status).to eq(200)

View file

@ -71,7 +71,6 @@ describe ::DiscoursePoll::PollsValidator do
)
end
it 'should ensure that polls have at least 2 options' do
raw = <<~RAW
[poll]