blocks 1.1.4 → 1.1.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/README.rdoc +71 -88
  2. data/app/views/blocks/_table.html.erb +11 -11
  3. metadata +2 -2
data/README.rdoc CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Wiki[http://wiki.github.com/hunterae/blocks]
4
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).
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}[https://github.com/hunterae/blocks/wiki/table_for]) and list generator (list_for).
6
6
 
7
7
  == Installation
8
8
 
@@ -21,9 +21,7 @@ Alternatively, you can install it as a plugin.
21
21
 
22
22
  == Getting Started
23
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:
24
+ What makes blocks particularly powerful is that it represent a combination of the ruby definition of blocks and partials. A block may be defined inline utilizing syntax similar to that of content_for:
27
25
 
28
26
  <%= blocks.define :some_block_name, :some_parameter => 1, :some_parameter2 => 2 do |options| %>
29
27
  <%= options[:some_parameter] %>
@@ -31,87 +29,35 @@ To define a block:
31
29
  <%= options[:some_parameter3] %>
32
30
  <% end %>
33
31
 
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".
32
+ or it may be written as a partial, existing in the controller view directory for whatever controller renders the view, or the globally configured blocks directory (views/blocks by default). The above block definition might then be written in the file views/blocks/_some_block_name.html.erb:
35
33
 
36
- Now, we may use the above block like follows:
34
+ <%= some_parameter if defined?(some_parameter)%>
35
+ <%= some_parameter2 if defined?(some_parameter2) %>
36
+ <%= some_parameter3 if defined?(some_parameter3) %>
37
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:
38
+ Notice that in the context of a partial, we have direct access to the parameters as local variables. However, since we're not utilizing "blocks.define" to create our block, we can't specify the default parameters :some_parameter and :some_parameter2 as we do in the first example. This has the potential to cause a runtime exception in the context of partials and we will need to assure that optional parameters are defined using "defined?".
41
39
 
42
- <%= blocks.use :some_block_name, :some_parameter3 => 3 %>
40
+ In the first example, we specified 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 (or local variables for partials), no error will occur if the user fails to specify "some_parameter3".
43
41
 
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:
42
+ Now, we may use the above block as follows:
45
43
 
46
- <%= blocks.use :some_block_name, :some_parameter2 => "overridden", :some_parameter3 => 3 %>
44
+ <%= blocks.use :some_block_name %>
47
45
 
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>
46
+ The system will first look for a block that's been defined inline by the name :some_block_name. If it cannot find one, it will try and render a partial with the same name within the current controller's view directory. Failing to find that, it will try and render a partial within the global blocks view directory (by default, views/blocks). If that partial also does not exist, it will give up and render nothing. (See [[Render Order]])
59
47
 
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:
48
+ Here, we will see the output "1 2" for the first example (and "" for the second example). But if we pass in "some_parameter3", as follows:
61
49
 
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:
50
+ <%= blocks.use :some_block_name, :some_parameter3 => 3 %>
83
51
 
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:
52
+ Then we will see "1 2 3" for the first example (and "3" for the second example). Additionally, we can override any of the previous parameters when using a block simply by passing in the new value as a parameter:
105
53
 
106
- <%= blocks.use :some_block %>
54
+ <%= blocks.use :some_block_name, :some_parameter2 => "overridden", :some_parameter3 => 3 %>
107
55
 
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.
56
+ In this case, we will see "1 overridden 3" for the first example (and "overridden 3" for the second example). Thus, we now have content_for but with parameters, and additional intelligence by utilizing partials.
111
57
 
112
58
  == table_for
113
59
 
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:
60
+ table_for is a useful example of how blocks may be utilized in an invaluable way. It is built entirely using the core blocks library. It is exposed as a helper method with the following prototype:
115
61
 
116
62
  table_for(records, options={}, &block)
117
63
 
@@ -120,38 +66,75 @@ For example, if you have an Array of objects that respond to the following [:nam
120
66
  @records = [OpenStruct.new({:name => "The Name", :email => "The Email", :phonenumber => "A phone number"}),
121
67
  OpenStruct.new({:name => "The Second Name", :email => "The Second Email", :phonenumber => "A second phone number"})]
122
68
 
123
- And using table_for you define each column with the following:
124
-
125
- <%= table_for @records do |table| %>
126
- <%= table.column :name %>
127
- <%= table.column :email %>
128
- <%= table.column :phonenumber %>
129
- <% end %>
69
+ And using table_for you can very easily specify intricate details about the table (Note: most of the options in this example are optional):
70
+
71
+ <%= table_for @records,
72
+ :table_html => {:id => "records"},
73
+ :header_html => {:style => "color:red"},
74
+ :row_html => {:class => lambda { |parameters| cycle('odd', 'even')}},
75
+ :column_html => {:style => "color:green"} do |table| %>
76
+ <%= table.column :name, :column_html => {:style => "color:blue"}, :header_html => {:style => "color:orange"} %>
77
+ <%= table.column :email %>
78
+ <%= table.column :phonenumber, :column_html => {:style => "color:orange"}, :header_html => {:style => "color:blue"} %>
79
+ <%= table.column :label => "???" do %>
80
+ Some Random Column
81
+ <% end %>
82
+ <% end %>
130
83
 
131
84
  Will generate the following table:
132
85
 
133
- <table>
86
+ <table id="records">
134
87
  <thead>
135
88
  <tr>
136
- <th>Name</th>
137
- <th>Email</th>
138
- <th>Phonenumber</th>
89
+ <th style="color:orange">Name</th>
90
+ <th style="color:red">Email</th>
91
+ <th style="color:blue">Phonenumber</th>
92
+ <th style="color:red">???</th>
139
93
  </tr>
140
94
  </thead>
141
95
  <tbody>
142
96
  <tr class="odd">
143
- <td>The Name</td>
144
- <td>The Email</td>
145
- <td>A phone number</td>
97
+ <td style="color:blue">The Name</td>
98
+ <td style="color:green">The Email</td>
99
+ <td style="color:orange">A phone number</td>
100
+ <td style="color:green">Some Random Column</td>
146
101
  </tr>
147
102
  <tr class="even">
148
- <td>The Second Name</td>
149
- <td>The Second Email</td>
150
- <td>A second phone number</td>
103
+ <td style="color:blue">The Second Name</td>
104
+ <td style="color:green">The Second Email</td>
105
+ <td style="color:orange">A second phone number</td>
106
+ <td style="color:green">Some Random Column</td>
151
107
  </tr>
152
108
  </tbody>
153
109
  </table>
110
+
111
+ See {table_for}[https://github.com/hunterae/blocks/wiki/table_for] for details.
112
+
113
+ == Building Layouts
114
+
115
+ Utilizing blocks, we can rethink the way we define our templates. Everywhere where we might be tempted to write "yield :some_block_name", we can replace with "blocks.use :some_block_name". This gives us
116
+ the ability to provide a default implementation for that block if it cannot be found (see {Providing a Default Implementation of a Block}[https://github.com/hunterae/blocks/wiki/Providing-a-Default-Implementation-of-a-Block]), and provides hooks for being able to render code before and after :some_block_name, utilizing "blocks.before :some_block_name" and "blocks.after :some_block_name" (See {Before and After Blocks}[https://github.com/hunterae/blocks/wiki/Before-and-After-Blocks]]).
117
+
118
+ See {Building Layouts}[https://github.com/hunterae/blocks/wiki/Building-Layouts] for more details on how to utilize Blocks to build layouts.
119
+
120
+ == Wiki Docs
121
+
122
+ * {Basics}[https://github.com/hunterae/blocks/wiki/Basics]
123
+ * {Specifying your blocks as controller level partials}[https://github.com/hunterae/blocks/wiki/Specifying-your-blocks-as-controller-level-partials]
124
+ * {Specifying your blocks as global partials}[https://github.com/hunterae/blocks/wiki/Specifying-your-blocks-as-global-partials]
125
+ * {How Inline Blocks, Controller Level Blocks, and Global Blocks Work Together}[https://github.com/hunterae/blocks/wiki/How-Inline-Blocks,-Controller-Level-Blocks,-and-Global-Blocks-Work-Together]
126
+ * {Building Layouts}[https://github.com/hunterae/blocks/wiki/Building-Layouts]
127
+ * {Render Order}[https://github.com/hunterae/blocks/wiki/Render-Order]
128
+ * {Before and After Blocks}[https://github.com/hunterae/blocks/wiki/Before-and-After-Blocks]
129
+ * {Providing a Default Implementation of a Block}[https://github.com/hunterae/blocks/wiki/Providing-a-Default-Implementation-of-a-Block]
130
+ * {table_for}[https://github.com/hunterae/blocks/wiki/table_for]
131
+ * {Overriding the defaults in table_for}[https://github.com/hunterae/blocks/wiki/Overriding-the-defaults-in-table_for]
132
+ * {Defining your own reusage table_for implementation}[https://github.com/hunterae/blocks/wiki/Defining-your-own-reusage-table_for-implementation]
133
+ * {See more}[https://github.com/hunterae/blocks/wiki]
134
+
135
+ == Questions or Problems?
154
136
 
137
+ If you have any issues with Blocks which you cannot find the solution to in the documentation[https://github.com/hunterae/blocks/wiki], please add an {issue on GitHub}[https://github.com/hunterae/blocks/issues] or fork the project and send a pull request.
155
138
 
156
139
  == Special Thanks
157
140
 
@@ -1,13 +1,13 @@
1
- <%= table.define :edit do |row, options| %>
2
- <%= link_to "Edit", [:edit, options[:scope], options[:record]].flatten %>
1
+ <%= table.define :edit do |record, options| %>
2
+ <%= link_to "Edit", [:edit, options[:scope], record].flatten %>
3
3
  <% end %>
4
4
 
5
- <%= table.define :show do |row, options| %>
6
- <%= link_to "Show", [options[:scope], options[:record]].flatten %>
5
+ <%= table.define :show do |record, options| %>
6
+ <%= link_to "Show", [options[:scope], record].flatten %>
7
7
  <% end %>
8
8
 
9
- <%= table.define :delete do |row, options| %>
10
- <%= link_to "Delete", [options[:scope], options[:record]].flatten, :method => "delete", :confirm => "Are you sure you want to delete this #{options[:record].class.to_s.humanize}?" %>
9
+ <%= table.define :delete do |record, options| %>
10
+ <%= link_to "Delete", [options[:scope], record].flatten, :method => "delete", :confirm => "Are you sure you want to delete this #{record.class.to_s.humanize}?" %>
11
11
  <% end %>
12
12
 
13
13
  <%= table.define :thead do %>
@@ -44,20 +44,20 @@
44
44
 
45
45
  <%= table.define :rows do %>
46
46
  <% records.each do |record| %>
47
- <%= table.use :row, :record => record %>
47
+ <%= table.use :row, record %>
48
48
  <% end %>
49
49
  <% end %>
50
50
 
51
- <%= table.define :row do |options| %>
51
+ <%= table.define :row do |record, options| %>
52
52
  <%= content_tag :tr, evaluated_content_options(options[:row_html], options) do %>
53
- <%= table.use :data_columns, options %>
53
+ <%= table.use :data_columns, record, options %>
54
54
  <% end %>
55
55
  <% end %>
56
56
 
57
- <%= table.define :data_columns do |options| %>
57
+ <%= table.define :data_columns do |record, options| %>
58
58
  <% table.columns.each do |column| %>
59
59
  <%= content_tag :td, options.merge(column.options)[:column_html] do %>
60
- <%= table.use column, options[:record], options.merge(:column => column) %>
60
+ <%= table.use column, record, options.merge(:column => column) %>
61
61
  <% end %>
62
62
  <% end %>
63
63
  <% end %>
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: blocks
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 1.1.4
5
+ version: 1.1.5
6
6
  platform: ruby
7
7
  authors:
8
8
  - Andrew Hunter
@@ -11,7 +11,7 @@ autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
13
 
14
- date: 2011-03-08 00:00:00 -05:00
14
+ date: 2011-04-15 00:00:00 -04:00
15
15
  default_executable:
16
16
  dependencies: []
17
17