watson-acts_as_ferret 0.4.8.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (162) hide show
  1. data/LICENSE +20 -0
  2. data/README +104 -0
  3. data/acts_as_ferret.gemspec +58 -0
  4. data/bin/aaf_install +29 -0
  5. data/config/ferret_server.yml +24 -0
  6. data/doc/README.win32 +23 -0
  7. data/doc/demo/README +154 -0
  8. data/doc/demo/README_DEMO +23 -0
  9. data/doc/demo/Rakefile +10 -0
  10. data/doc/demo/app/controllers/admin/backend_controller.rb +14 -0
  11. data/doc/demo/app/controllers/admin_area_controller.rb +4 -0
  12. data/doc/demo/app/controllers/application.rb +5 -0
  13. data/doc/demo/app/controllers/contents_controller.rb +49 -0
  14. data/doc/demo/app/controllers/searches_controller.rb +8 -0
  15. data/doc/demo/app/helpers/admin/backend_helper.rb +2 -0
  16. data/doc/demo/app/helpers/application_helper.rb +3 -0
  17. data/doc/demo/app/helpers/content_helper.rb +2 -0
  18. data/doc/demo/app/helpers/search_helper.rb +2 -0
  19. data/doc/demo/app/models/comment.rb +48 -0
  20. data/doc/demo/app/models/content.rb +12 -0
  21. data/doc/demo/app/models/content_base.rb +28 -0
  22. data/doc/demo/app/models/search.rb +19 -0
  23. data/doc/demo/app/models/shared_index1.rb +3 -0
  24. data/doc/demo/app/models/shared_index2.rb +3 -0
  25. data/doc/demo/app/models/special_content.rb +3 -0
  26. data/doc/demo/app/models/stats.rb +20 -0
  27. data/doc/demo/app/views/admin/backend/search.rhtml +18 -0
  28. data/doc/demo/app/views/contents/_form.rhtml +10 -0
  29. data/doc/demo/app/views/contents/edit.rhtml +9 -0
  30. data/doc/demo/app/views/contents/index.rhtml +24 -0
  31. data/doc/demo/app/views/contents/new.rhtml +8 -0
  32. data/doc/demo/app/views/contents/show.rhtml +8 -0
  33. data/doc/demo/app/views/layouts/application.html.erb +17 -0
  34. data/doc/demo/app/views/searches/_content.html.erb +2 -0
  35. data/doc/demo/app/views/searches/search.html.erb +20 -0
  36. data/doc/demo/config/boot.rb +109 -0
  37. data/doc/demo/config/database.yml +38 -0
  38. data/doc/demo/config/environment.rb +69 -0
  39. data/doc/demo/config/environments/development.rb +16 -0
  40. data/doc/demo/config/environments/production.rb +19 -0
  41. data/doc/demo/config/environments/test.rb +21 -0
  42. data/doc/demo/config/ferret_server.yml +18 -0
  43. data/doc/demo/config/lighttpd.conf +40 -0
  44. data/doc/demo/config/routes.rb +9 -0
  45. data/doc/demo/db/development_structure.sql +15 -0
  46. data/doc/demo/db/migrate/001_initial_migration.rb +18 -0
  47. data/doc/demo/db/migrate/002_add_type_to_contents.rb +9 -0
  48. data/doc/demo/db/migrate/003_create_shared_index1s.rb +11 -0
  49. data/doc/demo/db/migrate/004_create_shared_index2s.rb +11 -0
  50. data/doc/demo/db/migrate/005_special_field.rb +9 -0
  51. data/doc/demo/db/migrate/006_create_stats.rb +15 -0
  52. data/doc/demo/db/schema.sql +18 -0
  53. data/doc/demo/db/schema.sqlite +14 -0
  54. data/doc/demo/doc/README_FOR_APP +2 -0
  55. data/doc/demo/doc/howto.txt +70 -0
  56. data/doc/demo/public/404.html +8 -0
  57. data/doc/demo/public/500.html +8 -0
  58. data/doc/demo/public/dispatch.cgi +10 -0
  59. data/doc/demo/public/dispatch.fcgi +24 -0
  60. data/doc/demo/public/dispatch.rb +10 -0
  61. data/doc/demo/public/favicon.ico +0 -0
  62. data/doc/demo/public/images/rails.png +0 -0
  63. data/doc/demo/public/index.html +277 -0
  64. data/doc/demo/public/robots.txt +1 -0
  65. data/doc/demo/public/stylesheets/scaffold.css +74 -0
  66. data/doc/demo/script/about +3 -0
  67. data/doc/demo/script/breakpointer +3 -0
  68. data/doc/demo/script/console +3 -0
  69. data/doc/demo/script/destroy +3 -0
  70. data/doc/demo/script/ferret_server +10 -0
  71. data/doc/demo/script/generate +3 -0
  72. data/doc/demo/script/performance/benchmarker +3 -0
  73. data/doc/demo/script/performance/profiler +3 -0
  74. data/doc/demo/script/plugin +3 -0
  75. data/doc/demo/script/process/inspector +3 -0
  76. data/doc/demo/script/process/reaper +3 -0
  77. data/doc/demo/script/process/spawner +3 -0
  78. data/doc/demo/script/process/spinner +3 -0
  79. data/doc/demo/script/runner +3 -0
  80. data/doc/demo/script/server +3 -0
  81. data/doc/demo/test/fixtures/comments.yml +12 -0
  82. data/doc/demo/test/fixtures/contents.yml +13 -0
  83. data/doc/demo/test/fixtures/remote_contents.yml +9 -0
  84. data/doc/demo/test/fixtures/shared_index1s.yml +7 -0
  85. data/doc/demo/test/fixtures/shared_index2s.yml +7 -0
  86. data/doc/demo/test/functional/admin/backend_controller_test.rb +35 -0
  87. data/doc/demo/test/functional/contents_controller_test.rb +81 -0
  88. data/doc/demo/test/functional/searches_controller_test.rb +71 -0
  89. data/doc/demo/test/smoke/drb_smoke_test.rb +321 -0
  90. data/doc/demo/test/smoke/process_stats.rb +21 -0
  91. data/doc/demo/test/test_helper.rb +30 -0
  92. data/doc/demo/test/unit/comment_test.rb +217 -0
  93. data/doc/demo/test/unit/content_test.rb +705 -0
  94. data/doc/demo/test/unit/ferret_result_test.rb +24 -0
  95. data/doc/demo/test/unit/multi_index_test.rb +329 -0
  96. data/doc/demo/test/unit/remote_index_test.rb +23 -0
  97. data/doc/demo/test/unit/shared_index1_test.rb +108 -0
  98. data/doc/demo/test/unit/shared_index2_test.rb +13 -0
  99. data/doc/demo/test/unit/sort_test.rb +21 -0
  100. data/doc/demo/test/unit/special_content_test.rb +25 -0
  101. data/doc/demo/vendor/plugins/will_paginate/LICENSE +18 -0
  102. data/doc/demo/vendor/plugins/will_paginate/README +108 -0
  103. data/doc/demo/vendor/plugins/will_paginate/Rakefile +23 -0
  104. data/doc/demo/vendor/plugins/will_paginate/init.rb +21 -0
  105. data/doc/demo/vendor/plugins/will_paginate/lib/will_paginate/collection.rb +45 -0
  106. data/doc/demo/vendor/plugins/will_paginate/lib/will_paginate/core_ext.rb +44 -0
  107. data/doc/demo/vendor/plugins/will_paginate/lib/will_paginate/finder.rb +159 -0
  108. data/doc/demo/vendor/plugins/will_paginate/lib/will_paginate/view_helpers.rb +95 -0
  109. data/doc/demo/vendor/plugins/will_paginate/test/array_pagination_test.rb +23 -0
  110. data/doc/demo/vendor/plugins/will_paginate/test/boot.rb +27 -0
  111. data/doc/demo/vendor/plugins/will_paginate/test/console +10 -0
  112. data/doc/demo/vendor/plugins/will_paginate/test/finder_test.rb +219 -0
  113. data/doc/demo/vendor/plugins/will_paginate/test/fixtures/admin.rb +3 -0
  114. data/doc/demo/vendor/plugins/will_paginate/test/fixtures/companies.yml +24 -0
  115. data/doc/demo/vendor/plugins/will_paginate/test/fixtures/company.rb +23 -0
  116. data/doc/demo/vendor/plugins/will_paginate/test/fixtures/developer.rb +11 -0
  117. data/doc/demo/vendor/plugins/will_paginate/test/fixtures/developers_projects.yml +13 -0
  118. data/doc/demo/vendor/plugins/will_paginate/test/fixtures/project.rb +4 -0
  119. data/doc/demo/vendor/plugins/will_paginate/test/fixtures/projects.yml +7 -0
  120. data/doc/demo/vendor/plugins/will_paginate/test/fixtures/replies.yml +20 -0
  121. data/doc/demo/vendor/plugins/will_paginate/test/fixtures/reply.rb +5 -0
  122. data/doc/demo/vendor/plugins/will_paginate/test/fixtures/schema.sql +44 -0
  123. data/doc/demo/vendor/plugins/will_paginate/test/fixtures/topic.rb +19 -0
  124. data/doc/demo/vendor/plugins/will_paginate/test/fixtures/topics.yml +30 -0
  125. data/doc/demo/vendor/plugins/will_paginate/test/fixtures/user.rb +2 -0
  126. data/doc/demo/vendor/plugins/will_paginate/test/fixtures/users.yml +35 -0
  127. data/doc/demo/vendor/plugins/will_paginate/test/helper.rb +42 -0
  128. data/doc/demo/vendor/plugins/will_paginate/test/lib/activerecord_test_connector.rb +64 -0
  129. data/doc/demo/vendor/plugins/will_paginate/test/lib/load_fixtures.rb +10 -0
  130. data/doc/demo/vendor/plugins/will_paginate/test/pagination_test.rb +136 -0
  131. data/doc/monit-example +22 -0
  132. data/init.rb +24 -0
  133. data/install.rb +18 -0
  134. data/lib/act_methods.rb +147 -0
  135. data/lib/acts_as_ferret.rb +593 -0
  136. data/lib/ar_mysql_auto_reconnect_patch.rb +41 -0
  137. data/lib/blank_slate.rb +54 -0
  138. data/lib/bulk_indexer.rb +56 -0
  139. data/lib/class_methods.rb +279 -0
  140. data/lib/ferret_extensions.rb +192 -0
  141. data/lib/ferret_find_methods.rb +142 -0
  142. data/lib/ferret_result.rb +58 -0
  143. data/lib/ferret_server.rb +238 -0
  144. data/lib/index.rb +99 -0
  145. data/lib/instance_methods.rb +172 -0
  146. data/lib/local_index.rb +202 -0
  147. data/lib/more_like_this.rb +217 -0
  148. data/lib/multi_index.rb +133 -0
  149. data/lib/rdig_adapter.rb +149 -0
  150. data/lib/remote_functions.rb +43 -0
  151. data/lib/remote_index.rb +54 -0
  152. data/lib/remote_multi_index.rb +20 -0
  153. data/lib/search_results.rb +50 -0
  154. data/lib/server_manager.rb +71 -0
  155. data/lib/unix_daemon.rb +86 -0
  156. data/lib/without_ar.rb +52 -0
  157. data/recipes/aaf_recipes.rb +116 -0
  158. data/script/ferret_daemon +94 -0
  159. data/script/ferret_server +12 -0
  160. data/script/ferret_service +178 -0
  161. data/tasks/ferret.rake +39 -0
  162. metadata +246 -0
