Skip to content

Commit

Permalink
Merge pull request #71 from manuquentin/fix_literal
Browse files Browse the repository at this point in the history
Fix specs about upsert_keys and literal
  • Loading branch information
olleolleolle authored Apr 25, 2018
2 parents 488d69a + e727f95 commit 3328696
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 14 deletions.
2 changes: 1 addition & 1 deletion lib/active_record_upsert/arel/crud.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module Arel
module Crud
def compile_upsert(upsert_keys, upsert_options, upsert_values, insert_values, wheres)
# Support non-attribute key (like `md5(my_attribute)``)
target = self[upsert_keys.kind_of?(Hash) ? ::Arel::Nodes::SqlLiteral.new(upsert_keys[:literal]) : upsert_keys.join(',')]
target = self[upsert_options.key?(:literal) ? ::Arel::Nodes::SqlLiteral.new(upsert_options[:literal]) : upsert_keys.join(',')]
on_conflict_do_update = OnConflictDoUpdateManager.new

on_conflict_do_update.target = target
Expand Down
29 changes: 16 additions & 13 deletions spec/active_record/key_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ module ActiveRecord
end
context 'different ways of setting keys' do
let(:attrs) { {make: 'Ford', name: 'Focus', long_field: SecureRandom.uuid} }
before { Vehicle.create(attrs) }
let!(:vehicule) { Vehicle.create(attrs) }

it 'works with multiple symbol args' do
Vehicle.upsert_keys :make, :name
Expand All @@ -71,28 +71,31 @@ module ActiveRecord
expect(upserted.wheels_count).to eq(1)
end
it 'works with a single symbol' do
v = Vehicle.create
Vehicle.upsert_keys :id
upserted = Vehicle.new(id: v.id, wheels_count: 1)
upserted.upsert
upserted = Vehicle.new(id: vehicule.id, name: 'ford', wheels_count: 1)
result = upserted.upsert

expect(result).to be_truthy
expect(upserted.wheels_count).to eq(1)
expect(upserted.id).to eq(v.id)
expect(upserted.id).to eq(vehicule.id)
end
it 'works with a single string' do
v = Vehicle.create
Vehicle.upsert_keys 'id'
upserted = Vehicle.new(id: v.id, wheels_count: 1)
upserted.upsert
upserted = Vehicle.new(id: vehicule.id, name: 'ford', wheels_count: 1)
result = upserted.upsert

expect(result).to be_truthy
expect(upserted.wheels_count).to eq(1)
expect(upserted.id).to eq(v.id)
expect(upserted.id).to eq(vehicule.id)
end
it 'works with a literal' do
v = Vehicle.create
Vehicle.upsert_keys literal: 'md5(long_field)'
upserted = Vehicle.new(id: v.id, long_field: attrs[:long_field])
upserted.upsert
upserted = Vehicle.new(id: vehicule.id, name: 'ford', long_field: attrs[:long_field])
result = upserted.upsert

expect(result).to be_truthy
expect(upserted.long_field).to eq(attrs[:long_field])
expect(upserted.id).to eq(v.id)
expect(upserted.id).to eq(vehicule.id)
end
end

Expand Down

0 comments on commit 3328696

Please sign in to comment.