Skip to content

Commit 14d636e

Browse files
David Padillakarmi
David Padilla
authored andcommitted
[RAILS] Added an example application template that loads settings from a file
Related: elastic#346 Related: elastic#351
1 parent b969783 commit 14d636e

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# $ rails new searchapp --skip --skip-bundle --template https://raw.githubusercontent.com/elastic/elasticsearch-rails/master/elasticsearch-rails/lib/rails/templates/05-settings-files.rb
2+
3+
# (See: 01-basic.rb, 02-pretty.rb, 03-expert.rb, 04-dsl.rb)
4+
5+
append_to_file 'README.rdoc', <<-README
6+
7+
== [5] Settings Files
8+
9+
The `settings-files` template refactors the `Searchable` module to load the index settings
10+
from an external file.
11+
12+
README
13+
14+
git add: "README.rdoc"
15+
git commit: "-m '[05] Updated the application README'"
16+
17+
# ----- Setup the Searchable module to load settings from config/elasticsearch/articles_settings.json
18+
19+
gsub_file "app/models/concerns/searchable.rb",
20+
/index: { number_of_shards: 1, number_of_replicas: 0 }/,
21+
"File.open('config/elasticsearch/articles_settings.json')"
22+
23+
git add: "app/models/concerns/searchable.rb"
24+
git commit: "-m 'Setup the Searchable module to load settings from file'"
25+
26+
# ----- Copy the articles_settings.json file -------------------------------------------------------
27+
28+
copy_file File.expand_path('../articles_settings.json', __FILE__), 'config/elasticsearch/articles_settings.json'
29+
30+
git add: "config/elasticsearch/articles_settings.json"
31+
git commit: "-m 'Create the articles settings file'"
32+
33+
# ----- Temporarily set local repo for testing ----------------------------------------------------
34+
35+
gsub_file "Gemfile",
36+
%r{gem 'elasticsearch-model', git: 'git://github.com/elasticsearch/elasticsearch-rails.git'},
37+
"gem 'elasticsearch-model', path: File.expand_path('../../../../../../elasticsearch-model', __FILE__)"
38+
39+
# ----- Run bundle install ------------------------------------------------------------------------
40+
41+
run "bundle install"
42+
43+
# ----- Recreate the index ------------------------------------------------------------------------
44+
45+
rake "environment elasticsearch:import:model CLASS='Article' BATCH=100 FORCE=y"
46+
47+
# ----- Print Git log -----------------------------------------------------------------------------
48+
49+
puts
50+
say_status "Git", "Details about the application:", :yellow
51+
puts '-'*80, ''
52+
53+
git tag: "settings-files"
54+
git log: "--reverse --oneline HEAD...dsl"
55+
56+
# ----- Start the application ---------------------------------------------------------------------
57+
58+
unless ENV['RAILS_NO_SERVER_START']
59+
require 'net/http'
60+
if (begin; Net::HTTP.get(URI('http://localhost:3000')); rescue Errno::ECONNREFUSED; false; rescue Exception; true; end)
61+
puts "\n"
62+
say_status "ERROR", "Some other application is running on port 3000!\n", :red
63+
puts '-'*80
64+
65+
port = ask("Please provide free port:", :bold)
66+
else
67+
port = '3000'
68+
end
69+
70+
puts "", "="*80
71+
say_status "DONE", "\e[1mStarting the application. Open http://localhost:#{port}\e[0m", :yellow
72+
puts "="*80, ""
73+
74+
run "rails server --port=#{port}"
75+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{ "number_of_shards": 1, "number_of_replicas": 0 }

0 commit comments

Comments
 (0)