/
tools_controller.rb
193 lines (167 loc) · 3.56 KB
/
tools_controller.rb
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
class ToolsController < ApplicationController
before_action :store_location!, only: [:new]
before_action :authenticate_user!, except: [:index, :show, :directory]
before_action :assign_tool, only: [:show, :update, :edit, :destroy, :adoption]
before_action :override_search
#
# GET /tools
#
# Lists all +Tool+ instances.
#
def index
@current_params = tool_index_params
@tools = if @current_params[:order] == "created_at"
Tool.order(:created_at)
else
Tool.order(:name)
end
if @current_params[:q].present?
@tools = @tools.search(@current_params[:q])
end
if Tool::ALLOWED_TYPES.include?(@current_params[:type])
@tools = @tools.where(type: @current_params[:type])
end
@tools = @tools.page(@current_params[:page]).per(20)
respond_to do |format|
format.html
format.atom
end
end
#
# GET /tools/new
#
# Display the form for creating a new +Tool+.
#
def new
@tool = current_user.tools.new
@user = current_user
end
#
# GET /tools/:id
#
# Display the detail page for a +Tool+.
#
def show
@other_tools = @tool.others_from_this_owner
@collaborators = @tool.collaborators
end
#
# POST /tools
#
# Create a new +Tool+
#
def create
@tool = current_user.tools.build(tool_params)
@user = current_user
if @tool.save
redirect_to(
tools_user_path(@tool.owner),
notice: t("tool.created", name: @tool.name)
)
else
render :new
end
end
#
# GET /tools/:id/edit
#
# Display the form for editing an existing +Tool+.
#
def edit
@user = current_user
authorize! @tool
end
#
# PATCH /tools/:id
#
# Updates an existing +Tool+.
#
def update
@user = current_user
authorize! @tool
if @tool.update(tool_params)
key = if tool_params.key?(:up_for_adoption)
if tool_params[:up_for_adoption] == "true"
"adoption.up"
else
"adoption.down"
end
else
"tool.updated"
end
redirect_to(
tool_path(@tool),
notice: t(key, name: @tool.name)
)
else
render :edit
end
end
#
# DELETE /tools/:id
#
# Deletes a +Tool+.
#
def destroy
authorize! @tool
@tool.destroy
redirect_to(
tools_user_path(@tool.owner),
notice: t("tool.deleted", name: @tool.name)
)
end
#
# GET /tools-directory
#
# Displays general information about tools and the most recently added ones.
#
def directory
@recently_added_tools = Tool.ordered_by("recently_added").limit(5)
end
#
# POST /tools/:id/adoption
#
# Sends an email to the tool owner letting them know that someone is
# interested in adopting their tool.
#
def adoption
AdoptionMailer.delay.interest_email(@tool, current_user)
redirect_to(
@tool,
notice: t(
"adoption.email_sent",
cookbook_or_tool: @tool.name
)
)
end
private
def tool_index_params
params.permit(:q, :order, :type, :page)
end
#
# Strong params for a +Tool+
#
def tool_params
params.require(:tool).permit(
:name,
:slug,
:type,
:description,
:source_url,
:instructions,
:up_for_adoption
)
end
#
# Assigns a +Tool+ based on the slug.
#
def assign_tool
@tool = Tool.find_by!(slug: params[:id])
end
#
# Override the default search settings.
#
def override_search
@search = { path: tools_path, name: "Tools" }
end
end