table-for 1.2.0 → 1.2.1

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.rdoc CHANGED
@@ -4,4 +4,10 @@
4
4
 
5
5
  1.2.0 (February 5, 2012)
6
6
 
7
- * Upgraded dependency on BuildingBlocks to at least version 1.2.0
7
+ * Upgraded dependency on BuildingBlocks to at least version 1.2.0
8
+
9
+ 1.2.1 (February 9, 2012)
10
+
11
+ * Moved table_for_evaluated_options method into BuildingBlocks gem and renamed to evaluated_procs
12
+ * Also now utilizing a method created in BuildingBlocks called evaluated_proc
13
+ * The url for an edit, show, and delete link can now be a Proc that takes the current row's domain object as a paramter
data/README.rdoc CHANGED
@@ -22,19 +22,19 @@ In users_controller.rb (index action)
22
22
 
23
23
  == Sample Usage
24
24
  <%= table_for @users, :table_html => {:style => "border: 1px solid black"},
25
- :sortable => true,
25
+ :sortable => true, :sort_url => users_path,
26
26
  :row_html => {:class => lambda { cycle('even', 'odd')},
27
27
  :id => lambda {|user| "user-#{user.id}"}} do |table| %>
28
- <%= table.column :edit %>
28
+ <%= table.column :edit, :link_label => "Modify" %>
29
29
  <%= table.column :show %>
30
30
  <%= table.column :email, :label => "Email Address" %>
31
31
  <%= table.column :label => "Full Name", :sortable => false, :header_html => {:style => "color:orange"} do |user| %>
32
32
  <%= "#{user.first_name} #{user.last_name}" %>
33
33
  <% end %>
34
- <%= table.column :delete %>
34
+ <%= table.column :delete, :confirm => "Are you sure?" %>
35
35
  <% end %>
36
36
 
37
- == Specifying the columns to use
37
+ == Specifying the Columns to Use
38
38
  <%= table_for @users do |table| %>
39
39
  <% table.column :email %>
40
40
  <% table.column :first_name %>
@@ -42,30 +42,91 @@ In users_controller.rb (index action)
42
42
  <% end %>
43
43
 
44
44
  Here, each column will send its respective column name to the user object: user.send(:email), user.send(:first_name), user.send(:last_name),
45
- to determine what data to output in each column
45
+ to determine what data to output in each column.
46
46
 
47
- == Specifying the label to use for a column
47
+ == Specifying the Label to Use for a Column
48
48
  <%= table_for @users do |table| %>
49
49
  <% table.column :email, :label => "Email Address" %>
50
50
  <% end %>
51
51
 
52
- == Overridding the default way or rendering a column
52
+ == Overriding the Default Way of Rendering a Column
53
53
  <%= table_for @users do |table| %>
54
54
  <% table.column :name do |user| %>
55
55
  <%= "#{user.first_name} #{user.last_name}" %>
56
56
  <% end %>
57
57
  <% end %>
58
58
 
59
- Here, since the name of the column is not used anymore to determine what data to output, this code could be changed to:
59
+ <!-- Or, since the name of the column is no longer used to figure out what data to render: -->
60
60
  <%= table_for @users do |table| %>
61
61
  <% table.column :full_name do |user| %>
62
62
  <%= "#{user.first_name} #{user.last_name}" %>
63
63
  <% end %>
64
64
  <% end %>
65
65
 
66
- Or the block name could have been removed altogether and replaced with a label as follows:
66
+ <!-- Or the block name could be removed altogether and replaced with a label: -->
67
67
  <%= table_for @users do |table| %>
68
68
  <% table.column :label => "Full Name" do |user| %>
69
69
  <%= "#{user.first_name} #{user.last_name}" %>
70
70
  <% end %>
71
+ <% end %>
72
+
73
+ == Specifying Sortability
74
+ <!-- Globally: -->
75
+ <%= table_for @users, :sortable => true, :sort_url => users_path do |table| %>
76
+ <% table.column :id, :sortable => false %>
77
+ <% table.column :email %>
78
+ <% table.column :name, :order => "first_name,last_name" do |user| %>
79
+ <%= "#{user.first_name} #{user.last_name}" %>
80
+ <% end %>
81
+ <% end %>
82
+
83
+ <!-- Column by column basis: -->
84
+ <%= table_for @users, :sort_url => users_path do |table| %>
85
+ <% table.column :id %>
86
+ <% table.column :email, :sortable => true %>
87
+ <% table.column :name, :order => "first_name,last_name", :sortable => true do |user| %>
88
+ <%= "#{user.first_name} #{user.last_name}" %>
89
+ <% end %>
90
+ <% end %>
91
+
92
+ == Specifying the Table HTML
93
+ <%= table_for @users, :table_html => {:class => "table-for-table", :style => "border:1px solid black"} %>
94
+
95
+ == Specifying the Row HTML
96
+ <!-- Header row -->
97
+ <%= table_for @users, :header_row_html => {:style => "background-color:blue", :class => "table-header"} do |table| %>
98
+ <% table.column :email %>
99
+ <% end %>
100
+
101
+ <!-- Data rows -->
102
+ <%= table_for @users, :row_html => {:style => "background-color:blue",
103
+ :class => lambda {"table-row #{cycle('even', 'odd')}"},
104
+ :id => lambda {|user| "user-#{user.id}"}} do |table| %>
105
+ <% table.column :email %>
106
+ <% end %>
107
+
108
+ == Specifying the Column HTML
109
+ <!-- Header columns -->
110
+ <%= table_for @users, :header_html => {:class => lambda {|column| "header-column #{column.name}"} } do |table| %>
111
+ <!-- Override this columns' header's html -->
112
+ <% table.column :email, :header_html => {:style => "color: red"} %>
113
+
114
+ <% table.column :first_name %>
115
+ <% table.column :last_name %>
116
+ <% end %>
117
+
118
+ <!-- Data columns -->
119
+ <%= table_for @users, :column_html => {:class => lambda {|column| "data-column #{column.name}"} } do |table| %>
120
+ <!-- Override this column's html -->
121
+ <% table.column :email, :column_html => {:style => "background-color: red"} %>
122
+
123
+ <% table.column :first_name %>
124
+ <% table.column :last_name %>
125
+ <% end %>
126
+
127
+ == Using Show, Edit, and Delete Columns
128
+ <%= table_for @users do |table| %>
129
+ <% table.column :edit, :url => lambda {|user| edit_user_path(user) } %>
130
+ <% table.column :show %>
131
+ <% table.column :show %>
71
132
  <% end %>
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.2.0
1
+ 1.2.1
@@ -24,7 +24,7 @@
24
24
  <% end %>
25
25
 
26
26
  <% table.define :header_column do |column, options| %>
27
- <%= content_tag :th, table_for_header_html(column, options) do %>
27
+ <%= content_tag :th, table_for_header_html(table, column, options) do %>
28
28
  <%= table.render "#{column.name}_header", column, column.options %>
29
29
  <% end %>
30
30
  <% end %>
@@ -58,7 +58,7 @@
58
58
  <% end %>
59
59
 
60
60
  <% table.define :row do |record, options| %>
61
- <%= content_tag :tr, table_for_evaluated_options(record, options[:row_html]) do %>
61
+ <%= content_tag :tr, table.evaluated_procs(record, options[:row_html]) do %>
62
62
  <%= table.render :columns, record %>
63
63
  <% end %>
64
64
  <% end %>
@@ -70,23 +70,26 @@
70
70
  <% end %>
71
71
 
72
72
  <% table.define :column do |record, column, options| %>
73
- <%= content_tag :td, table_for_evaluated_options(record, column, options[:column_html]) do %>
73
+ <%= content_tag :td, table.evaluated_procs(record, column, options[:column_html]) do %>
74
74
  <%= table.render column.name, record, column, column.options %>
