elasticsearch-persistence 5.0.2 → 6.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (81) hide show
  1. checksums.yaml +5 -5
  2. data/.rspec +2 -0
  3. data/Gemfile +9 -0
  4. data/README.md +206 -338
  5. data/Rakefile +15 -12
  6. data/elasticsearch-persistence.gemspec +6 -7
  7. data/examples/notes/application.rb +3 -4
  8. data/lib/elasticsearch/persistence.rb +2 -110
  9. data/lib/elasticsearch/persistence/repository.rb +212 -53
  10. data/lib/elasticsearch/persistence/repository/dsl.rb +94 -0
  11. data/lib/elasticsearch/persistence/repository/find.rb +27 -10
  12. data/lib/elasticsearch/persistence/repository/response/results.rb +21 -8
  13. data/lib/elasticsearch/persistence/repository/search.rb +30 -18
  14. data/lib/elasticsearch/persistence/repository/serialize.rb +65 -7
  15. data/lib/elasticsearch/persistence/repository/store.rb +38 -44
  16. data/lib/elasticsearch/persistence/version.rb +1 -1
  17. data/spec/repository/find_spec.rb +179 -0
  18. data/spec/repository/response/results_spec.rb +128 -0
  19. data/spec/repository/search_spec.rb +181 -0
  20. data/spec/repository/serialize_spec.rb +53 -0
  21. data/spec/repository/store_spec.rb +327 -0
  22. data/spec/repository_spec.rb +723 -0
  23. data/spec/spec_helper.rb +32 -0
  24. metadata +26 -104
  25. data/examples/music/album.rb +0 -54
  26. data/examples/music/artist.rb +0 -70
  27. data/examples/music/artists/_form.html.erb +0 -8
  28. data/examples/music/artists/artists_controller.rb +0 -67
  29. data/examples/music/artists/artists_controller_test.rb +0 -53
  30. data/examples/music/artists/index.html.erb +0 -60
  31. data/examples/music/artists/show.html.erb +0 -54
  32. data/examples/music/assets/application.css +0 -257
  33. data/examples/music/assets/autocomplete.css +0 -48
  34. data/examples/music/assets/blank_artist.png +0 -0
  35. data/examples/music/assets/blank_cover.png +0 -0
  36. data/examples/music/assets/form.css +0 -113
  37. data/examples/music/index_manager.rb +0 -73
  38. data/examples/music/search/index.html.erb +0 -95
  39. data/examples/music/search/search_controller.rb +0 -41
  40. data/examples/music/search/search_controller_test.rb +0 -12
  41. data/examples/music/search/search_helper.rb +0 -15
  42. data/examples/music/suggester.rb +0 -69
  43. data/examples/music/template.rb +0 -430
  44. data/examples/music/vendor/assets/jquery-ui-1.10.4.custom.min.css +0 -7
  45. data/examples/music/vendor/assets/jquery-ui-1.10.4.custom.min.js +0 -6
  46. data/examples/music/vendor/assets/stylesheets/ui-lightness/images/ui-bg_highlight-soft_100_eeeeee_1x100.png +0 -0
  47. data/lib/elasticsearch/persistence/client.rb +0 -51
  48. data/lib/elasticsearch/persistence/model.rb +0 -135
  49. data/lib/elasticsearch/persistence/model/base.rb +0 -87
  50. data/lib/elasticsearch/persistence/model/errors.rb +0 -8
  51. data/lib/elasticsearch/persistence/model/find.rb +0 -180
  52. data/lib/elasticsearch/persistence/model/rails.rb +0 -47
  53. data/lib/elasticsearch/persistence/model/store.rb +0 -254
  54. data/lib/elasticsearch/persistence/model/utils.rb +0 -0
  55. data/lib/elasticsearch/persistence/repository/class.rb +0 -71
  56. data/lib/elasticsearch/persistence/repository/naming.rb +0 -115
  57. data/lib/rails/generators/elasticsearch/model/model_generator.rb +0 -21
  58. data/lib/rails/generators/elasticsearch/model/templates/model.rb.tt +0 -9
  59. data/lib/rails/generators/elasticsearch_generator.rb +0 -2
  60. data/test/integration/model/model_basic_test.rb +0 -233
  61. data/test/integration/repository/custom_class_test.rb +0 -85
  62. data/test/integration/repository/customized_class_test.rb +0 -82
  63. data/test/integration/repository/default_class_test.rb +0 -116
  64. data/test/integration/repository/virtus_model_test.rb +0 -118
  65. data/test/test_helper.rb +0 -55
  66. data/test/unit/model_base_test.rb +0 -72
  67. data/test/unit/model_find_test.rb +0 -153
  68. data/test/unit/model_gateway_test.rb +0 -101
  69. data/test/unit/model_rails_test.rb +0 -112
  70. data/test/unit/model_store_test.rb +0 -576
  71. data/test/unit/persistence_test.rb +0 -32
  72. data/test/unit/repository_class_test.rb +0 -51
  73. data/test/unit/repository_client_test.rb +0 -32
  74. data/test/unit/repository_find_test.rb +0 -388
  75. data/test/unit/repository_indexing_test.rb +0 -37
  76. data/test/unit/repository_module_test.rb +0 -146
  77. data/test/unit/repository_naming_test.rb +0 -146
  78. data/test/unit/repository_response_results_test.rb +0 -98
  79. data/test/unit/repository_search_test.rb +0 -117
  80. data/test/unit/repository_serialize_test.rb +0 -57
  81. data/test/unit/repository_store_test.rb +0 -303
