pluggable_js 1.0.3 → 2.0.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: d8344f65c6acf13c203b313e6057a414f83e49fd
4
- data.tar.gz: 6450599c3ab762d0a385abffd81292f6be6a63b0
3
+ metadata.gz: 9f7dafe469757bf25d292d0b49081de701d62f8d
4
+ data.tar.gz: 7fad58ce62bc70a5d0541e117673395873681131
5
5
  SHA512:
6
- metadata.gz: 54a40748b5a52170e0ac9c0f9537c2a7e9812f43f9ee2cc76145b292e3b936fde5881be6f5a8dcdd3a231ad0a1bb07ae2b727ae4cd791d72a7b501562dc07316
7
- data.tar.gz: 7fc292f5541052fe50a30b8953607cdedf6459892c5fbe96731af0473a8c493f865411e2efd1a3ea4293c15ca65e04aa94aec281e64d8717e86c0eb00acd3ec6
6
+ metadata.gz: e1ea81dfe8a2006359d5162d4d8e901ee80af6f6ae3a3ffed85bf2b2660ed6e937bfc8638b38db1972eb9eabd9e25642220e9ee89d70a899fd3bbf905af47d58
7
+ data.tar.gz: ad1cd94d4e0d88b2bb056db7f071f21781723fee930bee51abc1dd8c8232322d147535205d491588306576febbdb9a20eb69773e7c4eebba9cf47012e1cfe63b
@@ -45,3 +45,7 @@
45
45
  ## v1.0.3
46
46
 
47
47
  * removed unnecessary conditions from pluggable_js controller helper
48
+
49
+ ## v2.0.0
50
+
51
+ * improved namespace and data passing
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 passing data from a controller (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.
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
- Next steps are necessary only if you want to use generator for large pieces of js code (see usage):
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.posts = {}
21
- posts.index = () ->
26
+ window['posts#index'] = (data) ->
22
27
  # your code goes here
23
- posts.new = () ->
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
- Starting with version 1.0.0 you may pass data to javascript using `pluggable_js` helper in a controller. See example below:
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.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
- 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
- Note: `pjs` is an alias of `pluggable_js`.
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.index()` function. In such situation you may create `config/initializers/pluggable_js.rb` and use pair actions config:
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)
@@ -21,4 +21,5 @@ Feature: PluggableJs
21
21
  Scenario: New Post
22
22
  When I go to new post
23
23
  Then I should see 'You wanna piece of me, boy?'
24
+ And I should see 'Fire it up.'
24
25
  And I should not see 'My life for aiur.'
@@ -1,23 +1,29 @@
1
1
  module PluggableJs
2
2
  module Helpers
3
3
 
4
- module ViewHelpers
5
- # include file if it exists or pass data and call a function
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
- 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
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 ControllerHelpers
37
- # convert hash passed from a controller's action to js data string
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 do |key, value|
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
 
@@ -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::ViewHelpers
5
- ActionController::Base.send :include, Helpers::ControllerHelpers
4
+ ActionView::Base.send :include, Helpers::View
5
+ ActionController::Base.send :include, Helpers::Controller
6
6
  end
7
7
  end
8
8
  end
@@ -1,3 +1,3 @@
1
1
  module PluggableJs
2
- VERSION = "1.0.3"
3
- end
2
+ VERSION = "2.0.0"
3
+ end
@@ -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{Convention of how to load page specific javascript based on controller and action parameters.}
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
@@ -2,4 +2,4 @@
2
2
  # don't want to define as a function in controller related js file.
3
3
 
4
4
  jQuery ->
5
- $('.terran-quotes').text('You wanna piece of me, boy?')
5
+ $('.terran-quotes').append('<p>Fire it up.</p>')
@@ -1,11 +1,13 @@
1
- window.posts = {}
2
- posts.index = () ->
3
- $('.protoss-quotes').append("<p>#{pjs.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, 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 pluggable_js.air_units_quotes
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>")
@@ -9,6 +9,7 @@ class PostsController < ApplicationController
9
9
  end
10
10
 
11
11
  def new
12
+ pjs({marine_quote: 'You wanna piece of me, boy?'})
12
13
  end
13
14
 
14
15
  private
@@ -1,6 +1,7 @@
1
1
  require File.expand_path('../boot', __FILE__)
2
2
 
3
- require 'rails/all'
3
+ require 'action_controller/railtie'
4
+ require 'sprockets/railtie'
4
5
 
5
6
  Bundler.require(*Rails.groups)
6
7
  require 'pluggable_js'
@@ -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: 1.0.3
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-17 00:00:00.000000000 Z
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: Convention of how to load page specific javascript based on controller
140
- and action parameters.
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
@@ -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
File without changes