Skip to content

Commit

Permalink
Fix translation foreign key exclusion bug (#181)
Browse files Browse the repository at this point in the history
I ran into a bug with this logic when using annotaterb + globalize:

For compound names for models such as "ABCustomer", it resulted in
"abcustomer_id" being added to the exclusion list, where the actual
foreign key name was "ab_customer_id".

Therefore it was not excluded. And therefore all the annotations for
ABCustomer ended up including "ab_customer_id" even though that wasn't
an actual column.

This PR includes changes to the unit tests to not break them. However it
didn't seem like this bug something I could actually reproduce with the
unit tests. I couldn't get the integration test suite working and
couldn't spend more time on it right now.

---------

Co-authored-by: Andrew W. Lee <[email protected]>
  • Loading branch information
galori and drwl authored Feb 4, 2025
1 parent 3d0e3f7 commit 44bfc09
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 10 deletions.
11 changes: 1 addition & 10 deletions lib/annotate_rb/model_annotator/model_wrapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -162,21 +162,12 @@ def classified_sort(cols)
# These are the columns that the globalize gem needs to work but
# are not necessary for the models to be displayed as annotations.
def ignored_translation_table_columns
# Construct the foreign column name in the translations table
# eg. Model: Car, foreign column name: car_id
foreign_column_name = [
@klass.translation_class.to_s
.gsub("::Translation", "").gsub("::", "_")
.downcase,
"_id"
].join.to_sym

[
:id,
:created_at,
:updated_at,
:locale,
foreign_column_name
@klass.name.foreign_key.to_sym
]
end
end
Expand Down
1 change: 1 addition & 0 deletions spec/dummyapp/.rbenv-gemsets
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dummyapp
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@
let :klass do
mock_class(:posts, primary_key, columns, indexes, foreign_keys).tap do |mock_klass|
allow(mock_klass).to receive(:translation_class).and_return(translation_klass)
allow(mock_klass).to receive(:name).and_return(double("name", foreign_key: double("foreign_key", to_sym: :post_id)))
end
end

Expand Down

0 comments on commit 44bfc09

Please sign in to comment.