From 3fb15f81a59f1577b72be88b5d31c07727ae07f6 Mon Sep 17 00:00:00 2001 From: lukemelia Date: Sun, 14 Jan 2007 19:18:07 +0000 Subject: [PATCH] Convert context_controller to use RESTful routes (required renaming contexts_controller -> contexts_controller per rails convention). This change also changes context detail page URLs from /context/my_context to /contexts/my_context Add a database index on the projects and contexts tables, user_d + name, to speed the lookup used in urls Brought the URLs within various feeds up-to-date git-svn-id: http://www.rousette.org.uk/svn/tracks-repos/trunk@399 a4c988fc-2ded-0310-b66e-134b36920a42 --- ...t_controller.rb => contexts_controller.rb} | 23 +++++---- tracks/app/helpers/application_helper.rb | 4 +- .../{context_helper.rb => contexts_helper.rb} | 4 +- tracks/app/models/context.rb | 4 ++ .../app/views/context/_context_listing.rhtml | 48 ------------------- tracks/app/views/context/update.rjs | 2 - .../{context => contexts}/_context.rhtml | 0 .../{context => contexts}/_context_form.rhtml | 0 .../app/views/contexts/_context_listing.rhtml | 46 ++++++++++++++++++ .../{context => contexts}/_edit_context.rhtml | 0 .../views/{context => contexts}/create.rjs | 0 .../app/views/{context => contexts}/error.rjs | 0 .../views/{context => contexts}/index.rhtml | 20 ++++---- .../views/{context => contexts}/show.rhtml | 2 +- tracks/app/views/contexts/update.rjs | 2 + tracks/app/views/feed/contexts_rss.rxml | 4 +- tracks/app/views/feed/projects_rss.rxml | 4 +- tracks/app/views/feed/rss.rxml | 6 +-- tracks/app/views/layouts/standard.rhtml | 2 +- tracks/app/views/note/_notes.rhtml | 2 +- tracks/app/views/projects/update.rjs | 2 +- tracks/app/views/todo/create.rjs | 2 +- tracks/app/views/todo/index.rhtml | 2 +- tracks/config/routes.rb | 7 +-- .../migrate/024_add_find_by_name_indices.rb | 11 +++++ ...er_test.rb => contexts_controller_test.rb} | 10 ++-- .../functional/projects_controller_test.rb | 6 +-- .../test/integration/context_xml_api_test.rb | 12 ++--- .../test/selenium/context_listing/delete.rsel | 6 +++ tracks/test/unit/context_test.rb | 4 ++ 30 files changed, 122 insertions(+), 113 deletions(-) rename tracks/app/controllers/{context_controller.rb => contexts_controller.rb} (91%) rename tracks/app/helpers/{context_helper.rb => contexts_helper.rb} (67%) delete mode 100644 tracks/app/views/context/_context_listing.rhtml delete mode 100644 tracks/app/views/context/update.rjs rename tracks/app/views/{context => contexts}/_context.rhtml (100%) rename tracks/app/views/{context => contexts}/_context_form.rhtml (100%) create mode 100644 tracks/app/views/contexts/_context_listing.rhtml rename tracks/app/views/{context => contexts}/_edit_context.rhtml (100%) rename tracks/app/views/{context => contexts}/create.rjs (100%) rename tracks/app/views/{context => contexts}/error.rjs (100%) rename tracks/app/views/{context => contexts}/index.rhtml (66%) rename tracks/app/views/{context => contexts}/show.rhtml (78%) create mode 100644 tracks/app/views/contexts/update.rjs create mode 100644 tracks/db/migrate/024_add_find_by_name_indices.rb rename tracks/test/functional/{context_controller_test.rb => contexts_controller_test.rb} (84%) create mode 100644 tracks/test/selenium/context_listing/delete.rsel diff --git a/tracks/app/controllers/context_controller.rb b/tracks/app/controllers/contexts_controller.rb similarity index 91% rename from tracks/app/controllers/context_controller.rb rename to tracks/app/controllers/contexts_controller.rb index f890923d..5351fe7f 100644 --- a/tracks/app/controllers/context_controller.rb +++ b/tracks/app/controllers/contexts_controller.rb @@ -1,4 +1,4 @@ -class ContextController < ApplicationController +class ContextsController < ApplicationController helper :todo @@ -13,9 +13,6 @@ class ContextController < ApplicationController end end - # Filter the projects to show just the one passed in the URL - # e.g. /context/ shows just . - # def show @page_title = "TRACKS::Context: #{@context.name}" end @@ -23,7 +20,7 @@ class ContextController < ApplicationController # Example XML usage: curl -H 'Accept: application/xml' -H 'Content-Type: application/xml' # -u username:password # -d 'new context_name' - # http://our.tracks.host/context/create + # http://our.tracks.host/contexts # def create if params[:format] == 'application/xml' && params['exception'] @@ -101,19 +98,21 @@ class ContextController < ApplicationController protected def check_user_set_context - if params["url_friendly_name"] - @context = @user.contexts.find_by_url_friendly_name(params["url_friendly_name"]) + if params['url_friendly_name'] + @context = @user.contexts.find_by_url_friendly_name(params['url_friendly_name']) + elsif params['id'] && params['id'] =~ /\d+/ + @context = @user.contexts.find(params['id']) elsif params['id'] - @context = @user.contexts.find(params["id"]) + @context = @user.contexts.find_by_url_friendly_name(params['id']) else redirect_to :action => 'index' end - if @user == @context.user + if @context && @user == @context.user return @context else @context = nil # Should be nil anyway. notify :warning, "Item and session user mis-match: #{@context.user_id} and #{@user.id}!" - render_text "" + render :text => '' end end @@ -124,7 +123,7 @@ class ContextController < ApplicationController else @context = nil notify :warning, "Project and session user mis-match: #{@context.user_id} and #{@user.id}!" - render_text "" + render :text => '' end end @@ -134,7 +133,7 @@ class ContextController < ApplicationController return item else notify :warning, "Item and session user mis-match: #{item.user.name} and #{@user.name}!" - render_text "" + render :text => '' end end diff --git a/tracks/app/helpers/application_helper.rb b/tracks/app/helpers/application_helper.rb index a9e14903..db40ffed 100644 --- a/tracks/app/helpers/application_helper.rb +++ b/tracks/app/helpers/application_helper.rb @@ -129,11 +129,11 @@ module ApplicationHelper end def link_to_context(context, descriptor = sanitize(context.name)) - link_to( descriptor, { :controller => "context", :action => "show", :url_friendly_name => context.url_friendly_name }, :title => "View context: #{context.name}" ) + link_to( descriptor, context_path(context), :title => "View context: #{context.name}" ) end def link_to_project(project, descriptor = sanitize(project.name)) - link_to( descriptor, project_url(project), :title => "View project: #{project.name}" ) + link_to( descriptor, project_path(project), :title => "View project: #{project.name}" ) end def item_link_to_context(item) diff --git a/tracks/app/helpers/context_helper.rb b/tracks/app/helpers/contexts_helper.rb similarity index 67% rename from tracks/app/helpers/context_helper.rb rename to tracks/app/helpers/contexts_helper.rb index 71e1485d..abe5299b 100644 --- a/tracks/app/helpers/context_helper.rb +++ b/tracks/app/helpers/contexts_helper.rb @@ -1,11 +1,11 @@ -module ContextHelper +module ContextsHelper def get_listing_sortable_options { :tag => 'div', :handle => 'handle', :complete => visual_effect(:highlight, 'list-contexts'), - :url => {:controller => 'context', :action => 'order'} + :url => order_contexts_path } end diff --git a/tracks/app/models/context.rb b/tracks/app/models/context.rb index 5ca18ed8..041abb11 100644 --- a/tracks/app/models/context.rb +++ b/tracks/app/models/context.rb @@ -19,5 +19,9 @@ class Context < ActiveRecord::Base def hidden? self.hide == true end + + def to_param + url_friendly_name + end end diff --git a/tracks/app/views/context/_context_listing.rhtml b/tracks/app/views/context/_context_listing.rhtml deleted file mode 100644 index e85801ca..00000000 --- a/tracks/app/views/context/_context_listing.rhtml +++ /dev/null @@ -1,48 +0,0 @@ -<% context = context_listing %> -
- -
-
- DRAG -
-
- <%= link_to_context( context ) %> - <%= " (" + count_undone_todos(context,"actions") + ")" %> -
- -
-<% if context.hide? %> - HIDDEN -<% else %> - VISIBLE -<% end %> - -<%= link_to_remote( image_tag("blank.png", :title =>"Delete context", :class=>"delete_item"), - :loading => visual_effect(:fade, "container_#{context.id}"), - :url => { :controller => "context", :action => "destroy", :id => context.id }, - :confirm => "Are you sure that you want to delete the context \'#{context.name}\'?" ) + " " + - link_to_function( image_tag( "blank.png", :title => "Edit context", :class=>"edit_item"), "Element.toggle('context-#{context.id}');Element.toggle('context-#{context.id}-edit-form'); new Effect.Appear('context-#{context.id}-edit-form'); Form.focusFirstElement('form-context-#{context.id}');" ) %> -
-
- - -
-<% if controller.action_name == 'create' %> - -<% end %> diff --git a/tracks/app/views/context/update.rjs b/tracks/app/views/context/update.rjs deleted file mode 100644 index 6c6bcc9b..00000000 --- a/tracks/app/views/context/update.rjs +++ /dev/null @@ -1,2 +0,0 @@ -page.replace_html "container_#{@context.id}", :partial => 'context_listing', :object => @context -page.sortable "list-contexts", get_listing_sortable_options \ No newline at end of file diff --git a/tracks/app/views/context/_context.rhtml b/tracks/app/views/contexts/_context.rhtml similarity index 100% rename from tracks/app/views/context/_context.rhtml rename to tracks/app/views/contexts/_context.rhtml diff --git a/tracks/app/views/context/_context_form.rhtml b/tracks/app/views/contexts/_context_form.rhtml similarity index 100% rename from tracks/app/views/context/_context_form.rhtml rename to tracks/app/views/contexts/_context_form.rhtml diff --git a/tracks/app/views/contexts/_context_listing.rhtml b/tracks/app/views/contexts/_context_listing.rhtml new file mode 100644 index 00000000..95aa98ed --- /dev/null +++ b/tracks/app/views/contexts/_context_listing.rhtml @@ -0,0 +1,46 @@ +<% context = context_listing %> +
" class="list"> +
+
+ DRAG +
+
+ <%= link_to_context( context ) %> <%= " (" + count_undone_todos(context,"actions") + ")" %> +
+ +
+ <% if context.hide? %> + HIDDEN + <% else %> + VISIBLE + <% end %> + <%= link_to_remote( image_tag("blank.png", :title =>"Delete context", :class=>"delete_item"), + :update => dom_id(context, "container"), + :loading => visual_effect(:fade, dom_id(context, 'container')), + :url => context_path(context), + :method => :delete, + :confirm => "Are you sure that you want to delete the context \'#{context.name}\'?" ) %> + <%= link_to_function( image_tag( "blank.png", :title => "Edit context", :class=>"edit_item"), "Element.toggle('#{dom_id(context)}');Element.toggle('#{dom_id(context, 'edit')}'); new Effect.Appear('#{dom_id(context, 'edit')}'); Form.focusFirstElement('#{dom_id(context, 'edit_form')}');" ) %> +
+
+ +
+<% if controller.action_name == 'create' %> + +<% end %> \ No newline at end of file diff --git a/tracks/app/views/context/_edit_context.rhtml b/tracks/app/views/contexts/_edit_context.rhtml similarity index 100% rename from tracks/app/views/context/_edit_context.rhtml rename to tracks/app/views/contexts/_edit_context.rhtml diff --git a/tracks/app/views/context/create.rjs b/tracks/app/views/contexts/create.rjs similarity index 100% rename from tracks/app/views/context/create.rjs rename to tracks/app/views/contexts/create.rjs diff --git a/tracks/app/views/context/error.rjs b/tracks/app/views/contexts/error.rjs similarity index 100% rename from tracks/app/views/context/error.rjs rename to tracks/app/views/contexts/error.rjs diff --git a/tracks/app/views/context/index.rhtml b/tracks/app/views/contexts/index.rhtml similarity index 66% rename from tracks/app/views/context/index.rhtml rename to tracks/app/views/contexts/index.rhtml index 9232e6c5..2897d7ae 100644 --- a/tracks/app/views/context/index.rhtml +++ b/tracks/app/views/contexts/index.rhtml @@ -9,21 +9,17 @@
Create new context »
\ No newline at end of file diff --git a/tracks/app/views/context/show.rhtml b/tracks/app/views/contexts/show.rhtml similarity index 78% rename from tracks/app/views/context/show.rhtml rename to tracks/app/views/contexts/show.rhtml index e0a3f013..fd7bd72e 100644 --- a/tracks/app/views/context/show.rhtml +++ b/tracks/app/views/contexts/show.rhtml @@ -1,5 +1,5 @@
-<%= render :partial => "context/context", :locals => { :context => @context, :collapsible => false } %> +<%= render :partial => "contexts/context", :locals => { :context => @context, :collapsible => false } %> <%= render :partial => "todo/completed", :locals => { :done => @done, :collapsible => false, :append_descriptor => "in this context (last #{@user.prefs.show_number_completed})" } %>
diff --git a/tracks/app/views/contexts/update.rjs b/tracks/app/views/contexts/update.rjs new file mode 100644 index 00000000..3680032b --- /dev/null +++ b/tracks/app/views/contexts/update.rjs @@ -0,0 +1,2 @@ +page.replace_html dom_id(@context, 'container'), :partial => 'context_listing', :object => @context +page.sortable "list-contexts", get_listing_sortable_options \ No newline at end of file diff --git a/tracks/app/views/feed/contexts_rss.rxml b/tracks/app/views/feed/contexts_rss.rxml index 46a9f693..82703c7c 100644 --- a/tracks/app/views/feed/contexts_rss.rxml +++ b/tracks/app/views/feed/contexts_rss.rxml @@ -2,12 +2,12 @@ headers["Content-Type"] = "text/xml; charset=utf-8" xml.rss("version" => "2.0", "xmlns:dc" => "http://purl.org/dc/elements/1.1/") do xml.channel do xml.title(@title) - xml.link("http://#{@request.host}:#{@request.port}/contexts") + xml.link(contexts_url) xml.description(@description) @contexts.each do |c| xml.item do xml.title(c.name) - xml.link(url_for(:only_path => false, :controller => 'context', :action => 'show', :url_friendly_name => c.url_friendly_name)) + xml.link(context_url(c)) context_description = '' context_description += "

#{count_undone_todos(c)}. " context_description += "Context is #{c.hidden? ? 'Hidden' : 'Active'}. " diff --git a/tracks/app/views/feed/projects_rss.rxml b/tracks/app/views/feed/projects_rss.rxml index 06de4788..98b24906 100644 --- a/tracks/app/views/feed/projects_rss.rxml +++ b/tracks/app/views/feed/projects_rss.rxml @@ -2,12 +2,12 @@ xml.rss("version" => "2.0", "xmlns:dc" => "http://purl.org/dc/elements/1.1/") do xml.channel do xml.title(@title) - xml.link("http://#{@request.host}:#{@request.port}/projects") + xml.link(projects_url) xml.description(@description) @projects.each do |p| xml.item do xml.title(p.name) - xml.link(url_for(:only_path => false, :controller => 'project', :action => 'show', :url_friendly_name => p.url_friendly_name)) + xml.link(project_url(p)) project_description = '' project_description += sanitize(markdown( p.description )) if p.description_present? project_description += "

#{count_undone_todos(p)}. " diff --git a/tracks/app/views/feed/rss.rxml b/tracks/app/views/feed/rss.rxml index 42ab94f5..bb47cf0a 100644 --- a/tracks/app/views/feed/rss.rxml +++ b/tracks/app/views/feed/rss.rxml @@ -6,15 +6,15 @@ xml.rss("version" => "2.0", "xmlns:dc" => "http://purl.org/dc/elements/1.1/") do @todos.each do |i| xml.item do xml.title(i.description) - xml.link(url_for(:only_path => false, :controller => 'context', :action => 'show', :url_friendly_name => i.context.url_friendly_name)) + xml.link(context_url(i.context)) item_notes = sanitize(markdown( i.notes )) if i.notes? due = "

Due: #{format_date(i.due)}
\n" if i.due? toggle_link = link_to( "mark as done", {:only_path => false, :controller => "todo", :action => "toggle_check", :id => i.id}) done = "
#{toggle_link}
" unless i.completed? done = "
Completed: #{format_date(i.completed_at)}
\n" if i.completed? - context_link = link_to( i.context.name, { :only_path => false, :controller => "context", :action => "show", :url_friendly_name => i.context.url_friendly_name } ) + context_link = link_to( i.context.name, context_url(i.context) ) if i.project_id? - project_link = link_to (i.project.name, { :only_path => false, :controller => "project", :action => "show", :url_friendly_name => i.project.url_friendly_name} ) + project_link = link_to (i.project.name, project_url(i.project) ) else project_link = "none" end diff --git a/tracks/app/views/layouts/standard.rhtml b/tracks/app/views/layouts/standard.rhtml index 4fad9c15..9f50b126 100644 --- a/tracks/app/views/layouts/standard.rhtml +++ b/tracks/app/views/layouts/standard.rhtml @@ -41,7 +41,7 @@