pluggable_js 2.1.0 → 2.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: 042a86301eb3629ddde7da5e4601b20bc1bbf8a2
4
- data.tar.gz: ce6aee1cd8a3d65d9c89e9f46a98e6e1f0a545d9
3
+ metadata.gz: 0e1428335e519b6cd7c4ada68e3509110730777c
4
+ data.tar.gz: 382b93ad474f9f69ece97e2ea1d1f553fe59c4e3
5
5
  SHA512:
6
- metadata.gz: 96c4ee114de5a06a5d576dbb5390c6899bef8b43144cb7cd76c51fd718517cada5e68b9d6a8940a10f9593d63b0f5a34817ff5a7844c715c31f560ea23b2c4c1
7
- data.tar.gz: 3c1a316e6125e8754cc6789be4a2c1fe68a46631112f9dacdbecde1c8a18ab9e1815ed24944d3f1841dd58585eeebd6e55c23eed968f2f8ae58d74ec7c3ea9a9
6
+ metadata.gz: fc2209b7ce395bcbfe88f026a8df1be0bf73ee7c1f30196e79047a0b1b0c07bd3094103882d83190b2b2fc42d513ea75c5b4de1e4014bb7f3748aa41a21aa4c3
7
+ data.tar.gz: 02b55ddb93e3590200a4950fad3e9a275594e9680dc1601376d11af2be7782b0ee4b0be31e4eabbc73a360731e06301fde8ad5437b0267d7ffe532eb7c0a95a2
@@ -67,3 +67,7 @@
67
67
  * removed jquery and coffee from core dependency
68
68
  * removed unused generator
69
69
  * simplified main helper
70
+
71
+ ## v2.2.0
72
+
73
+ * shared data helper between view and controller
data/README.md CHANGED
@@ -2,16 +2,18 @@
2
2
 
