poke_js 0.1.6 → 0.1.7

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.
@@ -18,42 +18,47 @@ POKE = {
18
18
  return $.extend(found_namespace, definition);
19
19
  },
20
20
 
21
- get_action_namespace: function(controller, format) {
22
- var levels = controller.split('/');
23
- levels.push(format);
24
-
25
- var current_namespace = APP;
21
+ traverse_namespace: function(namespace, levels) {
22
+ if (!$.isArray(levels)) levels = [levels.controller, levels.format, levels.action];
23
+ levels = POKE.formatted_levels(levels);
26
24
 
25
+ var current_level = namespace;
26
+ var level;
27
27
  for (var i=0;i<levels.length;i++) {
28
- var level = levels[i];
29
- if (POKE.blank(current_namespace[level]))
30
- return false;
31
- current_namespace = current_namespace[level];
28
+ level = levels[i];
29
+ current_level = current_level[level];
30
+ if (typeof current_level === "undefined") return undefined;
32
31
  }
33
- return current_namespace;
32
+ return current_level;
34
33
  },
35
-
36
- exec_all: function(controller, format, action) {
37
- POKE.exec("application", format);
38
- POKE.exec("application", format, action);
39
- POKE.exec(controller, format);
40
- POKE.exec(controller, format, action);
34
+ formatted_levels: function(levels) {
35
+ var formatted_levels = [];
36
+ var level;
37
+ for (var i=0;i<levels.length;i++) {
38
+ level = levels[i];
39
+ formatted_levels = formatted_levels.concat(level.split('/'));
40
+ }
41
+ return formatted_levels;
41
42
  },
42
- exec: function(controller, format, action) {
43
- var action_namespace = POKE.get_action_namespace(controller, format)
44
43
 
45
- if ($.type(action_namespace) === "object") {
46
- action = (action === undefined) ? "init" : action;
47
- var funct = action_namespace[action],
48
- params = action_namespace[action + "_params"];
44
+ create_namespace: function(namespace) {
45
+ var current_level = window;
46
+ $.each(namespace.split("."), function(index, level) {
47
+ if (!$.isPlainObject(current_level[level])) current_level[level] = {};
48
+ current_level = current_level[level];
49
+ });
50
+ },
49
51
 
50
- if ($.isFunction(funct))
51
- funct(params);
52
- }
52
+ exec_all: function(params) {
53
+ POKE.exec("application", params.format, "before", params);
54
+ POKE.exec(params.controller, params.format, "before", params);
55
+ POKE.exec(params.controller, params.format, params.action, params);
56
+ POKE.exec(params.controller, params.format, "after", params);
57
+ POKE.exec("application", params.format, "after", params);
53
58
  },
54
- init: function() {
55
- var $body = $('body');
56
- POKE.exec_all(POKE.controller, "html", POKE.action);
59
+ exec: function(controller, format, action, params) {
60
+ var action_namespace = APP.traverse_namespace([controller, format, action]);
61
+ if ($.isFunction(action_namespace)) action_namespace(params);
57
62
  }
58
63
  };
59
64
 
@@ -67,8 +72,9 @@ if (POKE.blank(window["APP"])) {
67
72
  },
68
73
  define: function(namespace_string, definition) {
69
74
  return POKE.define(APP.namespace_string(namespace_string), definition);
75
+ },
76
+ traverse_namespace: function(levels) {
77
+ return POKE.traverse_namespace(APP, levels);
70
78
  }
71
79
  };
72
- }
73
- $(POKE.init);
74
- $(document).on('page:change', POKE.init);
80
+ }
@@ -1,6 +1,5 @@
1
1
  (function() {
2
- <% controller, action = poke_js_template %>
3
- POKE.define('POKE', { controller: "<%=controller%>", action: "<%=action%>", method: "<%=request.method%>", path: "<%=request.env['PATH_INFO']%>" });
4
- APP.<%= controller.gsub("/", ".") %>.js.<%= action %>_params = <%= yield %>;
5
- POKE.exec_all('<%= poke_js_template[0] %>', 'js', '<%= poke_js_template[1] %>');
2
+ POKE.create_namespace('<%= poke_js_params %>');
3
+ <%= poke_js_params %> = <%= define_poke_js_params { yield } %>;
4
+ POKE.exec_all(<%= poke_js_params %>);
6
5
  })();
@@ -1,4 +1,5 @@
1
1
  require "poke_js/view_helpers"
2
+ require "poke_js/controller"
2
3
 
3
4
  module PokeJs
4
5
  if defined?(Rails) && defined?(Rails::Engine)
@@ -0,0 +1,38 @@
1
+ module PokeJs
2
+ module Controller
3
+ extend ActiveSupport::Concern
4
+ included do
5
+ helper_method :poke_js_template
6
+ helper_method :extract_template
7
+ end
8
+
9
+ def poke_js_template
10
+ extract_template(@poke_js_template)
11
+ end
12
+
13
+ protected
14
+ def extract_template(template)
15
+ extracted_template = [controller_path, action_name]
16
+ if template
17
+ array = if template.class == Array
18
+ template
19
+ else
20
+ split = template.to_s.split('/')
21
+ template_controller = split[0..-2]
22
+ unless template_controller.empty?
23
+ template_controller = [template_controller.join('/')]
24
+ end
25
+ template_controller + [split.last]
26
+ end
27
+ if array.size == 1
28
+ extracted_template[1] = array.first
29
+ else
30
+ extracted_template = array
31
+ end
32
+ end
33
+ extracted_template
34
+ end
35
+ end
36
+ end
37
+
38
+ ::ActionController::Base.send :include, PokeJs::Controller
@@ -1,3 +1,3 @@
1
1
  module PokeJs
