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

Upgrade mongoid 6 and rails 5 #132

Open
wants to merge 17 commits into
base: legacy-1.x
Choose a base branch
from
7 changes: 3 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
language: ruby
services: mongodb
rvm:
- 1.9.3
- 2.0.0
- 2.1.1
- jruby-19mode
- 2.2.2
- 2.2.7
- jruby-9.1.12.0
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ source "http://rubygems.org"
gemspec

# Fix at 1.9.x, because 1.10 errors on jruby
gem 'mongo', '~> 1.9.0', :platform => :jruby
gem 'mongo', '~> 2.3', :platform => :jruby

# some development deps
gem 'activerecord-jdbcsqlite3-adapter', :platform => :jruby
Expand Down
2 changes: 1 addition & 1 deletion lib/simple_enum/enum_hash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class EnumHash < ::ActiveSupport::OrderedHash
def self.symbolize(sym)
return sym.to_enum_sym if sym.respond_to?(:to_enum_sym)
return sym.to_sym if sym.respond_to?(:to_sym)
return sym.name.to_s.parameterize('_').to_sym if sym.respond_to?(:name)
return sym.name.to_s.parameterize(separator: '_').to_sym if sym.respond_to?(:name)
sym.to_param.to_sym if sym.present? && sym.respond_to?(:to_param)
sym unless sym.blank?
end
Expand Down
42 changes: 25 additions & 17 deletions lib/simple_enum/mongoid.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'simple_enum'
require 'mongoid'

module SimpleEnum

Expand All @@ -23,29 +24,36 @@ module SimpleEnum
# as_enum :gender, [:female, :male], :field => { :type => Integer }
#
module Mongoid
extend ActiveSupport::Concern

included do
# create class level methods
class_attribute :simple_enum_definitions, :instance_writer => false, :instance_reader => false
def self.included(base)
base.extend SimpleEnum::ClassMethods
base.class_eval do
class_attribute :simple_enum_definitions, instance_writer: false,
instance_reader: false
end
base.prepend AsEnumRedefinition
end

module ClassMethods
include SimpleEnum::ClassMethods
module AsEnumRedefinition
module ClassMethods
# Wrap method chain to create mongoid field and additional
# column options
def as_enum(enum_cd, values, options = {})
options = SimpleEnum.default_options.merge(column: "#{enum_cd}_cd").deep_merge(options)

# Wrap method chain to create mongoid field and additional
# column options
def as_enum_with_mongoid(enum_cd, values, options = {})
options = SimpleEnum.default_options.merge({ :column => "#{enum_cd}_cd" }).deep_merge(options)
# forward custom field options
field_options = options.delete(:field)
field(options[:column], field_options.is_a?(Hash) ? field_options : {}) unless field_options === false

# forward custom field options
field_options = options.delete(:field)
field(options[:column], field_options.is_a?(Hash) ? field_options : {}) unless field_options === false
# call original as_enum method
super(enum_cd, values, options)
end
end

# call original as_enum method
as_enum_without_mongoid(enum_cd, values, options)
def self.prepended(base)
class << base
prepend ClassMethods
end
end
alias_method_chain :as_enum, :mongoid
end
end
end
7 changes: 3 additions & 4 deletions lib/simple_enum/validation.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'active_model'

module ActiveModel
module Validations
class AsEnumValidator < ActiveModel::Validator
Expand All @@ -9,11 +11,8 @@ def initialize(options)
super
end

def setup(klass)
@klass = klass
end

def validate(record)
@klass ||= record.class
attributes.each do |attribute|
enum_def = @klass.enum_definitions[attribute]
raw_value = record.send(enum_def[:column])
Expand Down
11 changes: 6 additions & 5 deletions simple_enum.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Gem::Specification.new do |s|
s.summary = "Simple enum-like field support for models."
s.description = "Provides enum-like fields for ActiveRecord, ActiveModel and Mongoid models."

s.required_ruby_version = ">= 1.8.7"
s.required_ruby_version = ">= 2.2.2"
s.required_rubygems_version = ">= 1.3.6"

s.authors = ["Lukas Westermann"]
Expand All @@ -21,10 +21,11 @@ Gem::Specification.new do |s|

s.license = 'MIT'

s.add_dependency "activesupport", '>= 3.0.0'
s.add_dependency "activesupport", '>= 6.0.1'

