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.
- data/LICENSE +20 -0
- data/README +104 -0
- data/acts_as_ferret.gemspec +58 -0
- data/bin/aaf_install +29 -0
- data/config/ferret_server.yml +24 -0
- data/doc/README.win32 +23 -0
- data/doc/demo/README +154 -0
- data/doc/demo/README_DEMO +23 -0
- data/doc/demo/Rakefile +10 -0
- data/doc/demo/app/controllers/admin/backend_controller.rb +14 -0
- data/doc/demo/app/controllers/admin_area_controller.rb +4 -0
- data/doc/demo/app/controllers/application.rb +5 -0
- data/doc/demo/app/controllers/contents_controller.rb +49 -0
- data/doc/demo/app/controllers/searches_controller.rb +8 -0
- data/doc/demo/app/helpers/admin/backend_helper.rb +2 -0
- data/doc/demo/app/helpers/application_helper.rb +3 -0
- data/doc/demo/app/helpers/content_helper.rb +2 -0
- data/doc/demo/app/helpers/search_helper.rb +2 -0
- data/doc/demo/app/models/comment.rb +48 -0
- data/doc/demo/app/models/content.rb +12 -0
- data/doc/demo/app/models/content_base.rb +28 -0
- data/doc/demo/app/models/search.rb +19 -0
- data/doc/demo/app/models/shared_index1.rb +3 -0
- data/doc/demo/app/models/shared_index2.rb +3 -0
- data/doc/demo/app/models/special_content.rb +3 -0
- data/doc/demo/app/models/stats.rb +20 -0
- data/doc/demo/app/views/admin/backend/search.rhtml +18 -0
- data/doc/demo/app/views/contents/_form.rhtml +10 -0
- data/doc/demo/app/views/contents/edit.rhtml +9 -0
- data/doc/demo/app/views/contents/index.rhtml +24 -0
- data/doc/demo/app/views/contents/new.rhtml +8 -0
- data/doc/demo/app/views/contents/show.rhtml +8 -0
- data/doc/demo/app/views/layouts/application.html.erb +17 -0
- data/doc/demo/app/views/searches/_content.html.erb +2 -0
- data/doc/demo/app/views/searches/search.html.erb +20 -0
- data/doc/demo/config/boot.rb +109 -0
- data/doc/demo/config/database.yml +38 -0
- data/doc/demo/config/environment.rb +69 -0
- data/doc/demo/config/environments/development.rb +16 -0
- data/doc/demo/config/environments/production.rb +19 -0
- data/doc/demo/config/environments/test.rb +21 -0
- data/doc/demo/config/ferret_server.yml +18 -0
- data/doc/demo/config/lighttpd.conf +40 -0
- data/doc/demo/config/routes.rb +9 -0
- data/doc/demo/db/development_structure.sql +15 -0
- data/doc/demo/db/migrate/001_initial_migration.rb +18 -0
- data/doc/demo/db/migrate/002_add_type_to_contents.rb +9 -0
- data/doc/demo/db/migrate/003_create_shared_index1s.rb +11 -0
- data/doc/demo/db/migrate/004_create_shared_index2s.rb +11 -0
- data/doc/demo/db/migrate/005_special_field.rb +9 -0
- data/doc/demo/db/migrate/006_create_stats.rb +15 -0
- data/doc/demo/db/schema.sql +18 -0
- data/doc/demo/db/schema.sqlite +14 -0
- data/doc/demo/doc/README_FOR_APP +2 -0
- data/doc/demo/doc/howto.txt +70 -0
- data/doc/demo/public/404.html +8 -0
- data/doc/demo/public/500.html +8 -0
- data/doc/demo/public/dispatch.cgi +10 -0
- data/doc/demo/public/dispatch.fcgi +24 -0
- data/doc/demo/public/dispatch.rb +10 -0
- data/doc/demo/public/favicon.ico +0 -0
- data/doc/demo/public/images/rails.png +0 -0
- data/doc/demo/public/index.html +277 -0
- data/doc/demo/public/robots.txt +1 -0
- data/doc/demo/public/stylesheets/scaffold.css +74 -0
- data/doc/demo/script/about +3 -0
- data/doc/demo/script/breakpointer +3 -0
- data/doc/demo/script/console +3 -0
- data/doc/demo/script/destroy +3 -0
- data/doc/demo/script/ferret_server +10 -0
- data/doc/demo/script/generate +3 -0
- data/doc/demo/script/performance/benchmarker +3 -0
- data/doc/demo/script/performance/profiler +3 -0
- data/doc/demo/script/plugin +3 -0
- data/doc/demo/script/process/inspector +3 -0
- data/doc/demo/script/process/reaper +3 -0
- data/doc/demo/script/process/spawner +3 -0
- data/doc/demo/script/process/spinner +3 -0
- data/doc/demo/script/runner +3 -0
- data/doc/demo/script/server +3 -0
- data/doc/demo/test/fixtures/comments.yml +12 -0
- data/doc/demo/test/fixtures/contents.yml +13 -0
- data/doc/demo/test/fixtures/remote_contents.yml +9 -0
- data/doc/demo/test/fixtures/shared_index1s.yml +7 -0
- data/doc/demo/test/fixtures/shared_index2s.yml +7 -0
- data/doc/demo/test/functional/admin/backend_controller_test.rb +35 -0
- data/doc/demo/test/functional/contents_controller_test.rb +81 -0
- data/doc/demo/test/functional/searches_controller_test.rb +71 -0
- data/doc/demo/test/smoke/drb_smoke_test.rb +321 -0
- data/doc/demo/test/smoke/process_stats.rb +21 -0
- data/doc/demo/test/test_helper.rb +30 -0
- data/doc/demo/test/unit/comment_test.rb +217 -0
- data/doc/demo/test/unit/content_test.rb +705 -0
- data/doc/demo/test/unit/ferret_result_test.rb +24 -0
- data/doc/demo/test/unit/multi_index_test.rb +329 -0
- data/doc/demo/test/unit/remote_index_test.rb +23 -0
- data/doc/demo/test/unit/shared_index1_test.rb +108 -0
- data/doc/demo/test/unit/shared_index2_test.rb +13 -0
- data/doc/demo/test/unit/sort_test.rb +21 -0
- data/doc/demo/test/unit/special_content_test.rb +25 -0
- data/doc/demo/vendor/plugins/will_paginate/LICENSE +18 -0
- data/doc/demo/vendor/plugins/will_paginate/README +108 -0
- data/doc/demo/vendor/plugins/will_paginate/Rakefile +23 -0
- data/doc/demo/vendor/plugins/will_paginate/init.rb +21 -0
- data/doc/demo/vendor/plugins/will_paginate/lib/will_paginate/collection.rb +45 -0
- data/doc/demo/vendor/plugins/will_paginate/lib/will_paginate/core_ext.rb +44 -0
- data/doc/demo/vendor/plugins/will_paginate/lib/will_paginate/finder.rb +159 -0
- data/doc/demo/vendor/plugins/will_paginate/lib/will_paginate/view_helpers.rb +95 -0
- data/doc/demo/vendor/plugins/will_paginate/test/array_pagination_test.rb +23 -0
- data/doc/demo/vendor/plugins/will_paginate/test/boot.rb +27 -0
- data/doc/demo/vendor/plugins/will_paginate/test/console +10 -0
- data/doc/demo/vendor/plugins/will_paginate/test/finder_test.rb +219 -0
- data/doc/demo/vendor/plugins/will_paginate/test/fixtures/admin.rb +3 -0
- data/doc/demo/vendor/plugins/will_paginate/test/fixtures/companies.yml +24 -0
- data/doc/demo/vendor/plugins/will_paginate/test/fixtures/company.rb +23 -0
- data/doc/demo/vendor/plugins/will_paginate/test/fixtures/developer.rb +11 -0
- data/doc/demo/vendor/plugins/will_paginate/test/fixtures/developers_projects.yml +13 -0
- data/doc/demo/vendor/plugins/will_paginate/test/fixtures/project.rb +4 -0
- data/doc/demo/vendor/plugins/will_paginate/test/fixtures/projects.yml +7 -0
- data/doc/demo/vendor/plugins/will_paginate/test/fixtures/replies.yml +20 -0
- data/doc/demo/vendor/plugins/will_paginate/test/fixtures/reply.rb +5 -0
- data/doc/demo/vendor/plugins/will_paginate/test/fixtures/schema.sql +44 -0
- data/doc/demo/vendor/plugins/will_paginate/test/fixtures/topic.rb +19 -0
- data/doc/demo/vendor/plugins/will_paginate/test/fixtures/topics.yml +30 -0
- data/doc/demo/vendor/plugins/will_paginate/test/fixtures/user.rb +2 -0
- data/doc/demo/vendor/plugins/will_paginate/test/fixtures/users.yml +35 -0
- data/doc/demo/vendor/plugins/will_paginate/test/helper.rb +42 -0
- data/doc/demo/vendor/plugins/will_paginate/test/lib/activerecord_test_connector.rb +64 -0
- data/doc/demo/vendor/plugins/will_paginate/test/lib/load_fixtures.rb +10 -0
- data/doc/demo/vendor/plugins/will_paginate/test/pagination_test.rb +136 -0
- data/doc/monit-example +22 -0
- data/init.rb +24 -0
- data/install.rb +18 -0
- data/lib/act_methods.rb +147 -0
- data/lib/acts_as_ferret.rb +593 -0
- data/lib/ar_mysql_auto_reconnect_patch.rb +41 -0
- data/lib/blank_slate.rb +54 -0
- data/lib/bulk_indexer.rb +56 -0
- data/lib/class_methods.rb +279 -0
- data/lib/ferret_extensions.rb +192 -0
- data/lib/ferret_find_methods.rb +142 -0
- data/lib/ferret_result.rb +58 -0
- data/lib/ferret_server.rb +238 -0
- data/lib/index.rb +99 -0
- data/lib/instance_methods.rb +172 -0
- data/lib/local_index.rb +202 -0
- data/lib/more_like_this.rb +217 -0
- data/lib/multi_index.rb +133 -0
- data/lib/rdig_adapter.rb +149 -0
- data/lib/remote_functions.rb +43 -0
- data/lib/remote_index.rb +54 -0
- data/lib/remote_multi_index.rb +20 -0
- data/lib/search_results.rb +50 -0
- data/lib/server_manager.rb +71 -0
- data/lib/unix_daemon.rb +86 -0
- data/lib/without_ar.rb +52 -0
- data/recipes/aaf_recipes.rb +116 -0
- data/script/ferret_daemon +94 -0
- data/script/ferret_server +12 -0
- data/script/ferret_service +178 -0
- data/tasks/ferret.rake +39 -0
- 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,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,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,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,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,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!
|