关于分析一段代码的modularity和cohesion

pj20000101 2017-02-25 01:09:15
大家好, 我是一个在美国的留学生 今天在面试的时候面试官给了我以下这段代码让我分析下面这段代码的modularity跟有没有缺少cohesion, 我由于只有用rails写过两个网站所以不是特别了解rails回答的时候根本胡扯, 所以我想问下有没有大神来讲解下这段代码. 这是这段代码的背景:"This segment of syntax implements aspects of user interface in an existing software. It binds the internal representation(s) of information that enable(s) the processing of Scenarios and the way the user interacts with those Scenarios."

class ScenariosController < ApplicationController

include ScenarioCopier

before_action :set_scenario, only: [:show, :copy, :edit, :tree, :update, :destroy]

respond_to :java



# GET /scenarios

def index
authorize! :index, Scenario
render json: Scenario.where(:locked => false, :university => current_user.university)
end



# GET /scenarios/1
def show
authorize! :read, @scenario
render json: @
end



# GET /scenarios/1/edit
def edit
end

def tree
authorize! :read, @scenario
render json: @scenario, choices: @scenario.complete_choices, serializer: ScenarioTreeSerializer
end



# POST /
def create
authorize! :create, Scenario
scenario = Scenario.new(scenario_params)
scenario.university = current_user.university
scenario.archived = false
scenario.locked = false
Scenario.transaction do
begin

save_scenario_with_root_node(scenario)

rescue ActiveRecord::RecordInvalid => invalid

render json: invalid.record.errors, status: :unprocessable_entity and return

end



render json: scenario, status: :created

end

end



# POST /scenarios/1/copy

def copy
authorize! :copy, @scenario
unless scenario_params[:name]
render json: {error: 'Must provide a new scenario name.'}, status: :unprocessable_entity and return
end
scenario_copy = copy_scenario(@scenario, {:copy_name => scenario_params[:name], :description => scenario_params[:description]}); return if performed?
render json: scenario_copy, status: :created

end

# PATCH/PUT /scenarios/1

def update
authorize! :update, @scenario
if @scenario.locked
render json: {error: 'Cannot update a locked scenario.'}, status: :bad_request and return
end

if @scenario.update(scenario_params)
head :no_content
else
render json: @scenario.errors, status: :unprocessable_entity
end
end



private

def set_scenario
@scenario = Scenario.find(params[:id])
end

def scenario_params
params.require(:scenario).permit(:name, :description, :archived, :root_node_id)
end

def save_scenario_with_root_node(scenario)
scenario.save!
root_node = initialize_root_node(scenario)
scenario.update!(root_node_id: root_node.id)
end

def initialize_root_node(scenario)

node = Node.create!(:scenario_id => scenario.id)

node.update!(is_start: false, label: 1)

node

end
end
...全文
4805 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

2,763

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 Ruby/Rails
社区管理员
  • Ruby/Rails社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