Permalink
Browse files

Add comment entity, repository and mapping

  • Loading branch information...
avdgaag committed Mar 24, 2015
1 parent a91fc9b commit 42ded30dfc6435f762d3b43af222ebed62873cd7
@@ -0,0 +1,11 @@
Sequel.migration do
change do
create_table :comments do
primary_key :id
String :author, null: false
String :body, null: false, text: true
Time :created_at, null: false, default: 'now()'
foreign_key :article_id, :articles, on_delete: :cascade, null: false
end
end
end
View
@@ -7,3 +7,14 @@
attribute :body, String
attribute :created_at, Time
end
collection :comments do
entity Demo::Comment
repository Demo::CommentRepository
attribute :id, Integer
attribute :author, String
attribute :body, String
attribute :created_at, Time
attribute :article_id, Integer
end
@@ -0,0 +1,6 @@
module Demo
class Comment
include Lotus::Entity
attributes :id, :author, :body, :created_at, :article_id
end
end
@@ -0,0 +1,11 @@
module Demo
class CommentRepository
include Lotus::Repository
def self.for_article(article)
query do
where(article_id: article.id)
end
end
end
end
@@ -0,0 +1,23 @@
require_relative '../../spec_helper'
module Demo
describe Comment do
it 'is a Lotus::Entity' do
assert_kind_of Lotus::Entity, Comment.new
end
it 'can set attribute through the initializer' do
timestamp = Time.now
article = Comment.new(
id: 1,
author: 'John',
body: 'Lorem ipsum',
created_at: timestamp
)
assert_equal 1, article.id
assert_equal 'John', article.author
assert_equal 'Lorem ipsum', article.body
assert_equal timestamp, article.created_at
end
end
end
@@ -0,0 +1,26 @@
require_relative '../../spec_helper'
module Demo
describe CommentRepository do
before do
ArticleRepository.clear
CommentRepository.clear
end
it 'is a Lotus::Repository' do
assert_kind_of Lotus::Repository, CommentRepository.new
end
it 'can find comments by article_id' do
article = ArticleRepository.create(
Article.new(title: 'Hello, world', body: 'Lorem ipsum')
)
comment = CommentRepository.create(
Comment.new(author: "Author", body: 'Lorem Ipsum', article_id: article.id)
)
comments = CommentRepository.for_article(article)
assert_equal 1, comments.count
assert_equal comment, comments.first
end
end
end

0 comments on commit 42ded30

Please sign in to comment.