Skip to content

Commit

Permalink
App updates for finding/displaying the right doc
Browse files Browse the repository at this point in the history
* model updates to find the current/specified version of a man page
* controller updates to load up the right doc
* view updates to display the pregenerated HTML
* tests
  • Loading branch information
nickh committed Apr 4, 2012
1 parent 104d5df commit 1b461c9
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 23 deletions.
5 changes: 5 additions & 0 deletions Gemfile
Expand Up @@ -12,6 +12,7 @@ gem 'yajl-ruby', '~> 1.1.0'
gem 'excon', '~> 0.9.4'
gem 'heroku'
gem 'haml'
gem 'tilt'

# Gems used only for assets and not required
# in production environments by default.
Expand All @@ -34,3 +35,7 @@ group :development do
gem "sqlite3-ruby"
gem "shotgun"
end

group :test do
gem 'factory_girl_rails'
end
33 changes: 20 additions & 13 deletions Gemfile.lock
Expand Up @@ -39,8 +39,7 @@ GEM
coffee-script-source
execjs
coffee-script-source (1.2.0)
commonjs (0.2.0)
therubyracer (~> 0.9.9)
commonjs (0.2.5)
compass (0.12.1)
chunky_png (~> 1.2)
fssm (>= 0.2.7)
Expand All @@ -53,13 +52,19 @@ GEM
excon (0.9.6)
execjs (1.3.0)
multi_json (~> 1.0)
factory_girl (3.0.0)
activesupport (>= 3.0.0)
factory_girl_rails (3.0.0)
factory_girl (~> 3.0.0)
railties (>= 3.0.0)
faraday (0.7.6)
addressable (~> 2.2)
multipart-post (~> 1.1)
rack (~> 1.1)
fssm (0.2.8.1)
haml (3.1.4)
heroku (2.22.0)
heroku (2.23.0)
launchy (>= 0.3.2)
netrc (~> 0.7.1)
rest-client (~> 1.6.1)
Expand All @@ -70,27 +75,27 @@ GEM
jquery-rails (2.0.1)
railties (>= 3.2.0, < 5.0)
thor (~> 0.14)
json (1.6.5)
json (1.6.6)
launchy (2.1.0)
addressable (~> 2.2.6)
less (2.0.9)
less (2.0.11)
commonjs (~> 0.2.0)
therubyracer (~> 0.9.9)
less-rails (2.1.7)
less-rails (2.1.8)
actionpack (>= 3.1)
less (~> 2.0.7)
less-rails-bootstrap (2.0.6)
less-rails-bootstrap (2.0.8)
less-rails (~> 2.1.0)
libv8 (3.3.10.4)
mail (2.4.3)
mail (2.4.4)
i18n (>= 0.4.0)
mime-types (~> 1.16)
treetop (~> 1.4.8)
mime-types (1.17.2)
multi_json (1.1.0)
mime-types (1.18)
multi_json (1.2.0)
multipart-post (1.1.5)
netrc (0.7.1)
pg (0.11.0)
pg (0.13.2)
polyglot (0.3.3)
rack (1.4.1)
rack-cache (1.2)
Expand Down Expand Up @@ -121,7 +126,7 @@ GEM
mime-types (>= 1.16)
rubyzip (0.9.6.1)
sass (3.1.15)
sass-rails (3.2.4)
sass-rails (3.2.5)
railties (~> 3.2.0)
sass (>= 3.1.10)
tilt (~> 1.3)
Expand All @@ -136,7 +141,7 @@ GEM
sqlite3 (>= 1.3.3)
therubyracer (0.9.10)
libv8 (~> 3.3.10)
thin (1.2.7)
thin (1.3.1)
daemons (>= 1.0.9)
eventmachine (>= 0.12.6)
rack (>= 1.0.0)
Expand All @@ -146,7 +151,7 @@ GEM
polyglot
polyglot (>= 0.3.1)
tzinfo (0.3.32)
uglifier (1.2.3)
uglifier (1.2.4)
execjs (>= 0.3.0)
multi_json (>= 1.0.2)
yajl-ruby (1.1.0)
Expand All @@ -158,6 +163,7 @@ DEPENDENCIES
coffee-rails (~> 3.2.1)
compass-rails
excon (~> 0.9.4)
factory_girl_rails
faraday
haml
heroku
Expand All @@ -171,5 +177,6 @@ DEPENDENCIES
sqlite3
sqlite3-ruby
thin
tilt
uglifier (>= 1.0.3)
yajl-ruby (~> 1.1.0)
15 changes: 11 additions & 4 deletions app/controllers/doc_controller.rb
Expand Up @@ -9,10 +9,17 @@ def ref
end

