diff --git a/.gitignore b/.gitignore index ff04183..cbe4ddb 100644 --- a/.gitignore +++ b/.gitignore @@ -2,8 +2,11 @@ *.db # Generate gem file -*.gem +#*.gem # vim swap files *.swp *.swo + +# bak files +*.bak diff --git a/.specification b/.specification new file mode 100644 index 0000000..1af32c3 --- /dev/null +++ b/.specification @@ -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 + diff --git a/README.markdown b/README.markdown index b872582..8987f10 100644 --- a/README.markdown +++ b/README.markdown @@ -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: + + port: + +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 ======= diff --git a/Rakefile b/Rakefile index 24684b2..79cad86 100644 --- a/Rakefile +++ b/Rakefile @@ -1,6 +1,5 @@ require 'rake' require 'rake/testtask' -require 'rake/rdoctask' desc 'Default: run unit tests.' task :default => :test @@ -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 \ No newline at end of file diff --git a/escargot-0.0.4.gem b/escargot-0.0.4.gem new file mode 100644 index 0000000..9247499 Binary files /dev/null and b/escargot-0.0.4.gem differ diff --git a/escargot.gemspec b/escargot.gemspec index 24b44db..593055c 100644 --- a/escargot.gemspec +++ b/escargot.gemspec @@ -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" diff --git a/generators/escargot_install/USAGE b/generators/escargot_install/USAGE new file mode 100644 index 0000000..fc2a646 --- /dev/null +++ b/generators/escargot_install/USAGE @@ -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 diff --git a/generators/escargot_install/escargot_install_generator.rb b/generators/escargot_install/escargot_install_generator.rb new file mode 100644 index 0000000..ebaab64 --- /dev/null +++ b/generators/escargot_install/escargot_install_generator.rb @@ -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 + diff --git a/generators/escargot_install/templates/config/escargot.yml b/generators/escargot_install/templates/config/escargot.yml new file mode 100644 index 0000000..fa29b5c --- /dev/null +++ b/generators/escargot_install/templates/config/escargot.yml @@ -0,0 +1,2 @@ +host: localhost +port: 9200 \ No newline at end of file diff --git a/lib/escargot.rb b/lib/escargot.rb index 22b29f0..18695b3 100644 --- a/lib/escargot.rb +++ b/lib/escargot.rb @@ -1,4 +1,3 @@ -# Escargot require 'elasticsearch' require 'escargot/activerecord_ex' require 'escargot/elasticsearch_ex' @@ -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? @@ -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 @@ -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') +#------------------------------------------------------------------------------- diff --git a/lib/escargot/rails/init.rb b/lib/escargot/rails/init.rb new file mode 100644 index 0000000..ebb7f52 --- /dev/null +++ b/lib/escargot/rails/init.rb @@ -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 \ No newline at end of file diff --git a/lib/escargot/rails/init_commun.rb b/lib/escargot/rails/init_commun.rb new file mode 100644 index 0000000..77e3fdf --- /dev/null +++ b/lib/escargot/rails/init_commun.rb @@ -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 diff --git a/lib/escargot/rails/railtie.rb b/lib/escargot/rails/railtie.rb new file mode 100644 index 0000000..5584fc6 --- /dev/null +++ b/lib/escargot/rails/railtie.rb @@ -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 diff --git a/lib/escargot/tasks/escargot.rake b/lib/escargot/tasks/escargot.rake new file mode 100644 index 0000000..9db1263 --- /dev/null +++ b/lib/escargot/tasks/escargot.rake @@ -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 diff --git a/lib/escargot/version.rb b/lib/escargot/version.rb index 2f21807..855de2d 100644 --- a/lib/escargot/version.rb +++ b/lib/escargot/version.rb @@ -1,3 +1,3 @@ module Escargot - VERSION = "0.0.3" + VERSION = "0.0.4" end diff --git a/lib/generators/escargot_install/USAGE b/lib/generators/escargot_install/USAGE new file mode 100644 index 0000000..fc2a646 --- /dev/null +++ b/lib/generators/escargot_install/USAGE @@ -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 diff --git a/lib/generators/escargot_install/escargot_install_generator.rb b/lib/generators/escargot_install/escargot_install_generator.rb new file mode 100644 index 0000000..30a646d --- /dev/null +++ b/lib/generators/escargot_install/escargot_install_generator.rb @@ -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 + diff --git a/lib/generators/escargot_install/templates/config/escargot.yml b/lib/generators/escargot_install/templates/config/escargot.yml new file mode 100644 index 0000000..fa29b5c --- /dev/null +++ b/lib/generators/escargot_install/templates/config/escargot.yml @@ -0,0 +1,2 @@ +host: localhost +port: 9200 \ No newline at end of file diff --git a/rails/init.rb b/rails/init.rb deleted file mode 100644 index 7a3a5e7..0000000 --- a/rails/init.rb +++ /dev/null @@ -1,21 +0,0 @@ -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 - -unless File.exists?(Rails.root + "/config/elasticsearch.yml") - Rails.logger.warn "No config/elastic_search.yaml file found, connecting to localhost:9200" - $elastic_search_client = ElasticSearch.new("localhost:9200") -else - config = YAML.load_file(RAILS_ROOT + "/config/elasticsearch.yml") - $elastic_search_client = ElasticSearch.new(config["host"] + ":" + config["port"].to_s, :timeout => 20) -end \ No newline at end of file