reins 0.0.3 → 0.0.4

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: afbdfa48fc19a1f413c5f71527218bb1daaf3d3a
4
- data.tar.gz: 612f9ff4237d55935bcbf3cf0b88c689c732ed57
3
+ metadata.gz: 4c94c1362681a7d1fc9abb25e1cdbcc9d77a5136
4
+ data.tar.gz: 5f59f0adb23f90acb5f8d448f9044088d8ce995e
5
5
  SHA512:
6
- metadata.gz: d1aaa49ceda92281a40d5c96bdac4a8e598b2a026cca0ddd6ce9858b179adf545f5ba99681c067f26718dc3e980db539b7aa7abf3e5c8fe9cc52842256e6cc65
7
- data.tar.gz: b9d4833090d527baeaf94a5ce298754e334d140023b8666d1cb380a67462e7debc5756f8ca78673ba5b2f5ffb4306f9d989fc967eff22810b2840ee38df35de0
6
+ metadata.gz: 9178864998b34cb92cd9a624f894291661b44267f37d3eb53c7555026846f1680e5e5409862fd5dafd3f4349a680ad239ebf0c0523253c7a5947375948b9112f
7
+ data.tar.gz: 555e9f4427efa2de4ec4d63cbeb9e4d395fe4e1021206e1c0ecc3e5ab5ee5ba36d99cdbcdfa09661a6d9d63a64e62ae61d1926c07715c9618a759dfae52f8ed1
data/README.md CHANGED
@@ -1,30 +1,182 @@
1
- # Reins [![Gem Version](https://badge.fury.io/rb/reins.svg)](http://badge.fury.io/rb/reins) [![wercker status](https://app.wercker.com/status/6ea31467d299403dc3bac9f6bd962d96/s/master "wercker status")](https://app.wercker.com/project/bykey/6ea31467d299403dc3bac9f6bd962d96) ![](http://ruby-gem-downloads-badge.herokuapp.com/reins)
1
+ # Reins!
2
2
 
3
- Reins is a ruby gem that automates the creation of javascript controllers that match the structure of your rails controllers.
4
- It enables you to keep javascript code out of your views by defining namespaced functions that get called automatically for each rails controller action that runs.
5
-
6
- This even works in the case of javascript code that needs data from the server (which almost always ends up inside a <script> tag somewhere with <%%> tags...).
7
- You just have to define the @reins_params hash on the controller and this hash will be JSONified and passed to the javascript controller as a dictionary object.
8
-
9
- Reins can be used with or without turbolinks.
3
+ #### Reins brings some convention over configuration to your Rails application's javascript.
10
4
 
