pluggable_js 1.0.3 → 2.0.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 +4 -4
- data/CHANGELOG.md +4 -0
- data/README.md +35 -26
- data/features/pluggable_js.feature +1 -0
- data/lib/pluggable_js/helpers.rb +22 -19
- data/lib/pluggable_js/railtie.rb +2 -2
- data/lib/pluggable_js/version.rb +2 -2
- data/pluggable_js.gemspec +3 -5
- data/test/dummy/app/assets/javascripts/pluggable/posts/new.js.coffee +1 -1
- data/test/dummy/app/assets/javascripts/posts.js.coffee +10 -8
- data/test/dummy/app/controllers/posts_controller.rb +1 -0
- data/test/dummy/config/application.rb +2 -1
- data/test/dummy/config/environments/development.rb +0 -6
- data/test/dummy/config/environments/test.rb +0 -5
- metadata +5 -41
- data/test/dummy/config/database.yml +0 -25
- data/test/dummy/db/development.sqlite3 +0 -0
- data/test/dummy/db/schema.rb +0 -16
- data/test/dummy/db/test.sqlite3 +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9f7dafe469757bf25d292d0b49081de701d62f8d
|
4
|
+
data.tar.gz: 7fad58ce62bc70a5d0541e117673395873681131
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e1ea81dfe8a2006359d5162d4d8e901ee80af6f6ae3a3ffed85bf2b2660ed6e937bfc8638b38db1972eb9eabd9e25642220e9ee89d70a899fd3bbf905af47d58
|
7
|
+
data.tar.gz: ad1cd94d4e0d88b2bb056db7f071f21781723fee930bee51abc1dd8c8232322d147535205d491588306576febbdb9a20eb69773e7c4eebba9cf47012e1cfe63b
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -1,40 +1,35 @@
|
|
1
1
|
# PluggableJs
|
2
2
|
|
3
|
-
This gem provides simple functionality of loading page specific javascript and
|
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.
|
4
4
|
|
5
5
|
## Installation
|
6
6
|
|
7
|
+
### Basic
|
8
|
+
|
7
9
|
* Add `gem 'pluggable_js'` to Gemfile and run `bundle` command to install it
|
8
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)
|
9
11
|
|
10
|
-
|
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)):
|
11
15
|
|
12
16
|
* Add `pluggable/*` to assets precompile configuration in production.rb (and staging.rb if you have one), e.g.: `config.assets.precompile += %w(pluggable/*)`
|
13
17
|
* Be sure that `pluggable` folder is out of `require_tree` statement in application.js
|
14
18
|
|
15
19
|
## Usage
|
16
20
|
|
21
|
+
### Basic
|
22
|
+
|
17
23
|
Simply define functions in your controller related file (e.g. posts.js.coffee) like so:
|
18
24
|
|
19
25
|
```coffeescript
|
20
|
-
window
|
21
|
-
posts.index = () ->
|
26
|
+
window['posts#index'] = (data) ->
|
22
27
|
# your code goes here
|
23
|
-
posts
|
28
|
+
window['posts#new'] = (data) ->
|
24
29
|
# and here
|
25
30
|
```
|
26
|
-
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.:
|
27
|
-
|
28
|
-
rails generate pluggable_js posts index new
|
29
|
-
|
30
|
-
It will create two files where you may add your code (don't forget to follow necessary installation steps):
|
31
|
-
|
32
|
-
app/assets/javascripts/pluggable/posts/index.js.coffee
|
33
|
-
app/assets/javascripts/pluggable/posts/new.js.coffee
|
34
|
-
|
35
|
-
## Passing data
|
36
31
|
|
37
|
-
|
32
|
+
You may pass data to javascript using `pluggable_js` helper in a controller (`pjs` is an alias method). See example below:
|
38
33
|
|
39
34
|
```ruby
|
40
35
|
class PostsController < ApplicationController
|
@@ -54,21 +49,29 @@ end
|
|
54
49
|
Now you can access data in posts.js.coffee:
|
55
50
|
|
56
51
|
```coffeescript
|
57
|
-
window
|
58
|
-
|
59
|
-
|
60
|
-
console.log
|
61
|
-
console.log
|
62
|
-
console.log
|
63
|
-
console.log
|
64
|
-
console.log pluggable_js.array_of_hashes
|
52
|
+
window['posts#index'] = (data) ->
|
53
|
+
if data.boolean
|
54
|
+
console.log data.string
|
55
|
+
console.log data.integer
|
56
|
+
console.log data.array
|
57
|
+
console.log data.hash
|
58
|
+
console.log data.array_of_hashes
|
65
59
|
```
|
66
60
|
|
67
|
-
|
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
|
68
71
|
|
69
72
|
## Config
|
70
73
|
|
71
|
-
Let's say you've created action `search` that renders `index` template. Most likely we still need to trigger `posts
|
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:
|
72
75
|
|
73
76
|
```ruby
|
74
77
|
PluggableJs.config do |config|
|
@@ -79,3 +82,9 @@ end
|
|
79
82
|
`{ 'create' => 'new', 'update' => 'edit' }` is a default REST configuration.
|
80
83
|
|
81
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).
|
85
|
+
|
86
|
+
## Upgrade
|
87
|
+
|
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
|
+
* [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
|
+
* [from v1.0 to v2.0](https://github.com/peresleguine/pluggable_js/wiki/Upgrade-from-v1.0-to-v2.0)
|
data/lib/pluggable_js/helpers.rb
CHANGED
@@ -1,23 +1,29 @@
|
|
1
1
|
module PluggableJs
|
2
2
|
module Helpers
|
3
3
|
|
4
|
-
module
|
5
|
-
#
|
4
|
+
module View
|
5
|
+
# call function and pass data, include file if it exists
|
6
6
|
def javascript_pluggable_tag
|
7
7
|
controller = params[:controller]
|
8
8
|
action = define_pair_action
|
9
9
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
}
|
19
|
-
});"
|
20
|
-
|
10
|
+
''.tap do |content|
|
11
|
+
content << (javascript_tag "
|
12
|
+
(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
|
+
});
|
19
|
+
}).call(this);"
|
20
|
+
)
|
21
|
+
|
22
|
+
if File.exist?(Rails.root + "app/assets/javascripts/pluggable/#{controller}/#{action}.js.coffee")
|
23
|
+
content << (javascript_include_tag "pluggable/#{controller}/#{action}")
|
24
|
+
end
|
25
|
+
end.html_safe
|
26
|
+
|
21
27
|
end
|
22
28
|
|
23
29
|
private
|
@@ -33,13 +39,10 @@ module PluggableJs
|
|
33
39
|
|
34
40
|
end
|
35
41
|
|
36
|
-
module
|
37
|
-
# convert hash passed from
|
42
|
+
module Controller
|
43
|
+
# convert hash passed from controller's action to data string
|
38
44
|
def pluggable_js(hash)
|
39
|
-
data_string = hash.map
|
40
|
-
"pluggable_js.#{key} = pjs.#{key} = #{value.to_json};"
|
41
|
-
end.join(' ')
|
42
|
-
@js_data_string = 'window.pluggable_js = window.pjs = {}; ' + data_string
|
45
|
+
@data_string = hash.map { |key, value| "pjs_data.#{key} = #{value.to_json}" }.join('; ')
|
43
46
|
end
|
44
47
|
alias_method :pjs, :pluggable_js
|
45
48
|
|
data/lib/pluggable_js/railtie.rb
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
module PluggableJs
|
2
2
|
class Railtie < Rails::Railtie
|
3
3
|
initializer 'pluggable_js.helpers' do
|
4
|
-
ActionView::Base.send :include, Helpers::
|
5
|
-
ActionController::Base.send :include, Helpers::
|
4
|
+
ActionView::Base.send :include, Helpers::View
|
5
|
+
ActionController::Base.send :include, Helpers::Controller
|
6
6
|
end
|
7
7
|
end
|
8
8
|
end
|
data/lib/pluggable_js/version.rb
CHANGED
@@ -1,3 +1,3 @@
|
|
1
1
|
module PluggableJs
|
2
|
-
VERSION = "
|
3
|
-
end
|
2
|
+
VERSION = "2.0.0"
|
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{
|
12
|
-
spec.summary = %q{Pluggable javascript.}
|
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.}
|
13
13
|
spec.homepage = 'https://github.com/peresleguine/pluggable_js'
|
14
14
|
spec.license = 'MIT'
|
15
15
|
|
@@ -25,7 +25,5 @@ Gem::Specification.new do |spec|
|
|
25
25
|
spec.add_development_dependency 'bundler', '~> 1.3'
|
26
26
|
spec.add_development_dependency 'cucumber-rails'
|
27
27
|
spec.add_development_dependency 'rspec'
|
28
|
-
spec.add_development_dependency 'sqlite3'
|
29
|
-
spec.add_development_dependency 'database_cleaner'
|
30
28
|
spec.add_development_dependency 'capybara-webkit'
|
31
|
-
end
|
29
|
+
end
|
@@ -1,11 +1,13 @@
|
|
1
|
-
window
|
2
|
-
|
3
|
-
$('.protoss-quotes').append(
|
4
|
-
$('.protoss-quotes').append('<p>
|
5
|
-
$('.protoss-quotes').append(
|
6
|
-
|
7
|
-
for key, value of pluggable_js.ground_units_quotes
|
1
|
+
window['posts#index'] = (data) ->
|
2
|
+
$('.protoss-quotes').append("<p>#{data.zealot_quote}</p>")
|
3
|
+
$('.protoss-quotes').append('<p>You have not enough minerals.</p>' if data.minerals_size < 1000)
|
4
|
+
$('.protoss-quotes').append('<p>Base is under attack.</p>') if data.base_is_under_attack
|
5
|
+
$('.protoss-quotes').append("<p>#{data.alert.join(' ')}</p>")
|
6
|
+
for key, value of data.ground_units_quotes
|
8
7
|
$('.protoss-quotes').append("<p>#{key}: #{value}</p>")
|
9
|
-
for unit in
|
8
|
+
for unit in data.air_units_quotes
|
10
9
|
for key, value of unit
|
11
10
|
$('.protoss-quotes').append("<p>#{key}: #{value}</p>")
|
11
|
+
|
12
|
+
window['posts#new'] = (data) ->
|
13
|
+
$('.terran-quotes').append("<p>#{data.marine_quote}</p>")
|
@@ -13,15 +13,9 @@ Dummy::Application.configure do
|
|
13
13
|
config.consider_all_requests_local = true
|
14
14
|
config.action_controller.perform_caching = false
|
15
15
|
|
16
|
-
# Don't care if the mailer can't send.
|
17
|
-
config.action_mailer.raise_delivery_errors = false
|
18
|
-
|
19
16
|
# Print deprecation notices to the Rails logger.
|
20
17
|
config.active_support.deprecation = :log
|
21
18
|
|
22
|
-
# Raise an error on page load if there are pending migrations
|
23
|
-
config.active_record.migration_error = :page_load
|
24
|
-
|
25
19
|
# Debug mode disables concatenation and preprocessing of assets.
|
26
20
|
# This option may cause significant delays in view rendering with a large
|
27
21
|
# number of complex assets.
|
@@ -26,11 +26,6 @@ Dummy::Application.configure do
|
|
26
26
|
# Disable request forgery protection in test environment.
|
27
27
|
config.action_controller.allow_forgery_protection = false
|
28
28
|
|
29
|
-
# Tell Action Mailer not to deliver emails to the real world.
|
30
|
-
# The :test delivery method accumulates sent emails in the
|
31
|
-
# ActionMailer::Base.deliveries array.
|
32
|
-
config.action_mailer.delivery_method = :test
|
33
|
-
|
34
29
|
# Print deprecation notices to the stderr.
|
35
30
|
config.active_support.deprecation = :stderr
|
36
31
|
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:
|
4
|
+
version: 2.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-12-
|
11
|
+
date: 2013-12-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: coffee-rails
|
@@ -94,34 +94,6 @@ dependencies:
|
|
94
94
|
- - '>='
|
95
95
|
- !ruby/object:Gem::Version
|
96
96
|
version: '0'
|
97
|
-
- !ruby/object:Gem::Dependency
|
98
|
-
name: sqlite3
|
99
|
-
requirement: !ruby/object:Gem::Requirement
|
100
|
-
requirements:
|
101
|
-
- - '>='
|
102
|
-
- !ruby/object:Gem::Version
|
103
|
-
version: '0'
|
104
|
-
type: :development
|
105
|
-
prerelease: false
|
106
|
-
version_requirements: !ruby/object:Gem::Requirement
|
107
|
-
requirements:
|
108
|
-
- - '>='
|
109
|
-
- !ruby/object:Gem::Version
|
110
|
-
version: '0'
|
111
|
-
- !ruby/object:Gem::Dependency
|
112
|
-
name: database_cleaner
|
113
|
-
requirement: !ruby/object:Gem::Requirement
|
114
|
-
requirements:
|
115
|
-
- - '>='
|
116
|
-
- !ruby/object:Gem::Version
|
117
|
-
version: '0'
|
118
|
-
type: :development
|
119
|
-
prerelease: false
|
120
|
-
version_requirements: !ruby/object:Gem::Requirement
|
121
|
-
requirements:
|
122
|
-
- - '>='
|
123
|
-
- !ruby/object:Gem::Version
|
124
|
-
version: '0'
|
125
97
|
- !ruby/object:Gem::Dependency
|
126
98
|
name: capybara-webkit
|
127
99
|
requirement: !ruby/object:Gem::Requirement
|
@@ -136,8 +108,8 @@ dependencies:
|
|
136
108
|
- - '>='
|
137
109
|
- !ruby/object:Gem::Version
|
138
110
|
version: '0'
|
139
|
-
description:
|
140
|
-
|
111
|
+
description: A solution for Rails of how to load page specific javascript and pass
|
112
|
+
data.
|
141
113
|
email:
|
142
114
|
- peresleguine@gmail.com
|
143
115
|
executables: []
|
@@ -185,7 +157,6 @@ files:
|
|
185
157
|
- test/dummy/config.ru
|
186
158
|
- test/dummy/config/application.rb
|
187
159
|
- test/dummy/config/boot.rb
|
188
|
-
- test/dummy/config/database.yml
|
189
160
|
- test/dummy/config/environment.rb
|
190
161
|
- test/dummy/config/environments/development.rb
|
191
162
|
- test/dummy/config/environments/production.rb
|
@@ -200,9 +171,6 @@ files:
|
|
200
171
|
- test/dummy/config/initializers/wrap_parameters.rb
|
201
172
|
- test/dummy/config/locales/en.yml
|
202
173
|
- test/dummy/config/routes.rb
|
203
|
-
- test/dummy/db/development.sqlite3
|
204
|
-
- test/dummy/db/schema.rb
|
205
|
-
- test/dummy/db/test.sqlite3
|
206
174
|
- test/dummy/lib/assets/.keep
|
207
175
|
- test/dummy/public/404.html
|
208
176
|
- test/dummy/public/422.html
|
@@ -231,7 +199,7 @@ rubyforge_project:
|
|
231
199
|
rubygems_version: 2.1.11
|
232
200
|
signing_key:
|
233
201
|
specification_version: 4
|
234
|
-
summary: Pluggable javascript.
|
202
|
+
summary: Pluggable javascript and data passing for Rails.
|
235
203
|
test_files:
|
236
204
|
- features/pluggable_js.feature
|
237
205
|
- features/step_definitions/pluggable_js_steps.rb
|
@@ -259,7 +227,6 @@ test_files:
|
|
259
227
|
- test/dummy/config.ru
|
260
228
|
- test/dummy/config/application.rb
|
261
229
|
- test/dummy/config/boot.rb
|
262
|
-
- test/dummy/config/database.yml
|
263
230
|
- test/dummy/config/environment.rb
|
264
231
|
- test/dummy/config/environments/development.rb
|
265
232
|
- test/dummy/config/environments/production.rb
|
@@ -274,9 +241,6 @@ test_files:
|
|
274
241
|
- test/dummy/config/initializers/wrap_parameters.rb
|
275
242
|
- test/dummy/config/locales/en.yml
|
276
243
|
- test/dummy/config/routes.rb
|
277
|
-
- test/dummy/db/development.sqlite3
|
278
|
-
- test/dummy/db/schema.rb
|
279
|
-
- test/dummy/db/test.sqlite3
|
280
244
|
- test/dummy/lib/assets/.keep
|
281
245
|
- test/dummy/public/404.html
|
282
246
|
- test/dummy/public/422.html
|
@@ -1,25 +0,0 @@
|
|
1
|
-
# SQLite version 3.x
|
2
|
-
# gem install sqlite3
|
3
|
-
#
|
4
|
-
# Ensure the SQLite 3 gem is defined in your Gemfile
|
5
|
-
# gem 'sqlite3'
|
6
|
-
development:
|
7
|
-
adapter: sqlite3
|
8
|
-
database: db/development.sqlite3
|
9
|
-
pool: 5
|
10
|
-
timeout: 5000
|
11
|
-
|
12
|
-
# Warning: The database defined as "test" will be erased and
|
13
|
-
# re-generated from your development database when you run "rake".
|
14
|
-
# Do not set this db to the same as development or production.
|
15
|
-
test:
|
16
|
-
adapter: sqlite3
|
17
|
-
database: db/test.sqlite3
|
18
|
-
pool: 5
|
19
|
-
timeout: 5000
|
20
|
-
|
21
|
-
production:
|
22
|
-
adapter: sqlite3
|
23
|
-
database: db/production.sqlite3
|
24
|
-
pool: 5
|
25
|
-
timeout: 5000
|
Binary file
|
data/test/dummy/db/schema.rb
DELETED
@@ -1,16 +0,0 @@
|
|
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
|
data/test/dummy/db/test.sqlite3
DELETED
File without changes
|