75
75
  <% end %>
76
76
  <% end %>
77
77
 
78
78
  <% table.define :edit, :action => :edit, :link_label => "Edit" do |record, column, options| %>
79
- <%= link_to options[:link_label], [options[:action], options[:scope], record].flatten, options[:link_html] %>
79
+ <% url = options[:url] ? table.evaluated_proc(record, options[:url]) : [options[:action], options[:scope], record].flatten %>
80
+ <%= link_to options[:link_label], url, options[:link_html] %>
80
81
  <% end %>
81
82
 
82
83
  <% table.define :show, :action => nil, :link_label => "Show" do |record, column, options| %>
83
- <%= link_to options[:link_label], [options[:action], options[:scope], record].flatten, options[:link_html] %>
84
+ <% url = options[:url] ? table.evaluated_proc(record, options[:url]) : [options[:action], options[:scope], record].flatten %>
85
+ <%= link_to options[:link_label], url, options[:link_html] %>
84
86
  <% end %>
85
87
 
86
88
  <% table.define :delete, :action => nil, :link_html => {}, :link_label => "Delete" do |record, column, options| %>
89
+ <% url = options[:url] ? table.evaluated_proc(record, options[:url]) : [options[:action], options[:scope], record].flatten %>
87
90
  <% confirm = options[:confirm] ? options[:confirm] : "Are you sure you want to delete this #{record.class.to_s.titleize}?" %>
88
91
  <% method = options[:method] ? options[:method] : "delete" %>
89
- <%= link_to options[:link_label], [options[:action], options[:scope], record].flatten, {:method => method, :confirm => confirm}.merge(options[:link_html]) %>
92
+ <%= link_to options[:link_label], url, {:method => method, :confirm => confirm}.merge(options[:link_html]) %>
90
93
  <% end %>
91
94
 
92
95
  <% table.columns.each do |column| %>
@@ -2,16 +2,12 @@ module TableFor
2
2
  module ViewAdditions
3
3
  module ClassMethods
4
4
  def table_for(records, options={}, &block)
5
+ options[:use_partials_for_before_and_after_hooks] = false
5
6
  TableFor::Base.new(self, options.merge(:variable => "table", :records => records)).render_template("table_for/table_for", &block)
6
7
  end
7
8
 
8
- def table_for_evaluated_options(*args)
9
- options = args.extract_options!
10
- options.inject({}) { |hash, (k, v)| hash[k] = (v.is_a?(Proc) ? v.call(*args) : v); hash} unless options.nil?
11
- end
12
-
13
- def table_for_header_html(column, options={})
14
- header_html = table_for_evaluated_options(column, options[:header_html])
9
+ def table_for_header_html(table, column, options={})
10
+ header_html = table.evaluated_procs(column, options[:header_html])
15
11
  if options[:sortable]
16
12
  order = options[:order] ? options[:order].to_s : column.name.to_s
17
13
  sort_class = (params[:order] != order || params[:sort_mode] == "reset") ? "sorting" : (params[:sort_mode] == "desc" ? "sorting_desc" : "sorting_asc")
@@ -42,61 +42,44 @@ describe TableFor::ViewAdditions do
42
42
  end
43
43
  end
44
44
 
45
- describe "table_for_evaluated_options method" do
46
- it "should evaluate any proc options" do
47
- proc1 = lambda {@view.cycle("even", "odd")}
48
- proc2 = lambda {@view.cycle("one", "two")}
49
- evaluated_options = @view.table_for_evaluated_options(:class => proc1, :id => proc2, :style => "color:red")
50
- evaluated_options[:class].should eql "even"
51
- evaluated_options[:id].should eql "one"
52
- evaluated_options[:style].should eql "color:red"
53
- end
54
-
55
- it "should pass any additional arguments to evaluated procs" do
56
- proc1 = lambda { |param1, param2| "user_#{param1}_#{param2}"}
57
- evaluated_options = @view.table_for_evaluated_options(1, 2, :class => proc1)
58
- evaluated_options[:class].should eql "user_1_2"
59
- end
60
- end
61
-
62
45
  describe "table_for_header_html method" do
