Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changed JS methods to be scoped by name of controller action #26

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ When application is MyApp, and controller/action is `animes#show`, `javascript_i
MyApp.init();
if(MyApp.animes) {
if(MyApp.animes.init) { MyApp.animes.init(); }
if(MyApp.animes.init_show) { MyApp.animes.init_show(); }
if(MyApp.animes.show && MyApp.animes.show.init) { MyApp.animes.show.init(); }
}
//]]>
</script>
Expand Down
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();"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}..init();" typo with two dots?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi Juanito, it is not a typo. I believe this test is testing if there is no
custom js method provided, it should not generate an empty method name.
On Thu, 17 Mar 2016 at 10:37 PM Juanito Fatas [email protected]
wrote:

In test/rails_utils_test.rb
#26 (comment):

@@ -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();"
    

..init();" typo?


You are receiving this because you authored the thread.
Reply to this email directly or view it on GitHub
https://github.com/winston/rails_utils/pull/26/files/7bc9b02dd2a7e64ade28eeba39156c9dee89c76d#r56513879

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

end
end
end
Expand Down