From bd41f2904fdbe7198af52251c0995252b3821a4f Mon Sep 17 00:00:00 2001 From: daiao <358551898@qq.com> Date: Thu, 11 Jul 2019 14:28:27 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=AE=9E=E8=AE=AD?= =?UTF-8?q?=E8=AF=84=E8=AE=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/discusses_controller.rb | 9 ++++++--- app/models/challenge.rb | 2 ++ app/models/discuss.rb | 1 + app/views/discusses/_discuss.json.jbuilder | 3 ++- ...0190711062033_modify_challenge_id_for_discusses.rb | 11 +++++++++++ 5 files changed, 22 insertions(+), 4 deletions(-) create mode 100644 db/migrate/20190711062033_modify_challenge_id_for_discusses.rb diff --git a/app/controllers/discusses_controller.rb b/app/controllers/discusses_controller.rb index db3047e74..f5de277b4 100644 --- a/app/controllers/discusses_controller.rb +++ b/app/controllers/discusses_controller.rb @@ -9,15 +9,18 @@ class DiscussesController < ApplicationController # 总数,分页使用 if current_user.admin? @disscuss_count = Discuss.where(:dis_id => @container.id, :dis_type => @container.class.to_s, :root_id => nil).count - @discusses = Discuss.limit(LIMIT).where(:dis_id => @container.id, :dis_type => @container.class.to_s, - :root_id => nil).includes(:user, :praise_treads).offset(offset) + disscusses = Discuss.where(:dis_id => @container.id, :dis_type => @container.class.to_s, + :root_id => nil).joins(challenge: :games) + .where("discusses.user_id = games.user_id").select("discusses.*, games.identifier") else disscusses = Discuss.where("dis_id = :dis_id and dis_type = :dis_type and root_id is null and (hidden = :hidden or user_id = :user_id)", {dis_id: @container.id, dis_type: @container.class.to_s, hidden: false, user_id: current_user.id}) + .joins(challenge: :games).where("discusses.user_id = games.user_id").select("discusses.*, games.identifier") @disscuss_count = disscusses.count - @discusses = disscusses.limit(LIMIT).includes(:user, :praise_treads).offset(offset) end + @discusses = disscusses.limit(LIMIT).includes(:user, :praise_treads).offset(offset) + @manger = @container.has_manager?(current_user) @current_user = current_user end diff --git a/app/models/challenge.rb b/app/models/challenge.rb index 7afb919a0..4d136a718 100644 --- a/app/models/challenge.rb +++ b/app/models/challenge.rb @@ -16,6 +16,8 @@ class Challenge < ApplicationRecord # 参考答案 has_many :challenge_answers, :dependent => :destroy has_many :exercise_bank_shixun_challenges, :dependent => :destroy + # 回复 + has_many :discusses, :dependent => :destroy # acts_as_attachable diff --git a/app/models/discuss.rb b/app/models/discuss.rb index 737a89ec1..00e3209c0 100644 --- a/app/models/discuss.rb +++ b/app/models/discuss.rb @@ -9,6 +9,7 @@ class Discuss < ApplicationRecord has_many :tidings, as: :container, dependent: :destroy has_one :praise_tread_cache, as: :object, dependent: :destroy + belongs_to :challenge after_create :send_tiding scope :children, -> (discuss_id){ where(parent_id: discuss_id).includes(:user).reorder(created_at: :asc) } diff --git a/app/views/discusses/_discuss.json.jbuilder b/app/views/discusses/_discuss.json.jbuilder index 992466559..ee9484131 100644 --- a/app/views/discusses/_discuss.json.jbuilder +++ b/app/views/discusses/_discuss.json.jbuilder @@ -9,7 +9,8 @@ json.shixun_id discuss.dis_id json.hidden discuss.hidden json.manage current_user.manager_of_shixun?(container) json.reward discuss.reward -json.game_url discuss.game_url(container, current_user) +#json.game_url discuss.game_url(container, current_user) +json.game_url "/tasks/#{discuss.identifier}" if @manger # 主贴和回复有一些不同点 if discuss.parent_id json.can_delete discuss.can_deleted?(current_user) diff --git a/db/migrate/20190711062033_modify_challenge_id_for_discusses.rb b/db/migrate/20190711062033_modify_challenge_id_for_discusses.rb new file mode 100644 index 000000000..86052375b --- /dev/null +++ b/db/migrate/20190711062033_modify_challenge_id_for_discusses.rb @@ -0,0 +1,11 @@ +class ModifyChallengeIdForDiscusses < ActiveRecord::Migration[5.2] + def change + discusses = Discuss.where(challenge_id: nil) + discusses.each do |dis| + challenge_id = Shixun.find(dis.dis_id).challenges.first.id + dis.update_column(:challenge_id, challenge_id) + end + add_index :discusses, :challenge_id + + end +end From 5af5fdc9138239fe31f39ca21bd12d5e3a603d62 Mon Sep 17 00:00:00 2001 From: daiao <358551898@qq.com> Date: Thu, 11 Jul 2019 14:31:03 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E5=AE=9E=E8=AE=AD=E8=AF=84=E8=AE=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/discusses_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/discusses_controller.rb b/app/controllers/discusses_controller.rb index f5de277b4..c09e9e285 100644 --- a/app/controllers/discusses_controller.rb +++ b/app/controllers/discusses_controller.rb @@ -17,7 +17,7 @@ class DiscussesController < ApplicationController (hidden = :hidden or user_id = :user_id)", {dis_id: @container.id, dis_type: @container.class.to_s, hidden: false, user_id: current_user.id}) .joins(challenge: :games).where("discusses.user_id = games.user_id").select("discusses.*, games.identifier") - @disscuss_count = disscusses.count + @disscuss_count = disscusses.count("discusses.id") end @discusses = disscusses.limit(LIMIT).includes(:user, :praise_treads).offset(offset) @manger = @container.has_manager?(current_user) From fc40905fdf7638380b7b2c3ef6fe3be770d67f65 Mon Sep 17 00:00:00 2001 From: daiao <358551898@qq.com> Date: Thu, 11 Jul 2019 14:33:06 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E5=AE=9E=E8=AE=AD=E8=AF=84=E8=AE=BA?= =?UTF-8?q?=E6=8A=A5500?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/discusses_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/discusses_controller.rb b/app/controllers/discusses_controller.rb index c09e9e285..f100ac233 100644 --- a/app/controllers/discusses_controller.rb +++ b/app/controllers/discusses_controller.rb @@ -14,7 +14,7 @@ class DiscussesController < ApplicationController .where("discusses.user_id = games.user_id").select("discusses.*, games.identifier") else disscusses = Discuss.where("dis_id = :dis_id and dis_type = :dis_type and root_id is null and - (hidden = :hidden or user_id = :user_id)", + (discusses.hidden = :hidden or discusses.user_id = :user_id)", {dis_id: @container.id, dis_type: @container.class.to_s, hidden: false, user_id: current_user.id}) .joins(challenge: :games).where("discusses.user_id = games.user_id").select("discusses.*, games.identifier") @disscuss_count = disscusses.count("discusses.id")