rails_script 0.4.1 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 466139880f91bff9dc250cfb577db783e2be2c64
4
- data.tar.gz: 766488d7eabddc2ead1c10d93e9d30e71f8c1daf
3
+ metadata.gz: a9366216ecd08633b2fb945a1179b274ee61afdd
4
+ data.tar.gz: 39d2113ccc01fd63af0819ea6cd876e8895fd6f4
5
5
  SHA512:
6
- metadata.gz: ba3c9eaf4c2c040b877a8614750f490c2421ebe265dccef7577a957a7efd4bc54474898d17e142779cfca4326314b0ac6adc585dd5c9f18e0a4b048878f65b18
7
- data.tar.gz: 427607b7861accd331d93d9e14f42ad7c839c1b48cdb31b45ad33f6314f1fb9b43f1a65bfe536994d23c768afef89efe5c4d9b5e048a2f28c8b1369bdb31872f
6
+ metadata.gz: bff7f4ef8443eae001baf920c046ee73e770c1a6134c35325ec7fd12ed0279bef745612c18bf145e8424fcf24da476d42e297e5eab0770dce9aa5cc9db1dc3d5
7
+ data.tar.gz: 78bcd83907daa4cc47ced13db26154733a5be5185a982d218cb662a3937a6a1dd8a037b52ddb83970b1ea08de8e35ea1bd283f8f7eb21c96841a254464f8e8d7
data/README.md CHANGED
@@ -24,32 +24,9 @@ After the generator finishes, you will be prompted to add helper call to your ap
24
24
 
25
25
  ## Usage
26
26
 
27
- ### Global Functions
28
-
29
- Any functions that need to be accessible in the global scope should be defined in ```global.js.coffee``` using the ```App``` namespace. Below is an example of one of our favorite functions that we use to submit a form using AJAX as a JSON request.
30
-
31
- ```
32
- # app/assets/javascripts/global.js.coffee
33
- window.App ||= {}
34
-
35
- App.remoteSubmission = ($form, onCompleteCallBack) ->
36
- $.ajax
37
- url: $form.attr('action')
38
- type: $form.attr('method')
39
- data: $form.serialize()
40
- dataType: 'json'
41
- complete: (result) ->
42
- onCompleteCallBack(result)
43
-
44
- return
45
- ```
46
-
47
- Now you can access this function from anywhere in the application by just calling ```App.remoteSubmission($('#myForm'), alert('Hello'))```
48
-
27
+ ### Page (Action) Specific JavaScript
49
28
 
50
- ### Page Specific JavaScript
51
-
52
- This is where things get even easier, your JavaScript class is named after your Controller and there is a method for each Controller action. Whenever you generate a Controller, the CoffeeScript file that is generated will define the new JavaScript class and the basic REST actions. This means on ```Users#show``` we can submit that 'follow' request in the background like so:
29
+ Your JavaScript class is named after your Controller and there is a method for each Controller action. Whenever you generate a Controller, the CoffeeScript file that is generated will define the new JavaScript class and the basic REST actions. The example below would print 'users#show' in the console for the ```Users#show``` action.
53
30
 
54
31
  ```
55
32
  # app/assets/javascripts/users.js.coffee
@@ -57,28 +34,25 @@ This is where things get even easier, your JavaScript class is named after your
57
34
  window.App ||= {}
58
35
  class App.Users extends App.Base
59
36
 
60
- show: ->
61
- App.remoteSubmission($('#follow-user-form'), alert('You are now following them!'))
37
+ show: =>
38
+ console.log 'users#show'
62
39
  ```
63
40
 
64
41
 
42
+
65
43
  ### Controller Specific JavaScript
66
44
 
67
- Executing some JavaScript to run on all controller actions is just a matter of adding it to the class constructor. In the below example we will change the background color of the page for all actions in ```UsersController```.
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.
68
46
 
69
47
  ```
70
48
  # app/assets/javascripts/users.js.coffee
71
49
  window.App ||= {}
72
- class App.Example extends App.Base
50
+ class App.Users extends App.Base
73
51
 
74
- constructor: ->
75
- super
76
- $('body').css('background-color', 'yellow')
77
- return this
52
+ all: =>
53
+ console.log 'users'
78
54
  ```
79
55
 
80
- Note the call to ```super``` and the ```return this```, it is very important to keep these. If you wanted your Controller specific JavaScript to run before Application wide JavaScript, then you would call ```super``` just before ```return this```. Returning ```this``` allows the Application layout JavaScript to call the action specific functions.
81
-
82
56
 
83
57
  ### Application Wide JavaScript
84
58
 
@@ -105,6 +79,29 @@ class App.Base
105
79
  In this example we extracted the rollover action into a new function. Doing so will make the class cleaner and easier to maintain as the application grows. Once again note the ```return this``` in the constructor.
106
80
 
107
81
 
