Skip to content

Commit a8b1214

Browse files
committed
feat: add timestamp_columns config option
1 parent efc56fb commit a8b1214

File tree

4 files changed

+75
-5
lines changed

4 files changed

+75
-5
lines changed

lib/annotate_rb/model_annotator/model_wrapper.rb

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ class ModelWrapper
66
# Should be the wrapper for an ActiveRecord model that serves as the source of truth of the model
77
# of the model that we're annotating
88

9+
DEFAULT_TIMESTAMP_COLUMNS = %w[created_at updated_at]
10+
911
def initialize(klass, options)
1012
@klass = klass
1113
@options = options
@@ -143,18 +145,24 @@ def classified_sort(cols)
143145
associations = []
144146
id = nil
145147

148+
# specs don't load defaults, so ensure we have defaults here
149+
timestamp_columns = @options[:timestamp_columns] || DEFAULT_TIMESTAMP_COLUMNS
150+
146151
cols.each do |c|
147152
if c.name.eql?("id")
148153
id = c
149-
elsif c.name.eql?("created_at") || c.name.eql?("updated_at")
154+
elsif timestamp_columns.include?(c.name)
150155
timestamps << c
151156
elsif c.name[-3, 3].eql?("_id")
152157
associations << c
153158
else
154159
rest_cols << c
155160
end
156161
end
157-
[rest_cols, timestamps, associations].each { |a| a.sort_by!(&:name) }
162+
163+
timestamp_order = timestamp_columns.each_with_index.to_h
164+
timestamps.sort_by! { |col| timestamp_order[col.name] }
165+
[rest_cols, associations].each { |a| a.sort_by!(&:name) }
158166

159167
([id] << rest_cols << timestamps << associations).flatten.compact
160168
end

lib/annotate_rb/options.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ def from(options = {}, state = {})
6969
# ModelAnnotator
7070
hide_limit_column_types: "",
7171

72+
# ModelAnnotator
73+
timestamp_columns: ModelAnnotator::ModelWrapper::DEFAULT_TIMESTAMP_COLUMNS,
74+
7275
ignore_columns: nil, # ModelAnnotator
7376
ignore_routes: nil, # RouteAnnotator
7477
ignore_unknown_models: false, # ModelAnnotator
@@ -130,6 +133,7 @@ def from(options = {}, state = {})
130133
:debug,
131134
:hide_default_column_types,
132135
:hide_limit_column_types,
136+
:timestamp_columns,
133137
:ignore_columns,
134138
:ignore_routes,
135139
:ignore_unknown_models,

spec/lib/annotate_rb/model_annotator/annotation/annotation_builder_spec.rb

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,60 @@
182182
it 'works with option "classified_sort"' do
183183
is_expected.to eq expected_result
184184
end
185+
186+
context "when default timestamps are included" do
187+
let(:columns) do
188+
[
189+
mock_column("parent_id", :integer),
190+
mock_column("updated_at", :datetime),
191+
mock_column("name", :string),
192+
mock_column("id", :integer),
193+
mock_column("deleted_at", :datetime),
194+
mock_column("created_at", :datetime)
195+
]
196+
end
197+
198+
it "sorts default timestamps second last before associations" do
199+
is_expected.to eq <<~EOS
200+
# == Schema Information
201+
#
202+
# Table name: users
203+
#
204+
# id :integer not null, primary key
205+
# deleted_at :datetime not null
206+
# name :string not null
207+
# created_at :datetime not null
208+
# updated_at :datetime not null
209+
# parent_id :integer not null
210+
#
211+
EOS
212+
end
213+
214+
context "when timestamps_column option is set" do
215+
let(:options) do
216+
AnnotateRb::Options.new(
217+
classified_sort: true,
218+
timestamp_columns: %w[created_at updated_at deleted_at]
219+
)
220+
end
221+
222+
it "sorts configured timestamps into config order" do
223+
is_expected.to eq <<~EOS
224+
# == Schema Information
225+
#
226+
# Table name: users
227+
#
228+
# id :integer not null, primary key
229+
# name :string not null
230+
# created_at :datetime not null
231+
# updated_at :datetime not null
232+
# deleted_at :datetime not null
233+
# parent_id :integer not null
234+
#
235+
EOS
236+
end
237+
end
238+
end
185239
end
186240

187241
context "when geometry columns are included" do

spec/lib/annotate_rb/options_spec.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,11 @@
5656
describe ".load_defaults" do
5757
subject { described_class.new(options, state).load_defaults }
5858

59+
let(:options) { {} }
5960
let(:state) { {} }
6061

6162
context 'when default value of "show_complete_foreign_keys" is not set' do
6263
let(:key) { :show_complete_foreign_keys }
63-
let(:options) { {} }
6464

6565
it "returns false" do
6666
expect(subject[key]).to eq(false)
@@ -76,10 +76,14 @@
7676
end
7777
end
7878

79+
describe "default timestamp_columns" do
80+
it "sets defaults matching Rails' convention" do
81+
expect(subject[:timestamp_columns]).to match_array(%w[created_at updated_at])
82+
end
83+
end
84+
7985
describe "comment options" do
8086
context "when using defaults" do
81-
let(:options) { {} }
82-
8387
it "uses the defaults" do
8488
expect(subject[:with_comment]).to eq(true)
8589
expect(subject[:with_column_comments]).to eq(true)

0 commit comments

Comments
 (0)