2
- VERSION = "0.1.6"
2
+ VERSION = "0.1.7"
3
3
  end
@@ -9,15 +9,37 @@ module PokeJs
9
9
  result
10
10
  end
11
11
 
12
- def poke_js_template
13
- extract_template(@poke_js_template)
14
- end
15
-
16
12
  def with_template(template)
17
13
  old_template = @poke_js_template
18
14
  @poke_js_template = template
19
- yield
15
+ result = yield
20
16
  @poke_js_template = old_template
17
+ result
18
+ end
19
+
20
+ def poke_js_params(template=@poke_js_template)
21
+ controller, action = extract_template(template)
22
+ raw "APP.#{controller.gsub("/", ".")}.#{request.format.symbol}.#{action}_params"
23
+ end
24
+
25
+ def define_poke_js_params(template=@poke_js_template)
26
+ controller, action = extract_template(template)
27
+
28
+ params = { controller: controller, action: action, method: request.method, path: request.env['PATH_INFO'], format: request.format.symbol }
29
+ if self.respond_to? :add_params
30
+ params.reverse_merge! add_params
31
+ end
32
+ javascript = params.to_json
33
+ if block_given?
34
+ if yield
35
+ javascript = "$.extend(#{yield}, #{javascript});"
36
+ end
37
+ end
38
+ raw javascript
39
+ end
40
+
41
+ def head_poke
42
+ raw "#{source_poke}\n#{poke}"
21
43
  end
22
44
 
23
45
  def poke(template=@poke_js_template, format=formats.first)
@@ -25,18 +47,34 @@ module PokeJs
25
47
  poke_lambda = -> do
26
48
  if format == :html
27
49
  with_format(:js) do
28
- if lookup_context.template_exists? "#{controller}/#{action}_params"
29
- #defined in application.js.erb
30
- poke_string = %Q/POKE.define('POKE', { controller: "#{controller}", action: "#{action}", method: "#{request.method}", path: "#{request.env['PATH_INFO']}" });/
31
- javascript_tag do
32
- raw %Q/APP.#{controller.gsub("/", ".")}.html.#{action}_params = #{ render :template => "#{controller}/#{action}_params" };#{poke_string}/
33
- end
50
+ javascript_tag do
51
+ raw %Q/
52
+ POKE.create_namespace('#{poke_js_params}');
53
+ #{poke_js_params} = #{ define_poke_js_params do
54
+ if lookup_context.template_exists? "#{controller}/#{action}_params"
55
+ render(template: "#{controller}/#{action}_params")
56
+ end
57
+ end };
58
+ #{
59
+ if @source_poke
60
+ %Q/$(function() { POKE.exec_all(#{poke_js_params}); });/
61
+ else
62
+ %Q/
63
+ POKE.define('POKE', {
64
+ params: #{poke_js_params},
65
+ init: function() { POKE.exec_all(POKE.params); },
66
+ });
67
+ $(POKE.init);
68
+ /
69
+ end
70
+ }
71
+ /
34
72
  end
35
73
  end
36
74
  elsif format == :js
37
75
  content_for :head do
38
76
  javascript_tag do
39
- "$(function(){#{render :template => "#{controller}/#{action}", :formats => [:js], :layout => "layouts/application"}});".html_safe
77
+ raw "$(function(){#{render template: "#{controller}/#{action}", formats: [:js], layout: "layouts/application"}});"
40
78
  end
41
79
  end
42
80
  end
@@ -49,27 +87,14 @@ module PokeJs
49
87
  end
50
88
  end
51
89
 
52
- private
53
- def extract_template(template)
54
- extracted_template = [controller_path, action_name]
55
- if template
56
- array = if template.class == Array
57
- template
58
- else
59
- split = template.to_s.split('/')
60
- template_controller = split[0..-2]
61
- unless template_controller.empty?
62
- template_controller = [template_controller.join('/')]
63
- end
64
- template_controller + [split.last]
65
- end
66
- if array.size == 1
67
- extracted_template[1] = array.first
68
- else
69
- extracted_template = array
70
- end
90
+ def source_poke
91
+ source_template = flash[:source_template]
92
+ if source_template
93
+ @source_poke = true
94
+ result = poke source_template
95
+ @source_poke = false
96
+ result
71
97
  end
72
- extracted_template
73
98
  end
74
99
  end
75
100
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: poke_js
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.1.7
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-03-01 00:00:00.000000000 Z
12
+ date: 2013-03-10 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -59,6 +59,7 @@ files:
59
59
  - app/assets/javascripts/poke_js.js
60
60
  - app/views/layouts/application.js.erb
61
61
  - lib/poke_js.rb
62
+ - lib/poke_js/controller.rb
62
63
  - lib/poke_js/version.rb
63
64
  - lib/poke_js/view_helpers.rb
64
65
  - poke_js.gemspec