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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5562c201846d6f7300763ae176e9419888c3d31d
4
- data.tar.gz: 5aa02bbfdaefb4af6476049965a0bd89819a0c66
3
+ metadata.gz: d7e1dc20d0c0328f1bf75473e5a53c1498cb92ee
4
+ data.tar.gz: 1ea6d564384dc36b3f4e11a7a870b2217351e2a0
5
5
  SHA512:
6
- metadata.gz: 3f0c88cbf4c76faca4f8ceab301ae0e75ae25852952295e6a787c4fd0f65030e2ceeecaa3de38499b8f9345e2e6087b2d48f2495e55c9bf9478cd904c0a16a97
7
- data.tar.gz: 123d78a1fc300758f3463741e509b88d2d40e9fcb5a4498812ec814cd6c2c8396cdd7f98b04112fd3b8aef2afdb12f1ee24437fcd5fa423aaab5575bb0efc768
6
+ metadata.gz: a72f01f53602186364f71b60cad17bc562369eeb6ef5b006746462d1200ef9af7c42c566716d3604e9f1189728eb24ee8f1ff9f67f73777e71b315789eff243a
7
+ data.tar.gz: 26a4568fd34a37e100ddb26e0e2813214a52f92496c6dd71dfe9aa2c9e72be1a0b1ac27846aaef9a96bf59b10224077d00d7f1072e4644377b5f98731d419d56
data/CHANGELOG.md CHANGED
@@ -29,3 +29,7 @@
29
29
  ## v0.0.6
30
30
 
31
31
  * fixed function call for IE
32
+
33
+ ## v1.0.0
34
+
35
+ * passing data to javascript functionality
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.Post ||= {}
22
- Post.index = () ->
21
+ window.posts = {}
22
+ posts.index = () ->
23
23
  # your code goes here
24
- Post.new = () ->
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 Post index new
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 `Post.index()` function. In such situation you may create `config/initializers/pluggable_js.rb` and use pair actions config:
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 different working scripts
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 list of posts
8
- Then I should see 'My life for aiur!'
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 list of posts$/) do
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 Post index new
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
@@ -12,10 +12,6 @@ class PluggableJsGenerator < Rails::Generators::Base
12
12
 
13
13
  private
14
14
 
15
- def model_name
16
- controller.classify
17
- end
18
-
19
15
  def controller_name
20
16
  controller.tableize
21
17
  end
@@ -1,33 +1,54 @@
1
1
  module PluggableJs
2
2
  module Helpers
3
3
 
4
- # if file exists – include it, else – call function
5
- def javascript_pluggable_tag
6
- controller = params[:controller]
7
- action = define_pair_action
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
- if File.exist?(Rails.root + "app/assets/javascripts/pluggable/#{controller}/#{action}.js.coffee")
10
- javascript_include_tag "pluggable/#{controller}/#{action}"
11
- else
12
- object = controller.classify
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
- javascript_tag "jQuery(function() {
15
- if (typeof(#{object}) == 'object' && typeof(#{object}['#{action}']) == 'function') {
16
- return #{object}['#{action}']();
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
- private
34
+ end
23
35
 
24
- def define_pair_action
25
- action = params[:action]
26
- if Config.pair_actions.has_key?(action)
27
- Config.pair_actions[action]
28
- else
29
- action
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
@@ -1,7 +1,8 @@
1
1
  module PluggableJs
2
2
  class Railtie < Rails::Railtie
3
- initializer "pluggable_js.helpers" do
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
@@ -1,3 +1,3 @@
1
1
  module PluggableJs
2
- VERSION = "0.0.6"
2
+ VERSION = "1.0.0"
3
3
  end
@@ -1,4 +1,4 @@
1
- # This file is used only for large piece of js code that you
1
+ # This file is used only for a 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
4
  jQuery ->
@@ -1,3 +1,8 @@
1
- window.Post ||= {}
2
- Post.index = () ->
3
- $('.zealot-quote').text('My life for aiur!')
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
- def index
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='zealot-quote'></div>
1
+ <div class='protoss-quotes'></div>
2
2
  <div class='marine-quote'></div>
@@ -77,4 +77,6 @@ Dummy::Application.configure do
77
77
 
78
78
  # Use default logging formatter so that PID and timestamp are not suppressed.
79
79
  config.log_formatter = ::Logger::Formatter.new
80
+
81
+ config.assets.precompile += %w(pluggable/*)
80
82
  end
@@ -0,0 +1,3 @@
1
+ PluggableJs.config do |config|
2
+ config.pair_actions = { 'search' => 'index' }
3
+ end
@@ -1,3 +1,7 @@
1
1
  Dummy::Application.routes.draw do
2
- resources :posts, only: [:index, :new]
2
+ resources :posts, only: [:index, :new] do
3
+ collection do
4
+ get :search
5
+ end
6
+ end
3
7
  end
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.6
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-15 00:00:00.000000000 Z
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