From c3f8ee0346b24eb05879ec6bc482ef9a5461daa9 Mon Sep 17 00:00:00 2001 From: Soh Yu Ming Date: Thu, 17 Mar 2016 16:09:28 +0800 Subject: [PATCH] Changed JS methods to be scoped by name of controller action --- lib/rails_utils.rb | 4 ++-- test/rails_utils_test.rb | 10 +++++----- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/rails_utils.rb b/lib/rails_utils.rb index 6f271ed..241b39d 100644 --- a/lib/rails_utils.rb +++ b/lib/rails_utils.rb @@ -36,14 +36,14 @@ def javascript_initialization(options = {}) if content_for?(:js_init_method) js_custom_name = content_for(:js_init_method) - custom_js_init_method = "if(#{application_name}.#{js_namespace_name}.init_#{js_custom_name}) { #{application_name}.#{js_namespace_name}.init_#{js_custom_name}(); }" + custom_js_init_method = "if(#{application_name}.#{js_namespace_name}.#{js_custom_name}.init) { #{application_name}.#{js_namespace_name}.#{js_custom_name}.init(); }" end javascript_tag <<-JS #{application_name}.init(); if(#{application_name}.#{js_namespace_name}) { if(#{application_name}.#{js_namespace_name}.init) { #{application_name}.#{js_namespace_name}.init(); } - if(#{application_name}.#{js_namespace_name}.init_#{js_function_name}) { #{application_name}.#{js_namespace_name}.init_#{js_function_name}(); } + if(#{application_name}.#{js_namespace_name}.#{js_function_name} && #{application_name}.#{js_namespace_name}.#{js_function_name}.init) { #{application_name}.#{js_namespace_name}.#{js_function_name}.init(); } #{custom_js_init_method} } JS diff --git a/test/rails_utils_test.rb b/test/rails_utils_test.rb index 3b2b527..297abf3 100644 --- a/test/rails_utils_test.rb +++ b/test/rails_utils_test.rb @@ -169,7 +169,7 @@ it "invokes controller and action javascript" do view.javascript_initialization.must_match "Dummy.#{controller_name}.init();" - view.javascript_initialization.must_match "Dummy.#{controller_name}.init_#{action_name}();" + view.javascript_initialization.must_match "Dummy.#{controller_name}.#{action_name}.init();" end end @@ -177,7 +177,7 @@ let(:action_name) { "create" } it "replaces create with new" do - view.javascript_initialization.must_match "Dummy.#{controller_name}.init_new();" + view.javascript_initialization.must_match "Dummy.#{controller_name}.new.init();" end end @@ -185,7 +185,7 @@ let(:action_name) { "update" } it "replaces update with create" do - view.javascript_initialization.must_match "Dummy.#{controller_name}.init_edit();" + view.javascript_initialization.must_match "Dummy.#{controller_name}.edit.init();" end end @@ -194,7 +194,7 @@ it "uses the custom js init method" do view.content_for(:js_init_method) { "custom" } - view.javascript_initialization.must_match "Dummy.#{controller_name}.init_custom();" + view.javascript_initialization.must_match "Dummy.#{controller_name}.custom.init();" end end @@ -202,7 +202,7 @@ let(:action_name) { "update" } it "does not generate an additional javascript method" do - view.javascript_initialization.wont_match "Dummy.#{controller_name}.init_();" + view.javascript_initialization.wont_match "Dummy.#{controller_name}..init();" end end end