Skip to content

Commit

Permalink
Fix wikipage slugs and urls with friendlyid 4
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesu committed Oct 5, 2011
1 parent bf72c97 commit 2a09b14
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 11 deletions.
6 changes: 3 additions & 3 deletions app/controllers/wiki_pages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def create

if @wiki_page.save
flash[:message] = I18n.t 'wiki_engine.success_creating_wiki_page'
redirect_to @wiki_page.main ? wiki_pages_path : wiki_page_path(:id => @wiki_page)
redirect_to @wiki_page.main ? wiki_pages_path : wiki_page_path(:id => @wiki_page.slug)
else
render :action => 'new'
end
Expand Down Expand Up @@ -140,11 +140,11 @@ def find_main_wiki_page
end

def find_wiki_page
@wiki_page = wiki_pages.where(:project_id => @active_project.id).find_by_title(params[:id])
@wiki_page = wiki_pages.where(:project_id => @active_project.id).find_by_slug(params[:id])
end

def find_sidebar_page
@wiki_sidebar = wiki_pages.where(:project_id => @active_project.id).find_by_title("sidebar") rescue nil
@wiki_sidebar = wiki_pages.where(:project_id => @active_project.id).find_by_slug("sidebar") rescue nil
@content_for_sidebar = @wiki_sidebar.nil? ? nil : 'wiki_sidebar'
end
end
4 changes: 2 additions & 2 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,8 @@ def actions_for_time_short(time)
end

def actions_for_wiki_page(page)
[{:name => I18n.t('edit'), :url => {:controller => 'wiki_pages', :action => 'edit', :id => page}, :cond => can?(:edit,page)},
{:name => I18n.t('delete'), :url => {:controller => 'wiki_pages', :action => 'destroy', :id => page}, :cond => can?(:delete,page), :method => :delete, :confirm => I18n.t('wiki_page_confirm_delete')}]
[{:name => I18n.t('edit'), :url => {:controller => 'wiki_pages', :action => 'edit', :id => page.slug}, :cond => can?(:edit,page)},
{:name => I18n.t('delete'), :url => {:controller => 'wiki_pages', :action => 'destroy', :id => page.slug}, :cond => can?(:delete,page), :method => :delete, :confirm => I18n.t('wiki_page_confirm_delete')}]
end

def running_time_for_task(task)
Expand Down
2 changes: 1 addition & 1 deletion app/helpers/wiki_pages_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,6 @@ def wiki_id_to_title(id)
end

def wiki_title_to_id(title)
WikiPage.new(:title => title).slug_text
WikiPage.new(:title => title).normalize_friendly_id(title)
end
end
4 changes: 3 additions & 1 deletion app/models/wiki_page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class WikiPage < ActiveRecord::Base

before_save :set_main_page
acts_as_versioned :extend => WikiPageUser
friendly_id :title, :use => :slugged, :slug_column => :title
friendly_id :title, :use => :slugged
validates_presence_of :title

def title_from_id=(id)
Expand All @@ -14,6 +14,8 @@ def title_from_id=(id)

belongs_to :project
self.non_versioned_columns << :project_id
self.non_versioned_columns << :title
self.non_versioned_columns << :slug
scope :main, lambda{ |project| where(:main => true, :project_id => project.id) }

after_create :process_create
Expand Down
2 changes: 1 addition & 1 deletion app/views/wiki_pages/_list.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<tbody>
<% list.each do |wiki_page| %>
<tr class="<%= cycle('odd', 'even') %>">
<td><%= link_to h(wiki_page.title), wiki_page_path(:id => wiki_page) %></td>
<td><%= link_to h(wiki_page.title), wiki_page_path(:id => wiki_page.slug) %></td>
<td><%= wiki_page.updated_at.to_s(:long) %></td>
<td class="operations"><%= render :partial => 'operations', :locals => {:wiki_page => wiki_page} %></td>
</tr>
Expand Down
2 changes: 1 addition & 1 deletion app/views/wiki_pages/_widget.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<ul>
<% wiki_pages.each do |wiki_page| %>
<li><%= link_to_unless_current h(wiki_page.title), wiki_page_path(wiki_page) %></li>
<li><%= link_to_unless_current h(wiki_page.title), wiki_page_path(:id => wiki_page.slug)) %></li>
<% end %>
</ul>
</div>
Expand Down
2 changes: 1 addition & 1 deletion app/views/wiki_pages/edit.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<%= form_for(@wiki_page, :url => wiki_page_path(:id => @wiki_page)) do |form| %>
<%= form_for(@wiki_page, :url => wiki_page_path(:id => @wiki_page.slug)) do |form| %>
<%= render :partial => form %>
<div class="buttons">
<button class="submit" type="submit"><%= t('edit_page') %></button>
Expand Down
32 changes: 32 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,38 @@ en:

product_signature: "Powered by <a href=\"http://rubyforge.org/projects/railscollab\">RailsCollab</a>"

wiki_engine:
success_creating_wiki_page: 'New wiki page has been created'
success_updating_wiki_page: 'The wiki page has been updated'
success_deleting_wiki_page: 'The wiki page has been deleted'
preview: 'Preview'
current_version: 'Current'
current_version_edited_by: 'Current by %{user}'
version: 'Version %{version}'
version_edited_by: 'Version %{version} by %{user}'
title: 'Title'
content: 'Content'
main_page: 'Main page'
create: 'Create'
update: 'Update'
cancel: 'Cancel'
new: 'New page'
edit: 'Edit'
delete: 'Delete'
confirm_deletion: 'Are you sure?'
no_main_page: 'There is no main page yet'
last_modified: 'Last modified'
last_modified_with_time: 'Last modified %{time}'
not_found: 'Page not found'
non_existing_page: 'This wiki page does not exist yet.'
create_it: 'Create it'
pages: 'Pages'
help:
link_to_format: '%{link} is supported. You can also use raw HTML.'
links_to_wiki_pages: 'Links to other wiki pages'
format_code: 'Format source code samples like this'
highlight_language: 'Specify programming language in the sample with <code>class="language"</code> on <code>code</code> element.'

# rails
date:
formats:
Expand Down
21 changes: 21 additions & 0 deletions db/migrate/20111005191500_kill_slugs.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class PageFix < ActiveRecord::Base
set_table_name 'wiki_pages'
end

class SlugFix < ActiveRecord::Base
set_table_name 'slugs'
end

class KillSlugs < ActiveRecord::Migration
def up
add_column :wiki_pages, :slug, :string
add_index :wiki_pages, :slug
PageFix.all.each do |page|
page.slug = SlugFix.where(:sluggable_type => 'WikiPage', :sluggable_id => page.id).first.try(:name)
end
end

def down
remove_column :wiki_pages, :slug
end
end
4 changes: 3 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 20111004100837) do
ActiveRecord::Schema.define(:version => 20111005191500) do

create_table "activities", :force => true do |t|
t.integer "project_id", :default => 0, :null => false
Expand Down Expand Up @@ -406,9 +406,11 @@
t.integer "version"
t.integer "project_id"
t.integer "created_by_id"
t.string "slug"
end

add_index "wiki_pages", ["main"], :name => "index_wiki_pages_on_main"
add_index "wiki_pages", ["project_id"], :name => "index_wiki_pages_on_project_id"
add_index "wiki_pages", ["slug"], :name => "index_wiki_pages_on_slug"

end

0 comments on commit 2a09b14

Please sign in to comment.