sortablecolumns 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt CHANGED
@@ -1,3 +1,8 @@
1
+ === 0.1.2 / 2008-05-01
2
+
3
+ * Added date and datetime formats for columns
4
+ * Added ability to supply attributes as extra url parameters
5
+
1
6
  === 0.1.1 / 2008-04-29
2
7
 
3
8
  * Minor fixes to test code
data/Manifest.txt CHANGED
@@ -2,7 +2,6 @@ History.txt
2
2
  Manifest.txt
3
3
  README.txt
4
4
  Rakefile
5
- bin/sortable_columns
6
5
  lib/sortablecolumns.rb
7
6
  lib/sortablecolumns/helpers.rb
8
7
  lib/sortablecolumns/sortablecolumns.rb
data/README.txt CHANGED
@@ -64,6 +64,10 @@ In model:
64
64
  end
65
65
 
66
66
  YAML file:
67
+
68
+ The YAML file needs to be in an ordered format (i.e., it gets loaded into Ruby as an Array). The order
69
+ of the columns in the YAML file determines their display order when rendered as HTML.
70
+
67
71
  -
68
72
  firstname:
69
73
  heading: First
@@ -104,6 +108,11 @@ YAML file:
104
108
  delimiter: " "
105
109
  unit: £
106
110
  td_class: right
111
+ -
112
+ created_at:
113
+ heading: Create Date
114
+ datatype: datetime
115
+ date_format: "%m/%d/%Y at %I:%M%p"
107
116
  -
108
117
  edit:
109
118
  in_resultset: false
@@ -129,6 +138,17 @@ YAML file:
129
138
  confirm: Are you sure?
130
139
 
131
140
  In the link_options, use obj_id to specify that the current object's id field should be used.
141
+ You can also have extra parameters in the url that are attributes in the resultset.
142
+ e.g.
143
+ -
144
+ print_view:
145
+ heading: Print
146
+ link_options:
147
+ controller: dudes
148
+ action: print_view
149
+ person_id: person_id
150
+
151
+ ...where person_id is a field (method) in the result set that will be invoked.
132
152
 
133
153
  In the controller:
134
154
 
@@ -1,5 +1,5 @@
1
1
  module Sortablecolumns #:nodoc:
2
- VERSION = '0.1.1'
2
+ VERSION = '0.1.2'
3
3
  end
4
4
  Dir[File.join(File.dirname(__FILE__), "sortablecolumns/**/*.rb")].sort.each { |lib| require lib }
5
5
  ActiveRecord::Base.send(:include, ActiveRecord::Acts::Sortablecolumns)
@@ -32,6 +32,13 @@ module Sortablecolumns
32
32
  :unit => unit, :separator => separator, :delimiter => delimiter)
33
33
  end
34
34
 
35
+ if datatype == 'datetime' or datatype == 'date'
36
+ if txt.respond_to?(:strftime)
37
+ format = klass.send("#{sorter}_date_format",col)
38
+ txt = txt.strftime(format)
39
+ end
40
+ end
41
+
35
42
  do_link = klass.send("#{sorter}_link?",col)
36
43
 
37
44
  if do_link
@@ -47,6 +54,15 @@ module Sortablecolumns
47
54
  extras = link_ops.delete(:extras)
48
55
  txt = link_to(txt, link_ops, extras)
49
56
  else
57
+ if link_ops.size > 2
58
+ # cycle through link_ops, other than controller, action, and id,
59
+ # and check if the option value is a method for this object. If not,
60
+ # assign the value directly in the url.
61
+ ops = link_ops.reject{|key,val| key == :controller or key == :action or key == :id}
62
+ ops.each do |key, val|
63
+ link_ops[key] = obj.send(val) if obj.respond_to?(val)
64
+ end
65
+ end
50
66
  txt = link_to(txt, link_ops)
51
67
  end
52
68
  elsif link_ops[:url]
