js_hooks 0.0.1 → 0.0.2

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: cef0ed4b6e0af39af7ad94682ff3a511e204da13
4
- data.tar.gz: ecead09416b0e5b8e50d091503f93dba21426e30
3
+ metadata.gz: adeb9388407f9a822354ef1354aea2f819eb8409
4
+ data.tar.gz: b5d0ad3fc47bcd2319929b5adeb3d4c0e12b3113
5
5
  SHA512:
6
- metadata.gz: 144ed7796be9b670e2480b96b47cfde3ee783298b21c17cade786f77c2b7ba85bb9734553514d9292796db695fc26abb6b922df67a7caf0449f8893afdd73634
7
- data.tar.gz: 2d08b477408ec0469e0f50360b46bc0c97e3f0577d7c7e81337e368078b2642e09b11ef1d5c25e85daadb2edf00f4c5dd8727861b53eae5bcec660c77c3bd3ad
6
+ metadata.gz: 9ef9f8f095bbefcc463ba7448dcd447e8b0e15f0f959281c56f82c04dcc8003280788ff8fe56add281d8349e2e2fb5d69cd70e07b018df9086f536453f65dd1e
7
+ data.tar.gz: 5cce48c9949e703361325f5f199919e32865391077300b5959b632a3db7f2b6a566c9469a7862f68ab237765e1fedb826b231011631c5f2d80ad1d0bfe599113
data/README.md CHANGED
@@ -54,17 +54,17 @@ This ``init`` function will be called automatically whenever the ``UsersControll
54
54
  By default, the gem invokes the hooks related to the current controller, but hooks can be added if they are not named
55
55
  after the current controller. For example, in your view, add:
56
56
 
57
- <%= add_js_hook( :users, :clients ) %>
57
+ <% add_js_hook( :users, :clients ) %>
58
58
  --> Users.init(); Clients.init()
59
59
 
60
60
  By default, the ``init`` function is called for each hook. However, a suffix can be added to the ``init`` method to refine
61
61
  the initialization process:
62
62
 
63
- <%= add_js_hook( :users, :clients, users: 'action' ) %>
63
+ <% add_js_hook( :users, :clients, users: 'action' ) %>
64
64
  --> Users.init(); Clients.init(); Users.initAction();
65
65
 
66
- <%= add_js_hook( :clients, users: ['', 'action1', 'action2'] ) %>
67
- --> Clients.init(); Users.init(); Users.initAction1(); Users.initAction2();
66
+ <% add_js_hook( :clients, users: [:default, 'show', 'edit'] ) %>
67
+ --> Clients.init(); Users.init(); Users.initShow(); Users.initEdit();
68
68
 
69
69
  Three hooks are added by default: one with 'application' as name, one with the controller as name and one last
70
70
  with the controller as name and template as suffix.
@@ -73,6 +73,12 @@ So, by default, the following methods will be called if they exist:
73
73
 
74
74
  GET /users/1 --> Application.init(); Users.init(); Users.initShow()
75
75
 
76
+ A hook can be deleted by passing a false value as suffix
77
+
78
+ GET /users/1
79
+ <% add_js_hook( :clients, users: false) %>
80
+ --> Application.init(); Clients.init();
81
+
76
82
  ## Contributing
77
83
 
78
84
  1. Fork it
@@ -2,6 +2,7 @@ require 'abstract_controller/helpers'
2
2
 
3
3
  module JsHooks
4
4
  module Controller
5
+ PREFIX = 'init'
5
6
  extend ActiveSupport::Concern
6
7
 
7
8
  module ClassMethods
@@ -30,7 +31,7 @@ module JsHooks
30
31
  # add_js_hook( :users, :clients, users: 'action' )
31
32
  # --> Users.init(); Clients.init(); Users.initAction();
32
33
  #
33
- # add_js_hook( :clients, users: ['', 'action1', 'action2'] )
34
+ # add_js_hook( :clients, users: [:default, 'action1', 'action2'] )
34
35
  # --> Clients.init(); Users.init(); Users.initAction1(); Users.initAction2();
35
36
  #
36
37
  # Three hooks are added by default: one with 'application' as name, one with the controller as name and one last
@@ -38,6 +39,11 @@ module JsHooks
38
39
  # So, by default, the following methods will be called if they exist:
39
40
  #
40
41
  # GET /users/1 --> Application.init(); Users.init(); Users.initShow()
42
+ #
43
+ # A hook can be deleted by passing a false value as suffix
44
+ # GET /users/1
45
+ # add_js_hook( :clients, users: false)
46
+ # --> Application.init(); Clients.init();
41
47
  def add_js_hook(*args)
42
48
  opts = args.extract_options!
43
49
 
@@ -45,15 +51,22 @@ module JsHooks
45
51
  args.each do |hook|
46
52
  hook = hook.to_s.camelize
47
53
  js_hooks[hook] ||= []
48
- js_hooks[hook] = (js_hooks[hook] + [true]).uniq
54
+ js_hooks[hook] = (js_hooks[hook] + [PREFIX]).uniq
49
55
  end
50
56
 
51
57
  # Suffix init method
52
58
  opts.each_pair do |hook, methods|
53
59
  hook = hook.to_s.camelize
54
60
  js_hooks[hook] ||= []
61
+ remove_default = false
55
62
  if methods
56
- js_hooks[hook] = (js_hooks[hook] + [methods].flatten.map! { |m| m.to_s.camelize.presence || true }).uniq
63
+ methods = [methods].flatten.map! do |m|
64
+ remove_default ||= !m
65
+ m = '' if m == :default
66
+ "#{PREFIX}#{m.to_s.camelize}"
67
+ end
68
+ js_hooks[hook] = (js_hooks[hook] + methods).uniq
69
+ js_hooks[hook].delete(PREFIX) if remove_default
57
70
  else
58
71
  js_hooks.delete(hook)
59
72
  end
@@ -1,3 +1,3 @@
1
1
  module Jshooks
2
- VERSION = '0.0.1'
2
+ VERSION = '0.0.2'
3
3
  end
@@ -26,17 +26,14 @@
26
26
 
27
27
  var methods = hooks[hookClass];
28
28
  if (!$.isArray(methods)) {
29
- methods = ['']; // invoke the default .init() method
29
+ methods = [methods];
30
30
  }
31
31
 
32
32
  for (var i = 0; i < methods.length; i++) {
33
33
  var method = methods[i];
34
- if (method === true) {
35
- method = '';
36
- } else if (method === false) {
34
+ if (method === false) {
37
35
  continue;
38
36
  }
39
- method = 'init' + method;
40
37
 
41
38
  if ($.isFunction(hook[method])) {
42
39
  // if(console && console.debug) console.debug('Calling ' + hookClass + '.' + method + '();');
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: js_hooks
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Francisco R. Santos