diff --git a/Gemfile b/Gemfile index e45e65f..9f31790 100644 --- a/Gemfile +++ b/Gemfile @@ -1,2 +1,4 @@ source :rubygems gemspec + +gem "pry" diff --git a/Gemfile.lock b/Gemfile.lock index 091ed35..b1f1857 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -2,8 +2,8 @@ PATH remote: . specs: elastictastic (0.11.2) - activemodel (~> 3.0) - activesupport (~> 3.0) + activemodel (>= 3.0) + activesupport (>= 3.0) hashie i18n multi_json @@ -11,18 +11,29 @@ PATH GEM remote: http://rubygems.org/ specs: - activemodel (3.2.12) - activesupport (= 3.2.12) - builder (~> 3.0.0) - activesupport (3.2.12) - i18n (~> 0.6) - multi_json (~> 1.0) - builder (3.0.4) + activemodel (4.0.0) + activesupport (= 4.0.0) + builder (~> 3.1.0) + activesupport (4.0.0) + i18n (~> 0.6, >= 0.6.4) + minitest (~> 4.2) + multi_json (~> 1.3) + thread_safe (~> 0.1) + tzinfo (~> 0.3.37) + atomic (1.1.12) + builder (3.1.4) + coderay (1.0.9) diff-lcs (1.2.1) fakeweb (1.3.0) - hashie (2.0.3) + hashie (2.0.5) i18n (0.6.4) - multi_json (1.7.2) + method_source (0.8.2) + minitest (4.7.5) + multi_json (1.7.7) + pry (0.9.12.2) + coderay (~> 1.0.5) + method_source (~> 0.8) + slop (~> 3.4) rspec (2.13.0) rspec-core (~> 2.13.0) rspec-expectations (~> 2.13.0) @@ -31,6 +42,10 @@ GEM rspec-expectations (2.13.0) diff-lcs (>= 1.1.3, < 2.0) rspec-mocks (2.13.0) + slop (3.4.6) + thread_safe (0.1.2) + atomic + tzinfo (0.3.37) yard (0.8.5.2) PLATFORMS @@ -39,5 +54,6 @@ PLATFORMS DEPENDENCIES elastictastic! fakeweb (~> 1.3) + pry rspec (~> 2.0) yard (~> 0.6) diff --git a/elastictastic.gemspec b/elastictastic.gemspec index 3997d9d..ff50959 100644 --- a/elastictastic.gemspec +++ b/elastictastic.gemspec @@ -19,8 +19,8 @@ DESC s.has_rdoc = true s.extra_rdoc_files = 'README.md' s.required_ruby_version = '>= 1.9' - s.add_runtime_dependency 'activesupport', '~> 3.0' - s.add_runtime_dependency 'activemodel', '~> 3.0' + s.add_runtime_dependency 'activesupport', '>= 3.0' + s.add_runtime_dependency 'activemodel', '>= 3.0' s.add_runtime_dependency 'hashie' s.add_runtime_dependency 'i18n' s.add_runtime_dependency 'multi_json' diff --git a/lib/elastictastic.rb b/lib/elastictastic.rb index 9a06e11..4b63531 100644 --- a/lib/elastictastic.rb +++ b/lib/elastictastic.rb @@ -32,8 +32,6 @@ module Elastictastic autoload :Document, 'elastictastic/document' autoload :MassAssignmentSecurity, 'elastictastic/mass_assignment_security' autoload :NestedCollectionProxy, 'elastictastic/nested_collection_proxy' - autoload :Observer, 'elastictastic/observer' - autoload :Observing, 'elastictastic/observing' autoload :ParentChild, 'elastictastic/parent_child' autoload :Rotor, 'elastictastic/rotor' autoload :TestHelpers, 'elastictastic/test_helpers' @@ -200,4 +198,5 @@ def Index(name_or_index) end end + require 'elastictastic/railtie' if defined? Rails diff --git a/lib/elastictastic/basic_document.rb b/lib/elastictastic/basic_document.rb index d64cec0..4b5a187 100644 --- a/lib/elastictastic/basic_document.rb +++ b/lib/elastictastic/basic_document.rb @@ -3,7 +3,7 @@ module Elastictastic # The top-level module mixed in to classes which will be mapped as # ElasticSearch documents. Note that most people will want to use the Document # mixin, which extends BasicDocument with ActiveModel functionality such as - # validations, lifecycle hooks, observers, mass-assignment security, etc. The + # validations, lifecycle hooks, mass-assignment security, etc. The # BasicDocument module is exposed directly for those who wish to avoid the # performance penalty associated with ActiveModel functionality, or those who # wish to only mix in the ActiveModel modules they need. diff --git a/lib/elastictastic/configuration.rb b/lib/elastictastic/configuration.rb index 3a1c7c4..dfdccfd 100644 --- a/lib/elastictastic/configuration.rb +++ b/lib/elastictastic/configuration.rb @@ -78,14 +78,10 @@ def default_host def url_from_env return @url_from_env if defined? @url_from_env - @url_from_env = + @url_from_env = if ENV['ELASTICSEARCH_URL'] URI.parse(ENV['ELASTICSEARCH_URL']) end end - - ActiveModel::Observing::ClassMethods.public_instance_methods(false).each do |method| - delegate method, :to => :"::Elastictastic::Observing" - end end end diff --git a/lib/elastictastic/dirty.rb b/lib/elastictastic/dirty.rb index 7a4ff9b..49d5f21 100644 --- a/lib/elastictastic/dirty.rb +++ b/lib/elastictastic/dirty.rb @@ -19,7 +19,7 @@ def define_embed(embed_name, options) private - # + # # We have to rewrite ActiveModel functionality here because in Rails 3.0, # #define_attribute_methods has to be called exactly one time, and there's # no place for us to do that. This appears to be fixed in ActiveModel 3.1 diff --git a/lib/elastictastic/document.rb b/lib/elastictastic/document.rb index 234795e..d88949c 100644 --- a/lib/elastictastic/document.rb +++ b/lib/elastictastic/document.rb @@ -5,7 +5,6 @@ module Document included do include BasicDocument include Callbacks - include Observing include Dirty include MassAssignmentSecurity include Validations diff --git a/lib/elastictastic/mass_assignment_security.rb b/lib/elastictastic/mass_assignment_security.rb index 15066e6..57f32b6 100644 --- a/lib/elastictastic/mass_assignment_security.rb +++ b/lib/elastictastic/mass_assignment_security.rb @@ -2,12 +2,18 @@ module Elastictastic module MassAssignmentSecurity extend ActiveSupport::Concern - included do - include ActiveModel::MassAssignmentSecurity - end + if ActiveModel.version.version < '4.0' + included do + include ActiveModel::MassAssignmentSecurity + end - def attributes=(attributes) - super(sanitize_for_mass_assignment(attributes)) + def attributes=(attributes) + super(sanitize_for_mass_assignment(attributes)) + end + else + def attributes=(attributes) + super(attributes.tap {|attrs| attrs.delete(:index)}) + end end end end diff --git a/lib/elastictastic/observer.rb b/lib/elastictastic/observer.rb deleted file mode 100644 index fa2424d..0000000 --- a/lib/elastictastic/observer.rb +++ /dev/null @@ -1,3 +0,0 @@ -module Elastictastic - Observer = Class.new(ActiveModel::Observer) -end diff --git a/lib/elastictastic/observing.rb b/lib/elastictastic/observing.rb deleted file mode 100644 index 7e58094..0000000 --- a/lib/elastictastic/observing.rb +++ /dev/null @@ -1,23 +0,0 @@ -module Elastictastic - module Observing - extend ActiveSupport::Concern - extend ActiveModel::Observing::ClassMethods - - included do - include ActiveModel::Observing - end - - Callbacks::HOOKS.each do |method| - module_eval <<-RUBY, __FILE__, __LINE__ + 1 - def #{method}(options = {}) - if options[:observers] == false - super - else - notify_observers(:before_#{method}) - super.tap { notify_observers(:after_#{method}) } - end - end - RUBY - end - end -end diff --git a/lib/elastictastic/railtie.rb b/lib/elastictastic/railtie.rb index 3e6247b..e5b72b6 100644 --- a/lib/elastictastic/railtie.rb +++ b/lib/elastictastic/railtie.rb @@ -22,14 +22,5 @@ class Railtie < Rails::Railtie require 'elastictastic/new_relic_instrumentation' if defined? NewRelic end - initializer "elastictastic.instantiate_observers" do - config.after_initialize do - ::Elastictastic::Observing.instantiate_observers - - ActionDispatch::Callbacks.to_prepare do - ::Elastictastic::Observing.instantiate_observers - end - end - end end end diff --git a/spec/environment.rb b/spec/environment.rb index 2e50301..13ff31e 100644 --- a/spec/environment.rb +++ b/spec/environment.rb @@ -1,6 +1,6 @@ require 'bundler' Bundler.require(:default, :test, :development) -%w(author blog comment photo post post_observer).each do |model| +%w(author blog comment photo post).each do |model| require File.expand_path("../models/#{model}", __FILE__) end diff --git a/spec/examples/mass_assignment_security_spec.rb b/spec/examples/mass_assignment_security_spec.rb index 3aab945..52e4f1a 100644 --- a/spec/examples/mass_assignment_security_spec.rb +++ b/spec/examples/mass_assignment_security_spec.rb @@ -1,6 +1,6 @@ require File.expand_path('../spec_helper', __FILE__) -describe Elastictastic::MassAssignmentSecurity do +describe Elastictastic::MassAssignmentSecurity, compatibility: :active_model_4 do let(:post) { Post.new(:title => 'hey guy', :comments_count => 3) } it 'should allow allowed attributes' do diff --git a/spec/examples/middleware_spec.rb b/spec/examples/middleware_spec.rb index d5661a2..d7ad981 100644 --- a/spec/examples/middleware_spec.rb +++ b/spec/examples/middleware_spec.rb @@ -21,12 +21,12 @@ it 'should log get requests to logger' do FakeWeb.register_uri(:get, "http://localhost:9200/default/post/1", :body => '{}') client.get('default', 'post', '1') - io.string.should == "ElasticSearch GET (3ms) /default/post/1\n" + io.string.should include "ElasticSearch GET (3ms) /default/post/1\n" end it 'should log body of POST requests to logger' do stub_es_create('default', 'post') client.create('default', 'post', nil, {}) - io.string.should == "ElasticSearch POST (3ms) /default/post {}\n" + io.string.should include "ElasticSearch POST (3ms) /default/post {}\n" end end diff --git a/spec/examples/observing_spec.rb b/spec/examples/observing_spec.rb deleted file mode 100644 index f3e7b17..0000000 --- a/spec/examples/observing_spec.rb +++ /dev/null @@ -1,162 +0,0 @@ -require File.expand_path('../spec_helper', __FILE__) - -describe Elastictastic::Observing do - include Elastictastic::TestHelpers - - let(:id) { '123' } - let(:post) { Post.new } - let(:persisted_post) do - Post.new.tap do |post| - post.elasticsearch_hit = { '_id' => id, '_index' => 'default' } - end - end - - before do - stub_es_create('default', 'post') - stub_es_update('default', 'post', id) - stub_es_destroy('default', 'post', id) - Elastictastic.config.observers = [:post_observer] - Elastictastic.config.instantiate_observers - end - - context 'on create' do - let(:observers) { post.observers_that_ran } - - before do - post.save - end - - it 'should run before_create' do - observers.should include(:before_create) - end - - it 'should run after_create' do - observers.should include(:after_create) - end - - it 'should run before_save' do - observers.should include(:before_save) - end - - it 'should run after_save' do - observers.should include(:after_save) - end - - it 'should not run before_update' do - observers.should_not include(:before_update) - end - - it 'should not run after_update' do - observers.should_not include(:after_update) - end - - it 'should not run before_destroy' do - observers.should_not include(:before_destroy) - end - - it 'should not run after_destroy' do - observers.should_not include(:after_destroy) - end - end - - context 'on create with observing disabled' do - it 'should not run any observers' do - post.save(:observers => false) - post.observers_that_ran.should be_empty - end - end - - context 'on update' do - let(:observers) { persisted_post.observers_that_ran } - - before do - persisted_post.save - end - - it 'should not run before_create' do - observers.should_not include(:before_create) - end - - it 'should not run after_create' do - observers.should_not include(:after_create) - end - - it 'should run before_update' do - observers.should include(:before_update) - end - - it 'should run after_update' do - observers.should include(:after_update) - end - - it 'should run before_save' do - observers.should include(:before_save) - end - - it 'should run after_save' do - observers.should include(:after_save) - end - - it 'should not run before_destroy' do - observers.should_not include(:before_destroy) - end - - it 'should not run after_destroy' do - observers.should_not include(:after_destroy) - end - end - - context 'on update with observers disabled' do - it 'should not run any observers' do - persisted_post.save(:observers => false) - persisted_post.observers_that_ran.should be_empty - end - end - - context 'on destroy' do - let(:observers) { persisted_post.observers_that_ran } - - before do - persisted_post.destroy - end - - it 'should not run before_create' do - observers.should_not include(:before_create) - end - - it 'should not run after_create' do - observers.should_not include(:after_create) - end - - it 'should not run before_update' do - observers.should_not include(:before_update) - end - - it 'should not run after_update' do - observers.should_not include(:after_update) - end - - it 'should not run before_save' do - observers.should_not include(:before_save) - end - - it 'should not run after_save' do - observers.should_not include(:after_save) - end - - it 'should run before_destroy' do - observers.should include(:before_destroy) - end - - it 'should run after_destroy' do - observers.should include(:after_destroy) - end - end - - context 'on destroy with observers disabled' do - it 'should not run any observers' do - persisted_post.destroy(:observers => false) - persisted_post.observers_that_ran.should be_empty - end - end -end diff --git a/spec/examples/spec_helper.rb b/spec/examples/spec_helper.rb index 0550b0c..a54dab3 100644 --- a/spec/examples/spec_helper.rb +++ b/spec/examples/spec_helper.rb @@ -12,4 +12,6 @@ FakeWeb.requests.clear FakeWeb.clean_registry end + + config.filter_run_excluding compatibility: :active_model_4 end diff --git a/spec/models/post.rb b/spec/models/post.rb index 3ce72aa..cdbe38c 100644 --- a/spec/models/post.rb +++ b/spec/models/post.rb @@ -17,14 +17,12 @@ class Post belongs_to :blog - attr_accessible :title + if defined? attr_accessible + attr_accessible :title + end validates :title, :exclusion => %w(INVALID) - def observers_that_ran - @observers_that_ran ||= Set[] - end - def self.search_keywords(keywords) query do query_string do diff --git a/spec/models/post_observer.rb b/spec/models/post_observer.rb deleted file mode 100644 index 1118463..0000000 --- a/spec/models/post_observer.rb +++ /dev/null @@ -1,11 +0,0 @@ -class PostObserver < Elastictastic::Observer - %w(create update save destroy).each do |lifecycle| - %w(before after).each do |phase| - module_eval <<-RUBY, __FILE__, __LINE__ + 1 - def #{phase}_#{lifecycle}(post) - post.observers_that_ran << :#{phase}_#{lifecycle} - end - RUBY - end - end -end