Skip to content

Commit e1fc0e6

Browse files
committed
[RAILS] Updated the templates for example Rails applications
* Removed the code for Elasticsearch installation and added instructions for running Elasticsearch via Docker * Updated the Bootstrap CSS * Various fixes for application visual style * Various fixes in tests (Mocha stubbing, proper requires, etc) * Fixed the Elasticsearch error due to empty `post_filter`
1 parent 7815039 commit e1fc0e6

9 files changed

+105
-90
lines changed

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

+46-67
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
# * Git
1212
# * Ruby >= 1.9.3
1313
# * Rails >= 5
14-
# * Java >= 8 (for Elasticsearch)
1514
#
1615
# Usage:
1716
# ------
@@ -22,74 +21,54 @@
2221

2322
require 'uri'
2423
require 'net/http'
25-
26-
at_exit do
27-
pid = File.read("#{destination_root}/tmp/pids/elasticsearch.pid") rescue nil
28-
if pid
29-
say_status "Stop", "Elasticsearch", :yellow
30-
run "kill #{pid}"
31-
end
32-
end
24+
require 'json'
3325

3426
$elasticsearch_url = ENV.fetch('ELASTICSEARCH_URL', 'http://localhost:9200')
3527

36-
# ----- Check & download Elasticsearch ------------------------------------------------------------
28+
# ----- Check for Elasticsearch -------------------------------------------------------------------
3729

38-
cluster_info = Net::HTTP.get(URI.parse($elasticsearch_url)) rescue nil
39-
cluster_info = JSON.parse(cluster_info) if cluster_info
30+
required_elasticsearch_version = '6'
4031

41-
if cluster_info.nil? || cluster_info['version']['number'] < '5'
42-
# Change the port when incompatible Elasticsearch version is running on localhost:9200
43-
if $elasticsearch_url == 'http://localhost:9200' && cluster_info && cluster_info['version']['number'] < '5'
44-
$change_port = '9280'
45-
$elasticsearch_url = "http://localhost:#{$change_port}"
46-
end
32+
docker_command =<<-CMD.gsub(/\s{1,}/, ' ').strip
33+
docker run \
34+
--name elasticsearch-rails-searchapp \
35+
--publish 9200:9200 \
36+
--env "discovery.type=single-node" \
37+
--env "cluster.name=elasticsearch-rails" \
38+
--env "cluster.routing.allocation.disk.threshold_enabled=false" \
39+
--rm \
40+
docker.elastic.co/elasticsearch/elasticsearch-oss:6.3.0
41+
CMD
4742

