diff --git a/app/controllers/wiki_controller.rb b/app/controllers/wiki_controller.rb index a3000ed..3395a75 100644 --- a/app/controllers/wiki_controller.rb +++ b/app/controllers/wiki_controller.rb @@ -164,7 +164,7 @@ class WikiController < ApplicationController params[:course_id] = Course.find(:first, :conditions => ['short_name = ?', params[:course_id]], :order => 'period desc').id if !params[:course_id].is_numeric? and !Course.find_by_short_name(params[:course_id]).nil? @course = Course.find(params[:course_id]) - params[:id] = @course.wiki_pages.find_by_title(params[:id]).id if params[:id] and !params[:id].is_numeric? and !@course.wiki_pages.find_by_title(params[:id]).nil? + params[:id] = @course.wiki_pages.find_by_canonical_title(params[:id].pretty_url).id if params[:id] and !params[:id].is_numeric? and !@course.wiki_pages.find_by_canonical_title(params[:id].pretty_url).nil? @wiki_page = params[:id] ? @course.wiki_pages.find(params[:id]) : WikiPage.new(params[:wiki_page]) end diff --git a/app/models/wiki_page.rb b/app/models/wiki_page.rb index d27dc79..30ba4ab 100644 --- a/app/models/wiki_page.rb +++ b/app/models/wiki_page.rb @@ -27,6 +27,7 @@ class WikiPage < ActiveRecord::Base acts_as_paranoid_versioned self.non_versioned_columns << 'position' self.non_versioned_columns << 'deleted_at' + self.non_versioned_columns << 'canonical_title' # Associacoes belongs_to :course @@ -35,8 +36,13 @@ class WikiPage < ActiveRecord::Base # Valicacao generate_validations validates_uniqueness_of :title, :scope => :course_id + validates_uniqueness_of :canonical_title, :scope => :course_id validates_format_of :title, :with => /^[^0-9]/ + def before_validation + self.canonical_title = self.title.pretty_url + end + def validate begin self.content.format_wiki @@ -46,7 +52,7 @@ class WikiPage < ActiveRecord::Base end def to_param - self.title.match(/^[-_a-z0-9]*$/i).nil? ? self.id.to_s : self.title + self.canonical_title end def WikiPage.diff(from, to) diff --git a/config/initializers/nasty_hacks.rb b/config/initializers/nasty_hacks.rb index 59e61bc..2edbb19 100644 --- a/config/initializers/nasty_hacks.rb +++ b/config/initializers/nasty_hacks.rb @@ -16,6 +16,14 @@ class String %w[auto_link excerpt highlight sanitize simple_format strip_tags truncate word_wrap].each do |method| eval "def #{method}(*args); ActionController::Base.helpers.#{method}(self, *args); end" end + + def pretty_url + self.mb_chars.normalize(:kd). + gsub(/[^\x00-\x7F]/n,''). + gsub(/[^a-z._0-9 -]/i,""). + gsub(/ +/,"_"). + downcase.to_s + end end class Array diff --git a/db/migrate/20090907095812_canonical.rb b/db/migrate/20090907095812_canonical.rb new file mode 100644 index 0000000..a496ca0 --- /dev/null +++ b/db/migrate/20090907095812_canonical.rb @@ -0,0 +1,12 @@ +class Canonical< ActiveRecord::Migration + def self.up + add_column :wiki_pages, :canonical_title, :string + WikiPage.find(:all).each do |wiki| + wiki.update_attribute(:canonical_title, wiki.title.pretty_url) + end + end + + def self.down + remove_column :wiki_pages, :canonical_title + end +end diff --git a/db/schema.rb b/db/schema.rb index 404a5f2..fba8f11 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -1,5 +1,5 @@ # This file is auto-generated from the current state of the database. Instead of editing this file, -# please use the migrations feature of ActiveRecord to incrementally modify your database, and +# please use the migrations feature of Active Record to incrementally modify your database, and # then regenerate this schema definition. # # Note that this schema.rb definition is the authoritative source for your database schema. If you need @@ -9,7 +9,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 36) do +ActiveRecord::Schema.define(:version => 20090907095812) do create_table "attachments", :force => true do |t| t.string "file_name", :null => false @@ -102,8 +102,8 @@ ActiveRecord::Schema.define(:version => 36) do t.datetime "updated_at" end - add_index "sessions", ["updated_at"], :name => "index_sessions_on_updated_at" add_index "sessions", ["session_id"], :name => "index_sessions_on_session_id" + add_index "sessions", ["updated_at"], :name => "index_sessions_on_updated_at" create_table "users", :force => true do |t| t.string "login", :null => false @@ -137,16 +137,17 @@ ActiveRecord::Schema.define(:version => 36) do end create_table "wiki_pages", :force => true do |t| - t.integer "course_id", :null => false - t.integer "user_id", :null => false - t.integer "version", :null => false - t.string "description", :null => false - t.string "title", :null => false - t.text "content", :null => false + t.integer "course_id", :null => false + t.integer "user_id", :null => false + t.integer "version", :null => false + t.string "description", :null => false + t.string "title", :null => false + t.text "content", :null => false t.datetime "created_at" t.datetime "updated_at" t.integer "position" t.datetime "deleted_at" + t.string "canonical_title" end end