diff --git a/tracks/app/controllers/application.rb b/tracks/app/controllers/application.rb index 2d21906e..822f25ab 100644 --- a/tracks/app/controllers/application.rb +++ b/tracks/app/controllers/application.rb @@ -96,6 +96,14 @@ class ApplicationController < ActionController::Base redirect_back_or_default home_url end + def boolean_param(param_name) + return false if param_name.blank? + s = params[param_name] + return false if s.blank? || s == false || s =~ /^false$/i + return true if s == true || s =~ /^true$/i + raise ArgumentError.new("invalid value for Boolean: \"#{s}\"") + end + private def parse_date_per_user_prefs( s ) diff --git a/tracks/app/controllers/projects_controller.rb b/tracks/app/controllers/projects_controller.rb index e5395018..4d4bbd80 100644 --- a/tracks/app/controllers/projects_controller.rb +++ b/tracks/app/controllers/projects_controller.rb @@ -85,7 +85,7 @@ class ProjectsController < ApplicationController end @project.attributes = params['project'] if @project.save - if params['wants_render'] + if boolean_param('wants_render') if (@project.hidden?) @project_project_hidden_todo_counts = Hash.new @project_project_hidden_todo_counts[@project.id] = @project.reload().not_done_todo_count(:include_project_hidden_todos => true) @@ -93,7 +93,7 @@ class ProjectsController < ApplicationController @project_not_done_counts[@project.id] = @project.reload().not_done_todo_count(:include_project_hidden_todos => true) end render - elsif params['update_status'] + elsif boolean_param('update_status') render :action => 'update_status' else render :text => success_text || 'Success' diff --git a/tracks/app/views/contexts/_context_listing.rhtml b/tracks/app/views/contexts/_context_listing.rhtml index 95aa98ed..9194aec3 100644 --- a/tracks/app/views/contexts/_context_listing.rhtml +++ b/tracks/app/views/contexts/_context_listing.rhtml @@ -1,6 +1,6 @@ <% context = context_listing %>
" class="list"> -
+
DRAG
@@ -14,33 +14,43 @@ <% 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')}');" ) %> + <%= image_tag( "blank.png", :title => "Delete context", :class=>"delete_item") %> + <% apply_behavior "a.delete_context_button:click", :prevent_default => true do |page| + page << "if (confirm('Are you sure that you want to ' + this.title + '?')) {" + page << "new Ajax.Updater(this.up('.list'), this.href, {asynchronous:true, evalScripts:true, method:'delete', onLoading:function(request){Effect.Fade(this.up('.list'));}}); };" + end -%> + <%= image_tag( "blank.png", :title => "Edit context", :class=>"edit_item") %> + <% apply_behavior 'a.edit_context_button:click', {:prevent_default => true } do |page, element| + element.up('.context').toggle + editform = element.up('.list').down('.edit-form') + editform.toggle + editform.visual_effect(:appear) + editform.down('input').focus + end + -%>
<% if controller.action_name == 'create' %> -<% end %> \ No newline at end of file +<% end %> diff --git a/tracks/app/views/contexts/index.rhtml b/tracks/app/views/contexts/index.rhtml index 2897d7ae..c107a7f6 100644 --- a/tracks/app/views/contexts/index.rhtml +++ b/tracks/app/views/contexts/index.rhtml @@ -2,14 +2,12 @@
<%= render :partial => 'context_listing', :collection => @contexts %>
- - <%= sortable_element 'list-contexts', get_listing_sortable_options %>
- Create new context » + Create new context » -
\ No newline at end of file + +<% +sortable_element 'list-contexts', get_listing_sortable_options + +apply_behavior '#toggle-new-context-form:click', :prevent_default => true do |page| + page['context_new'].toggle + page['context-form'].down('input').focus + #page << "Form.focusFirstElement('context-form');" +end +-%> \ No newline at end of file diff --git a/tracks/app/views/layouts/login.rhtml b/tracks/app/views/layouts/login.rhtml index 5acec669..e124c2bc 100644 --- a/tracks/app/views/layouts/login.rhtml +++ b/tracks/app/views/layouts/login.rhtml @@ -19,8 +19,6 @@ <%= yield %> - +<%= render :partial => "shared/footer" %> diff --git a/tracks/app/views/layouts/standard.rhtml b/tracks/app/views/layouts/standard.rhtml index 56c55c70..9f049dca 100644 --- a/tracks/app/views/layouts/standard.rhtml +++ b/tracks/app/views/layouts/standard.rhtml @@ -7,7 +7,7 @@ <% end -%> <%= stylesheet_link_tag "standard" %> <%= stylesheet_link_tag "print", :media => "print" %> - <%= javascript_include_tag :defaults %> + <%= javascript_include_tag :defaults, :unobtrusive %> <%= javascript_include_tag "selector-addon-v1" %> <%= stylesheet_link_tag 'calendar-system.css' %> <%= javascript_include_tag 'calendar', 'calendar-en', 'calendar-setup' %> @@ -70,8 +70,6 @@ <%= yield %> - +<%= render :partial => "shared/footer" %> diff --git a/tracks/app/views/projects/show.rhtml b/tracks/app/views/projects/show.rhtml index 52488f07..afef1406 100644 --- a/tracks/app/views/projects/show.rhtml +++ b/tracks/app/views/projects/show.rhtml @@ -36,11 +36,13 @@

