insulate 1.1.0 → 1.2.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: 3dcc6c1468070e6d248fddd9ee4af609f4b24fa6
4
- data.tar.gz: a2d954d791fac57f5b85d2532662d6e89800a14b
3
+ metadata.gz: 12408cd7e93560b07bb0f46dd6129093801670b0
4
+ data.tar.gz: a07ec86e10d5f80d5aa4bf4dfcdb859cc8b734d5
5
5
  SHA512:
6
- metadata.gz: 60fa7ce1d0acce026fe9782a7dc8c4f09f02c7cf2ef8e5cea38a6edfd984556323b9d196b0a5ceeb68ae0c6bd40d49f88987a2f7711b8249d7ce49344fbc3920
7
- data.tar.gz: 7bef667899f13ae0d11dad373a48666f456a647bd0b8a37ad584378390409391d18273b38518c166a99e258282bef9ba216a0293a032988a8c8685d9016947f1
6
+ metadata.gz: 32b265e31a80f3de4e23221c988b8a3d225e4c44507448e551aef87cb5fa7fbeff138dc8324353d575ccb0e8204b62606bf631753150c2016f1a86d1813c3dc5
7
+ data.tar.gz: 7b83f30cb273b40eb4aa09297ec406e36fc5de44216b0279090feee293805f460320aec447bbe0bd947f85ada35c1109060c571d6484e34af05080fe998192f9
data/README.md CHANGED
@@ -1,10 +1,8 @@
1
1
  # Insulate
2
2
 
3
- *"Page-specific JavaScript".*
3
+ *"Page-specific JavaScript and CSS".*
4
4
 
