scinote-web/app/controllers/sample_groups_controller.rb

144 lines
3.6 KiB
Ruby
Raw Normal View History

2016-02-12 23:52:43 +08:00
class SampleGroupsController < ApplicationController
2016-12-02 16:33:09 +08:00
before_action :load_vars_nested
before_action :check_create_permissions
before_action :set_sample_group, except: [:create, :index]
before_action :set_project_my_module, only: :index
layout 'fluid'
2016-02-12 23:52:43 +08:00
def create
@sample_group = SampleGroup.new(sample_group_params)
@sample_group.team = @team
2016-02-12 23:52:43 +08:00
@sample_group.created_by = current_user
@sample_group.last_modified_by = current_user
respond_to do |format|
if @sample_group.save
2016-12-02 16:33:09 +08:00
format.json do
2016-02-12 23:52:43 +08:00
render json: {
2016-12-02 16:33:09 +08:00
html: render_to_string(
partial: 'sample_group.html.erb',
locals: { sample_group: @sample_group,
team: @team }
)
2016-02-12 23:52:43 +08:00
},
status: :ok
2016-12-02 16:33:09 +08:00
end
2016-02-12 23:52:43 +08:00
else
2016-12-02 16:33:09 +08:00
format.json do
2016-02-12 23:52:43 +08:00
render json: @sample_group.errors,
status: :unprocessable_entity
2016-12-02 16:33:09 +08:00
end
end
end
end
def index
render_404 unless current_team
@sample_groups = current_team.sample_groups
2016-12-02 16:33:09 +08:00
end
def update
@sample_group.update_attributes(sample_group_params)
respond_to do |format|
format.json do
if @sample_group.save
render json: {
html: render_to_string(
partial: 'sample_group.html.erb',
locals: { sample_group: @sample_group,
team: @team }
2016-12-02 16:33:09 +08:00
)
}
else
render json: @sample_group.errors,
status: :unprocessable_entity
end
end
end
end
def edit
respond_to do |format|
format.json do
render json: {
html:
render_to_string(
partial: 'edit.html.erb',
locals: { sample_group: @sample_group,
team: @team }
2016-12-02 16:33:09 +08:00
),
id: @sample_group.id
2016-02-12 23:52:43 +08:00
}
end
end
end
2016-12-02 16:33:09 +08:00
def sample_group_element
respond_to do |format|
format.json do
render json: {
html: render_to_string(
partial: 'sample_group.html.erb',
locals: { sample_group: @sample_group,
team: @team }
2016-12-02 16:33:09 +08:00
)
}
end
end
end
def destroy_confirmation
respond_to do |format|
format.json do
render json: {
html: render_to_string(
partial: 'delete_sample_group_modal.html.erb',
locals: { sample_group: @sample_group,
team: @team }
2016-12-02 16:33:09 +08:00
)
}
end
end
end
def destroy
flash[:success] = t 'sample_groups.index.destroy_flash',
name: @sample_group.name
Sample.where(sample_group: @sample_group).find_each do |sample|
sample.update(sample_group_id: nil)
end
@sample_group.destroy
redirect_to :back
end
2016-02-12 23:52:43 +08:00
private
def set_project_my_module
@project = Project.find_by_id(params[:project_id]) if params[:project_id]
2016-12-12 23:03:14 +08:00
@experiment = Experiment
.find_by_id(params[:experiment_id]) if params[:experiment_id]
@my_module = MyModule
.find_by_id(params[:my_module_id]) if params[:my_module_id]
render_403 unless @project || @my_module
end
2016-12-02 16:33:09 +08:00
def set_sample_group
@sample_group = SampleGroup.find_by_id(params[:id])
end
2016-02-12 23:52:43 +08:00
def load_vars_nested
@team = Team.find_by_id(params[:team_id])
2016-02-12 23:52:43 +08:00
render_404 unless @team
2016-02-12 23:52:43 +08:00
end
def check_create_permissions
render_403 unless can_create_sample_type_in_team(@team)
2016-02-12 23:52:43 +08:00
end
def sample_group_params
params.require(:sample_group).permit(:name, :color)
end
end