Skip to content

Commit

Permalink
[MODEL] Added support for ActiveRecord/Mongoid 3 and 4 via separate G…
Browse files Browse the repository at this point in the history
…emfiles

Example:

    $ BUNDLE_GEMFILE=./gemfiles/3.gemfile bundle install
    $ BUNDLE_GEMFILE=./gemfiles/3.gemfile bundle exec rake test:integration

The syntax and support is modeled after <https://github.com/thoughtbot/appraisal>
  • Loading branch information
karmi committed Jan 18, 2014
1 parent a1486b5 commit fee1f2b
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 6 deletions.
3 changes: 3 additions & 0 deletions elasticsearch-model/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,6 @@ spec/reports
test/tmp
test/version_tmp
tmp

gemfiles/3.gemfile.lock
gemfiles/4.gemfile.lock
4 changes: 2 additions & 2 deletions elasticsearch-model/elasticsearch-model.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ Gem::Specification.new do |s|
s.add_development_dependency "sqlite3"
s.add_development_dependency "activesupport", "> 3.0"
s.add_development_dependency "activemodel", "> 3.0"
s.add_development_dependency "activerecord", "> 3.0"
s.add_development_dependency "mongoid", "> 2.0"
s.add_development_dependency "activerecord", "> 4.0"

s.add_development_dependency "oj"

s.add_development_dependency "shoulda-context"
Expand Down
10 changes: 7 additions & 3 deletions elasticsearch-model/examples/mongoid_article.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,18 @@

Mongoid.connect_to 'articles'

Elasticsearch::Model.client = Elasticsearch::Client.new log: true
Elasticsearch::Model.client = Elasticsearch::Client.new host: 'localhost:9250', log: true

class Article
include Mongoid::Document
field :id, type: String
field :title, type: String
field :published_at, type: DateTime
attr_accessible :id, :title, :published_at
attr_accessible :id, :title, :published_at if respond_to? :attr_accessible

def as_indexed_json(options={})
as_json(except: [:id, :_id])
end
end

# Extend the model with Elasticsearch support
Expand All @@ -45,7 +49,7 @@ class Article

# Index data
#
client = Elasticsearch::Client.new log:true
client = Elasticsearch::Client.new host:'localhost:9250', log:true

client.indices.delete index: 'articles' rescue nil
client.bulk index: 'articles',
Expand Down
11 changes: 11 additions & 0 deletions elasticsearch-model/gemfiles/3.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Usage:
#
# $ BUNDLE_GEMFILE=./gemfiles/3.gemfile bundle install
# $ BUNDLE_GEMFILE=./gemfiles/3.gemfile bundle exec rake test:integration

source 'https://rubygems.org'

gem 'activerecord', '~> 3.2'
gem 'mongoid', '>= 3.0'

gemspec path: '../'
11 changes: 11 additions & 0 deletions elasticsearch-model/gemfiles/4.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Usage:
#
# $ BUNDLE_GEMFILE=./gemfiles/4.gemfile bundle install
# $ BUNDLE_GEMFILE=./gemfiles/4.gemfile bundle exec rake test:integration

source 'https://rubygems.org'

gem 'activerecord', '~> 4'
gem 'mongoid', '~> 4.0.0.alpha1'

gemspec path: '../'
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
require 'test_helper'

puts "ActiveRecord #{ActiveRecord::VERSION::STRING}", '-'*80

module Elasticsearch
module Model
class ActiveRecordBasicIntegrationTest < Elasticsearch::Test::IntegrationTestCase
Expand Down
7 changes: 6 additions & 1 deletion elasticsearch-model/test/integration/mongoid_basic_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
end

if ENV["MONGODB_AVAILABLE"]
puts "Mongoid #{Mongoid::VERSION}", '-'*80

logger = ::Logger.new(STDERR)
logger.formatter = lambda { |s, d, p, m| " #{m.ansi(:faint, :cyan)}\n" }
Expand All @@ -30,12 +31,16 @@ class ::MongoidArticle

field :id, type: String
field :title, type: String
attr_accessible :title
attr_accessible :title if respond_to? :attr_accessible

mapping do
indexes :title, type: 'string', analyzer: 'snowball'
indexes :created_at, type: 'date'
end

def as_indexed_json(options={})
as_json(except: [:id, :_id])
end
end

context "Mongoid integration" do
Expand Down

0 comments on commit fee1f2b

Please sign in to comment.