5
- Easily partition your JavaScript code based on controller actions. Works great with Rails' asset pipeline default configuration (i.e. all JavaScript concatenated into one huge application.js file).
6
-
7
- This gem is a fork of [Paloma](https://github.com/kbparagua/paloma), an excellent and much more comprehensive page-specific JavaScript solution. What does it do differently? Basically, I've just removed a lot of the shiny features and fluff in order to provide something that is extremely simple, compact, and *just works*. Think of it as Paloma Lite.
5
+ Easily partition your JavaScript and CSS based on controller actions. Works great with Rails' default asset pipeline configuration (i.e. all JavaScript and CSS concatenated into application.js and application.css respectively). Generally recommended only for small Rails applications where you have a direct one-to-one mapping between controller actions and views and don't mind introducing a little coupling.
8
6
 
9
7
  ## Installation
10
8
 
@@ -22,19 +20,56 @@ $ bundle install
22
20
 
23
21
  ## Usage
24
22
 
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.
23
+ This gem automatically injects a global JavaScript string variable, `INSULATE_PAGE`, 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.
24
+
25
+ JavaScript example:
26
+
27
+ ```javascript
28
+ if (INSULATE_PAGE == 'users#edit') {
29
+ alert('Edit an existing user!');
30
+ }
31
+ ```
26
32
 
27
33
  CoffeeScript example:
28
34
 
29
35
  ```coffeescript
30
- if INSULATE_PAGE_ID is 'users#new'
36
+ if INSULATE_PAGE is 'users#new'
31
37
  alert 'Create a new user!'
32
38
  ```
33
39
 
34
- JavaScript example:
40
+ Furthermore, this gem also appends a special CSS class to the `<body>` element of every page. The name of this class changes dynamically based on the current controller action, and is always in the format `insulate-page-controller-action`. So for example, if the controller action is `users#show`, the name of the class will be `insulate-page-users-show`. You can easily segregate your stylesheet markup by targeting this class in CSS selectors.
35
41
 
36
- ```javascript
37
- if (INSULATE_PAGE_ID === 'users#edit') {
38
- alert('Edit an existing user!');
42
+ CSS example:
43
+
44
+ ```css
45
+ .insulate-page-studies-index h1
46
+ {
47
+ color: red;
48
+ }
49
+
50
+ .insulate-page-studies-index p
51
+ {
52
+ color: blue;
53
+ }
54
+ ```
55
+
56
+ LESS example:
57
+
58
+ ```less
59
+ .insulate-page-studies-index
60
+ {
61
+ h1
62
+ {
63
+ color: red;
64
+ }
65
+
66
+ p
67
+ {
68
+ color: blue;
69
+ }
39
70
  }
40
71
  ```
72
+
73
+ ## Contributing
74
+
75
+ My intention with this gem is to keep it as simple and lean as it currently is, so do keep that in mind when submitting pull requests. If you want more advanced functionality, it's worth having a look at [Paloma](https://github.com/kbparagua/paloma), an excellent gem that provides a much more comprehensive page-specific JavaScript solution.
data/insulate.gemspec CHANGED
@@ -8,8 +8,8 @@ Gem::Specification.new do |spec|
8
8
  spec.version = Insulate::VERSION
9
9
  spec.authors = ["Luke Horvat"]
10
10
  spec.email = ["lukehorvat@gmail.com"]
11
- spec.description = %q{Simple page-specific JavaScript solution for small Ruby on Rails applications.}
12
- spec.summary = %q{Simple page-specific JavaScript solution for small Rails apps.}
11
+ spec.description = %q{A simple page-specific JavaScript and CSS solution for small Ruby on Rails applications.}
12
+ spec.summary = %q{A simple page-specific JavaScript and CSS solution for small Rails apps.}
13
13
  spec.homepage = "https://github.com/lukehorvat/insulate"
14
14
  spec.license = "MIT"
15
15
 
@@ -14,10 +14,11 @@ module Insulate
14
14
 
15
15
  def inject_script
16
16
  doc = Nokogiri::HTML response.body
17
+
17
18
  head = doc.at_css 'head'
18
19
  if head
19
20
  # 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
+ script = doc.create_element 'script', head_script(controller_path, action_name), :type => 'text/javascript'
21
22
 
22
23
  head_children = head.children
23
24
  if head_children.empty?
@@ -25,16 +26,27 @@ module Insulate
25
26
  else
26
27
  head_children.before script
27
28
  end
29
+ end
28
30
 
29
- # replace the response
30
- response.body = doc.to_html
31
+ body = doc.at_css 'body'
32
+ if body
33
+ # append the following class to the <body>
34
+ body['class'] ||= ''
35
+ body['class'] = body['class'] << ' ' << body_class(controller_path, action_name)
31
36
  end
37
+
38
+ # replace the response
39
+ response.body = doc.to_html
32
40
  end
33
41
 
34
42
  private
35
43
 
36
- def script_contents(controller, action)
37
- "window.INSULATE_PAGE_ID='#{controller}##{action}';"
44
+ def head_script(controller, action)
45
+ "window.INSULATE_PAGE='#{controller}##{action}';"
46
+ end
47
+
48
+ def body_class(controller, action)
49
+ "insulate-page-#{controller}-#{action}"
38
50
  end
39
51
  end
40
52
 
@@ -1,3 +1,3 @@
1
1
  module Insulate
2
- VERSION = "1.1.0"
2
+ VERSION = "1.2.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: insulate
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.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-15 00:00:00.000000000 Z
11
+ date: 2013-07-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nokogiri
@@ -52,7 +52,8 @@ dependencies:
52
52
  - - '>='
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
- description: Simple page-specific JavaScript solution for small Ruby on Rails applications.
55
+ description: A simple page-specific JavaScript and CSS solution for small Ruby on
56
+ Rails applications.
56
57
  email:
57
58
  - lukehorvat@gmail.com
58
59
  executables: []
@@ -91,5 +92,5 @@ rubyforge_project:
91
92
  rubygems_version: 2.0.3
92
93
  signing_key:
93
94
  specification_version: 4
94
- summary: Simple page-specific JavaScript solution for small Rails apps.
95
+ summary: A simple page-specific JavaScript and CSS solution for small Rails apps.
95
96
  test_files: []