@@ -57,7 +57,6 @@ module ActiveRecord #:nodoc:
57
57
 
58
58
  def self.#{sorter_name}_yaml_path
59
59
  raise "RAILS_ROOT is not defined" unless defined?(RAILS_ROOT)
60
- #klass_name = self.class_name.downcase
61
60
  klass_name = Inflector.underscore(self.class_name)
62
61
  file_name = "#{sorter_name}.yml"
63
62
  if sorter_options[:subclass_name]
@@ -172,6 +171,19 @@ module ActiveRecord #:nodoc:
172
171
  #{sorter_name}_col_def_hash[col]['unit'] || "$"
173
172
  end
174
173
 
174
+ def self.#{sorter_name}_date_format(col)
175
+ format = #{sorter_name}_col_def_hash[col]['date_format']
176
+ unless format
177
+ datatype = #{sorter_name}_datatype(col)
178
+ if datatype.downcase == 'datetime'
179
+ format = "%Y-%m-%d %I:%M:%S"
180
+ else
181
+ format = "%Y-%m-%d"
182
+ end
183
+ end
184
+ format
185
+ end
186
+
175
187
  def self.#{sorter_name}_print_text(col)
176
188
  #{sorter_name}_col_def_hash[col]['print_text'] || ''
177
189
  end
@@ -22,3 +22,21 @@
22
22
  wrappers:
23
23
  - truncate: 5
24
24
  - simple_format
25
+ -
26
+ print_view:
27
+ in_resultset: false
28
+ heading: false
29
+ print_text: Print
30
+ link_options:
31
+ controller: dudes
32
+ action: print_view
33
+ person_id: person_id
34
+ -
35
+ created_at:
36
+ heading: Create Date
37
+ datatype: datetime
38
+ date_format: "%m/%d/%Y at %I:%M%p"
39
+ -
40
+ registered_at:
41
+ heading: Registration Date
42
+ datatype: datetime
@@ -21,3 +21,8 @@
21
21
  controller: foods
22
22
  action: calories
23
23
  foo: bar
24
+ -
25
+ packaged_on:
26
+ heading: Packaged On
27
+ datatype: date
28
+ date_format: '%m/%d/%Y'
@@ -25,6 +25,8 @@ def setup_db
25
25
  t.column :age, :integer
26
26
  t.column :description, :string
27
27
  t.column :balance, :decimal
28
+ t.column :created_at, :datetime
29
+ t.column :registered_at, :datetime
28
30
  end
29
31
  end
30
32
  end
@@ -57,6 +59,10 @@ class Person < ActiveRecord::Base
57
59
  def calories
58
60
  1200
59
61
  end
62
+
63
+ def packaged_on
64
+ Date.new(2008,01,03)
65
+ end
60
66
  end
61
67
 
62
68
  class Dude < Person
@@ -66,6 +72,10 @@ class Dude < Person
66
72
  def dudename
67
73
  firstname
68
74
  end
75
+
76
+ def person_id
77
+ id
78
+ end
69
79
  end
70
80
 
71
81
  class SubPerson < Person
@@ -154,23 +164,34 @@ class SortableColumnsTest< Test::Unit::TestCase
154
164
  end
155
165
 
156
166
  def test_dude_col_def_hash
