blocks 1.2.5 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,31 +0,0 @@
1
- <%= list.define :item do |options| %>
2
- <%= options[:field] ? options[:record].send(options[:field]) : options[:record].to_s %>
3
- <% end %>
4
-
5
- <%= list.define :items do |options| %>
6
- <% if records %>
7
- <% records.each do |record| %>
8
- <%= content_tag :li, options[:item_html] do %>
9
- <%= list.use :item, :record => record %>
10
- <% end %>
11
- <% end %>
12
- <% else %>
13
- <% list.items.each do |item| %>
14
- <%= content_tag :li, options.merge(item.options)[:item_html] do %>
15
- <%= list.use item.name %>
16
- <% end %>
17
- <% end %>
18
- <% end %>
19
- <% end %>
20
-
21
- <% list.items.each do |item| %>
22
- <%= list.define item.name, :item => item do |options| %>
23
- <%= item.name %>
24
- <% end %>
25
- <% end %>
26
-
27
- <%= list.use :list do |options| %>
28
- <%= content_tag :ul, options[:list_html] do %>
29
- <%= list.use :items %>
30
- <% end %>
31
- <% end %>
@@ -1,105 +0,0 @@
1
- <%= table.define :edit do |record, options| %>
2
- <%= link_to "Edit", [:edit, options[:scope], record].flatten %>
3
- <% end %>
4
-
5
- <%= table.define :show do |record, options| %>
6
- <%= link_to "Show", [options[:scope], record].flatten %>
7
- <% end %>
8
-
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
- <% end %>
12
-
13
- <%= table.define :thead do %>
14
- <thead>
15
- <%= table.use :header_row %>
16
- </thead>
17
- <% end %>
18
-
19
- <%= table.define :header_row do %>
20
- <tr>
21
- <%= table.use :header_columns %>
22
- </tr>
23
- <% end %>
24
-
25
- <%= table.define :header_columns do |options| %>
26
- <% table.columns.each do |column| %>
27
- <% header_html = options.merge(column.options)[:header_html] %>
28
- <% if options.merge(column.options)[:sortable] %>
29
- <% order = column.options[:order] ? column.options[:order].to_s : column.name.to_s %>
30
-
31
- <% sort_class = params[:order] != order ? "sorting" : (params[:sort_mode] == "desc" ? "sorting_desc" : "sorting_asc") %>
32
- <% header_html = {} if header_html.nil? %>
33
- <% header_html[:class] ||= "" %>
34
- <% header_html[:class] += " #{sort_class}" %>
35
- <% end %>
36
-
37
- <%= content_tag :th, header_html do %>
38
- <%= table.use "#{column.name.to_s}_header", options.merge(column.options) %>
39
- <% end %>
40
- <% end %>
41
- <% end %>
42
-
43
- <% table.columns.each do |column| %>
44
- <%= table.define "#{column.name.to_s}_header", :column => column do |options| %>
45
- <% if options[:sortable] %>
46
- <%= table.use "#{options[:column].name.to_s}_header_sortable_link", options %>
47
- <% else %>
48
- <%= options[:label] ? options[:label] : options[:column].name.to_s.titleize %>
49
- <% end %>
50
- <% end %>
51
-
52
- <%= table.define "#{column.name.to_s}_header_sortable_link", :column => column do |options| %>
53
- <%= table.use :header_sortable_link, options %>
54
- <% end %>
55
- <% end %>
56
-
57
- <%= table.define :header_sortable_link do |options| %>
58
- <% order = options[:order] ? options[:order].to_s : options[:column].name.to_s %>
59
- <% label = (options[:label] ? options[:label] : options[:column].name.to_s.titleize) %>
60
- <% sort_mode = ((params[:order] != order or params[:sort_mode] == "desc") ? "asc" : "desc") %>
61
- <% parameters = params.merge({:order => (options[:order] ? options[:order] : options[:column].name), :sort_mode => sort_mode}) %>
62
- <% parameters.delete(:action); parameters.delete(:controller) %>
63
- <% url = options[:sort_url] ? options[:sort_url] : "" %>
64
-
65
- <%= link_to label, "#{url}?#{parameters.to_query}" %>
66
- <% end %>
67
-
68
- <%= table.define :tbody do %>
69
- <tbody>
70
- <%= table.use :rows %>
71
- </tbody>
72
- <% end %>
73
-
74
- <%= table.define :rows do %>
75
- <% records.each do |record| %>
76
- <%= table.use :row, record %>
77
- <% end %>
78
- <% end %>
79
-
80
- <%= table.define :row do |record, options| %>
81
- <%= content_tag :tr, evaluated_content_options(options[:row_html], options) do %>
82
- <%= table.use :data_columns, record, options %>
83
- <% end %>
84
- <% end %>
85
-
86
- <%= table.define :data_columns do |record, options| %>
87
- <% table.columns.each do |column| %>
88
- <%= content_tag :td, options.merge(column.options)[:column_html] do %>
89
- <%= table.use column, record, options.merge(:column => column) %>
90
- <% end %>
91
- <% end %>
92
- <% end %>
93
-
94
- <% table.columns.each do |column| %>
95
- <%= table.define column.name, :column => column do |record, options| %>
96
- <%= record.send(options[:column].name) %>
97
- <% end %>
98
- <% end %>
99
-
100
- <%= table.use :table do |options| %>
101
- <%= content_tag :table, options[:table_html] do %>
102
- <%= table.use :thead %>
103
- <%= table.use :tbody %>
104
- <% end %>
105
- <% end %>
@@ -1,236 +0,0 @@
1
- module Blocks
2
- class Builder
3
-
4
- attr_accessor :view, :blocks, :block_positions, :anonymous_block_number,
5
- :start_rendering_blocks, :block_groups, :shared_options
6
-
7
- def defined?(name)
8
- !blocks[name.to_sym].nil?
9
- end
10
-
11
- def define(name, options={}, &block)
12
- block_container = Blocks::Container.new
13
- block_container.name = name
14
- block_container.options = options
15
- block_container.block = block
16
-
17
- blocks[name.to_sym] = block_container if blocks[name.to_sym].nil?
18
-
19
- nil
20
- end
21
-
22
- def replace(name, options={}, &block)
23
- block_container = Blocks::Container.new
24
- block_container.name = name
25
- block_container.options = options
26
- block_container.block = block
27
-
28
- blocks[name.to_sym] = block_container
29
-
30
- nil
31
- end
32
-
33
- def use(*args, &block)
34
- options = args.extract_options!
35
-
36
- # If the user doesn't specify a block name, we generate an anonymous block name to assure other
37
- # anonymous blocks don't override its definition
38
- name = args.first ? args.shift : self.anonymous_block_name
39
-
40
- block_container = Blocks::Container.new
41
- block_container.name = name
42
- block_container.options = options
43
- block_container.block = block
44
-
45
- blocks[name.to_sym] = block_container if !name.is_a?(Blocks::Container) and blocks[name.to_sym].nil? and block
46
-
47
- if start_rendering_blocks
48
- render_block name, args, options
49
- else
50
- # Delays rendering this block until the partial has been rendered and all the blocks have had a chance to be defined
51
- self.block_positions << block_container
52
- end
53
-
54
- nil
55
- end
56
-
57
- def render
58
- self.start_rendering_blocks = false
59
-
60
- shared_options[:captured_block] = view.capture(self, &shared_options[:block]) if shared_options[:block]
61
-
62
- self.start_rendering_blocks = true
63
-
64
- view.render shared_options[:template], shared_options
65
- end
66
-
67
- def before(name, options={}, &block)
68
- name = "before_#{name.to_s}".to_sym
69
-
70
- block_container = Blocks::Container.new
71
- block_container.name = name
72
- block_container.options = options
73
- block_container.block = block
74
-
75
- if view.blocks.blocks[name].nil?
76
- view.blocks.blocks[name] = [block_container]
77
- else
78
- view.blocks.blocks[name] << block_container
79
- end
80
-
81
- nil
82
- end
83
- alias prepend before
84
-
85
- def after(name, options={}, &block)
86
- name = "after_#{name.to_s}".to_sym
87
-
88
- block_container = Blocks::Container.new
89
- block_container.name = name
90
- block_container.options = options
91
- block_container.block = block
92
-
93
- if view.blocks.blocks[name].nil?
94
- view.blocks.blocks[name] = [block_container]
95
- else
96
- view.blocks.blocks[name] << block_container
97
- end
98
-
99
- nil
100
- end
101
- alias append after
102
-
103
- # If a method is missing, we'll assume the user is starting a new block group by that missing method name
104
- def method_missing(m, options={}, &block)
105
- # If the specified block group has already been defined, it is simply returned here for iteration.
106
- # It will consist of all the blocks used in this block group that have yet to be rendered,
107
- # as the call for their use occurred before the template was rendered (where their definitions likely occurred)
108
- return self.block_groups[m] unless self.block_groups[m].nil?
109
-
110
- # Allows for nested block groups, store the current block positions array and start a new one
111
- original_block_positions = self.block_positions
112
- self.block_positions = []
113
- self.block_groups[m] = self.block_positions
114
-
115
- # Capture the contents of the block group (this will only capture block definitions and block uses)
116
- view.capture(shared_options.merge(options), &block) if block_given?
117
-
118
- # restore the original block positions array
119
- self.block_positions = original_block_positions
120
- nil
121
- end
122
-
123
- protected
124
-
125
- def initialize(options)
126
- self.view = options[:view]
127
- self.shared_options = options
128
-
129
- options[options[:variable] ? options[:variable].to_sym : :blocks] = self
130
- options[:templates_folder] = "blocks" if options[:templates_folder].nil?
131
-
132
- self.block_positions = []
133
- self.blocks = {}
134
- self.anonymous_block_number = 0
135
- self.block_groups = {}
136
- self.start_rendering_blocks = true
137
- end
138
-
139
- def anonymous_block_name
140
- self.anonymous_block_number = self.anonymous_block_number + 1
141
- "block_#{anonymous_block_number}"
142
- end
143
-
144
- def render_block(name_or_container, args, options={})
145
- render_options = options
146
-
147
- if (name_or_container.is_a?(Blocks::Container))
148
- name = name_or_container.name.to_sym
149
- render_options = render_options.merge(name_or_container.options)
150
- else
151
- name = name_or_container.to_sym
152
- end
153
-
154
- view.concat(render_before_blocks(name, options))
155
-
156
- if blocks[name]
157
- block_container = blocks[name]
158
-
159
- args.push(shared_options.merge(block_container.options).merge(render_options))
160
-
161
- # If the block is taking more than one parameter, we can use *args
162
- if block_container.block.arity > 1
163
- view.concat(view.capture *args, &block_container.block)
164
-
165
- # However, if the block only takes a single parameter, we do not want ruby to try to cram the args list into that parameter
166
- # as an array
167
- else
168
- view.concat(view.capture args.first, &block_container.block)
169
- end
170
- elsif view.blocks.blocks[name]
171
- block_container = view.blocks.blocks[name]
172
-
173
- args.push(shared_options.merge(block_container.options).merge(render_options))
174
-
175
- # If the block is taking more than one parameter, we can use *args
176
- if block_container.block.arity > 1
177
- view.concat(view.capture *args, &block_container.block)
178
-
179
- # However, if the block only takes a single parameter, we do not want ruby to try to cram the args list into that parameter
180
- # as an array
181
- else
182
- view.concat(view.capture args.first, &block_container.block)
183
- end
184
- else
185
- begin
186
- begin
187
- view.concat(view.render "#{name.to_s}", shared_options.merge(render_options))
188
- rescue ActionView::MissingTemplate
189
- # This partial did not exist in the current controller's view directory; now checking in the default templates folder
190
- view.concat(view.render "#{shared_options[:templates_folder]}/#{name.to_s}", shared_options.merge(render_options))
191
- end
192
- rescue ActionView::MissingTemplate
193
- # This block does not exist and no partial can be found to satify it
194
- end
195
- end
196
-
197
- view.concat(render_after_blocks(name, options))
198
- end
199
-
200
- def render_before_blocks(name, options={})
201
- name = "before_#{name.to_s}".to_sym
202
-
203
- unless blocks[name].nil?
204
- blocks[name].each do |block_container|
205
- view.concat(view.capture shared_options.merge(block_container.options).merge(options), &block_container.block)
206
- end
207
- end
208
-
209
- unless view.blocks.blocks[name].nil? || view.blocks.blocks == blocks
210
- view.blocks.blocks[name].each do |block_container|
211
- view.concat(view.capture shared_options.merge(block_container.options).merge(options), &block_container.block)
212
- end
213
- end
214
-
215
- nil
216
- end
217
-
218
- def render_after_blocks(name, options={})
219
- name = "after_#{name.to_s}".to_sym
220
-
221
- unless blocks[name].nil?
222
- blocks[name].each do |block_container|
223
- view.concat(view.capture shared_options.merge(block_container.options).merge(options), &block_container.block)
224
- end
225
- end
226
-
227
- unless view.blocks.blocks[name].nil? || view.blocks.blocks == blocks
228
- view.blocks.blocks[name].each do |block_container|
229
- view.concat(view.capture shared_options.merge(block_container.options).merge(options), &block_container.block)
230
- end
231
- end
232
-
233
- nil
234
- end
235
- end
236
- end
@@ -1,12 +0,0 @@
1
- require "blocks"
2
- require "rails"
3
-
4
- module Blocks
5
- class Engine < Rails::Engine
6
- initializer "blocks.initialize_helpers" do
7
- ActiveSupport.on_load(:action_view) do
8
- include Blocks::HelperMethods
9
- end
10
- end
11
- end
12
- end
@@ -1,14 +0,0 @@
1
- module Blocks
2
- class ListFor < Blocks::Builder
3
- alias items block_positions
4
- alias item use
5
-
6
- def initialize(options)
7
- options[:template] = "blocks/list"
8
- options[:templates_folder] = "blocks/lists"
9
- options[:record_variable] = "records"
10
- options[:variable] = "list"
11
- super
12
- end
13
- end
14
- end
@@ -1,18 +0,0 @@
1
- module Blocks
2
- class TableFor < Blocks::Builder
3
- alias columns block_positions
4
- alias column use
5
-
6
- def header(name, options={}, &block)
7
- define("#{name.to_s}_header", options, &block)
8
- end
9
-
10
- def initialize(options)
11
- options[:template] = "blocks/table"
12
- options[:templates_folder] = "blocks/tables"
13
- options[:record_variable] = "records"
14
- options[:variable] = "table"
15
- super
16
- end
17
- end
18
- end