# encoding: UTF-8 require 'spec_helper' describe TableGo::Renderers::HtmlRenderer do let(:articles) do [ Article.new(:title => 'iPutz', :date_of_order => Date.new(2012), :ident => 1, :vat => 19, :price => 5, :xmas_bonus => true, :my_type => 'super_type'), Article.new(:title => 'Nutzbook', :date_of_order => Date.new(2012), :ident => 2, :vat => 19, :price => 5, :xmas_bonus => false, :my_type => 'hardware_type') ] end let(:template) do ActionView::Base.new.tap do |view| view.output_buffer = ActiveSupport::SafeBuffer.new rescue '' end end describe 'automatic mode' do subject { TableGo.render_html(articles, Article, template, {}) } it 'should render a simple automatic html table' do subject.cleanup_html.should == %Q(
Ident Title Date of order Vat Price Xmas bonus My type
1 iPutz 2012-01-01 19 5 true super_type
2 Nutzbook 2012-01-01 19 5 false hardware_type
).cleanup_html end end describe 'custom mode' do subject do TableGo.render_html(articles, Article, template, :title => 'one Table', :table_html => { :id => :articles }, :row_html => { :class => :row_css_class, :id => lambda { |record| "row_#{record.ident}" }}) do |t| t.column :ident, :column_html => { :class => lambda { |record, column, value| value.even? ? :even : :odd } } t.column :vat, :label => 'as percent', :as => :percent t.column :price, :label => 'as € currency', :as => :currency t.column :date_of_order, :header_html => { :class => :date, :style => :xyz, :id => :date_column }, :column_html => { :class => :date, :style => :xyz, :id => lambda { |record, column, value| "date_#{record.ident}" }} t.column :date_of_order, :as => :date, :as_options => { :format => :short } t.column :date_of_order, :label => 'with custom formatter', :as => lambda { |value, record, column| value.to_s.reverse } # t.column :date_of_order, # :label => 'with block level custom formatter' do |value, record, column| # "a special
value" # end t.column :xmas_bonus, :as => :boolean, :label => 'as boolean' t.column :my_type, :send => :titleize # t.column :trader, # :method => :name end end it 'should render a html table', 'with custom attributes' do subject.cleanup_html.should == %Q(
one Table
Ident as percent as € currency Date of order Date of order with custom formatter as boolean My type
1 19.000% $5.00 2012-01-01 Jan 01 10-10-2102 Super Type
2 19.000% $5.00 2012-01-01 Jan 01 10-10-2102 Hardware Type
).cleanup_html end end end