質問編集履歴
2
あ
test
CHANGED
File without changes
|
test
CHANGED
@@ -242,7 +242,7 @@
|
|
242
242
|
|
243
243
|
|
244
244
|
|
245
|
-
|
245
|
+
【モデル】
|
246
246
|
|
247
247
|
app\models\serch\bese.rb
|
248
248
|
|
1
ko-do追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -31,3 +31,333 @@
|
|
31
31
|
|
32
32
|
|
33
33
|
宜しくお願い致します。
|
34
|
+
|
35
|
+
|
36
|
+
|
37
|
+
【2017/04/05追記】
|
38
|
+
|
39
|
+
参考サイトのサンプルコードまんまですが記載致します。
|
40
|
+
|
41
|
+
|
42
|
+
|
43
|
+
【コントローラ】
|
44
|
+
|
45
|
+
app\controllers\products_controller.rb
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
```ruby
|
50
|
+
|
51
|
+
class ProductsController < ApplicationController
|
52
|
+
|
53
|
+
before_action :set_product, only: [:show, :edit, :update, :destroy]
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
# GET /products
|
58
|
+
|
59
|
+
# GET /products.json
|
60
|
+
|
61
|
+
def index
|
62
|
+
|
63
|
+
@product = Search::Product.new
|
64
|
+
|
65
|
+
end
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
def search
|
70
|
+
|
71
|
+
@product = Search::Product.new(search_params)
|
72
|
+
|
73
|
+
@products = @product
|
74
|
+
|
75
|
+
.matches
|
76
|
+
|
77
|
+
.order(availability: :desc, code: :asc)
|
78
|
+
|
79
|
+
.decorate
|
80
|
+
|
81
|
+
end
|
82
|
+
|
83
|
+
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
|
88
|
+
|
89
|
+
# GET /products/1
|
90
|
+
|
91
|
+
# GET /products/1.json
|
92
|
+
|
93
|
+
def show
|
94
|
+
|
95
|
+
end
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
# GET /products/new
|
100
|
+
|
101
|
+
def new
|
102
|
+
|
103
|
+
@product = Product.new
|
104
|
+
|
105
|
+
end
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
# GET /products/1/edit
|
110
|
+
|
111
|
+
def edit
|
112
|
+
|
113
|
+
end
|
114
|
+
|
115
|
+
|
116
|
+
|
117
|
+
# POST /products
|
118
|
+
|
119
|
+
# POST /products.json
|
120
|
+
|
121
|
+
def create
|
122
|
+
|
123
|
+
@product = Product.new(product_params)
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
respond_to do |format|
|
128
|
+
|
129
|
+
if @product.save
|
130
|
+
|
131
|
+
format.html { redirect_to @product, notice: 'Product was successfully created.' }
|
132
|
+
|
133
|
+
format.json { render :show, status: :created, location: @product }
|
134
|
+
|
135
|
+
else
|
136
|
+
|
137
|
+
format.html { render :new }
|
138
|
+
|
139
|
+
format.json { render json: @product.errors, status: :unprocessable_entity }
|
140
|
+
|
141
|
+
end
|
142
|
+
|
143
|
+
end
|
144
|
+
|
145
|
+
end
|
146
|
+
|
147
|
+
|
148
|
+
|
149
|
+
# PATCH/PUT /products/1
|
150
|
+
|
151
|
+
# PATCH/PUT /products/1.json
|
152
|
+
|
153
|
+
def update
|
154
|
+
|
155
|
+
respond_to do |format|
|
156
|
+
|
157
|
+
if @product.update(product_params)
|
158
|
+
|
159
|
+
format.html { redirect_to @product, notice: 'Product was successfully updated.' }
|
160
|
+
|
161
|
+
format.json { render :show, status: :ok, location: @product }
|
162
|
+
|
163
|
+
else
|
164
|
+
|
165
|
+
format.html { render :edit }
|
166
|
+
|
167
|
+
format.json { render json: @product.errors, status: :unprocessable_entity }
|
168
|
+
|
169
|
+
end
|
170
|
+
|
171
|
+
end
|
172
|
+
|
173
|
+
end
|
174
|
+
|
175
|
+
|
176
|
+
|
177
|
+
# DELETE /products/1
|
178
|
+
|
179
|
+
# DELETE /products/1.json
|
180
|
+
|
181
|
+
def destroy
|
182
|
+
|
183
|
+
@product.destroy
|
184
|
+
|
185
|
+
respond_to do |format|
|
186
|
+
|
187
|
+
format.html { redirect_to products_url, notice: 'Product was successfully destroyed.' }
|
188
|
+
|
189
|
+
format.json { head :no_content }
|
190
|
+
|
191
|
+
end
|
192
|
+
|
193
|
+
end
|
194
|
+
|
195
|
+
|
196
|
+
|
197
|
+
private
|
198
|
+
|
199
|
+
# Use callbacks to share common setup or constraints between actions.
|
200
|
+
|
201
|
+
def set_product
|
202
|
+
|
203
|
+
@product = Product.find(params[:id])
|
204
|
+
|
205
|
+
end
|
206
|
+
|
207
|
+
|
208
|
+
|
209
|
+
# Never trust parameters from the scary internet, only allow the white list through.
|
210
|
+
|
211
|
+
def product_params
|
212
|
+
|
213
|
+
params.require(:product).permit(:code, :name)
|
214
|
+
|
215
|
+
end
|
216
|
+
|
217
|
+
|
218
|
+
|
219
|
+
|
220
|
+
|
221
|
+
# 検索フォームから受け取ったパラメータ
|
222
|
+
|
223
|
+
def search_params
|
224
|
+
|
225
|
+
params
|
226
|
+
|
227
|
+
.require(:search_product)
|
228
|
+
|
229
|
+
.permit(Search::Product::ATTRIBUTES)
|
230
|
+
|
231
|
+
end
|
232
|
+
|
233
|
+
|
234
|
+
|
235
|
+
|
236
|
+
|
237
|
+
end
|
238
|
+
|
239
|
+
|
240
|
+
|
241
|
+
```
|
242
|
+
|
243
|
+
|
244
|
+
|
245
|
+
|
246
|
+
|
247
|
+
app\models\serch\bese.rb
|
248
|
+
|
249
|
+
```ruby
|
250
|
+
|
251
|
+
class Search::Base
|
252
|
+
|
253
|
+
include ActiveModel::Model
|
254
|
+
|
255
|
+
include ActiveModel::Validations::Callbacks
|
256
|
+
|
257
|
+
|
258
|
+
|
259
|
+
def contains(arel_attribute, value)
|
260
|
+
|
261
|
+
arel_attribute.matches("%#{escape_like(value)}%")
|
262
|
+
|
263
|
+
end
|
264
|
+
|
265
|
+
|
266
|
+
|
267
|
+
def escape_like(string)
|
268
|
+
|
269
|
+
string.gsub(/[\\%_]/) { |m| "\\#{m}" }
|
270
|
+
|
271
|
+
end
|
272
|
+
|
273
|
+
|
274
|
+
|
275
|
+
def value_to_boolean(value)
|
276
|
+
|
277
|
+
ActiveRecord::ConnectionAdapters::Column.value_to_boolean(value)
|
278
|
+
|
279
|
+
end
|
280
|
+
|
281
|
+
end
|
282
|
+
|
283
|
+
```
|
284
|
+
|
285
|
+
|
286
|
+
|
287
|
+
app\models\serch\product.rb
|
288
|
+
|
289
|
+
|
290
|
+
|
291
|
+
```ruby
|
292
|
+
|
293
|
+
class Search::Product < Search::Base
|
294
|
+
|
295
|
+
ATTRIBUTES = %i(
|
296
|
+
|
297
|
+
code
|
298
|
+
|
299
|
+
name name_kana
|
300
|
+
|
301
|
+
price_from price_to
|
302
|
+
|
303
|
+
purchase_cost_from purchase_cost_to
|
304
|
+
|
305
|
+
availability
|
306
|
+
|
307
|
+
)
|
308
|
+
|
309
|
+
attr_accessor(*ATTRIBUTES)
|
310
|
+
|
311
|
+
|
312
|
+
|
313
|
+
def matches
|
314
|
+
|
315
|
+
t = ::Product.arel_table
|
316
|
+
|
317
|
+
results = ::Product.all
|
318
|
+
|
319
|
+
results = results.where(contains(t[:code], code)) if code.present?
|
320
|
+
|
321
|
+
results = results.where(contains(t[:name], name)) if name.present?
|
322
|
+
|
323
|
+
results = results.where(contains(t[:name_kana], name_kana)) if name_kana.present?
|
324
|
+
|
325
|
+
results = results.where(t[:price].gteq(price_from)) if price_from.present?
|
326
|
+
|
327
|
+
results = results.where(t[:price].lteq(price_to)) if price_to.present?
|
328
|
+
|
329
|
+
if purchase_cost_from.present?
|
330
|
+
|
331
|
+
results = results.where(t[:purchase_cost].gteq(purchase_cost_from))
|
332
|
+
|
333
|
+
end
|
334
|
+
|
335
|
+
if purchase_cost_to.present?
|
336
|
+
|
337
|
+
results = results.where(t[:purchase_cost].lteq(purchase_cost_to))
|
338
|
+
|
339
|
+
end
|
340
|
+
|
341
|
+
results = results.where(availability: true) if value_to_boolean(availability)
|
342
|
+
|
343
|
+
results
|
344
|
+
|
345
|
+
end
|
346
|
+
|
347
|
+
end
|
348
|
+
|
349
|
+
```
|
350
|
+
|
351
|
+
|
352
|
+
|
353
|
+
app\models\product.rb
|
354
|
+
|
355
|
+
```ruby
|
356
|
+
|
357
|
+
class Product < ApplicationRecord
|
358
|
+
|
359
|
+
end
|
360
|
+
|
361
|
+
|
362
|
+
|
363
|
+
```
|