Status

<% ['active', 'hidden', 'completed'].each do | state | %> - <% js = "new Ajax.Request('#{ url_for :controller => 'projects', :action => 'update', :id => @project.url_friendly_name, :wants_render => false, :update_status => true, 'project[state]' => state }', {method: 'put', asynchronous:true, evalScripts:true});" - span_class = @project.current_state.to_s == state ? 'active_state' : 'inactive_state' - %> - <%= radio_button(:project, 'state', state, {:onclick => js}) %> <%= state.titlecase %> + <% span_class = @project.current_state.to_s == state ? 'active_state' : 'inactive_state' %> + <%= radio_button(:project, 'state', state) %> <%= state.titlecase %> <% end %> + <% apply_behavior "#project_status input:click", + remote_function(:url => project_path(@project), :method => :put, + :with => "{wants_render: false, update_status: true, 'project[state]' : this.value}" ) + %>
diff --git a/tracks/app/views/shared/_footer.rhtml b/tracks/app/views/shared/_footer.rhtml new file mode 100644 index 00000000..bb7dc81b --- /dev/null +++ b/tracks/app/views/shared/_footer.rhtml @@ -0,0 +1,3 @@ + diff --git a/tracks/config/routes.rb b/tracks/config/routes.rb index 83a2e19d..a06a3b4d 100644 --- a/tracks/config/routes.rb +++ b/tracks/config/routes.rb @@ -1,4 +1,5 @@ ActionController::Routing::Routes.draw do |map| + UJS::routes # Mobile/lite version map.connect 'mobile', :controller => 'mobile', :action => 'index' diff --git a/tracks/public/javascripts/lowpro.js b/tracks/public/javascripts/lowpro.js new file mode 100644 index 00000000..c0124e61 --- /dev/null +++ b/tracks/public/javascripts/lowpro.js @@ -0,0 +1,307 @@ +LowPro = {}; +LowPro.Version = '0.1'; + +// Adapted from DOM Ready extension by Dan Webb +// http://www.vivabit.com/bollocks/2006/06/21/a-dom-ready-extension-for-prototype +// which was based on work by Matthias Miller, Dean Edwards and John Resig +// +// Usage: +// +// Event.onReady(callbackFunction); +Object.extend(Event, { + _domReady : function() { + if (arguments.callee.done) return; + arguments.callee.done = true; + + if (Event._timer) clearInterval(Event._timer); + + Event._readyCallbacks.each(function(f) { f() }); + Event._readyCallbacks = null; + + }, + onReady : function(f) { + if (!this._readyCallbacks) { + var domReady = this._domReady; + + if (domReady.done) return f(); + + if (document.addEventListener) + document.addEventListener("DOMContentLoaded", domReady, false); + + /*@cc_on @*/ + /*@if (@_win32) + document.write("') + end + + def test_should_render_script_tag_for_current_requests_behaviour + assert @output.include?('') + end + + def test_should_render_index_behaviour_when_request_path_is_just_a_forward_slash + @controller.request.stubs(:path).returns('/') + @output = javascript_include_tag(:unobtrusive).split("\n") + assert @output.include?('') + end + + def test_should_render_index_behaviour_when_request_path_is_blank_as_a_result_of_a_url_prefix + @controller.request.stubs(:path).returns('') + @output = javascript_include_tag(:unobtrusive).split("\n") + assert @output.include?('') + end +end \ No newline at end of file diff --git a/tracks/vendor/plugins/unobtrusive_javascript/test/behaviour_helper_test.rb b/tracks/vendor/plugins/unobtrusive_javascript/test/behaviour_helper_test.rb new file mode 100644 index 00000000..f8c800b3 --- /dev/null +++ b/tracks/vendor/plugins/unobtrusive_javascript/test/behaviour_helper_test.rb @@ -0,0 +1,180 @@ +require File.dirname(__FILE__) + '/test_helper' +require 'prototype_helper_patches' +require 'scriptaculous_helper_patches' +require 'ujs/behaviour_helper' + +class MakeSortableTest < Test::Unit::TestCase + include ActionView::Helpers::JavaScriptHelper + include ActionView::Helpers::ScriptaculousHelper + include ActionView::Helpers::PrototypeHelper + include ActionView::Helpers::UrlHelper + include ActionView::Helpers::TagHelper + include UJS::BehaviourHelper + + def setup + initialize_test_request + end + + def test_should_output_sortable_javascript + output = make_sortable + + assert_equal sortable_element_js(javascript_variable('this')), output + end + + def test_should_pass_arguments_through + output = make_sortable :onUpdate => 'function() { alert("updated") }' + assert_equal 'Sortable.create(this, {onUpdate:function() { alert("updated") }});', output + end +end + +class MakeRemoteLinkTest < Test::Unit::TestCase + include ActionView::Helpers::JavaScriptHelper + include ActionView::Helpers::ScriptaculousHelper + include ActionView::Helpers::PrototypeHelper + include ActionView::Helpers::UrlHelper + include ActionView::Helpers::TagHelper + include UJS::BehaviourHelper + + def setup + initialize_test_request + end + + def test_should_generate_ajax_request + output = make_remote_link + assert_match(/new Ajax\.Request/, output) + end + + def test_should_default_to_element_href + output = make_remote_link + assert_match(/\(this\.href/, output) + end + + def test_should_respond_to_given_options + output = make_remote_link( :update => 'fartknocker' ) + assert_match(/new Ajax\.Updater\('fartknocker'/, output) + end +end + +class MakeRemoteFormTest < Test::Unit::TestCase + include ActionView::Helpers::JavaScriptHelper + include ActionView::Helpers::ScriptaculousHelper + include ActionView::Helpers::PrototypeHelper + include ActionView::Helpers::UrlHelper + include ActionView::Helpers::TagHelper + include UJS::BehaviourHelper + + def setup + initialize_test_request + end + + def test_should_generate_ajax_request + output = make_remote_form + assert_match(/new Ajax\.Request/, output) + end + + def test_should_default_to_form_action + output = make_remote_form + assert_match(/\(this\.action/, output) + end +end + +class MakeDraggableTest < Test::Unit::TestCase + include ActionView::Helpers::JavaScriptHelper + include ActionView::Helpers::ScriptaculousHelper + include ActionView::Helpers::PrototypeHelper + include ActionView::Helpers::UrlHelper + include ActionView::Helpers::TagHelper + include UJS::BehaviourHelper + + def setup + initialize_test_request + end + + def test_should_create_draggable_instance + output = make_draggable + assert_match(/new Draggable/, output) + end + + def test_should_pass_this + output = make_draggable + assert_match(/\(this/, output) + end + + def test_should_respond_to_options + output = make_draggable( :revert => true ) + assert_match(/revert\:true/, output) + end +end + +class MakeDropRecievingTest < Test::Unit::TestCase + include ActionView::Helpers::JavaScriptHelper + include ActionView::Helpers::ScriptaculousHelper + include ActionView::Helpers::PrototypeHelper + include ActionView::Helpers::UrlHelper + include ActionView::Helpers::TagHelper + include UJS::BehaviourHelper + + def setup + initialize_test_request + end + + def test_should_add_to_droppables + output = make_drop_receiving + assert_match(/Droppables\.add/, output) + end + + def test_should_pass_this + output = make_drop_receiving + assert_match(/\(this/, output) + end + + def test_should_generate_a_url_from_options + output = make_drop_receiving( :url => { :action => "bingo" } ) + assert_match(/controller_stub\/bingo/, output) + end +end + +class MakeObservedTest < Test::Unit::TestCase + include ActionView::Helpers::JavaScriptHelper + include ActionView::Helpers::ScriptaculousHelper + include ActionView::Helpers::PrototypeHelper + include ActionView::Helpers::UrlHelper + include ActionView::Helpers::TagHelper + include UJS::BehaviourHelper + + def setup + initialize_test_request + end + + def test_should_make_form_observer + output = make_observed(:form) + assert_match(/new Form\.EventObserver/, output) + end + + def test_should_make_field_observer + output = make_observed(:field) + assert_match(/new Form\.Element\.EventObserver/, output) + end + + def test_should_pass_this + output = make_observed(:field) + assert_match(/\(this/, output) + end + + def test_should_make_a_timed_observer_if_frequency_passed + output = make_observed(:form, :frequency => 3 ) + assert_match(/new Form.Observer/, output) + assert_match(/3,/, output) + end + + def test_should_generate_a_url_from_options + output = make_observed(:field, :url => { :action => "bingo" } ) + assert_match(/controller_stub\/bingo/, output) + end + + def test_should_respond_to_options + output = make_observed(:field, :function => 'alert("boo")' ) + assert_match(/function\(element, value\) \{alert\("boo"\)\}/, output) + end +end + diff --git a/tracks/vendor/plugins/unobtrusive_javascript/test/behaviour_script_converter_test.rb b/tracks/vendor/plugins/unobtrusive_javascript/test/behaviour_script_converter_test.rb new file mode 100644 index 00000000..e403ed33 --- /dev/null +++ b/tracks/vendor/plugins/unobtrusive_javascript/test/behaviour_script_converter_test.rb @@ -0,0 +1,72 @@ +require File.dirname(__FILE__) + '/test_helper' + +class DefaultBehaviourScriptConversionTest < Test::Unit::TestCase + def setup + @script = UJS::BehaviourScript.new + @output = UJS::BehaviourScriptConverter.convert_to_hash(@script) + end + + def test_should_have_no_rules_and_the_correct_default_options + assert_equal({ :options => { :cache => false, :reapply_after_ajax => true }, + :rules => [] }, @output) + end +end + +class EmptyBehaviourScriptWithDifferentOptionsConversionTest < Test::Unit::TestCase + def setup + @script = UJS::BehaviourScript.new(true, false) + @output = UJS::BehaviourScriptConverter.convert_to_hash(@script) + end + + def test_should_have_no_rules_and_the_correct_options + assert_equal({ :options => { :cache => true, :reapply_after_ajax => false }, + :rules => [] }, @output) + end +end + +class BehaviourScriptWithOneRuleConversionTest < Test::Unit::TestCase + def setup + @script = UJS::BehaviourScript.new + @script.add_rule('div.foo:click', 'alert("TEST")') + @output = UJS::BehaviourScriptConverter.convert_to_hash(@script) + end + + def test_should_have_one_behaviour_and_correct_options + assert_equal({ :options => { :cache => false, :reapply_after_ajax => true }, + :rules => [ + ['div.foo:click', 'alert("TEST")'] + ] }, @output) + end +end + +class BehaviourScriptWithTwoRuleConversionTest < Test::Unit::TestCase + def setup + @script = UJS::BehaviourScript.new + @script.add_rule('div.foo:click', 'alert("TEST")') + @script.add_rule('div.bar:click', 'alert("TEST 2")') + @output = UJS::BehaviourScriptConverter.convert_to_hash(@script) + end + + def test_should_have_one_behaviour_and_correct_options + assert_equal({ :options => { :cache => false, :reapply_after_ajax => true }, + :rules => [ + ['div.foo:click', 'alert("TEST")'], + ['div.bar:click', 'alert("TEST 2")'] + ] }, @output) + end +end + +class BehaviourScriptFromHashTest < Test::Unit::TestCase + def setup + @script = UJS::BehaviourScript.new + @script.add_rule('div.foo:click', 'alert("TEST")') + @script.add_rule('div.bar:click', 'alert("TEST 2")') + @converted_script = UJS::BehaviourScriptConverter.convert_from_hash(@script.to_hash) + end + + def test_should_equal_the_script_it_was_converted_from_in_the_first_place + assert_equal @script.cache?, @converted_script.cache? + assert_equal @script.reapply_after_ajax?, @converted_script.reapply_after_ajax? + assert_equal @script.rules, @converted_script.rules + end +end \ No newline at end of file diff --git a/tracks/vendor/plugins/unobtrusive_javascript/test/behaviour_script_test.rb b/tracks/vendor/plugins/unobtrusive_javascript/test/behaviour_script_test.rb new file mode 100644 index 00000000..33e1f671 --- /dev/null +++ b/tracks/vendor/plugins/unobtrusive_javascript/test/behaviour_script_test.rb @@ -0,0 +1,98 @@ +require File.dirname(__FILE__) + '/test_helper' +require 'ujs/behaviour_script' + +class NewBehaviourScriptTest < Test::Unit::TestCase + def setup + @script = UJS::BehaviourScript.new + end + + def test_should_render_nothing_on_to_string + assert_equal "", @script.to_s + end + + def test_should_not_be_cached + assert !@script.cache? + end + + def test_should_be_reapplied_after_an_ajax_request + assert @script.reapply_after_ajax? + end +end + +class BehaviourScriptWithOneRuleTest < Test::Unit::TestCase + def setup + @script = UJS::BehaviourScript.new + @script.add_rule("div.header:click", "alert('Hello World')") + end + + def test_should_render_the_rule_as_a_javascript_event_on_to_s + expected_js = "Event.addBehavior({\n\"div.header:click\": function(event) {\nalert('Hello World')\n}\n});" + assert_equal expected_js, @script.to_s + end +end + +class BehaviourScriptWithTwoRulesTest < Test::Unit::TestCase + def setup + @script = UJS::BehaviourScript.new + @script.add_rule("div.header:mouseover", "alert('Hello World')") + @script.add_rule("div.header:mouseout", "alert('Goodbye World')") + end + + def test_should_render_all_rules_as_javascript_events_on_to_s + expected_js = "Event.addBehavior({\n\"div.header:mouseover\": function(event) {\nalert('Hello World')\n}," + expected_js = expected_js + "\n\"div.header:mouseout\": function(event) {\nalert('Goodbye World')\n}\n});" + assert_equal expected_js, @script.to_s + end +end + +class BehaviourScriptWithRuleThatCancelsDefaultTest < Test::Unit::TestCase + def setup + @script = UJS::BehaviourScript.new + @script.add_rule("div.header:mouseover", "alert('Hello World');", true) + end + + def test_should_render_rule_with_return_false_appended_on_to_s + expected_js = "Event.addBehavior({\n\"div.header:mouseover\": function(event) {\nalert('Hello World'); return false;\n}\n});" + assert_equal expected_js, @script.to_s + end +end + +class BehaviourScriptWithNoRulesTest < Test::Unit::TestCase + def setup + @script = UJS::BehaviourScript.new + end + + def test_should_render_nothing_on_to_s + assert_equal "", @script.to_s + end +end + +class BehaviourScriptWithRulesSetToNotReapplyAfterAjaxTest < Test::Unit::TestCase + def setup + @script = UJS::BehaviourScript.new + @script.reapply_after_ajax = false + @script.add_rule("div.header:click", "alert('Hello World')") + end + + def test_should_append_reapply_javascript_to_end_of_rules_javascript_on_to_s + expected_js = "Event.addBehavior({\n\"div.header:click\": function(event) {\nalert('Hello World')\n}\n});" + expected_js = expected_js + "\nEvent.addBehavior.reapplyAfterAjax = false;" + assert_equal expected_js, @script.to_s + end +end + +class BehaviourScriptToHashTest < Test::Unit::TestCase + def setup + @script = UJS::BehaviourScript.new(true, false) + @script.add_rule("div.header:mouseover", "alert('Hello World')") + @script.add_rule("div.header:mouseout", "alert('Goodbye World')") + end + + def test_should_return_converted_behaviour_script + assert_equal({ :options => { :cache => true, :reapply_after_ajax => false }, + :rules => [ + ['div.header:mouseover', "alert('Hello World')"], + ['div.header:mouseout', "alert('Goodbye World')"] + ] }, @script.to_hash) + end +end \ No newline at end of file diff --git a/tracks/vendor/plugins/unobtrusive_javascript/test/config/database.yml b/tracks/vendor/plugins/unobtrusive_javascript/test/config/database.yml new file mode 100644 index 00000000..bafaf2ab --- /dev/null +++ b/tracks/vendor/plugins/unobtrusive_javascript/test/config/database.yml @@ -0,0 +1,19 @@ +test: + adapter: sqlite3 + dbfile: test.sqlite3.db + +# adapter: sqlite +# dbfile: test.sqlite.db + +# adapter: mysql +# host: localhost +# username: +# password: +# database: test + +# adapter: postgresql +# host: localhost +# username: +# password: +# database: test + diff --git a/tracks/vendor/plugins/unobtrusive_javascript/test/config/environment.rb b/tracks/vendor/plugins/unobtrusive_javascript/test/config/environment.rb new file mode 100644 index 00000000..47ed1736 --- /dev/null +++ b/tracks/vendor/plugins/unobtrusive_javascript/test/config/environment.rb @@ -0,0 +1,8 @@ +Rails::Initializer.run do |config| + config.cache_classes = true + config.whiny_nils = true + config.action_controller.consider_all_requests_local = true + config.action_controller.perform_caching = false + config.action_mailer.delivery_method = :test + config.action_mailer.perform_deliveries = true +end \ No newline at end of file diff --git a/tracks/vendor/plugins/unobtrusive_javascript/test/config/routes.rb b/tracks/vendor/plugins/unobtrusive_javascript/test/config/routes.rb new file mode 100644 index 00000000..d7da080e --- /dev/null +++ b/tracks/vendor/plugins/unobtrusive_javascript/test/config/routes.rb @@ -0,0 +1,4 @@ +ActionController::Routing::Routes.draw do |map| + map.connect ':controller/:action/:id' + UJS::routes +end \ No newline at end of file diff --git a/tracks/vendor/plugins/unobtrusive_javascript/test/config/schema.rb b/tracks/vendor/plugins/unobtrusive_javascript/test/config/schema.rb new file mode 100644 index 00000000..4c9c90fe --- /dev/null +++ b/tracks/vendor/plugins/unobtrusive_javascript/test/config/schema.rb @@ -0,0 +1,3 @@ +ActiveRecord::Schema.define(:version => 2) do + +end \ No newline at end of file diff --git a/tracks/vendor/plugins/unobtrusive_javascript/test/controller_methods_test.rb b/tracks/vendor/plugins/unobtrusive_javascript/test/controller_methods_test.rb new file mode 100644 index 00000000..1f9eaa65 --- /dev/null +++ b/tracks/vendor/plugins/unobtrusive_javascript/test/controller_methods_test.rb @@ -0,0 +1,48 @@ +require File.dirname(__FILE__) + '/test_helper' + +class ControllerWithControllerMethodsInjected < Test::Unit::TestCase + def setup + @controller = ControllerStub.new + @request = ActionController::TestRequest.new + @response = ActionController::TestResponse.new + get :index + end + + def test_should_add_a_before_filter_that_creates_a_new_behaviour_script + assert ControllerStub.before_filters.include?(:initialise_js_behaviours) + assert_instance_of UJS::BehaviourScript, assigns(:js_behaviours) + assert_equal "", assigns(:js_behaviours).to_s + end + + def test_should_store_applied_behaviours_in_the_behaviour_script + @controller.apply_behaviour("div.foo", "alert('foo')") + assert_equal 1, assigns(:js_behaviours).rules.size + end + + def test_should_add_an_after_filter_that_stores_the_behaviour_script_in_the_session_as_a_hash + assert ControllerStub.after_filters.include?(:store_js_behaviours) + assert_equal session[:js_behaviours], assigns(:js_behaviours).to_hash + end + + def test_should_not_store_behaviour_script_in_the_session_if_js_behaviours_is_nil + @controller.send(:reset_js_behaviours) + assert_nil @controller.send(:js_behaviours) + end + + def test_should_turn_behaviour_script_caching_on_when_cache_behaviours_is_called + @controller.cache_behaviours + assert assigns(:js_behaviours).cache? + end + + def test_should_toggle_reload_after_ajax_when_set + @controller.reapply_behaviours_after_ajax = false + assert !assigns(:js_behaviours).reapply_after_ajax? + @controller.reapply_behaviours_after_ajax = true + assert assigns(:js_behaviours).reapply_after_ajax? + end + + def test_should_also_allow_american_spelling_for_apply_behaviour + @controller.apply_behavior("div.foo", "alert('foo')") + assert_equal 1, assigns(:js_behaviours).rules.size + end +end \ No newline at end of file diff --git a/tracks/vendor/plugins/unobtrusive_javascript/test/helpers_test.rb b/tracks/vendor/plugins/unobtrusive_javascript/test/helpers_test.rb new file mode 100644 index 00000000..405e0a50 --- /dev/null +++ b/tracks/vendor/plugins/unobtrusive_javascript/test/helpers_test.rb @@ -0,0 +1,186 @@ +require File.dirname(__FILE__) + '/test_helper' + +class ApplyingBehaviourWithStringOfJavascriptTest < Test::Unit::TestCase + include UJS::Helpers + + def setup + @controller = ControllerStub.new + @request = ActionController::TestRequest.new + @response = ActionController::TestResponse.new + get :index + @output = apply_behaviour("#mydiv:click", "alert('hello world')") + end + + def test_should_store_registered_behaviour + assert_equal 1, assigns(:js_behaviours).rules.size + assert_equal "#mydiv:click", assigns(:js_behaviours).rules.first[0] + assert_equal "alert('hello world');", assigns(:js_behaviours).rules.first[1] + end +end + +class ApplyingBehaviourThatIsRendererdInlineTest < Test::Unit::TestCase + include UJS::Helpers + include ActionView::Helpers::JavaScriptHelper + include ActionView::Helpers::TagHelper + + def setup + @controller = ControllerStub.new + @request = ActionController::TestRequest.new + @response = ActionController::TestResponse.new + get :index + @output = apply_behaviour("#mydiv:click", "alert('hello world')", :external => false) + end + + def test_should_not_store_registered_behaviour + assert_equal 0, assigns(:js_behaviours).rules.size + end +end + +class PreventDefaultBehaviourOptionTest < Test::Unit::TestCase + include UJS::Helpers + include ActionView::Helpers::JavaScriptHelper + include ActionView::Helpers::TagHelper + + def setup + @controller = ControllerStub.new + @request = ActionController::TestRequest.new + @response = ActionController::TestResponse.new + get :index + @output = apply_behaviour("#mydiv:click", "alert('hello world')", :prevent_default => true) + end + + def test_should_return_false_with_prevent_default + assert_equal ['#mydiv:click', "alert('hello world'); return false;"], assigns(:js_behaviours).rules.last + end +end + +class ApplyingBehaviourWithBlockTest < Test::Unit::TestCase + include UJS::Helpers + + def setup + @controller = ControllerStub.new + @request = ActionController::TestRequest.new + @response = ActionController::TestResponse.new + get :index + end + + def test_should_use_page_argument + apply_behaviour '#thing' do |page| + page.alert('hello') + end + + assert_equal '#thing', assigns(:js_behaviours).rules.last[0] + assert_equal "alert(\"hello\");", assigns(:js_behaviours).rules.last[1] + end + + def test_should_use_element_argument + apply_behaviour '#thing' do |page, element| + element.hide + end + + assert_equal '#thing', assigns(:js_behaviours).rules.last[0] + assert_equal "this.hide();", assigns(:js_behaviours).rules.last[1] + end + + def test_should_use_event_argument + apply_behaviour '#thing' do |page, element, event| + event.stop + end + + assert_equal '#thing', assigns(:js_behaviours).rules.last[0] + assert_equal "Event.stop(event);", assigns(:js_behaviours).rules.last[1] + end + + def test_should_use_allow_multiple_calls + apply_behaviour '#thing' do |page, element, event| + page.alert('hiding thing') + element.hide + element.show + event.stop + end + + assert_equal '#thing', assigns(:js_behaviours).rules.last[0] + assert_equal "alert(\"hiding thing\");\nthis.hide();\nthis.show();\nEvent.stop(event);", assigns(:js_behaviours).rules.last[1] + end + + def test_should_allow_options_with_block_without_specifying_string + apply_behaviour '#thing2', :prevent_default => true do |page| + page.alert('boo') + end + + assert_equal '#thing2', assigns(:js_behaviours).rules.last[0] + assert_equal "alert(\"boo\"); return false;", assigns(:js_behaviours).rules.last[1] + end + + def test_should_allow_element_proxy_methods_to_be_called + apply_behaviour '#thing3' do |page, element| + element.replace_html 'Wow!' + end + + assert_equal '#thing3', assigns(:js_behaviours).rules.last[0] + assert_equal "this.update(\"Wow!\");", assigns(:js_behaviours).rules.last[1] + end +end + +class MultipleBehavioursAppliedAtOnceTest < Test::Unit::TestCase + include ActionView::Helpers::JavaScriptHelper + include ActionView::Helpers::ScriptaculousHelper + include ActionView::Helpers::PrototypeHelper + include ActionView::Helpers::UrlHelper + include ActionView::Helpers::TagHelper + include UJS::BehaviourHelper + include UJS::Helpers + + def setup + @controller = ControllerStub.new + @request = ActionController::TestRequest.new + @response = ActionController::TestResponse.new + get :index + apply_behaviours do + on "div.foo", "alert('foo')" + on "div.bar", "alert('bar')" + end + end + + def test_should_all_get_registered_in_the_behaviour_script + assert_equal 2, assigns(:js_behaviours).rules.size + end + + def test_should_work_with_apply_behaviour_helpers + apply_behaviours do + on "ul.sortable", make_sortable + end + assert_equal 3, assigns(:js_behaviours).rules.size + end +end + +class MultipleBehavioursAppliedAtOnceWithExternalFalseTest < Test::Unit::TestCase + include ActionView::Helpers::JavaScriptHelper + include ActionView::Helpers::ScriptaculousHelper + include ActionView::Helpers::PrototypeHelper + include ActionView::Helpers::UrlHelper + include ActionView::Helpers::TagHelper + include UJS::BehaviourHelper + include UJS::Helpers + + def setup + @controller = ControllerStub.new + @request = ActionController::TestRequest.new + @response = ActionController::TestResponse.new + get :index + @output = apply_behaviours do + on "div.foo", "alert('foo')", :external => false + on "div.bar", :external => false do |page| + page.alert('bar') + end + end + end + + def test_should_output_behaviours + assert_not_equal '', @output + assert_match(/