Skip to content

Commit

Permalink
Add compatiblity with Rails5
Browse files Browse the repository at this point in the history
Fixes #191
  • Loading branch information
nviennot committed Feb 16, 2016
1 parent b32cd91 commit 0b2590d
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 18 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Gemfile.lock
gemfiles/*.lock
*.swp
.rbenv*
todo
Expand Down
7 changes: 5 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
language: ruby
rvm:
- 2.2
- 2.3.0
- jruby-9
- rbx-2
before_install:
Expand All @@ -18,7 +18,10 @@ notifications:
on_failure: change
bundler_args: --without development
script: bundle exec rspec spec
gemfile:
- gemfiles/rails4.gemfile
- gemfiles/rails5.gemfile
matrix:
include:
- rvm: 2.2
- rvm: 2.3.0
env: EM=1
11 changes: 0 additions & 11 deletions Gemfile

This file was deleted.

1 change: 1 addition & 0 deletions Gemfile
1 change: 1 addition & 0 deletions gemfiles/nobrainer.gemspec
14 changes: 14 additions & 0 deletions gemfiles/rails4.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
source 'https://rubygems.org'
gemspec

gem 'rspec', '~> 3.0'
gem 'timecop'
gem 'eventmachine' if ENV['EM']

gem "activesupport", "< 5.0.0"
gem "activemodel", "< 5.0.0"

group :development do
gem 'pry'
gem 'simplecov', :require => false
end
14 changes: 14 additions & 0 deletions gemfiles/rails5.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
source 'https://rubygems.org'
gemspec

gem 'rspec', '~> 3.0'
gem 'timecop'
gem 'eventmachine' if ENV['EM']

gem "activesupport", ">= 5.0.0.beta2"
gem "activemodel", ">= 5.0.0.beta2"

group :development do
gem 'pry'
gem 'simplecov', :require => false
end
11 changes: 9 additions & 2 deletions lib/no_brainer/document/callbacks.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
module NoBrainer::Document::Callbacks
extend ActiveSupport::Concern

def self.define_callbacks_options(options={})
if ActiveSupport::Callbacks.respond_to?(:halt_and_display_warning_on_return_false)
ActiveSupport::Callbacks.halt_and_display_warning_on_return_false = false
end
NoBrainer.rails5? ? options : options.merge(:terminator => proc { false })
end

included do
extend ActiveModel::Callbacks

define_model_callbacks :initialize, :create, :update, :save, :destroy, :terminator => proc { false }
define_model_callbacks :find, :only => [:after], :terminator => proc { false }
define_model_callbacks :initialize, :create, :update, :save, :destroy, NoBrainer::Document::Callbacks.define_callbacks_options
define_model_callbacks :find, NoBrainer::Document::Callbacks.define_callbacks_options(:only => [:after])
end

def initialize(*args, &block)
Expand Down
5 changes: 2 additions & 3 deletions lib/no_brainer/document/validation/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ module NoBrainer::Document::Validation::Core
include ActiveModel::Validations::Callbacks

included do
# We don't want before_validation returning false to halt the chain.
define_callbacks :validation, :skip_after_callbacks_if_terminated => true,
:scope => [:kind, :name], :terminator => proc { false }
define_callbacks :validation, NoBrainer::Document::Callbacks.define_callbacks_options(
:skip_after_callbacks_if_terminated => true, :scope => [:kind, :name])
end

def valid?(context=nil, options={})
Expand Down
4 changes: 4 additions & 0 deletions lib/nobrainer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ def jruby?
RUBY_PLATFORM == 'java'
end

def rails5?
Gem.loaded_specs['activesupport'].version >= Gem::Version.new('5.0.0.beta')
end

def eager_load!
# XXX This forces all the NoBrainer code to be loaded in memory.
# Not to be confused with eager_load() that operates on documents.
Expand Down

0 comments on commit 0b2590d

Please sign in to comment.