Permalink
Browse files

Wrap articles in a presenter

  • Loading branch information...
avdgaag committed Mar 24, 2015
1 parent d25b381 commit a91fc9bf0bdcbe01b38b41aba7362df7e673cf34
View
@@ -14,6 +14,7 @@ gem 'lotusrb', github: 'lotus/lotus'
gem 'pg'
gem 'kramdown'
group :test do
gem 'minitest'
View
@@ -78,6 +78,7 @@ GEM
http_router (0.11.1)
rack (>= 1.0.0)
url_mount (~> 0.2.1)
kramdown (1.6.0)
mime-types (2.4.3)
mini_portile (0.6.2)
minitest (5.5.1)
@@ -104,6 +105,7 @@ PLATFORMS
DEPENDENCIES
bundler
capybara
kramdown
lotus-controller!
lotus-helpers!
lotus-model!
View
@@ -17,7 +17,8 @@ class Application < Lotus::Application
#
load_paths << [
'controllers',
'views'
'views',
'presenters'
]
# Handle exceptions with HTTP statuses (true) or don't catch them (false).
@@ -0,0 +1,15 @@
require 'kramdown'
module Web::Presenters
class ArticlePresenter
include Lotus::Presenter
def created_at
super.strftime '%e %b %Y %H:%M'
end
def body
_raw Kramdown::Document.new(super).to_html
end
end
end
@@ -1,5 +1,9 @@
module Web::Views::Articles
class Show
include Web::View
def article
Web::Presenters::ArticlePresenter.new(locals[:article])
end
end
end
@@ -1,5 +1,11 @@
module Web::Views::Home
class Index
include Web::View
def articles
locals[:articles].map do |article|
Web::Presenters::ArticlePresenter.new(article)
end
end
end
end
@@ -0,0 +1,20 @@
require_relative '../../spec_helper'
module Web::Presenters
describe ArticlePresenter do
let(:article) { Demo::Article.new(title: 'Hello, world', body: 'lorem ipsum', created_at: Time.new(2015, 2, 1, 12, 30)) }
let(:presenter) { ArticlePresenter.new(article) }
it 'is a Lotus::Presenter' do
assert_kind_of Lotus::Presenter, presenter
end
it 'formats the created_at timestamp' do
assert_equal ' 1 Feb 2015 12:30', presenter.created_at, 'created_at formatting is incorrect'
end
it 'formats the body using markdown' do
assert_equal "<p>lorem ipsum</p>\n", presenter.body, "no markdown was applied to the body"
end
end
end
@@ -6,8 +6,8 @@ module Web::Views::Articles
let(:template) { Lotus::View::Template.new('apps/web/templates/articles/show.html.erb') }
let(:view) { Web::Views::Articles::Show.new(template, exposures) }
it "exposes #article" do
assert_same exposures[:article], view.article, 'view should have exposed article'
it 'exposes #article wrapped in a presenter' do
assert_kind_of Web::Presenters::ArticlePresenter, view.article, 'article should have been wrapped in a presenter'
end
end
end
@@ -2,12 +2,12 @@
module Web::Views::Home
describe Index do
let(:exposures) { Hash[articles: []] }
let(:exposures) { Hash[articles: [Object.new]] }
let(:template) { Lotus::View::Template.new('apps/web/templates/home/index.html.erb') }
let(:view) { Web::Views::Home::Index.new(template, exposures) }
it 'exposes #articles' do
assert_same exposures[:articles], view.articles, 'articles should have been exposed'
it 'exposes #articles with presenter applied' do
assert_kind_of Web::Presenters::ArticlePresenter, view.articles.first, 'articles should have been wrapped in presenters'
end
end
end

0 comments on commit a91fc9b

Please sign in to comment.