paloma 4.1.2 → 4.2.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: fb171dd2c32755bd619029e3e93a859f8888f666
4
- data.tar.gz: 9184bb4268f33cdc3f8581426f3a48e7a6acd95d
3
+ metadata.gz: b10b31f63c970076c4afe47277ce5f90a3c6fccd
4
+ data.tar.gz: 0f63a749171a49ed7b88c4bedc9f66b559a5bdf7
5
5
  SHA512:
6
- metadata.gz: 8c5d9ae6c6f97388e55d0f625733f2b145c4653612e6ee09eb8c2ce5760531c8ad212457f4c942320706ab9e3c27dbdd45bf370d728844c3e6249a6685713d48
7
- data.tar.gz: be6d27412b201fcecd6af8ca6ab8d18eb5889524aff9f7402d30571c715726f61030a83efd1ffd49a3d93a171ba78d72d4734bf1a23f5f4bb027815edd9d553b
6
+ metadata.gz: df5a36190acab372138dab88ed9e41a864c8f05e460a5be02e905f06f675d89b4b303ba3ca28ee9ddf4738f8937ad8286f855312cb2338e8a4f287db17eef786
7
+ data.tar.gz: cfc151c6dbc4afc174dd6e5b4288476094bbb762a311c24744a008868c447a51d6e11a653058a5105862b3a7d76522ade0eb7e05399092955edd473da27e5a2c
data/.gitignore CHANGED
@@ -1,6 +1,5 @@
1
1
  Gemfile.lock
2
2
  *.gem
