質問編集履歴

3

medel追加

2017/04/07 03:10

投稿

pecchan
pecchan

スコア555

test CHANGED
File without changes
test CHANGED
@@ -175,3 +175,125 @@
175
175
  【rake routes 画像追加】
176
176
 
177
177
  ![イメージ説明](703f6f2617c6259172ca9f1ed899017e.jpeg)
178
+
179
+
180
+
181
+
182
+
183
+ 【モデル追加】
184
+
185
+ app\models\search\base.rb
186
+
187
+ ```Ruby
188
+
189
+ class Search::Base
190
+
191
+ include ActiveModel::Model
192
+
193
+ include ActiveModel::Validations::Callbacks
194
+
195
+
196
+
197
+ def contains(arel_attribute, value)
198
+
199
+ arel_attribute.matches("%#{escape_like(value)}%")
200
+
201
+ end
202
+
203
+
204
+
205
+ def escape_like(string)
206
+
207
+ string.gsub(/[\\%_]/) { |m| "\\#{m}" }
208
+
209
+ end
210
+
211
+
212
+
213
+ def value_to_boolean(value)
214
+
215
+ ActiveRecord::ConnectionAdapters::Column.value_to_boolean(value)
216
+
217
+ end
218
+
219
+ end
220
+
221
+ ```
222
+
223
+
224
+
225
+ app\models\search\Product.rb
226
+
227
+ ```Ruby
228
+
229
+ class Search::Product < Search::Base
230
+
231
+ ATTRIBUTES = %i(
232
+
233
+ code
234
+
235
+ name name_kana
236
+
237
+ price_from price_to
238
+
239
+ purchase_cost_from purchase_cost_to
240
+
241
+ availability
242
+
243
+ )
244
+
245
+ attr_accessor(*ATTRIBUTES)
246
+
247
+
248
+
249
+ def matches
250
+
251
+ t = ::Product.arel_table
252
+
253
+ results = ::Product.all
254
+
255
+ results = results.where(contains(t[:code], code)) if code.present?
256
+
257
+ results = results.where(contains(t[:name], name)) if name.present?
258
+
259
+ results = results.where(contains(t[:name_kana], name_kana)) if name_kana.present?
260
+
261
+ results = results.where(t[:price].gteq(price_from)) if price_from.present?
262
+
263
+ results = results.where(t[:price].lteq(price_to)) if price_to.present?
264
+
265
+ if purchase_cost_from.present?
266
+
267
+ results = results.where(t[:purchase_cost].gteq(purchase_cost_from))
268
+
269
+ end
270
+
271
+ if purchase_cost_to.present?
272
+
273
+ results = results.where(t[:purchase_cost].lteq(purchase_cost_to))
274
+
275
+ end
276
+
277
+ results = results.where(availability: true) if value_to_boolean(availability)
278
+
279
+ results
280
+
281
+ end
282
+
283
+ end
284
+
285
+ ```
286
+
287
+
288
+
289
+ app\models\product.rb
290
+
291
+ ```Ruby
292
+
293
+ class Product < ApplicationRecord
294
+
295
+ end
296
+
297
+
298
+
299
+ ```

2

画像

2017/04/07 03:10

投稿

pecchan
pecchan

スコア555

test CHANGED
File without changes
test CHANGED
@@ -167,3 +167,11 @@
167
167
  【エラー画像追加】
168
168
 
169
169
  ![イメージ説明](29fd1628ce982617283e25cac1ea1b78.jpeg)
170
+
171
+
172
+
173
+
174
+
175
+ 【rake routes 画像追加】
176
+
177
+ ![イメージ説明](703f6f2617c6259172ca9f1ed899017e.jpeg)

1

画像追加

2017/04/07 01:53

投稿

pecchan
pecchan

スコア555

test CHANGED
File without changes
test CHANGED
@@ -161,3 +161,9 @@
161
161
  <% end %>
162
162
 
163
163
  ```
164
+
165
+
166
+
167
+ 【エラー画像追加】
168
+
169
+ ![イメージ説明](29fd1628ce982617283e25cac1ea1b78.jpeg)