pluggable_js 2.0.0 → 2.0.1

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: 9f7dafe469757bf25d292d0b49081de701d62f8d
4
- data.tar.gz: 7fad58ce62bc70a5d0541e117673395873681131
3
+ metadata.gz: 1c053d4795af1596c881bb47fea30ce23856cbf7
4
+ data.tar.gz: ff0bbd8d51e1929d48410a0651e68ed99e1123d3
5
5
  SHA512:
6
- metadata.gz: e1ea81dfe8a2006359d5162d4d8e901ee80af6f6ae3a3ffed85bf2b2660ed6e937bfc8638b38db1972eb9eabd9e25642220e9ee89d70a899fd3bbf905af47d58
7
- data.tar.gz: ad1cd94d4e0d88b2bb056db7f071f21781723fee930bee51abc1dd8c8232322d147535205d491588306576febbdb9a20eb69773e7c4eebba9cf47012e1cfe63b
6
+ metadata.gz: 0ec2f03e85a3a2e775608410813752b80f139434d1fe934908f620f1cbf1b07911b09d70c3a9e1088b096fa965194c6cdd293b50d1d2299db5b6152a6e430834
7
+ data.tar.gz: 22fa3a8fabe1c300ac37b5c0c85b259e35b43ca2efc7008c2e5eb4c6e5bc279cf85084b0dea157bc1b7864fd85518dc6e9b470aeda1a011323a19c7396c215fc
data/CHANGELOG.md CHANGED
@@ -49,3 +49,7 @@
49
49
  ## v2.0.0
50
50
 
51
51
  * improved namespace and data passing
52
+
53
+ ## v2.0.1
54
+
55
+ * refactored helpers, improved readme
data/README.md CHANGED
@@ -1,31 +1,20 @@
1
1
  # PluggableJs
2
2
 
3
- This gem provides simple functionality of loading page specific javascript and allows to pass data from a controller (for Rails 3 and Rails 4 with asset pipeline enabled). Keep desired js code in controller related files as action based functions. They will be triggered only when matching controller and action parameters and when DOM is loaded.
3
+ This gem provides simple functionality of loading page specific javascript and allows to pass data from a controller (for Rails 3 and Rails 4 with asset pipeline enabled). Keep desired js code in controller related files as action based functions. They will be triggered only when matching controller and action parameters and when DOM is ready.
4
4
 
5
5
  ## Installation
6
6
 
