Permalink
Browse files

Add HTML format

  • Loading branch information...
1 parent 51a4fb6 commit a5a43fbd6e55adfef61ff77fe74e61867afe6d63 Stephen Anderson and Devin Walters committed with bendyworks Jan 5, 2011
Showing with 87 additions and 36 deletions.
  1. +9 −3 lib/generators/pig/pig_generator.rb
  2. +43 −19 lib/pig.rb
  3. +35 −14 spec/pig_spec.rb
@@ -5,18 +5,24 @@ class PigGenerator < Rails::Generators::Base
class_option :revision_path,
:banner => '/path_to_revisions',
:default => '/revision',
- :desc => 'A root-relative path at which to display revision information.'
+ :desc => 'A root-relative path at which to display revision information'
class_option :skip_routes,
:type => :boolean,
:default => false,
- :desc => 'Do not generate routes.'
+ :desc => 'Do not generate routes'
+
+ class_option :html,
+ :type => :boolean,
+ :default => true,
+ :desc => 'HTML output'
def install_pig
if options[:skip_routes]
puts 'Nothing to do'
else
- route("match '#{formatted_revision_path}', :to => Pig.new")
+ style_type = options[:html] ? ':html' : ':plain'
@bigtiger
bigtiger Jan 6, 2011

Is there a reason you chose to refer to it as a 'style' here and not a 'format'?

@bendycode
bendycode Jan 6, 2011 Member

Not a good one.

+ route("match '#{formatted_revision_path}', :to => Pig.new({:style => #{style_type}})")
end
end
View
@@ -1,9 +1,45 @@
require 'git'
+WORKING_DIR = File.join(File.dirname(File.expand_path(__FILE__)), "..")
+NUMBER_OF_COMMITS = 10
+
class Pig
- WORKING_DIR = File.join(File.dirname(File.expand_path(__FILE__)), "..")
- NUMBER_OF_COMMITS = 10
+ def initialize options
+ @style = options[:style]
+ end
+
+ def history
+ results = ""
+ commits = repository.log(NUMBER_OF_COMMITS) || []
+ commits.each do |commit|
+ results << format(commit)
+ end
+ @style == :html ? wrap(results) : results
+ end
+
+ def call env
+ rack_response_template << [history]
+ end
+
+ def rack_response_template
+ [200, {"Content-Type" => "text/#{@style}"}]
+ end
+
+ private
+
+ def format commit
+ case @style
+ when :plain
+ format_plain commit
+ when :html
+ format_html commit
+ end
+ end
+
+ def repository
+ Git.open root_dir
+ end
def root_dir
if defined? Rails
@@ -13,28 +49,16 @@ def root_dir
end
end
- def repository
- Git.open(root_dir)
+ def wrap html_results
+ "<html><head><title>Deployed Revisions</title></head><body><ul>#{html_results}</ul></body></html>"
end
- def history
- results = ""
- commits = repository.log(NUMBER_OF_COMMITS) || []
- commits.each do |commit|
- results << format(commit)
- end
- results
- end
-
- def format(commit)
+ def format_plain commit
"#{commit.message}\n #{commit.to_s}\n #{commit.author.date.strftime("%Y-%m-%d")} #{commit.author.name}\n\n"
end
- def call(env)
- rack_response_template << [history]
+ def format_html commit
+ "<li><h3>#{commit.message}</h3><br />#{commit.to_s}<br />#{commit.author.date.strftime("%Y-%m-%d")} #{commit.author.name}<br /><br /></li>"
end
- def rack_response_template
- [200, {"Content-Type" => "text/plain"}]
- end
end
View
@@ -2,7 +2,8 @@
describe Pig do
- let(:pig) { Pig.new }
+ let(:style) { :plain }
+ let(:pig) { Pig.new({:style => style}) }
describe "#call" do
@@ -22,40 +23,60 @@
let(:date) { stub(:strftime => "12-31-01") }
let(:commit) { stub(:author => author, :to_s => "393932", :message => commit_message) }
- it "returns the commit message, sha1, date, and author by default" do
- pig.format(commit).should == "One great commit\n 393932\n 12-31-01 Dev Author\n\n"
+ context "plain style" do
+ let(:style) { :plain }
+ it "returns the commit message, sha1, date, and author in plain text" do
+ pig.send(:format, commit).should == "One great commit\n 393932\n 12-31-01 Dev Author\n\n"
+ end
+ end
+
+ context "html style" do
+ let(:style) { :html }
+ it "returns the commit message, sha1, date, and author in html format" do
+ pig.send(:format, commit).should == "<li><h3>One great commit</h3><br />393932<br />12-31-01 Dev Author<br /><br /></li>"
+ end
end
end
describe "#repository" do
it "calls Git.open on the specified working directory" do
- Git.should_receive(:open, Pig::WORKING_DIR)
- pig.repository
+ Git.should_receive(:open, WORKING_DIR)
+ pig.send(:repository)
end
end
describe "#history" do
- before do
- repo = stub(:log)
- pig.stub(:repository).and_return(repo)
- repo.stub(:log, 10).and_return([])
- end
-
it "retrieves a log of the last 10 commits by default" do
repo = stub(:log)
pig.should_receive(:repository).and_return(repo)
repo.should_receive(:log, 10).and_return([])
pig.history
end
- it "returns an empty string if no commits are found" do
- pig.history.should == ""
+ context "when no commits" do
+ before do
+ repo = stub(:log)
+ pig.stub(:repository).and_return(repo)
+ repo.stub(:log, 10).and_return([])
+ end
+
+ context "plain style" do
+ it "returns an empty string" do
+ pig.history.should == ""
+ end
+ end
+
+ context "html style" do
+ let(:style) { :html }
+ it "returns empty html" do
+ pig.history.should == "<html><head><title>Deployed Revisions</title></head><body><ul></ul></body></html>"
+ end
+ end
end
-
end
end

0 comments on commit a5a43fb

Please sign in to comment.