Skip to content

Commit

Permalink
Merge pull request #1270 from neo4jrb/fix-enum-default-value
Browse files Browse the repository at this point in the history
Accepting `nil` as enum value
  • Loading branch information
ProGM authored Aug 18, 2016
2 parents fd96dfa + 818ba3b commit 2891b21
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 13 deletions.
16 changes: 11 additions & 5 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ source 'http://rubygems.org'

gemspec

if ENV['CI']
gem 'neo4j-core', github: 'neo4jrb/neo4j-core', branch: 'master'
gem 'neo4j-rake_tasks', github: 'neo4jrb/neo4j-rake_tasks', branch: 'master'
end
# if ENV['CI']
# gem 'neo4j-core', github: 'neo4jrb/neo4j-core', branch: 'master'
# gem 'neo4j-rake_tasks', github: 'neo4jrb/neo4j-rake_tasks', branch: 'master'
# end

# gem 'active_attr', github: 'neo4jrb/active_attr', branch: 'performance'
# gem 'active_attr', path: '../active_attr'
Expand All @@ -14,13 +14,19 @@ gem 'listen', '< 3.1'

group 'test' do
gem 'coveralls', require: false
if RUBY_VERSION.to_f < 2.0
gem 'tins', '< 1.7'
gem 'overcommit', '< 0.35.0'
else
gem 'overcommit'
gem 'activesupport', '>= 4.2'
end
gem 'codecov', require: false
gem 'simplecov', require: false
gem 'simplecov-html', require: false
gem 'rspec', '~> 3.4'
gem 'its'
gem 'test-unit'
gem 'overcommit'
gem 'colored'
gem 'dotenv'
gem 'timecop'
Expand Down
8 changes: 4 additions & 4 deletions lib/neo4j/shared/enum.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def normalize_key_list(enum_keys)
end
end

VALID_OPTIONS_FOR_ENUMS = [:_index, :_prefix, :_suffix]
VALID_OPTIONS_FOR_ENUMS = [:_index, :_prefix, :_suffix, :_default]
DEFAULT_OPTIONS_FOR_ENUMS = {
_index: true
}
Expand All @@ -88,12 +88,12 @@ def split_options_and_parameters(parameters)
def define_property(property_name, enum_keys, options)
property_options = build_property_options(enum_keys, options)
property property_name, property_options
serialize property_name, Neo4j::Shared::TypeConverters::EnumConverter.new(enum_keys)
serialize property_name, Neo4j::Shared::TypeConverters::EnumConverter.new(enum_keys, property_options)
end

def build_property_options(enum_keys, _options = {})
def build_property_options(_enum_keys, options = {})
{
default: enum_keys.keys.first
default: options[:_default]
}
end

Expand Down
3 changes: 2 additions & 1 deletion lib/neo4j/shared/type_converters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -264,8 +264,9 @@ def to_ruby(value)
end

class EnumConverter
def initialize(enum_keys)
def initialize(enum_keys, options)
@enum_keys = enum_keys
@options = options
end

def converted?(value)
Expand Down
2 changes: 1 addition & 1 deletion neo4j.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ A Neo4j OGM (Object-Graph-Mapper) for use in Ruby on Rails and Rack frameworks h
s.add_dependency('orm_adapter', '~> 0.5.0')
s.add_dependency('activemodel', '~> 4')
s.add_dependency('activesupport', '~> 4')
s.add_dependency('neo4j-core', '>= 6.0.0')
s.add_dependency('neo4j-core', '>= 6.0.0', '< 7.0.0')
s.add_dependency('neo4j-community', '~> 2.0') if RUBY_PLATFORM =~ /java/
s.add_development_dependency('railties', '~> 4')
s.add_development_dependency('pry')
Expand Down
23 changes: 21 additions & 2 deletions spec/e2e/enum_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
describe Neo4j::ActiveNode do
before(:each) do
stub_active_node_class('StoredFile') do
enum type: [:unknown, :image, :video]
enum type: [:unknown, :image, :video], _default: :unknown
enum size: {big: 100, medium: 7, small: 2}, _prefix: :dimension
enum flag: [:clean, :dangerous], _suffix: true

Expand Down Expand Up @@ -31,7 +31,12 @@
end

describe 'getters and setters' do
it 'returns a type as symbol' do
it 'returns nil by default' do
file = StoredFile.new
expect(file.flag).to be_nil
end

it 'returns the default value' do
file = StoredFile.new
expect(file.type).to eq(:unknown)
end
Expand All @@ -49,6 +54,14 @@
expect(StoredFile.as(:f).pluck('f.type')).to eq([2])
expect(file.reload.type).to eq(:video)
end

it 'accepts nil as value' do
file = StoredFile.new
file.flag = nil
file.save!
expect(StoredFile.as(:f).where(id: file.id).pluck('f.flag')).to eq([nil])
expect(file.reload.flag).to eq(nil)
end
end

describe 'scopes' do
Expand Down Expand Up @@ -93,6 +106,12 @@
end

describe '? methods' do
it 'returns false when accessing to a nil value' do
file = StoredFile.new
expect(file).not_to be_clean_flag
expect(file).not_to be_dangerous_flag
end

it 'returns true when the enum is in the current state' do
file = StoredFile.new
file.type = :video
Expand Down

0 comments on commit 2891b21

Please sign in to comment.