Skip to content

Commit 3bc55a1

Browse files
committed
Merge pull request #15 from kpheasey/master
v2 - Engine
2 parents d794b5d + 7710c10 commit 3bc55a1

File tree

18 files changed

+75
-77
lines changed

18 files changed

+75
-77
lines changed

README.md

+1-4
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,6 @@ After the generator finishes, you will be prompted to add helper call to your ap
2828

2929
NOTE: Your JS files needed have been included before `include_rails_script`. In other words, you still need `<%= javascript_include_tag "application" %>` in your application layout.
3030

31-
## Configuration
32-
You can configure name of your application in config/initializers/rails_script.rb file.
33-
3431
## Usage
3532

3633
### Page (Action) Specific JavaScript
@@ -231,7 +228,7 @@ Inheritance from the generator can only come from a Utility class. Any class yo
231228

232229
### Custom Controllers
233230

234-
When a new controller is generated, the JavaScript asset file will be generated with RailsScript. However, if you need to manually generate a RailsScript controller you can use:
231+
When a new controller is generated, the JavaScript asset file will be generated with App. However, if you need to manually generate a RailsScript controller you can use:
235232

236233
$ rails g rails_script:controller Some::NewController
237234

app/assets/javascripts/base.coffee

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class App.Base
2+
3+
constructor: ->
4+
if (window.jQuery) then RailsScript.setClearEventHandlers() # clearing application event handlers only possible with jQuery
5+
return this
6+
7+
8+
###
9+
Run the new action for the create action. Generally the create action will 'render :new' if there was a problem.
10+
This prevents doubling the code for each action.
11+
###
12+
create: ->
13+
if typeof $this.new == 'function'
14+
return $this.new()
15+
16+
17+
###
18+
Run the edit action for the update action. Generally the update action will 'render :edit' if there was a problem.
19+
This prevents doubling the code for each action.
20+
###
21+
update: ->
22+
if typeof $this.edit == 'function'
23+
return $this.edit()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#= require rails_script/init
2+
#= require base
3+
#= require_tree ./utilities
4+
#= require_tree ./elements
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
class <%= RailsScript.app_namespace %>.<%= class_name.gsub('::', '') %>
1+
class App.<%= class_name.gsub('::', '') %>
22

33
constructor: ->
44
return this

lib/generators/rails_script/controller/templates/javascript.js.coffee

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
class <%= RailsScript.app_namespace %>.<%= controller.gsub('::', '') %> extends <%= RailsScript.app_namespace %>.Base
1+
class App.<%= controller.gsub('::', '') %> extends App.Base
22

33
beforeAction: (action) =>
44
return

lib/generators/rails_script/element/element_generator.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module RailsScript
22
module Generators
33
class ElementGenerator < ::Rails::Generators::Base
4-
source_root File.expand_path("../templates", __FILE__)
4+
source_root File.expand_path('../templates', __FILE__)
55
argument :element_name, type: :string
66
argument :utility, type: :string, default: ''
77

lib/generators/rails_script/install/install_generator.rb

+7-13
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,28 @@
11
module RailsScript
22
module Generators
33
class InstallGenerator < ::Rails::Generators::Base
4-
source_root File.expand_path("../templates", __FILE__)
4+
source_root File.expand_path("../../../../../app/assets/javascripts", __FILE__)
55

66
def copy_files
7-
template 'base.js.coffee', 'app/assets/javascripts/base.js.coffee'
8-
template 'global.js.coffee', 'app/assets/javascripts/global.js.coffee'
9-
template 'rails_script.rb', 'config/initializers/rails_script.rb'
10-
end
11-
12-
def create_directories
13-
directory 'utilities/', 'app/assets/javascripts/utilities'
14-
directory 'elements/', 'app/assets/javascripts/elements'
7+
template 'base.coffee', 'app/assets/javascripts/base.coffee'
8+
template 'global.coffee', 'app/assets/javascripts/global.coffee'
159
end
1610

1711
def insert_load_order
1812
if File.exist?('app/assets/javascripts/application.js')
1913

