From a48a4491c92a7b4e16b744ab28a56b0f88c85ca4 Mon Sep 17 00:00:00 2001 From: Jens-Christian Fischer Date: Sun, 21 Sep 2008 23:08:54 +0200 Subject: [PATCH] + neue permalink methode --- app/controllers/chapters_controller.rb | 3 +- app/controllers/sections_controller.rb | 5 ++-- app/models/chapter.rb | 11 +------- app/models/section.rb | 10 ++----- app/views/chapters/show.html.haml | 2 +- app/views/page/home.html.haml | 6 ++++ app/views/sections/show.html.haml | 2 +- .../shared/_chapter_navigation.html.haml | 2 +- app/views/shared/_navigation.html.haml | 2 +- config/environment.rb | 3 +- config/routes.rb | 1 + db/migrate/018_add_permalink.rb | 11 ++++++++ spec/controllers/chapters_controller_spec.rb | 4 +-- spec/models/chapter_spec.rb | 28 ------------------- 14 files changed, 32 insertions(+), 58 deletions(-) create mode 100644 db/migrate/018_add_permalink.rb diff --git a/app/controllers/chapters_controller.rb b/app/controllers/chapters_controller.rb index a2850b0..d9a39f1 100644 --- a/app/controllers/chapters_controller.rb +++ b/app/controllers/chapters_controller.rb @@ -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 diff --git a/app/controllers/sections_controller.rb b/app/controllers/sections_controller.rb index d007288..8bc3058 100644 --- a/app/controllers/sections_controller.rb +++ b/app/controllers/sections_controller.rb @@ -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 diff --git a/app/models/chapter.rb b/app/models/chapter.rb index 7e84880..22adcbf 100644 --- a/app/models/chapter.rb +++ b/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 diff --git a/app/models/section.rb b/app/models/section.rb index d0c8203..ea19ddf 100644 --- a/app/models/section.rb +++ b/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 diff --git a/app/views/chapters/show.html.haml b/app/views/chapters/show.html.haml index ed8b042..b596820 100644 --- a/app/views/chapters/show.html.haml +++ b/app/views/chapters/show.html.haml @@ -6,5 +6,5 @@ %h2 = "#{@chapter.number} – #{@chapter.title}" #book_content - = @chapter.body + ~ @chapter.body diff --git a/app/views/page/home.html.haml b/app/views/page/home.html.haml index 569dcc1..ff4472d 100644 --- a/app/views/page/home.html.haml +++ b/app/views/page/home.html.haml @@ -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' diff --git a/app/views/sections/show.html.haml b/app/views/sections/show.html.haml index 63a0192..f5d1195 100644 --- a/app/views/sections/show.html.haml +++ b/app/views/sections/show.html.haml @@ -5,5 +5,5 @@ #book %h2= @section.title #book_content - = @section.body + ~ @section.body \ No newline at end of file diff --git a/app/views/shared/_chapter_navigation.html.haml b/app/views/shared/_chapter_navigation.html.haml index 25d460f..e8c48fd 100644 --- a/app/views/shared/_chapter_navigation.html.haml +++ b/app/views/shared/_chapter_navigation.html.haml @@ -13,4 +13,4 @@ %span.number = section.number %span.title - = link_to_unless_current section.title, chapter_section_path(chapter, section) \ No newline at end of file + = link_to_unless_current section.title, section_path(section) \ No newline at end of file diff --git a/app/views/shared/_navigation.html.haml b/app/views/shared/_navigation.html.haml index 3d9cf5c..8572158 100644 --- a/app/views/shared/_navigation.html.haml +++ b/app/views/shared/_navigation.html.haml @@ -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 diff --git a/config/environment.rb b/config/environment.rb index f53d828..301597a 100644 --- a/config/environment.rb +++ b/config/environment.rb @@ -83,4 +83,5 @@ #ActionController::AbstractRequest.relative_url_root = "/referenz2/" #ActionController::CgiRequest.relative_url_root = "/referenz2/" -require 'maruku' \ No newline at end of file +require 'maruku' +require 'permalink' \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 94f216b..a468354 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -35,6 +35,7 @@ map.resources :categories map.resources :erratas + map.resources :sections map.resources :chapters do |chapter| chapter.resources :sections end diff --git a/db/migrate/018_add_permalink.rb b/db/migrate/018_add_permalink.rb new file mode 100644 index 0000000..3b9e3b3 --- /dev/null +++ b/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 diff --git a/spec/controllers/chapters_controller_spec.rb b/spec/controllers/chapters_controller_spec.rb index 4aea64a..53a08a4 100644 --- a/spec/controllers/chapters_controller_spec.rb +++ b/spec/controllers/chapters_controller_spec.rb @@ -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 @@ -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 diff --git a/spec/models/chapter_spec.rb b/spec/models/chapter_spec.rb index 2bf2eef..de50117 100644 --- a/spec/models/chapter_spec.rb +++ b/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 \ No newline at end of file