spec/table_spec.rb in tableficate-0.2.1 vs spec/table_spec.rb in tableficate-0.3.0
- old
+ new
@@ -3,11 +3,11 @@
describe Tableficate::Table do
before(:each) do
template = mock('Template')
template.stub!(:lookup_context).and_return(ActionView::LookupContext.new([]))
template.lookup_context.stub!(:exists?).and_return(true)
- @table = Tableficate::Table.new(template, NobelPrizeWinner.limit(1), {}, {current_sort: {column: :first_name, dir: 'asc'}})
+ @table = Tableficate::Table.new(template, NobelPrizeWinner.joins(:nobel_prizes).limit(1), {}, {current_sort: {column: :first_name, dir: 'asc'}})
end
it 'should have the current sort if provided' do
@table.current_sort.should == {column: :first_name, dir: 'asc'}
end
@@ -15,10 +15,32 @@
it 'should use the :as option or default to the table_name of the scope' do
Tableficate::Table.new(nil, NobelPrizeWinner.limit(1), {as: 'npw'}, {}).as.should == 'npw'
@table.as.should == 'nobel_prize_winners'
end
+ it 'should add a empty' do
+ @table.empty('There is no data.')
+ @table.empty.is_a?(Tableficate::Empty).should be true
+ @table.empty.value.should == 'There is no data.'
+
+ @table.empty do
+ 'No data.'
+ end
+ @table.empty.value.should == 'No data.'
+ end
+
+ it 'should add a caption' do
+ @table.caption('Nobel Prize Winners')
+ @table.caption.is_a?(Tableficate::Caption).should be true
+ @table.caption.value.should == 'Nobel Prize Winners'
+
+ @table.caption do
+ 'Nobel Winners'
+ end
+ @table.caption.value.should == 'Nobel Winners'
+ end
+
it 'should add a Column' do
@table.column(:first_name)
@table.column(:last_name)
@table.columns.first.name.should == :first_name
@@ -83,13 +105,102 @@
it 'should add the Input for known types and pass through the type based on :as' do
@table.filter(:first_name, as: :search)
@table.filters.first.is_a?(Tableficate::Filter::Input).should be true
- @table.filters.first.options[:type].should == 'search'
+ @table.filters.first.attrs[:type].should == 'search'
end
+ it 'should default the type to "checkbox" for boolean fields' do
+ @table.filter(:shared)
+
+ @table.filters.first.is_a?(Tableficate::Filter::CheckBox).should be true
+ @table.filters.first.attrs[:type].should == 'checkbox'
+ end
+
+ it 'should default the type to "email" for string fields with "email" somewhere in the name' do
+ @table.filter(:email)
+
+ @table.filters.first.is_a?(Tableficate::Filter::Input).should be true
+ @table.filters.first.attrs[:type].should == 'email'
+ end
+
+ it 'should default the type to "url" for string fields with "url" somewhere in the name' do
+ @table.filter(:url)
+
+ @table.filters.first.is_a?(Tableficate::Filter::Input).should be true
+ @table.filters.first.attrs[:type].should == 'url'
+ end
+
+ it 'should default the type to "tel" for string fields with "phone" somewhere in the name' do
+ @table.filter(:home_phone)
+
+ @table.filters.first.is_a?(Tableficate::Filter::Input).should be true
+ @table.filters.first.attrs[:type].should == 'tel'
+ end
+
+ it 'should default the type to "number" for integer fields' do
+ @table.filter(:year)
+
+ @table.filters.first.is_a?(Tableficate::Filter::Input).should be true
+ @table.filters.first.attrs[:type].should == 'number'
+ end
+
+ it 'should default the type to "number" for float fields' do
+ @table.filter(:meaningless_float)
+
+ @table.filters.first.is_a?(Tableficate::Filter::Input).should be true
+ @table.filters.first.attrs[:type].should == 'number'
+ end
+
+ it 'should default the type to "number" for decimal fields' do
+ @table.filter(:meaningless_decimal)
+
+ @table.filters.first.is_a?(Tableficate::Filter::Input).should be true
+ @table.filters.first.attrs[:type].should == 'number'
+ end
+
+ it 'should default the type to "date" for date fields' do
+ @table.filter(:birthdate)
+
+ @table.filters.first.is_a?(Tableficate::Filter::Input).should be true
+ @table.filters.first.attrs[:type].should == 'date'
+ end
+
+ it 'should default the type to "time" for time fields' do
+ @table.filter(:meaningless_time)
+
+ @table.filters.first.is_a?(Tableficate::Filter::Input).should be true
+ @table.filters.first.attrs[:type].should == 'time'
+ end
+
+ it 'should default the type to "datetime" for datetime fields' do
+ @table.filter(:created_at)
+
+ @table.filters.first.is_a?(Tableficate::Filter::Input).should be true
+ @table.filters.first.attrs[:type].should == 'datetime'
+ end
+
+ it 'should default the type to "datetime" for timestamp fields' do
+ @table.filter(:updated_at)
+
+ @table.filters.first.is_a?(Tableficate::Filter::Input).should be true
+ @table.filters.first.attrs[:type].should == 'datetime'
+ end
+
+ it 'should default the type based on the actual field, not the label' do
+ template = mock('Template')
+ template.stub!(:lookup_context).and_return(ActionView::LookupContext.new([]))
+ template.lookup_context.stub!(:exists?).and_return(true)
+ table = Tableficate::Table.new(template, NobelPrizeWinner.joins(:nobel_prizes).limit(1), {}, {field_map: {foo: 'year'}})
+
+ table.filter(:foo)
+
+ table.filters.first.is_a?(Tableficate::Filter::Input).should be true
+ table.filters.first.attrs[:type].should == 'number'
+ end
+
it 'should add a InputRange filter' do
@table.filter_range(:first_name, label: 'First')
@table.filter_range(:last_name, label: 'Last')
@table.filters.first.name.should == :first_name
@@ -103,10 +214,10 @@
it 'should add the InputRange for known types and pass through the type based on :as' do
@table.filter_range(:first_name, as: :search)
@table.filters.first.is_a?(Tableficate::Filter::InputRange).should be true
- @table.filters.first.options[:type].should == 'search'
+ @table.filters.first.attrs[:type].should == 'search'
end
it 'should add a Select filter' do
@table.filter(:first_name, collection: {}, label: 'First')
@table.filter(:last_name, collection: {}, label: 'Last')