3
3
  /tmp/*
4
- /spec/tmp/
5
- /spec/test_app/tmp
6
- /spec/test_app/spec/tmp/*
4
+ /test_app/tmp/*
5
+ /test_app/spec/tmp/*
@@ -1,3 +1,6 @@
1
+ ## 4.2.0
2
+ * https://github.com/kbparagua/paloma/pull/75 - Explicitly insert hook in view.
3
+
1
4
  ## 4.1.2
2
5
  * https://github.com/kbparagua/paloma/issues/73 - Fix `js false` issue.
3
6
 
@@ -4,9 +4,10 @@
4
4
 
5
5
  1. Go to `test_app`
6
6
  1. Comment out `turbolinks.js` on `applciation.js`
7
- 1. Run `rake spec:javascript`.
8
- 1. Run `rake spec:units`.
9
- 1. Run `rake spec:integration`.
7
+ 1. Run `bundle exec rails s`.
8
+ 1. Open your browser and visit [http://localhost:3000/specs](http://localhost:3000/specs).
9
+ 1. Run `bundle exec rake spec:units`.
10
+ 1. Run `bundle exec rake spec:integration`.
10
11
 
11
12
  ## TODO
12
13
 
data/README.md CHANGED
@@ -1,35 +1,16 @@
1
- # Paloma
2
-
3
- **This README is for Paloma 4 only.**
4
-
5
- **For version 3 README please go [here](https://github.com/kbparagua/paloma/blob/3.0/README.md).**
6
-
7
- **For version 2 README please go [here](https://github.com/kbparagua/paloma/blob/2.0/README.md).**
8
-
9
-
10
- ## Paloma 4, why so sudden?
11
- The last major version (v3) of Paloma introduced a major paradigm shift, and it took me a while to realize that some of the major changes introduced are not really that good and needed to be removed.
12
-
13
-
14
- ## What's new?
15
- *(compared to version 2)*
16
- It is now simpler and more flexible. The old callback thingy paradigm is replaced by a `Controller` layer for better abstraction. Generators are also removed, so programmers need not to follow specific directory structure, unlike in the old versions.
17
-
18
- Previously, there are generators that create Paloma files, and these files are written in vanilla javascript. Because of that there are some users who are requesting for coffeescript setup. Now since there are no generated files programmers can write their code either by using vanilla javascript or **coffeescript**. Yay!
1
+ **Important**
2
+ - `master` branch contains the bleeding edge development code.
3
+ - check `branches` or `tags` for the latest stable release or specific versions.
19
4
 
20
- ### Controller
21
- The new paradigm is patterned after Rails Controller, so it is easier to grasp than the old callback paradigm. Basically, you have a Paloma Controller counterpart for every Rails Controller.
22
-
23
-
24
- ### How about Model and View?
25
-
26
- It is tempting to convert Paloma 3 to a full-blown MVC or MVP (or whatever) framework. But I've decided to keep it simple and just provide a Controller component as way to execute a specific javascript code per Rails Controller action and give developers freedom on how to handle each action. So you can still have your own Model and View components and just use them in your Paloma Controllers, since a controller is just a middle-man.
5
+ # Paloma
27
6
 
7
+ Page-specific javascript for Rails done right.
28
8
 
29
9
  ## Advantages
30
10
  * Choose what specific javascript code to run per page.
31
11
  * Easily make ruby variables available on your javascript files.
32
-
12
+ * Can be written using vanilla javascript, coffeescript, and anything that compiles to js.
13
+ * Easy to understand (*because it is patterned after Rails' controller module*).
33
14
 
34
15
  ## Quick Example
35
16
 
@@ -64,10 +45,23 @@ That's it! Simply Sexy!
64
45
 
65
46
  ## Install
66
47
 
67
- * Without bundler: `sudo gem install paloma`.
68
- * With bundler, add this to your Gemfile: `gem 'paloma'`
69
- * Require `paloma` in your `application.js`: `//= require paloma`
48
+ 1. Without bundler: `sudo gem install paloma`.
49
+ 1. With bundler, add this to your Gemfile: `gem 'paloma'`
50
+ 1. Require `paloma` in your `application.js`: `//= require paloma`
51
+ 1. In your layouts insert Paloma hook.
70
52
 
53
+ `application.html.erb`
54
+ ```html
55
+ <html>
56
+ <head>
57
+ </head>
58
+
59
+ <body>
60
+ <%= yield %>
61
+ <%= insert_paloma_hook %>
62
+ </body>
63
+ </html>
64
+ ```
71
65
 
72
66
  ## Controllers
73
67
 
@@ -276,6 +270,28 @@ class UsersController < ApplicationController
276
270
  end
277
271
  ```
278
272
 
273
+ ## Hook
274
+
275
+ `insert_paloma_hook` is a helper method that you can use in your views to insert Paloma's HTML hook.
276
+ Inside this HTML hook is where the magic happens. This is the reason why Paloma can magically know what Javascript controller/action to execute. To further understand how Paloma works, you can inspect the HTML hook, by checking the generated HTML (*inspect element*) and locate the `div` element that has the class `js-paloma-hook`.
277
+
278
+ Ideally, you just need to call `insert_paloma_hook` in your layouts, since the layout will always be included in every rendered view. But if you are rendering a view without a layout, make sure to call `insert_paloma_hook` in that view.
279
+
280
+
281
+ ## AJAX
282
+
283
+ 1. Make sure that the AJAX response contains the html hook. (use `insert_paloma_hook`)
284
+ 2. Execute the hook and start Paloma's engine on complete/success.
285
+
286
+ ```js
287
+ $.get('http://example.com', function(response){
288
+ $('#result').html(response);
289
+
290
+ // Execute Paloma hook and start the engine.
291
+ Paloma.executeHook();
292
+ Palama.engine.start();
293
+ });
294
+ ```
279
295
 
280
296
  ## Turbolinks Support
281
297
 
@@ -308,14 +324,7 @@ $(document).on('page:load', function(){
308
324
 
309
325
  ## Gotchas
310
326
 
311
- * Paloma will execute on all `render` calls, except for calls with the following formats: `js`, `json`, `xml`, and `file`.
312
-
313
- Example:
314
-
315
- ```ruby
316
- render :json => {:x => 1} # Paloma will not execute`
317
- render :partial => '/path/to/partial' # Paloma will execute
318
- ```
327
+ * Make sure that the rendered view has the paloma hook (*use `insert_paloma_hook`*) for Paloma to execute.
319
328
 
320
329
  * It will cause conflicts if you have a controller and a module that has the same name.
321
330
 
@@ -334,3 +343,11 @@ Again, Paloma is now flexible and doesn't force developers to follow specific di
334
343
  You have the freedom to create controllers anywhere in your application.
335
344
 
336
345
  Personally, I prefer having a javascript file for each controller.
346
+
347
+
348
+ ## Contribute
349
+
350
+ 1. Fork.
351
+ 2. Do awesome things.
352
+ 3. Submit Pull-Request to `master` branch.
353
+ 4. Add short summary of changes on your PR.
@@ -12,7 +12,7 @@ module Paloma
12
12
  prepend_view_path "#{Paloma.root}/app/views/"
13
13
 
14
14
  before_filter :track_paloma_request
15
- after_filter :append_paloma_hook, :if => :not_redirect?
15
+ helper_method :insert_paloma_hook
16
16
  end
17
17
  end
18
18
 
@@ -107,61 +107,26 @@ module Paloma
107
107
 
108
108
 
109
109
  #
110
- # Before rendering html reponses,
111
- # this is exectued to append Paloma's html hook to the response.
110
+ # Call in your view to insert Paloma's html hook.
112
111
  #
113
112
  # The html hook contains the javascript code that
114
113
  # will execute the tracked Paloma requests.
115
114
  #
116
- def append_paloma_hook
115
+ def insert_paloma_hook
117
116
  return true if self.paloma.has_no_request?
118
-
119
- # Render the partial if it is present, otherwise do nothing.
120
- begin
121
- hook = view_context.render(
122
- :partial => 'paloma/hook',
123
- :locals => {:request => self.paloma.request})
124
- rescue ActionView::MissingTemplate
125
- return true
126
- end
127
-
128
- before_body_end_index = response_body[0].rindex('</body>')
129
117
 
130
- # Append the hook after the body tag if it is present.
131
- if before_body_end_index.present?
132
- before_body = response_body[0][0, before_body_end_index].html_safe
133
- after_body = response_body[0][before_body_end_index..-1].html_safe
134
-
135
- response.body = before_body + hook + after_body
136
- else
137
- # If body tag is not present, append hook in the response body
138
- response.body += hook
139
- end
118
+ hook = view_context.render(
119
+ :partial => 'paloma/hook',
120
+ :locals => {:request => self.paloma.request})
140
121
 
141
122
  self.paloma.clear_request
123
+ hook
142
124
  end
143
125
  end
144
126
 
145
127
 
146
- def not_redirect?
147
- self.status != 302
148
- end
149
128
 
150
129
 
151
- #
152
- # Make sure not to execute paloma on the following response type
153
- #
154
- def render options = nil, extra_options = {}, &block
155
- [:json, :js, :xml, :file].each do |format|
156
- if options.has_key?(format)
157
- self.paloma.clear_request
158
- break
159
- end
160
- end if options.is_a?(Hash)
161
-
162
- super
163
- end
164
-
165
130
 
166
131
  protected
167
132
 
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'paloma'
3
- s.version = '4.1.2'
3
+ s.version = '4.2.0'
4
4
  s.summary = "Provides an easy way to execute page-specific javascript for Rails."
5
5
  s.description = "Page-specific javascript for Rails done right"
6
6
  s.authors = ['Karl Paragua']
@@ -52,3 +52,16 @@ Foos.prototype.otherAction = function(){};
52
52
  var NotFoos = Paloma.controller('NotAdmin/Foos');
53
53
  NotFoos.prototype.show = function(){};
54
54
  NotFoos.prototype.otherAction = function(){};
55
+
56
+
57
+ $(document).ready(function(){
58
+ $('#js-ajax-link').on('click', function(e){
59
+ e.preventDefault();
60
+
61
+ $.get($(this).prop('href'), function(response){
62
+ $('#js-ajax-response').html(response);
63
+ Paloma.executeHook();
64
+ Paloma.engine.start();
65
+ });
66
+ });
67
+ });
@@ -40,6 +40,11 @@ class MainController < ApplicationController
40
40
  end
41
41
 
42
42
 
43
+ def ajax
44
+ render :ajax, :layout => false
45
+ end
46
+
47
+
43
48
 
44
49
 
45
50
 
@@ -63,7 +68,7 @@ class MainController < ApplicationController
63
68
 
64
69
 
65
70
  def file_response
66
- render :file => "#{Rails.root}/Gemfile"
71
+ render :file => "#{Rails.root}/Gemfile", :layout => false
67
72
  end
68
73
 
69
74
  end
@@ -19,12 +19,17 @@
19
19
  <li><%= link_to 'Main#basic_params', basic_params_main_index_path %></li>
20
20
  <li><%= link_to 'Main#xml_response', xml_response_main_index_path %></li>
21
21
  <li><%= link_to 'Main#file_response', file_response_main_index_path %></li>
22
+ <li><%= link_to 'Main#ajax', ajax_main_index_path, :id => 'js-ajax-link' %></li>
22
23
  </ul>
23
24
  </div>
24
25
 
25
26
  <hr/>
26
27
 
28
+ <div id='js-ajax-response'>
29
+ </div>
30
+
27
31
  <%= yield %>
32
+ <%= insert_paloma_hook %>
28
33
 
29
34
  </body>
30
35
  </html>
@@ -0,0 +1,2 @@
1
+ <h1>Main#AJAX</h1>
2
+ <%= insert_paloma_hook %>
@@ -12,6 +12,7 @@ TestApp::Application.routes.draw do
12
12
  get :js_response
13
13
  get :xml_response
14
14
  get :file_response
15
+ get :ajax
15
16
  end
16
17
  end
17
18
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: paloma
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.1.2
4
+ version: 4.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Karl Paragua
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-03-24 00:00:00.000000000 Z
11
+ date: 2015-05-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: jquery-rails
@@ -180,6 +180,7 @@ files:
180
180
  - test_app/app/mailers/.gitkeep
181
181
  - test_app/app/models/.gitkeep
182
182
  - test_app/app/views/layouts/application.html.erb
183
+ - test_app/app/views/main/ajax.html.erb
183
184
  - test_app/config.ru
184
185
  - test_app/config/application.rb
185
186
  - test_app/config/boot.rb
@@ -212,15 +213,6 @@ files:
212
213
  - test_app/spec/javascripts/router_spec.js
213
214
  - test_app/spec/javascripts/support/jasmine.yml
214
215
  - test_app/spec/spec_helper.rb
215
- - test_app/spec/tmp/assets/jasmine-boot.js
216
- - test_app/spec/tmp/assets/jasmine-console-reporter.js
217
- - test_app/spec/tmp/assets/jasmine-console-shims.js
218
- - test_app/spec/tmp/assets/jasmine-html.js
219
- - test_app/spec/tmp/assets/jasmine-specs.js
220
- - test_app/spec/tmp/assets/jasmine.css
221
- - test_app/spec/tmp/assets/jasmine.js
222
- - test_app/spec/tmp/assets/json2.js
223
- - test_app/spec/tmp/runner.html
224
216
  - test_app/spec/units/controller_spec.rb
225
217
  - test_app/spec/units/utilities_spec.rb
226
218
  - test_app/test/fixtures/.gitkeep
@@ -1,27 +0,0 @@
1
- var jsApiReporter;
2
- (function() {
3
- var jasmineEnv = jasmine.getEnv();
4
-
5
- jsApiReporter = new jasmine.JsApiReporter();
6
- jasmineEnv.addReporter(jsApiReporter);
7
-
8
- var htmlReporter = new jasmine.HtmlReporter();
9
- jasmineEnv.addReporter(htmlReporter);
10
- jasmineEnv.specFilter = function(spec) {
11
- return htmlReporter.specFilter(spec);
12
- };
13
-
14
- if (jasmine.ConsoleReporter) {
15
- jasmineEnv.addReporter(new jasmine.ConsoleReporter());
16
- }
17
-
18
- function execJasmine() {
19
- jasmineEnv.execute();
20
- }
21
-
22
- if (window.addEventListener) { // W3C
23
- window.addEventListener('load', execJasmine, false);
24
- } else if (window.attachEvent) { // MSIE
25
- window.attachEvent('onload', execJasmine);
26
- }
27
- })();
@@ -1,111 +0,0 @@
1
- /**
2
- Jasmine Reporter that outputs test results to the browser console.
3
- Useful for running in a headless environment such as PhantomJs, ZombieJs etc.
4
-
5
- Usage:
6
- // From your html file that loads jasmine:
7
- jasmine.getEnv().addReporter(new jasmine.ConsoleReporter());
8
- jasmine.getEnv().execute();
9
- */
10
-
11
-
12
- (function(jasmine, console) {
13
- if (!jasmine) {
14
- throw "jasmine library isn't loaded!";
15
- }
16
-
17
- var ANSI = {}
18
- ANSI.color_map = {
19
- "green" : 32,
20
- "red" : 31
21
- }
22
-
23
- ANSI.colorize_text = function(text, color) {
24
- var color_code = this.color_map[color];
25
- return "\033[" + color_code + "m" + text + "\033[0m";
26
- }
27
-
28
- var ConsoleReporter = function() {
29
- if (!console || !console.log) { throw "console isn't present!"; }
30
- this.status = this.statuses.stopped;
31
- };
32
-
33
- var proto = ConsoleReporter.prototype;
34
- proto.statuses = {
35
- stopped : "stopped",
36
- running : "running",
37
- fail : "fail",
38
- success : "success"
39
- };
40
-
41
- proto.reportRunnerStarting = function(runner) {
42
- this.status = this.statuses.running;
43
- this.start_time = (new Date()).getTime();
44
- this.executed_specs = 0;
45
- this.passed_specs = 0;
46
- this.log("Starting...");
47
- };
48
-
49
- proto.reportRunnerResults = function(runner) {
50
- var failed = this.executed_specs - this.passed_specs;
51
- var spec_str = this.executed_specs + (this.executed_specs === 1 ? " spec, " : " specs, ");
52
- var fail_str = failed + (failed === 1 ? " failure in " : " failures in ");
53
- var color = (failed > 0)? "red" : "green";
54
- var dur = (new Date()).getTime() - this.start_time;
55
-
56
- this.log("");
57
- this.log("Finished");
58
- this.log("-----------------");
59
- this.log(spec_str + fail_str + (dur/1000) + "s.", color);
60
-
61
- this.status = (failed > 0)? this.statuses.fail : this.statuses.success;
62
-
63
- /* Print something that signals that testing is over so that headless browsers
64
- like PhantomJs know when to terminate. */
65
- this.log("");
66
- this.log("ConsoleReporter finished");
67
- };
68
-
69
-
70
- proto.reportSpecStarting = function(spec) {
71
- this.executed_specs++;
72
- };
73
-
74
- proto.reportSpecResults = function(spec) {
75
- if (spec.results().skipped) {
76
- return;
77
- }
78
- if (spec.results().passed()) {
79
- this.passed_specs++;
80
- return;
81
- }
82
-
83
- var resultText = spec.suite.description + " : " + spec.description;
84
- this.log(resultText, "red");
85
-
86
- var items = spec.results().getItems()
87
- for (var i = 0; i < items.length; i++) {
88
- var item = items[i];
89
- var output = ' ' + item.message;
90
- this.log(output, "red");
91
- }
92
- };
93
-
94
- proto.reportSuiteResults = function(suite) {
95
- if (suite.parentSuite) { return; }
96
- var results = suite.results();
97
- if (results.totalCount === 0) {
98
- return;
99
- }
100
- var failed = results.totalCount - results.passedCount;
101
- var color = (failed > 0)? "red" : "green";
102
- this.log(suite.description + ": " + results.passedCount + " of " + results.totalCount + " passed.", color);
103
- };
104
-
105
- proto.log = function(str, color) {
106
- var text = (color != undefined)? ANSI.colorize_text(str, color) : str;
107
- console.log(text)
108
- };
109
-
110
- jasmine.ConsoleReporter = ConsoleReporter;
111
- })(jasmine, console);