Skip to content

Commit

Permalink
Fix default headers including all model columns when using :select in…
Browse files Browse the repository at this point in the history
… the collection query [#1 state:resolved]
  • Loading branch information
obrie committed Apr 26, 2009
1 parent d8980ca commit b9e107d
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.rdoc
@@ -1,5 +1,7 @@
== master

* Fix default headers including all model columns when using :select in the collection query

== 0.2.0 / 2009-04-25

* Reorganize documentation
Expand Down
12 changes: 11 additions & 1 deletion lib/table_helper/header.rb
Expand Up @@ -33,7 +33,17 @@ def initialize(table)
# pre-fill the header with those columns so that the user doesn't
# have to
klass = table.klass
column(*klass.column_names.map(&:to_sym)) if klass && klass.respond_to?(:column_names)
if klass && klass.respond_to?(:column_names)
if !table.empty? && klass < ActiveRecord::Base
# Make sure only the attributes that have been loaded are used
column_names = table.collection.first.attributes.keys
else
# Assume all attributes are loaded
column_names = klass.column_names
end

column(*column_names.map(&:to_sym))
end

@customized = false
end
Expand Down
2 changes: 2 additions & 0 deletions test/app_root/app/models/person.rb
@@ -0,0 +1,2 @@
class Person < ActiveRecord::Base
end
11 changes: 11 additions & 0 deletions test/app_root/db/migrate/001_create_people.rb
@@ -0,0 +1,11 @@
class CreatePeople < ActiveRecord::Migration
def self.up
create_table :people do |t|
t.string :first_name, :last_name
end
end

def self.down
drop_table :people
end
end
3 changes: 3 additions & 0 deletions test/test_helper.rb
Expand Up @@ -3,6 +3,9 @@
require 'rubygems'
require 'plugin_test_helper'

# Run the migrations
ActiveRecord::Migrator.migrate("#{Rails.root}/db/migrate")

Test::Unit::TestCase.class_eval do
private
def assert_html_equal(expected, actual)
Expand Down
18 changes: 18 additions & 0 deletions test/unit/header_test.rb
Expand Up @@ -250,3 +250,21 @@ def test_should_include_html_options_for_header_row
assert_html_equal expected, @header.html
end
end

class HeaderWithModelsTest < ActiveRecord::TestCase
def setup
Person.create(:first_name => 'John', :last_name => 'Smith')
end

def test_should_include_all_columns_if_not_selecting_columns
table = TableHelper::CollectionTable.new(Person.all)
@header = TableHelper::Header.new(table)
assert_equal %w(first_name id last_name), @header.column_names.sort
end

def test_should_only_include_selected_columns_if_specified_in_query
table = TableHelper::CollectionTable.new(Person.all(:select => 'first_name'))
@header = TableHelper::Header.new(table)
assert_equal %w(first_name), @header.column_names.sort
end
end

0 comments on commit b9e107d

Please sign in to comment.