@@ -79,7 +79,8 @@ def mock_column(name, type, options = {})
79
79
limit : nil ,
80
80
null : false ,
81
81
default : nil ,
82
- sql_type : type
82
+ sql_type : type ,
83
+ comment : nil
83
84
}
84
85
85
86
stubs = default_options . dup
@@ -3048,6 +3049,58 @@ class User < ActiveRecord::Base
3048
3049
end
3049
3050
end
3050
3051
3052
+ describe 'if a table has a commented column' do
3053
+ it 'should be added the column with comment' do
3054
+ annotation_options = { with_comment : true , position : :before }
3055
+ klass = mock_class ( :'users' ,
3056
+ :id ,
3057
+ [
3058
+ mock_column ( :id , :integer ) ,
3059
+ mock_column ( :name , :string , comment : 'commented column' )
3060
+ ] )
3061
+ schema_info = AnnotateModels . get_schema_info ( klass , '== Schema Info' , annotation_options )
3062
+ AnnotateModels . annotate_one_file ( @model_file_name , schema_info , :position_in_class , annotation_options )
3063
+ expect ( File . read ( @model_file_name ) ) . to include ( 'commented column' )
3064
+ end
3065
+
3066
+ context 'and the model already has annotation and the commented column is updated to non-null' do
3067
+ before do
3068
+ @annotation_options = { with_comment : true , position : :before }
3069
+ klass = mock_class ( :'users' ,
3070
+ :id ,
3071
+ [
3072
+ mock_column ( :id , :integer ) ,
3073
+ mock_column ( :name , :string , comment : 'commented column' , null : true )
3074
+ ] )
3075
+ schema_info = AnnotateModels . get_schema_info ( klass , '== Schema Info' , @annotation_options )
3076
+ AnnotateModels . annotate_one_file ( @model_file_name , schema_info , :position_in_class , @annotation_options )
3077
+ end
3078
+
3079
+ it 'should be added non-null option in the commented column' do
3080
+ klass = mock_class ( :'users' ,
3081
+ :id ,
3082
+ [
3083
+ mock_column ( :id , :integer ) ,
3084
+ mock_column ( :name , :string , comment : 'commented column' , null : false )
3085
+ ] )
3086
+ schema_info = AnnotateModels . get_schema_info ( klass , '== Schema Info' , @annotation_options )
3087
+ expect do
3088
+ AnnotateModels . annotate_one_file (
3089
+ @model_file_name ,
3090
+ schema_info ,
3091
+ :position_in_class ,
3092
+ @annotation_options
3093
+ )
3094
+ end
3095
+ . to (
3096
+ change { File . read ( @model_file_name ) }
3097
+ . from ( include ( "name(commented column) :string\n " ) )
3098
+ . to ( include ( "name(commented column) :string not null\n " ) )
3099
+ )
3100
+ end
3101
+ end
3102
+ end
3103
+
3051
3104
describe "if a file can't be annotated" do
3052
3105
before do
3053
3106
allow ( AnnotateModels ) . to receive ( :get_loaded_model_by_path ) . with ( 'user' ) . and_return ( nil )
0 commit comments