happy_place 0.0.1 → 0.0.2

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: 9a0ca9961aa7ce72e1430811b7ddc2fde64b43b4
4
- data.tar.gz: ce2bcc45125449f60a1fba8bb293286cce344eb2
3
+ metadata.gz: 056e62b5797b23e69b1e0cae4158020d0aee4d4a
4
+ data.tar.gz: e47b08f4ee4d10d4b25c607a71110621a13550b9
5
5
  SHA512:
6
- metadata.gz: cee773a6407f2021bdab9608ab0409adb95290839802de7a76fc80caf7de299839ecbc7130ef0f1b3d97abe66d99191f0f2eeab6181ecad3b2e837760dc3f9e2
7
- data.tar.gz: e7615e937d451b6c695cfb698c180223a61e71892b6fd5c40ce65de7d399612fda8d9a5109e83b766313595992db8bb4a0ff06953aa1f7a4bf7287dc10db2804
6
+ metadata.gz: 3ce1d9a81d9aebb0b26e178e9d0d42cbe7a555f21ef12e751eee2c16d8599fb09a992a00c1c332e40036204daadc3be6639e086b68f131dcda56008cd995372d
7
+ data.tar.gz: bea28f72651714c95facdc577c3ddfa60d29be862e1679dae435b5e4ac1e4fd20dfcf1a473b5acdf78cac06b7305b0bbcc220d4c8d678db0ee7f972f761292a2
data/README.md CHANGED
@@ -1,13 +1,28 @@
1
- # HappyPlace
1
+ happy_place
2
+ ============
3
+ | Project | Gem Release |
4
+ |------------------------ | ----------------- |
5
+ | Gem name | happy_place |
6
+ | License | [MIT](LICENSE.txt) |
7
+ | Version | [![Gem Version](https://badge.fury.io/rb/happy_place.png)](http://badge.fury.io/rb/happy_place) |
8
+ | Continuous Integration | [![Build Status](https://travis-ci.org/mfpiccolo/happy_place.png?branch=master)](https://travis-ci.org/mfpiccolo/happy_place)
9
+ | Test Coverage | [![Coverage Status](https://coveralls.io/repos/mfpiccolo/happy_place/badge.png?branch=master)](https://coveralls.io/r/mfpiccolo/happy_place?branch=coveralls)
10
+ | Grade | [![Code Climate](https://codeclimate.com/github/mfpiccolo/happy_place.png)](https://codeclimate.com/github/mfpiccolo/happy_place)
11
+ | Dependencies | [![Dependency Status](https://gemnasium.com/mfpiccolo/happy_place.png)](https://gemnasium.com/mfpiccolo/happy_place)
12
+ | Homepage | [http://mfpiccolo.github.io/happy_place][homepage] |
13
+ | Documentation | [http://rdoc.info/github/mfpiccolo/happy_place/frames][documentation] |
14
+ | Issues | [https://github.com/mfpiccolo/happy_place/issues][issues] |
2
15
 
3
- TODO: Write a gem description
16
+ ## Description
17
+
18
+ A happy place for javascript in Rails
4
19
 
5
20
  ## Installation
6
21
 
7
22
  Add this line to your application's Gemfile:
8
23
 
9
24
  ```ruby
10
- gem 'happy_place'
25
+ gem "happy_place"
11
26
  ```
12
27
 
13
28
  And then execute:
@@ -18,14 +33,244 @@ Or install it yourself as:
18
33
 
19
34
  $ gem install happy_place
20
35
 
21
- ## Usage
36
+ ## Features
37
+ happy_place adds a js method to your controller. This method accepts the following keyword arguments:
38
+
39
+ `js_class:` String name of the js class that you want to user
40
+
41
+ `function:` Sting of the name of the function you would like to call
42
+
43
+ `partial:` String of the partial name and path that you would like to render
44
+
45
+ `args:` Hash of arguments that you would like to be available in your js function
46
+
47
+ Both partial and keys passed from args will be available in your js function by accessing the object passed in.
48
+
49
+ ```ruby
50
+ class ExampleContorller
51
+ def index
52
+ @examples = Example.all
53
+
54
+ respond_to do |format|
55
+ format.js {
56
+ js(js_class: "ExampleClass",
57
+ function: "doSomething",
58
+ partial: "some_partial",
59
+ args: {first_id: @examples.first.id}
60
+ )
61
+ }
62
+ format.html
63
+ end
64
+ end
65
+ end
66
+ ```
67
+
68
+ ```coffeescript
69
+ class this.ExampleClass
70
+ constructor: ->
71
+
72
+ @doSomething: (args) ->
73
+ html_to_append = args.partial
74
+ first_id = args.first_id
75
+ alert(html_to_append)
76
+ alert(first_id)
77
+ ```
78
+
79
+ happy_place will infer the js_class and function name if you name it the same as your controller and action.
80
+
81
+ ```ruby
82
+ class ExampleContorller
83
+ def index
84
+ @examples = Example.all
85
+
86
+ respond_to do |format|
87
+ format.js {
88
+ js(
89
+ partial: "some_partial",
90
+ args: {first_id: @examples.first.id}
91
+ )
92
+ }
93
+ format.html
94
+ end
95
+ end
96
+ end
97
+ ```
98
+
99
+ ```coffeescript
100
+ class this.ExampleController
101
+ constructor: ->
102
+
103
+ @index: (args) ->
104
+ html_to_append = args.partial
105
+ first_id = args.first_id
106
+ alert(html_to_append)
107
+ alert(first_id)
108
+ ```
109
+
110
+ If your controller action is only ever going to be hit by one format type or you want the same js to run for both html or js formats then you don't need the responds to and you can use the js method directly in the action definition.
111
+
112
+ Also if you do not need to pass along args or a partial then you can simply call js
113
+
114
+ ```ruby
115
+ class ExampleContorller
116
+ def index
117
+ @examples = Example.all
118
+
119
+ js
120
+ end
121
+ end
122
+ ```
123
+ ```coffeescript
124
+ class this.ExampleController
125
+ constructor: ->
126
+
127
+ @index: ->
128
+ alert "Huzza!)
129
+ ```
130
+
131
+ ## Naming and Directory Structure
132
+ Technically you can put your code anywhere you want but to make it to your happy place you should follow the naming and directory structure used by rails.
22
133
 
23
- TODO: Write usage instructions here
134
+ If you are adding code that is conroller and action specific, then add a directory called controllers in your `app/assets/javascripts` directory. If your controllers are namespaced then namespace them just like you do in your rails controllers. Here is an example of a namespaced coffee class:
135
+
136
+ ```coffeescript
137
+ # app/assets/javascripts/controllers/admin/special/orders_controller
138
+ this.Admin or= {};
139
+ Admin.Special or= {};
140
+
141
+ class this.Admin.Special.OrdersController
142
+ constructor: ->
143
+
144
+ @index: (args) ->
145
+ alert("Do some js stuff here...")
146
+ ```
147
+
148
+ Make note of the or=. This is to make sure that you don't overwrite the js object if it already exists.
149
+
150
+ Use this same naming and directory structure for all your js. If you are creating service objects then put them in `app/assets/javascripts/services`
151
+
152
+ Remember to add your paths to the manifest so sprockets can load them:
153
+
154
+ ```
155
+ //= require_tree ./controllers
156
+ //= require_tree ./services
157
+ ```
158
+
159
+ Or require them explicitly:
160
+
161
+ `//= require controllers/admin/special/orders_controller`
162
+
163
+
164
+ ## Example Usecases
165
+
166
+ ### Stop using js views after remote actions!
167
+
168
+ Lets say you have a blog where you can see a list of posts. (Imagine that!) You use the Posts#index to display this and it is loaded noramally with :html.
169
+
170
+ ```html
171
+ <!-- posts/index.html.erb -->
172
+ <div class="posts-table">
173
+ <%= render partial: "posts_table" %>
174
+ </div>
175
+ ```
176
+
177
+ ```html
178
+ <!-- posts/_posts_table.html.erb -->
179
+ <table>
180
+ <thead>
181
+ <tr>
182
+ <th>ID</th>
183
+ <th>Name</th>
184
+ <th>Description</th>
185
+ <th>Content</th>
186
+ <th></th>
187
+ </tr>
188
+ </thead>
189
+ <tbody>
190
+ <% @posts.each do |p| %>
191
+ <tr>
192
+ <td><%= p.id %></td>
193
+ <td><%= p.name %></td>
194
+ <td><%= p.description %></td>
195
+ <td><%= p.state.humanize.titlecase %></td>
196
+ <td class="button-column">
197
+ <%= link_to post_path(p) %>
198
+ </td>
199
+ </tr>
200
+ <% end %>
201
+ </tbody>
202
+ </table>
203
+ ```
204
+
205
+ However you want to be able to updat the posts table with a filter that uses ajax. Normally, you would create view of `views/posts/index.coffee` with the following code:
206
+
207
+ ```coffee
208
+ # views/posts/index.coffee
209
+ $(".posts-table).html("""<%=j render(partial: "/posts_table", posts: @posts ) %>""");
210
+ ```
211
+
212
+ I really do not like this patern. This leaves your you javascript spread out all over your application and it is hard to keep track your apps workflow.
213
+
214
+ Here is my happy place for this
215
+
216
+ Step 1. Add the gem to gemfile and bundle
217
+
218
+ Step 2. In the controller action you are interested in handling with js:
219
+
220
+ ```ruby
221
+ class PostsController
222
+ def index
223
+ @posts = Posts.all
224
+
225
+ respond_to do |format|
226
+ format.js { js partial: "posts" }
227
+ format.html
228
+ end
229
+ end
230
+ end
231
+ ```
232
+
233
+ Step 3. in `app/assets/javascripts/controllers/posts_controller.coffee`
234
+
235
+ ```ruby
236
+ class this.PostsController
237
+ constructor: ->
238
+
239
+ @index: (args) ->
240
+ $(".posts_table").html(args.partial);
241
+ ```
242
+
243
+ Step 4. Add `assets/controllers` to your manifest application.js
244
+
245
+ ```
246
+ //= require_tree ./controllers
247
+ ```
248
+
249
+ Step 5. Rejoice!
250
+
251
+ ## Donating
252
+ Support this project and [others by mfpiccolo][gittip-mfpiccolo] via [gittip][gittip-mfpiccolo].
253
+
254
+ [gittip-mfpiccolo]: https://www.gittip.com/mfpiccolo/
255
+
256
+ ## Copyright
257
+
258
+ Copyright (c) 2014 Mike Piccolo
259
+
260
+ See [LICENSE.txt](LICENSE.txt) for details.
24
261
 
25
262
  ## Contributing
26
263
 
27
- 1. Fork it ( https://github.com/[my-github-username]/happy_place/fork )
264
+ 1. Fork it ( http://github.com/mfpiccolo/happy_place/fork )
28
265
  2. Create your feature branch (`git checkout -b my-new-feature`)
29
266
  3. Commit your changes (`git commit -am 'Add some feature'`)
30
267
  4. Push to the branch (`git push origin my-new-feature`)
31
- 5. Create a new Pull Request
268
+ 5. Create new Pull Request
269
+
270
+ [![githalytics.com alpha](https://cruel-carlota.pagodabox.com/e1a155a07163d56ca0c4f246c7aa8766 "githalytics.com")](http://githalytics.com/mfpiccolo/happy_place)
271
+
272
+ [license]: https://github.com/mfpiccolo/happy_place/MIT-LICENSE
273
+ [homepage]: http://mfpiccolo.github.io/happy_place
274
+ [documentation]: http://rdoc.info/github/mfpiccolo/happy_place/frames
275
+ [issues]: https://github.com/mfpiccolo/happy_place/issues
276
+
@@ -13,22 +13,22 @@ module HappyPlace
13
13
  # end
14
14
 
15
15
  # instance methods to go on every controller go here
16
- def js(js_class: nil, method: nil, partial: nil)
16
+ def js(js_class: nil, function: nil, partial: nil, args: {})
17
17
  return unless [:js, :html].include?(request.format.to_sym)
18
18
 
19
19
  js_class ||= self.class.name.gsub("::", ".")
20
- method ||= action_name
20
+ function ||= action_name
21
21
 
22
22
  if partial.present?
23
23
  appendable = (render_to_string partial: partial).gsub("\n", "")
24
- arg = "('#{appendable}');"
24
+ built_args = "({" + (["partial: '#{appendable}'"] + hash_to_js_args(args)).join(", ") + "});"
25
25
  else
26
- arg = "()"
26
+ built_args = "({" + hash_to_js_args(args).join(", ") + "});"
27
27
  end
28
28
 
29
- class_method = [js_class, method].join(".")
29
+ class_function = [js_class, function].join(".")
30
30
  if request.format.to_sym == :js
31
- render js: class_method + arg
31
+ render js: class_function + built_args
32
32
  elsif request.format.to_sym == :html
33
33
  render
34
34
  response_body = response.body
@@ -38,13 +38,22 @@ module HappyPlace
38
38
  before_body = response_body[0, before_body_end_index].html_safe
39
39
  after_body = response_body[before_body_end_index..-1].html_safe
40
40
 
41
- response.body = before_body + clean_script(class_method, arg).html_safe + after_body
41
+ response.body = before_body + clean_script(class_function, built_args).html_safe + after_body
42
42
  end
43
43
  end
44
44
  end
45
45
 
46
- def clean_script(class_method, arg)
47
- "<script type='application/javascript'>jQuery(document).ready(function($) {" + render_to_string(js: class_method + arg) + "});</script>"
46
+ def clean_script(class_function, args)
47
+ "<script type='application/javascript'>jQuery(document).ready(function($) {" + render_to_string(js: class_function + args) + "});</script>"
48
+ end
49
+
50
+ def hash_to_js_args(args)
51
+ js_args = []
52
+
53
+ args.each_pair do |k, v|
54
+ js_args << (k.to_s + ": " + v.to_s)
55
+ end
56
+ js_args
48
57
  end
49
58
 
50
59
  end
@@ -1,3 +1,3 @@
1
1
  module HappyPlace
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: happy_place
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mike Piccolo
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-11-01 00:00:00.000000000 Z
11
+ date: 2014-11-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails