blocks 0.10 → 0.11
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +122 -0
- data/lib/blocks/builder.rb +2 -0
- metadata +7 -7
- data/README +0 -1
data/README.rdoc
ADDED
@@ -0,0 +1,122 @@
|
|
1
|
+
= Blocks
|
2
|
+
|
3
|
+
Wiki[http://wiki.github.com/hunterae/blocks]
|
4
|
+
|
5
|
+
Blocks is a replacement / complement to content_for with yield. It allows a user to specify a block capable of taking parameters that may be passed in when using that block. A user may also specify other blocks to be prepended or appended before or after a specific block is rendered. A template may also be specified to blocks that will provide the layout for a specific component and provide default implementations for its blocks. In this way, blocks is able to offer a very simple to use table generator (table_for) and list generator (list_for).
|
6
|
+
|
7
|
+
== Installation
|
8
|
+
|
9
|
+
In <b>Rails 3</b>, add this to your Gemfile.
|
10
|
+
|
11
|
+
gem "blocks"
|
12
|
+
|
13
|
+
In <b>Rails 2</b>, add this to your environment.rb file. (At this time, it is untested in Rails 2)
|
14
|
+
|
15
|
+
config.gem "blocks"
|
16
|
+
|
17
|
+
Alternatively, you can install it as a plugin.
|
18
|
+
|
19
|
+
rails plugin install git://github.com/hunterae/blocks.git
|
20
|
+
|
21
|
+
|
22
|
+
== Getting Started
|
23
|
+
|
24
|
+
At the very simplest level, blocks may be used in much the same way that content_for and yield are used:
|
25
|
+
|
26
|
+
To define a block:
|
27
|
+
|
28
|
+
<%= blocks.define :some_block_name, :some_parameter => 1, :some_parameter2 => 2 do |options| %>
|
29
|
+
<%= options[:some_parameter] %>
|
30
|
+
<%= options[:some_parameter2] %>
|
31
|
+
<%= options[:some_parameter3] %>
|
32
|
+
<% end %>
|
33
|
+
|
34
|
+
Here, we are specifying two parameters that are provided automatically to the block named "some_block_name". They are "some_parameter" and "some_parameter2". In addition, the definition of the block is assuming that the user will be passing in another parameter called "some_parameter3". However, all parameters are passed in in a hash, no error will occur if the user fails to specify "some_parameter3".
|
35
|
+
|
36
|
+
Now, we may use the above block like follows:
|
37
|
+
|
38
|
+
<%= blocks.use :some_block_name %>
|
39
|
+
|
40
|
+
Here, we will see the output "1 2". But if we pass in "some_parameter3", as follows:
|
41
|
+
|
42
|
+
<%= blocks.use :some_block_name, :some_parameter3 => 3 %>
|
43
|
+
|
44
|
+
Then we will see "1 2 3". Additionally, we can override any of the previous parameters when using a block simply by passing in the new value as a parameter:
|
45
|
+
|
46
|
+
<%= blocks.use :some_block_name, :some_parameter2 => "overridden", :some_parameter3 => 3 %>
|
47
|
+
|
48
|
+
In this case, we will see "1 overridden 3". Thus, we now have content_for but with parameters.
|
49
|
+
|
50
|
+
== Providing a Default Implementation of a Block
|
51
|
+
|
52
|
+
We can easily provide a default implementation of a block that will be used wherever it is specified unless that block has been specified elsewhere, in which case that version of the block will be rendered in it place. A perfect example of this would be by placing a default implementation of a block inside of a layout, and optionally specifying a different implementation to be rendered inside of your view. For example, inside one of my layouts (such as "views/layouts/application.html.erb"), I may provide my default page title:
|
53
|
+
|
54
|
+
<title>
|
55
|
+
<%= blocks.use :title do %>
|
56
|
+
My default page title
|
57
|
+
<% end %>
|
58
|
+
</title>
|
59
|
+
|
60
|
+
Then, inside of a specific layout (such as "views/home/index.html.erb"), I may provide a specific page title for that particular view:
|
61
|
+
|
62
|
+
<%= blocks.define :title do %>
|
63
|
+
A different page title
|
64
|
+
<% end %>
|
65
|
+
|
66
|
+
When the home index page is rendered, it's page title will be "A different page title" (assuming the HomeController uses the default application layout), but any other page that uses the default application layout will show "My default page title" as its page title. This occurs since the view is parsed first, so the definition of the block named "title" will already exist by the time the layout is rendered. However, for any page that does not specify a definition of the block "title", the default implementation provided by the layout (using the command "blocks.use") will be used.
|
67
|
+
|
68
|
+
What would happen, though, if no default implementation were provided? In other words, what if the code in the layout looked like this:
|
69
|
+
|
70
|
+
<title>
|
71
|
+
<%= blocks.use :title %>
|
72
|
+
</title>
|
73
|
+
|
74
|
+
Well, in the case where the block if defined in the view (as was the case with "views/home/index.html.erb"), a title will be provided for the page. However, for any other page, since it can't find any implementation of the named block, it will simply return nothing, and you will have a page with no title.
|
75
|
+
|
76
|
+
== Using partials
|
77
|
+
|
78
|
+
It's not completely accurate to say that if no block definition is found (as was the case above), "blocks.use :title" will simply return nothing. It will first try and render a partial with that name. By default, it will look for a partial named "_title.html.erb" in the "view/blocks" directory (or "_title.html.haml" if you're using haml). It will attempt to render that partial with all the options passed in as local variables to the partial.
|
79
|
+
|
80
|
+
== Before and After Blocks
|
81
|
+
|
82
|
+
Blocks provides a very convenient way to specify blocks that are to be rendered before a specific block or after a specific block. For example:
|
83
|
+
|
84
|
+
<%= blocks.define :some_block do %>
|
85
|
+
Some Block
|
86
|
+
<% end %>
|
87
|
+
|
88
|
+
<%= blocks.before :some_block do %>
|
89
|
+
Before 1
|
90
|
+
<% end %>
|
91
|
+
|
92
|
+
<%= blocks.prepend :some_block do %>
|
93
|
+
Before 2
|
94
|
+
<% end %>
|
95
|
+
|
96
|
+
<%= blocks.after :some_block do %>
|
97
|
+
After 1
|
98
|
+
<% end %>
|
99
|
+
|
100
|
+
<%= blocks.append :some_block do %>
|
101
|
+
After 2
|
102
|
+
<% end %>
|
103
|
+
|
104
|
+
Here, "blocks.before" is synonymous with "blocks.prepend" and "blocks.after" is synonymous with "blocks.append". Now, if somewhere, we called:
|
105
|
+
|
106
|
+
<%= blocks.use :some_block %>
|
107
|
+
|
108
|
+
We would see: "Before 1 Before 2 Some Block After 1 After 2".
|
109
|
+
|
110
|
+
What's particularly useful about this is that it doesn't matter if the "before" / "prepend" and "after" / "append" some before or after the "block.define" call. The only thing that matters is that the "blocks.define" occurs before the "blocks.use" call.
|
111
|
+
|
112
|
+
== table_for
|
113
|
+
|
114
|
+
table_for is a useful example of using blocks in a very practical way. It is built entirely using the core blocks library. It is exposed as a helper method with the following prototype:
|
115
|
+
|
116
|
+
table_for(records, options={}, &block)
|
117
|
+
|
118
|
+
The first argument, "records", is the list of objects to render rows in the table for, "options" allows you to specify table-wide settings, and the "block" allows you to specify the
|
119
|
+
|
120
|
+
== Special Thanks
|
121
|
+
|
122
|
+
Thanks to Todd Fisher of Captico for implementation help and setup of gem and to Jon Phillips for suggestions and use case help.
|
data/lib/blocks/builder.rb
CHANGED
@@ -82,6 +82,7 @@ module Blocks
|
|
82
82
|
|
83
83
|
nil
|
84
84
|
end
|
85
|
+
alias prepend before
|
85
86
|
|
86
87
|
def after(name, options={}, &block)
|
87
88
|
name = "after_#{name.to_s}".to_sym
|
@@ -99,6 +100,7 @@ module Blocks
|
|
99
100
|
|
100
101
|
nil
|
101
102
|
end
|
103
|
+
alias append after
|
102
104
|
|
103
105
|
# If a method is missing, we'll assume the user is starting a new block group by that missing method name
|
104
106
|
def method_missing(m, options={}, &block)
|
metadata
CHANGED
@@ -1,12 +1,12 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: blocks
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 29
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
version: "0.
|
8
|
+
- 11
|
9
|
+
version: "0.11"
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Andrew Hunter
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2010-12-
|
18
|
+
date: 2010-12-22 00:00:00 -05:00
|
19
19
|
default_executable:
|
20
20
|
dependencies: []
|
21
21
|
|
@@ -27,7 +27,7 @@ extensions: []
|
|
27
27
|
|
28
28
|
extra_rdoc_files:
|
29
29
|
- LICENSE
|
30
|
-
- README
|
30
|
+
- README.rdoc
|
31
31
|
files:
|
32
32
|
- lib/blocks/builder.rb
|
33
33
|
- lib/blocks/container.rb
|
@@ -38,10 +38,10 @@ files:
|
|
38
38
|
- app/helpers/blocks/helper_methods.rb
|
39
39
|
- app/views/blocks/_list.html.erb
|
40
40
|
- app/views/blocks/_table.html.erb
|
41
|
-
- README
|
41
|
+
- README.rdoc
|
42
42
|
- LICENSE
|
43
43
|
has_rdoc: true
|
44
|
-
homepage:
|
44
|
+
homepage: https://github.com/hunterae/blocks
|
45
45
|
licenses: []
|
46
46
|
|
47
47
|
post_install_message:
|
data/README
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
This is blocks it's easy and fun
|