diff --git a/tracks/app/controllers/context_controller.rb b/tracks/app/controllers/context_controller.rb index 019ddc38..030c65aa 100644 --- a/tracks/app/controllers/context_controller.rb +++ b/tracks/app/controllers/context_controller.rb @@ -35,16 +35,10 @@ class ContextController < ApplicationController # Creates a new context via Ajax helpers # def new_context - context = @user.contexts.build - context.attributes = params['context'] - context.name = deurlize(context.name) - - if context.save - render :partial => 'context_listing', :locals => { :context_listing => context } - else - flash["warning"] = "Couldn't add new context" - render :text => "#{flash["warning"]}" - end + @context = @user.contexts.build + @context.attributes = params['context'] + @context.name = deurlize(@context.name) + @saved = @context.save end # Called by a form button diff --git a/tracks/app/helpers/context_helper.rb b/tracks/app/helpers/context_helper.rb index bdd0d8ea..71e1485d 100644 --- a/tracks/app/helpers/context_helper.rb +++ b/tracks/app/helpers/context_helper.rb @@ -1,2 +1,12 @@ module ContextHelper + + def get_listing_sortable_options + { + :tag => 'div', + :handle => 'handle', + :complete => visual_effect(:highlight, 'list-contexts'), + :url => {:controller => 'context', :action => 'order'} + } + end + end diff --git a/tracks/app/models/context.rb b/tracks/app/models/context.rb index 8944364a..329abc4a 100644 --- a/tracks/app/models/context.rb +++ b/tracks/app/models/context.rb @@ -7,10 +7,11 @@ class Context < ActiveRecord::Base attr_protected :user # Context name must not be empty - # and must be less than 255 bytes + # and must be less than 256 characters validates_presence_of :name, :message => "context must have a name" - validates_length_of :name, :maximum => 255, :message => "context name must be less than %d" + validates_length_of :name, :maximum => 255, :message => "context name must be less than 256 characters" validates_uniqueness_of :name, :message => "already exists", :scope => "user_id" + validates_format_of :name, :with => /^[^\/]*$/i, :message => "cannot contain the slash ('/') character" def self.list_of(hidden=false) find(:all, :conditions => [ "hide = ?" , hidden ], :order => "position ASC") diff --git a/tracks/app/views/context/list.rhtml b/tracks/app/views/context/list.rhtml index 66c29c07..39faec8c 100644 --- a/tracks/app/views/context/list.rhtml +++ b/tracks/app/views/context/list.rhtml @@ -1,48 +1,32 @@ -
- - <% for name in ["notice", "warning", "message"] %> - <% if flash[name] %> - <%= "
#{flash[name]}
" %> - <% end %> +
+ <% for name in ["notice", "warning", "message"] %> +
><%= flash[name] %>
<% end %> -
-<% for context in @contexts %> - <%= render_partial( 'context_listing', context ) %> -<% end %> -
-<% sortable_options = { - :tag => 'div', - :handle => 'handle', - :complete => visual_effect(:highlight, 'list-contexts'), - :url => {:controller => 'context', :action => 'order'} - }%> -<%= sortable_element 'list-contexts', sortable_options %> -
-Create new context » - -<% if flash["confirmation"] %> -
<%= flash["confirmation"] %>
-<% end %> -<% if flash["warning"] %> -
<%= flash["warning"] %>
-<% end %> - -
\ No newline at end of file +
+ Create new context » + +
\ No newline at end of file diff --git a/tracks/app/views/context/new_context.rjs b/tracks/app/views/context/new_context.rjs new file mode 100644 index 00000000..6e8b3e07 --- /dev/null +++ b/tracks/app/views/context/new_context.rjs @@ -0,0 +1,11 @@ +if @saved + page.hide "warning" + page.insert_html :bottom, "list-contexts", :partial => 'context_listing', :locals => { :context_listing => @context } + page.sortable "list-contexts", get_listing_sortable_options + page.call "Form.reset", "context-form" + page.call "Form.focusFirstElement", "context-form" +else + page.hide "warning" + page.replace_html "warning", content_tag("div", content_tag("h2", "#{pluralize(@context.errors.count, "error")} prohibited this record from being saved") + content_tag("p", "There were problems with the following fields:") + content_tag("ul", @context.errors.each_full { |msg| content_tag("li", msg) }), "id" => "ErrorExplanation", "class" => "ErrorExplanation") + page.visual_effect :appear, 'warning', :duration => 0.5 +end \ No newline at end of file diff --git a/tracks/app/views/project/list.rhtml b/tracks/app/views/project/list.rhtml index 1649779b..5f53798c 100644 --- a/tracks/app/views/project/list.rhtml +++ b/tracks/app/views/project/list.rhtml @@ -17,10 +17,15 @@ <%= form_remote_tag :url => { :action => "new_project" }, :html=> { :id=>'project-form', :name=>'project', :class => 'inline-form' } %> -
- <%= text_field 'project', 'name' %>
-
- <%= text_area 'project', 'description', "cols" => 30, "rows" => 4 %> + +
+ <%= text_field 'project', 'name' %> +
+ +
+ <%= text_area 'project', 'description', "cols" => 30, "rows" => 4 %> +
+ <%= end_form_tag %>
diff --git a/tracks/test/functional/context_controller_test.rb b/tracks/test/functional/context_controller_test.rb index 15929e29..0b7ea61e 100644 --- a/tracks/test/functional/context_controller_test.rb +++ b/tracks/test/functional/context_controller_test.rb @@ -5,13 +5,34 @@ require 'context_controller' class ContextController; def rescue_action(e) raise e end; end class ContextControllerTest < Test::Unit::TestCase + fixtures :users, :contexts + def setup @controller = ContextController.new - request, response = ActionController::TestRequest.new, ActionController::TestResponse.new + @request, @response = ActionController::TestRequest.new, ActionController::TestResponse.new end - # Replace this with your real tests. - def test_truth - assert true + def test_create_context + num_contexts = Context.count + @request.session['user_id'] = users(:other_user).id + xhr :post, :new_context, :context => {:name => 'newcontext'} + assert_rjs :hide, "warning" + assert_rjs :insert_html, :bottom, "list-contexts" + assert_rjs :sortable, 'list-contexts', { :tag => 'div', :handle => 'handle', :complete => visual_effect(:highlight, 'list-contexts'), :url => {:controller => 'context', :action => 'order'} } + # not yet sure how to write the following properly... + assert_rjs :call, "Form.reset", "context-form" + assert_rjs :call, "Form.focusFirstElement", "context-form" + assert_equal num_contexts + 1, Context.count end + + def test_create_with_slash_in_name_fails + num_contexts = Context.count + @request.session['user_id'] = users(:other_user).id + xhr :post, :new_context, :context => {:name => 'foo/bar'} + assert_rjs :hide, "warning" + assert_rjs :replace_html, 'warning', "

