From 26c17ebca4a52d11f3169e4e9af184af05375c33 Mon Sep 17 00:00:00 2001 From: Luca Guidi Date: Mon, 23 May 2016 04:08:38 +0200 Subject: [PATCH] Use schema auto-infer Ref: https://github.com/rom-rb/rom/pull/345 Ref: https://github.com/rom-rb/rom-sql/pull/63 --- lib/hanami/entity.rb | 5 +++++ test/integration/repository_test.rb | 2 +- test/support/fixtures.rb | 11 ++--------- 3 files changed, 8 insertions(+), 10 deletions(-) diff --git a/lib/hanami/entity.rb b/lib/hanami/entity.rb index dc5f3e1a..0cf9fd01 100644 --- a/lib/hanami/entity.rb +++ b/lib/hanami/entity.rb @@ -72,6 +72,11 @@ def ==(other) id == other.id end + def to_h + @attributes.dup + end + alias_method :to_hash, :to_h + private attr_reader :attributes diff --git a/test/integration/repository_test.rb b/test/integration/repository_test.rb index b6599edb..6e661b58 100644 --- a/test/integration/repository_test.rb +++ b/test/integration/repository_test.rb @@ -120,7 +120,7 @@ found = repository.find_with_comments(user.id) found.must_equal user - found.comments.must_equal [comment] + found.comments.map(&:to_h).must_equal [comment.to_h] end end end diff --git a/test/support/fixtures.rb b/test/support/fixtures.rb index 2ac572c0..16362350 100644 --- a/test/support/fixtures.rb +++ b/test/support/fixtures.rb @@ -28,10 +28,7 @@ class Comment class UserRepository < Hanami::Repository relation(:users) do - schema do - attribute :id, ROM::SQL::Types::Serial # We can copy these types to Hanami - attribute :name, ROM::SQL::Types::String # We can copy these types to Hanami - + schema(infer: true) do associate do many :comments end @@ -66,11 +63,7 @@ def find_with_comments(id) class CommentRepository < Hanami::Repository relation(:comments) do - schema do - attribute :id, ROM::SQL::Types::Serial # We can copy these types to Hanami - attribute :user_id, ROM::SQL::Types::ForeignKey(:users) # We can copy these types to Hanami - attribute :text, ROM::SQL::Types::String # We can copy these types to Hanami - end + schema(infer: true) def by_id(id) where(id: id)