Skip to content

Commit a026409

Browse files
committed
Simplify installation by using a helper to include the javascript loader in application layout.
1 parent b4e3637 commit a026409

File tree

4 files changed

+23
-19
lines changed

4 files changed

+23
-19
lines changed

lib/generators/rails_script/install/install_generator.rb

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,26 +22,9 @@ def create_controllers
2222

2323
def insert_layout_javascript
2424
say <<-RUBY
25-
In order to complete installation, you must add the following JavaScript snippet before the CLOSING body tag in your application layout.
25+
In order to complete installation, you must include the following helper BEFORE the closing body tag in the application layout:
2626
27-
ERB:
28-
<script>
29-
jQuery(function() {
30-
window.$this = new (App.<%= controller_path.split(/\\/|_/).map(&:capitalize).join('') %> || App.Base)();
31-
if (typeof $this.<%= action_name %> === 'function') {
32-
return $this.<%= action_name%>.call();
33-
}
34-
});
35-
</script>
36-
37-
HAML:
38-
:javascript
39-
jQuery(function() {
40-
window.$this = new (App.\#{controller_path.split(/\\/|_/).map(&:capitalize).join('')} || App.Base)();
41-
if (typeof $this.\#{action_name} === 'function') {
42-
return $this.\#{action_name}.call();
43-
}
44-
});
27+
<%= include_rails_script %>
4528
RUBY
4629
end
4730

lib/rails_script.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require 'rails_script/version'
2+
require 'rails_script/loader_helper'
23
require 'rails_script/railtie' if defined?(Rails)
34

45
module RailsScript

lib/rails_script/loader_helper.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
module RailsScript
2+
module LoaderHelper
3+
4+
def include_rails_script
5+
javascript_tag <<-RUBY
6+
jQuery(function() {
7+
window.$this = new (App.#{ controller_path.split(/\/|_/).map(&:capitalize).join('') } || App.Base)();
8+
if (typeof $this.#{ action_name } === 'function') {
9+
return $this.#{ action_name }.call();
10+
}
11+
});
12+
RUBY
13+
end
14+
15+
end
16+
end

lib/rails_script/railtie.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,9 @@ class Railtie < Rails::Railtie
33
config.app_generators do |g|
44
g.templates.unshift File::expand_path('../../templates', __FILE__)
55
end
6+
7+
initializer 'rails_Script.loader_helper' do
8+
ActionView::Base.send :include, LoaderHelper
9+
end
610
end
711
end

0 commit comments

Comments
 (0)