Changeset 945
- Timestamp:
- 11/10/07 23:31:48 (10 months ago)
- Files:
-
- apps/merki/trunk/LICENSE (added)
- apps/merki/trunk/README (added)
- apps/merki/trunk/app/models/page.rb (modified) (2 diffs)
- apps/merki/trunk/spec/models/page_spec.rb (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
apps/merki/trunk/app/models/page.rb
r942 r945 2 2 3 3 class Page < DataMapper::Base 4 INTERNAL_LINK_REGEX = /\[\[(.*)\]\]/ 5 4 6 ## props 5 7 property :title, :string … … 12 14 13 15 ## callbacks 14 before_save : parse16 before_save :convert_body 15 17 16 18 ## validations 17 19 validates_presence_of :title, :body 18 20 21 19 22 ## class methods 20 21 def self.find_by_slug(slug) 22 find_by_title(CGI.unescape(slug)) 23 class << self 24 def find_by_slug(slug) 25 find_by_title(unescape(slug)) || find_by_title(slug) 26 end 27 28 def escape(string) 29 CGI.escape(string) 30 end 31 32 def unescape(string) 33 CGI.unescape(string) 34 end 23 35 end 24 36 25 37 26 38 ## instance methods 27 28 def parse(preprocessed_body = self.body) 29 ## TODO: parse for internal links 39 def to_html 40 ## TODO: look up page first 41 converted_body.gsub(INTERNAL_LINK_REGEX) { |s| 42 page = $1 43 link = Page.escape(page) 44 %Q{<a href="/pages/#{link}">#{page}</a>} 45 } 46 end 47 48 def convert_body(preprocessed_body = self.body) 30 49 self.converted_body = RedCloth.new(preprocessed_body).to_html 31 50 end 51 32 52 33 53 ## Private apps/merki/trunk/spec/models/page_spec.rb
r942 r945 80 80 it "should use textile to convert body to converted_body" do 81 81 @page.body = textile_text 82 @page. parse82 @page.convert_body 83 83 @page.converted_body.should == textile_text_as_html 84 84 end 85 85 86 86 it "should convert [[page links]] to URLs when the Page exists" do 87 pending "Needs implementation"87 # pending "Needs implementation" 88 88 Page.stub!(:find_by_title).and_return(true) 89 @page. body = "[[Some Page]]"90 @page.to_html.should match(%r{<a href="/pages/Some+Page">Some Page</a>})89 @page.converted_body = "[[Some Page]]" 90 @page.to_html.should == %q{<a href="/pages/Some+Page">Some Page</a>} 91 91 end 92 92 … … 94 94 pending "Needs implementation" 95 95 Page.stub!(:find_by_title).and_return(false) 96 @page. body = "[[Some Page]]"97 @page.to_html.should match(%r{<a href="/pages/Some+Page">Some Page\?</a>})96 @page.converted_body = "[[Some Page]]" 97 @page.to_html.should == %q{<a href="/pages/Some+Page">Some Page?</a>} 98 98 end 99 99