3
3
  [![Gem Version](https://badge.fury.io/rb/pluggable_js.svg)](http://badge.fury.io/rb/pluggable_js)
4
4
 
5
- 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.
5
+ This gem provides simple functionality of loading page specific javascript and allows to pass data from a controller. It requires only Rails >= 3.1 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.
6
6
 
7
7
  ## Installation
8
8
 
9
- 1. Add `gem 'pluggable_js', '~> 2.1.0'` to Gemfile and run `bundle` command to install it
9
+ 1. Add `gem 'pluggable_js', '~> 2.2.0'` to Gemfile and run `bundle` command to install it
10
10
  2. Add `<%= javascript_pluggable_tag %>` helper to application layout file above the closing `</body>` tag
11
11
 
12
+ The place for the helper is important. Primarily it serves the DOM ready purpose and completely necessary if you decided to use turbolinks.
13
+
12
14
  ## Usage
13
15
 
14
- Simply define functions in your controller related file (e.g. posts.js.coffee) like so:
16
+ Simply define functions in your controller related file (e.g. posts.coffee) like so:
15
17
 
16
18
  ```coffeescript
17
19
  @['posts#index'] = (data) ->
@@ -20,7 +22,7 @@ Simply define functions in your controller related file (e.g. posts.js.coffee) l
20
22
  # and here
21
23
  ```
22
24
 
23
- You may pass data to javascript using `pluggable_js` helper in a controller (`pjs` is an alias method). See example below:
25
+ You may pass data to javascript function using `pluggable_js` helper in rails controller (`pjs` is an alias method). See example below:
24
26
 
25
27
  ```ruby
26
28
  class PostsController < ApplicationController
@@ -37,18 +39,20 @@ class PostsController < ApplicationController
37
39
  end
38
40
  ```
39
41
 
40
- Now you can access data in posts.js.coffee:
42
+ If you feel like this logic doesn't belong to a controller, safely move it to a view. Then you can access data in posts.coffee:
41
43
 
42
44
  ```coffeescript
43
45
  @['posts#index'] = (data) ->
44
- if data.boolean
45
- console.log data.string
46
- console.log data.integer
47
- console.log data.array
48
- console.log data.hash
49
- console.log data.array_of_hashes
46
+ console.log data.string
47
+ console.log data.integer
48
+ console.log data.boolean
49
+ console.log data.array
50
+ console.log data.hash
51
+ console.log data.array_of_hashes
50
52
  ```
51
53
 
54
+ CoffeeScript used here just for the sake of simplicity. You may implement the same with plain JavaScript.
55
+
52
56
  ## Config
53
57
 
54
58
  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:
@@ -61,8 +65,6 @@ end
61
65
 
62
66
  `{ 'create' => 'new', 'update' => 'edit' }` is a default REST configuration.
63
67
 
64
- 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).
65
-
66
68
  ## Upgrade
67
69
 
68
70
  * [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)
@@ -1,7 +1,16 @@
1
1
  module PluggableJs
2
2
  module Helpers
3
3
 
4
+ module Shared
5
+ def pluggable_js(data)
6
+ @pluggable_js_data = data.to_json
7
+ end
8
+ alias_method :pjs, :pluggable_js
9
+ end
10
+
4
11
  module View
12
+ include PluggableJs::Helpers::Shared
13
+
5
14
  def javascript_pluggable_tag
6
15
  controller = params[:controller]
7
16
  action = define_pair_action
@@ -28,10 +37,7 @@ module PluggableJs
28
37
  end
29
38
 
30
39
  module Controller
31
- def pluggable_js(data)
32
- @pluggable_js_data = data.to_json
33
- end
34
- alias_method :pjs, :pluggable_js
40
+ include PluggableJs::Helpers::Shared
35
41
  end
36
42
 
37
43
  end
@@ -1,3 +1,3 @@
1
1
  module PluggableJs
2
- VERSION = '2.1.0'
2
+ VERSION = '2.2.0'
3
3
  end
@@ -1,6 +1,5 @@
1
1
  module PluggableJs
2
2
  class PostsController < ApplicationController
3
- before_action :set_pluggable_js, only: [:index, :search]
4
3
 
5
4
  def index
6
5
  end
@@ -13,18 +12,5 @@ module PluggableJs
13
12
  pjs(marine_quote: 'You wanna piece of me, boy?')
14
13
  end
15
14
 
16
- private
17
-
18
- def set_pluggable_js
19
- pluggable_js(
20
- zealot_quote: 'My life for aiur.',
21
- minerals_size: 999,
22
- base_is_under_attack: true,
23
- alert: ['Nuclear', 'launch', 'detected.'],
24
- ground_units_quotes: { 'Dragoon' => 'Make use of me.', 'High Templar' => 'It shall be done.', 'Archon' => 'We burn...' },
25
- air_units_quotes: [{'Scout' => 'Awaiting command.'}, {'Arbiter' => 'We feel your presence.'}, {'Carrier' => 'Affirmative.'}]
26
- )
27
- end
28
-
29
15
  end
30
16
  end
@@ -2,3 +2,12 @@
2
2
  <div class='terran-quotes'></div>
3
3
 
4
4
  <%= link_to 'New post', new_pluggable_js_post_path %>
5
+
6
+ <% pluggable_js(
7
+ zealot_quote: 'My life for aiur.',
8
+ minerals_size: 999,
9
+ base_is_under_attack: true,
10
+ alert: ['Nuclear', 'launch', 'detected.'],
11
+ ground_units_quotes: { 'Dragoon' => 'Make use of me.', 'High Templar' => 'It shall be done.', 'Archon' => 'We burn...' },
12
+ air_units_quotes: [{'Scout' => 'Awaiting command.'}, {'Arbiter' => 'We feel your presence.'}, {'Carrier' => 'Affirmative.'}]
13
+ ) %>
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.1.0
4
+ version: 2.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrey Peresleguine
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-01-06 00:00:00.000000000 Z
11
+ date: 2016-01-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails