36
36
append_to_file ".gitignore" , "vendor/elasticsearch-0.90.7/\n "
37
37
38
38
git :init
39
- git : add => '.'
40
- git : commit => "-m 'Initial commit: Clean application'"
39
+ git add : "."
40
+ git commit : "-m 'Initial commit: Clean application'"
41
41
42
42
# ----- Download Elasticsearch --------------------------------------------------------------------
43
43
75
75
# ----- Add README --------------------------------------------------------------------------------
76
76
77
77
puts
78
- say_status "README" , "Adding readme ...\n " , :yellow
78
+ say_status "README" , "Adding Readme ...\n " , :yellow
79
79
puts '-' *80 , '' ; sleep 0.25
80
80
81
+ remove_file 'README.rdoc'
82
+
81
83
create_file 'README.rdoc' , <<-README
82
84
= Ruby on Rails and Elasticsearch: Example application
83
85
87
89
It has been generated by application templates available at
88
90
https://github.com/elasticsearch/elasticsearch-rails/tree/master/elasticsearch-rails/lib/rails/templates.
89
91
90
- == Basic
92
+ == [1] Basic
91
93
92
94
The `basic` version provides a simple integration for a simple Rails model, `Article`, showing how
93
95
to include the search engine support in your model, import the data, and provide form to perform
96
98
README
97
99
98
100
99
- git : add => '.'
100
- git : commit => "-m 'Added README for the application'"
101
+ git add : "."
102
+ git commit : "-m 'Added README for the application'"
101
103
102
104
# ----- Use Thin ----------------------------------------------------------------------------------
103
105
111
113
rescue LoadError
112
114
end
113
115
116
+ # ----- Auxiliary gems -----------------------------------------------------------------------------
117
+
118
+ gem 'turn' , group : 'test'
119
+ gem 'mocha' , group : 'test' , require : 'mocha/setup'
120
+
114
121
# ----- Add gems into Gemfile ---------------------------------------------------------------------
115
122
116
123
puts
117
- say_status "Rubygems" , "Adding Rubygems into Gemfile...\n " , :yellow
124
+ say_status "Rubygems" , "Adding Elasticsearch libraries into Gemfile...\n " , :yellow
118
125
puts '-' *80 , '' ; sleep 0.75
119
126
120
- gem 'elasticsearch' , :git => 'git@github.com:elasticsearch/elasticsearch-ruby.git'
121
- gem 'elasticsearch-model' , :git => 'git@github.com:elasticsearch/elasticsearch-rails.git'
122
- gem 'elasticsearch-rails' , :git => 'git@github.com:elasticsearch/elasticsearch-rails.git'
123
-
124
- git :add => '.'
125
- git :commit => "-m 'Added gems'"
127
+ gem 'elasticsearch' , git : 'git@github.com:elasticsearch/elasticsearch-ruby.git'
128
+ gem 'elasticsearch-model' , git : 'git@github.com:elasticsearch/elasticsearch-rails.git'
129
+ gem 'elasticsearch-rails' , git : 'git@github.com:elasticsearch/elasticsearch-rails.git'
126
130
127
131
# ----- Install gems ------------------------------------------------------------------------------
128
132
132
136
133
137
run "bundle install"
134
138
139
+ git add : "Gemfile*"
140
+ git commit : "-m 'Added libraries into Gemfile'"
141
+
135
142
# ----- Generate Article resource -----------------------------------------------------------------
136
143
137
144
puts
138
145
say_status "Model" , "Generating the Article resource..." , :yellow
139
146
puts '-' *80 , '' ; sleep 0.75
140
147
141
148
generate :scaffold , "Article title:string content:text published_on:date"
142
- route "root :to => 'articles#index'"
149
+ route "root to: 'articles#index'"
143
150
rake "db:migrate"
144
151
145
- git : add => '.'
146
- git : commit => "-m 'Added the Article resource'"
152
+ git add : "."
153
+ git commit : "-m 'Added the Article resource'"
147
154
148
155
# ----- Add Elasticsearch integration into the model ----------------------------------------------
149
156
@@ -159,7 +166,7 @@ class Article < ActiveRecord::Base
159
166
end
160
167
CODE
161
168
162
- git : commit => "-a -m 'Added Elasticsearch support into the Article model'"
169
+ git commit : "-a -m 'Added Elasticsearch support into the Article model'"
163
170
164
171
# ----- Add Elasticsearch integration into the interface ------------------------------------------
165
172
@@ -172,18 +179,18 @@ class Article < ActiveRecord::Base
172
179
def search
173
180
@articles = Article.search(params[:q]).records
174
181
175
- render : action => "index"
182
+ render action: "index"
176
183
end
177
184
178
185
# GET /articles/1
179
186
CODE
180
187
181
- gsub_file 'app/views/articles/index.html.erb' , %r{<h1>Listing articles</h1>} , <<-CODE
188
+ gsub_file 'app/views/articles/index.html.erb' , %r{<h1>Listing articles</h1>} , <<-CODE unless File . read ( 'app/views/articles/index.html.erb' ) . include? ( 'form_tag search_articles_path' )
182
189
<h1>Listing articles</h1>
183
190
184
191
<hr>
185
192
186
- <%= form_tag search_articles_path, : method => 'get' do %>
193
+ <%= form_tag search_articles_path, method: 'get' do %>
187
194
<%= label_tag :query %>
188
195
<%= text_field_tag :q, params[:q] %>
189
196
<%= submit_tag :search %>
@@ -197,13 +204,37 @@ def search
197
204
<%= link_to 'All articles', articles_path if params[:q] %>
198
205
CODE
199
206
200
- gsub_file 'config/routes.rb' , %r{resources :articles} , <<-CODE
207
+ gsub_file 'config/routes.rb' , %r{resources :articles$ } , <<-CODE
201
208
resources :articles do
202
209
collection { get :search }
203
210
end
204
211
CODE
205
212
206
- git :commit => "-a -m 'Added search form and controller action'"
213
+ gsub_file 'test/controllers/articles_controller_test.rb' , %r{setup do.*?end}m , <<-CODE
214
+ setup do
215
+ @article = articles(:one)
216
+
217
+ Article.__elasticsearch__.import
218
+ Article.__elasticsearch__.refresh_index!
219
+ end
220
+ CODE
221
+
222
+ gsub_file 'test/controllers/articles_controller_test.rb' , %r{test "should get index" do.*?end}m do |match |
223
+ match << <<-CODE
224
+
225
+
226
+ test "should get search results" do
227
+ get :search, q: 'mystring'
228
+ assert_response :success
229
+ assert_not_nil assigns(:articles)
230
+ assert_equal 2, assigns(:articles).size
231
+ end
232
+ CODE
233
+ end
234
+
235
+
236
+
237
+ git commit : "-a -m 'Added search form and controller action'"
207
238
208
239
# ----- Seed the database -------------------------------------------------------------------------
209
240
@@ -228,33 +259,35 @@ def search
228
259
229
260
puts "Creating articles..."
230
261
%w[ One Two Three Four Five ].each_with_index do |title, i|
231
- Article.create : title => title, : content => contents[i], : published_on => i.days.ago.utc
262
+ Article.create title: title, content: contents[i], published_on: i.days.ago.utc
232
263
end
233
264
234
265
else
235
266
236
- puts "Creating 10,000 articles..."
267
+ print "Generating articles..."
237
268
(1..ENV['COUNT'].to_i).each_with_index do |title, i|
238
- Article.create : title => "Title #{title}", : content => 'Lorem', : published_on => i.days.ago.utc
239
- print '.'
269
+ Article.create title: "Title #{title}", content: 'Lorem ipsum dolor ', published_on: i.days.ago.utc
270
+ print '.' if i % ENV['COUNT'].to_i/10 == 0
240
271
end
272
+ puts "\n"
241
273
242
274
end
243
275
}
244
276
277
+ run "rails runner 'Article.__elasticsearch__.create_index! force: true'"
245
278
rake "db:seed"
246
279
247
- git : add => "db/seeds.rb"
248
- git : commit => "-m 'Added database seeding script'"
280
+ git add : "db/seeds.rb"
281
+ git commit : "-m 'Added database seeding script'"
249
282
250
283
# ----- Print Git log -----------------------------------------------------------------------------
251
284
252
285
puts
253
286
say_status "Git" , "Details about the application:" , :yellow
254
287
puts '-' *80 , ''
255
288
256
- git : tag => "basic"
257
- git : log => "--reverse --oneline"
289
+ git tag : "basic"
290
+ git log : "--reverse --oneline"
258
291
259
292
# ----- Start the application ---------------------------------------------------------------------
260
293
0 commit comments