Skip to content
This repository
Browse code

refactor blog controller & add blog presenter

  • Loading branch information...
commit 25563fcdd1fcd82dc0d83cec3c581baf2b6125b4 1 parent cd5259a
Bryan Goines authored September 20, 2012
28  app/controllers/blog_controller.rb
@@ -6,21 +6,14 @@ def progit
6 6
     month = params[:month]
7 7
     day   = params[:day]
8 8
     slug  = params[:slug]
9  
-    file  = "#{year}-#{month}-#{day}-#{slug}"
10  
-    @path = "#{Rails.root}/app/views/blog/progit/#{file}"
11  
-    content = ''
12  
-    if File.exists?("#{@path}.markdown")
13  
-      markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML)
14  
-      content = File.read("#{@path}.markdown")
15  
-      content, @frontmatter = extract_frontmatter(content)
16  
-      @content = markdown.render(content)
17  
-    elsif File.exists?("#{path}.html")
18  
-      content = File.read("#{path}.html")
19  
-      @content, @frontmatter = extract_frontmatter(content)
  9
+    file  = [year, month, day, slug] * "-"
  10
+    blog = BlogPresenter.new(file)
  11
+    if blog.exists?
  12
+      @content, @frontmatter = blog.render
20 13
     else
21 14
       raise PageNotFound
22 15
     end
23  
-
  16
+    render :progit
24 17
   end
25 18
 
26 19
   # for Gitscm blog
@@ -30,14 +23,5 @@ def gitscm
30 23
 
31 24
   private
32 25
 
33  
-  def extract_frontmatter(content)
34  
-    if content =~ /^(---\s*\n.*?\n?)^(---\s*$\n?)(.*)/m
35  
-      cnt = $3
36  
-      data = YAML.load($1)
37  
-      [cnt, data]
38  
-    else
39  
-      [content, {}]
40  
-    end
41  
-  end
42  
-
  26
+ 
43 27
 end
40  app/presenters/blog_presenter.rb
... ...
@@ -0,0 +1,40 @@
  1
+class BlogPresenter
  2
+  
  3
+  def initialize(file)
  4
+    @file = file
  5
+    @path = "#{Rails.root}/app/views/blog/progit/#{@file}"
  6
+  end
  7
+
  8
+  def exists?
  9
+    File.exists?("#{@path}.markdown") || File.exists?("#{@path}.html")
  10
+  end
  11
+
  12
+  def render
  13
+    if File.exists?("#{@path}.markdown")
  14
+      source = File.read("#{@path}.markdown")
  15
+      content, frontmatter = extract_frontmatter(source)
  16
+      template = markdown.render(content)
  17
+      [template, frontmatter]
  18
+    elsif File.exists?("#{@path}.html")
  19
+      source = File.read("#{@path}.html")
  20
+      extract_frontmatter(source)
  21
+    end
  22
+  end
  23
+
  24
+  private
  25
+  
  26
+  def markdown
  27
+    @markdown ||= Redcarpet::Markdown.new(Redcarpet::Render::HTML)
  28
+  end
  29
+
  30
+  def extract_frontmatter(content)
  31
+    if content =~ /^(---\s*\n.*?\n?)^(---\s*$\n?)(.*)/m
  32
+      cnt = $3
  33
+      data = YAML.load($1)
  34
+      [cnt, data]
  35
+    else
  36
+      [content, {}]
  37
+    end
  38
+  end
  39
+
  40
+end
2  config/application.rb
@@ -16,7 +16,7 @@ class Application < Rails::Application
16 16
     # -- all .rb files in that directory are automatically loaded.
17 17
 
18 18
     # Custom directories with classes and modules you want to be autoloadable.
19  
-    # config.autoload_paths += %W(#{config.root}/extras)
  19
+    config.autoload_paths += %W(#{config.root}/app/presenters)
20 20
 
21 21
     # Only load the plugins named here, in the order given (default is alphabetical).
22 22
     # :all can be used as a placeholder for all plugins not explicitly named.
30  test/unit/presenters/blog_presenter_test.rb
... ...
@@ -0,0 +1,30 @@
  1
+require 'test_helper'
  2
+
  3
+class BlogPresenterTest < ActiveSupport::TestCase
  4
+
  5
+  setup do
  6
+    @good   = ["2010", "08", "25", "notes"] * "-"
  7
+    @bad    = ["2015", "01", "01", "failing-test"] * "-"
  8
+  end
  9
+
  10
+  test "file should be existed" do
  11
+    blog = BlogPresenter.new(@good)
  12
+    assert_equal blog.exists?, true
  13
+  end
  14
+
  15
+  test "file should not be existed" do
  16
+    blog  = BlogPresenter.new(@bad)
  17
+    assert_equal blog.exists?, false
  18
+  end
  19
+
  20
+  test "file should have content" do
  21
+    blog = BlogPresenter.new(@good)
  22
+    assert_not_nil blog.render
  23
+  end
  24
+
  25
+  test "file should have no content" do
  26
+    blog = BlogPresenter.new(@bad)
  27
+    assert_nil blog.render
  28
+  end
  29
+
  30
+end

0 notes on commit 25563fc

Please sign in to comment.
Something went wrong with that request. Please try again.