active_path 0.0.3 → 0.0.4

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 212776f05fd76c2a04a29697b2136ac5b07b9ecf
4
- data.tar.gz: 5638ceb2a84610ad98e0b5ffec0adc53c81a4b9c
3
+ metadata.gz: 8794ff562056311cf10ac54cf375f96986a96de3
4
+ data.tar.gz: 176ef110a98d119a2f15329d39fdc631754bb87e
5
5
  SHA512:
6
- metadata.gz: 4766f9ad22588546c2f819d0124d609f94c4d22c9bed2570379386ac181f6a503afec8b4126398986e887be3ec2cffd1153af111895fb368997da8180d40a192
7
- data.tar.gz: ee97a9efdf88b22b8399b6f2b68dcdf9aa06164a7812dbcce1ab977c4e39663b493963a908e9caa84289e927aff5bafc922f81bd3fea5134d326bd9fefe6939e
6
+ metadata.gz: da11c5dddf9a1ab9abec7da09634a2e4c9eb066e9b7e2068ae2f749249210f6008c096daa7368fa147bd02504f5fab6c653aa19535ace0ecebf72d5984bb2774
7
+ data.tar.gz: e362383364329d229a2844f317f433daa3ebd6dd1dcbdeedcd89689dafee359c8715f35d51b75731ad185c591fad478beef18a0bea2b301f9928c63beb178ddc
data/README.md CHANGED
@@ -50,18 +50,23 @@ config.partials.append('pages/content').with('example/test')
50
50
  ```
51
51
 
52
52
 
53
- With the above configuration 'example/test' will be rendered after 'pages/content'.
53
+ The above renders 'example/test' after 'pages/content'.
54
54
 
55
55
  --
56
56
 
57
57
  **Conditions:**
58
58
 
59
- The `when` condition gives you the ability to conditionally apply your partial.
59
+ The `when` method allows you to conditionally inject your view. Consider this partial:
60
60
 
61
+ ```ruby
62
+ render partial: 'pages/content', locals: { page_id: 9 }
63
+ ```
64
+
65
+ You can pass in a hash which must match the local variables.
61
66
 
62
67
  ```ruby
63
68
 
64
- config.partials.append('pages/content').with('example/test').when(title: 'Welcome')
69
+ config.partials.append('pages/content').with('example/test').when(page_id: 9)
65
70
 
66
71
  ```
67
72
 
@@ -70,7 +75,7 @@ Or pass in a block for more flexibility with your conditions:
70
75
  ```ruby
71
76
 
72
77
  config.partials.append('pages/content').with('example/test').when do |locals|
73
- locals[:title] == 'Welcome'
78
+ locals[:page_id] == 9
74
79
  end
75
80
 
76
81
  ```
@@ -93,7 +98,7 @@ HTML comments are added before and after each partial. Here is the output from o
93
98
  <p>page content</p>
94
99
  <!-- end pages/content -->
95
100
  <!-- start example/test -->
96
- <p>example test</p>
101
+ <p>example content</p>
97
102
  <!-- end example/test -->
98
103
 
99
104
  ```
@@ -1,5 +1,5 @@
1
1
  module ActivePath
2
- module Renderer
2
+ module Conditions
3
3
  module Conditional
4
4
  def conditions_match?(conditions)
5
5
  conditions.all? do |condition|
@@ -1,3 +1,5 @@
1
+ require 'active_path/subscribers/view_injection'
2
+ require 'active_path/subscribers/path_hints'
1
3
  require 'active_path/helpers/rendering_helper'
2
4
  module ActivePath
3
5
  class Engine < Rails::Engine
@@ -12,5 +14,12 @@ module ActivePath
12
14
  _helpers.module_eval { include ActivePath::Helpers::RenderingHelper }
13
15
  end
14
16
  end
17
+
18
+ config.after_initialize do
19
+ if ActivePath.config.path_hints
20
+ ActiveSupport::Notifications.subscribe(:render_partial, Subscribers::PathHints.new)
21
+ end
22
+ ActiveSupport::Notifications.subscribe(:render_partial, Subscribers::ViewInjection.new)
23
+ end
15
24
  end
16
25
  end
@@ -1,13 +1,13 @@
1
- require 'active_path/renderer/partial_renderer'
2
1
  module ActivePath
3
2
  module Helpers
4
3
  module RenderingHelper
5
4
  extend ActionView::Helpers::RenderingHelper
6
- # Delegate rendering to the activepath renderer
5
+ # Notify subscribers that a partial has rendered
7
6
  def render(options = {}, locals = {}, &block)
8
- Renderer::PartialRenderer.new(self, options).render do
9
- super
10
- end
7
+ buffer = super
8
+ opts = { buffer: buffer, context: self, options: options }
9
+ ActiveSupport::Notifications.instrument(:render_partial, opts)
10
+ buffer
11
11
  end
12
12
  end
13
13
  end
@@ -1,12 +1,10 @@
1
+ require 'active_path/subscribers/subscriber'
1
2
  module ActivePath
2
- module Renderer
3
- module PathHints
4
- def start_path_hints(output_buffer)
5
- output_buffer.prepend("<!-- start #{partial_name}#{with_local_variables.to_s} --!>".html_safe)
6
- end
7
-
8
- def end_path_hints(output_buffer)
9
- output_buffer.concat("<!-- end #{partial_name} --!>".html_safe)
3
+ module Subscribers
4
+ class PathHints < Subscriber
5
+ def perform
6
+ buffer.prepend("<!-- start #{partial}#{with_local_variables.to_s} --!>".html_safe)
7
+ buffer.concat("<!-- end #{partial} --!>".html_safe)
10
8
  end
