Changeset 945

Show
Ignore:
Timestamp:
11/10/07 23:31:48 (10 months ago)
Author:
iv..@gweezlebur.com
Message:

Merki: page links

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • apps/merki/trunk/app/models/page.rb

    r942 r945  
    22 
    33class Page < DataMapper::Base 
     4  INTERNAL_LINK_REGEX = /\[\[(.*)\]\]/ 
     5   
    46  ## props 
    57  property :title, :string 
     
    1214   
    1315  ## callbacks 
    14   before_save :parse 
     16  before_save :convert_body 
    1517   
    1618  ## validations 
    1719  validates_presence_of :title, :body 
    1820   
     21     
    1922  ## 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 
    2335  end 
    2436   
    2537   
    2638  ## 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) 
    3049    self.converted_body = RedCloth.new(preprocessed_body).to_html 
    3150  end 
     51   
    3252   
    3353  ## Private 
  • apps/merki/trunk/spec/models/page_spec.rb

    r942 r945  
    8080  it "should use textile to convert body to converted_body" do 
    8181    @page.body = textile_text 
    82     @page.parse 
     82    @page.convert_body 
    8383    @page.converted_body.should == textile_text_as_html 
    8484  end 
    8585   
    8686  it "should convert [[page links]] to URLs when the Page exists" do 
    87     pending "Needs implementation"     
     87    # pending "Needs implementation"     
    8888    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>} 
    9191  end 
    9292 
     
    9494    pending "Needs implementation"     
    9595    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>} 
    9898  end 
    9999