Skip to content

Commit e53b203

Browse files
committed
feat: add timestamp_columns config option
1 parent 910971b commit e53b203

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
@@ -68,6 +68,9 @@ def from(options = {}, state = {})
6868
# ModelAnnotator
6969
hide_limit_column_types: "",
7070

71+
# ModelAnnotator
72+
timestamp_columns: ModelAnnotator::ModelWrapper::DEFAULT_TIMESTAMP_COLUMNS,
73+
7174
ignore_columns: nil, # ModelAnnotator
7275
ignore_routes: nil, # RouteAnnotator
7376
ignore_unknown_models: false, # ModelAnnotator
@@ -129,6 +132,7 @@ def from(options = {}, state = {})
129132
:debug,
130133
:hide_default_column_types,
131134
:hide_limit_column_types,
135+
:timestamp_columns,
132136
:ignore_columns,
133137
:ignore_routes,
134138
: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
@@ -181,6 +181,60 @@
181181
it 'works with option "classified_sort"' do
182182
is_expected.to eq expected_result
183183
end
184+
185+
context 'when default timestamps are included' do
186+
let(:columns) do
187+
[
188+
mock_column("parent_id", :integer),
189+
mock_column("updated_at", :datetime),
190+
mock_column("name", :string),
191+
mock_column("id", :integer),
192+
mock_column("deleted_at", :datetime),
193+
mock_column("created_at", :datetime)
194+
]
195+
end
196+
197+
it 'sorts default timestamps second last before associations' do
198+
is_expected.to eq <<~EOS
199+
# == Schema Information
200+
#
201+
# Table name: users
202+
#
203+
# id :integer not null, primary key
204+
# deleted_at :datetime not null
205+
# name :string not null
206+
# created_at :datetime not null
207+
# updated_at :datetime not null
208+
# parent_id :integer not null
209+
#
210+
EOS
211+
end
212+
213+
context 'when timestamps_column option is set' do
214+
let(:options) do
215+
AnnotateRb::Options.new(
216+
classified_sort: true,
217+
timestamp_columns: %w[created_at updated_at deleted_at]
218+
)
219+
end
220+
221+
it 'sorts configured timestamps into config order' do
222+
is_expected.to eq <<~EOS
223+
# == Schema Information
224+
#
225+
# Table name: users
226+
#
227+
# id :integer not null, primary key
228+
# name :string not null
229+
# created_at :datetime not null
230+
# updated_at :datetime not null
231+
# deleted_at :datetime not null
232+
# parent_id :integer not null
233+
#
234+
EOS
235+
end
236+
end
237+
end
184238
end
185239

186240
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)