insulate 1.0.0 → 1.1.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: 0d894f3bf475a3f7006194f98a406f5665ae68ba
4
- data.tar.gz: 9857f4e9ca4471c9eb036a9ebda89e07ea164baa
3
+ metadata.gz: 3dcc6c1468070e6d248fddd9ee4af609f4b24fa6
4
+ data.tar.gz: a2d954d791fac57f5b85d2532662d6e89800a14b
5
5
  SHA512:
6
- metadata.gz: 30b517d55aa2f29cd2deb2839d130b3056641b9e91fc504a81f94cab40dc9063f769df97c38d01a8b2e2e00bb209266430ca2ae7a81c39c54a04103e34ca242e
7
- data.tar.gz: 6764530b185b19424830c9120e70b60e079f54948cbda5d99a51dcd9d321153942ff065cb306b7590912d271a13c19df484933890afee0b823a0f92f6eda8bce
6
+ metadata.gz: 60fa7ce1d0acce026fe9782a7dc8c4f09f02c7cf2ef8e5cea38a6edfd984556323b9d196b0a5ceeb68ae0c6bd40d49f88987a2f7711b8249d7ce49344fbc3920
7
+ data.tar.gz: 7bef667899f13ae0d11dad373a48666f456a647bd0b8a37ad584378390409391d18273b38518c166a99e258282bef9ba216a0293a032988a8c8685d9016947f1
data/README.md CHANGED
@@ -20,29 +20,21 @@ And then execute:
20
20
  $ bundle install
21
21
  ```
22
22
 
23
- Lastly, add the following line to the application.js manifest after `//= require jquery` and before `//= require_tree .`:
24
-
25
- ```
26
- //= require insulate
27
- ```
28
-
29
23
  ## Usage
30
24
 
31
- It's all very simple. For every controller action that you want to hook into, just add a callback function to a global `Insulate` object. The key must be a string in the format `controller#action`.
25
+ It's all very simple. Installing this gem automatically injects (at runtime) a global string variable, `INSULATE_PAGE_ID`, into every page served by your app. This string is always in the format `controller#action`, so if you want to conditionally execute some JavaScript code based on the current controller action, just check the value of this variable.
32
26
 
33
27
  CoffeeScript example:
34
28
 
35
29
  ```coffeescript
36
- Insulate['users#new'] = ->
30
+ if INSULATE_PAGE_ID is 'users#new'
37
31
  alert 'Create a new user!'
38
32
  ```
39
33
 
40
34
  JavaScript example:
41
35
 
42
36
  ```javascript
43
- Insulate['users#edit'] = function() {
37
+ if (INSULATE_PAGE_ID === 'users#edit') {
44
38
  alert('Edit an existing user!');
45
- };
39
+ }
46
40
  ```
47
-
48
- Any callback function you define this way will be executed on jQuery's `$(document).ready()` event (hence why jQuery is a dependency of this gem).
data/insulate.gemspec CHANGED
@@ -18,7 +18,7 @@ Gem::Specification.new do |spec|
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
19
  spec.require_paths = ["lib"]
20
20
 
21
- spec.add_dependency "jquery-rails"
21
+ spec.add_dependency "nokogiri"
22
22
 
23
23
  spec.add_development_dependency "bundler", "~> 1.3"
24
24
  spec.add_development_dependency "rake"
@@ -2,8 +2,7 @@ module Insulate
2
2
  module ActionControllerFilters
3
3
  def self.included base
4
4
  base.module_eval do
5
- prepend_view_path "#{Insulate.root}/app/views/"
6
- after_filter :inject_callback_script, :if => :html_response_from_render?
5
+ after_filter :inject_script, :if => :html_response_from_render?
7
6
  end
8
7
  end
9
8
 
@@ -13,20 +12,30 @@ module Insulate
13
12
  [nil, 'text/html'].include?(response.content_type) && self.status != 302
14
13
  end
15
14
 
16
- def inject_callback_script
17
- callback_script = view_context.render(
18
- :partial => 'insulate/callback',
19
- :locals => { :controller => controller_path, :action => action_name })
20
-
21
- body_closing_tag_index = response_body[0].rindex('</body>')
22
- if body_closing_tag_index.present?
23
- # Insert callback_script just before </body> tag
24
- response_body[0].insert body_closing_tag_index, callback_script
25
- else
26
- # Append callback_script in the response body
27
- response_body[0].concat callback_script
15
+ def inject_script
16
+ doc = Nokogiri::HTML response.body
17
+ head = doc.at_css 'head'
18
+ if head
19
+ # inject the following script as the first child of the <head>
20
+ script = doc.create_element 'script', script_contents(controller_path, action_name), :type => 'text/javascript'
21
+
22
+ head_children = head.children
23
+ if head_children.empty?
24
+ head.add_child script
25
+ else
26
+ head_children.before script
27
+ end
28
+
29
+ # replace the response
30
+ response.body = doc.to_html
28
31
  end
29
32
  end
33
+
34
+ private
35
+
36
+ def script_contents(controller, action)
37
+ "window.INSULATE_PAGE_ID='#{controller}##{action}';"
38
+ end
30
39
  end
31
40
 
32
41
  ::ActionController::Base.send :include, ActionControllerFilters
@@ -1,3 +1,3 @@
1
1
  module Insulate
2
- VERSION = "1.0.0"
2
+ VERSION = "1.1.0"
3
3
  end
data/lib/insulate.rb CHANGED
@@ -6,4 +6,3 @@ end
6
6
 
7
7
  require 'insulate/version'
8
8
  require 'insulate/action_controller_filters'
9
- require 'insulate/rails/engine'
metadata CHANGED
@@ -1,17 +1,17 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: insulate
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Luke Horvat
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-07-14 00:00:00.000000000 Z
11
+ date: 2013-07-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: jquery-rails
14
+ name: nokogiri
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - '>='
@@ -64,13 +64,10 @@ files:
64
64
  - LICENSE.txt
65
65
  - README.md
66
66
  - Rakefile
67
- - app/views/insulate/_callback.html.erb
68
67
  - insulate.gemspec
69
68
  - lib/insulate.rb
70
69
  - lib/insulate/action_controller_filters.rb
71
- - lib/insulate/rails/engine.rb
72
70
  - lib/insulate/version.rb
73
- - vendor/assets/javascripts/insulate.js
74
71
  homepage: https://github.com/lukehorvat/insulate
75
72
  licenses:
76
73
  - MIT
@@ -1,10 +0,0 @@
1
- <script type="text/javascript">
2
- (function(){
3
- $(document).ready(function(){
4
- var callback = Insulate['<%= controller %>#<%= action %>'];
5
- if (callback != undefined){
6
- callback();
7
- }
8
- });
9
- })();
10
- </script>
@@ -1,8 +0,0 @@
1
- # Define a "dummy" engine in order to make vendor/assets/javascripts/insulate.js available to the app
2
-
3
- module Insulate
4
- module Rails
5
- class Engine < ::Rails::Engine
6
- end
7
- end
8
- end
@@ -1,3 +0,0 @@
1
- (function(){
2
- window.Insulate = {};
3
- })();