1 error prohibited this record from being saved

There were problems with the following fields:

" + assert_rjs :visual_effect, :appear, "warning", :duration => '0.5' + assert_equal num_contexts, Context.count + end + end diff --git a/tracks/test/unit/context_test.rb b/tracks/test/unit/context_test.rb index a6e624ce..275ef18b 100644 --- a/tracks/test/unit/context_test.rb +++ b/tracks/test/unit/context_test.rb @@ -3,8 +3,38 @@ require File.dirname(__FILE__) + '/../test_helper' class ContextTest < Test::Unit::TestCase fixtures :contexts - # Replace this with your real tests. - def test_truth - assert true + def setup + @agenda = contexts(:agenda) + @email = contexts(:email) + end + + def test_validate_presence_of_name + @agenda.name = "" + assert !@agenda.save + assert_equal 1, @agenda.errors.count + assert_equal "context must have a name", @agenda.errors.on(:name) + end + + def test_validate_name_is_less_than_256 + @agenda.name = "a"*256 + assert !@agenda.save + assert_equal 1, @agenda.errors.count + assert_equal "context name must be less than 256 characters", @agenda.errors.on(:name) + end + + def test_validate_name_is_unique + newcontext = Context.new + newcontext.name = contexts(:agenda).name + assert !newcontext.save + assert_equal 1, newcontext.errors.count + assert_equal "already exists", newcontext.errors.on(:name) + end + + def test_validate_name_does_not_contain_slash + newcontext = Context.new + newcontext.name = "phone/telegraph" + assert !newcontext.save + assert_equal 1, newcontext.errors.count + assert_equal "cannot contain the slash ('/') character", newcontext.errors.on(:name) end end diff --git a/tracks/test/unit/project_test.rb b/tracks/test/unit/project_test.rb index aae30025..e7eb8578 100644 --- a/tracks/test/unit/project_test.rb +++ b/tracks/test/unit/project_test.rb @@ -4,8 +4,8 @@ class ProjectTest < Test::Unit::TestCase fixtures :projects def setup - @timemachine = Project.find(1) - @moremoney = Project.find(2) + @timemachine = projects(:timemachine) + @moremoney = projects(:moremoney) end def test_validate_presence_of_name