Skip to content

Commit

Permalink
Add detail view for one document
Browse files Browse the repository at this point in the history
On the index list, each document now is a link to the respective article
detail page. A new route `GET /:document_id` provides the detail view.
  • Loading branch information
nicolas-fricke committed Nov 24, 2017
1 parent 6dc78af commit 7d038a9
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 13 deletions.
7 changes: 7 additions & 0 deletions app/google_drive_connector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,11 @@ def docs
.sort_by(&:modified_time)
.reverse
end

def doc(document_id)
document = session.file_by_id(document_id)
document if document.parents.include?(Config[:folder_id])
rescue Google::Apis::ClientError
nil
end
end
10 changes: 10 additions & 0 deletions app/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,13 @@
docs: GoogleDriveConnector.new.docs,
}
end

get '/:document_id' do
doc = GoogleDriveConnector.new.doc(params[:document_id])
halt 404 unless doc
erb :show,
locals: {
blog_title: Config[:blog_title],
doc: doc,
}
end
2 changes: 1 addition & 1 deletion app/views/index.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<ul>
<% docs.each do |doc| %>
<li>
<a>
<a href="/<%= doc.id %>">
<%= doc.title %> (<%= doc.modified_time.strftime('%d %b %Y') %>)
</a>
</li>
Expand Down
9 changes: 9 additions & 0 deletions app/views/show.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<h1><%= doc.title %></h1>
<div class="meta-info">
<cite><%= doc.last_modifying_user.display_name %></cite>
//
<time><%= doc.modified_time.strftime('%d %b %Y') %></time>
</div>
<article>
Lorem ipsum, sit dolor amet.
</article>
39 changes: 39 additions & 0 deletions spec/google_drive_connector_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,43 @@
it { should contain_exactly older_document_double, newer_document_double }
it { should eq [newer_document_double, older_document_double] }
end

describe '#doc' do
let(:doc_id) { 'd0c-1d' }
subject { described_class.new.doc(doc_id) }

context 'when the `doc_id` exists' do
let(:folder_id) { '123abc' }
let(:document_double) { double('document', parents: document_parents) }

before do
allow(Config).to receive(:[]).with(:folder_id).and_return(folder_id)
allow(session_double).to(
receive(:file_by_id).with(doc_id).and_return(document_double)
)
end

context 'and it is in the right folder' do
let(:document_parents) { [folder_id] }
it { should eq document_double }
end

context 'but it is not in the right folder' do
let(:document_parents) { ['qwe987'] }
it { should eq nil }
end
end

context 'when the `doc_id` does not exist' do
before do
allow(session_double).to(
receive(:file_by_id)
.with(doc_id)
.and_raise(Google::Apis::ClientError, 'File not found')
)
end

it { should eq nil }
end
end
end
65 changes: 53 additions & 12 deletions spec/server_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,7 @@ def app
let(:blog_title) { 'My Blog' }
let(:config) { { blog_title: blog_title } }

let(:document_double_1) do
double 'document_1',
modified_time: DateTime.now - 3, # 3 days ago
title: 'My first document'
end
let(:document_double_2) do
double 'document_2',
modified_time: DateTime.now - 1, # 1 day ago
title: 'New stuff'
end
let(:docs) { [document_double_1, document_double_2] }
let(:drive_connector_double) { double('drive_connector', docs: docs) }
let(:drive_connector_double) { double('drive_connector') }

before do
Config.instance_variable_set(:@config, config)
Expand All @@ -33,6 +22,22 @@ def app
end

describe 'GET /' do
let(:document_double_1) do
double 'document_1',
id: 'd0cum3nt-1',
modified_time: DateTime.now - 3, # 3 days ago
title: 'My first document'
end
let(:document_double_2) do
double 'document_2',
id: 'd0cum3nt-2',
modified_time: DateTime.now - 1, # 1 day ago
title: 'New stuff'
end
let(:docs) { [document_double_1, document_double_2] }

before { allow(drive_connector_double).to receive(:docs).and_return(docs) }

it 'should return the blog index page' do
get '/'
expect(last_response).to be_successful
Expand All @@ -41,4 +46,40 @@ def app
document_double_2.title
end
end

describe 'GET /:document_id' do
let(:doc_id) { 'd0cum3nt' }

before do
allow(drive_connector_double)
.to receive(:doc).with(doc_id).and_return(document_double)
end

context 'when there is a document for the given ID' do
let(:user_double) { double 'user', display_name: 'Us Er' }
let(:document_double) do
double 'document_1',
id: doc_id,
modified_time: DateTime.now - 3, # 3 days ago
title: 'My first document',
last_modifying_user: user_double
end

it 'should return the article page for the given document' do
get "/#{doc_id}"
expect(last_response).to be_successful
expect(last_response.body).to include document_double.title
end
end

context 'when there is no document for the given ID' do
let(:document_double) { nil }

it 'should return a 404 error' do
get "/#{doc_id}"
expect(last_response).to_not be_successful
expect(last_response).to be_not_found
end
end
end
end

0 comments on commit 7d038a9

Please sign in to comment.