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

Add #one to HasMany (#550) #553

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
10 changes: 10 additions & 0 deletions lib/hanami/model/associations/has_many.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@ def initialize(repository, source, target, subject, scope = nil)
freeze
end

# @raise [MultipleResultsError] if more than one record is available
#
# @since 1.3.3
# @api private
def one
raise Hanami::Model::MultipleResultsError, "#{count} results returned" if count > 1

scope.one
end

# @since 0.7.0
# @api private
def create(data)
Expand Down
3 changes: 3 additions & 0 deletions lib/hanami/model/error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,5 +127,8 @@ def initialize(url)
super("Unknown database adapter for URL: #{url.inspect}. Please check your database configuration (hint: ENV['DATABASE_URL']).")
end
end

class MultipleResultsError < Error
end
end
end
29 changes: 29 additions & 0 deletions spec/integration/hanami/model/associations/has_many_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -192,4 +192,33 @@
# skipped spec
it '#remove'
end

describe '#one' do
it 'raises an error if more than one record exists' do
author = authors.create(name: 'Cormac McCarthy')
book = books.create(author_id: author.id, title: 'Blood Meridian')
book = books.create(author_id: author.id, title: 'Blood Meridian')

expect do
authors.find_book_by_title(author, book.title)
end.to raise_error(Hanami::Model::MultipleResultsError)
end

it 'returns an individual record if only one record exists' do
author = authors.create(name: 'Toni Morrison')
book = books.create(author_id: author.id, title: 'Song of Solomon')

found = authors.find_book(author, book.id)

expect(found).to eq(book)
end

it 'returns nil if no records exist' do
author = authors.create(name: 'Aspiring Author')

found = authors.find_book(author, nil)

expect(found).to eq(nil)
end
end
end
8 changes: 8 additions & 0 deletions spec/support/fixtures.rb
Original file line number Diff line number Diff line change
Expand Up @@ -258,12 +258,20 @@ def find_book(author, id)
book_for(author, id).one
end

def find_book_by_title(author, title)
book_by_title(author, title).one
end

def book_exists?(author, id)
book_for(author, id).exists?
end

private

def book_by_title(author, title)
assoc(:books, author).where(title: title)
end

def book_for(author, id)
assoc(:books, author).where(id: id)
end
Expand Down