Skip to content

Commit

Permalink
Changed JS methods to be scoped by name of controller action
Browse files Browse the repository at this point in the history
  • Loading branch information
sohymg authored and JuanitoFatas committed Mar 19, 2016
1 parent 1e2142a commit c3f8ee0
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
4 changes: 2 additions & 2 deletions lib/rails_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions test/rails_utils_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -169,23 +169,23 @@

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

describe "when action name is create" do
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

describe "when action name is update" do
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

Expand All @@ -194,15 +194,15 @@

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

describe "without a content_for custom js_init_method as an argument" do
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
Expand Down

0 comments on commit c3f8ee0

Please sign in to comment.