-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstartups_controller.rb
More file actions
113 lines (98 loc) · 3.29 KB
/
startups_controller.rb
File metadata and controls
113 lines (98 loc) · 3.29 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
class StartupsController < ApplicationController
before_action :set_startup, only: [ :show, :like, :edit, :update, :destroy ]
skip_before_action :authenticate, only: [ :index, :show, :search ]
def index
@q = Startup.ransack(params[:q])
@startups = @q.result(distinct: true).order(created_at: :desc)
end
def search
@q = Startup.ransack(params[:q])
@startups = @q.result(distinct: true)
end
def new
@startup = Startup.new
end
def edit
unless @startup.user == Current.user
redirect_to root_path, alert: "You can only edit your own startups."
end
end
def update
unless @startup.user == Current.user
redirect_to root_path, alert: "You can only update your own startups."
return
end
if @startup.update(startup_params.except(:role_names))
submitted_titles = (startup_params[:role_names] || "").split(",").map(&:strip).uniq
@startup.roles.where.not(title: submitted_titles).destroy_all
existing_titles = @startup.roles.pluck(:title)
(submitted_titles - existing_titles).each do |title|
@startup.roles.create(title: title, status: :open)
end
redirect_to @startup, notice: "Startup was successfully updated."
else
flash.now[:alert] = "There was an error updating the startup."
render :edit, status: :unprocessable_entity
end
end
def destroy
unless @startup.user == Current.user
redirect_to root_path, alert: "You can only delete your own startups."
return
end
@startup.destroy
redirect_to startups_path, notice: "Startup was successfully deleted."
end
def create
@startup = Startup.new(startup_params.except(:role_names))
@startup.user = Current.user
if startup_params[:role_names].present?
role_titles = startup_params[:role_names].split(",")
role_titles.each do |title|
@startup.roles.build(title: title.strip, status: :open) # or your default status
end
end
if @startup.save
redirect_to root_path, notice: "Startup was successfully created."
else
flash.now[:alert] = "There was an error creating the startup."
render :new, status: :unprocessable_entity
end
end
def show
end
def like
like = @startup.likes.find_by(user_id: Current.user.id)
if like
like.destroy
@startup.reload
respond_to do |format|
format.turbo_stream
format.json { render json: { liked: false, count: @startup.likes.count } }
end
else
@startup.likes.create(user_id: Current.user.id)
@startup.reload
respond_to do |format|
format.turbo_stream
format.json { render json: { liked: true, count: @startup.likes.count } }
end
end
end
private
def startup_params
params.require(:startup).permit(:name,
:description,
:tag_list,
:role_names,
:website_url,
:instagram_url,
:twitter_url,
:linkedin_url,
:github_url,
:discord_url)
end
def set_startup
@startup = Startup.find(params[:id])
end
end