63
46
  it "should return nil if header_html is not passed in" do
64
- header_html = @view.table_for_header_html(@column)
47
+ header_html = @view.table_for_header_html(mock(:evaluated_procs => {}), @column)
65
48
  header_html.should eql({})
66
49
  end
67
50
 
68
51
  it "should evaluate any procs for header_html" do
69
- header_html = @view.table_for_header_html(@column, :header_html => {:class => lambda {|column| "#{column.name}_header"}})
52
+ header_html = @view.table_for_header_html(mock(:evaluated_procs => {:class => "#{@column.name}_header"}), @column, :header_html => {:class => lambda {|column| "#{column.name}_header"}})
70
53
  header_html[:class].should eql "#{@column.name}_header"
71
54
  end
72
55
 
73
56
  it "should join the 'sorting' class with any other header_html class provided" do
74
57
  @view.expects(:params).returns({})
75
- header_html = @view.table_for_header_html(@column, :header_html => {:class => "c1 c2"}, :sortable => true)
58
+ header_html = @view.table_for_header_html(mock(:evaluated_procs => {:class => "c1 c2"}), @column, :header_html => {:class => "c1 c2"}, :sortable => true)
76
59
  header_html[:class].should eql "c1 c2 sorting"
77
60
  end
78
61
 
79
62
  it "should add a 'sorting' class to the header_html class if a column is sortable" do
80
63
  @view.expects(:params).returns({})
81
- header_html = @view.table_for_header_html(@column, :sortable => true)
64
+ header_html = @view.table_for_header_html(mock(:evaluated_procs => {}), @column, :sortable => true)
82
65
  header_html[:class].should eql "sorting"
83
66
  end
84
67
 
85
68
  it "should add a 'sorting_asc' class to the header_html class if a column is sortable and it is already sorted in asc order" do
86
69
  @view.expects(:params).at_least_once.returns(:order => @column.name.to_s, :sort_mode => "asc")
87
- header_html = @view.table_for_header_html(@column, :sortable => true)
70
+ header_html = @view.table_for_header_html(mock(:evaluated_procs => {}), @column, :sortable => true)
88
71
  header_html[:class].should eql "sorting_asc"
89
72
  end
90
73
 
91
74
  it "should add a 'sorting_desc' class to the header_html class if a column is sortable and it is already sorted in desc order" do
92
75
  @view.expects(:params).at_least_once.returns(:order => @column.name.to_s, :sort_mode => "desc")
93
- header_html = @view.table_for_header_html(@column, :sortable => true)
76
+ header_html = @view.table_for_header_html(mock(:evaluated_procs => {}), @column, :sortable => true)
94
77
  header_html[:class].should eql "sorting_desc"
95
78
  end
96
79
 
97
80
  it "should add a 'sorting' class to the header_html class if a column is sortable and it is reset mode" do
98
81
  @view.expects(:params).at_least_once.returns(:order => @column.name.to_s, :sort_mode => "reset")
99
- header_html = @view.table_for_header_html(@column, :sortable => true)
82
+ header_html = @view.table_for_header_html(mock(:evaluated_procs => {}), @column, :sortable => true)
100
83
  header_html[:class].should eql "sorting"
101
84
  end
102
85
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: table-for
3
3
  version: !ruby/object:Gem::Version
4
- hash: 31
4
+ hash: 29
5
5
  prerelease:
6
6
  segments:
7
7
  - 1
8
8
  - 2
9
- - 0
10
- version: 1.2.0
9
+ - 1
10
+ version: 1.2.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - Andrew Hunter
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-02-05 00:00:00 -05:00
18
+ date: 2012-02-09 00:00:00 -05:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -55,12 +55,12 @@ dependencies:
55
55
  requirements:
56
56
  - - ">="
57
57
  - !ruby/object:Gem::Version
