Skip to content

Commit

Permalink
Splat options explicitly
Browse files Browse the repository at this point in the history
  • Loading branch information
xjunior committed Aug 4, 2022
1 parent 1aeb598 commit 11368f7
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 15 deletions.
8 changes: 4 additions & 4 deletions lib/lhm/adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ def remove_column(column_name)
# @param columns [Array<String>, Array<Symbol>, String, Symbol]
# @param index_name [String]
def add_index(columns, index_name = nil)
options = { name: index_name } if index_name
migration.add_index(table_name, columns, options || {})
options = index_name ? { name: index_name } : { }
migration.add_index(table_name, columns, **options)
end

# Removes the index in the given columns or by its name
Expand All @@ -51,7 +51,7 @@ def remove_index(columns, index_name = nil)
else
{ column: columns }
end
migration.remove_index(table_name, options)
migration.remove_index(table_name, **options)
end

# Change the column to use the provided definition, through ActiveRecord
Expand Down Expand Up @@ -79,7 +79,7 @@ def add_unique_index(columns, index_name = nil)
options = { unique: true }
options.merge!(name: index_name) if index_name # rubocop:disable Performance/RedundantMerge

migration.add_index(table_name, columns, options)
migration.add_index(table_name, columns, **options)
end

private
Expand Down
25 changes: 14 additions & 11 deletions spec/lhm/adapter/add_index_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,42 +9,45 @@

before do
allow(migration).to(
receive(:add_index).with(table_name, columns, options)
receive(:add_index).with(table_name, columns, **{})
)
end

let(:options) { {} }

context 'when passing a single column' do
before { adapter.add_index(columns) }

let(:columns) { :some_id_field }

before { adapter.add_index(columns) }

it 'calls #add_index in the migration' do
expect(migration).to(
have_received(:add_index).with(table_name, columns, options)
have_received(:add_index).with(table_name, columns)
)
end
end

context 'when passing an array of columns' do
before { adapter.add_index(columns) }

let(:columns) { [:some_id_field, :name] }

before { adapter.add_index(columns) }

it 'calls #add_index in the migration' do
expect(migration).to(
have_received(:add_index).with(table_name, columns, options)
have_received(:add_index).with(table_name, columns)
)
end
end

context 'when passing also an index name' do
before { adapter.add_index(columns, options[:name]) }

let(:columns) { [:some_id_field, :name] }
let(:options) { { name: 'index_name' } }

before do
allow(migration).to(
receive(:add_index).with(table_name, columns, name: 'index_name')
)
end
before { adapter.add_index(columns, options[:name]) }

it 'calls #add_index in the migration' do
expect(migration).to(
have_received(:add_index).with(table_name, columns, options)
Expand Down

0 comments on commit 11368f7

Please sign in to comment.