nestive-rails 1.0.1 → 1.0.2
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 +4 -4
- data/CHANGELOG.md +13 -14
- data/LICENSE +42 -21
- data/README.md +311 -317
- data/lib/nestive-rails.rb +9 -9
- data/lib/nestive-rails/layout_helper.rb +260 -245
- data/lib/nestive-rails/railtie.rb +13 -13
- data/lib/nestive-rails/version.rb +5 -5
- metadata +19 -6
data/lib/nestive-rails.rb
CHANGED
@@ -1,9 +1,9 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
end
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'nestive-rails/version'
|
4
|
+
|
5
|
+
module NestiveRails
|
6
|
+
autoload :LayoutHelper, 'nestive-rails/layout_helper'
|
7
|
+
|
8
|
+
require 'nestive-rails/railtie'
|
9
|
+
end
|
@@ -1,245 +1,260 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module NestiveRails
|
4
|
+
# The Nestive LayoutHelper provides a handful of helper methods for use in
|
5
|
+
# your layouts and views.
|
6
|
+
#
|
7
|
+
# See the documentation for each individual method for detailed information,
|
8
|
+
# but at a high level, your parent layouts define `area`s of content. You can
|
9
|
+
# define an area and optionally add content to it at the same time using
|
10
|
+
# either a String, or a block:
|
11
|
+
#
|
12
|
+
# # app/views/layouts/global.html.erb
|
13
|
+
# <html>
|
14
|
+
# <head>
|
15
|
+
# <title><%= area :title, "MySite.com" %></title>
|
16
|
+
# </head>
|
17
|
+
# <body>
|
18
|
+
# <div id="content">
|
19
|
+
# <%= area :content %>
|
20
|
+
# </div>
|
21
|
+
# <div id="sidebar">
|
22
|
+
# <%= area :sidebar do %>
|
23
|
+
# <h2>About MySite.com</h2>
|
24
|
+
# <p>...</p>
|
25
|
+
# <% end %>
|
26
|
+
# </div>
|
27
|
+
# </body>
|
28
|
+
# </html>
|
29
|
+
#
|
30
|
+
# Your child layouts (or views) inherit and modify the parent by wrapping in
|
31
|
+
# an `extends` block helper. You can then either `append`, `prepend` or
|
32
|
+
# `replace` the content that has previously been assigned to each area by
|
33
|
+
# parent layouts.
|
34
|
+
#
|
35
|
+
# The `append`, `prepend` or `replace` helpers are *similar* to Rails' own
|
36
|
+
# `content_for`, which accepts content for the named area with either a String
|
37
|
+
# or with a block). They're different to `content_for` because they're only
|
38
|
+
# used modify the content assigned to the area, not retrieve it:
|
39
|
+
#
|
40
|
+
# # app/views/layouts/admin.html.erb
|
41
|
+
# <%= extends :global do %>
|
42
|
+
# <% prepend :title, "Admin :: " %>
|
43
|
+
# <% replace :sidebar do %>
|
44
|
+
# <h2>Quick Links</h2>
|
45
|
+
# <ul>
|
46
|
+
# <li>...</li>
|
47
|
+
# </ul>
|
48
|
+
# <% end %>
|
49
|
+
# <% end %>
|
50
|
+
#
|
51
|
+
# # app/views/admin/posts/index.html.erb
|
52
|
+
# <%= extends :admin do %>
|
53
|
+
# <% prepend :title, "Posts ::" %>
|
54
|
+
# <% replace :content do %>
|
55
|
+
# Normal view stuff goes here.
|
56
|
+
# <% end %>
|
57
|
+
# <% end %>
|
58
|
+
module LayoutHelper
|
59
|
+
# Declares that the current layour (or view) is inheriting from and
|
60
|
+
# extending another layout.
|
61
|
+
#
|
62
|
+
# @param [String] layout
|
63
|
+
# The base name of the file in `layouts/` that you wish to extend (eg
|
64
|
+
# `application` for `layouts/application.html.erb`)
|
65
|
+
#
|
66
|
+
# @example Extending the `application` layout to create an `admin` layout
|
67
|
+
#
|
68
|
+
# # app/views/layouts/admin.html.erb
|
69
|
+
# <%= extends :application do %>
|
70
|
+
# ...
|
71
|
+
# <% end %>
|
72
|
+
#
|
73
|
+
# @example Extending the `admin` layout in a view (you'll need to render the
|
74
|
+
# view with `layout: nil`)
|
75
|
+
#
|
76
|
+
# # app/controllers/admin/posts_controller.rb
|
77
|
+
# class Admin::PostsController < ApplicationController
|
78
|
+
# # You can disable Rails' layout rendering for all actions
|
79
|
+
# layout nil
|
80
|
+
#
|
81
|
+
# # Or disable Rails' layout rendering per-controller
|
82
|
+
# def index
|
83
|
+
# render layout: nil
|
84
|
+
# end
|
85
|
+
# end
|
86
|
+
#
|
87
|
+
# # app/views/admin/posts/index.html.erb
|
88
|
+
# <%= extends :admin do %>
|
89
|
+
# ...
|
90
|
+
# <% end %>
|
91
|
+
def extends(layout, &block)
|
92
|
+
# Make sure it's a string
|
93
|
+
layout = layout.to_s
|
94
|
+
|
95
|
+
# If there's no directory component, presume a plain layout name
|
96
|
+
layout = "layouts/#{layout}" unless layout.include?('/')
|
97
|
+
|
98
|
+
# Capture the content to be placed inside the extended layout
|
99
|
+
@view_flow.get(:layout).replace capture(&block).to_s
|
100
|
+
|
101
|
+
render file: layout
|
102
|
+
end
|
103
|
+
|
104
|
+
# Defines an area of content in your layout that can be modified or replaced
|
105
|
+
# by child layouts that extend it. You can optionally add content to an area
|
106
|
+
# using either a String, or a block.
|
107
|
+
#
|
108
|
+
# Areas are declared in a parent layout and modified by a child layout, but
|
109
|
+
# since Nestive allows for multiple levels of inheritance, a child layout
|
110
|
+
# can also declare an area for it's children to modify.
|
111
|
+
#
|
112
|
+
# @example Define an area without adding content to it:
|
113
|
+
# <%= area :sidebar %>
|
114
|
+
#
|
115
|
+
# @example Define an area and add a String of content to it:
|
116
|
+
# <%= area :sidebar, "Some content." %>
|
117
|
+
#
|
118
|
+
# @example Define an area and add content to it with a block:
|
119
|
+
# <%= area :sidebar do %>
|
120
|
+
# Some content.
|
121
|
+
# <% end %>
|
122
|
+
#
|
123
|
+
# @example Define an area in a child layout:
|
124
|
+
# <%= extends :global do %>
|
125
|
+
# <%= area :sidebar do %>
|
126
|
+
# Some content.
|
127
|
+
# <% end %>
|
128
|
+
# <% end %>
|
129
|
+
#
|
130
|
+
# @param [Symbol] name
|
131
|
+
# A unique name to identify this area of content.
|
132
|
+
#
|
133
|
+
# @param [String] content
|
134
|
+
# An optional String of content to add to the area as you declare it.
|
135
|
+
def area(name, content = nil, &block)
|
136
|
+
content = capture(&block) if block_given?
|
137
|
+
append name, content
|
138
|
+
render_area name
|
139
|
+
end
|
140
|
+
|
141
|
+
# Appends content to an area previously defined or modified in parent
|
142
|
+
# layout(s). You can provide the content using either a String, or a block.
|
143
|
+
#
|
144
|
+
# @example Appending content with a String
|
145
|
+
# <% append :sidebar, "Some content." %>
|
146
|
+
#
|
147
|
+
# @example Appending content with a block:
|
148
|
+
# <% append :sidebar do %>
|
149
|
+
# Some content.
|
150
|
+
# <% end %>
|
151
|
+
#
|
152
|
+
# @param [Symbol] name
|
153
|
+
# A name to identify the area of content you wish to append to
|
154
|
+
#
|
155
|
+
# @param [String] content
|
156
|
+
# Optionally provide a String of content, instead of a block. A block will
|
157
|
+
# take precedence.
|
158
|
+
def append(name, content = nil, &block)
|
159
|
+
content = capture(&block) if block_given?
|
160
|
+
add_instruction_to_area name, :push, content
|
161
|
+
end
|
162
|
+
|
163
|
+
# Prepends content to an area previously declared or modified in parent
|
164
|
+
# layout(s). You can provide the content using either a String, or a block.
|
165
|
+
#
|
166
|
+
# @example Prepending content with a String
|
167
|
+
# <% prepend :sidebar, "Some content." %>
|
168
|
+
#
|
169
|
+
# @example Prepending content with a block:
|
170
|
+
# <% prepend :sidebar do %>
|
171
|
+
# Some content.
|
172
|
+
# <% end %>
|
173
|
+
#
|
174
|
+
# @param [Symbol] name
|
175
|
+
# A name to identify the area of content you wish to prepend to
|
176
|
+
#
|
177
|
+
# @param [String] content
|
178
|
+
# Optionally provide a String of content, instead of a block. A block will
|
179
|
+
# take precedence.
|
180
|
+
def prepend(name, content = nil, &block)
|
181
|
+
content = capture(&block) if block_given?
|
182
|
+
add_instruction_to_area name, :unshift, content
|
183
|
+
end
|
184
|
+
|
185
|
+
# Replaces the content of an area previously declared or modified in parent
|
186
|
+
# layout(s). You can provide the content using either a String, or a block.
|
187
|
+
#
|
188
|
+
# @example Replacing content with a String
|
189
|
+
# <% replace :sidebar, "New content." %>
|
190
|
+
#
|
191
|
+
# @example Replacing content with a block:
|
192
|
+
# <% replace :sidebar do %>
|
193
|
+
# New content.
|
194
|
+
# <% end %>
|
195
|
+
#
|
196
|
+
# @param [Symbol] name
|
197
|
+
# A name to identify the area of content you wish to replace
|
198
|
+
#
|
199
|
+
# @param [String] content
|
200
|
+
# Optionally provide a String of content, instead of a block. A block will
|
201
|
+
# take precedence.
|
202
|
+
def replace(name, content = nil, &block)
|
203
|
+
content = capture(&block) if block_given?
|
204
|
+
add_instruction_to_area name, :replace, [content]
|
205
|
+
end
|
206
|
+
|
207
|
+
# Purge the content of an area previously declared or modified in parent
|
208
|
+
# layout(s).
|
209
|
+
#
|
210
|
+
# @example Purge content
|
211
|
+
# <% purge :sidebar %>
|
212
|
+
#
|
213
|
+
# @param names
|
214
|
+
# A list of area names to purge
|
215
|
+
def purge(*names)
|
216
|
+
names.each { |name| replace(name, nil) }
|
217
|
+
end
|
218
|
+
|
219
|
+
private
|
220
|
+
|
221
|
+
# We record the instructions (declaring, appending, prepending and
|
222
|
+
# replacing) for an area of content into an array that we can later retrieve
|
223
|
+
# and replay. Instructions are stored in an instance variable Hash
|
224
|
+
# `@_area_for`, with each key representing an area name, and each value an
|
225
|
+
# Array of instructions. Each instruction is a two element array containing
|
226
|
+
# a instruction method (eg `:push`, `:unshift`, `:replace`) and a value
|
227
|
+
# (content String).
|
228
|
+
#
|
229
|
+
# @_area_for[:sidebar] # => [ [:push,"World"], [:unshift,"Hello"] ]
|
230
|
+
#
|
231
|
+
# Due to the way we extend layouts (render the parent layout after the
|
232
|
+
# child), the instructions are captured in reverse order. `render_area`
|
233
|
+
# reversed them and plays them back at rendering time.
|
234
|
+
#
|
235
|
+
# @example
|
236
|
+
# add_instruction_to_area(:sidebar, :push, "More content.")
|
237
|
+
def add_instruction_to_area(name, instruction, value)
|
238
|
+
@_area_for ||= {}
|
239
|
+
@_area_for[name] ||= []
|
240
|
+
@_area_for[name] << [instruction, value]
|
241
|
+
nil
|
242
|
+
end
|
243
|
+
|
244
|
+
# Take the instructions we've gathered for the area and replay them one
|
245
|
+
# after the other on an empty array. These instructions will push, unshift
|
246
|
+
# or replace items into our output array, which we then join and mark as
|
247
|
+
# html_safe.
|
248
|
+
#
|
249
|
+
# These instructions are reversed and replayed when we render the block
|
250
|
+
# (rather than as they happen) due to the way they are gathered by the
|
251
|
+
# layout extension process (in reverse).
|
252
|
+
def render_area(name)
|
253
|
+
[].tap do |output|
|
254
|
+
@_area_for.fetch(name, []).reverse_each do |method_name, content|
|
255
|
+
output.public_send method_name, content
|
256
|
+
end
|
257
|
+
end.join.html_safe
|
258
|
+
end
|
259
|
+
end
|
260
|
+
end
|