active_path 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +42 -8
- data/lib/active_path/configuration/attachment.rb +17 -0
- data/lib/active_path/configuration/partial.rb +5 -4
- data/lib/active_path/configuration/partials.rb +1 -1
- data/lib/active_path/renderer/conditional.rb +29 -0
- data/lib/active_path/renderer/partial_renderer.rb +37 -7
- data/lib/active_path/renderer/path_hints.rb +25 -0
- data/lib/active_path/version.rb +1 -1
- metadata +4 -2
- data/lib/active_path/path_hints.rb +0 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 212776f05fd76c2a04a29697b2136ac5b07b9ecf
|
4
|
+
data.tar.gz: 5638ceb2a84610ad98e0b5ffec0adc53c81a4b9c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4766f9ad22588546c2f819d0124d609f94c4d22c9bed2570379386ac181f6a503afec8b4126398986e887be3ec2cffd1153af111895fb368997da8180d40a192
|
7
|
+
data.tar.gz: ee97a9efdf88b22b8399b6f2b68dcdf9aa06164a7812dbcce1ab977c4e39663b493963a908e9caa84289e927aff5bafc922f81bd3fea5134d326bd9fefe6939e
|
data/README.md
CHANGED
@@ -33,34 +33,68 @@ ActivePath.configure do |config|
|
|
33
33
|
|
34
34
|
...
|
35
35
|
|
36
|
-
config.partials.prepend('
|
36
|
+
config.partials.prepend('pages/content').with('example/test')
|
37
37
|
|
38
38
|
end
|
39
39
|
|
40
40
|
```
|
41
41
|
|
42
|
-
The above example assumes your application renders a partial called '
|
42
|
+
The above example assumes your application renders a partial called 'pages/content' and you want to `prepend` a partial called 'example/test'.
|
43
43
|
|
44
|
-
Your partial will have the same local parameters access as the prepended partial.
|
44
|
+
Your partial will have the same local parameters access as the prepended partial. You can also use `append`:
|
45
45
|
|
46
|
+
```ruby
|
47
|
+
|
48
|
+
config.partials.append('pages/content').with('example/test')
|
49
|
+
|
50
|
+
```
|
51
|
+
|
52
|
+
|
53
|
+
With the above configuration 'example/test' will be rendered after 'pages/content'.
|
46
54
|
|
47
55
|
--
|
48
56
|
|
49
|
-
**
|
57
|
+
**Conditions:**
|
58
|
+
|
59
|
+
The `when` condition gives you the ability to conditionally apply your partial.
|
50
60
|
|
51
61
|
|
52
62
|
```ruby
|
53
63
|
|
54
|
-
|
64
|
+
config.partials.append('pages/content').with('example/test').when(title: 'Welcome')
|
55
65
|
|
56
|
-
|
66
|
+
```
|
67
|
+
|
68
|
+
Or pass in a block for more flexibility with your conditions:
|
57
69
|
|
58
|
-
|
70
|
+
```ruby
|
59
71
|
|
72
|
+
config.partials.append('pages/content').with('example/test').when do |locals|
|
73
|
+
locals[:title] == 'Welcome'
|
60
74
|
end
|
61
75
|
|
76
|
+
```
|
77
|
+
|
78
|
+
--
|
79
|
+
|
80
|
+
**Path hints:** (use in development only)
|
81
|
+
|
82
|
+
|
83
|
+
```ruby
|
84
|
+
|
85
|
+
config.path_hints = true
|
86
|
+
|
62
87
|
```
|
63
88
|
|
64
|
-
HTML comments are added before and after each partial.
|
89
|
+
HTML comments are added before and after each partial. Here is the output from our above examples:
|
90
|
+
|
91
|
+
```html
|
92
|
+
<!-- start pages/content -->
|
93
|
+
<p>page content</p>
|
94
|
+
<!-- end pages/content -->
|
95
|
+
<!-- start example/test -->
|
96
|
+
<p>example test</p>
|
97
|
+
<!-- end example/test -->
|
65
98
|
|
99
|
+
```
|
66
100
|
Feel free to [submit issues](https://github.com/ryaan-anthony/active-path/issues) or [help make it better](https://github.com/ryaan-anthony/active-path/pulls).
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module ActivePath
|
2
|
+
module Configuration
|
3
|
+
class Attachment
|
4
|
+
attr_reader :partial, :conditions
|
5
|
+
|
6
|
+
def initialize(partial)
|
7
|
+
@partial = partial
|
8
|
+
@conditions = []
|
9
|
+
end
|
10
|
+
|
11
|
+
def when(condition = nil, &block)
|
12
|
+
@conditions << (condition || block)
|
13
|
+
self
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -1,15 +1,16 @@
|
|
1
|
+
require 'active_path/configuration/attachment'
|
1
2
|
module ActivePath
|
2
3
|
module Configuration
|
3
4
|
class Partial
|
4
5
|
attr_reader :attachments
|
5
6
|
|
6
|
-
def initialize
|
7
|
-
@key = key
|
7
|
+
def initialize
|
8
8
|
@attachments = []
|
9
9
|
end
|
10
10
|
|
11
|
-
def with(
|
12
|
-
@attachments <<
|
11
|
+
def with(partial_name)
|
12
|
+
@attachments << Attachment.new(partial_name)
|
13
|
+
@attachments.last
|
13
14
|
end
|
14
15
|
end
|
15
16
|
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module ActivePath
|
2
|
+
module Renderer
|
3
|
+
module Conditional
|
4
|
+
def conditions_match?(conditions)
|
5
|
+
conditions.all? do |condition|
|
6
|
+
condition_match?(condition)
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
def condition_match?(condition)
|
11
|
+
if condition.is_a?(Hash)
|
12
|
+
hash_match?(condition)
|
13
|
+
elsif condition.is_a?(Proc)
|
14
|
+
proc_match?(condition)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def hash_match?(condition)
|
19
|
+
condition.all? do |k,v|
|
20
|
+
options[:locals][k] == v
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def proc_match?(condition)
|
25
|
+
condition.call(options[:locals])
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -1,7 +1,11 @@
|
|
1
|
+
require 'active_path/renderer/conditional'
|
2
|
+
require 'active_path/renderer/path_hints'
|
1
3
|
module ActivePath
|
2
4
|
module Renderer
|
3
5
|
class PartialRenderer
|
4
6
|
attr_reader :context, :options
|
7
|
+
include Conditional
|
8
|
+
include PathHints
|
5
9
|
|
6
10
|
def initialize(context, options = {})
|
7
11
|
@context = context
|
@@ -10,16 +14,42 @@ module ActivePath
|
|
10
14
|
|
11
15
|
def render
|
12
16
|
output_buffer = yield
|
13
|
-
|
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|
|
14
28
|
output_buffer.prepend(render_attachment(attachment))
|
15
29
|
end
|
16
|
-
|
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|
|
17
36
|
output_buffer.concat(render_attachment(attachment))
|
18
37
|
end
|
19
|
-
output_buffer
|
20
38
|
end
|
21
39
|
|
22
|
-
|
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
|
23
53
|
|
24
54
|
def partials
|
25
55
|
ActivePath.config.partials
|
@@ -30,12 +60,12 @@ module ActivePath
|
|
30
60
|
end
|
31
61
|
|
32
62
|
def render_attachment(attachment)
|
33
|
-
options_hash = attachment_options(attachment)
|
63
|
+
options_hash = attachment_options(attachment.partial)
|
34
64
|
context.render(options_hash)
|
35
65
|
end
|
36
66
|
|
37
|
-
def attachment_options(
|
38
|
-
hash = { partial:
|
67
|
+
def attachment_options(partial_name)
|
68
|
+
hash = { partial: partial_name }
|
39
69
|
options.is_a?(Hash) ? options.merge(hash) : hash
|
40
70
|
end
|
41
71
|
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
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)
|
10
|
+
end
|
11
|
+
|
12
|
+
private
|
13
|
+
|
14
|
+
def with_local_variables
|
15
|
+
if options.is_a?(Hash)
|
16
|
+
variables = options[:locals].map do |k,v|
|
17
|
+
count = (" (#{v.count})" if v.is_a?(Array)).to_s
|
18
|
+
"#{k} => #{v.class}#{count}"
|
19
|
+
end
|
20
|
+
"\nwith local variables: #{JSON.pretty_generate(variables)}"
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
data/lib/active_path/version.rb
CHANGED
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.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ryan Tulino
|
@@ -36,12 +36,14 @@ files:
|
|
36
36
|
- active_path.gemspec
|
37
37
|
- lib/active_path.rb
|
38
38
|
- lib/active_path/configuration.rb
|
39
|
+
- lib/active_path/configuration/attachment.rb
|
39
40
|
- lib/active_path/configuration/partial.rb
|
40
41
|
- lib/active_path/configuration/partials.rb
|
41
42
|
- lib/active_path/engine.rb
|
42
43
|
- lib/active_path/helpers/rendering_helper.rb
|
43
|
-
- lib/active_path/
|
44
|
+
- lib/active_path/renderer/conditional.rb
|
44
45
|
- lib/active_path/renderer/partial_renderer.rb
|
46
|
+
- lib/active_path/renderer/path_hints.rb
|
45
47
|
- lib/active_path/version.rb
|
46
48
|
homepage: http://rubygems.org/gems/active_path
|
47
49
|
licenses: []
|