rails_script 0.0.7 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: de3fe1e20520d3125d8c02b2d9f4681e5c840de2
4
- data.tar.gz: c356853b2af063bf9ae9f1ccf8152599c7603e19
3
+ metadata.gz: e1158eac848d3653884eb4a115085124d41cb56a
4
+ data.tar.gz: 8fa77658f9637b99cc3dc770c271c640ccd3c4f6
5
5
  SHA512:
6
- metadata.gz: 8e7b9c229aec1887b80f2318dfea2c164101bacf802fb8d93dbb78524dfd6d1fd356590d165eca8356bc5419c53a4856901d0a26c0c3ea9d3f4af03a2fa48bd8
7
- data.tar.gz: b5c492541e160b797d1ac4dbc788433c6c304b2e917a0b5cddeca39f13b89fdaa6a3342e3c41fd8baed1ce8fee943ae10ea75e9039d5ccf0e9d33e643aa61a90
6
+ metadata.gz: b9e718d2fc47235da59cf1205528b494174e3f0269375099931eccd64240f4000e400458b12a6e3b524ea2db60f81a5c2d2f47d01a7fd6513cd060b7dd33c1d9
7
+ data.tar.gz: 45a80a07e93adbc9da211fb5c6dbc5770c8e3047ac0dfac0eca2f34bbe746de27b16f1a405a618f4bc1c897876d048fa6adaf690d953aef4b2d0254ab4313e6a
data/README.md CHANGED
@@ -105,7 +105,129 @@ class App.Base
105
105
  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 contructor.
106
106
 
107
107
 
108
- ### Generating New Controllers
108
+ ### Utilities
109
+
110
+ 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.
111
+
112
+ First, generate the ```Utility```
113
+
114
+ $ rails g rails_script:utility Modal
115
+
116
+ This will create the following in ```/app/assets/javascripts/utilities/modal.js.coffee```:
117
+
118
+ ```
119
+ # /app/assets/javascripts/utilities/modal.js.coffee
120
+ window.App ||= {}
121
+ window.App.Utility ||= {}
122
+ class App.Utility.Modal
123
+
124
+ constructor: ->
125
+ return this
126
+ ```
127
+
128
+ Let's add some basic functionality:
129
+
130
+ ```
131
+ # /app/assets/javascripts/utilities/modal.js.coffee
132
+ window.App ||= {}
133
+ window.App.Utility ||= {}
134
+ class App.Utility.Modal
135
+ @element
136
+ @trigger
137
+ @isOpen = false
138
+
139
+ constructor: ($element, $trigger) ->
140
+ @element = $element
141
+ @trigger = $trigger
142
+ @trigger.on 'click', @toggle
143
+ return this
144
+
145
+
146
+ toggle: (event) =>
147
+ event.preventDefault()
148
+ if @isShowing then @close() else @open()
149
+
150
+
151
+ open: =>
152
+ @isShowing = true
153
+ @element.show()
154
+
155
+
156
+ close: =>
157
+ @isShowing = false
158
+ @element.fadeOut('fast')
159
+ ```
160
+
161
+ Now, here's how we use the utility from ```users#show```
162
+
163
+ ```
164
+ # app/assets/javascripts/users.js.coffee
165
+
166
+ window.App ||= {}
167
+ class App.Users extends App.Base
168
+
169
+ show: ->
170
+ @galleryModal = new App.Utility.Modal($('#user-gallery-modal-wrapper'), $('user-gallery-modal-toggle-button'))
171
+
172
+ ```
173
+
174
+
175
+ ### Elements
176
+
177
+ An ```Element``` is a class that describes the funcionality of a one off element in the applicattion. A Main Menu is a good example of this since there is usually only a single Main Menu.
178
+
179
+ First generate the ```Element```
180
+
181
+ $ rails g rails_script:element MainMenu
182
+
183
+ This will create the following in ```/app/assets/javascripts/elements/main_menu.js.coffee```
184
+
185
+ ```
186
+ # /app/assets/javascripts/elements/main_menu.js.coffee```
187
+
188
+ window.App.Element ||= {}
189
+ class App.Element.MainMenu
190
+
191
+ constructor: ->
192
+ return this
193
+ ```
194
+
195
+ We can now add all the logic for the main menu in a separate class and call it on every page like so:
196
+
197
+ ```
198
+ # app/assets/javascripts/base.js.coffee
199
+
200
+ window.App ||= {}
201
+ class App.Base
202
+
203
+ constructor: ->
204
+ App.mainMenu = new App.Element.MainMenu()
205
+ return this
206
+ ```
207
+
208
+
209
+ #### Element Inheritance
210
+
211
+ 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:
212
+
213
+ $ rails g rails:script MainMenu Modal
214
+
215
+ Which generates:
216
+
217
+ ````
218
+ # /app/assets/javascripts/elements/main_menu.js.coffee
219
+
220
+ window.App.Element ||= {}
221
+ class App.Element.MainMenu extends App.Utility.Modal
222
+
223
+ constructor: ->
224
+ return this
225
+ ````
226
+
227
+ 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```.
228
+
229
+
230
+ ### Custom Controllers
109
231
 
110
232
  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:
111
233
 
@@ -143,6 +265,28 @@ class App.SomeNewController extends App.Base
143
265
  None of the pre-defined functions are necessary, you can remove the ones you don't want.
144
266
 
145
267
 
