@@ -814,4 +814,56 @@ def self.logger
814
814
end
815
815
end
816
816
end
817
+
818
+ context '#define_field_type' do
819
+ around do |example |
820
+ klass = Mongoid ::Fields ::FieldTypes
821
+ klass . instance_variable_set ( :@mapping , klass ::DEFAULT_MAPPING . dup )
822
+ example . run
823
+ klass . instance_variable_set ( :@mapping , klass ::DEFAULT_MAPPING . dup )
824
+ end
825
+
826
+ it 'can define a custom type' do
827
+ Mongoid . configure do |config |
828
+ config . define_field_type :my_type , Integer
829
+ end
830
+
831
+ expect ( Mongoid ::Fields ::FieldTypes . get ( :my_type ) ) . to eq Integer
832
+ end
833
+
834
+ it 'can override and existing type' do
835
+ Mongoid . configure do |config |
836
+ config . define_field_type :integer , String
837
+ end
838
+
839
+ expect ( Mongoid ::Fields ::FieldTypes . get ( :integer ) ) . to eq String
840
+ end
841
+ end
842
+
843
+ context '#define_field_option method' do
844
+ after do
845
+ Mongoid ::Fields . instance_variable_set ( :@options , { } )
846
+ end
847
+
848
+ it 'can define a custom field option' do
849
+ Mongoid . configure do |config |
850
+ config . define_field_option :my_required do |model , field , value |
851
+ model . validates_presence_of field . name if value
852
+ end
853
+ end
854
+
855
+ klass = Class . new do
856
+ include Mongoid ::Document
857
+ field :my_field , my_required : true
858
+
859
+ def self . model_name
860
+ OpenStruct . new ( human : 'Klass' )
861
+ end
862
+ end
863
+
864
+ instance = klass . new
865
+ expect ( instance . valid? ) . to eq false
866
+ expect ( instance . errors . full_messages ) . to eq [ "My field can't be blank" ]
867
+ end
868
+ end
817
869
end
0 commit comments