Skip to content

Account for object default values when annotating models #1042

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/annotate/annotate_models.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def quote(value)
when BigDecimal then value.to_s('F')
when Array then value.map { |v| quote(v) }
else
value.inspect
value.respond_to?(:to_s) ? value.to_s.inspect : value.inspect

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This change looks good and correctly addresses the issue. However, consider adding a comment to explain why to_s.inspect is used instead of just inspect. This would help future developers understand the intent behind this specific implementation.

For example, you could add a comment like:

# Use to_s.inspect to ensure objects with custom string representations are properly quoted
        value.respond_to?(:to_s) ? value.to_s.inspect : value.inspect # Ensure objects with custom string representations are properly quoted

end
end

Expand Down
14 changes: 14 additions & 0 deletions spec/lib/annotate/annotate_models_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,20 @@ def mock_column(name, type, options = {})
is_expected.to eq(['1.2'])
end
end

context 'when the argument is an object that responds to to_s' do
let(:value) do
obj = Object.new
def obj.to_s
"custom string representation"
end
obj
end

it 'returns the result of to_s wrapped in quotes' do
is_expected.to eq('"custom string representation"')
end
end
end

describe '.parse_options' do
Expand Down
Loading