Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Escargot is now rails 3.0.x compatible #13

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
*.db

# Generate gem file
*.gem
#*.gem

# vim swap files
*.swp
*.swo

# bak files
*.bak
74 changes: 74 additions & 0 deletions .specification
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
--- !ruby/object:Gem::Specification
name: escargot
version: !ruby/object:Gem::Version
version: 0.0.3
prerelease:
segments:
- 0
- 0
- 3
platform: ruby
authors:
- Angel Faus
autorequire:
bindir: bin
cert_chain: []
date: 2011-04-25 00:00:00.000000000Z
dependencies:
- !ruby/object:Gem::Dependency
name: bundler
requirement: &21062412 !ruby/object:Gem::Requirement
none: false
requirements:
- - ! '>='
- !ruby/object:Gem::Version
version: 1.0.0
type: :development
prerelease: false
version_requirements: *21062412
- !ruby/object:Gem::Dependency
name: rubberband
requirement: &21062052 !ruby/object:Gem::Requirement
none: false
requirements:
- - ! '>='
- !ruby/object:Gem::Version
version: 0.0.5
type: :runtime
prerelease: false
version_requirements: *21062052
description: Connects any Rails model with ElasticSearch, supports near real time
updates, distributed indexing and models that integrate data from many databases.
email:
- angel@vlex.com
executables: []
extensions: []
extra_rdoc_files: []
files: []
homepage: http://github.com/angelf/escargot
licenses: []
post_install_message:
rdoc_options: []
require_paths:
- lib
required_ruby_version: !ruby/object:Gem::Requirement
none: false
requirements:
- - ! '>='
- !ruby/object:Gem::Version
version: '0'
required_rubygems_version: !ruby/object:Gem::Requirement
none: false
requirements:
- - ! '>='
- !ruby/object:Gem::Version
version: 1.3.6
requirements: []
rubyforge_project: escargot
rubygems_version: 1.7.2
signing_key:
specification_version: 3
summary: ElasticSearch connector for Rails
test_files: []
has_rdoc: true

29 changes: 28 additions & 1 deletion README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,36 @@ distributed indexing and models that integrate data from many databases.

Requirements
============
Currently only rails 2.3 is supported. You will need ElasticSearch, the 'rubberband' gem
Escargot supports both rails 2.3.x and rails 3.0.x. You will need ElasticSearch, the 'rubberband' gem
and (if you want to use the **optional** distributed indexing mode) Redis.

Configuration
=============
By default, Escargot will try to connect to elasticsearch at

host: localhost

port: 9200

To explicitly specify a server host and a port of elasticsearch to connect to, one need to create
config/escargot.yml and add the following lines:

host: <elasticsearch-server>

port: <elasticsearch-port-number>

If you are lazy to create config/escargot.yml by hand, there is a solution for you named escargot install generator.
To invoque the generator, just trigger one of the following commands:

- rails 2.3.x:

ruby script/generate escargot_install

- rails 3.0.x:

rails generate escargot:install


Usage
=======

Expand Down
10 changes: 0 additions & 10 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
require 'rake'
require 'rake/testtask'
require 'rake/rdoctask'

desc 'Default: run unit tests.'
task :default => :test
Expand All @@ -11,13 +10,4 @@ Rake::TestTask.new(:test) do |t|
t.libs << 'test'
t.pattern = 'test/**/*_test.rb'
t.verbose = true
end

desc 'Generate documentation for the elastic_rails plugin.'
Rake::RDocTask.new(:rdoc) do |rdoc|
rdoc.rdoc_dir = 'rdoc'
rdoc.title = 'ElasticRails'
rdoc.options << '--line-numbers' << '--inline-source'
rdoc.rdoc_files.include('README')
rdoc.rdoc_files.include('lib/**/*.rb')
end
Binary file added escargot-0.0.4.gem
Binary file not shown.
3 changes: 2 additions & 1 deletion escargot.gemspec
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# -*- encoding: utf-8 -*-
require File.expand_path("../lib/escargot/version", __FILE__)
$:.push File.expand_path("../lib", __FILE__)
require "escargot/version"

Gem::Specification.new do |s|
s.name = "escargot"
Expand Down
9 changes: 9 additions & 0 deletions generators/escargot_install/USAGE
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Description:
Explain the generator

Example:
rails 2.3.x: ruby script/generate escargot_install
rails 3.0.x: rails generate escargot:install

This will create:
config/escargot.yml
11 changes: 11 additions & 0 deletions generators/escargot_install/escargot_install_generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# rails 2 generator
class EscargotInstallGenerator < Rails::Generator::Base

def manifest
record do |m|
m.template 'config/escargot.yml', 'config/escargot.yml'
end
end

end

2 changes: 2 additions & 0 deletions generators/escargot_install/templates/config/escargot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
host: localhost
port: 9200
15 changes: 11 additions & 4 deletions lib/escargot.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# Escargot
require 'elasticsearch'
require 'escargot/activerecord_ex'
require 'escargot/elasticsearch_ex'
Expand All @@ -7,7 +6,6 @@
require 'escargot/queue_backend/base'
require 'escargot/queue_backend/resque'


module Escargot
def self.register_model(model)
return unless model.table_exists?
Expand Down Expand Up @@ -80,8 +78,8 @@ def self.search_count(query = "*", options = {}, call_by_instance_method = false
def self.register_all_models
models = []
# Search all Models in the application Rails
Dir[File.join("#{RAILS_ROOT}/app/models".split(/\\/), "**", "*.rb")].each do |file|
model = file.gsub(/#{RAILS_ROOT}\/app\/models\/(.*?)\.rb/,'\1').classify.constantize
Dir[File.join(Rails.root.to_s + "/app/models".split(/\\/), "**", "*.rb")].each do |file|
model = file.gsub(/Rails.root.to_s\/app\/models\/(.*?)\.rb/,'\1').classify.constantize
unless models.include?(model)
require file
end
Expand All @@ -91,3 +89,12 @@ def self.register_all_models


end


#-------------------------------------------------------------------------------
require 'escargot'
require 'escargot/rails/init_commun'

# preserve rails 2.x compatibility
(Rails::VERSION::MAJOR == 3) ? (require 'escargot/rails/railtie') : (require 'escargot/rails/init')
#-------------------------------------------------------------------------------
15 changes: 15 additions & 0 deletions lib/escargot/rails/init.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require 'escargot'

ActiveRecord::Base.class_eval do
include Escargot::ActiveRecordExtensions
end

ElasticSearch::Api::Hit.class_eval do
include Escargot::HitExtensions
end

ElasticSearch::Client.class_eval do
include Escargot::AdminIndexVersions
end

init_elastic_Search_client
11 changes: 11 additions & 0 deletions lib/escargot/rails/init_commun.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
def init_elastic_Search_client
path_to_elasticsearch_config_file = Rails.root.to_s + "/config/escargot.yml"

unless File.exists?(path_to_elasticsearch_config_file)
Rails.logger.warn "No config/escargot.yaml file found, connecting to localhost:9200"
$elastic_search_client = ElasticSearch.new("localhost:9200")
else
config = YAML.load_file(path_to_elasticsearch_config_file)
$elastic_search_client = ElasticSearch.new(config["host"] + ":" + config["port"].to_s, :timeout => 20)
end
end
24 changes: 24 additions & 0 deletions lib/escargot/rails/railtie.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module Escargot
class Railtie < ::Rails::Railtie
initializer 'escargot.init' do
ActiveSupport.on_load(:active_record) do
ElasticSearch::Api::Hit.class_eval{include Escargot::HitExtensions}
ElasticSearch::Client.class_eval{include Escargot::AdminIndexVersions}
include(Escargot::ActiveRecordExtensions)
init_elastic_Search_client
end
#ActiveSupport.on_load(:action_controller) do
# include(ElasticSearch::RequestLifecycle)
#end
end

rake_tasks do
load 'escargot/tasks/escargot.tasks'
end

generators do
load "generators/escargot_install/escargot_install_generator.rb"
end

end
end
50 changes: 50 additions & 0 deletions lib/escargot/tasks/escargot.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# desc "Explaining what the task does"
# task :elastic_rails do
# # Task goes here
# end

namespace :escargot do
desc "indexes the models"
task :index, :models, :needs => [:environment, :load_all_models] do |t, args|
each_indexed_model(args) do |model|
puts "Indexing #{model}"
Escargot::LocalIndexing.create_index_for_model(model)
end
end

desc "indexes the models"
task :distributed_index, :models, :needs => [:environment, :load_all_models] do |t, args|
each_indexed_model(args) do |model|
puts "Indexing #{model}"
Escargot::DistributedIndexing.create_index_for_model(model)
end
end

desc "prunes old index versions for this models"
task :prune_versions, :models, :needs => [:environment, :load_all_models] do |t, args|
each_indexed_model(args) do |model|
$elastic_search_client.prune_index_versions(model.index_name)
end
end

task :load_all_models do
models = ActiveRecord::Base.send(:subclasses)
Dir["#{Rails.root}/app/models/*.rb", "#{Rails.root}/app/models/*/*.rb"].each do |file|
model = File.basename(file, ".*").classify
unless models.include?(model)
require file
end
models << model
end
end

private
def each_indexed_model(args)
if args[:models]
models = args[:models].split(",").map{|m| m.classify.constantize}
else
models = Escargot.indexed_models
end
models.each{|m| yield m}
end
end
2 changes: 1 addition & 1 deletion lib/escargot/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Escargot
VERSION = "0.0.3"
VERSION = "0.0.4"
end
9 changes: 9 additions & 0 deletions lib/generators/escargot_install/USAGE
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Description:
Explain the generator

Example:
rails 2.3.x: ruby script/generate escargot_install
rails 3.0.x: rails generate escargot:install

This will create:
config/escargot.yml
14 changes: 14 additions & 0 deletions lib/generators/escargot_install/escargot_install_generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module Escargot
class InstallGenerator < Rails::Generators::NamedBase

source_root File.expand_path('../templates', __FILE__)

argument :name, :required => false, :type => :string, :default => "escargot.yml"

def copy_config_file
template 'config/escargot.yml'
end

end
end

2 changes: 2 additions & 0 deletions lib/generators/escargot_install/templates/config/escargot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
host: localhost
port: 9200
21 changes: 0 additions & 21 deletions rails/init.rb

This file was deleted.