projects_controller.rb
2.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
include OwnershipAuthentication
class ProjectsController < ApplicationController
before_action :authenticate_user!,
except: [:index, :show]
before_action :project_owner?, only: [:edit, :update, :destroy]
# GET /projects/new
def new
@project = Project.new
end
# GET /projects
# GET /projects.json
def index
@projects = Project.all.select { |project| !project.attributes.hidden }
end
# POST /projects
# POST /projects.json
def create
image_url = project_params.delete(:image_url)
@project = Project.new(project_params)
respond_to do |format|
create_and_redir(format)
@project.attributes.update(image_url: image_url) unless @project.attributes.nil?
end
end
# GET /project/1
# GET /project/1.json
def show
set_project
@project_repositories = @project.repositories if @project.is_a?(Project)
end
# GET /projects/1/edit
# GET /projects/1/edit.json
def edit
set_project
end
def update
set_project
image_url = project_params.delete(:image_url)
if @project.update(project_params) && @project.attributes.update(image_url: image_url)
redirect_to(project_path(@project.id))
else
render "edit"
end
end
# DELETE /project/1
# DELETE /project/1.json
def destroy
set_project
@project.destroy
respond_to do |format|
format.html { redirect_to projects_url }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_project
@project = Project.find(params[:id].to_i)
end
# Never trust parameters from the scary internet, only allow the white list through.
def project_params
params[:project][:name].strip!
params[:project]
end
# Extracted code from create action
def create_and_redir(format)
if @project.save
current_user.project_attributes.create(project_id: @project.id)
format.html { redirect_to project_path(@project.id), notice: t('successfully_created', :record => t(@project.class.name)) }
format.json { render action: 'show', status: :created, location: @project }
else
format.html { render action: 'new' }
format.json { render json: @project.kalibro_errors, status: :unprocessable_entity }
end
end
end