s.add_development_dependency 'rake', '>= 0.9.2'
s.add_development_dependency 'minitest', '~> 2.0'
s.add_development_dependency 'activerecord', '>= 3.0.0'
s.add_development_dependency 'mongoid', '~> 2.0'
s.add_development_dependency 'minitest'
s.add_development_dependency 'activerecord', '>= 6.0.1'
s.add_development_dependency 'mongoid', '>= 6'
s.add_development_dependency 'sqlite3'
end
2 changes: 1 addition & 1 deletion test/array_conversions_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'test_helper'

class ArrayConversionsTest < MiniTest::Unit::TestCase
class ArrayConversionsTest < Minitest::Test
def setup
reload_db :genders => true
end
Expand Down
4 changes: 1 addition & 3 deletions test/class_methods_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'test_helper'

class ClassMethodsTest < MiniTest::Unit::TestCase
class ClassMethodsTest < Minitest::Test
def setup
reload_db
end
Expand Down Expand Up @@ -35,8 +35,6 @@ def test_inverted_hash_returns_synonym_by_code
end

def test_generation_of_value_shortcuts_on_class
g = Dummy.new

assert_equal 0, Dummy.male
assert_equal 1, Dummy.female
assert_equal 'alpha', Dummy.alpha
Expand Down
2 changes: 1 addition & 1 deletion test/enum_hash_test.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require 'test_helper'
require 'simple_enum/enum_hash'

class EnumHashTest < MiniTest::Unit::TestCase
class EnumHashTest < Minitest::Test

def test_create_new_enumhash_instance_from_array_of_symbols
genders = SimpleEnum::EnumHash.new [:male, :female]
Expand Down
2 changes: 1 addition & 1 deletion test/finders_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'test_helper'

class FindersTest < MiniTest::Unit::TestCase
class FindersTest < Minitest::Test
def setup
reload_db
end
Expand Down
4 changes: 2 additions & 2 deletions test/mongoid_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'test_helper'

class MongoidTest < MiniTest::Unit::TestCase
class MongoidTest < Minitest::Test
def setup
@default_options = SimpleEnum.default_options
reload_db
Expand All @@ -27,7 +27,7 @@ def test_passing_custom_field_options

gender_field = klass.new.fields['gender_cd']
refute_nil gender_field
assert_equal 1, gender_field.default
assert_equal 1, gender_field.options[:default]
assert_equal klass.fields['verify'].class, gender_field.class
assert_equal :female, klass.new.gender
end
Expand Down
15 changes: 8 additions & 7 deletions test/object_backed_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'test_helper'

class ObjectBackedTest < MiniTest::Unit::TestCase
class ObjectBackedTest < Minitest::Test
def setup
reload_db :genders => true
end
Expand All @@ -18,9 +18,10 @@ def initialize(name)
with_object = anonymous_dummy do
as_enum :gender, { simple_obj.new('Male') => 0, simple_obj.new('Female') => 1 }
end

d = with_object.where(:name => 'Anna').first


d = with_object.where(:name => 'Anna')
d = d.where('_type' => nil) if mongoid?
d = d.first
assert_same simple_obj, d.gender.class
assert_equal 'Female', d.gender.name
assert_same true, d.female?
Expand All @@ -38,9 +39,9 @@ def test_db_backed_objects
with_db_obj = anonymous_dummy do
as_enum :gender, genders
end
d = with_db_obj.where(:name => 'Bella').first

with_db_obj.where(:name => 'Bella').first

assert_respond_to with_db_obj, :female
assert_respond_to with_db_obj, :male
assert_equal 0, with_db_obj.male
Expand Down
29 changes: 9 additions & 20 deletions test/orm/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,11 @@ def orm_version
ActiveRecord::VERSION::STRING
end

def ar32?
ActiveRecord::VERSION::MAJOR >= 3 && ActiveRecord::VERSION::MINOR >= 2
end

def setup_db
# create database connection (in memory db!)
ActiveRecord::Base.establish_connection({
:adapter => RUBY_PLATFORM =~ /java/ ? 'jdbcsqlite3' : 'sqlite3',
:database => ':memory:'})

# Fix visitor, for JRuby
if RUBY_PLATFORM =~ /java/ && ar32?
ActiveRecord::ConnectionAdapters::SQLiteAdapter.send(:define_method, :visitor) do
@visitor ||= Arel::Visitors::SQLite.new(self)
end
end
end

# Reload database
Expand Down Expand Up @@ -52,15 +41,15 @@ def reload_db(options = {})
# Models
def anonymous_dummy(&block)
Class.new(ActiveRecord::Base) do
ar32? ? self.table_name = 'dummies' : set_table_name('dummies')
instance_eval &block
self.table_name = 'dummies'
instance_eval(&block)
end
end

