質問編集履歴
2
あ
title
CHANGED
File without changes
|
body
CHANGED
@@ -120,7 +120,7 @@
|
|
120
120
|
|
121
121
|
```
|
122
122
|
|
123
|
-
|
123
|
+
【モデル】
|
124
124
|
app\models\serch\bese.rb
|
125
125
|
```ruby
|
126
126
|
class Search::Base
|
1
ko-do追加
title
CHANGED
File without changes
|
body
CHANGED
@@ -14,4 +14,169 @@
|
|
14
14
|
親クラスの名前解決が出来ていないようですが、
|
15
15
|
何か必要な設定などがあるのでしょうか?
|
16
16
|
|
17
|
-
宜しくお願い致します。
|
17
|
+
宜しくお願い致します。
|
18
|
+
|
19
|
+
【2017/04/05追記】
|
20
|
+
参考サイトのサンプルコードまんまですが記載致します。
|
21
|
+
|
22
|
+
【コントローラ】
|
23
|
+
app\controllers\products_controller.rb
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
class ProductsController < ApplicationController
|
27
|
+
before_action :set_product, only: [:show, :edit, :update, :destroy]
|
28
|
+
|
29
|
+
# GET /products
|
30
|
+
# GET /products.json
|
31
|
+
def index
|
32
|
+
@product = Search::Product.new
|
33
|
+
end
|
34
|
+
|
35
|
+
def search
|
36
|
+
@product = Search::Product.new(search_params)
|
37
|
+
@products = @product
|
38
|
+
.matches
|
39
|
+
.order(availability: :desc, code: :asc)
|
40
|
+
.decorate
|
41
|
+
end
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
# GET /products/1
|
46
|
+
# GET /products/1.json
|
47
|
+
def show
|
48
|
+
end
|
49
|
+
|
50
|
+
# GET /products/new
|
51
|
+
def new
|
52
|
+
@product = Product.new
|
53
|
+
end
|
54
|
+
|
55
|
+
# GET /products/1/edit
|
56
|
+
def edit
|
57
|
+
end
|
58
|
+
|
59
|
+
# POST /products
|
60
|
+
# POST /products.json
|
61
|
+
def create
|
62
|
+
@product = Product.new(product_params)
|
63
|
+
|
64
|
+
respond_to do |format|
|
65
|
+
if @product.save
|
66
|
+
format.html { redirect_to @product, notice: 'Product was successfully created.' }
|
67
|
+
format.json { render :show, status: :created, location: @product }
|
68
|
+
else
|
69
|
+
format.html { render :new }
|
70
|
+
format.json { render json: @product.errors, status: :unprocessable_entity }
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
# PATCH/PUT /products/1
|
76
|
+
# PATCH/PUT /products/1.json
|
77
|
+
def update
|
78
|
+
respond_to do |format|
|
79
|
+
if @product.update(product_params)
|
80
|
+
format.html { redirect_to @product, notice: 'Product was successfully updated.' }
|
81
|
+
format.json { render :show, status: :ok, location: @product }
|
82
|
+
else
|
83
|
+
format.html { render :edit }
|
84
|
+
format.json { render json: @product.errors, status: :unprocessable_entity }
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
# DELETE /products/1
|
90
|
+
# DELETE /products/1.json
|
91
|
+
def destroy
|
92
|
+
@product.destroy
|
93
|
+
respond_to do |format|
|
94
|
+
format.html { redirect_to products_url, notice: 'Product was successfully destroyed.' }
|
95
|
+
format.json { head :no_content }
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
private
|
100
|
+
# Use callbacks to share common setup or constraints between actions.
|
101
|
+
def set_product
|
102
|
+
@product = Product.find(params[:id])
|
103
|
+
end
|
104
|
+
|
105
|
+
# Never trust parameters from the scary internet, only allow the white list through.
|
106
|
+
def product_params
|
107
|
+
params.require(:product).permit(:code, :name)
|
108
|
+
end
|
109
|
+
|
110
|
+
|
111
|
+
# 検索フォームから受け取ったパラメータ
|
112
|
+
def search_params
|
113
|
+
params
|
114
|
+
.require(:search_product)
|
115
|
+
.permit(Search::Product::ATTRIBUTES)
|
116
|
+
end
|
117
|
+
|
118
|
+
|
119
|
+
end
|
120
|
+
|
121
|
+
```
|
122
|
+
|
123
|
+
|
124
|
+
app\models\serch\bese.rb
|
125
|
+
```ruby
|
126
|
+
class Search::Base
|
127
|
+
include ActiveModel::Model
|
128
|
+
include ActiveModel::Validations::Callbacks
|
129
|
+
|
130
|
+
def contains(arel_attribute, value)
|
131
|
+
arel_attribute.matches("%#{escape_like(value)}%")
|
132
|
+
end
|
133
|
+
|
134
|
+
def escape_like(string)
|
135
|
+
string.gsub(/[\\%_]/) { |m| "\\#{m}" }
|
136
|
+
end
|
137
|
+
|
138
|
+
def value_to_boolean(value)
|
139
|
+
ActiveRecord::ConnectionAdapters::Column.value_to_boolean(value)
|
140
|
+
end
|
141
|
+
end
|
142
|
+
```
|
143
|
+
|
144
|
+
app\models\serch\product.rb
|
145
|
+
|
146
|
+
```ruby
|
147
|
+
class Search::Product < Search::Base
|
148
|
+
ATTRIBUTES = %i(
|
149
|
+
code
|
150
|
+
name name_kana
|
151
|
+
price_from price_to
|
152
|
+
purchase_cost_from purchase_cost_to
|
153
|
+
availability
|
154
|
+
)
|
155
|
+
attr_accessor(*ATTRIBUTES)
|
156
|
+
|
157
|
+
def matches
|
158
|
+
t = ::Product.arel_table
|
159
|
+
results = ::Product.all
|
160
|
+
results = results.where(contains(t[:code], code)) if code.present?
|
161
|
+
results = results.where(contains(t[:name], name)) if name.present?
|
162
|
+
results = results.where(contains(t[:name_kana], name_kana)) if name_kana.present?
|
163
|
+
results = results.where(t[:price].gteq(price_from)) if price_from.present?
|
164
|
+
results = results.where(t[:price].lteq(price_to)) if price_to.present?
|
165
|
+
if purchase_cost_from.present?
|
166
|
+
results = results.where(t[:purchase_cost].gteq(purchase_cost_from))
|
167
|
+
end
|
168
|
+
if purchase_cost_to.present?
|
169
|
+
results = results.where(t[:purchase_cost].lteq(purchase_cost_to))
|
170
|
+
end
|
171
|
+
results = results.where(availability: true) if value_to_boolean(availability)
|
172
|
+
results
|
173
|
+
end
|
174
|
+
end
|
175
|
+
```
|
176
|
+
|
177
|
+
app\models\product.rb
|
178
|
+
```ruby
|
179
|
+
class Product < ApplicationRecord
|
180
|
+
end
|
181
|
+
|
182
|
+
```
|