Skip to content

Commit 9137a0c

Browse files
committed
[RAILS] Updated and polished the "basic" and "pretty" application templates
1 parent 01f351a commit 9137a0c

File tree

2 files changed

+229
-87
lines changed

2 files changed

+229
-87
lines changed

elasticsearch-rails/lib/rails/templates/01-basic.rb

Lines changed: 63 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@
3636
append_to_file ".gitignore", "vendor/elasticsearch-0.90.7/\n"
3737

3838
git :init
39-
git :add => '.'
40-
git :commit => "-m 'Initial commit: Clean application'"
39+
git add: "."
40+
git commit: "-m 'Initial commit: Clean application'"
4141

4242
# ----- Download Elasticsearch --------------------------------------------------------------------
4343

@@ -75,9 +75,11 @@
7575
# ----- Add README --------------------------------------------------------------------------------
7676

7777
puts
78-
say_status "README", "Adding readme...\n", :yellow
78+
say_status "README", "Adding Readme...\n", :yellow
7979
puts '-'*80, ''; sleep 0.25
8080

81+
remove_file 'README.rdoc'
82+
8183
create_file 'README.rdoc', <<-README
8284
= Ruby on Rails and Elasticsearch: Example application
8385
@@ -87,7 +89,7 @@
8789
It has been generated by application templates available at
8890
https://github.com/elasticsearch/elasticsearch-rails/tree/master/elasticsearch-rails/lib/rails/templates.
8991
90-
== Basic
92+
== [1] Basic
9193
9294
The `basic` version provides a simple integration for a simple Rails model, `Article`, showing how
9395
to include the search engine support in your model, import the data, and provide form to perform
@@ -96,8 +98,8 @@
9698
README
9799

98100

99-
git :add => '.'
100-
git :commit => "-m 'Added README for the application'"
101+
git add: "."
102+
git commit: "-m 'Added README for the application'"
101103

102104
# ----- Use Thin ----------------------------------------------------------------------------------
103105

@@ -111,18 +113,20 @@
111113
rescue LoadError
112114
end
113115

116+
# ----- Auxiliary gems -----------------------------------------------------------------------------
117+
118+
gem 'turn', group: 'test'
119+
gem 'mocha', group: 'test', require: 'mocha/setup'
120+
114121
# ----- Add gems into Gemfile ---------------------------------------------------------------------
115122

116123
puts
117-
say_status "Rubygems", "Adding Rubygems into Gemfile...\n", :yellow
124+
say_status "Rubygems", "Adding Elasticsearch libraries into Gemfile...\n", :yellow
118125
puts '-'*80, ''; sleep 0.75
119126

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'
126130

127131
# ----- Install gems ------------------------------------------------------------------------------
128132

@@ -132,18 +136,21 @@
132136

133137
run "bundle install"
134138

139+
git add: "Gemfile*"
140+
git commit: "-m 'Added libraries into Gemfile'"
141+
135142
# ----- Generate Article resource -----------------------------------------------------------------
136143

137144
puts
138145
say_status "Model", "Generating the Article resource...", :yellow
139146
puts '-'*80, ''; sleep 0.75
140147

141148
generate :scaffold, "Article title:string content:text published_on:date"
142-
route "root :to => 'articles#index'"
149+
route "root to: 'articles#index'"
143150
rake "db:migrate"
144151

145-
git :add => '.'
146-
git :commit => "-m 'Added the Article resource'"
152+
git add: "."
153+
git commit: "-m 'Added the Article resource'"
147154

148155
# ----- Add Elasticsearch integration into the model ----------------------------------------------
149156

@@ -159,7 +166,7 @@ class Article < ActiveRecord::Base
159166
end
160167
CODE
161168

162-
git :commit => "-a -m 'Added Elasticsearch support into the Article model'"
169+
git commit: "-a -m 'Added Elasticsearch support into the Article model'"
163170

164171
# ----- Add Elasticsearch integration into the interface ------------------------------------------
165172

@@ -172,18 +179,18 @@ class Article < ActiveRecord::Base
172179
def search
173180
@articles = Article.search(params[:q]).records
174181
175-
render :action => "index"
182+
render action: "index"
176183
end
177184
178185
# GET /articles/1
179186
CODE
180187

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')
182189
<h1>Listing articles</h1>
183190
184191
<hr>
185192
186-
<%= form_tag search_articles_path, :method => 'get' do %>
193+
<%= form_tag search_articles_path, method: 'get' do %>
187194
<%= label_tag :query %>
188195
<%= text_field_tag :q, params[:q] %>
189196
<%= submit_tag :search %>
@@ -197,13 +204,37 @@ def search
197204
<%= link_to 'All articles', articles_path if params[:q] %>
198205
CODE
199206

200-
gsub_file 'config/routes.rb', %r{resources :articles}, <<-CODE
207+
gsub_file 'config/routes.rb', %r{resources :articles$}, <<-CODE
201208
resources :articles do
202209
collection { get :search }
203210
end
204211
CODE
205212

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'"
207238

208239
# ----- Seed the database -------------------------------------------------------------------------
209240

@@ -228,33 +259,35 @@ def search
228259
229260
puts "Creating articles..."
230261
%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
232263
end
233264
234265
else
235266
236-
puts "Creating 10,000 articles..."
267+
print "Generating articles..."
237268
(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
240271
end
272+
puts "\n"
241273
242274
end
243275
}
244276

277+
run "rails runner 'Article.__elasticsearch__.create_index! force: true'"
245278
rake "db:seed"
246279

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'"
249282

250283
# ----- Print Git log -----------------------------------------------------------------------------
251284

252285
puts
253286
say_status "Git", "Details about the application:", :yellow
254287
puts '-'*80, ''
255288

256-
git :tag => "basic"
257-
git :log => "--reverse --oneline"
289+
git tag: "basic"
290+
git log: "--reverse --oneline"
258291

259292
# ----- Start the application ---------------------------------------------------------------------
260293

0 commit comments

Comments
 (0)