def extend_computer(current_i18n_name = "Computer", &block)
Class.new(Computer) do
ar32? ? self.table_name = 'computers' : set_table_name('computers')
instance_eval &block
self.table_name = 'computers'
instance_eval(&block)
instance_eval <<-RUBY
def self.model_name; MockName.mock!(#{current_i18n_name.inspect}) end
RUBY
Expand All @@ -69,8 +58,8 @@ def self.model_name; MockName.mock!(#{current_i18n_name.inspect}) end

def extend_dummy(current_i18n_name = "Dummy", &block)
Class.new(Dummy) do
ar32? ? self.table_name = 'dummies' : set_table_name('dummies')
instance_eval &block
self.table_name = 'dummies'
instance_eval(&block)
instance_eval <<-RUBY
def self.model_name; MockName.mock!(#{current_i18n_name.inspect}) end
RUBY
Expand All @@ -83,8 +72,8 @@ def named_dummy(class_name, &block)
rescue NameError
klass = Object.const_set(class_name, Class.new(ActiveRecord::Base))
klass.module_eval do
ar32? ? self.table_name = 'dummies' : set_table_name('dummies')
instance_eval &block
self.table_name = 'dummies'
instance_eval(&block)
end
klass
end
Expand All @@ -109,6 +98,6 @@ class Computer < ActiveRecord::Base

# Used to test STI stuff
class SpecificDummy < Dummy
ar32? ? self.table_name = 'dummies' : set_table_name('dummies')
self.table_name = 'dummies'
end

24 changes: 13 additions & 11 deletions test/orm/mongoid.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ def orm_version
def setup_db
# create database connection
Mongoid.configure do |config|
config.master = Mongo::Connection.new('localhost').db("simple-enum-test-suite")
config.load_configuration(clients: { default: { database: 'simple_enum_test_suite',
hosts: ['localhost:27017'] } })
config.use_utc = true
config.include_root_in_json = true
end
Expand All @@ -19,8 +20,8 @@ def setup_db
def reload_db(options = {})

# clear collections except system
Mongoid.master.collections.select do |collection|
collection.name !~ /system/
Mongoid.default_client.collections.reject do |collection|
collection.name =~ /system/
end.each(&:drop)

fill_db(options)
Expand All @@ -31,15 +32,14 @@ def anonymous_dummy(&block)
Class.new do
include Mongoid::Document
include SimpleEnum::Mongoid
self.collection_name = 'dummies'
instance_eval &block
store_in collection: 'dummies'
instance_eval(&block)
end
end

def extend_computer(current_i18n_name = "Computer", &block)
Class.new(Computer) do
self.collection_name = 'computers'
instance_eval &block
instance_eval(&block)
instance_eval <<-RUBY
def self.model_name; MockName.mock!(#{current_i18n_name.inspect}) end
RUBY
Expand All @@ -48,8 +48,7 @@ def self.model_name; MockName.mock!(#{current_i18n_name.inspect}) end

def extend_dummy(current_i18n_name = "Dummy", &block)
Class.new(Dummy) do
self.collection_name = 'dummies'
instance_eval &block
instance_eval(&block)
instance_eval <<-RUBY
def self.model_name; MockName.mock!(#{current_i18n_name.inspect}) end
RUBY
Expand All @@ -65,8 +64,8 @@ def named_dummy(class_name, &block)
include Mongoid::Document
include SimpleEnum::Mongoid

self.collection_name = 'dummies'
instance_eval &block
store_in collection: 'dummies'
instance_eval(&block)
end

klass
Expand All @@ -77,13 +76,15 @@ def named_dummy(class_name, &block)
class Dummy
include Mongoid::Document
include SimpleEnum::Mongoid
store_in collection: 'dummies'

as_enum :gender, [:male, :female]
as_enum :word, { :alpha => 'alpha', :beta => 'beta', :gamma => 'gamma'}
as_enum :didum, [ :foo, :bar, :foobar ], :column => 'other'
as_enum :role, [:admin, :member, :anon], :strings => true
as_enum :numeric, [:"100", :"3.14"], :strings => true
as_enum :nilish, [:nil], :strings => true
field :name, :type => String

before_save :check_typed

Expand All @@ -103,6 +104,7 @@ class Gender
class Computer
include Mongoid::Document
include SimpleEnum::Mongoid
store_in collection: 'computers'

field :name, :type => String

Expand Down
2 changes: 1 addition & 1 deletion test/poro_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'test_helper'

class POROTest < MiniTest::Unit::TestCase
class POROTest < Minitest::Test
class MyPORO
include SimpleEnum

Expand Down
Loading