关于分析一段代码的modularity和cohesion
大家好, 我是一个在美国的留学生 今天在面试的时候面试官给了我以下这段代码让我分析下面这段代码的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