157
- expected = {"lastname"=>{"heading"=>"Last", "datatype"=>"string"},
167
+ expected = {"registered_at"=>{"heading"=>"Registration Date", "datatype"=>"datetime"},
168
+ "print_view"=>
169
+ {"link_options"=>
170
+ {"action"=>"print_view", "controller"=>"dudes", "person_id"=>"person_id"},
171
+ "in_resultset"=>false,
172
+ "heading"=>false,
173
+ "print_text"=>"Print"},
174
+ "lastname"=>{"heading"=>"Last", "datatype"=>"string"},
158
175
  "description"=>
159
176
  {"print_options"=>{"wrappers"=>[{"truncate"=>5}, "simple_format"]},
160
177
  "sortable"=>false,
161
178
  "heading"=>"Description",
162
179
  "datatype"=>"string"},
163
180
  "dudename"=>
164
- {"link_options"=>{'object_url'=>true},
181
+ {"link_options"=>{"object_url"=>true},
165
182
  "heading"=>"Dudename",
166
183
  "datatype"=>"string"},
167
- "age"=>{"heading"=>"Age", "datatype"=>"number"}}
184
+ "age"=>{"heading"=>"Age", "datatype"=>"number"},
185
+ "created_at"=>
186
+ {"date_format"=>"%m/%d/%Y at %I:%M%p",
187
+ "heading"=>"Create Date",
188
+ "datatype"=>"datetime"}}
168
189
  assert_equal expected, Dude.dude_report_col_def_hash
169
190
  end
170
191
 
171
192
  def test_col_keys_in_order
172
193
  assert_equal ['firstname','lastname','age','description','balance','edit','delete'], Person.mysorter_col_keys_in_order
173
- assert_equal ['type','meat','calories'], Person.tacosorter_col_keys_in_order
194
+ assert_equal ['type','meat','calories','packaged_on'], Person.tacosorter_col_keys_in_order
174
195
  end
175
196
 
176
197
  def test_heading
@@ -182,10 +203,11 @@ class SortableColumnsTest< Test::Unit::TestCase
182
203
  end
183
204
 
184
205
  def test_headings_in_order
185
- assert_equal Dude.dude_report_headings_in_order, ['Dudename','Last','Age','Description']
186
- assert_equal Person.tacosorter_headings_in_order, ['Taco Type','Meat','Calories']
206
+ assert_equal ['Taco Type','Meat','Calories','Packaged On'], Person.tacosorter_headings_in_order
207
+ #should not include the print field in headings:
208
+ assert_equal ['Dudename','Last','Age','Description', 'Create Date', 'Registration Date'], Dude.dude_report_headings_in_order
187
209
  #should not include the edit field in headings:
188
- assert_equal Person.mysorter_headings_in_order, ['First','Last','Age','Description','Balance']
210
+ assert_equal ['First','Last','Age','Description','Balance'], Person.mysorter_headings_in_order
189
211
  end
190
212
 
191
213
  def test_datatype
@@ -237,6 +259,13 @@ class SortableColumnsTest< Test::Unit::TestCase
237
259
  assert_equal ".", Person.mysorter_separator('age')
238
260
  end
239
261
 
262
+ def test_date_format
263
+ assert_equal "%m/%d/%Y at %I:%M%p" , Dude.dude_report_date_format('created_at')
264
+ #default:
265
+ assert_equal "%Y-%m-%d %I:%M:%S", Dude.dude_report_date_format('registered_at')
266
+ assert_equal "%m/%d/%Y", Person.tacosorter_date_format('packaged_on')
267
+ end
268
+
240
269
  def test_default_sort_dir
241
270
  assert_equal "desc", Person.mysorter_default_sort_dir('age')
242
271
  assert_equal 'asc', Person.mysorter_default_sort_dir('firstname')
@@ -319,8 +348,10 @@ class SortablecolumnsHelperTest < Test::Unit::TestCase
319
348
  @person2 = Person.create(:firstname => "Joe", :lastname => "Shmoe", :age => 54,
320
349
  :balance => '12.00', :description => "Joe rocks")
321
350
 
351
+ created_at = DateTime.new(2008,4,28,9,26)
352
+ registered_at = DateTime.new(2008,5,1,7,30)
322
353
  @dude = Dude.create(:firstname => "The Dude", :lastname => "Lebowski", :age => 45,
323
- :description => "The Dude Speaks")
354
+ :description => "The Dude Speaks", :created_at => created_at, :registered_at => registered_at)
324
355
 