268
+ ### Generic Classes
269
+
270
+ To generate a generic class that isn't a Utility, Element or Controller, just use the following:
271
+
272
+ $ rails g rails_script:class My::ClassName
273
+
274
+ Which generates:
275
+
276
+ ```
277
+ # /app/assets/javascripts/my/class_name.js.coffee
278
+
279
+ window.App ||= {}
280
+ class App.MyClassName
281
+
282
+ constructor: ->
283
+ return this
284
+
285
+ ```
286
+
287
+
288
+
289
+
146
290
  ## Contributing
147
291
 
148
292
  1. Fork it ( https://github.com/[my-github-username]/rails_script/fork )
File without changes
@@ -0,0 +1,14 @@
1
+ module RailsScript
2
+ module Generators
3
+ class ElementGenerator < ::Rails::Generators::Base
4
+ source_root File.expand_path("../templates", __FILE__)
5
+ argument :element_name, type: :string
6
+ argument :utility, type: :string, default: ''
7
+
8
+ def generate_file
9
+ template 'javascript.js.coffee', "app/assets/javascripts/elements/#{element_name.underscore}.js.coffee"
10
+ end
11
+
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,7 @@
1
+ window.App ||= {}
2
+ window.App.Element ||= {}
3
+ class App.Element.<%= element_name.gsub('::', '') %> <%= "extends App.Utility.#{utility.gsub('::', '')}" unless utility.blank? %>
4
+
5
+ constructor: ->
6
+ super
7
+ return this
@@ -8,11 +8,27 @@ module RailsScript
8
8
  template 'global.js.coffee', 'app/assets/javascripts/global.js.coffee'
9
9
  end
10
10
 
11
+ def create_directories
12
+ directory 'utilities/', 'app/assets/javascripts/utilities'
13
+ directory 'elements/', 'app/assets/javascripts/elements'
14
+ end
15
+
11
16
  def insert_load_order
12
17
  if File.exist?('app/assets/javascripts/application.js')
13
- inject_into_file 'app/assets/javascripts/application.js', "\n//= require base", before: "\n//= require_tree ."
18
+
19
+ if File.readlines('app/assets/javascripts/application.js').grep('//= require_tree .').any?
20
+ inject_into_file 'app/assets/javascripts/application.js', "\n//= require base\n//= require_tree ./utilities", before: "\n//= require_tree ."
21
+ else
22
+ append_file 'app/assets/javascripts/application.js', "\n//= require base\n//= require_tree ./utilities\n//= require_tree ."
23
+ end
24
+
14
25
  elsif File.exist?('app/assets/javascripts/application.js.coffee')
15
- inject_into_file 'app/assets/javascripts/application.js.coffee', "\n#= require base", before: "\n#= require_tree ."
26
+
27
+ if File.readlines('app/assets/javascripts/application.js.coffee').grep('#= require_tree .').any?
28
+ inject_into_file 'app/assets/javascripts/application.js.coffee', "\n#= require base\n#= require_tree ./utilities", before: "\n#= require_tree ."
29
+ else
30
+ append_file 'app/assets/javascripts/application.js.coffee', "\n#= require base\n#= require_tree ./utilities\n#= require_tree ."
31
+ end
16
32
  end
17
33
  end
18
34
 
File without changes
@@ -0,0 +1,7 @@
1
+ window.App ||= {}
2
+ window.App.Utility ||= {}
3
+ class App.Utility.<%= utility_name.gsub('::', '') %>
4
+
5
+ constructor: ->
6
+ super
7
+ return this
@@ -0,0 +1,13 @@
1
+ module RailsScript
2
+ module Generators
3
+ class UtilityGenerator < ::Rails::Generators::Base
4
+ source_root File.expand_path("../templates", __FILE__)
5
+ argument :utility_name, type: :string
6
+
7
+ def generate_file
8
+ template 'javascript.js.coffee', "app/assets/javascripts/utilities/#{utility_name.underscore}.js.coffee"
9
+ end
10
+
11
+ end
12
+ end
13
+ end
@@ -1,3 +1,3 @@
1
1
  module RailsScript
2
- VERSION = '0.0.7'
2
+ VERSION = '0.1.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_script
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kevin Pheasey
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-09-14 00:00:00.000000000 Z
11
+ date: 2014-09-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -79,10 +79,16 @@ files:
79
79
  - lib/generators/rails_script/controller/USAGE
80
80
  - lib/generators/rails_script/controller/controller_generator.rb
81
81
  - lib/generators/rails_script/controller/templates/javascript.js.coffee
82
+ - lib/generators/rails_script/element/USAGE
83
+ - lib/generators/rails_script/element/element_generator.rb
84
+ - lib/generators/rails_script/element/templates/javascript.js.coffee
82
85
  - lib/generators/rails_script/install/USAGE
83
86
  - lib/generators/rails_script/install/install_generator.rb
84
87
  - lib/generators/rails_script/install/templates/base.js.coffee
85
88
  - lib/generators/rails_script/install/templates/global.js.coffee
89
+ - lib/generators/rails_script/utility/USAGE
90
+ - lib/generators/rails_script/utility/templates/javascript.js.coffee
91
+ - lib/generators/rails_script/utility/utility_generator.rb
86
92
  - lib/rails_script.rb
87
93
  - lib/rails_script/loader_helper.rb
88
94
  - lib/rails_script/railtie.rb