Skip to content

Commit

Permalink
Move page? to the business logic
Browse files Browse the repository at this point in the history
  • Loading branch information
jimweirich committed Feb 19, 2014
1 parent 0e80bf1 commit 217622c
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 16 deletions.
1 change: 1 addition & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ def run(klass, *args, &block)
def repo
@repo = WikiRepository.new
end
helper_method :repo

def current_user
@current_user ||= begin
Expand Down
6 changes: 5 additions & 1 deletion app/models/biz/page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@

module Biz
class Page < Model
def wiki
Biz::Wiki.wrap(super)
end

def html_content(context)
Kramdown::Document.new(referenced_content(context)).to_html
end

def referenced_content(context)
content.gsub(/(([A-Z][a-z0-9]+){2,})/) { |page_name|
if wiki.page?(page_name)
if wiki.page?(context.repo, page_name)
"[#{page_name}](#{context.named_page_path(wiki.name,page_name)})"
elsif context.current_user.can_write?(wiki)
"#{page_name}[?](#{context.new_named_page_path(wiki.name, page_name)})"
Expand Down
6 changes: 6 additions & 0 deletions app/models/biz/wiki.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,11 @@

module Biz
class Wiki < Model

# Is the page in the defined in the repository?
def page?(repo, page_name)
page = repo.find_named_page(data.name, page_name)
! page.nil?
end
end
end
5 changes: 0 additions & 5 deletions app/models/wiki.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,4 @@ class Wiki < ActiveRecord::Base
def self.active_wikis
all.order("name")
end

def page?(page_name)
page = pages.where("name = ?", page_name).first
! page.nil?
end
end
8 changes: 7 additions & 1 deletion spec/models/biz/page_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,16 @@
Given(:page) { Biz::Page.wrap(page_data) }

describe "#html_content" do
Given(:found_page) { "PAGE" }
Given(:writer) { true }
Given(:user) { double(:can_write? => writer) }
Given(:repo) { double(:find_named_page => found_page) }
Given(:context) {
double(new_named_page_path: "MISSING", named_page_path: "EXISTING", current_user: user)
double(
new_named_page_path: "MISSING",
named_page_path: "EXISTING",
current_user: user,
repo: repo)
}

describe "basic styling" do
Expand Down
20 changes: 20 additions & 0 deletions spec/models/biz/wiki_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
require 'fast_helper'
require 'app/models/biz/wiki'

describe Biz::Wiki do

Given(:wiki_data) { double(Attrs.wiki(:biz? => false)) }
Given(:wiki) { Biz::Wiki.new(wiki_data) }

describe "#page?" do
context "with existing page" do
Given(:repo) { double(:find_named_page => :page) }
Then { wiki.page?(repo, "PageName") }
end
context "with non-existing page" do
Given(:repo) { double(:find_named_page => nil) }
Then { ! wiki.page?(repo, "PagName") }
end
end

end
9 changes: 0 additions & 9 deletions spec/models/wiki_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,4 @@
end
end

describe "#page?" do
Given {
wiki.save
wiki.pages.create(Attrs.page)
}
Then { wiki.page?(Attrs.page[:name]) }
Then { ! wiki.page?("AnotherName") }
end

end

0 comments on commit 217622c

Please sign in to comment.