pluggable_js 0.0.6 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/README.md +43 -7
- data/features/pluggable_js.feature +14 -6
- data/features/step_definitions/pluggable_js_steps.rb +27 -1
- data/lib/generators/pluggable_js/USAGE +1 -1
- data/lib/generators/pluggable_js/pluggable_js_generator.rb +0 -4
- data/lib/pluggable_js/helpers.rb +42 -21
- data/lib/pluggable_js/railtie.rb +3 -2
- data/lib/pluggable_js/version.rb +1 -1
- data/test/dummy/app/assets/javascripts/pluggable/posts/new.js.coffee +1 -1
- data/test/dummy/app/assets/javascripts/posts.js.coffee +8 -3
- data/test/dummy/app/controllers/posts_controller.rb +20 -1
- data/test/dummy/app/views/posts/index.html.erb +1 -1
- data/test/dummy/config/environments/production.rb +2 -0
- data/test/dummy/config/initializers/pluggable_js.rb +3 -0
- data/test/dummy/config/routes.rb +5 -1
- data/test/dummy/db/development.sqlite3 +0 -0
- data/test/dummy/db/schema.rb +16 -0
- metadata +6 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d7e1dc20d0c0328f1bf75473e5a53c1498cb92ee
|
4
|
+
data.tar.gz: 1ea6d564384dc36b3f4e11a7a870b2217351e2a0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a72f01f53602186364f71b60cad17bc562369eeb6ef5b006746462d1200ef9af7c42c566716d3604e9f1189728eb24ee8f1ff9f67f73777e71b315789eff243a
|
7
|
+
data.tar.gz: 26a4568fd34a37e100ddb26e0e2813214a52f92496c6dd71dfe9aa2c9e72be1a0b1ac27846aaef9a96bf59b10224077d00d7f1072e4644377b5f98731d419d56
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
|
2
2
|
# PluggableJs
|
3
3
|
|
4
|
-
This gem provides simple functionality of loading page specific javascript (for 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.
|
4
|
+
This gem provides simple functionality of loading page specific javascript and passing data (for 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 loaded.
|
5
5
|
|
6
6
|
## Installation
|
7
7
|
|
@@ -18,24 +18,54 @@ Next steps are necessary only if you want to use generator for large pieces of j
|
|
18
18
|
Simply define functions in your controller related file (e.g. posts.js.coffee) like so:
|
19
19
|
|
20
20
|
```coffeescript
|
21
|
-
window.
|
22
|
-
|
21
|
+
window.posts = {}
|
22
|
+
posts.index = () ->
|
23
23
|
# your code goes here
|
24
|
-
|
24
|
+
posts.new = () ->
|
25
25
|
# and here
|
26
26
|
```
|
27
|
-
Or, in rare cases, if you have large piece of code (maybe external lib) that you don't want to define as a function, choose controller and actions you want to use and run generator, e.g.:
|
27
|
+
Or, in rare cases, if 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.:
|
28
28
|
|
29
|
-
rails generate pluggable_js
|
29
|
+
rails generate pluggable_js posts index new
|
30
30
|
|
31
31
|
It will create two files where you may add your code (don't forget to follow necessary installation steps):
|
32
32
|
|
33
33
|
app/assets/javascripts/pluggable/posts/index.js.coffee
|
34
34
|
app/assets/javascripts/pluggable/posts/new.js.coffee
|
35
35
|
|
36
|
+
## Passing data
|
37
|
+
|
38
|
+
Starting with version 1.0.0 you may pass data to javascript using `pluggable_js` helper in a controller. See example below:
|
39
|
+
|
40
|
+
```ruby
|
41
|
+
class PostsController < ApplicationController
|
42
|
+
def index
|
43
|
+
pluggable_js({
|
44
|
+
string: 'string',
|
45
|
+
integer: 1,
|
46
|
+
boolean: true,
|
47
|
+
array: [1, 2, 3],
|
48
|
+
hash: { a: 1, b: 2, c: 3 }
|
49
|
+
})
|
50
|
+
end
|
51
|
+
end
|
52
|
+
```
|
53
|
+
|
54
|
+
Now you can access data in posts.js.coffee:
|
55
|
+
|
56
|
+
```coffeescript
|
57
|
+
window.posts = {}
|
58
|
+
posts.index = () ->
|
59
|
+
if pluggable_js.boolean
|
60
|
+
console.log pluggable_js.string
|
61
|
+
console.log pluggable_js.integer
|
62
|
+
console.log pluggable_js.array
|
63
|
+
console.log pluggable_js.hash
|
64
|
+
```
|
65
|
+
|
36
66
|
## Config
|
37
67
|
|
38
|
-
Let's say you've created action `search` that renders `index` template. Most likely we still need to trigger `
|
68
|
+
Let's say you've created action `search` that renders `index` template. Most likely we still need to trigger `posts.index()` function. In such situation you may create `config/initializers/pluggable_js.rb` and use pair actions config:
|
39
69
|
|
40
70
|
```ruby
|
41
71
|
PluggableJs.config do |config|
|
@@ -44,3 +74,9 @@ end
|
|
44
74
|
```
|
45
75
|
|
46
76
|
`{ 'create' => 'new', 'update' => 'edit' }` is a default REST configuration.
|
77
|
+
|
78
|
+
If you 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).
|
79
|
+
|
80
|
+
## Upgrade
|
81
|
+
|
82
|
+
If you are upgrading from version `<= 0.0.6` to `1.0.0`, all you have to do is rename old construction `window.Post` to new `window.posts` (see usage).
|
@@ -1,15 +1,23 @@
|
|
1
1
|
Feature: PluggableJs
|
2
|
-
In order to use some page specific javascript
|
3
|
-
I want to visit different pages and see
|
2
|
+
In order to use some page specific javascript and pass some data
|
3
|
+
I want to visit different pages and see appropriate content
|
4
4
|
|
5
5
|
@javascript
|
6
|
-
Scenario: Posts List
|
7
|
-
When I go to
|
8
|
-
Then I should see
|
6
|
+
Scenario Outline: Posts List
|
7
|
+
When I go to posts '<action>'
|
8
|
+
Then I should see content with '<data_type>'
|
9
9
|
And I should not see 'You wanna piece of me, boy?'
|
10
10
|
|
11
|
+
Examples:
|
12
|
+
| action | data_type |
|
13
|
+
| index | string |
|
14
|
+
| search | integer |
|
15
|
+
| index | boolean |
|
16
|
+
| search | array |
|
17
|
+
| index | hash |
|
18
|
+
|
11
19
|
@javascript
|
12
20
|
Scenario: New Post
|
13
21
|
When I go to new post
|
14
22
|
Then I should see 'You wanna piece of me, boy?'
|
15
|
-
And I should not see 'My life for aiur
|
23
|
+
And I should not see 'My life for aiur.'
|
@@ -1,7 +1,11 @@
|
|
1
|
-
When(/^I go to
|
1
|
+
When(/^I go to posts 'index'$/) do
|
2
2
|
visit posts_path
|
3
3
|
end
|
4
4
|
|
5
|
+
When(/^I go to posts 'search'$/) do
|
6
|
+
visit search_posts_path
|
7
|
+
end
|
8
|
+
|
5
9
|
When(/^I go to new post$/) do
|
6
10
|
visit new_post_path
|
7
11
|
end
|
@@ -13,3 +17,25 @@ end
|
|
13
17
|
And(/^I should not see '(.*?)'$/) do |text|
|
14
18
|
page.should_not have_content text
|
15
19
|
end
|
20
|
+
|
21
|
+
Then(/^I should see content with 'string'$/) do
|
22
|
+
page.should have_content 'My life for aiur.'
|
23
|
+
end
|
24
|
+
|
25
|
+
Then(/^I should see content with 'integer'$/) do
|
26
|
+
page.should have_content 'You have not enough minerals.'
|
27
|
+
end
|
28
|
+
|
29
|
+
Then(/^I should see content with 'boolean'$/) do
|
30
|
+
page.should have_content 'Base is under attack.'
|
31
|
+
end
|
32
|
+
|
33
|
+
Then(/^I should see content with 'array'$/) do
|
34
|
+
page.should have_content 'Nuclear launch detected.'
|
35
|
+
end
|
36
|
+
|
37
|
+
Then(/^I should see content with 'hash'$/) do
|
38
|
+
page.should have_content 'Zealots: 12.'
|
39
|
+
page.should have_content 'Dragoons: 6.'
|
40
|
+
page.should have_content 'Archons: 1.'
|
41
|
+
end
|
@@ -2,7 +2,7 @@ Description:
|
|
2
2
|
Creates js files containing function callers based on controller and action parameters.
|
3
3
|
|
4
4
|
Example:
|
5
|
-
$ rails generate pluggable_js
|
5
|
+
$ rails generate pluggable_js Posts index new
|
6
6
|
|
7
7
|
create app/assets/javascripts/pluggable/posts/index.js.coffee
|
8
8
|
create app/assets/javascripts/pluggable/posts/show.js.coffee
|
data/lib/pluggable_js/helpers.rb
CHANGED
@@ -1,33 +1,54 @@
|
|
1
1
|
module PluggableJs
|
2
2
|
module Helpers
|
3
3
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
4
|
+
module ViewHelpers
|
5
|
+
# include file if it exists or pass data and call a function
|
6
|
+
def javascript_pluggable_tag
|
7
|
+
controller = params[:controller]
|
8
|
+
action = define_pair_action
|
8
9
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
10
|
+
if File.exist?(Rails.root + "app/assets/javascripts/pluggable/#{controller}/#{action}.js.coffee")
|
11
|
+
javascript_include_tag "pluggable/#{controller}/#{action}"
|
12
|
+
else
|
13
|
+
javascript_tag "
|
14
|
+
#{@js_data_string}
|
15
|
+
jQuery(function() {
|
16
|
+
if (typeof(#{controller}) == 'object' && typeof(#{controller}['#{action}']) == 'function') {
|
17
|
+
return #{controller}['#{action}']();
|
18
|
+
}
|
19
|
+
});"
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
13
24
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
25
|
+
def define_pair_action
|
26
|
+
action = params[:action]
|
27
|
+
if Config.pair_actions.has_key?(action)
|
28
|
+
Config.pair_actions[action]
|
29
|
+
else
|
30
|
+
action
|
31
|
+
end
|
19
32
|
end
|
20
|
-
end
|
21
33
|
|
22
|
-
|
34
|
+
end
|
23
35
|
|
24
|
-
|
25
|
-
action
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
36
|
+
module ControllerHelpers
|
37
|
+
# convert hash, passed from controller's action, to js data string
|
38
|
+
def pluggable_js(hash)
|
39
|
+
data_string = hash.map do |key, value|
|
40
|
+
value = if value.is_a?(String)
|
41
|
+
"'#{value}'"
|
42
|
+
elsif value.is_a?(Hash)
|
43
|
+
value.to_json
|
44
|
+
else
|
45
|
+
value
|
46
|
+
end
|
47
|
+
"pluggable_js.#{key} = #{value};"
|
48
|
+
end.join(' ')
|
49
|
+
@js_data_string = 'window.pluggable_js = {}; ' + data_string
|
30
50
|
end
|
51
|
+
|
31
52
|
end
|
32
53
|
|
33
54
|
end
|
data/lib/pluggable_js/railtie.rb
CHANGED
@@ -1,7 +1,8 @@
|
|
1
1
|
module PluggableJs
|
2
2
|
class Railtie < Rails::Railtie
|
3
|
-
initializer
|
4
|
-
ActionView::Base.send :include, Helpers
|
3
|
+
initializer 'pluggable_js.helpers' do
|
4
|
+
ActionView::Base.send :include, Helpers::ViewHelpers
|
5
|
+
ActionController::Base.send :include, Helpers::ControllerHelpers
|
5
6
|
end
|
6
7
|
end
|
7
8
|
end
|
data/lib/pluggable_js/version.rb
CHANGED
@@ -1,3 +1,8 @@
|
|
1
|
-
window.
|
2
|
-
|
3
|
-
$('.
|
1
|
+
window.posts = {}
|
2
|
+
posts.index = () ->
|
3
|
+
$('.protoss-quotes').append("<p>#{pluggable_js.zealot_quote}</p>")
|
4
|
+
$('.protoss-quotes').append('<p>You have not enough minerals.</p>' if pluggable_js.minerals_size < 1000)
|
5
|
+
$('.protoss-quotes').append('<p>Base is under attack.</p>') if pluggable_js.base_is_under_attack
|
6
|
+
$('.protoss-quotes').append("<p>#{pluggable_js.alert.join(' ')}</p>")
|
7
|
+
for key of pluggable_js.units
|
8
|
+
$('.protoss-quotes').append("<p>#{key}: #{pluggable_js.units[key]}.</p>")
|
@@ -1,7 +1,26 @@
|
|
1
1
|
class PostsController < ApplicationController
|
2
|
-
|
2
|
+
before_action :init_pluggable_js, only: [:index, :search]
|
3
|
+
|
4
|
+
def index
|
5
|
+
end
|
6
|
+
|
7
|
+
def search
|
8
|
+
render :index
|
3
9
|
end
|
4
10
|
|
5
11
|
def new
|
6
12
|
end
|
13
|
+
|
14
|
+
private
|
15
|
+
|
16
|
+
def init_pluggable_js
|
17
|
+
pluggable_js({
|
18
|
+
zealot_quote: 'My life for aiur.',
|
19
|
+
minerals_size: 999,
|
20
|
+
base_is_under_attack: true,
|
21
|
+
alert: ['Nuclear', 'launch', 'detected.'],
|
22
|
+
units: { 'Zealots' => 12, 'Dragoons' => 6, 'Archons' => 1 }
|
23
|
+
})
|
24
|
+
end
|
25
|
+
|
7
26
|
end
|
@@ -1,2 +1,2 @@
|
|
1
|
-
<div class='
|
1
|
+
<div class='protoss-quotes'></div>
|
2
2
|
<div class='marine-quote'></div>
|
data/test/dummy/config/routes.rb
CHANGED
Binary file
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
# This file is auto-generated from the current state of the database. Instead
|
3
|
+
# of editing this file, please use the migrations feature of Active Record to
|
4
|
+
# incrementally modify your database, and then regenerate this schema definition.
|
5
|
+
#
|
6
|
+
# Note that this schema.rb definition is the authoritative source for your
|
7
|
+
# database schema. If you need to create the application database on another
|
8
|
+
# system, you should be using db:schema:load, not running all the migrations
|
9
|
+
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
|
10
|
+
# you'll amass, the slower it'll run and the greater likelihood for issues).
|
11
|
+
#
|
12
|
+
# It's strongly recommended that you check this file into your version control system.
|
13
|
+
|
14
|
+
ActiveRecord::Schema.define(version: 0) do
|
15
|
+
|
16
|
+
end
|
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: 0.0
|
4
|
+
version: 1.0.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: 2013-
|
11
|
+
date: 2013-12-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: coffee-rails
|
@@ -194,12 +194,14 @@ files:
|
|
194
194
|
- test/dummy/config/initializers/filter_parameter_logging.rb
|
195
195
|
- test/dummy/config/initializers/inflections.rb
|
196
196
|
- test/dummy/config/initializers/mime_types.rb
|
197
|
+
- test/dummy/config/initializers/pluggable_js.rb
|
197
198
|
- test/dummy/config/initializers/secret_token.rb
|
198
199
|
- test/dummy/config/initializers/session_store.rb
|
199
200
|
- test/dummy/config/initializers/wrap_parameters.rb
|
200
201
|
- test/dummy/config/locales/en.yml
|
201
202
|
- test/dummy/config/routes.rb
|
202
203
|
- test/dummy/db/development.sqlite3
|
204
|
+
- test/dummy/db/schema.rb
|
203
205
|
- test/dummy/db/test.sqlite3
|
204
206
|
- test/dummy/lib/assets/.keep
|
205
207
|
- test/dummy/public/404.html
|
@@ -266,12 +268,14 @@ test_files:
|
|
266
268
|
- test/dummy/config/initializers/filter_parameter_logging.rb
|
267
269
|
- test/dummy/config/initializers/inflections.rb
|
268
270
|
- test/dummy/config/initializers/mime_types.rb
|
271
|
+
- test/dummy/config/initializers/pluggable_js.rb
|
269
272
|
- test/dummy/config/initializers/secret_token.rb
|
270
273
|
- test/dummy/config/initializers/session_store.rb
|
271
274
|
- test/dummy/config/initializers/wrap_parameters.rb
|
272
275
|
- test/dummy/config/locales/en.yml
|
273
276
|
- test/dummy/config/routes.rb
|
274
277
|
- test/dummy/db/development.sqlite3
|
278
|
+
- test/dummy/db/schema.rb
|
275
279
|
- test/dummy/db/test.sqlite3
|
276
280
|
- test/dummy/lib/assets/.keep
|
277
281
|
- test/dummy/public/404.html
|