Skip to content

Commit

Permalink
+ neue permalink methode
Browse files Browse the repository at this point in the history
  • Loading branch information
jcfischer committed Sep 21, 2008
1 parent 6417bc1 commit a48a449
Show file tree
Hide file tree
Showing 14 changed files with 32 additions and 58 deletions.
3 changes: 1 addition & 2 deletions app/controllers/chapters_controller.rb
Expand Up @@ -9,8 +9,7 @@ def index
end

def show
nr = params[:id].split("_").first
@chapter = Chapter.find_by_number(nr)
@chapter = Chapter.find_by_permalink(params[:id])
end

private
Expand Down
5 changes: 2 additions & 3 deletions app/controllers/sections_controller.rb
Expand Up @@ -6,9 +6,8 @@ class SectionsController < ApplicationController

def show
@chapters = Chapter.find(:all)
logger.debug params
title = params[:id]
@section = Section.find_by_title(title)
@section = Section.find_by_permalink(params[:id])
@chapter = @section.chapter
end

end
11 changes: 1 addition & 10 deletions app/models/chapter.rb
@@ -1,19 +1,10 @@
class Chapter < ActiveRecord::Base

has_many :sections
use_permalink :title

def ordinal
number ? "#{number}." : number
end

def to_param
"#{number}_#{slug}"
end

def slug
return '' if title.blank?
slug = title.downcase.gsub(/\s/, '_')

end

end
10 changes: 2 additions & 8 deletions app/models/section.rb
@@ -1,18 +1,12 @@
class Section < ActiveRecord::Base

belongs_to :chapter
use_permalink :title

def ordinal
number ? "#{number}." : number
end

def to_param
"#{slug}"
end

def slug
return '' if title.blank?
slug = title.downcase.gsub(/\s/, '_')
end


end
2 changes: 1 addition & 1 deletion app/views/chapters/show.html.haml
Expand Up @@ -6,5 +6,5 @@
%h2
= "#{@chapter.number} &ndash; #{@chapter.title}"
#book_content
= @chapter.body
~ @chapter.body

6 changes: 6 additions & 0 deletions app/views/page/home.html.haml
Expand Up @@ -6,6 +6,12 @@
Das Praxisbuch für Entwickler

%h2 Neuigkeiten
%p
Das Buch ist neben der PDF Version auch in einer freien HTML Version verfügbar.
= link_to "HTML Version", chapters_path
%p
Das Buch ist jetzt auch als PDF Version verfügbar. Sie können die PDF Version beim mitp Verlag für 28 Euro
= link_to "bestellen", 'http://www.mitp.de/vmi/mitp/detail/pWert/516831/titel/Professionelle%20Webentwicklung%20mit%20Ruby%20on%20Rails%202%20E-BOOK'
%p
Die Einleitung, sowie Kapitel 11, "RSpec und User Stories" sind als PDF erhältlich und können heruntergeladen werden.
= link_to "Kapitel-Downloads", :controller => 'files'
Expand Down
2 changes: 1 addition & 1 deletion app/views/sections/show.html.haml
Expand Up @@ -5,5 +5,5 @@
#book
%h2= @section.title
#book_content
= @section.body
~ @section.body

2 changes: 1 addition & 1 deletion app/views/shared/_chapter_navigation.html.haml
Expand Up @@ -13,4 +13,4 @@
%span.number
= section.number
%span.title
= link_to_unless_current section.title, chapter_section_path(chapter, section)
= link_to_unless_current section.title, section_path(section)
2 changes: 1 addition & 1 deletion app/views/shared/_navigation.html.haml
Expand Up @@ -4,7 +4,7 @@
%li= link_to_unless_current 'Referenz', pages_path
%li= link_to_unless_current 'Errata', erratas_path
%li= link_to_unless_current 'Download', :controller => 'files'
%li= link_to_unless_current 'Chapters', chapters_path
%li= link_to_unless_current 'Buch', chapters_path
- if current_user && current_user.has_role?('system_admin')
%li= link_to 'Edit Chapters', admin_chapters_path

Expand Down
3 changes: 2 additions & 1 deletion config/environment.rb
Expand Up @@ -83,4 +83,5 @@
#ActionController::AbstractRequest.relative_url_root = "/referenz2/"
#ActionController::CgiRequest.relative_url_root = "/referenz2/"

require 'maruku'
require 'maruku'
require 'permalink'
1 change: 1 addition & 0 deletions config/routes.rb
Expand Up @@ -35,6 +35,7 @@

map.resources :categories
map.resources :erratas
map.resources :sections
map.resources :chapters do |chapter|
chapter.resources :sections
end
Expand Down
11 changes: 11 additions & 0 deletions db/migrate/018_add_permalink.rb
@@ -0,0 +1,11 @@
class AddPermalink < ActiveRecord::Migration
def self.up
add_column :chapters, :permalink, :string
add_column :sections, :permalink, :string
end

def self.down
remove_column :sections, :permalink
remove_column :chapters, :permalink
end
end
4 changes: 2 additions & 2 deletions spec/controllers/chapters_controller_spec.rb
Expand Up @@ -41,7 +41,7 @@ def do_get
before(:each) do
@chapter = mock_model(Chapter, :number => '2')
Chapter.stub!(:find).and_return([@chapter])
Chapter.stub!(:find_by_number).and_return(@chapter)
Chapter.stub!(:find_by_permalink).and_return(@chapter)
end

def do_get
Expand All @@ -68,7 +68,7 @@ def do_get
end

it "should call the find method of the chapter class" do
Chapter.should_receive(:find_by_number).with("2").and_return(@chapter)
Chapter.should_receive(:find_by_permalink).with("2_einfuhrung").and_return(@chapter)
do_get
end

Expand Down
28 changes: 0 additions & 28 deletions spec/models/chapter_spec.rb
@@ -1,30 +1,2 @@
require File.dirname(__FILE__) + '/../spec_helper'

describe Chapter do
before(:each) do
@chapter = Chapter.new(:number => '2')
end

it "should have to_param method that returns chapter number" do
@chapter.to_param.should == "2_"
end
it "should convert title to slug" do
@chapter.slug.should_not be_nil
end

end

describe Chapter, "slug" do
before(:each) do
@chapter = Chapter.new(:number => '2', :title => "Einführung ins Testen")
end

it "should convert title to slug" do
@chapter.slug.should_not be_nil
end

it "should convert spaces to _" do
@chapter.slug.should == "einführung_ins_testen"
end

end

0 comments on commit a48a449

Please sign in to comment.