def man
p params
@docfile = DocFile.where(:name => params[:file]).first
@doc = @docfile.doc_versions.first.doc.plain
p @doc
if params[:version]
doc_version = DocVersion.for_version(params[:file], params[:version])
else
doc_version = DocVersion.latest_for(params[:file])
end

if doc_version.nil?
redirect_to :index
else
@doc = doc_version.doc
end
end

def book
Expand Down
15 changes: 15 additions & 0 deletions app/models/doc_version.rb
Expand Up @@ -2,4 +2,19 @@ class DocVersion < ActiveRecord::Base
belongs_to :doc
belongs_to :version
belongs_to :doc_file

def self.latest_for(doc_name)
for_doc(doc_name).joins(:version).order('versions.id DESC').first
end

def self.for_version(doc_name, version_name)
for_doc(doc_name).joins(:version).where(['versions.name=?', version_name]).first
end

private

def self.for_doc(doc_name)
includes(:doc).joins(:doc_file).where('doc_files.name=?', doc_name)
end

end
2 changes: 1 addition & 1 deletion app/views/doc/man.html.erb
Expand Up @@ -4,5 +4,5 @@
</div>

<div id='main'>
<pre><%= @doc %></pre>
<%= @doc.html %>
</div>
1 change: 1 addition & 0 deletions config/routes.rb
Expand Up @@ -4,6 +4,7 @@
match "/doc" => "doc#index"
match "/doc/ref" => "doc#ref"
match "/doc/ref/:file" => "doc#man"
match "/doc/ref/:file/:version" => "doc#man"
match "/doc/book" => "doc#book"
match "/doc/videos" => "doc#videos"
match "/doc/ext" => "doc#ext"
Expand Down
26 changes: 26 additions & 0 deletions test/factories.rb
@@ -0,0 +1,26 @@
FactoryGirl.define do
sequence :plain do |n|
"Doc #{n}"
end

sequence :version_name do |n|
"v#{n}.0"
end

factory :doc_file do
end

factory :doc do
plain :plain
end

factory :doc_version do
doc_file
version
doc
end

factory :version do
name :version_name
end
end
11 changes: 10 additions & 1 deletion test/functional/doc_controller_test.rb
@@ -1,4 +1,4 @@
require 'test_helper'
require File.expand_path("../../test_helper", __FILE__)

class DocControllerTest < ActionController::TestCase
test "should get index" do
Expand All @@ -11,4 +11,13 @@ class DocControllerTest < ActionController::TestCase
assert_response :success
end

test "gets the latest version of a man page" do
get :man, :file => 'test-command'
assert_response :success
end

test "gets a specific version of a man page" do
get :man, :file => 'test-command', :version => 'v1.0'
assert_response :success
end
end
26 changes: 22 additions & 4 deletions test/unit/doc_version_test.rb
@@ -1,7 +1,25 @@
require 'test_helper'
require File.expand_path("../../test_helper", __FILE__)

class DocVersionTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
test 'finds most recent version' do
range = 0..3
file = Factory(:doc_file, :name => 'test-command')
docs = range.map{|i| Factory(:doc, :plain => "Doc #{i}")}
vers = range.map{|i| Factory(:version, :name => "v#{i}.0")}
dver = range.map{|i| Factory(:doc_version, :doc_file => file, :version => vers[i], :doc => docs[i])}

dv = DocVersion.latest_for('test-command')
assert_equal docs[3], dv.doc
end

test 'finds a specific version' do
range = 0..3
file = Factory(:doc_file, :name => 'test-command')
docs = range.map{|i| Factory(:doc, :plain => "Doc #{i}")}
vers = range.map{|i| Factory(:version, :name => "v#{i}.0")}
dver = range.map{|i| Factory(:doc_version, :doc_file => file, :version => vers[i], :doc => docs[i])}

dv = DocVersion.for_version('test-command', 'v2.0')
assert_equal docs[2], dv.doc
end
end

0 comments on commit 1b461c9

Please sign in to comment.