11
9
 
12
10
  private
@@ -0,0 +1,22 @@
1
+ module ActivePath
2
+ module Subscribers
3
+ class Subscriber
4
+ attr_reader :buffer, :context, :options
5
+
6
+ def call(name, started, finished, unique_id, payload)
7
+ @buffer = payload[:buffer]
8
+ @context = payload[:context]
9
+ @options = payload[:options]
10
+ perform
11
+ end
12
+
13
+ def partial
14
+ options.is_a?(Hash) ? options[:partial] : options.to_s
15
+ end
16
+
17
+ def perform
18
+ raise NotImplementedError
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,41 @@
1
+ require 'active_path/subscribers/subscriber'
2
+ require 'active_path/conditions/conditional'
3
+ module ActivePath
4
+ module Subscribers
5
+ class ViewInjection < Subscriber
6
+ include Conditions::Conditional
7
+
8
+ def perform
9
+ prepend_attachments.each do |attachment|
10
+ buffer.prepend(render_attachment(attachment))
11
+ end
12
+
13
+ append_attachments.each do |attachment|
14
+ buffer.concat(render_attachment(attachment))
15
+ end
16
+ end
17
+
18
+ private
19
+
20
+ def append_attachments
21
+ attachments(ActivePath.config.partials.append(partial))
22
+ end
23
+
24
+ def prepend_attachments
25
+ attachments(ActivePath.config.partials.prepend(partial))
26
+ end
27
+
28
+ def attachments(configuration)
29
+ configuration.attachments.select do |attachment|
30
+ conditions_match?(attachment.conditions)
31
+ end
32
+ end
33
+
34
+ def render_attachment(attachment)
35
+ hash = { partial: attachment.partial }
36
+ options_hash = options.is_a?(Hash) ? options.merge(hash) : hash
37
+ context.render(options_hash)
38
+ end
39
+ end
40
+ end
41
+ end
@@ -1,3 +1,3 @@
1
1
  module ActivePath
2
- VERSION = '0.0.3'
2
+ VERSION = '0.0.4'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_path
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryan Tulino
@@ -35,15 +35,16 @@ files:
35
35
  - Rakefile
36
36
  - active_path.gemspec
37
37
  - lib/active_path.rb
38
+ - lib/active_path/conditions/conditional.rb
38
39
  - lib/active_path/configuration.rb
39
40
  - lib/active_path/configuration/attachment.rb
40
41
  - lib/active_path/configuration/partial.rb
41
42
  - lib/active_path/configuration/partials.rb
42
43
  - lib/active_path/engine.rb
43
44
  - lib/active_path/helpers/rendering_helper.rb
44
- - lib/active_path/renderer/conditional.rb
45
- - lib/active_path/renderer/partial_renderer.rb
46
- - lib/active_path/renderer/path_hints.rb
45
+ - lib/active_path/subscribers/path_hints.rb
46
+ - lib/active_path/subscribers/subscriber.rb
47
+ - lib/active_path/subscribers/view_injection.rb
47
48
  - lib/active_path/version.rb
48
49
  homepage: http://rubygems.org/gems/active_path
49
50
  licenses: []
@@ -1,73 +0,0 @@
1
- require 'active_path/renderer/conditional'
2
- require 'active_path/renderer/path_hints'
3
- module ActivePath
4
- module Renderer
5
- class PartialRenderer
6
- attr_reader :context, :options
7
- include Conditional
8
- include PathHints
9
-
10
- def initialize(context, options = {})
11
- @context = context
12
- @options = options
13
- end
14
-
15
- def render
16
- output_buffer = yield
17
- prepend_to(output_buffer)
18
- append_to(output_buffer)
19
- output_buffer
20
- end
21
-
22
- private
23
-
24
- def prepend_to(output_buffer)
25
- start_path_hints(output_buffer) if ActivePath.config.path_hints
26
-
27
- prepend_attachments.each do |attachment|
28
- output_buffer.prepend(render_attachment(attachment))
29
- end
30
- end
31
-
32
- def append_to(output_buffer)
33
- end_path_hints(output_buffer) if ActivePath.config.path_hints
34
-
35
- append_attachments.each do |attachment|
36
- output_buffer.concat(render_attachment(attachment))
37
- end
38
- end
39
-
40
- def append_attachments
41
- attachments(partials.append(partial_name))
42
- end
43
-
44
- def prepend_attachments
45
- attachments(partials.prepend(partial_name))
46
- end
47
-
48
- def attachments(partial)
49
- partial.attachments.select do |attachment|
50
- conditions_match?(attachment.conditions)
51
- end
52
- end
53
-
54
- def partials
55
- ActivePath.config.partials
56
- end
57
-
58
- def partial_name
59
- options.is_a?(Hash) ? options[:partial] : options.to_s
60
- end
61
-
62
- def render_attachment(attachment)
63
- options_hash = attachment_options(attachment.partial)
64
- context.render(options_hash)
65
- end
66
-
67
- def attachment_options(partial_name)
68
- hash = { partial: partial_name }
69
- options.is_a?(Hash) ? options.merge(hash) : hash
70
- end
71
- end
72
- end
73
- end