Skip to content
This repository has been archived by the owner on Jul 15, 2021. It is now read-only.

Commit

Permalink
Add specs and refactor ArticleDecorator#authors
Browse files Browse the repository at this point in the history
  • Loading branch information
twinn committed Oct 27, 2015
1 parent c044ed7 commit ab80c67
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 7 deletions.
12 changes: 5 additions & 7 deletions app/decorators/article_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@ class ArticleDecorator < ApplicationDecorator
delegate_all

def authors
output = "#{author.link_tag} #{created_at_tag} "
output = [author.link_tag, created_at_tag]

if editor && updated_at_tag
if editor.id != source.author_id
output << "and updated by #{editor_email_tag} on #{updated_at_tag}"
else
output << "and updated on #{updated_at_tag}"
end
output << "and updated"
output << "by #{editor_email_tag}" if editor.id != source.author_id
output << "on #{updated_at_tag}"
end

output.html_safe
output.join(" ").html_safe
end

def title
Expand Down
52 changes: 52 additions & 0 deletions spec/decorators/article_decorator_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,56 @@
require "rails_helper"

RSpec.describe ArticleDecorator do
describe "#authors" do
let(:author) { create(:user) }
let(:expected_author_with_created_at) do
"#{subject.author.link_tag} #{subject.created_at_tag}"
end
subject { described_class.new(article) }

context "without an editor" do
let(:article) { create(:article, created_at: Time.at(0), author: author) }

it "displays a link to the author and the created_at time" do
expect(subject.authors).to eq(expected_author_with_created_at)
end
end

context "with an editor who is also the author" do
let(:article) do
create(
:article,
created_at: Time.at(0),
updated_at: Time.at(1),
author: author,
editor: author
)
end

it "displays a link to the author, created_at time and the updated at time" do
expect(subject.authors).to eq(
"#{expected_author_with_created_at} and updated on #{subject.updated_at_tag}"
)
end
end

context "with an editor who is not the author" do
let(:editor) { create(:user) }
let(:article) do
create(
:article,
created_at: Time.at(0),
updated_at: Time.at(1),
author: author,
editor: editor
)
end

it "displays a link to the author, created_at time and the editor with updated at time" do
expect(subject.authors).to eq(
"#{expected_author_with_created_at} and updated by #{subject.editor_email_tag} on #{subject.updated_at_tag}"
)
end
end
end
end

0 comments on commit ab80c67

Please sign in to comment.