5
+ [![wercker status](https://app.wercker.com/status/6ea31467d299403dc3bac9f6bd962d96/s/master "wercker status")](https://app.wercker.com/project/bykey/6ea31467d299403dc3bac9f6bd962d96)
6
+ [![Code Climate](https://codeclimate.com/github/weareredlight/reins/badges/gpa.svg)](https://codeclimate.com/github/weareredlight/reins)
7
+ [![Test Coverage](https://codeclimate.com/github/weareredlight/reins/badges/coverage.svg)](https://codeclimate.com/github/weareredlight/reins)
8
+ [![Gem Version](https://badge.fury.io/rb/reins.svg)](http://badge.fury.io/rb/reins)
9
+ ![](http://ruby-gem-downloads-badge.herokuapp.com/reins)
11
10
 
12
- To start using it:
11
+ ### Why reins?
12
+ Rails' convention over configuration approach to development is well known, however when it comes to javascript it is sorely missing.
13
+ Every time you create a scaffold, resource, or controller you get a new javascript file with that controller's name, but that's it.
14
+ Since all the code is minified and aggregated into application.js by the assets pipeline, it's very easy to end up with a big mess.
13
15
 
14
- add to your gemfile:
16
+ This means that you need to manually find a way to keep the global scope clean, avoid name collisions and define a common structure for your javascript code.
15
17
 
16
- gem 'reins'
17
18
 
19
+ Reins is a ruby gem that automates the creation of javascript controllers that match the structure of your rails controllers.
20
+ It creates a predefined structure for your javascript code where code for different controllers is kept in separate namespaces.
21
+ It also enables you to keep javascript code out of your views by defining functions that get called automatically on jQuery.ready and Turbolinks page:load for each rails controller action that runs.
18
22
 
19
- add to application.js:
23
+ This even works in the case of javascript code that needs data from the server (which almost always ends up inside a script tag somewhere with erb thrown in the mix to include server-side data...).
24
+ You just have to define a special hash on the controller and this hash will be JSONified and passed to the reins javascript controller as a dictionary object.
20
25
 
21
- //= require reins
26
+ ### How can I start using it?
22
27
 
28
+ Add reins to your Gemfile:
23
29
 
24
- add to layouts/*.html.erb, right before </body> closing tag:
30
+ ```
31
+ gem 'reins'
32
+ ```
25
33
 
26
- <%= reins_script_tag %>
34
+ Add reins.js to application.js:
27
35
 
36
+ ```
37
+ //= require reins
38
+ ```
28
39
 
40
+ Add the reins script tag to your application layout, or any layout/view where you want reins to automatically call your javascript controllers.
41
+ I prefer just adding it to ```layouts/application.html.erb``` (and any other layouts I might have) right before closing the ```<body>``` tag:
29
42
 
30
- Reins is licenced under the MIT License. Please see the file MIT-LICENSE for more details.
43
+ ```
44
+ ...
45
+ <body>
46
+ ...
47
+ <%= yield %>
48
+ ...
49
+ <%= reins_script_tag %>
50
+ </body>
51
+ ...
52
+ ```
53
+
54
+ ### Ok, what now?
55
+
56
+ After reins in installed, every time you generate a new scaffold/resource/controller you will get a new javascript file inside app/assets/controllers/resource_name.js that will have a specific structure.
57
+
58
+ If the above steps went ok, now is the perfect time for an example:
59
+
60
+ Suppose we generate a HelloController and add the following code inside:
61
+
62
+ ```
63
+ class HelloController < ApplicationController
64
+
65
+ def index
66
+ @reins_params = {
67
+ name: 'Lebowski'
68
+ }
69
+ end
70
+
71
+ end
72
+ ```
73
+
74
+ Let's quickly create a controller and view by running:
75
+ ```
76
+ rails g controller hello
77
+ touch app/views/hello/index.html.erb
78
+ ```
79
+ Don't worry about the empty view for now, as it is irrelevant for reins to work :)
80
+ When you run the rails generator, reins will create a javascript file at
81
+ /app/assets/javascripts/controllers/cookies.js with some boilerplate code. After
82
+ changing a few lines it might look like this:
83
+
84
+ ```
85
+ /**
86
+ * Anonymous self-running closure to avoid polluting the global namespace with
87
+ * definitions that should be kept private.
88
+ */
89
+ +function() {
90
+
91
+ /**
92
+ * Publicly accessible functions related to the hello rails
93
+ * controller.
94
+ * These can be called from anywhere like this:
95
+ * r.hello.function_name(arg1, arg2);
96
+ *
97
+ * You can declare your javascript controllers inside this namespace. They
98
+ * should have the same name as your rails actions, but starting with an
99
+ * underscore (so for the show action, the corresponding function would be
100
+ * named _show).
101
+ *
102
+ * They will be called automatically by reins every time the corresponding
103
+ * action renders an HTML view, or manually from a js.erb view by using the
104
+ * call_reins_controller helper.
105
+ */
106
+ window.r.hello = {
107
+
108
+ /**
109
+ * Controller action corresponding to the #index rails action.
110
+ *
111
+ * This function is called on $.ready or page:load every time that the
112
+ * "index" action renders an html view.
113
+ */
114
+ _index: function(params) {
115
+ this.greet_user(params.name);
116
+ },
117
+
118
+
119
+ /**
120
+ * You can also declare your own functions here.
121
+ *
122
+ * Call this one from anywhere like this:
123
+ * r.hello.greet_user('John Doe');
124
+ * Or from inside this namespace with:
125
+ * this.greet_user('John Doe');
126
+ */
127
+ greet_user: function(name) {
128
+ show_msg('Have a nice day ' + name + '!');
129
+ }
130
+
131
+ };
132
+
133
+
134
+ ////////////////////////////////////////////////////////////////////////////
135
+ // Anything below this but still inside the anonymous function is private //
136
+ ////////////////////////////////////////////////////////////////////////////
137
+
138
+ /**
139
+ * This is a private function. It can be called by any function inside this
140
+ * closure, including the controllers above, but it's invisible to the outside
141
+ * world.
142
+ *
143
+ * It can be called from anywhere inside this closure like this:
144
+ * my_function2('Hi there!');
145
+ * It cannot be called from anywhere else.
146
+ */
147
+ function show_msg(msg) {
148
+ alert(msg);
149
+ }
150
+
151
+ }();
152
+ ```
153
+
154
+ Functions inside the window.r.hello namespace that start with an underscore and match the name of a rails action will be called automatically by reins when that action is rendered. You can declare a hash called @reins_params on the rails controller and it will be JSONified and passed to your javascript controller as an argument.
155
+ Most of the functionality is explained in the comments inside the javascript snippet above.
156
+
157
+ ### More stuff
158
+
159
+ #### What if I'm rendering a js.erb view?
160
+
161
+ Reins will not be called automatically for these, as the whole idea of js.erb views is to give you full control.
162
+ However, to avoid repeating code, you can call your reins controller from js.erb views.
163
+ You just need to do this:
164
+ ```
165
+ <%= call_reins_controller %>
166
+ ```
167
+ This will call the matching controller and pass the @reins_params hash if it was defined in the controller.
168
+
169
+ #### Why do my controllers only get called on page:load when using turbolinks? I want page:change!
170
+
171
+ Just add this to your application.js:
172
+ ```
173
+ $(document).off('page:load').on('page:change', r.call_js_controller);
174
+ ```
175
+ THIS WILL REMOVE any page:load event handlers that you may have attached to $(document), so be warned!
176
+
177
+ Also of note is that calling the reins controller in this way will use the last params that were sent by the server.
178
+ This means that if you are going back to a page that was cached by turbolinks, the controller could be called with params from another controller/action, which is probably not what you want.
179
+
180
+
181
+
182
+ Reins is licenced under the MIT License. Please see the file MIT-LICENSE for details.
@@ -1,64 +1,44 @@
1
1
  /**
2
- * Anonymous self-running function to keep us from polluting the global namespace
3
- * with definitions that should be kept private.
2
+ * Anonymous self-running closure to avoid polluting the global namespace with
3
+ * definitions that should be kept private.
4
4
  */
5
5
  +function() {
6
6
 
7
7
  /**
8
- * Publicly accessible functions related to <%= file_name %>.
9
- * These can be called from anywhere as r.<%= file_name %>.function_name(arg1, arg2);
8
+ * Publicly accessible functions related to the <%= file_name %> rails
9
+ * controller.
10
+ * These can be called from anywhere like this:
11
+ * r.<%= file_name %>.function_name(arg1, arg2);
10
12
  *
11
- * This scope includes the JS controller action functions corresponding to the
12
- * rails <%= file_name %> controller, so keep in mind that funcions
13
- * that match the names of the actions will be called automatically by rails.
13
+ * You can declare your javascript controllers inside this namespace. They
14
+ * should have the same name as your rails actions, but starting with an
15
+ * underscore (so for the show action, the corresponding function would be
16
+ * named _show).
17
+ *
18
+ * They will be called automatically by reins every time the corresponding
19
+ * action renders an HTML view, or manually from a js.erb view by using the
20
+ * call_reins_controller helper.
14
21
  */
15
22
  window.r.<%= file_name %> = {
16
23
 
17
24
  /**
18
25
  * Controller action corresponding to the #new rails action.
19
- * This code is run everytime that the new.html format is rendered by rails.
26
+ *
27
+ * This function is called on $.ready or page:load every time that the
28
+ * "new" action renders an html view.
20
29
  */
21
30
  // _new: function() {
22
31
  // },
23
32
 
24
33
 
25
- /**
26
- * Controller action corresponding to the #create rails action.
27
- * This code is run everytime that the create.html format is rendered by rails.
28
- */
29
- // _create: function(params) {
30
- // },
31
-
32
-
33
- /**
34
- * Controller action corresponding to the #index rails action.
35
- * This code is run everytime that the create.html format is rendered by rails.
36
- */
37
- // _index: function(params) {
38
- // },
39
-
40
-
41
34
  /**
42
35
  * Controller action corresponding to the #show rails action.
43
- * This code is run everytime that the create.html format is rendered by rails.
36
+ *
37
+ * This function is called on $.ready or page:load every time that the
38
+ * "show" action renders an html view.
44
39
  */
45
40
  // _show: function(params) {
46
- // },
47
-
48
-
49
- /**
50
- * Controller action corresponding to the #update rails action.
51
- * This code is run everytime that the create.html format is rendered by rails.
52
- */
53
- // _update: function(params) {
54
- // },
55
-
56
-
57
- /**
58
- * Controller action corresponding to the #destroy rails action.
59
- * This code is run everytime that the create.html format is rendered by rails.
60
- */
61
- // _destroy: function(params) {
41
+ // this.my_function(params.name);
62
42
  // },
63
43
 
64
44
 
@@ -67,9 +47,11 @@
67
47
  *
68
48
  * Call this one from anywhere like this:
69
49
  * r.<%= file_name %>.my_function('John Doe');
50
+ * Or from inside this namespace with:
51
+ * this.my_function('John Doe');
70
52
  */
71
53
  // my_function: function(name) {
72
- // alert('Have a nice day ' + name + '!');
54
+ // my_function2('Have a nice day ' + name + '!');
73
55
  // }
74
56
 
75
57
  };
@@ -84,9 +66,13 @@
84
66
  * This is a private function. It can be called by any function inside this
85
67
  * closure, including the controllers above, but it's invisible to the outside
86
68
  * world.
69
+ *
70
+ * It can be called from anywhere inside this closure like this:
71
+ * my_function2('Hi there!');
72
+ * It cannot be called from anywhere else.
87
73
  */
88
- // function my_function2() {
89
- // // do stuff...
74
+ // function my_function2(msg) {
75
+ // alert(msg);
90
76
  // }
91
77
 
92
78
 
@@ -1,3 +1,3 @@
1
1
  module Reins
2
- VERSION = '0.0.3'
2
+ VERSION = '0.0.4'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: reins
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tony Goncalves
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-22 00:00:00.000000000 Z
11
+ date: 2015-05-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails