Skip to content

Commit 7f001ba

Browse files
committed
Implement controller beforeAction and afterAction methods in place of all
1 parent 03482e1 commit 7f001ba

File tree

5 files changed

+30
-13
lines changed

5 files changed

+30
-13
lines changed

README.md

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,18 @@ class App.Users extends App.Base
4242

4343
### Controller Specific JavaScript
4444

45-
Executing some JavaScript to run on all controller actions is just a matter of adding it to the ```all``` function. This is run before the action specific method. The example below would print 'users' for each ```Users``` controller action.
45+
Executing some JavaScript to run on all controller actions is just a matter of adding it to the ```beforeAction``` or ```afterAction``` function. ```beforeAction``` is run before all controller action functions and ```afterAction``` is run after all controller action functions. The before and after action functions have an optional argument ```action``` which is string containing the current action name. The example below would print 'before ACTION action' and 'after ACTION action' for each ```Users``` controller action.
4646

4747
```
4848
# app/assets/javascripts/users.js.coffee
4949
window.App ||= {}
5050
class App.Users extends App.Base
5151
52-
all: =>
53-
console.log 'users'
52+
beforeAction: (action) =>
53+
console.log "before #{action} action"
54+
55+
afterAction: (action) =>
56+
console.log "after #{action} action"
5457
```
5558

5659

@@ -237,8 +240,12 @@ Since the above example includes a namespace, it would generate:
237240
window.App ||= {}
238241
class App.SomeNewController extends App.Base
239242
240-
all: =>
243+
beforeAction: (action) =>
241244
return
245+
246+
247+
afterAction: (action) =>
248+
return
242249
243250
244251
index: =>

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
window.App ||= {}
22
class App.<%= controller.gsub('::', '') %> extends App.Base
33

4-
all: =>
4+
beforeAction: (action) =>
5+
return
6+
7+
8+
afterAction: (action) =>
59
return
610

711

@@ -18,4 +22,4 @@ class App.<%= controller.gsub('::', '') %> extends App.Base
1822

1923

2024
edit: =>
21-
return
25+
return

lib/rails_script/loader_helper.rb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,14 @@ def include_rails_script
88
99
(function() {
1010
window.$this = new (App.#{ controller_path.split(/\/|_/).map(&:capitalize).join('') } || App.Base)();
11-
if (typeof $this.all === 'function') {
12-
$this.all.call();
11+
if (typeof $this.beforeAction === 'function') {
12+
$this.beforeAction("#{action_name}");
1313
}
1414
if (typeof $this.#{ action_name } === 'function') {
15-
$this.#{ action_name }.call();
15+
$this.#{ action_name }();
16+
}
17+
if (typeof $this.afterAction === 'function') {
18+
$this.afterAction("#{action_name}");
1619
}
1720
})();
1821
RUBY

lib/rails_script/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module RailsScript
2-
VERSION = '0.5.0'
2+
VERSION = '0.6.0'
33
end

lib/templates/coffee/assets/javascript.js.coffee

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
window.App ||= {}
22
class App.<%= class_name %> extends App.Base
33

4-
constructor: ->
5-
super
6-
return this
4+
beforeAction: (action) =>
5+
return
6+
7+
8+
afterAction: (action) =>
9+
return
710

811

912
index: =>

0 commit comments

Comments
 (0)