js_hooks 0.0.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: cef0ed4b6e0af39af7ad94682ff3a511e204da13
4
+ data.tar.gz: ecead09416b0e5b8e50d091503f93dba21426e30
5
+ SHA512:
6
+ metadata.gz: 144ed7796be9b670e2480b96b47cfde3ee783298b21c17cade786f77c2b7ba85bb9734553514d9292796db695fc26abb6b922df67a7caf0449f8893afdd73634
7
+ data.tar.gz: 2d08b477408ec0469e0f50360b46bc0c97e3f0577d7c7e81337e368078b2642e09b11ef1d5c25e85daadb2edf00f4c5dd8727861b53eae5bcec660c77c3bd3ad
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ js_hooks
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ ruby-2.0.0-p353
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in jshooks.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,23 @@
1
+ Copyright (c) 2014 Francisco R. Santos
2
+
3
+
4
+ MIT License
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining
7
+ a copy of this software and associated documentation files (the
8
+ "Software"), to deal in the Software without restriction, including
9
+ without limitation the rights to use, copy, modify, merge, publish,
10
+ distribute, sublicense, and/or sell copies of the Software, and to
11
+ permit persons to whom the Software is furnished to do so, subject to
12
+ the following conditions:
13
+
14
+ The above copyright notice and this permission notice shall be
15
+ included in all copies or substantial portions of the Software.
16
+
17
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,82 @@
1
+ # Jshooks
2
+ Usually, there are tons of JavaScript hooks in an application, some used in all pages, but many used only in a single
3
+ group of pages, or even in only a single page. This gems invokes those hooks only when needed.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'js_hooks'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install js_hooks
18
+
19
+ ### Rails 3.1 or greater
20
+ Include the módule in your controller:
21
+
22
+ class MyController < ApplicationController
23
+ act_as_js_hookable
24
+ end
25
+
26
+ Add the js_hooks.js file to `app/assets/javascripts/application.js`:
27
+
28
+ //= require js_hooks
29
+
30
+ And add the helper to your views:
31
+
32
+ <%= javascript_hooks_tag %>
33
+
34
+ ## Usage
35
+ You can define hook by defining a ``init`` method inside an plain object and calling ``JsHooks.registerHook`` inside
36
+ a closure before the body close tag in your templates. For each hook added, the init method is called in the
37
+ ``documentReady`` event:
38
+
39
+ (function() {
40
+ 'use strict';
41
+
42
+ window.JsHooks.registerHook('Users', {
43
+ init: function() {
44
+ // Set my hooks
45
+ $('#id').on('click', function() {
46
+ // do something
47
+ });
48
+ }
49
+ });
50
+ })()
51
+
52
+ This ``init`` function will be called automatically whenever the ``UsersController`` is used.
53
+
54
+ By default, the gem invokes the hooks related to the current controller, but hooks can be added if they are not named
55
+ after the current controller. For example, in your view, add:
56
+
57
+ <%= add_js_hook( :users, :clients ) %>
58
+ --> Users.init(); Clients.init()
59
+
60
+ By default, the ``init`` function is called for each hook. However, a suffix can be added to the ``init`` method to refine
61
+ the initialization process:
62
+
63
+ <%= add_js_hook( :users, :clients, users: 'action' ) %>
64
+ --> Users.init(); Clients.init(); Users.initAction();
65
+
66
+ <%= add_js_hook( :clients, users: ['', 'action1', 'action2'] ) %>
67
+ --> Clients.init(); Users.init(); Users.initAction1(); Users.initAction2();
68
+
69
+ Three hooks are added by default: one with 'application' as name, one with the controller as name and one last
70
+ with the controller as name and template as suffix.
71
+
72
+ So, by default, the following methods will be called if they exist:
73
+
74
+ GET /users/1 --> Application.init(); Users.init(); Users.initShow()
75
+
76
+ ## Contributing
77
+
78
+ 1. Fork it
79
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
80
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
81
+ 4. Push to the branch (`git push origin my-new-feature`)
82
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/js_hooks.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'js_hooks/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'js_hooks'
8
+ spec.version = Jshooks::VERSION
9
+ spec.authors = ['Francisco R. Santos']
10
+ spec.email = ['frsantos@aspgems.com']
11
+ spec.description = %q{Manages JavaScript hooks for Rails controllers and views}
12
+ spec.summary = %q{Manages JavaScript hooks for Rails controllers and views}
13
+ spec.homepage = 'https://github.com/aspgems/js_hooks'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_dependency 'rails', '~> 4.0.2'
22
+
23
+ spec.add_development_dependency 'bundler', '~> 1.3'
24
+ spec.add_development_dependency 'rake'
25
+ end
@@ -0,0 +1,80 @@
1
+ require 'abstract_controller/helpers'
2
+
3
+ module JsHooks
4
+ module Controller
5
+ extend ActiveSupport::Concern
6
+
7
+ module ClassMethods
8
+ def act_as_js_hookable(options = {})
9
+ before_filter :add_default_js_hooks if :add_default_js_hooks?
10
+
11
+ include JsHooks::Controller::LocalInstanceMethods
12
+
13
+ helper_method :js_hooks, :add_js_hook
14
+ end
15
+ end
16
+
17
+ module LocalInstanceMethods
18
+ # Javascript on demand hooks
19
+ def js_hooks
20
+ @js_hooks ||= {}
21
+ end
22
+
23
+ # Add javascript components to execute inside a document ready jquery closure before the body close tag in your
24
+ # templates. For each hook added, an object with the same name is searched and the init method is called.
25
+ # A suffix can be used with the init method to refine the initialization process:
26
+ #
27
+ # add_js_hook( :users, :clients )
28
+ # --> Users.init(); Clients.init()
29
+ #
30
+ # add_js_hook( :users, :clients, users: 'action' )
31
+ # --> Users.init(); Clients.init(); Users.initAction();
32
+ #
33
+ # add_js_hook( :clients, users: ['', 'action1', 'action2'] )
34
+ # --> Clients.init(); Users.init(); Users.initAction1(); Users.initAction2();
35
+ #
36
+ # Three hooks are added by default: one with 'application' as name, one with the controller as name and one last
37
+ # with the controller as name and template as suffix.
38
+ # So, by default, the following methods will be called if they exist:
39
+ #
40
+ # GET /users/1 --> Application.init(); Users.init(); Users.initShow()
41
+ def add_js_hook(*args)
42
+ opts = args.extract_options!
43
+
44
+ # Default init method
45
+ args.each do |hook|
46
+ hook = hook.to_s.camelize
47
+ js_hooks[hook] ||= []
48
+ js_hooks[hook] = (js_hooks[hook] + [true]).uniq
49
+ end
50
+
51
+ # Suffix init method
52
+ opts.each_pair do |hook, methods|
53
+ hook = hook.to_s.camelize
54
+ js_hooks[hook] ||= []
55
+ if methods
56
+ js_hooks[hook] = (js_hooks[hook] + [methods].flatten.map! { |m| m.to_s.camelize.presence || true }).uniq
57
+ else
58
+ js_hooks.delete(hook)
59
+ end
60
+ end
61
+ end
62
+
63
+ private
64
+ def add_default_js_hooks
65
+ add_js_hook :application
66
+ add_js_hook controller_name
67
+ end
68
+
69
+ # Add template as suffix to the controller hook: GET users/1 -> Users.initShow()
70
+ def render_to_body(options = {})
71
+ add_js_hook controller_name => options[:template].to_s.camelize if options[:template] && add_default_js_hooks?
72
+ super
73
+ end
74
+
75
+ def add_default_js_hooks?
76
+ request.format.html?
77
+ end
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,12 @@
1
+ module JsHooks
2
+ module Helpers
3
+ # Build the javascript code need to initialize the components js_hooks
4
+ def javascript_hooks_tag
5
+ javascript_tag <<-JS
6
+ window.JsHooks = window.JsHooks || {};
7
+ window.JsHooks.namespace = '.#{Rails.application.class.to_s.split('::').first}';
8
+ window.JsHooks.hooks = #{js_hooks.to_json};
9
+ JS
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,19 @@
1
+ module JsHooks
2
+ class Railtie < Rails::Railtie
3
+ initializer 'js_hooks.initialize' do
4
+ ActiveSupport.on_load :action_controller do
5
+ require 'js_hooks/controller'
6
+ include JsHooks::Controller
7
+ end
8
+
9
+ ActiveSupport.on_load :action_view do
10
+ require 'js_hooks/helpers'
11
+ include JsHooks::Helpers
12
+ end
13
+ end
14
+ end
15
+
16
+ # Needed for assets pipeline
17
+ class Engine < Rails::Engine
18
+ end
19
+ end
@@ -0,0 +1,3 @@
1
+ module Jshooks
2
+ VERSION = '0.0.1'
3
+ end
data/lib/js_hooks.rb ADDED
@@ -0,0 +1,5 @@
1
+ require 'js_hooks/version'
2
+ require 'js_hooks/railtie'
3
+
4
+ module Jshooks
5
+ end
@@ -0,0 +1,59 @@
1
+ /*jshint nonew:true, jquery:true, curly:true, noarg:true, forin:true, noempty:true, eqeqeq:true, strict:true, undef:true, bitwise:true, browser:true */
2
+
3
+ (function () {
4
+ 'use strict';
5
+
6
+ function registerHook(hookClass, hooks) {
7
+ window.JsHooks[hookClass] = hooks;
8
+
9
+ }
10
+
11
+ function initHooks() {
12
+ var jsHooks = window.JsHooks;
13
+
14
+ // Remove all events added by the previous turbolinks page
15
+ if(jsHooks.namespace) {
16
+ $(document).off(jsHooks.namespace);
17
+ }
18
+
19
+ var hooks = jsHooks.hooks;
20
+ for (var hookClass in hooks) {
21
+ if(hooks.hasOwnProperty(hookClass)) {
22
+ var hook = jsHooks[hookClass];
23
+ if (!hook) {
24
+ continue;
25
+ }
26
+
27
+ var methods = hooks[hookClass];
28
+ if (!$.isArray(methods)) {
29
+ methods = ['']; // invoke the default .init() method
30
+ }
31
+
32
+ for (var i = 0; i < methods.length; i++) {
33
+ var method = methods[i];
34
+ if (method === true) {
35
+ method = '';
36
+ } else if (method === false) {
37
+ continue;
38
+ }
39
+ method = 'init' + method;
40
+
41
+ if ($.isFunction(hook[method])) {
42
+ // if(console && console.debug) console.debug('Calling ' + hookClass + '.' + method + '();');
43
+ hook[method]();
44
+ }
45
+ }
46
+ }
47
+ }
48
+ }
49
+
50
+ // Set initializer only once
51
+ if (!window.JsHooks || !window.JsHooks.initialized) {
52
+ $(document).ready(initHooks).on('page:load', initHooks);
53
+ }
54
+
55
+ window.JsHooks = window.JsHooks || {};
56
+ window.JsHooks.initialized = true;
57
+ window.JsHooks.registerHook = registerHook;
58
+ })();
59
+
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: js_hooks
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Francisco R. Santos
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-03-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: 4.0.2
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: 4.0.2
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '1.3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '1.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Manages JavaScript hooks for Rails controllers and views
56
+ email:
57
+ - frsantos@aspgems.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - .gitignore
63
+ - .ruby-gemset
64
+ - .ruby-version
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - js_hooks.gemspec
70
+ - lib/js_hooks.rb
71
+ - lib/js_hooks/controller.rb
72
+ - lib/js_hooks/helpers.rb
73
+ - lib/js_hooks/railtie.rb
74
+ - lib/js_hooks/version.rb
75
+ - vendor/assets/javascripts/js_hooks.js
76
+ homepage: https://github.com/aspgems/js_hooks
77
+ licenses:
78
+ - MIT
79
+ metadata: {}
80
+ post_install_message:
81
+ rdoc_options: []
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - '>='
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ requirements: []
95
+ rubyforge_project:
96
+ rubygems_version: 2.2.1
97
+ signing_key:
98
+ specification_version: 4
99
+ summary: Manages JavaScript hooks for Rails controllers and views
100
+ test_files: []