58
- hash: 31
58
+ hash: 25
59
59
  segments:
60
60
  - 1
61
61
  - 2
62
- - 0
63
- version: 1.2.0
62
+ - 3
63
+ version: 1.2.3
64
64
  prerelease: false
65
65
  type: :runtime
66
66
  requirement: *id003
@@ -85,7 +85,7 @@ dependencies:
85
85
  requirements:
86
86
  - - ">="
87
87
  - !ruby/object:Gem::Version
88
- hash: -1271655756
88
+ hash: -4164648240
89
89
  segments:
90
90
  - 2
91
91
  - 0
@@ -187,7 +187,7 @@ dependencies:
187
187
  requirements:
188
188
  - - ">="
189
189
  - !ruby/object:Gem::Version
190
- hash: -1271655756
190
+ hash: -4164648240
191
191
  segments:
192
192
  - 2
193
193
  - 0
@@ -289,7 +289,7 @@ dependencies:
289
289
  requirements:
290
290
  - - ">="
291
291
  - !ruby/object:Gem::Version
292
- hash: -1271655756
292
+ hash: -4164648240
293
293
  segments:
294
294
  - 2
295
295
  - 0
@@ -370,6 +370,210 @@ dependencies:
370
370
  prerelease: false
371
371
  type: :development
372
372
  requirement: *id024
