Skip to content

Commit

Permalink
Switch back to upsert to prep for passing unique_by
Browse files Browse the repository at this point in the history
  • Loading branch information
kaspth committed Jan 14, 2024
1 parent 93e85b2 commit c9a700b
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ orders.insert_all \

```ruby
# db/seeds/data/plans.rb
plans.insert :basic, title: "Basic", price_cents: 10_00
plans.upsert :basic, title: "Basic", price_cents: 10_00
```

Seed files will generally use `create` and/or `insert`. Passing a symbol to name the record is useful when reusing the data in tests.
Expand Down
4 changes: 2 additions & 2 deletions lib/oaken/stored/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ def create(label = nil, **attributes)
record
end

def insert(label = nil, **attributes)
def upsert(label = nil, **attributes)
attributes = @attributes.merge(attributes)
attributes.transform_values! { _1.respond_to?(:call) ? _1.call : _1 }

type.new(attributes).validate!
record = type.new(id: type.insert(attributes, returning: :id).rows.first.first)
record = type.new(id: type.upsert(attributes, returning: :id).rows.first.first)
label label => record if label
record
end
Expand Down
2 changes: 1 addition & 1 deletion test/dummy/db/seeds/data/plans.rb
Original file line number Diff line number Diff line change
@@ -1 +1 @@
plans.insert :basic, title: "Basic", price_cents: 10_00
plans.upsert :basic, title: "Basic", price_cents: 10_00
2 changes: 1 addition & 1 deletion test/dummy/db/seeds/test/data/plans.rb
Original file line number Diff line number Diff line change
@@ -1 +1 @@
plans.insert :test_premium, title: "Premium", price_cents: 20_00
plans.upsert :test_premium, title: "Premium", price_cents: 20_00

0 comments on commit c9a700b

Please sign in to comment.