82
+
83
+ ### Global Functions
84
+
85
+ Any functions that need to be accessible in the global scope should be defined in ```global.js.coffee``` using the ```App``` namespace. Below is an example of one of our favorite functions that we use to submit a form using AJAX as a JSON request.
86
+
87
+ ```
88
+ # app/assets/javascripts/global.js.coffee
89
+ window.App ||= {}
90
+
91
+ App.remoteSubmission = ($form) ->
92
+ $.ajax
93
+ url: $form.attr('action')
94
+ type: $form.attr('method')
95
+ data: $form.serialize()
96
+ dataType: 'json'
97
+
98
+ return
99
+ ```
100
+
101
+ Now you can access this function from anywhere in the application by just calling ```App.remoteSubmission($('#myForm'))```
102
+
103
+
104
+
108
105
  ### Utilities
109
106
 
110
107
  A ```Utility``` is a class that will be used to create similar functionality in many areas of the application. A good example of this is a Modal, which could appear multiple times on the same page. So, let's encapsulate this functionality in a highly reusable class.
@@ -168,6 +165,7 @@ class App.Users extends App.Base
168
165
  ```
169
166
 
170
167
 
168
+
171
169
  ### Elements
172
170
 
173
171
  An ```Element``` is a class that describes the functionality of a one off element in the application. A Main Menu is a good example of this since there is usually only a single Main Menu.
@@ -202,6 +200,7 @@ class App.Base
202
200
  ```
203
201
 
204
202
 
203
+
205
204
  #### Element Inheritance
206
205
 
207
206
  Inheritance is another key tool for reusability. Let's say our ```Element.MainMenu``` opens and closes in the same way as the ```Utility.Modal```. Well then MainMenu should just extend Modal, this can be accomplished from the generator:
@@ -223,6 +222,7 @@ class Element.MainMenu extends Utility.Modal
223
222
  Inheritance from the generator can only come from a Utility class. Any class you wish to extend should be created as a Utility. The installer adds the line ```//= require_tree ./utilities``` before loading tree to handle this. If you have a utility that extends a utility, then make sure the extended utility is loaded first by explicitly requiring it before ```//= require_tree ./utilities```.
224
223
 
225
224
 
225
+
226
226
  ### Custom Controllers
227
227
 
228
228
  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:
@@ -237,28 +237,28 @@ Since the above example includes a namespace, it would generate:
237
237
  window.App ||= {}
238
238
  class App.SomeNewController extends App.Base
239
239
 
240
- constructor: ->
241
- super
242
- return this
240
+ all: =>
241
+ return
243
242
 
244
243
 
245
- index: ->
244
+ index: =>
246
245
  return
247
246
 
248
247
 
249
- show: ->
248
+ show: =>
250
249
  return
251
250
 
252
251
 
253
- new: ->
252
+ new: =>
254
253
  return
255
254
 
256
255
 
257
- edit: ->
256
+ edit: =>
258
257
  return
259
258
  ```
260
259
 
261
- None of the pre-defined functions are necessary, you can remove the ones you don't want.
260
+ None of the pre-defined functions are necessary, you can remove the ones you don't need.
261
+
262
262
 
263
263
 
264
264
  ### Generic Classes
@@ -280,6 +280,8 @@ class App.MyClassName
280
280
 
281
281
  ```
282
282
 
283
+
284
+
283
285
  ### Passing Rails Variables
284
286
 
285
287
  To pass data from Rails to JavaScript, just call ```to_javascript``` along with a hash of your data. This is then converted to a JSON object with ```to_javascript.to_json``` and can be accessed with ```Utility.RailsVars```. The ```to_javascript``` helper may be called from multiple points in the application, all data is merged together.
@@ -315,6 +317,7 @@ class App.Users extends App.Base
315
317
  ```
316
318
 
317
319
 
320
+
318
321
  ### Events
319
322
 
320
323
  Since Turbolinks doesn't refresh the page and only replaces the body, event listeners defined on ```window``` and ```document``` carry between page loads. To avoid these event listeners stacking, RailsScript will destroy all event listeners on ```window``` and ```document``` that have a blank namespace, i.e. ```$(window).on 'scroll', myHandler```. If you need an event handler to persist between page changes, then define a namespace, i.e. ```$(window).on 'scroll.namespace', myHandler```.
@@ -1,9 +1,8 @@
1
1
  window.App ||= {}
2
2
  class App.<%= controller.gsub('::', '') %> extends App.Base
3
3
 
4
- constructor: ->
5
- super
6
- return this
4
+ all: =>
5
+ return
7
6
 
8
7
 
9
8
  index: =>
@@ -8,8 +8,11 @@ Utility.RailsVars = #{@to_javascript.nil? ? '{}' : @to_javascript.to_json};
8
8
 
9
9
  (function() {
10
10
  window.$this = new (App.#{ controller_path.split(/\/|_/).map(&:capitalize).join('') } || App.Base)();
11
+ if (typeof $this.all === 'function') {
12
+ $this.all.call();
13
+ }
11
14
  if (typeof $this.#{ action_name } === 'function') {
12
- return $this.#{ action_name }.call();
15
+ $this.#{ action_name }.call();
13
16
  }
14
17
  })();
15
18
  RUBY
@@ -1,3 +1,3 @@
1
1
  module RailsScript
2
- VERSION = '0.4.1'
2
+ VERSION = '0.5.0'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_script
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kevin Pheasey