325
356
  @expected_person_row = "<tr><td class=\"left\"><a href=\"http://test.host/people/show/1\">Billy</a></td><td>Jones</td><td class=\"center\">24</td><td class=\"left\"><p>Billy is an awesome guy.\n<br />However, he is also a punk. <a href=\"http://www.google.com\">www.google.com</a></p></td><td class=\"right\">£1 234,57</td><td><a href=\"http://test.host/people/edit/1\">Edit</a></td><td><a href=\"http://test.host/people/destroy/1\" onclick=\"if (confirm('Are you sure?')) { var f = document.createElement('form'); f.style.display = 'none'; this.parentNode.appendChild(f); f.method = 'POST'; f.action = this.href;var m = document.createElement('input'); m.setAttribute('type', 'hidden'); m.setAttribute('name', '_method'); m.setAttribute('value', 'delete'); f.appendChild(m);f.submit(); };return false;\">Delete</a></td></tr>"
326
357
  @expected_person_row_alternate = @expected_person_row.gsub(/<tr>/, "<tr class=\"even\">")
@@ -379,6 +410,38 @@ class SortablecolumnsHelperTest < Test::Unit::TestCase
379
410
  assert_equal "<td><a href=\"http://test.host/foods/calories?foo=bar\">1200.00</a></td>", @view.print_col(@person, :tacosorter, :calories)
380
411
  end
381
412
 
413
+ def test_print_col_dude_print_view
414
+ #testing if we can have extra parameters in the url, and those params are attributes in the resultset
415
+ # e.g.
416
+ #-
417
+ #print_view:
418
+ # heading: Print
419
+ # link_options:
420
+ # controller: dudes
421
+ # action: print_view
422
+ # person_id: person_id
423
+ #
424
+ # Where person_id is a field in the result set (in this test, there's a mock method instead)
425
+
426
+ expected = "<td><a href=\"http://test.host/dudes/print_view?person_id=#{@dude.id}\">Print</a></td>"
427
+ assert_equal expected, @view.print_col(@dude, :dude_report, :print_view)
428
+ end
429
+
430
+ def test_print_col_dude_created_at
431
+ expected = "<td>04/28/2008 at 09:26AM</td>"
432
+ assert_equal expected, @view.print_col(@dude, :dude_report, :created_at)
433
+ end
434
+
435
+ def test_print_col_dude_registered_at
436
+ expected = "<td>2008-05-01 07:30:00</td>"
437
+ assert_equal expected, @view.print_col(@dude, :dude_report, :registered_at)
438
+ end
439
+
440
+ def test_print_col_person_packaged_on
441
+ expected = "<td>01/03/2008</td>"
442
+ assert_equal expected, @view.print_col(@person, :tacosorter, :packaged_on)
443
+ end
444
+
382
445
  def test_print_col_edit
383
446
  expected = "<td><a href=\"http://test.host/people/edit/#{@person.id}\">Edit</a></td>"
384
447
  assert_equal expected, @view.print_col(@person, :mysorter, :edit)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sortablecolumns
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bryan Donovan - http://www.bryandonovan.com
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-04-29 00:00:00 +00:00
12
+ date: 2008-05-01 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -23,8 +23,8 @@ dependencies:
23
23
  version:
24
24
  description: Sortable HTML tables for Rails
25
25
  email: b.dondo+rubyforge@gmail.com
26
- executables:
27
- - sortable_columns
26
+ executables: []
27
+
28
28
  extensions: []
29
29
 
30
30
  extra_rdoc_files:
@@ -36,7 +36,6 @@ files:
36
36
  - Manifest.txt
37
37
  - README.txt
38
38
  - Rakefile
39
- - bin/sortable_columns
40
39
  - lib/sortablecolumns.rb
41
40
  - lib/sortablecolumns/helpers.rb
42
41
  - lib/sortablecolumns/sortablecolumns.rb
data/bin/sortable_columns DELETED
File without changes