373
+ - !ruby/object:Gem::Dependency
374
+ name: jeweler
375
+ version_requirements: &id025 !ruby/object:Gem::Requirement
376
+ none: false
377
+ requirements:
378
+ - - ">="
379
+ - !ruby/object:Gem::Version
380
+ hash: 3
381
+ segments:
382
+ - 0
383
+ version: "0"
384
+ prerelease: false
385
+ type: :development
386
+ requirement: *id025
387
+ - !ruby/object:Gem::Dependency
388
+ name: rspec-rails
389
+ version_requirements: &id026 !ruby/object:Gem::Requirement
390
+ none: false
391
+ requirements:
392
+ - - ">="
393
+ - !ruby/object:Gem::Version
394
+ hash: -4164648240
395
+ segments:
396
+ - 2
397
+ - 0
398
+ - 0
399
+ - beta
400
+ - 20
401
+ version: 2.0.0.beta.20
402
+ prerelease: false
403
+ type: :development
404
+ requirement: *id026
405
+ - !ruby/object:Gem::Dependency
406
+ name: mocha
407
+ version_requirements: &id027 !ruby/object:Gem::Requirement
408
+ none: false
409
+ requirements:
410
+ - - ">="
411
+ - !ruby/object:Gem::Version
412
+ hash: 3
413
+ segments:
414
+ - 0
415
+ version: "0"
416
+ prerelease: false
417
+ type: :development
418
+ requirement: *id027
419
+ - !ruby/object:Gem::Dependency
420
+ name: xml-simple
421
+ version_requirements: &id028 !ruby/object:Gem::Requirement
422
+ none: false
423
+ requirements:
424
+ - - ">="
425
+ - !ruby/object:Gem::Version
426
+ hash: 3
427
+ segments:
428
+ - 0
429
+ version: "0"
430
+ prerelease: false
431
+ type: :development
432
+ requirement: *id028
433
+ - !ruby/object:Gem::Dependency
434
+ name: supermodel
435
+ version_requirements: &id029 !ruby/object:Gem::Requirement
436
+ none: false
437
+ requirements:
438
+ - - ">="
439
+ - !ruby/object:Gem::Version
440
+ hash: 3
441
+ segments:
442
+ - 0
443
+ version: "0"
444
+ prerelease: false
445
+ type: :development
446
+ requirement: *id029
447
+ - !ruby/object:Gem::Dependency
448
+ name: sqlite3
449
+ version_requirements: &id030 !ruby/object:Gem::Requirement
450
+ none: false
451
+ requirements:
452
+ - - ">="
453
+ - !ruby/object:Gem::Version
454
+ hash: 3
455
+ segments:
456
+ - 0
457
+ version: "0"
458
+ prerelease: false
459
+ type: :development
460
+ requirement: *id030
461
+ - !ruby/object:Gem::Dependency
462
+ name: with_model
463
+ version_requirements: &id031 !ruby/object:Gem::Requirement
464
+ none: false
465
+ requirements:
466
+ - - ">="
467
+ - !ruby/object:Gem::Version
468
+ hash: 3
469
+ segments:
470
+ - 0
471
+ version: "0"
472
+ prerelease: false
473
+ type: :development
474
+ requirement: *id031
475
+ - !ruby/object:Gem::Dependency
476
+ name: jeweler
477
+ version_requirements: &id032 !ruby/object:Gem::Requirement
478
+ none: false
479
+ requirements:
480
+ - - ">="
481
+ - !ruby/object:Gem::Version
482
+ hash: 3
483
+ segments:
484
+ - 0
485
+ version: "0"
486
+ prerelease: false
487
+ type: :development
488
+ requirement: *id032
489
+ - !ruby/object:Gem::Dependency
490
+ name: rspec-rails
491
+ version_requirements: &id033 !ruby/object:Gem::Requirement
492
+ none: false
493
+ requirements:
494
+ - - ">="
495
+ - !ruby/object:Gem::Version
496
+ hash: -4164648240
497
+ segments:
498
+ - 2
499
+ - 0
500
+ - 0
501
+ - beta
502
+ - 20
503
+ version: 2.0.0.beta.20
504
+ prerelease: false
505
+ type: :development
506
+ requirement: *id033
507
+ - !ruby/object:Gem::Dependency
508
+ name: mocha
509
+ version_requirements: &id034 !ruby/object:Gem::Requirement
510
+ none: false
511
+ requirements:
512
+ - - ">="
513
+ - !ruby/object:Gem::Version
514
+ hash: 3
515
+ segments:
516
+ - 0
517
+ version: "0"
518
+ prerelease: false
519
+ type: :development
520
+ requirement: *id034
521
+ - !ruby/object:Gem::Dependency
522
+ name: xml-simple
523
+ version_requirements: &id035 !ruby/object:Gem::Requirement
524
+ none: false
525
+ requirements:
526
+ - - ">="
527
+ - !ruby/object:Gem::Version
528
+ hash: 3
529
+ segments:
530
+ - 0
531
+ version: "0"
532
+ prerelease: false
533
+ type: :development
534
+ requirement: *id035
535
+ - !ruby/object:Gem::Dependency
536
+ name: supermodel
537
+ version_requirements: &id036 !ruby/object:Gem::Requirement
538
+ none: false
539
+ requirements:
540
+ - - ">="
541
+ - !ruby/object:Gem::Version
542
+ hash: 3
543
+ segments:
544
+ - 0
545
+ version: "0"
546
+ prerelease: false
547
+ type: :development
548
+ requirement: *id036
549
+ - !ruby/object:Gem::Dependency
550
+ name: sqlite3
551
+ version_requirements: &id037 !ruby/object:Gem::Requirement
552
+ none: false
553
+ requirements:
554
+ - - ">="
555
+ - !ruby/object:Gem::Version
556
+ hash: 3
557
+ segments:
558
+ - 0
559
+ version: "0"
560
+ prerelease: false
561
+ type: :development
562
+ requirement: *id037
563
+ - !ruby/object:Gem::Dependency
564
+ name: with_model
565
+ version_requirements: &id038 !ruby/object:Gem::Requirement
566
+ none: false
567
+ requirements:
568
+ - - ">="
569
+ - !ruby/object:Gem::Version
570
+ hash: 3
571
+ segments:
572
+ - 0
573
+ version: "0"
574
+ prerelease: false
575
+ type: :development
576
+ requirement: *id038
373
577
  description: table-for is a table builder for an array of objects, easily allowing overriding of how any aspect of the table is generated
374
578
  email: hunterae@gmail.com
375
579
  executables: []