pagehook-rails 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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f1fb9f17ca1a217f7f7d7d2a0aca524fb4a05773
4
+ data.tar.gz: 032173384668bb9ed8e87411f4db844009066663
5
+ SHA512:
6
+ metadata.gz: e996a512a9d6e966416ef19ef3f088afac00a3d75aa48958080eb36c9b5bb2235790515373741165360ec6d40c8e70aa2c8fcbfb61ed68d84cdbed2575be045a
7
+ data.tar.gz: 66bae609658cc2f0d74684c635de547750c66719fabf3d02026b84e8c3f06bfbccb39d21c4077fb22498c310a5862fe44e4be4323e726ac7c3ccfcb5b82aaeb8
@@ -0,0 +1,20 @@
1
+ Copyright 2014 labocho
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,60 @@
1
+ pagehook-rails
2
+ ====================
3
+
4
+ pagehook-rails is Ruby on Rails plugin integrates [Pagehook](https://github.com/labocho/pagehook).
5
+
6
+
7
+ Installation
8
+ --------------------
9
+
10
+ Add this line to your Rails application's Gemfile:
11
+
12
+ gem "pagehook-rails"
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+ $ rails g pagehook:install
18
+ # Or `rails g pagehook:install --lang coffee` if you like CoofeeScript
19
+
20
+ If you use turbolinks, you should load `pagehook_rails` after `turbolinks` in `app/assets/javascripts/application.js`.
21
+
22
+
23
+ View helper
24
+ --------------------
25
+
26
+ `pagehook` view helper method generates "trigger element".
27
+
28
+ <%= pagehook("articles/index", id: 123) %>
29
+
30
+ This generates following HTML.
31
+
32
+ <script type="application/json" data-pagehook="articles/index">{"id":123}</script>
33
+
34
+
35
+ Generator
36
+ --------------------
37
+
38
+ `rails g pagehook path/to/view` generates hook definition template.
39
+
40
+ $ rails g pagehook articles/index
41
+ create app/assets/javascripts/pagehook/articles/index.coffee
42
+ $ cat app/assets/javascripts/pagehook/articles/index.coffee
43
+ //= require pagehook_rails
44
+ Pagehook.register("articles/index", function(data) {
45
+ // Your code
46
+ });
47
+
48
+
49
+ You can also do `rails g pagehook path/to/view --lang coffee` to generate .coffee rather than .js.
50
+
51
+
52
+ Contributing
53
+ --------------------
54
+
55
+ 1. Fork it ( http://github.com/labocho/pagehook-rails/fork )
56
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
57
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
58
+ 4. Push to the branch (`git push origin my-new-feature`)
59
+ 5. Create new Pull Request
60
+
@@ -0,0 +1,40 @@
1
+ #!/usr/bin/env rake
2
+ begin
3
+ require 'bundler/setup'
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
7
+ begin
8
+ require 'rdoc/task'
9
+ rescue LoadError
10
+ require 'rdoc/rdoc'
11
+ require 'rake/rdoctask'
12
+ RDoc::Task = Rake::RDocTask
13
+ end
14
+
15
+ RDoc::Task.new(:rdoc) do |rdoc|
16
+ rdoc.rdoc_dir = 'rdoc'
17
+ rdoc.title = 'PagehookRails'
18
+ rdoc.options << '--line-numbers'
19
+ rdoc.rdoc_files.include('README.rdoc')
20
+ rdoc.rdoc_files.include('lib/**/*.rb')
21
+ end
22
+
23
+ APP_RAKEFILE = File.expand_path("../spec/dummy/Rakefile", __FILE__)
24
+ load 'rails/tasks/engine.rake'
25
+
26
+
27
+
28
+ Bundler::GemHelper.install_tasks
29
+
30
+ require 'rake/testtask'
31
+
32
+ Rake::TestTask.new(:test) do |t|
33
+ t.libs << 'lib'
34
+ t.libs << 'test'
35
+ t.pattern = 'test/**/*_test.rb'
36
+ t.verbose = false
37
+ end
38
+
39
+
40
+ task :default => :test
@@ -0,0 +1,88 @@
1
+ (function() {
2
+ var Pagehook,
3
+ __slice = [].slice;
4
+
5
+ this.Pagehook = Pagehook = (function() {
6
+ Pagehook.GLOBAL_HOOK_NAME = "@global";
7
+
8
+ Pagehook.ATTRIBUTE_NAME = "data-pagehook";
9
+
10
+ Pagehook.register = function() {
11
+ var args, _ref;
12
+ args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
13
+ return (_ref = this.instance).register.apply(_ref, args);
14
+ };
15
+
16
+ Pagehook.dispatch = function() {
17
+ var args, _ref;
18
+ args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
19
+ return (_ref = this.instance).dispatch.apply(_ref, args);
20
+ };
21
+
22
+ function Pagehook() {
23
+ this.definitions = [];
24
+ this.handler = (function(_this) {
25
+ return function() {
26
+ return _this.handlerUnbound();
27
+ };
28
+ })(this);
29
+ }
30
+
31
+ Pagehook.prototype.register = function(name_or_map, func) {
32
+ var name, _results;
33
+ if (typeof name_or_map === "string") {
34
+ return this.definitions.push([name_or_map, func]);
35
+ } else {
36
+ _results = [];
37
+ for (name in name_or_map) {
38
+ func = name_or_map[name];
39
+ _results.push(this.definitions.push([name, func]));
40
+ }
41
+ return _results;
42
+ }
43
+ };
44
+
45
+ Pagehook.prototype.dispatch = function(name, params) {
46
+ var func, registered, _i, _len, _ref, _ref1, _results;
47
+ _ref = this.definitions;
48
+ _results = [];
49
+ for (_i = 0, _len = _ref.length; _i < _len; _i++) {
50
+ _ref1 = _ref[_i], registered = _ref1[0], func = _ref1[1];
51
+ if (registered === name) {
52
+ _results.push(func(params));
53
+ } else {
54
+ _results.push(void 0);
55
+ }
56
+ }
57
+ return _results;
58
+ };
59
+
60
+ Pagehook.prototype.handlerUnbound = function() {
61
+ var args, e, name, _i, _len, _ref, _results;
62
+ this.dispatch(Pagehook.GLOBAL_HOOK_NAME);
63
+ _ref = document.querySelectorAll("[" + Pagehook.ATTRIBUTE_NAME + "]");
64
+ _results = [];
65
+ for (_i = 0, _len = _ref.length; _i < _len; _i++) {
66
+ e = _ref[_i];
67
+ name = e.getAttribute(Pagehook.ATTRIBUTE_NAME);
68
+ args = this.isBlank(e.textContent) ? void 0 : JSON.parse(e.textContent);
69
+ _results.push(this.dispatch(name, args));
70
+ }
71
+ return _results;
72
+ };
73
+
74
+ Pagehook.prototype.isBlank = function(string) {
75
+ return !!(string.match(/^\s*$/));
76
+ };
77
+
78
+ return Pagehook;
79
+
80
+ })();
81
+
82
+ this.Pagehook.instance = new Pagehook();
83
+
84
+ this.Pagehook.handler = this.Pagehook.instance.handler;
85
+
86
+ }).call(this);
87
+
88
+ //# sourceMappingURL=pagehook.js.map
@@ -0,0 +1,18 @@
1
+ module PagehookRails
2
+ module PagehookHelper
3
+ # <%= pagehook("articles/show", id: 123) %>
4
+ # makes
5
+ # <script type="application/json" data-pagehook-name="articles/show">{"id": 123}</script>
6
+ def pagehook(name, data = {})
7
+ data_json = PagehookRails::PagehookHelper.escape_json(data.to_json)
8
+ content_tag(:script, data_json, type: "application/json", "data-pagehook" => name)
9
+ end
10
+
11
+ # convert html special characters to \uhhhh form
12
+ def self.escape_json(json)
13
+ json.gsub(/[\u2028\u2029><&]/u) {|s|
14
+ "\\u#{s.ord.to_s(16).rjust(4, "0")}"
15
+ }.html_safe
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,17 @@
1
+ module Pagehook
2
+ class InstallGenerator < Rails::Generators::Base
3
+ source_root File.expand_path("../templates", __FILE__)
4
+ class_option :lang, type: :string, default: "js"
5
+
6
+ def copy_loader
7
+ case options[:lang].downcase
8
+ when "coffee", "coffeescript"
9
+ copy_file "pagehook_rails.coffee", "app/assets/javascripts/pagehook_rails.coffee"
10
+ when "js", "javascript"
11
+ copy_file "pagehook_rails.js", "app/assets/javascripts/pagehook_rails.js"
12
+ else
13
+ raise "Unknown language: #{lang}"
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,6 @@
1
+ #= require pagehook_rails/pagehook
2
+ # Register event handler
3
+ if typeof(Turbolinks) == "undefined"
4
+ document.addEventListener("DOMContentLoaded", Pagehook.handler)
5
+ else
6
+ document.addEventListener("page:change", Pagehook.handler)
@@ -0,0 +1,7 @@
1
+ //= require pagehook_rails/pagehook
2
+ // Register event handler
3
+ if (typeof(Turbolinks) === "undefined") {
4
+ document.addEventListener("DOMContentLoaded", Pagehook.handler);
5
+ } else {
6
+ document.addEventListener("page:change", Pagehook.handler);
7
+ }
@@ -0,0 +1,20 @@
1
+ class PagehookGenerator < Rails::Generators::NamedBase
2
+ source_root File.expand_path("../templates", __FILE__)
3
+ class_option :lang, type: :string, default: "js"
4
+
5
+ def generate_script
6
+ case options[:lang].downcase
7
+ when "coffee", "coffeescript"
8
+ template "hook.coffee.erb", "app/assets/javascripts/pagehook/#{view_path}.coffee"
9
+ when "js", "javascript"
10
+ template "hook.js.erb", "app/assets/javascripts/pagehook/#{view_path}.js"
11
+ else
12
+ raise "Unknown language: #{lang}"
13
+ end
14
+ end
15
+
16
+ private
17
+ def view_path
18
+ file_path.gsub(/\..+/, "")
19
+ end
20
+ end
@@ -0,0 +1,3 @@
1
+ #= require pagehook_rails
2
+ Pagehook.register "<%= view_path %>", (data) ->
3
+ # Your code
@@ -0,0 +1,4 @@
1
+ //= require pagehook_rails
2
+ Pagehook.register("<%= view_path %>", function(data) {
3
+ // Your code
4
+ });
@@ -0,0 +1,4 @@
1
+ require "pagehook-rails/engine"
2
+
3
+ module PagehookRails
4
+ end
@@ -0,0 +1,11 @@
1
+ module PagehookRails
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace PagehookRails
4
+
5
+ initializer "pagehook-rails.action_controller" do |app|
6
+ ActiveSupport.on_load :action_controller do
7
+ helper PagehookRails::PagehookHelper
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,3 @@
1
+ module PagehookRails
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pagehook-rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - labocho
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-05-29 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: '3.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '3.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: sqlite3
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec-rails
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
+ - !ruby/object:Gem::Dependency
56
+ name: nokogiri
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Rails plugin integrates Pagehook (https://github.com/labocho/pagehook)
70
+ email:
71
+ - labocho@penguinlab.jp
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - MIT-LICENSE
77
+ - README.md
78
+ - Rakefile
79
+ - app/assets/javascripts/pagehook_rails/pagehook.js
80
+ - app/helpers/pagehook_rails/pagehook_helper.rb
81
+ - lib/generators/pagehook/install/install_generator.rb
82
+ - lib/generators/pagehook/install/templates/pagehook_rails.coffee
83
+ - lib/generators/pagehook/install/templates/pagehook_rails.js
84
+ - lib/generators/pagehook/pagehook_generator.rb
85
+ - lib/generators/pagehook/templates/hook.coffee.erb
86
+ - lib/generators/pagehook/templates/hook.js.erb
87
+ - lib/pagehook-rails.rb
88
+ - lib/pagehook-rails/engine.rb
89
+ - lib/pagehook-rails/version.rb
90
+ homepage: https://github.com/labocho/pagehook-rails
91
+ licenses: []
92
+ metadata: {}
93
+ post_install_message:
94
+ rdoc_options: []
95
+ require_paths:
96
+ - lib
97
+ required_ruby_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ requirements: []
108
+ rubyforge_project:
109
+ rubygems_version: 2.2.0
110
+ signing_key:
111
+ specification_version: 4
112
+ summary: Rails plugin integrates Pagehook (https://github.com/labocho/pagehook)
113
+ test_files: []