@@ -0,0 +1,14 @@
1
+ class Admin::BackendController < AdminAreaController
2
+
3
+ def search
4
+ @query = params[:query] || ''
5
+ unless @query.blank?
6
+ @results = Content.find_with_ferret @query
7
+ end
8
+ end
9
+
10
+ def show
11
+ @content = Content.find(params[:id])
12
+ render :template => 'content/show'
13
+ end
14
+ end
@@ -0,0 +1,4 @@
1
+ class AdminAreaController < ApplicationController
2
+
3
+ end
4
+
@@ -0,0 +1,5 @@
1
+ # Filters added to this controller will be run for all controllers in the application.
2
+ # Likewise, all the methods added will be available for all controllers.
3
+ class ApplicationController < ActionController::Base
4
+ end
5
+
@@ -0,0 +1,49 @@
1
+ class ContentsController < ApplicationController
2
+ before_filter :find_content, :only => [ :show, :edit, :update, :destroy ]
3
+
4
+ def index
5
+ @contents = Content.paginate :page => params[:page]
6
+ end
7
+
8
+ def show
9
+ end
10
+
11
+ def new
12
+ @content = Content.new
13
+ end
14
+
15
+ def create
16
+ @content = Content.new(params[:content])
17
+ if @content.save
18
+ flash[:notice] = 'Content was successfully created.'
19
+ redirect_to contents_url
20
+ else
21
+ render :action => 'new'
22
+ end
23
+ logger.error "#############{@content.errors.full_messages}"
24
+ end
25
+
26
+ def edit
27
+ end
28
+
29
+ def update
30
+ if @content.update_attributes(params[:content])
31
+ flash[:notice] = 'Content was successfully updated.'
32
+ redirect_to :action => 'show', :id => @content
33
+ else
34
+ render :action => 'edit'
35
+ end
36
+ end
37
+
38
+ def destroy
39
+ @content.destroy
40
+ redirect_to :action => 'list'
41
+ end
42
+
43
+ protected
44
+
45
+ def find_content
46
+ @content = Content.find params[:id]
47
+ end
48
+
49
+ end
@@ -0,0 +1,8 @@
1
+ class SearchesController < ApplicationController
2
+
3
+ def search
4
+ @search = Search.new params[:q], params[:page]
5
+ @results = @search.run if @search.valid?
6
+ end
7
+
8
+ end
@@ -0,0 +1,2 @@
1
+ module Admin::BackendHelper
2
+ end
@@ -0,0 +1,3 @@
1
+ # Methods added to this helper will be available to all templates in the application.
2
+ module ApplicationHelper
3
+ end
@@ -0,0 +1,2 @@
1
+ module ContentHelper
2
+ end
@@ -0,0 +1,2 @@
1
+ module SearchHelper
2
+ end
@@ -0,0 +1,48 @@
1
+
2
+ class PlainAsciiAnalyzer < ::Ferret::Analysis::Analyzer
3
+ include ::Ferret::Analysis
4
+ def token_stream(field, str)
5
+ StopFilter.new(
6
+ StandardTokenizer.new(str) ,
7
+ ["fax", "gsm"]
8
+ )
9
+ # raise #<<<----- is never executed when uncommented !!
10
+ end
11
+ end
12
+
13
+
14
+ class Comment < ActiveRecord::Base
15
+ belongs_to :parent, :class_name => 'Content', :foreign_key => :parent_id
16
+
17
+ # simplest case: just index all fields of this model:
18
+ # acts_as_ferret
19
+
20
+ # use the :additional_fields property to specify fields you intend
21
+ # to add in addition to those fields from your database table (which will be
22
+ # autodiscovered by acts_as_ferret)
23
+ # the :ignore flag tells aaf to not try to set this field's value itself (we
24
+ # do this in our custom to_doc method)
25
+ acts_as_ferret( :if => Proc.new { |comment| comment.do_index? },
26
+ :fields => {
27
+ :content => { :store => :yes },
28
+ :author => { },
29
+ :added => { :index => :untokenized, :store => :yes, :ignore => true },
30
+ :aliased => { :via => :content }
31
+ }, :ferret => { :analyzer => Ferret::Analysis::StandardAnalyzer.new(['fax', 'gsm', 'the', 'or']) } )
32
+ #}, :ferret => { :analyzer => PlainAsciiAnalyzer.new(['fax', 'gsm', 'the', 'or']) } )
33
+
34
+ def do_index?
35
+ self.content !~ /do not index/
36
+ end
37
+
38
+ # you can override the default to_doc method
39
+ # to customize what gets into your index.
40
+ def to_doc
41
+ # doc now has all the fields of our model instance, we
42
+ # just add another field to it:
43
+ doc = super
44
+ # add a field containing the current time
45
+ doc[:added] = Time.now.to_i.to_s
46
+ return doc
47
+ end
48
+ end
@@ -0,0 +1,12 @@
1
+ class Content < ContentBase #ActiveRecord::Base
2
+
3
+ def self.per_page; 10; end
4
+
5
+ has_many :comments
6
+
7
+ # returns the number of comments attached to this content.
8
+ # the value returned by this method will be indexed, too.
9
+ def comment_count
10
+ comments.size
11
+ end
12
+ end
@@ -0,0 +1,28 @@
1
+ # common base class for Content and SpecialContent
2
+ class ContentBase < ActiveRecord::Base
3
+ set_table_name 'contents'
4
+
5
+ # a higher boost means more importance for the field --> documents having a
6
+ # match in a field with a higher boost value will be ranked higher
7
+ #
8
+ # we use the store_class_name flag to be able to retrieve model instances when
9
+ # searching multiple indexes at once.
10
+ # the contents of the description field are stored in the index for running
11
+ # 'more like this' queries to find other content instances with similar
12
+ # descriptions
13
+ acts_as_ferret( :fields => { :comment_count => { :index => :untokenized },
14
+ :title => { :boost => :title_boost },
15
+ :description => { :boost => 1, :store => :yes },
16
+ :special => {} },
17
+ :boost => :record_boost)
18
+
19
+ def comment_count; 0 end
20
+
21
+ attr_accessor :record_boost
22
+ attr_writer :title_boost
23
+ def title_boost
24
+ @title_boost || 2
25
+ end
26
+ end
27
+
28
+
@@ -0,0 +1,19 @@
1
+ # search form model
2
+ class Search
3
+
4
+ attr_reader :query
5
+
6
+ def initialize(query, page)
7
+ @query = query
8
+ @page = page ? page.to_i : 1
9
+ end
10
+
11
+ def valid?
12
+ !@query.blank?
13
+ end
14
+
15
+ # run the search
16
+ def run
17
+ Content.find_with_ferret @query, :page => @page, :per_page => 10, :sort => 'title', :lazy => true
18
+ end
19
+ end
@@ -0,0 +1,3 @@
1
+ class SharedIndex1 < ActiveRecord::Base
2
+ acts_as_ferret :index => 'shared'
3
+ end
@@ -0,0 +1,3 @@
1
+ class SharedIndex2 < ActiveRecord::Base
2
+ acts_as_ferret :index => 'shared'
3
+ end
@@ -0,0 +1,3 @@
1
+ class SpecialContent < ContentBase
2
+
3
+ end
@@ -0,0 +1,20 @@
1
+ class Stats < ActiveRecord::Base
2
+ def self.compute(kind)
3
+ start_date = minimum :created_at
4
+ sql = <<-END
5
+ select min(processing_time), max(processing_time), avg(processing_time), stddev(processing_time),
6
+ concat_ws(':', hour(timediff(created_at, ?)), lpad(minute(timediff(created_at, ?)), 2, '0')) as time,
7
+ group_concat(processing_time) as data
8
+ from stats
9
+ where kind=? group by time;
10
+ END
11
+ result = connection.execute sanitize_sql([sql, start_date, start_date, kind.to_s])
12
+ [].tap do |res|
13
+ while row = result.fetch_row
14
+ data = row.last.split(',').map{|t|t.to_i}
15
+ median = data.size.odd? ? data[data.size/2] : ((data[data.size/2-1]+data[data.size/2]) / 2.0)
16
+ res << { :min => row[0].to_f, :max => row[1].to_f, :avg => row[2].to_f, :stddev => row[3].to_f, :time => row[4], :median => median }
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,18 @@
1
+ <h1>Search Content (backend)</h1>
2
+ <% form_tag :action => 'search' do %>
3
+ <fieldset>
4
+ <legend>Search</legend>
5
+ <input type="text" name="query" value="<%= h @query %>" />
6
+ </fieldset>
7
+ <%= submit_tag 'search' %>
8
+ <% end %>
9
+
10
+ <% if @results -%>
11
+ <p>Your search for <%= h @query %> returned <%= @results.size %> Results:</p>
12
+ <ul>
13
+ <% @results.each { |result| -%>
14
+ <li><%= link_to result.title, :action => 'show', :id => result %></li>
15
+ <% } -%>
16
+ </ul>
17
+ <% end -%>
18
+
@@ -0,0 +1,10 @@
1
+ <%= error_messages_for 'content' %>
2
+
3
+ <!--[form:content]-->
4
+ <p><label for="content_title">Title</label><br/>
5
+ <%= form.text_field 'title' %></p>
6
+
7
+ <p><label for="content_description">Description</label><br/>
8
+ <%= form.text_area 'description' %></p>
9
+ <!--[eoform:content]-->
10
+
@@ -0,0 +1,9 @@
1
+ <h1>Editing content</h1>
2
+
3
+ <% form_for :content, @content, content_path(@content), :html => { :method => :put } do |f| %>
4
+ <%= render :partial => 'form', :object => f %>
5
+ <%= submit_tag 'Edit' %>
6
+ <% end %>
7
+
8
+ <%= link_to 'Show', content_path(@content) %> |
9
+ <%= link_to 'Back', contents_path %>
@@ -0,0 +1,24 @@
1
+ <h1>Listing contents</h1>
2
+
3
+ <p><%= will_paginate @contents %></p>
4
+ <table>
5
+ <tr>
6
+ <% for column in Content.content_columns %>
7
+ <th><%= column.human_name %></th>
8
+ <% end %>
9
+ </tr>
10
+
11
+ <% for content in @contents %>
12
+ <tr>
13
+ <% for column in Content.content_columns %>
14
+ <td><%=h content.send(column.name) %></td>
15
+ <% end %>
16
+ <td><%= link_to 'Show', :action => 'show', :id => content %></td>
17
+ <td><%= link_to 'Edit', :action => 'edit', :id => content %></td>
18
+ <td><%= link_to 'Destroy', { :action => 'destroy', :id => content }, :confirm => 'Are you sure?' %></td>
19
+ </tr>
20
+ <% end %>
21
+ </table>
22
+
23
+ <p><%= will_paginate @contents %></p>
24
+ <p><%= link_to 'New content', :action => 'new' %></p>
@@ -0,0 +1,8 @@
1
+ <h1>New content</h1>
2
+
3
+ <% form_for :content, @content, :url => contents_path, :html => { :method => :post } do |f| %>
4
+ <%= render :partial => 'form', :object => f %>
5
+ <%= submit_tag "Create" %>
6
+ <% end %>
7
+
8
+ <%= link_to 'Back', :action => 'list' %>
@@ -0,0 +1,8 @@
1
+ <% for column in Content.content_columns %>
2
+ <p>
3
+ <b><%= column.human_name %>:</b> <%=h @content.send(column.name) %>
4
+ </p>
5
+ <% end %>
6
+
7
+ <%= link_to 'Edit', :action => 'edit', :id => @content %> |
8
+ <%= link_to 'Back', :action => 'list' %>
@@ -0,0 +1,17 @@
1
+ <html>
2
+ <head>
3
+ <title>acts_as_ferret demo application</title>
4
+ <%= stylesheet_link_tag 'scaffold' %>
5
+ </head>
6
+ <body>
7
+ <ul>
8
+ <li><%= link_to 'Contents', contents_path %></li>
9
+ <li><%= link_to 'Search', search_path %></li>
10
+ </ul>
11
+
12
+ <p style="color: green"><%= flash[:notice] %></p>
13
+
14
+ <%= yield %>
15
+
16
+ </body>
17
+ </html>
@@ -0,0 +1,2 @@
1
+ <li><%= link_to content.title, content_path(content.id) %></li>
2
+
@@ -0,0 +1,20 @@
1
+ <h1>Search</h1>
2
+ <% form_tag search_path do %>
3
+ <fieldset>
4
+ <legend>Search</legend>
5
+ <%= text_field_tag 'q', @search.query %>
6
+ </fieldset>
7
+ <%= submit_tag 'search' %>
8
+ <% end %>
9
+
10
+ <% if @results -%>
11
+
12
+ <p>Your search for <%= h @search.query %> returned <%= @results.total_hits %> Results:</p>
13
+
14
+ <p><%= will_paginate @results %></p>
15
+ <ul>
16
+ <%= render :partial => 'content', :collection => @results -%>
17
+ </ul>
18
+ <p><%= will_paginate @results %></p>
19
+
20
+ <% end -%>
@@ -0,0 +1,109 @@
1
+ # Don't change this file!
2
+ # Configure your app in config/environment.rb and config/environments/*.rb
3
+
4
+ RAILS_ROOT = "#{File.dirname(__FILE__)}/.." unless defined?(RAILS_ROOT)
5
+
6
+ module Rails
7
+ class << self
8
+ def boot!
9
+ unless booted?
10
+ preinitialize
11
+ pick_boot.run
12
+ end
13
+ end
14
+
15
+ def booted?
16
+ defined? Rails::Initializer
17
+ end
18
+
19
+ def pick_boot
20
+ (vendor_rails? ? VendorBoot : GemBoot).new
21
+ end
22
+
23
+ def vendor_rails?
24
+ File.exist?("#{RAILS_ROOT}/vendor/rails")
25
+ end
26
+
27
+ def preinitialize
28
+ load(preinitializer_path) if File.exist?(preinitializer_path)
29
+ end
30
+
31
+ def preinitializer_path
32
+ "#{RAILS_ROOT}/config/preinitializer.rb"
33
+ end
34
+ end
35
+
36
+ class Boot
37
+ def run
38
+ load_initializer
39
+ Rails::Initializer.run(:set_load_path)
40
+ end
41
+ end
42
+
43
+ class VendorBoot < Boot
44
+ def load_initializer
45
+ require "#{RAILS_ROOT}/vendor/rails/railties/lib/initializer"
46
+ Rails::Initializer.run(:install_gem_spec_stubs)
47
+ end
48
+ end
49
+
50
+ class GemBoot < Boot
51
+ def load_initializer
52
+ self.class.load_rubygems
53
+ load_rails_gem
54
+ require 'initializer'
55
+ end
56
+
57
+ def load_rails_gem
58
+ if version = self.class.gem_version
59
+ gem 'rails', version
60
+ else
61
+ gem 'rails'
62
+ end
63
+ rescue Gem::LoadError => load_error
64
+ $stderr.puts %(Missing the Rails #{version} gem. Please `gem install -v=#{version} rails`, update your RAILS_GEM_VERSION setting in config/environment.rb for the Rails version you do have installed, or comment out RAILS_GEM_VERSION to use the latest version installed.)
65
+ exit 1
66
+ end
67
+
68
+ class << self
69
+ def rubygems_version
70
+ Gem::RubyGemsVersion if defined? Gem::RubyGemsVersion
71
+ end
72
+
73
+ def gem_version
74
+ if defined? RAILS_GEM_VERSION
75
+ RAILS_GEM_VERSION
76
+ elsif ENV.include?('RAILS_GEM_VERSION')
77
+ ENV['RAILS_GEM_VERSION']
78
+ else
79
+ parse_gem_version(read_environment_rb)
80
+ end
81
+ end
82
+
83
+ def load_rubygems
84
+ require 'rubygems'
85
+ min_version = '1.1.1'
86
+ unless rubygems_version >= min_version
87
+ $stderr.puts %Q(Rails requires RubyGems >= #{min_version} (you have #{rubygems_version}). Please `gem update --system` and try again.)
88
+ exit 1
89
+ end
90
+
91
+ rescue LoadError
92
+ $stderr.puts %Q(Rails requires RubyGems >= #{min_version}. Please install RubyGems and try again: http://rubygems.rubyforge.org)
93
+ exit 1
94
+ end
95
+
96
+ def parse_gem_version(text)
97
+ $1 if text =~ /^[^#]*RAILS_GEM_VERSION\s*=\s*["']([!~<>=]*\s*[\d.]+)["']/
98
+ end
99
+
100
+ private
101
+ def read_environment_rb
102
+ File.read("#{RAILS_ROOT}/config/environment.rb")
103
+ end
104
+ end
105
+ end
106
+ end
107
+
108
+ # All that for this:
109
+ Rails.boot!