7
- ### Basic
8
-
9
- * Add `gem 'pluggable_js'` to Gemfile and run `bundle` command to install it
10
- * Add `<%= javascript_pluggable_tag %>` to application layout file after `<%= javascript_include_tag 'application' %>` line (if you use turbolinks, move helper inside the `body` tag)
11
-
12
- ### Additional
13
-
14
- This steps are necessary only if you want to use generator for large pieces of js code (see [additional usage](https://github.com/peresleguine/pluggable_js#additional-1)):
15
-
16
- * Add `pluggable/*` to assets precompile configuration in production.rb (and staging.rb if you have one), e.g.: `config.assets.precompile += %w(pluggable/*)`
17
- * Be sure that `pluggable` folder is out of `require_tree` statement in application.js
7
+ 1. Add `gem 'pluggable_js', '~> 2.0.0'` to Gemfile and run `bundle` command to install it
8
+ 2. Add `<%= javascript_pluggable_tag %>` helper to application layout file after `<%= javascript_include_tag 'application' %>` line (if you use turbolinks paste it above the closing `</body>` tag)
18
9
 
19
10
  ## Usage
20
11
 
21
- ### Basic
22
-
23
12
  Simply define functions in your controller related file (e.g. posts.js.coffee) like so:
24
13
 
25
14
  ```coffeescript
26
- window['posts#index'] = (data) ->
15
+ @['posts#index'] = (data) ->
27
16
  # your code goes here
28
- window['posts#new'] = (data) ->
17
+ @['posts#new'] = (data) ->
29
18
  # and here
30
19
  ```
31
20
 
@@ -34,14 +23,14 @@ You may pass data to javascript using `pluggable_js` helper in a controller (`pj
34
23
  ```ruby
35
24
  class PostsController < ApplicationController
36
25
  def index
37
- pluggable_js({
26
+ pluggable_js(
38
27
  string: 'string',
39
28
  integer: 1,
40
29
  boolean: true,
41
30
  array: [1, 2, 3],
42
31
  hash: { a: 1, b: 2, c: 3 },
43
32
  array_of_hashes: [{a: 1}, {b: 2}, {c: 3}]
44
- })
33
+ )
45
34
  end
46
35
  end
47
36
  ```
@@ -49,7 +38,7 @@ end
49
38
  Now you can access data in posts.js.coffee:
50
39
 
51
40
  ```coffeescript
52
- window['posts#index'] = (data) ->
41
+ @['posts#index'] = (data) ->
53
42
  if data.boolean
54
43
  console.log data.string
55
44
  console.log data.integer
@@ -58,20 +47,9 @@ window['posts#index'] = (data) ->
58
47
  console.log data.array_of_hashes
59
48
  ```
60
49
 
61
- ### Additional
62
-
63
- In such cases when you have large piece of code (maybe external lib) that you don't want to define as a function but include on a certain page, choose controller and actions you want to use and run generator, e.g.:
64
-
65
- rails generate pluggable_js posts index new
66
-
67
- It will create two files where you may add your code (don't forget to follow [additional installation steps](https://github.com/peresleguine/pluggable_js#additional)):
68
-
69
- app/assets/javascripts/pluggable/posts/index.js.coffee
70
- app/assets/javascripts/pluggable/posts/new.js.coffee
71
-
72
50
  ## Config
73
51
 
74
- Let's say you've created action `search` that renders `index` template. Most likely we still need to trigger `window['posts#index'](data)` function. In such situation you may create `config/initializers/pluggable_js.rb` and use pair actions config:
52
+ Let's say you've created action `search` that renders `index` template. Most likely we still need to trigger `@['posts#index'](data)` function. In such situation you may create `config/initializers/pluggable_js.rb` and use pair actions config:
75
53
 
76
54
  ```ruby
77
55
  PluggableJs.config do |config|
@@ -81,10 +59,25 @@ end
81
59
 
82
60
  `{ 'create' => 'new', 'update' => 'edit' }` is a default REST configuration.
83
61
 
84
- If you are passing data, move `pluggable_js` helper into a separate private method and use `before_action :your_private_method, only: [:index, :search]` (`before_filter` in Rails < 4).
62
+ If you are passing data, move `pluggable_js` helper into a separate private method and use `before_action :set_pluggable_js, only: [:index, :search]` (`before_filter` in Rails < 4).
85
63
 
86
64
  ## Upgrade
87
65
 
88
- * [from v0.0.4 to v0.0.5](https://github.com/peresleguine/pluggable_js/wiki/Upgrade-from-v0.0.4-to-v0.0.5)
89
66
  * [from <= v0.0.6 to v1.0.0](https://github.com/peresleguine/pluggable_js/wiki/Upgrade-from-v0.0.6-or-less-to-v1.0.0)
90
67
  * [from v1.0 to v2.0](https://github.com/peresleguine/pluggable_js/wiki/Upgrade-from-v1.0-to-v2.0)
68
+
69
+ ## Sublime Text Snippet
70
+
71
+ Go to `Sublime Text > Preferences > Browse Packages...` and save under `User` directory `pjs.sublime-snippet` with the following content:
72
+
73
+ ```xml
74
+ <snippet>
75
+ <content><![CDATA[
76
+ @['${0}#'] = (data) ->
77
+ ]]></content>
78
+ <tabTrigger>pjs</tabTrigger>
79
+ <scope>source.coffee</scope>
80
+ </snippet>
81
+ ```
82
+
83
+ Thereafter `pjs` snippet will be available in coffeescript files.
@@ -1,5 +1,5 @@
1
1
  Description:
2
- Creates js files containing function callers based on controller and action parameters.
2
+ Creates coffeescript files based on controller and action parameters.
3
3
 
4
4
  Example:
5
5
  $ rails generate pluggable_js Posts index new
@@ -1,5 +1,9 @@
1
1
  # This file is used only for large piece of js code that you
2
2
  # don't want to define as a function in controller related js file.
3
3
 
4
+ # Add pluggable/* to assets precompile configuration in production.rb (and staging.rb if you have one),
5
+ # e.g.: config.assets.precompile += %w(pluggable/*)
6
+ # Be sure that pluggable folder is out of require_tree statement in application.js
7
+
4
8
  # jQuery ->
5
9
  # # your code goes here
@@ -2,7 +2,6 @@ module PluggableJs
2
2
  module Helpers
3
3
 
4
4
  module View
5
- # call function and pass data, include file if it exists
6
5
  def javascript_pluggable_tag
7
6
  controller = params[:controller]
8
7
  action = define_pair_action
@@ -10,12 +9,12 @@ module PluggableJs
10
9
  ''.tap do |content|
11
10
  content << (javascript_tag "
12
11
  (function() {
13
- pjs_data = {}; #{@data_string}
14
- jQuery(function() {
15
- if (typeof(window['#{controller}##{action}']) == 'function') {
16
- return window['#{controller}##{action}'](pjs_data);
17
- }
18
- });
12
+ var function_name = '#{controller}##{action}';
13
+ if (typeof(this[function_name]) == 'function') {
14
+ $(function() {
15
+ return window[function_name](#{@pluggable_js_data});
16
+ });
17
+ }
19
18
  }).call(this);"
20
19
  )
21
20
 
@@ -23,10 +22,9 @@ module PluggableJs
23
22
  content << (javascript_include_tag "pluggable/#{controller}/#{action}")
24
23
  end
25
24
  end.html_safe
26
-
27
25
  end
28
26
 
29
- private
27
+ private
30
28
 
31
29
  def define_pair_action
32
30
  action = params[:action]
@@ -40,12 +38,10 @@ module PluggableJs
40
38
  end
41
39
 
42
40
  module Controller
43
- # convert hash passed from controller's action to data string
44
41
  def pluggable_js(hash)
45
- @data_string = hash.map { |key, value| "pjs_data.#{key} = #{value.to_json}" }.join('; ')
42
+ @pluggable_js_data = hash.to_json
46
43
  end
47
44
  alias_method :pjs, :pluggable_js
48
-
49
45
  end
50
46
 
51
47
  end
@@ -1,3 +1,3 @@
1
1
  module PluggableJs
2
- VERSION = "2.0.0"
2
+ VERSION = "2.0.1"
3
3
  end
data/pluggable_js.gemspec CHANGED
@@ -8,8 +8,8 @@ Gem::Specification.new do |spec|
8
8
  spec.version = PluggableJs::VERSION
9
9
  spec.authors = ['Andrey Peresleguine']
10
10
  spec.email = ['peresleguine@gmail.com']
11
- spec.description = %q{A solution for Rails of how to load page specific javascript and pass data.}
12
- spec.summary = %q{Pluggable javascript and data passing for Rails.}
11
+ spec.description = %q{Page-specific javascript for Rails applications with the ability of passing data.}
12
+ spec.summary = %q{Page-specific javascript for Rails.}
13
13
  spec.homepage = 'https://github.com/peresleguine/pluggable_js'
14
14
  spec.license = 'MIT'
15
15
 
@@ -1,4 +1,4 @@
1
- window['posts#index'] = (data) ->
1
+ @['posts#index'] = (data) ->
2
2
  $('.protoss-quotes').append("<p>#{data.zealot_quote}</p>")
3
3
  $('.protoss-quotes').append('<p>You have not enough minerals.</p>' if data.minerals_size < 1000)
4
4
  $('.protoss-quotes').append('<p>Base is under attack.</p>') if data.base_is_under_attack
@@ -9,5 +9,5 @@ window['posts#index'] = (data) ->
9
9
  for key, value of unit
10
10
  $('.protoss-quotes').append("<p>#{key}: #{value}</p>")
11
11
 
12
- window['posts#new'] = (data) ->
12
+ @['posts#new'] = (data) ->
13
13
  $('.terran-quotes').append("<p>#{data.marine_quote}</p>")
@@ -1,5 +1,5 @@
1
1
  class PostsController < ApplicationController
2
- before_action :init_pluggable_js, only: [:index, :search]
2
+ before_action :set_pluggable_js, only: [:index, :search]
3
3
 
4
4
  def index
5
5
  end
@@ -9,20 +9,20 @@ class PostsController < ApplicationController
9
9
  end
10
10
 
11
11
  def new
12
- pjs({marine_quote: 'You wanna piece of me, boy?'})
12
+ pjs(marine_quote: 'You wanna piece of me, boy?')
13
13
  end
14
14
 
15
15
  private
16
16
 
17
- def init_pluggable_js
18
- pluggable_js({
17
+ def set_pluggable_js
18
+ pluggable_js(
19
19
  zealot_quote: 'My life for aiur.',
20
20
  minerals_size: 999,
21
21
  base_is_under_attack: true,
22
22
  alert: ['Nuclear', 'launch', 'detected.'],
23
23
  ground_units_quotes: { 'Dragoon' => 'Make use of me.', 'High Templar' => 'It shall be done.', 'Archon' => 'We burn...' },
24
24
  air_units_quotes: [{'Scout' => 'Awaiting command.'}, {'Arbiter' => 'We feel your presence.'}, {'Carrier' => 'Affirmative.'}]
25
- })
25
+ )
26
26
  end
27
27
 
28
28
  end
@@ -1,15 +1,13 @@
1
1
  <!DOCTYPE html>
2
2
  <html>
3
- <head>
4
- <title>Dummy</title>
5
- <%= stylesheet_link_tag "application", media: "all" %>
6
- <%= javascript_include_tag "application" %>
7
- <%= javascript_pluggable_tag %>
8
- <%= csrf_meta_tags %>
9
- </head>
10
- <body>
11
-
12
- <%= yield %>
13
-
14
- </body>
3
+ <head>
4
+ <title>Dummy</title>
5
+ <%= stylesheet_link_tag "application", media: "all" %>
6
+ <%= javascript_include_tag "application" %>
7
+ <%= javascript_pluggable_tag %>
8
+ <%= csrf_meta_tags %>
9
+ </head>
10
+ <body>
11
+ <%= yield %>
12
+ </body>
15
13
  </html>
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pluggable_js
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 2.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrey Peresleguine
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-12-27 00:00:00.000000000 Z
11
+ date: 2014-10-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: coffee-rails
@@ -108,7 +108,7 @@ dependencies:
108
108
  - - '>='
109
109
  - !ruby/object:Gem::Version
110
110
  version: '0'
111
- description: A solution for Rails of how to load page specific javascript and pass
111
+ description: Page-specific javascript for Rails applications with the ability of passing
112
112
  data.
113
113
  email:
114
114
  - peresleguine@gmail.com
@@ -199,7 +199,7 @@ rubyforge_project:
199
199
  rubygems_version: 2.1.11
200
200
  signing_key:
201
201
  specification_version: 4
202
- summary: Pluggable javascript and data passing for Rails.
202
+ summary: Page-specific javascript for Rails.
203
203
  test_files:
204
204
  - features/pluggable_js.feature
205
205
  - features/step_definitions/pluggable_js_steps.rb