Skip to content
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

Make ids optionals #32

Closed
wants to merge 3 commits into from
Closed
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
13 changes: 8 additions & 5 deletions lib/oaken.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ def with(**attributes)
@attributes = previous_attributes if block_given?
end

def update(id, **attributes)
self.class.define_method(id) { find(id) }
def update(id = nil, **attributes)
self.class.define_method(id) { find(id) } if id

klass = nil
attributes = @attributes.merge(attributes)
Expand Down Expand Up @@ -72,8 +72,9 @@ def find(id)
@type.find identify id
end

def update(id, **attributes)
def update(id = nil, **attributes)
attributes = super
attributes.extract! :on_duplicate, :update_only, :returning, :unique_by, :record_timestamps

if record = @type.find_by(id: identify(id))
record.tap { _1.update!(**attributes) }
Expand All @@ -82,10 +83,12 @@ def update(id, **attributes)
end
end

def upsert(id, **attributes)
def upsert(id = nil, **attributes)
attributes = super
options = attributes.extract! :on_duplicate, :update_only, :returning, :unique_by, :record_timestamps

@type.new(attributes).validate!
@type.upsert({ id: identify(id), **attributes })
@type.upsert({ id: identify(id), **attributes }, **options)
end

private def identify(id) = ::ActiveRecord::FixtureSet.identify(id, @type.type_for_attribute(@type.primary_key).type)
Expand Down
17 changes: 16 additions & 1 deletion test/oaken_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,19 @@ def test_accessing_fixture

assert_equal [accounts.business], users.kasper.accounts
assert_equal [accounts.business], users.coworker.accounts
assert_equal [users.kasper, users.coworker], accounts.business.users

accounts.business.users.tap do
assert_includes _1, users.kasper
assert_includes _1, users.coworker
end
end

def test_unnamed_seeds
assert User.find_by(name: "Third")
assert_not_respond_to users, :third

assert Plan.find_by(title: "Premium")
assert_not_respond_to plans, :premium
end

def test_default_attributes
Expand All @@ -58,5 +70,8 @@ def test_upserting_vs_updating
plans.update :salty, title: "foo", price_cents: 0
end
assert_equal "after_save", error.message

plans.upsert title: "Basic", price_cents: 0
assert_equal 0, plans.basic.price_cents
end
end
2 changes: 2 additions & 0 deletions test/seeds/accounts/business.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ def accounts.increment_counter

users.update :kasper, name: "Kasper", accounts: [business]
users.update :coworker, name: "Coworker", accounts: [business]

users.update name: "Third", accounts: [business]
2 changes: 2 additions & 0 deletions test/seeds/data/plans.rb
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
plans.with unique_by: :title
plans.upsert :basic, title: "Basic", price_cents: 1000
plans.upsert title: "Premium", price_cents: 10_000
2 changes: 2 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ def self.root() = __dir__ # Needed for the sqlite3 tasks.
t.string :title, null: false
t.integer :price_cents, null: false
t.timestamps

t.index :title, unique: true
end

create_table :yaml_records, force: true do |t|
Expand Down