cheveret 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. data/Rakefile +1 -1
  2. data/VERSION +1 -1
  3. data/cheveret.gemspec +3 -3
  4. data/lib/cheveret.rb +66 -18
  5. metadata +5 -5
data/Rakefile CHANGED
@@ -10,7 +10,7 @@ begin
10
10
  application is not very DRY even for the simpler of cases. Cheveret allows you to more
11
11
  clearly separate logic and templating and reduce the amount of code in your views.}
12
12
  gem.email = "aulankz@gmail.com"
13
- gem.homepage = "http://github.com/lankz/cheveret"
13
+ gem.homepage = "http://github.com/ratecity/cheveret"
14
14
  gem.authors = ["Ben Caldwell"]
15
15
  gem.add_development_dependency "rspec", ">= 1.2.9"
16
16
  # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.0
1
+ 1.1.0
data/cheveret.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{cheveret}
8
- s.version = "1.0.0"
8
+ s.version = "1.1.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Ben Caldwell"]
12
- s.date = %q{2010-09-17}
12
+ s.date = %q{2010-09-20}
13
13
  s.description = %q{Generating HTML tables of data in the views of your Rails
14
14
  application is not very DRY even for the simpler of cases. Cheveret allows you to more
15
15
  clearly separate logic and templating and reduce the amount of code in your views.}
@@ -33,7 +33,7 @@ clearly separate logic and templating and reduce the amount of code in your view
33
33
  "spec/spec.opts",
34
34
  "spec/spec_helper.rb"
35
35
  ]
