From 10693c4bae76eef1ee1efda24f80eebd7446bc25 Mon Sep 17 00:00:00 2001 From: cxt <853663049@qq.com> Date: Sun, 9 Feb 2020 13:49:56 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AF=BE=E5=A0=82=E7=9B=B4=E6=92=AD=E6=A8=A1?= =?UTF-8?q?=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/live_links_controller.rb | 33 +++++++++++++++++++ app/helpers/courses_helper.rb | 2 ++ app/models/course.rb | 3 ++ app/models/live_link.rb | 11 +++++++ app/views/live_links/index.json.jbuilder | 7 ++++ config/locales/zh-CN.yml | 4 +++ config/routes.rb | 2 ++ .../20200209052429_create_live_links.rb | 13 ++++++++ spec/models/live_link_spec.rb | 5 +++ vendor/assets/codemirror/lib/codemirror.js | 2 +- 10 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 app/controllers/live_links_controller.rb create mode 100644 app/models/live_link.rb create mode 100644 app/views/live_links/index.json.jbuilder create mode 100644 db/migrate/20200209052429_create_live_links.rb create mode 100644 spec/models/live_link_spec.rb diff --git a/app/controllers/live_links_controller.rb b/app/controllers/live_links_controller.rb new file mode 100644 index 000000000..bc89c11d3 --- /dev/null +++ b/app/controllers/live_links_controller.rb @@ -0,0 +1,33 @@ +class LiveLinksController < ApplicationController + before_action :require_login + before_action :find_course, only: [:index, :create] + before_action :user_course_identity + before_action :teacher_allowed, only: [:create] + + def index + lives = @course.live_links.order("id desc") + @total_count = lives.size + @lives = paginate lives.includes(user: :user_extension) + end + + def create + @course.live_links.create!( create_params.merge(user_id: current_user.id)) + render_ok + end + + def update + render_forbidden("无权限操作") unless current_user.id == current_live.user_id || current_user.admin? + current_live.update!(on_status: params[:on_status]) + render_ok + end + + private + + def create_params + params.permit(:url, :description) + end + + def current_live + @_current_live = LiveLink.find params[:id] + end +end \ No newline at end of file diff --git a/app/helpers/courses_helper.rb b/app/helpers/courses_helper.rb index 769314815..37e108a74 100644 --- a/app/helpers/courses_helper.rb +++ b/app/helpers/courses_helper.rb @@ -128,6 +128,8 @@ module CoursesHelper course.informs.count when "online_learning" course.shixuns.count + when "video" + course.course_videos.count + course.live_links.count end end diff --git a/app/models/course.rb b/app/models/course.rb index 0326842ef..37af178fe 100644 --- a/app/models/course.rb +++ b/app/models/course.rb @@ -85,6 +85,9 @@ class Course < ApplicationRecord has_many :course_videos, dependent: :destroy has_many :videos, through: :course_videos + # 直播 + has_many :live_links, dependent: :destroy + validate :validate_sensitive_string scope :hidden, ->(is_hidden = true) { where(is_hidden: is_hidden) } diff --git a/app/models/live_link.rb b/app/models/live_link.rb new file mode 100644 index 000000000..e559baa6b --- /dev/null +++ b/app/models/live_link.rb @@ -0,0 +1,11 @@ +class LiveLink < ApplicationRecord + belongs_to :course + belongs_to :user + + validates :url, presence: true + validates :description, length: { maximum: 100, too_long: "不能超过100个字符" } + + def op_auth? + user == User.current || User.current.admin? + end +end diff --git a/app/views/live_links/index.json.jbuilder b/app/views/live_links/index.json.jbuilder new file mode 100644 index 000000000..acd923807 --- /dev/null +++ b/app/views/live_links/index.json.jbuilder @@ -0,0 +1,7 @@ +json.lives @lives do |live| + json.(live, :id, :url, :description, :on_status) + json.author_name live.user.show_real_name + json.author_login live.user.login + json.author_img url_to_avatar(live.user) + json.op_auth live.op_auth? +end \ No newline at end of file diff --git a/config/locales/zh-CN.yml b/config/locales/zh-CN.yml index 9f9aa0167..9d4d326cd 100644 --- a/config/locales/zh-CN.yml +++ b/config/locales/zh-CN.yml @@ -170,6 +170,10 @@ zh-CN: name: '名称' tag_discipline: name: '名称' + live: + description: '说明' + url: '链接' + diff --git a/config/routes.rb b/config/routes.rb index 2ed53511e..7dcd3baff 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -550,6 +550,8 @@ Rails.application.routes.draw do end end + resources :live_links, only: [:index, :update, :create], shallow: true + resources :homework_commons, shallow: true do member do diff --git a/db/migrate/20200209052429_create_live_links.rb b/db/migrate/20200209052429_create_live_links.rb new file mode 100644 index 000000000..d3b7ad953 --- /dev/null +++ b/db/migrate/20200209052429_create_live_links.rb @@ -0,0 +1,13 @@ +class CreateLiveLinks < ActiveRecord::Migration[5.2] + def change + create_table :live_links do |t| + t.references :course, index: true + t.references :user, index: true + t.string :url + t.text :description + t.boolean :on_status, default: 0 + + t.timestamps + end + end +end diff --git a/spec/models/live_link_spec.rb b/spec/models/live_link_spec.rb new file mode 100644 index 000000000..29682304a --- /dev/null +++ b/spec/models/live_link_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe LiveLink, type: :model do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/vendor/assets/codemirror/lib/codemirror.js b/vendor/assets/codemirror/lib/codemirror.js index 076a0b89e..fffab9e0f 100644 --- a/vendor/assets/codemirror/lib/codemirror.js +++ b/vendor/assets/codemirror/lib/codemirror.js @@ -4349,7 +4349,7 @@ // Actual scrollable element. d.scroller = elt("div", [d.sizer, d.heightForcer, d.gutters], "CodeMirror-scroll"); d.scroller.setAttribute("tabIndex", "-1"); - // The element in which the editor lives. + // The element in which the editor live_links. d.wrapper = elt("div", [d.scrollbarFiller, d.gutterFiller, d.scroller], "CodeMirror"); // Work around IE7 z-index bug (not perfect, hence IE7 not really being supported)