Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Enable test for writing aggregates in association
  • Loading branch information
jodosha committed Nov 14, 2016
1 parent f65d0e9 commit eebcace
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 11 deletions.
29 changes: 25 additions & 4 deletions lib/hanami/model/associations/has_many.rb
Expand Up @@ -41,11 +41,20 @@ def initialize(repository, source, target, subject, scope = nil)
@repository = repository
@source = source
@target = target
@subject = subject.to_h
@subject = subject.to_hash unless subject.nil?
@scope = scope || _build_scope
freeze
end

# @since 0.7.0
# @api private
def create(data)
entity.new(
command(:create, aggregate(target), use: [:timestamps])
.call(data)
)
end

# @since 0.7.0
# @api private
def add(data)
Expand Down Expand Up @@ -106,12 +115,24 @@ def command(target, relation, options = {})
repository.command(target, relation, options)
end

# @since 0.7.0
# @api private
def entity
repository.class.entity
end

# @since 0.7.0
# @api private
def relation(name)
repository.relations[name]
end

# @since 0.7.0
# @api private
def aggregate(name)
repository.aggregate(name)
end

# @since 0.7.0
# @api private
def association(name)
Expand Down Expand Up @@ -163,9 +184,9 @@ def association_keys
# @since 0.7.0
# @api private
def _build_scope
relation(target)
.where(foreign_key => subject.fetch(primary_key))
.as(Repository::MAPPER_NAME)
result = relation(target)
result = result.where(foreign_key => subject.fetch(primary_key)) unless subject.nil?
result.as(Repository::MAPPER_NAME)
end

# @since 0.7.0
Expand Down
2 changes: 1 addition & 1 deletion lib/hanami/repository.rb
Expand Up @@ -429,7 +429,7 @@ def clear
#
# @since 0.7.0
# @api private
def assoc(target, subject)
def assoc(target, subject = nil)
Hanami::Model::Association.build(self, target, subject)
end
end
Expand Down
15 changes: 9 additions & 6 deletions test/integration/associations/has_many_test.rb
Expand Up @@ -21,13 +21,16 @@
found.books.must_equal [book]
end

it 'creates an object with a collection of associated objects'
# it 'creates an object with a collection of associated objects' do
# repository = AuthorRepository.new
# author = repository.create_with_books(name: 'Henry Thoreau', books: [{ title: 'Walden' }])
it 'creates an object with a collection of associated objects' do
repository = AuthorRepository.new
author = repository.create_with_books(name: 'Henry Thoreau', books: [{ title: 'Walden' }])

# author.name.must_equal 'Fowler'
# end
author.must_be_instance_of(Author)
author.name.must_equal 'Henry Thoreau'
author.books.must_be_instance_of(Array)
author.books.first.must_be_instance_of(Book)
author.books.first.title.must_equal('Walden')
end

##############################################################################
# OPERATIONS #
Expand Down
4 changes: 4 additions & 0 deletions test/support/fixtures.rb
Expand Up @@ -52,6 +52,10 @@ class AuthorRepository < Hanami::Repository
has_many :books
end

def create_with_books(data)
assoc(:books).create(data)
end

def find_with_books(id)
aggregate(:books).where(authors__id: id).as(Author).one
end
Expand Down

0 comments on commit eebcace

Please sign in to comment.