36
- s.homepage = %q{http://github.com/lankz/cheveret}
36
+ s.homepage = %q{http://github.com/ratecity/cheveret}
37
37
  s.rdoc_options = ["--charset=UTF-8"]
38
38
  s.require_paths = ["lib"]
39
39
  s.rubygems_version = %q{1.3.7}
data/lib/cheveret.rb CHANGED
@@ -113,14 +113,14 @@ module Cheveret
113
113
  self.visible = true
114
114
  end
115
115
 
116
- # hides the column, prevents it from being rendered in the table
117
- # helper for setting :visible => false
116
+ # hides the column, prevents it from being rendered in the table. acts as a
117
+ # helper for setting <tt>:visible => false</tt>
118
118
  def hide
119
119
  self.visible = false
120
120
  end
121
121
 
122
- # returns true for columns that have :visible => true or #show called on them
123
- # columns are visible by default
122
+ # returns +true+ for columns that have <tt>:visible => true</tt> or #show called
123
+ # on them. columns are visible by default
124
124
  def visible?
125
125
  self.visible != false
126
126
  end
@@ -129,9 +129,14 @@ module Cheveret
129
129
  self.flexible != false
130
130
  end
131
131
 
132
+ # returns +true+ unless a column has explicitly set <tt>:sortable => false</tt>
133
+ def sortable?
134
+ self.sortable != false
135
+ end
136
+
132
137
  def label
133
138
  case @config.label
134
- when nil then @name.to_str.humanize # todo: support i18n for column labels
139
+ when nil then @name.to_s.humanize # todo: support i18n for column labels
135
140
  when false then nil
136
141
  else @config.label
137
142
  end
@@ -147,7 +152,25 @@ module Cheveret
147
152
  end
148
153
 
149
154
  module Helpers
150
- # renders a table for a collection of objects
155
+ # view helper that facilities the rendering of tables for a collection of similar
156
+ # objects
157
+ #
158
+ # behaves much in the same way as Rails +form+for+ helper in that it takes a proc
159
+ # and provides access to a table builder object. the entire block is captured, so
160
+ # additional markup can be output at any point
161
+ #
162
+ # <% table_for @books, :width => 480 do |t| %>
163
+ # <% t.columns :title, :author, :publisher, :price %>
164
+ # <% t.header %>
165
+ # <% t.body %>
166
+ # <% end %>
167
+ #
168
+ # === Options
169
+ # * <tt>:width</tt> - the total width of the table in pixels
170
+ # * <tt>:html</tt> - a hash of html attributes used for the form container
171
+ # * <tt>:url</tt> - used to generate urls for sortable columns
172
+ #
173
+ # === Examples
151
174
  def table_for(collection, options={}, &block)
152
175
  builder = options.delete(:builder) || ActionView::Base.default_table_builder
153
176
 
@@ -155,16 +178,17 @@ module Cheveret
155
178
  builder.render(Table.new, self, options, &block)
156
179
  end
157
180
 
181
+ # the default #TableBuilder class generates an HTML table using div tags
158
182
  class TableBuilder
159
183
  extend ::Forwardable
160
184
  def_delegators :@table, :columns
161
185
 
162
186
  def self.render(table, template, options={}, &block)
163
- # todo: merge container element with :html passed into table_for
164
- attrs = { :class => "table",
165
- :style => "width: #{options[:width]}px;" }
187
+ html_attrs = options.delete(:html) || {}
188
+ html_attrs.merge!({ :class => "table",
189
+ :style => "width: #{options[:width]}px;" })
166
190
 
167
- template.content_tag(:div, attrs) do
191
+ template.content_tag(:div, html_attrs) do
168
192
  template.capture(self.new(table, template, options), &block)
169
193
  end
170
194
  end
@@ -174,20 +198,22 @@ module Cheveret
174
198
 
175
199
  @width = options.delete(:width)
176
200
  @collection = options.delete(:collection)
201
+ @url = options.delete(:url)
177
202
  end
178
203
 
179
204
  def header(*args, &block)
180
205
  @table.resize!(@width)
181
206
 
182
- row = @template.content_tag(:div, :class => "tr") do
207
+ row = content_tag(:div, :class => "tr") do
183
208
  map_columns(:th) do |column|
184
209
  # todo: prevent output of empty <a> tag for header label
185
210
  output = nil_capture(column, &block) if block_given?
186
- output ||= @template.content_tag(:a, column.label)
211
+ output ||= sort_tag(column) if column.sortable?
212
+ output ||= content_tag(:span, column.label)
187
213
  end
188
214
  end
189
215
 
190
- @template.content_tag(:div, row, :class => "thead")
216
+ content_tag(:div, row, :class => "thead")
191
217
  end
192
218
 
193
219
  def body(&block)
@@ -197,15 +223,15 @@ module Cheveret
197
223
  rows = @collection.map do |object|
198
224
  object_name = object.class.to_s.split('::').last.underscore || ''
199
225
  klass = [ 'tr', object_name, (alt = !alt) ? nil : 'alt' ].compact
200
- @template.content_tag(:div, :class => klass.join(' ')) do
226
+ content_tag(:div, :class => klass.join(' ')) do
201
227
  map_columns(:td) do |column|
202
228
  output = nil_capture(column, object, &block) if block_given?
203
- output ||= @template.content_tag(:span, column.data(object))
229
+ output ||= content_tag(:span, column.data(object))
204
230
  end
205
231
  end
206
232
  end
207
233
 
208
- @template.content_tag(:div, rows.join, :class => "tbody")
234
+ content_tag(:div, rows.join, :class => "tbody")
209
235
  end
210
236
 
211
237
  private
@@ -215,16 +241,38 @@ module Cheveret
215
241
  attrs = { :class => [type, column.name].join(' '),
216
242
  :style => "width: #{column.width}px;" }
217
243
 
218
- @template.content_tag(:div, attrs) do
244
+ content_tag(:div, attrs) do
219
245
  yield(column)
220
246
  end if column.visible?
221
247
  end
222
248
  end
223
249
 
224
250
  def nil_capture(*args, &block) #:nodoc:
225
- custom = @template.capture(*args, &block).strip
251
+ custom = capture(*args, &block).strip
226
252
  output = custom.empty? ? nil : custom
227
253
  end
254
+
255
+ def sort_tag(column) #:nodoc:
256
+ return nil unless column.sortable?
257
+
258
+ klass = [ 'sortable' ]
259
+ sort = column.name
260
+ order = "asc"
261
+
262
+ if params[:sort].to_s == column.name.to_s
263
+ klass << [ 'sorted', params[:order] ]
264
+ order = "desc" if params[:order] == "asc"
265
+ end
266
+
267
+ content_tag(:a, column.label, {
268
+ :class => klass.flatten.join(' '),
269
+ :href => url_for(@url.merge({ :sort => sort, :order => order })) })
270
+ end
271
+
272
+ def method_missing(method_name, *args, &block) #:nodoc:
273
+ return @template.send(method_name, *args, &block) if @template.respond_to?(method_name)
274
+ super
275
+ end
228
276
  end
229
277
  end
230
278
 
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cheveret
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
4
+ hash: 19
5
5
  prerelease: false
6
6
  segments:
7
7
  - 1
8
+ - 1
8
9
  - 0
9
- - 0
10
- version: 1.0.0
10
+ version: 1.1.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Ben Caldwell
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-09-17 00:00:00 +10:00
18
+ date: 2010-09-20 00:00:00 +10:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -61,7 +61,7 @@ files:
61
61
  - spec/spec.opts
62
62
  - spec/spec_helper.rb
63
63
  has_rdoc: true
64
- homepage: http://github.com/lankz/cheveret
64
+ homepage: http://github.com/ratecity/cheveret
65
65
  licenses: []
66
66
 
67
67
  post_install_message: