Skip to content

Commit

Permalink
fix have_db_column.with_options misspelled options
Browse files Browse the repository at this point in the history
have_db_column().with_options() should raise an ArgumentError
if receives a misspelled argument
  • Loading branch information
rodriggochaves authored and mcmire committed Dec 14, 2020
1 parent e19ebf2 commit 63b6321
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
12 changes: 11 additions & 1 deletion lib/shoulda/matchers/active_record/have_db_column_matcher.rb
Expand Up @@ -84,6 +84,8 @@ def have_db_column(column)

# @private
class HaveDbColumnMatcher
OPTIONS = %i(precision limit default null scale primary)

def initialize(column)
@column = column
@options = {}
Expand All @@ -95,7 +97,8 @@ def of_type(column_type)
end

def with_options(opts = {})
%w(precision limit default null scale primary).each do |attribute|
validate_options(opts)
OPTIONS.each do |attribute|
if opts.key?(attribute.to_sym)
@options[attribute.to_sym] = opts[attribute.to_sym]
end
Expand Down Expand Up @@ -141,6 +144,13 @@ def description

protected

def validate_options(opts)
invalid_options = opts.keys.map(&:to_sym) - OPTIONS
if invalid_options.any?
raise ArgumentError, "Unknown option(s): #{invalid_options.map(&:inspect).join(", ")}"

This comment has been minimized.

Copy link
@lifeiscontent

lifeiscontent Feb 11, 2021

@rodriggochaves this broke a test I have

it { is_expected.to have_db_column(:author_id).with_options(null: false, foreign_key: { to_table: :users }) }

This comment has been minimized.

Copy link
@mcmire

mcmire Feb 11, 2021

Collaborator

Hi @lifeiscontent, can you make a new issue for this? This PR is already merged.

end
end

def column_exists?
if model_class.column_names.include?(@column.to_s)
true
Expand Down
Expand Up @@ -98,6 +98,14 @@
end
end

context 'with invalid argument option' do
it 'raises an error with the unknown options' do
expect {
have_db_column(:salary).with_options(preccision: 5, primaryy: true)
}.to raise_error("Unknown option(s): :preccision, :primaryy")
end
end

def model(options = {})
define_model(:employee, options).new
end
Expand Down

0 comments on commit 63b6321

Please sign in to comment.