@@ -1,113 +0,0 @@
1
- /* Based on https://github.com/plataformatec/simple_form/wiki/CSS-for-simple_form */
2
-
3
- body.edit h1,
4
- body.new h1 {
5
- color: #999;
6
- font-size: 100%;
7
- text-transform: uppercase;
8
- margin: 0 0 1em 5.5em;
9
- }
10
-
11
- body.edit a[href^="/artists"],
12
- body.new a[href^="/artists"],
13
- body.edit a[href^="/music/artists"],
14
- body.new a[href^="/music/artists"] {
15
- color: #222;
16
- background: #ccc;
17
- text-decoration: none;
18
- border-radius: 0.3em;
19
- padding: 0.25em 0.5em;
20
- margin: 2em 0 0 5.5em;
21
- display: inline-block;
22
- }
23
-
24
- body.edit a[href^="/artists"]:hover,
25
- body.new a[href^="/artists"]:hover,
26
- body.edit a[href^="/music/artists"]:hover,
27
- body.new a[href^="/music/artists"]:hover {
28
- color: #fff;
29
- background: #333;
30
- }
31
-
32
- body.edit a[href^="/artists"]:last-child,
33
- body.new a[href^="/artists"]:last-child,
34
- body.edit a[href^="/music/artists"]:last-child,
35
- body.new a[href^="/music/artists"]:last-child {
36
- margin-left: 0;
37
- }
38
-
39
- .simple_form div.input {
40
- margin-bottom: 1em;
41
- clear: both;
42
- }
43
-
44
- .simple_form label {
45
- color: #878787;
46
- font-size: 80%;
47
- text-transform: uppercase;
48
- font-weight: 200;
49
- float: left;
50
- width: 5em;
51
- text-align: right;
52
- margin: 0.25em 1em;
53
- }
54
-
55
- div.boolean, .simple_form input[type='submit'] {
56
- margin-left: 8.5em;
57
- }
58
-
59
- .field_with_errors input {
60
- border: 2px solid #c70008 !important;
61
- }
62
-
63
- .simple_form .error {
64
- color: #fff !important;
65
- background: #c70008;
66
- font-weight: bold;
67
- clear: left;
68
- display: block;
69
- padding: 0.25em 0.5em;
70
- margin-left: 5.6em;
71
- width: 27.45em;
72
- }
73
-
74
- .simple_form .hint {
75
- color: #878787;
76
- font-size: 80%;
77
- font-style: italic;
78
- display: block;
79
- margin: 0.25em 0 0 7em;
80
- clear: left;
81
- }
82
-
83
- input {
84
- margin: 0;
85
- }
86
-
87
- input.radio {
88
- margin-right: 5px;
89
- vertical-align: -3px;
90
- }
91
-
92
- input.check_boxes {
93
- margin-left: 3px;
94
- vertical-align: -3px;
95
- }
96
-
97
- label.collection_check_boxes {
98
- float: none;
99
- margin: 0;
100
- vertical-align: -2px;
101
- margin-left: 2px;
102
- }
103
-
104
- input.string,
105
- textarea.text {
106
- padding: 0.5em;
107
- min-width: 40em;
108
- border: 1px solid #ccc;
109
- }
110
-
111
- textarea.text {
112
- min-height: 5em;
113
- }
@@ -1,73 +0,0 @@
1
- require 'open-uri'
2
-
3
- class IndexManager
4
- def self.create_index(options={})
5
- client = Artist.gateway.client
6
- index_name = Artist.index_name
7
-
8
- client.indices.delete index: index_name rescue nil if options[:force]
9
-
10
- settings = Artist.settings.to_hash.merge(Album.settings.to_hash)
11
- mappings = Artist.mappings.to_hash.merge(Album.mappings.to_hash)
12
-
13
- client.indices.create index: index_name,
14
- body: {
15
- settings: settings.to_hash,
16
- mappings: mappings.to_hash }
17
- end
18
-
19
- def self.import_from_yaml(source, options={})
20
- create_index force: true if options[:force]
21
-
22
- input = open(source)
23
- artists = YAML.load_documents input
24
-
25
- artists.each do |artist|
26
- Artist.create artist.update(
27
- 'album_count' => artist['releases'].size,
28
- 'members_combined' => artist['members'].join(', '),
29
- 'suggest' => {
30
- 'name' => {
31
- 'input' => { 'input' => artist['namevariations'].unshift(artist['name']).reject { |d| d.to_s.empty? } },
32
- 'output' => artist['name'],
33
- 'payload' => {
34
- 'url' => "/artists/#{artist['id']}"
35
- }
36
- },
37
- 'member' => {
38
- 'input' => { 'input' => artist['members'] },
39
- 'output' => artist['name'],
40
- 'payload' => {
41
- 'url' => "/artists/#{artist['id']}"
42
- }
43
- }
44
- }
45
- )
46
-
47
- artist['releases'].each do |album|
48
- album.update(
49
- 'suggest' => {
50
- 'title' => {
51
- 'input' => { 'input' => album['title'] },
52
- 'output' => album['title'],
53
- 'payload' => {
54
- 'url' => "/artists/#{artist['id']}#album_#{album['id']}"
55
- }
56
- },
57
- 'track' => {
58
- 'input' => { 'input' => album['tracklist'].map { |d| d['title'] }.reject { |d| d.to_s.empty? } },
59
- 'output' => album['title'],
60
- 'payload' => {
61
- 'url' => "/artists/#{artist['id']}#album_#{album['id']}"
62
- }
63
- }
64
- }
65
- )
66
- album['notes'] = album['notes'].to_s.gsub(/<.+?>/, '').gsub(/ {2,}/, '')
67
- album['released'] = nil if album['released'] < 1
68
-
69
- Album.create album, id: album['id'], parent: artist['id']
70
- end
71
- end
72
- end
73
- end
@@ -1,95 +0,0 @@
1
- <header>
2
- <h1>
3
- <span class="back"><%= link_to "〈".html_safe, :back, title: "Back" %></span>
4
- Artists &amp; Albums
5
- </h1>
6
- </header>
7
-
8
- <section id="searchbox">
9
- <%= form_tag search_path, method: 'get' do %>
10
- <input type="text" name="q" value="<%= params[:q] %>" id="q" autofocus="autofocus" placeholder="start typing to search..." tabindex="0" />
11
- <% end %>
12
- </section>
13
-
14
- <section class="artists">
15
- <% @artists.each do |artist| %>
16
- <%= content_tag :div, class: 'artist search result clearfix' do %>
17
- <h2>
18
- <%= image_tag "http://ruby.elastic.co.s3-website-us-east-1.amazonaws.com/demo/music/bands/#{artist.id}.jpeg", height: '45px', class: 'band' %>
19
- <%= link_to artist do %>
20
- <span class="name"><%= highlighted(artist, :name) %></span>
21
- <small><%= pluralize artist.album_count, 'album' %></small>
22
- <% end %>
23
- </h2>
24
- <% if highlight = highlight(artist, :members_combined) %>
25
- <p class="highlight small">
26
- <span class="label">Members</span>
27
- <%= highlight.first.html_safe %>
28
- </p>
29
- <% end %>
30
- <% if highlight = highlight(artist, :profile) %>
31
- <p class="highlight small">
32
- <span class="label">Profile</span>
33
- <%= highlight.join('&hellip;').html_safe %>
34
- </p>
35
- <% end %>
36
- <% end %>
37
- <% end %>
38
- </section>
39
-
40
- <section class="albums">
41
- <% @albums.each do |album| %>
42
- <%= content_tag :div, class: 'album search result clearfix' do %>
43
- <h2>
44
- <%= image_tag "http://ruby.elastic.co.s3-website-us-east-1.amazonaws.com/demo/music/covers/#{album.id}.jpeg", width: '45px', class: 'cover' %>
45
- <%= link_to artist_path(album.artist_id, anchor: "album_#{album.id}") do %>
46
- <span class="name"><%= highlighted(album, :title) %></span>
47
- <small><%= album.artist %></small>
48
- <small>(<%= [album.meta.formats.first, album.released].compact.join(' ') %>)</small>
49
- <% end %>
50
- </h2>
51
-
52
- <% if highlight = highlight(album, 'tracklist.title') %>
53
- <p class="highlight small">
54
- <span class="label">Tracks</span>
55
- <%= highlight.join('&hellip;').html_safe %>
56
- </p>
57
- <% end %>
58
-
59
- <% if highlight = highlight(album, :notes) %>
60
- <p class="highlight small">
61
- <span class="label">Notes</span>
62
- <%= highlight.map { |d| d.gsub(/^\.\s?/, '') }.join('&hellip;').html_safe %>
63
- </p>
64
- <% end %>
65
- <% end %>
66
- <% end %>
67
- </section>
68
-
69
- <% if @artists.empty? && @albums.empty? %>
70
- <section class="no-results">
71
- <p>The search hasn't returned any results...</p>
72
- </section>
73
- <% end %>
74
-
75
- <script>
76
- $.widget( "custom.suggest", $.ui.autocomplete, {
77
- _renderMenu: function( ul, items ) {
78
- $.each( items, function( index, item ) {
79
- var category = ul.append( "<li class='ui-autocomplete-category'>" + item.label + "</li>" );
80
-
81
- $.each( item.value, function( index, item ) {
82
- var li = $('<li class="ui-autocomplete-item"><a href="<%= Rails.application.config.relative_url_root %>'+ item.url +'">'+ item.text +'</a></li>').data('ui-autocomplete-item', item )
83
- category.append(li)
84
- } )
85
- });
86
- }
87
- });
88
-
89
- $( "#q" ).suggest({
90
- source: '<%= suggest_path %>',
91
- select: function(event, ui) {
92
- document.location.href = '<%= Rails.application.config.relative_url_root %>'+ui.item.url
93
- }
94
- });
95
- </script>
@@ -1,41 +0,0 @@
1
- class SearchController < ApplicationController
2
-
3
- def index
4
- tags = { pre_tags: '<em class="hl">', post_tags: '</em>' }
5
- @artists = Artist.search \
6
- query: {
7
- multi_match: {
8
- query: params[:q],
9
- fields: ['name^10','members^2','profile']
10
- }
11
- },
12
- highlight: {
13
- tags_schema: 'styled',
14
- fields: {
15
- name: { number_of_fragments: 0 },
16
- members_combined: { number_of_fragments: 0 },
17
- profile: { fragment_size: 50 }
18
- }
19
- }
20
-
21
- @albums = Album.search \
22
- query: {
23
- multi_match: {
24
- query: params[:q],
25
- fields: ['title^100','tracklist.title^10','notes^1']
26
- }
27
- },
28
- highlight: {
29
- tags_schema: 'styled',
30
- fields: {
31
- title: { number_of_fragments: 0 },
32
- 'tracklist.title' => { number_of_fragments: 0 },
33
- notes: { fragment_size: 50 }
34
- }
35
- }
36
- end
37
-
38
- def suggest
39
- render json: Suggester.new(params)
40
- end
41
- end
@@ -1,12 +0,0 @@
1
- require 'test_helper'
2
-
3
- class SearchControllerTest < ActionController::TestCase
4
- setup do
5
- IndexManager.create_index force: true
6
- end
7
-
8
- test "should get suggest" do
9
- get :suggest, term: 'foo'
10
- assert_response :success
11
- end
12
- end
@@ -1,15 +0,0 @@
1
- module SearchHelper
2
-
3
- def highlight(object, field)
4
- object.try(:hit).try(:highlight).try(field)
5
- end
6
-
7
- def highlighted(object, field)
8
- if h = object.try(:hit).try(:highlight).try(field).try(:first)
9
- h.html_safe
10
- else
11
- field.to_s.split('.').reduce(object) { |result,item| result.try(item) }
12
- end
13
- end
14
-
15
- end
@@ -1,69 +0,0 @@
1
- class Suggester
2
- attr_reader :response
3
-
4
- def initialize(params={})
5
- @term = params[:term]
6
- end
7
-
8
- def response
9
- @response ||= begin
10
- Elasticsearch::Persistence.client.search \
11
- index: Artist.index_name,
12
- body: {
13
- suggest: {
14
- artists: {
15
- text: @term,
16
- completion: { field: 'suggest.name.input', size: 25 }
17
- },
18
- members: {
19
- text: @term,
20
- completion: { field: 'suggest.member.input', size: 25 }
21
- },
22
- albums: {
23
- text: @term,
24
- completion: { field: 'suggest.title.input', size: 25 }
25
- },
26
- tracks: {
27
- text: @term,
28
- completion: { field: 'suggest.track.input', size: 25 }
29
- }
30
- },
31
- _source: ['suggest.*']
32
- }
33
- end
34
- end
35
-
36
- def as_json(options={})
37
- return [] unless response['suggest']
38
-
39
- output = [
40
- { label: 'Bands',
41
- value: response['suggest']['artists'][0]['options'].map do |d|
42
- { text: d['_source']['suggest']['name']['output'],
43
- url: d['_source']['suggest']['name']['payload']['url'] }
44
- end
45
- },
46
-
47
- { label: 'Albums',
48
- value: response['suggest']['albums'][0]['options'].map do |d|
49
- { text: d['_source']['suggest']['title']['output'],
50
- url: d['_source']['suggest']['title']['payload']['url'] }
51
- end
52
- },
53
-
54
- { label: 'Band Members',
55
- value: response['suggest']['members'][0]['options'].map do |d|
56
- { text: "#{d['text']} (#{d['_source']['suggest']['member']['output']})",
57
- url: d['_source']['suggest']['member']['payload']['url'] }
58
- end
59
- },
60
-
61
- { label: 'Album Tracks',
62
- value: response['suggest']['tracks'][0]['options'].map do |d|
63
- { text: "#{d['text']} (#{d['_source']['suggest']['track']['output']})",
64
- url: d['_source']['suggest']['track']['payload']['url'] }
65
- end
66
- }
67
- ]
68
- end
69
- end
@@ -1,430 +0,0 @@
1
- # ======================================================================================
2
- # Template for generating a Rails application with support for Elasticsearch persistence
3
- # ======================================================================================
4
- #
5
- # This file creates a fully working Rails application with support for storing and retrieving models
6
- # in Elasticsearch, using the `elasticsearch-persistence` gem
7
- # (https://github.com/elasticsearch/elasticsearch-rails/tree/master/elasticsearch-persistence).
8
- #
9
- # Requirements:
10
- # -------------
11
- #
12
- # * Git
13
- # * Ruby >= 1.9.3
14
- # * Rails >= 5
15
- # * Java >= 8 (for Elasticsearch)
16
- #
17
- # Usage:
18
- # ------
19
- #
20
- # $ time rails new music --force --skip --skip-bundle --skip-active-record --template https://raw.githubusercontent.com/elastic/elasticsearch-rails/master/elasticsearch-persistence/examples/music/template.rb
21
- #
22
- # =====================================================================================================
23
-
24
- STDOUT.sync = true
25
- STDERR.sync = true
26
-
27
- require 'uri'
28
- require 'json'
29
- require 'net/http'
30
-
31
- at_exit do
32
- pid = File.read("#{destination_root}/tmp/pids/elasticsearch.pid") rescue nil
33
- if pid
34
- say_status "Stop", "Elasticsearch", :yellow
35
- run "kill #{pid}"
36
- end
37
- end
38
-
39
- $elasticsearch_url = ENV.fetch('ELASTICSEARCH_URL', 'http://localhost:9200')
40
-
41
- # ----- Check & download Elasticsearch ------------------------------------------------------------
42
-
43
- cluster_info = Net::HTTP.get(URI.parse($elasticsearch_url)) rescue nil
44
- cluster_info = JSON.parse(cluster_info) if cluster_info
45
-
46
- if cluster_info.nil? || cluster_info['version']['number'] < '5'
47
- # Change the port when incompatible Elasticsearch version is running on localhost:9200
48
- if $elasticsearch_url == 'http://localhost:9200' && cluster_info && cluster_info['version']['number'] < '5'
49
- $change_port = '9280'
50
- $elasticsearch_url = "http://localhost:#{$change_port}"
51
- end
52
-
53
- COMMAND = <<-COMMAND.gsub(/^ /, '')
54
- curl -# -O "https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.2.1.tar.gz"
55
- tar -zxf elasticsearch-5.2.1.tar.gz
56
- rm -f elasticsearch-5.2.1.tar.gz
57
- ./elasticsearch-5.2.1/bin/elasticsearch -d -p #{destination_root}/tmp/pids/elasticsearch.pid #{$change_port.nil? ? '' : "-E http.port=#{$change_port}" }
58
- COMMAND
59
-
60
- puts "\n"
61
- say_status "ERROR", "Elasticsearch not running!\n", :red
62
- puts '-'*80
63
- say_status '', "It appears that Elasticsearch 5 is not running on this machine."
64
- say_status '', "Is it installed? Do you want me to install and run it for you with this command?\n\n"
65
- COMMAND.each_line { |l| say_status '', "$ #{l}" }
66
- puts
67
- say_status '', "(To uninstall, just remove the generated application directory.)"
68
- puts '-'*80, ''
69
-
70
- if yes?("Install Elasticsearch?", :bold)
71
- puts
72
- say_status "Install", "Elasticsearch", :yellow
73
-
74
- java_info = `java -version 2>&1`
75
-
76
- unless java_info.match /1\.[8-9]/
77
- puts
78
- say_status "ERROR", "Required Java version (1.8) not found, exiting...", :red
79
- exit(1)
80
- end
81
-
82
- commands = COMMAND.split("\n")
83
- exec = commands.pop
84
- inside("vendor") do
85
- commands.each { |command| run command }
86
- run "(#{exec})" # Launch Elasticsearch in subshell
87
- end
88
-
89
- # Wait for Elasticsearch to be up...
90
- #
91
- system <<-COMMAND
92
- until $(curl --silent --head --fail #{$elasticsearch_url} > /dev/null 2>&1); do
93
- printf '.'; sleep 1
94
- done
95
- COMMAND
96
- end
97
- end unless ENV['RAILS_NO_ES_INSTALL']
98
-
99
- # ----- Application skeleton ----------------------------------------------------------------------
100
-
101
- run "touch tmp/.gitignore"
102
-
103
- append_to_file ".gitignore", "vendor/elasticsearch-5.2.1/\n"
104
-
105
- git :init
106
- git add: "."
107
- git commit: "-m 'Initial commit: Clean application'"
108
-
109
- # ----- Add README --------------------------------------------------------------------------------
110
-
111
- puts
112
- say_status "README", "Adding Readme...\n", :yellow
113
- puts '-'*80, ''; sleep 0.25
114
-
115
- remove_file 'README.md'
116
-
117
- create_file 'README.md', <<-README
118
- = Ruby on Rails and Elasticsearch persistence: Example application
119
-
120
- README
121
-
122
-
123
- git add: "."
124
- git commit: "-m 'Added README for the application'"
125
-
126
- # ----- Use Pry as the Rails console --------------------------------------------------------------
127
-
128
- puts
129
- say_status "Rubygems", "Adding Pry into Gemfile...\n", :yellow
130
- puts '-'*80, '';
131
-
132
- gem_group :development do
133
- gem 'pry'
134
- gem 'pry-rails'
135
- end
136
-
137
- git add: "Gemfile*"
138
- git commit: "-m 'Added Pry into the Gemfile'"
139
-
140
- # ----- Auxiliary gems ----------------------------------------------------------------------------
141
-
142
- puts
143
- say_status "Rubygems", "Adding libraries into the Gemfile...\n", :yellow
144
- puts '-'*80, ''; sleep 0.75
145
-
146
- gem "simple_form"
147
-
148
- git add: "Gemfile*"
149
- git commit: "-m 'Added auxiliary libraries into the Gemfile'"
150
-
151
- # ----- Remove CoffeeScript, Sass and "all that jazz" ---------------------------------------------
152
-
153
- comment_lines 'Gemfile', /gem 'coffee/
154
- comment_lines 'Gemfile', /gem 'sass/
155
- comment_lines 'Gemfile', /gem 'uglifier/
156
- uncomment_lines 'Gemfile', /gem 'therubyracer/
157
-
158
- # ----- Add gems into Gemfile ---------------------------------------------------------------------
159
-
160
- puts
161
- say_status "Rubygems", "Adding Elasticsearch libraries into Gemfile...\n", :yellow
162
- puts '-'*80, ''; sleep 0.75
163
-
164
- gem 'elasticsearch', git: 'https://github.com/elastic/elasticsearch-ruby.git'
165
- gem 'elasticsearch-model', git: 'https://github.com/elastic/elasticsearch-rails.git', require: 'elasticsearch/model'
166
- gem 'elasticsearch-persistence', git: 'https://github.com/elastic/elasticsearch-rails.git', require: 'elasticsearch/persistence/model'
167
- gem 'elasticsearch-rails', git: 'https://github.com/elastic/elasticsearch-rails.git'
168
-
169
- git add: "Gemfile*"
170
- git commit: "-m 'Added the Elasticsearch libraries into the Gemfile'"
171
-
172
- # ----- Install gems ------------------------------------------------------------------------------
173
-
174
- puts
175
- say_status "Rubygems", "Installing Rubygems...", :yellow
176
- puts '-'*80, ''
177
-
178
- run "bundle install"
179
-
180
- # ----- Autoload ./lib ----------------------------------------------------------------------------
181
-
182
- puts
183
- say_status "Application", "Adding autoloading of ./lib...", :yellow
184
- puts '-'*80, ''
185
-
186
- insert_into_file 'config/application.rb',
187
- '
188
- config.autoload_paths += %W(#{config.root}/lib)
189
-
190
- ',
191
- after: 'class Application < Rails::Application'
192
-
193
- git commit: "-a -m 'Added autoloading of the ./lib folder'"
194
-
195
- # ----- Add jQuery UI ----------------------------------------------------------------------------
196
-
197
- puts
198
- say_status "Assets", "Adding jQuery UI...", :yellow
199
- puts '-'*80, ''; sleep 0.25
200
-
201
- if ENV['LOCAL']
202
- copy_file File.expand_path('../vendor/assets/jquery-ui-1.10.4.custom.min.js', __FILE__),
203
- 'vendor/assets/javascripts/jquery-ui-1.10.4.custom.min.js'
204
- copy_file File.expand_path('../vendor/assets/jquery-ui-1.10.4.custom.min.css', __FILE__),
205
- 'vendor/assets/stylesheets/ui-lightness/jquery-ui-1.10.4.custom.min.css'
206
- copy_file File.expand_path('../vendor/assets/stylesheets/ui-lightness/images/ui-bg_highlight-soft_100_eeeeee_1x100.png', __FILE__),
207
- 'vendor/assets/stylesheets/ui-lightness/images/ui-bg_highlight-soft_100_eeeeee_1x100.png'
208
- else
209
- get 'https://raw.githubusercontent.com/elasticsearch/elasticsearch-rails/master/elasticsearch-persistence/examples/music/vendor/assets/jquery-ui-1.10.4.custom.min.js',
210
- 'vendor/assets/javascripts/jquery-ui-1.10.4.custom.min.js'
211
- get 'https://raw.githubusercontent.com/elasticsearch/elasticsearch-rails/master/elasticsearch-persistence/examples/music/vendor/assets/jquery-ui-1.10.4.custom.min.css',
212
- 'vendor/assets/stylesheets/ui-lightness/jquery-ui-1.10.4.custom.min.css'
213
- get 'https://raw.githubusercontent.com/elasticsearch/elasticsearch-rails/master/elasticsearch-persistence/examples/music/vendor/assets/stylesheets/ui-lightness/images/ui-bg_highlight-soft_100_eeeeee_1x100.png',
214
- 'vendor/assets/stylesheets/ui-lightness/images/ui-bg_highlight-soft_100_eeeeee_1x100.png'
215
- end
216
-
217
- append_to_file 'app/assets/javascripts/application.js', "//= require jquery-ui-1.10.4.custom.min.js"
218
-
219
- git commit: "-a -m 'Added jQuery UI'"
220
-
221
- # ----- Generate Artist scaffold ------------------------------------------------------------------
222
-
223
- puts
224
- say_status "Model", "Generating the Artist scaffold...", :yellow
225
- puts '-'*80, ''; sleep 0.25
226
-
227
- generate :scaffold, "Artist name:String --orm=elasticsearch"
228
- route "root to: 'artists#index'"
229
-
230
- git add: "."
231
- git commit: "-m 'Added the generated Artist scaffold'"
232
-
233
- # ----- Generate Album model ----------------------------------------------------------------------
234
-
235
- puts
236
- say_status "Model", "Generating the Album model...", :yellow
237
- puts '-'*80, ''; sleep 0.25
238
-
239
- generate :model, "Album --orm=elasticsearch"
240
-
241
- git add: "."
242
- git commit: "-m 'Added the generated Album model'"
243
-
244
- # ----- Add proper model classes ------------------------------------------------------------------
245
-
246
- puts
247
- say_status "Model", "Adding Album, Artist and Suggester models implementation...", :yellow
248
- puts '-'*80, ''; sleep 0.25
249
-
250
- if ENV['LOCAL']
251
- copy_file File.expand_path('../artist.rb', __FILE__), 'app/models/artist.rb'
252
- copy_file File.expand_path('../album.rb', __FILE__), 'app/models/album.rb'
253
- copy_file File.expand_path('../suggester.rb', __FILE__), 'app/models/suggester.rb'
254
- else
255
- get 'https://raw.githubusercontent.com/elasticsearch/elasticsearch-rails/master/elasticsearch-persistence/examples/music/artist.rb',
256
- 'app/models/artist.rb'
257
- get 'https://raw.githubusercontent.com/elasticsearch/elasticsearch-rails/master/elasticsearch-persistence/examples/music/album.rb',
258
- 'app/models/album.rb'
259
- get 'https://raw.githubusercontent.com/elasticsearch/elasticsearch-rails/master/elasticsearch-persistence/examples/music/suggester.rb',
260
- 'app/models/suggester.rb'
261
- end
262
-
263
- git add: "./app/models"
264
- git commit: "-m 'Added Album, Artist and Suggester models implementation'"
265
-
266
- # ----- Add controllers and views -----------------------------------------------------------------
267
-
268
- puts
269
- say_status "Views", "Adding ArtistsController and views...", :yellow
270
- puts '-'*80, ''; sleep 0.25
271
-
272
- if ENV['LOCAL']
273
- copy_file File.expand_path('../artists/artists_controller.rb', __FILE__), 'app/controllers/artists_controller.rb'
274
- copy_file File.expand_path('../artists/index.html.erb', __FILE__), 'app/views/artists/index.html.erb'
275
- copy_file File.expand_path('../artists/show.html.erb', __FILE__), 'app/views/artists/show.html.erb'
276
- copy_file File.expand_path('../artists/_form.html.erb', __FILE__), 'app/views/artists/_form.html.erb'
277
- copy_file File.expand_path('../artists/artists_controller_test.rb', __FILE__),
278
- 'test/controllers/artists_controller_test.rb'
279
- else
280
- get 'https://raw.githubusercontent.com/elasticsearch/elasticsearch-rails/master/elasticsearch-persistence/examples/music/artists/artists_controller.rb',
281
- 'app/controllers/artists_controller.rb'
282
- get 'https://raw.githubusercontent.com/elasticsearch/elasticsearch-rails/master/elasticsearch-persistence/examples/music/artists/index.html.erb',
283
- 'app/views/artists/index.html.erb'
284
- get 'https://raw.githubusercontent.com/elasticsearch/elasticsearch-rails/master/elasticsearch-persistence/examples/music/artists/show.html.erb',
285
- 'app/views/artists/show.html.erb'
286
- get 'https://raw.githubusercontent.com/elasticsearch/elasticsearch-rails/master/elasticsearch-persistence/examples/music/artists/_form.html.erb',
287
- 'app/views/artists/_form.html.erb'
288
- get 'https://raw.githubusercontent.com/elasticsearch/elasticsearch-rails/master/elasticsearch-persistence/examples/music/artists/artists_controller_test.rb',
289
- 'test/controllers/artists_controller_test.rb'
290
- end
291
-
292
- git commit: "-a -m 'Added ArtistsController and related views'"
293
-
294
- puts
295
- say_status "Views", "Adding SearchController and views...", :yellow
296
- puts '-'*80, ''; sleep 0.25
297
-
298
- if ENV['LOCAL']
299
- copy_file File.expand_path('../search/search_controller.rb', __FILE__), 'app/controllers/search_controller.rb'
300
- copy_file File.expand_path('../search/search_helper.rb', __FILE__), 'app/helpers/search_helper.rb'
301
- copy_file File.expand_path('../search/index.html.erb', __FILE__), 'app/views/search/index.html.erb'
302
- copy_file File.expand_path('../search/search_controller_test.rb', __FILE__),
303
- 'test/controllers/search_controller_test.rb'
304
- else
305
- get 'https://raw.githubusercontent.com/elasticsearch/elasticsearch-rails/master/elasticsearch-persistence/examples/music/search/search_controller.rb',
306
- 'app/controllers/search_controller.rb'
307
- get 'https://raw.githubusercontent.com/elasticsearch/elasticsearch-rails/master/elasticsearch-persistence/examples/music/search/search_helper.rb',
308
- 'app/helpers/search_helper.rb'
309
- get 'https://raw.githubusercontent.com/elasticsearch/elasticsearch-rails/master/elasticsearch-persistence/examples/music/search/index.html.erb',
310
- 'app/views/search/index.html.erb'
311
- get 'https://raw.githubusercontent.com/elasticsearch/elasticsearch-rails/master/elasticsearch-persistence/examples/music/search/search_controller_test.rb',
312
- 'test/controllers/search_controller_test.rb'
313
- end
314
-
315
- route "get 'search', to: 'search#index'"
316
- route "get 'suggest', to: 'search#suggest'"
317
-
318
- comment_lines 'test/test_helper.rb', /fixtures \:all/
319
-
320
- git add: "."
321
- git commit: "-m 'Added SearchController and related views'"
322
-
323
- # ----- Add assets -----------------------------------------------------------------
324
-
325
- puts
326
- say_status "Views", "Adding application assets...", :yellow
327
- puts '-'*80, ''; sleep 0.25
328
-
329
- git rm: 'app/assets/stylesheets/scaffold.css'
330
-
331
- gsub_file 'app/views/layouts/application.html.erb', /<body>/, '<body class="<%= controller.action_name %>">'
332
-
333
- if ENV['LOCAL']
334
- copy_file File.expand_path('../assets/application.css', __FILE__), 'app/assets/stylesheets/application.css'
335
- copy_file File.expand_path('../assets/autocomplete.css', __FILE__), 'app/assets/stylesheets/autocomplete.css'
336
- copy_file File.expand_path('../assets/form.css', __FILE__), 'app/assets/stylesheets/form.css'
337
- copy_file File.expand_path('../assets/blank_cover.png', __FILE__), 'public/images/blank_cover.png'
338
- copy_file File.expand_path('../assets/blank_artist.png', __FILE__), 'public/images/blank_artist.png'
339
- else
340
- get 'https://raw.githubusercontent.com/elasticsearch/elasticsearch-rails/master/elasticsearch-persistence/examples/music/assets/application.css',
341
- 'app/assets/stylesheets/application.css'
342
- get 'https://raw.githubusercontent.com/elasticsearch/elasticsearch-rails/master/elasticsearch-persistence/examples/music/assets/autocomplete.css',
343
- 'app/assets/stylesheets/autocomplete.css'
344
- get 'https://raw.githubusercontent.com/elasticsearch/elasticsearch-rails/master/elasticsearch-persistence/examples/music/assets/form.css',
345
- 'app/assets/stylesheets/form.css'
346
- get 'https://raw.githubusercontent.com/elasticsearch/elasticsearch-rails/master/elasticsearch-persistence/examples/music/assets/blank_cover.png',
347
- 'public/images/blank_cover.png'
348
- get 'https://raw.githubusercontent.com/elasticsearch/elasticsearch-rails/master/elasticsearch-persistence/examples/music/assets/blank_artist.png',
349
- 'public/images/blank_artist.png'
350
- end
351
-
352
- git add: "."
353
- git commit: "-m 'Added application assets'"
354
-
355
- # ----- Add an Elasticsearch initializer ----------------------------------------------------------
356
-
357
- puts
358
- say_status "Initializer", "Adding an Elasticsearch initializer...", :yellow
359
- puts '-'*80, ''; sleep 0.25
360
-
361
- initializer 'elasticsearch.rb', %q{
362
- Elasticsearch::Persistence.client = Elasticsearch::Client.new host: ENV['ELASTICSEARCH_URL'] || 'localhost:9200'
363
-
364
- if Rails.env.development?
365
- logger = ActiveSupport::Logger.new(STDERR)
366
- logger.level = Logger::INFO
367
- logger.formatter = proc { |s, d, p, m| "\e[2m#{m}\n\e[0m" }
368
- Elasticsearch::Persistence.client.transport.logger = logger
369
- end
370
- }.gsub(/^ /, '')
371
-
372
- git add: "./config"
373
- git commit: "-m 'Added an Elasticsearch initializer'"
374
-
375
- # ----- Add IndexManager -----------------------------------------------------------------
376
-
377
- puts
378
- say_status "Application", "Adding the IndexManager class...", :yellow
379
- puts '-'*80, ''; sleep 0.25
380
-
381
- if ENV['LOCAL']
382
- copy_file File.expand_path('../index_manager.rb', __FILE__), 'lib/index_manager.rb'
383
- else
384
- get 'https://raw.githubusercontent.com/elasticsearch/elasticsearch-rails/master/elasticsearch-persistence/examples/music/index_manager.rb',
385
- 'lib/index_manager.rb'
386
- end
387
-
388
- # TODO: get 'https://raw.github.com/...', '...'
389
-
390
- git add: "."
391
- git commit: "-m 'Added the IndexManager class'"
392
-
393
- # ----- Import the data ---------------------------------------------------------------------------
394
-
395
- puts
396
- say_status "Data", "Import the data...", :yellow
397
- puts '-'*80, ''; sleep 0.25
398
-
399
- source = ENV.fetch('DATA_SOURCE', 'https://github.com/elastic/elasticsearch-rails/releases/download/dischord.yml/dischord.yml')
400
-
401
- run "ELASTICSEARCH_URL=#{$elasticsearch_url} rails runner 'IndexManager.import_from_yaml(\"#{source}\", force: true)'"
402
-
403
- # ----- Print Git log -----------------------------------------------------------------------------
404
-
405
- puts
406
- say_status "Git", "Details about the application:", :yellow
407
- puts '-'*80, ''
408
-
409
- run "git --no-pager log --reverse --oneline"
410
-
411
- # ----- Start the application ---------------------------------------------------------------------
412
-
413
- unless ENV['RAILS_NO_SERVER_START']
414
- require 'net/http'
415
- if (begin; Net::HTTP.get(URI('http://localhost:3000')); rescue Errno::ECONNREFUSED; false; rescue Exception; true; end)
416
- puts "\n"
417
- say_status "ERROR", "Some other application is running on port 3000!\n", :red
418
- puts '-'*80
419
-
420
- port = ask("Please provide free port:", :bold)
421
- else
422
- port = '3000'
423
- end
424
-
425
- puts "", "="*80
426
- say_status "DONE", "\e[1mStarting the application.\e[0m", :yellow
427
- puts "="*80, ""
428
-
429
- run "ELASTICSEARCH_URL=#{$elasticsearch_url} rails server --port=#{port}"
430
- end