2014
if File.readlines('app/assets/javascripts/application.js').grep('//= require_tree .').any?
21-
inject_into_file 'app/assets/javascripts/application.js', "\n//= require base\n//= require_tree ./utilities\n//= require_tree ./elements", before: "\n//= require_tree ."
15+
inject_into_file 'app/assets/javascripts/application.js', "//= require rails_script\n", before: '//= require_tree .'
2216
else
23-
append_file 'app/assets/javascripts/application.js', "\n//= require base\n//= require_tree ./utilities\n//= require_tree ./elements\n//= require_tree ."
17+
append_file 'app/assets/javascripts/application.js', "\n//= require rails_script"
2418
end
2519

2620
elsif File.exist?('app/assets/javascripts/application.js.coffee')
2721

2822
if File.readlines('app/assets/javascripts/application.js.coffee').grep('#= require_tree .').any?
29-
inject_into_file 'app/assets/javascripts/application.js.coffee', "\n#= require base\n#= require_tree ./utilities\n#= require_tree ./elements", before: "\n#= require_tree ."
23+
inject_into_file 'app/assets/javascripts/application.js.coffee', "#= require rails_script\n", before: '#= require_tree .'
3024
else
31-
append_file 'app/assets/javascripts/application.js.coffee', "\n#= require base\n#= require_tree ./utilities\n#= require_tree ./elements\n#= require_tree ."
25+
append_file 'app/assets/javascripts/application.js.coffee', "\n#= require rails_script"
3226
end
3327
end
3428
end

lib/generators/rails_script/install/templates/base.js.coffee

-45
This file was deleted.

lib/generators/rails_script/install/templates/rails_script.rb

-4
This file was deleted.

lib/rails_script.rb

+2-6
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
require 'rails_script/version'
2+
require 'rails_script/engine'
23
require 'rails_script/loader_helper'
34
require 'rails_script/to_javascript'
45
require 'rails_script/railtie' if defined?(Rails)
56

67
module RailsScript
7-
mattr_accessor :app_namespace
8-
@@app_namespace = 'App'
9-
10-
def self.config
11-
yield self
12-
end
8+
139
end

lib/rails_script/engine.rb

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module RailsScript
2+
class Engine < ::Rails::Engine
3+
end
4+
end

lib/rails_script/loader_helper.rb

+5-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ module RailsScript
22
module LoaderHelper
33

44
def include_rails_script
5-
content_tag :div, nil, id: 'rails-script', data: { controller: controller_path.split(/\/|_/).map(&:capitalize).join(''), vars: @to_javascript.to_json }
5+
content_tag :div, nil, id: 'rails-script', data: {
6+
controller: controller_path.split(/\/|_/).map(&:capitalize).join(''),
7+
action: action_name,
8+
vars: @to_javascript.to_json
9+
}
610
end
711

812
end

lib/rails_script/version.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module RailsScript
2-
VERSION = '1.0.0'
2+
VERSION = '2.0.0'
33
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
window.RailsScript ||= {}
2+
window.App ||= {}
3+
window.Element ||= {}
4+
window.Utility ||= {}
5+
6+
$(document).on "turbolinks:load.rails_script", ->
7+
Utility.RailsVars = $('#rails-script').data('vars')
8+
window.$this = new (App["#{$('#rails-script').data('controller')}"] || App.Base)()
9+
10+
action = $('#rails-script').data('action')
11+
12+
if typeof $this.beforeAction == 'function'
13+
$this.beforeAction action
14+
if typeof $this[action] == 'function'
15+
$this[action]()
16+
if typeof $this.afterAction == 'function'
17+
$this.afterAction action
18+
19+
RailsScript.setClearEventHandlers = ->
20+
jQuery(document).on 'turbolinks:before-visit', ->
21+
for element in [window, document]
22+
for event, handlers of (jQuery._data(element, 'events') || {})
23+
for handler in handlers
24+
if handler? && handler.namespace == ''
25+
jQuery(element).off event, handler.handler

0 commit comments

Comments
 (0)