From 803d9d5d7dafac8ff313e1e8af578530b7336989 Mon Sep 17 00:00:00 2001 From: cxt <853663049@qq.com> Date: Thu, 24 Oct 2019 16:54:07 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AB=9E=E8=B5=9B=E6=8E=A5=E5=8F=A3=E8=B0=83?= =?UTF-8?q?=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/models/competition_module.rb | 26 ++++++++----------- .../competition_modules/show.json.jbuilder | 6 ++--- ...3706_migrate_competition_module_content.rb | 17 ++++++++++++ 3 files changed, 31 insertions(+), 18 deletions(-) create mode 100644 db/migrate/20191024083706_migrate_competition_module_content.rb diff --git a/app/models/competition_module.rb b/app/models/competition_module.rb index d520baef6..448983abe 100644 --- a/app/models/competition_module.rb +++ b/app/models/competition_module.rb @@ -3,22 +3,18 @@ class CompetitionModule < ApplicationRecord belongs_to :competition - has_one :competition_module_md_content, dependent: :destroy + has_many :competition_module_md_contents, dependent: :destroy def module_url - case module_type - when "home" - "/competitions/#{competition.identifier}.json" - when "inform" - "/competitions/#{competition.identifier}/informs.json?status=1" - when "manual" - "/competitions/#{competition.identifier}/informs.json?status=2" - when "chart" - "/competitions/#{competition.identifier}/charts.json" - when "enroll" - "/competitions/#{competition.identifier}/competition_teams.json" - else - url || "/competitions/#{competition.identifier}/md_content.json?md_content_id=#{competition_module_md_content&.id}" - end + result_url = url.present? ? url : case module_type + when "home" + "/competitions/#{competition.identifier}.json" + when "chart" + "/competitions/#{competition.identifier}/charts.json" + when "enroll" + "/competitions/#{competition.identifier}/competition_teams.json" + else + "/competitions/#{competition.identifier}/competition_modules/#{id}.json" + end end end diff --git a/app/views/competitions/competition_modules/show.json.jbuilder b/app/views/competitions/competition_modules/show.json.jbuilder index 69cdcc544..ba2d79804 100644 --- a/app/views/competitions/competition_modules/show.json.jbuilder +++ b/app/views/competitions/competition_modules/show.json.jbuilder @@ -1,7 +1,7 @@ -json.extract! @module, :id, :name, :position, :url, :md_edit +json.extract! @module, :id, :name, :position, :url -md = @module.competition_module_md_content -if md.present? +mds = @module.competition_module_md_contents +json.md_contents mds.each do |md| json.md_name md.name json.md_content md.content json.created_at md.created_at.strftime('%Y-%m-%d %H:%M:%S') diff --git a/db/migrate/20191024083706_migrate_competition_module_content.rb b/db/migrate/20191024083706_migrate_competition_module_content.rb new file mode 100644 index 000000000..72d023a38 --- /dev/null +++ b/db/migrate/20191024083706_migrate_competition_module_content.rb @@ -0,0 +1,17 @@ +class MigrateCompetitionModuleContent < ActiveRecord::Migration[5.2] + def change + Competition.all.each do |competition| + competition.informs.each do |inform| + if inform.status == 1 + com_module = competition.competition_modules.find_by(module_type: "inform") + elsif inform.status == 2 + com_module = competition.competition_modules.find_by(name: "参赛手册") + end + if com_module + new_md = CompetitionModuleMdContent.create!(competition_module_id: com_module.id, content: inform.description, name: inform.name) + Attachment.where(container_id: inform.id, container_type: "Inform").update_all(container_id: new_md.id, container_type: "CompetitionModuleMdContent") + end + end + end + end +end