Permalink
Browse files

allow :field as valid option key, fixes #27

The Mongoid integration depends on the :field key to set additional
options for automatically generated field. When setting a default
option for :field it fails, because as_enum asserts the option keys.

Implemented fix just allows :field as valid option key for as_enum,
eventhough it is a no-op (except for mongoid).
  • Loading branch information...
1 parent dacb466 commit 5bdf9a0cb2e84aad874efe6426cb7bb8d198a08e @lwe committed Aug 22, 2012
Showing with 18 additions and 2 deletions.
  1. +3 −1 lib/simple_enum.rb
  2. +15 −1 test/mongoid_test.rb
View
@@ -154,9 +154,11 @@ module ClassMethods
# <tt>false</tt> no exception is thrown and the internal value is set to <tt>nil</tt> (default is <tt>true</tt>)
# * <tt>:dirty</tt> - Boolean value which if set to <tt>true</tt> generates <tt>..._was</tt> and <tt>..._changed?</tt>
# methods for the enum, which delegate to the internal column (default is <tt>false</tt>)
+ # * <tt>:field</tt> - Also allowed as valid key, for Mongoid integration + default options, see simple_enum#27.
+ #
def as_enum(enum_cd, values, options = {})
options = SimpleEnum.default_options.merge({ :column => "#{enum_cd}_cd" }).merge(options)
- options.assert_valid_keys(:column, :whiny, :prefix, :slim, :upcase, :dirty)
+ options.assert_valid_keys(:column, :whiny, :prefix, :slim, :upcase, :dirty, :field)
metaclass = (class << self; self; end)
View
@@ -1,7 +1,10 @@
require 'test_helper'
class MongoidTest < MiniTest::Unit::TestCase
+ @@default_options = SimpleEnum.default_options
+
def setup
+ SimpleEnum.default_options.clear.merge(@@default_options)
reload_db
end
@@ -35,4 +38,15 @@ def test_skip_field_creation_if_field_false
assert_nil klass.new.fields['gender_cd']
end
-end
+
+ def test_default_field_options
+ skip('Only available in mongoid') unless mongoid?
+ SimpleEnum.default_options[:field] = { :type => String }
+ klass = anonymous_dummy do
+ as_enum :gender, [:male, :female]
+ end
+
+ refute_nil klass.new.fields['gender_cd']
+ assert_equal String, klass.new.fields['gender_cd'].options[:type]
+ end
+end

0 comments on commit 5bdf9a0

Please sign in to comment.