48-
COMMAND = <<-COMMAND.gsub(/^ /, '')
49-
curl -# -O "https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.2.1.tar.gz"
50-
tar -zxf elasticsearch-5.2.1.tar.gz
51-
rm -f elasticsearch-5.2.1.tar.gz
52-
./elasticsearch-5.2.1/bin/elasticsearch -d -p #{destination_root}/tmp/pids/elasticsearch.pid #{$change_port.nil? ? '' : "-E http.port=#{$change_port}" }
53-
COMMAND
54-
55-
puts "\n"
56-
say_status "ERROR", "Elasticsearch not running!\n", :red
57-
puts '-'*80
58-
say_status '', "It appears that Elasticsearch 5 is not running on this machine."
59-
say_status '', "Is it installed? Do you want me to install and run it for you with this command?\n\n"
60-
COMMAND.each_line { |l| say_status '', "$ #{l}" }
61-
puts
62-
say_status '', "(To uninstall, just remove the generated application directory.)"
63-
puts '-'*80, ''
64-
65-
if yes?("Install Elasticsearch?", :bold)
66-
puts
67-
say_status "Install", "Elasticsearch", :yellow
68-
69-
java_info = `java -version 2>&1`
70-
71-
unless java_info.match /1\.[8-9]/
72-
puts
73-
say_status "ERROR", "Required Java version (1.8) not found, exiting...", :red
74-
exit(1)
75-
end
76-
77-
commands = COMMAND.split("\n")
78-
exec = commands.pop
79-
inside("vendor") do
80-
commands.each { |command| run command }
81-
run "(#{exec})" # Launch Elasticsearch in subshell
82-
end
83-
84-
# Wait for Elasticsearch to be up...
85-
#
86-
system <<-COMMAND
87-
until $(curl --silent --head --fail #{$elasticsearch_url} > /dev/null 2>&1); do
88-
printf '.'; sleep 1
89-
done
90-
COMMAND
91-
end
92-
end unless ENV['RAILS_NO_ES_INSTALL']
43+
begin
44+
cluster_info = Net::HTTP.get(URI.parse($elasticsearch_url))
45+
rescue Errno::ECONNREFUSED => e
46+
say_status "ERROR", "Cannot connect to Elasticsearch on <#{$elasticsearch_url}>\n\n", :red
47+
say_status "", "The application requires an Elasticsearch cluster running, " +
48+
"but no cluster has been found on <#{$elasticsearch_url}>."
49+
say_status "", "The easiest way of launching Elasticsearch is by running it with Docker (https://www.docker.com/get-docker):\n\n"
50+
say_status "", docker_command + "\n"
51+
exit(1)
52+
rescue StandardError => e
53+
say_status "ERROR", "#{e.class}: #{e.message}", :red
54+
exit(1)
55+
end
56+
57+
cluster_info = JSON.parse(cluster_info)
58+
59+
unless cluster_info['version']
60+
say_status "ERROR", "Cannot determine Elasticsearch version from <#{$elasticsearch_url}>", :red
61+
say_status "", JSON.dump(cluster_info), :red
62+
exit(1)
63+
end
64+
65+
if cluster_info['version']['number'] < required_elasticsearch_version
66+
say_status "ERROR",
67+
"The application requires Elasticsearch version #{required_elasticsearch_version} or higher, found version #{cluster_info['version']['number']}.\n\n", :red
68+
say_status "", "The easiest way of launching Elasticsearch is by running it with Docker (https://www.docker.com/get-docker):\n\n"
69+
say_status "", docker_command + "\n"
70+
exit(1)
71+
end
9372

9473
# ----- Application skeleton ----------------------------------------------------------------------
9574

@@ -144,7 +123,7 @@
144123

145124
# ----- Auxiliary gems ----------------------------------------------------------------------------
146125

147-
gem 'mocha', group: 'test', require: 'mocha/api'
126+
gem 'mocha', group: 'test'
148127
gem 'rails-controller-testing', group: 'test'
149128

150129
# ----- Remove CoffeeScript, Sass and "all that jazz" ---------------------------------------------
@@ -160,9 +139,9 @@
160139
say_status "Rubygems", "Adding Elasticsearch libraries into Gemfile...\n", :yellow
161140
puts '-'*80, ''; sleep 0.75
162141

163-
gem 'elasticsearch', git: 'git://github.com/elasticsearch/elasticsearch-ruby.git'
164-
gem 'elasticsearch-model', git: 'git://github.com/elasticsearch/elasticsearch-rails.git'
165-
gem 'elasticsearch-rails', git: 'git://github.com/elasticsearch/elasticsearch-rails.git'
142+
gem 'elasticsearch', git: 'https://github.com/elasticsearch/elasticsearch-ruby.git'
143+
gem 'elasticsearch-model', git: 'https://github.com/elasticsearch/elasticsearch-rails.git'
144+
gem 'elasticsearch-rails', git: 'https://github.com/elasticsearch/elasticsearch-rails.git'
166145

167146

168147
git add: "Gemfile*"

elasticsearch-rails/lib/rails/templates/02-pretty.rb

+35-15
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,13 @@ def self.search(query)
109109
end
110110
CODE
111111

112+
insert_into_file "test/test_helper.rb",
113+
"require 'mocha/minitest'\n\n",
114+
before: "class ActiveSupport::TestCase\n"
115+
112116
git add: "app/models/article.rb"
113117
git add: "test/**/article_test.rb"
118+
git add: "test/test_helper.rb"
114119
git commit: "-m 'Added an `Article.search` method'"
115120

116121
# ----- Add loading Bootstrap assets --------------------------------------------------------------
@@ -126,8 +131,9 @@ def self.search(query)
126131
CODE
127132

128133
insert_into_file 'app/views/layouts/application.html.erb', <<-CODE, before: '</head>'
129-
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.2/css/bootstrap.min.css">
130-
<script src="//netdna.bootstrapcdn.com/bootstrap/3.0.2/js/bootstrap.min.js"></script>
134+
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
135+
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
136+
<script src="https://stackpath.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
131137
CODE
132138

133139
git commit: "-a -m 'Added loading Bootstrap assets in the application layout'"
@@ -142,12 +148,6 @@ def self.search(query)
142148
<<-CODE
143149
<div class="input-group">
144150
<%= text_field_tag :q, params[:q], class: 'form-control', placeholder: 'Search...' %>
145-
146-
<span class="input-group-btn">
147-
<button type="button" class="btn btn-default">
148-
<span class="glyphicon glyphicon-search"></span>
149-
</button>
150-
</span>
151151
</div>
152152
CODE
153153
end
@@ -166,7 +166,7 @@ def self.search(query)
166166

167167
gsub_file 'app/views/articles/index.html.erb', %r{<td><%= link_to [^%]+} do |match|
168168
match.gsub!('<td>', '<td style="width: 50px">')
169-
match.include?("btn") ? match : (match + ", class: 'btn btn-default btn-xs'")
169+
match.include?("btn") ? match : (match + ", class: 'btn btn-outline-primary btn-sm'")
170170
end
171171

172172
gsub_file 'app/views/articles/index.html.erb', %r{<br>\s*(<\%= link_to 'New Article'.*)}m do |content|
@@ -190,6 +190,12 @@ def self.search(query)
190190
"\n " + match + ", class: 'btn btn-primary btn-xs', style: 'color: #fff'"
191191
end
192192

193+
# ----- Customize the form -----------------------------------------------------------------
194+
195+
gsub_file 'app/views/articles/_form.html.erb', %r{<div class="field">} do |match|
196+
%Q|<div class="form-group">|
197+
end
198+
193199
git add: "app/views"
194200
git commit: "-m 'Refactored the articles listing to use Bootstrap components'"
195201

@@ -229,9 +235,9 @@ def self.search(query)
229235
CODE
230236
end
231237

232-
generate "kaminari:views", "bootstrap2", "--force"
238+
generate "kaminari:views", "bootstrap3", "--force"
233239

234-
gsub_file 'app/views/kaminari/_paginator.html.erb', %r{<ul>}, '<ul class="pagination">'
240+
gsub_file 'app/views/kaminari/_paginator.html.erb', %r{<nav>}, '<nav class="pagination">'
235241

236242
git add: "."
237243
git commit: "-m 'Added pagination to articles listing'"
@@ -246,21 +252,35 @@ def self.search(query)
246252
unless File.read('app/assets/stylesheets/application.css').include?('.label-highlight')
247253
<<-CODE
248254
255+
body * {
256+
font-size: 100% !important;
257+
}
258+
259+
.table {
260+
border-bottom: 1px solid #dee2e6;
261+
}
262+
263+
.table td {
264+
vertical-align: middle !important;
265+
}
266+
249267
.label-highlight {
250268
font-size: 100% !important;
251269
font-weight: inherit !important;
252270
font-style: inherit !important;
253271
color: #333 !important;
254272
background: #fff401 !important;
273+
padding: 0.25em 0.5em
274+
border-radius: 5px;
255275
}
256276
257-
div.pagination {
277+
nav.pagination {
258278
text-align: center;
259-
display: block;
279+
display: inline-block;
260280
}
261281
262-
div.pagination ul {
263-
display: inline-block;
282+
ul.pagination {
283+
margin-bottom: 0;
264284
}
265285
266286
CODE

elasticsearch-rails/lib/rails/templates/03-expert.rb

+13-1
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ class Article < ActiveRecord::Base
183183
# ----- Add Sidekiq indexer -----------------------------------------------------------------------
184184

185185
puts
186-
say_status "Application", "Adding Sidekiq worker for updating the index...\n", :yellow
186+
say_status "Sidekiq", "Adding Sidekiq worker for updating the index...\n", :yellow
187187
puts '-'*80, ''; sleep 0.25
188188

189189
gem "sidekiq"
@@ -241,6 +241,18 @@ def index
241241
git add: "app/views/search/ app/assets/stylesheets/search.css"
242242
git commit: "-m 'Added SearchController#index'"
243243

244+
# ----- Add SearchController -----------------------------------------------------------------------
245+
246+
puts
247+
say_status "Views", "Updating application layout...\n", :yellow
248+
puts '-'*80, ''; sleep 0.25
249+
250+
insert_into_file 'app/views/layouts/application.html.erb', <<-CODE, before: '</head>'
251+
<link href="https://fonts.googleapis.com/css?family=Rokkitt:400,700" rel="stylesheet">
252+
CODE
253+
254+
git commit: "-a -m 'Updated application template'"
255+
244256
# ----- Add initializer ---------------------------------------------------------------------------
245257

246258
puts

elasticsearch-rails/lib/rails/templates/index.html.dsl.erb

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
</button>
3939
<ul class="dropdown-menu" role="menu">
4040
<li><%= link_to "Sort by published on", search_path(params.permit(:q, :a, :c, :s, :w, :comments).merge(s: 'published_on')), class: 'btn-xs' %></li>
41-
<li><%= link_to "Sort by relevancy", search_path(params.permit(:q, :a, :c, :s, :w, :comments).merge(s: nil)), class: 'btn-xs' %></li>
41+
<li><%= link_to "Sort by relevancy", search_path(params.permit(:q, :a, :c, :s, :w, :comments).merge(s: nil)), class: 'btn-xs' unless params[:q].blank? %></li>
4242
</ul>
4343
</div>
4444
</div>
@@ -122,7 +122,7 @@
122122
<div class="result">
123123
<h3 class="title">
124124
<%= (article.try(:highlight).try(:title) ? article.highlight.title.join.html_safe : article.title) %>
125-
<small class="category"><%= article.categories.to_sentence %></small>
125+
<small class="category"> | <%= article.categories.to_sentence %></small>
126126
</h3>
127127

128128
<p class="body">

elasticsearch-rails/lib/rails/templates/index.html.erb

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
</button>
3939
<ul class="dropdown-menu" role="menu">
4040
<li><%= link_to "Sort by published on", search_path(params.permit(:q, :a, :c, :s, :w, :comments).merge(s: 'published_on')), class: 'btn-xs' %></li>
41-
<li><%= link_to "Sort by relevancy", search_path(params.permit(:q, :a, :c, :s, :w, :comments).merge(s: nil)), class: 'btn-xs' %></li>
41+
<li><%= link_to "Sort by relevancy", search_path(params.permit(:q, :a, :c, :s, :w, :comments).merge(s: nil)), class: 'btn-xs' unless params[:q].blank? %></li>
4242
</ul>
4343
</div>
4444
</div>
@@ -122,7 +122,7 @@
122122
<div class="result">
123123
<h3 class="title">
124124
<%= (article.try(:highlight).try(:title) ? article.highlight.title.join.html_safe : article.title) %>
125-
<small class="category"><%= article.categories.to_sentence %></small>
125+
<small class="category"> | <%= article.categories.to_sentence %></small>
126126
</h3>
127127

128128
<p class="body">

elasticsearch-rails/lib/rails/templates/search.css

+4
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,13 @@ form #form-options input {
5454
}
5555

5656
.result h3.title {
57+
font-size: 180% !important;
5758
font-family: 'Rokkitt', sans-serif;
5859
margin-top: 0;
5960
}
61+
.result h3.title small {
62+
font-size: 75% !important;
63+
}
6064

6165
.result .body {
6266
font-family: Georgia, serif;

elasticsearch-rails/lib/rails/templates/search_controller_test.dsl.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
class SearchControllerTest < ActionController::TestCase
55
setup do
6-
Time.stubs(:now).returns(Time.new(2015, 03, 16, 10, 00, 00, 0))
6+
travel_to Time.new(2015, 03, 16, 10, 00, 00, 0)
77

88
Article.delete_all
99

elasticsearch-rails/lib/rails/templates/search_controller_test.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
class SearchControllerTest < ActionController::TestCase
44
setup do
5-
Time.stubs(:now).returns(Time.new(2015, 03, 16, 10, 00, 00, 0))
5+
travel_to Time.new(2015, 03, 16, 10, 00, 00, 0)
66

77
Article.delete_all
88

elasticsearch-rails/lib/rails/templates/searchable.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ def self.search(query, options={})
9595
}
9696
},
9797

98-
post_filter: {},
98+
post_filter: { bool: { must: [ match_all: {} ] } },
9999

100100
aggregations: {
101101
categories: {

0 commit comments

Comments
 (0)