parent
107359163a
commit
10693c4bae
@ -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
|
@ -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
|
@ -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
|
@ -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
|
@ -0,0 +1,5 @@
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe LiveLink, type: :model do
|
||||
pending "add some examples to (or delete) #{__FILE__}"
|
||||
end
|
Loading…
Reference in new issue