projects_controller.rb
2.37 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
88
89
90
91
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
@project_image = ProjectImage.new
end
# GET /projects
# GET /projects.json
def index
@projects = Project.all
end
# POST /projects
# POST /projects.json
def create
@project = Project.new(project_params)
respond_to do |format|
create_and_redir(format)
ProjectImage.create(image_url: project_params[:image_url],project_id: @project.id )
end
end
# GET /project/1
# GET /project/1.json
def show
set_project
@project_repositories = @project.repositories
@project_image = ProjectImage.find_by_project_id(@project.id)
end
# GET /projects/1/edit
# GET /projects/1/edit.json
def edit
set_project
@project_image = ProjectImage.find_by_project_id(@project.id)
@project_image.check_no_image
end
def update
set_project
@project_image = ProjectImage.find_by_project_id(@project.id)
if @project.update(project_params)
@project_image.update(image_url: project_params[:image_url])
redirect_to(project_path(@project.id))
else
render "edit"
end
end
# DELETE /project/1
# DELETE /project/1.json
def destroy
set_project
current_user.project_ownerships.find_by_project_id(@project.id).destroy
@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])
end
# Never trust parameters from the scary internet, only allow the white list through.
def project_params
params[:project]
end
# Extracted code from create action
def create_and_redir(format)
if @project.save
current_user.project_ownerships.create project_id: @project.id
format.html { redirect_to project_path(@project.id), notice: 'Project was successfully created.' }
format.json { render action: 'show', status: :created, location: @project }
else
format.html { render action: 'new' }
format.json { render json: @project.errors, status: :unprocessable_entity }
end
end
end