require "spec_helper" describe "table_for" do with_model :user do table do |t| t.string "email" t.string "first_name" t.string "last_name" end end class Offer attr_accessor :first_name def initialize(first_name) @first_name = first_name end end before :each do User.create! :email => "andrew.hunter@livingsocial.com", :first_name => "Andrew", :last_name => "Hunter" User.create! :email => "todd.fisher@livingsocial.com", :first_name => "Todd", :last_name => "Fisher" User.create! :email => "jon.phillips@livingsocial.com", :first_name => "Jon", :last_name => "Phillips" @users = User.all @view = ActionView::Base.new("app/views") end it "should be able render a table with email and first and last name columns" do buffer = @view.table_for @users do |table| table.column :email table.column :first_name table.column :last_name end xml = XmlSimple.xml_in(%%
EmailFirst NameLast Name
andrew.hunter@livingsocial.comAndrewHunter
todd.fisher@livingsocial.comToddFisher
jon.phillips@livingsocial.comJonPhillips
%) XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml end describe "table block" do it "should be able to replace the table block" do buffer = @view.table_for @users[0,1] do |table| table.define :table do "My new table definition" end table.column :email table.column :first_name table.column :last_name end buffer.strip.should eql "My new table definition" end it "should be able to specify html attributes" do buffer = @view.table_for @users, :table_html => {:style => "background-color: orange"} xml = XmlSimple.xml_in(%%
%) XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml end end describe "header block" do it "should be able to replace the thead block" do buffer = @view.table_for @users[0,1] do |table| table.define :header do "My new header definition".html_safe end table.column :first_name end xml = XmlSimple.xml_in(%%
My new header definition
Andrew
%) XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml end it "should be able to specify html attributes" do buffer = @view.table_for @users[0,1], :thead_html => {:style => "background-color: orange"} xml = XmlSimple.xml_in(%%
%) XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml end it "should translate column names" do rails_4_active_record_array = @users[0, 1].clone Array.any_instance.expects(:model => User) I18n.expects(:t).with("activerecord.attributes.user.first_name", :default => "First Name").returns("Vorname") buffer = @view.table_for rails_4_active_record_array do |table| table.column :first_name end xml = XmlSimple.xml_in(%%
Vorname
Andrew
%) XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml Array.any_instance.stubs(:respond_to? => false) I18n.expects(:t).with("activerecord.attributes.user.first_name", :default => "First Name").returns("Vorname2") buffer = @view.table_for @users[0,1] do |table| table.column :first_name end xml = XmlSimple.xml_in(%%
Vorname2
Andrew
%) XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml non_activerecord_array = [Offer.new("Timmy")] I18n.expects(:t).with("tables.columns.offer.first_name", :default => "First Name").returns("Vorname3") buffer = @view.table_for non_activerecord_array do |table| table.column :first_name end xml = XmlSimple.xml_in(%%
Vorname3
Timmy
%) XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml # the rare case where the array has objects of different types mixed_array = [@users.first, Offer.new("Timmy")] I18n.expects(:t).with("tables.columns.first_name", :default => "First Name").returns("Vorname3") buffer = @view.table_for mixed_array do |table| table.column :first_name end xml = XmlSimple.xml_in(%%
Vorname3
Andrew
Timmy
%) XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml end it "should not translate column name if header passed" do I18n.expects(:t).never buffer = @view.table_for @users[0,1] do |table| table.column :first_name, :header => "My First Name" end xml = XmlSimple.xml_in(%%
My First Name
Andrew
%) XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml end end describe "header_row block" do it "should be able to replace the header_row block" do buffer = @view.table_for @users[0,1] do |table| table.define :header_row do "My new header_row definition".html_safe end table.column :first_name end xml = XmlSimple.xml_in(%%
My new header_row definition
Andrew
%) XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml end it "should be able to specify html attributes" do buffer = @view.table_for @users[0,1], :header_row_html => {:style => "background-color: orange"} do |table| table.column :first_name end xml = XmlSimple.xml_in(%%
First Name
Andrew
%) XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml end end describe "header_column block" do it "should be able to replace the header_columns block" do buffer = @view.table_for @users[0,1] do |table| table.define :header_column do "Repeated Column".html_safe end table.column :first_name table.column :last_name end xml = XmlSimple.xml_in(%%
Repeated ColumnRepeated Column
AndrewHunter
%) XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml end it "should be able to specify html attributes" do buffer = @view.table_for @users[0,1], :header_column_html => {:class => "sortable"} do |table| table.column :first_name end xml = XmlSimple.xml_in(%%
First Name
Andrew
%) XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml end it "should be able to dynamically specify column attributes" do buffer = @view.table_for @users[0, 1], :header_column_html => {:class => lambda {@view.cycle("even", "odd")}, :id => lambda {|column| "#{column.name.to_s}_header"}} do |table| table.column :email table.column :first_name table.column :last_name end xml = XmlSimple.xml_in(%%
Email First Name Last Name
andrew.hunter@livingsocial.comAndrewHunter
%) XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml end end # describe "edit_header block" do # it "should be able to replace the edit_header block" do # @view.expects(:edit_user_path).with(User.first).returns("/users/1/edit") # buffer = @view.table_for @users[0,1] do |table| # table.define :edit_header do # "Edit" # end # table.column :edit # end # # xml = XmlSimple.xml_in(%% # # # # # # # #
Edit
# Edit #
%) # XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml # end # end # # describe "delete_header block" do # it "should be able to replace the delete_header block" do # @view.expects(:user_path).with(User.first).returns("/users/1") # buffer = @view.table_for @users[0,1] do |table| # table.define :delete_header do # "Delete" # end # table.column :delete # end # # xml = XmlSimple.xml_in(%% # # # # # # # #
Delete
# Delete #
%) # XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml # end # end # # describe "show_header block" do # it "should be able to replace the show_header block" do # @view.expects(:user_path).with(User.first).returns("/users/1") # buffer = @view.table_for @users[0,1] do |table| # table.define :show_header do # "Show" # end # table.column :show # end # # xml = XmlSimple.xml_in(%% # # # # # # # #
Show
# Show #
%) # XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml # end # end describe "column header contents block" do it "should be able to override the header for a particular column" do buffer = @view.table_for @users[0,1] do |table| table.column :email, :header => "Email Address" end xml = XmlSimple.xml_in(%%
Email Address
andrew.hunter@livingsocial.com
%) XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml end it "should be able to override the global defined header for all columns" do buffer = @view.table_for @users[0,1], :header => "My Default Header" do |table| table.column :email, :header => "Email Address" table.column :first_name end xml = XmlSimple.xml_in(%%
Email AddressMy Default Header
andrew.hunter@livingsocial.comAndrew
%) XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml end it "should be able to override the definition for a particular column header block" do buffer = @view.table_for @users[0,1] do |table| table.column :email, :header => "Email Address" table.define :email_header do |column, options| "My Own Header (Replaced #{options[:header]})" end end xml = XmlSimple.xml_in(%%
My Own Header (Replaced Email Address)
andrew.hunter@livingsocial.com
%) XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml end it "should be able to override the definition for a particular column header block using the table_for 'header' method" do buffer = @view.table_for @users[0,1] do |table| table.column :email, :header => "Email Address" table.header :email do |column, options| "My Own Header (Replaced #{options[:header]})" end end xml = XmlSimple.xml_in(%%
My Own Header (Replaced Email Address)
andrew.hunter@livingsocial.com
%) XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml end it "should add a 'sorting' class to the 'th' element and a link around the header content if a column is sortable" do @view.expects(:params).at_least_once.returns({}) buffer = @view.table_for @users[0,1] do |table| table.column :email, :sortable => true, :header_column_html => {:class => "email", :style => "color:red"} end xml = XmlSimple.xml_in(%%
andrew.hunter@livingsocial.com
%) XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml end it "should add a 'sorting_asc' class to the 'th' element and a link around the header content if a column is sortable and the column is sorted in asc order" do @view.expects(:params).at_least_once.returns({:order => "email", :sort_mode => "asc"}) buffer = @view.table_for @users[0,1] do |table| table.column :email, :sortable => true end xml = XmlSimple.xml_in(%%
Email
andrew.hunter@livingsocial.com
%) XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml end it "should add a 'sorting_asc' class to the 'th' element and a link around the header content if a column is sortable and the column is sorted in asc order and the order param matches the column order field" do @view.expects(:params).at_least_once.returns({:order => "email_address", :sort_mode => "asc"}) buffer = @view.table_for @users[0,1] do |table| table.column :email, :sortable => true, :order => "email_address" end xml = XmlSimple.xml_in(%%
Email
andrew.hunter@livingsocial.com
%) XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml end it "should not add a 'sorting_asc' class to the 'th' element and a link around the header content if a column is sortable and the column is sorted in asc order and the order param does not match the column order field" do @view.expects(:params).at_least_once.returns({:order => "email", :sort_mode => "asc"}) buffer = @view.table_for @users[0,1] do |table| table.column :email, :sortable => true, :order => "email_address" end xml = XmlSimple.xml_in(%%
Email
andrew.hunter@livingsocial.com
%) XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml end it "should add a 'sorting_desc' class to the 'th' element and a link around the header content if a column is sortable and the column is sorted in desc order" do @view.expects(:params).at_least_once.returns({:order => "email", :sort_mode => "desc"}) buffer = @view.table_for @users[0,1] do |table| table.column :email, :sortable => true end xml = XmlSimple.xml_in(%%
Email
andrew.hunter@livingsocial.com
%) XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml end it "should add a 'sorting' class to the 'th' element and a link around the header content if a column is sortable and the sort_mode is in reset mode" do @view.expects(:params).at_least_once.returns({:order => "email", :sort_mode => ""}) buffer = @view.table_for @users[0,1] do |table| table.column :email, :sortable => true end xml = XmlSimple.xml_in(%%
Email
andrew.hunter@livingsocial.com
%) XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml end it "should allow a sort_url to be specified for sortable columns" do @view.expects(:params).at_least_once.returns({}) buffer = @view.table_for @users[0,1] do |table| table.column :email, :sortable => true, :sort_url => "/users" end xml = XmlSimple.xml_in(%%
Email
andrew.hunter@livingsocial.com
%) XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml end it "should allow a global sortable option for all columns and the ability to override on a column by column basis" do @view.expects(:params).at_least_once.returns({}) buffer = @view.table_for @users[0,1], :sortable => true do |table| table.column :email table.column :first_name, :sortable => false table.column :last_name end xml = XmlSimple.xml_in(%%
Email First Name Last Name
andrew.hunter@livingsocial.comAndrewHunter
%) XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml end it "should allow a global header_column_html option for all columns and the ability to override on a column by column basis" do buffer = @view.table_for @users[0,1], :header_column_html => {:class => lambda {|column| "#{column.name}_header"}} do |table| @view.expects(:params).at_least_once.returns({}) table.column :email, :sortable => true table.column :first_name, :header_column_html => {:class => "my_header"} table.column :last_name, :header_column_html => {:class => "my_other_header"}, :sortable => true end xml = XmlSimple.xml_in(%%
First Name Last Name
andrew.hunter@livingsocial.comAndrewHunter
%) XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml end end describe "body block" do it "should be able to replace the body block" do buffer = @view.table_for @users[0,1] do |table| table.define :body do "My new body definition".html_safe end table.column :first_name end xml = XmlSimple.xml_in(%%
First Name
My new body definition
%) XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml end it "should be able to specify html attributes" do buffer = @view.table_for @users[0,1], :tbody_html => {:style => "background-color: orange"} xml = XmlSimple.xml_in(%%
%) XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml end end describe "data_row block" do it "should be able to replace the data_row block" do buffer = @view.table_for @users do |table| table.define :data_row do |user| "User #{user.first_name}".html_safe end table.column :first_name end xml = XmlSimple.xml_in(%%
First Name
User Andrew
User Todd
User Jon
%) XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml end it "should be able to dynamically specify row attributes" do buffer = @view.table_for @users, :data_row_html => {:class => lambda {@view.cycle("even", "odd")}, :id => lambda {|user| "user-#{user.id}"}} do |table| table.column :email end xml = XmlSimple.xml_in(%%
Email
andrew.hunter@livingsocial.com
todd.fisher@livingsocial.com
jon.phillips@livingsocial.com
%) XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml end end describe "data_column block" do it "should be able to replace the data_column block" do buffer = @view.table_for @users[0, 1] do |table| table.define :data_column do |column, user, options| "#{column.name.to_s.titleize} value is #{user.send(column.name)}".html_safe end table.column :email table.column :first_name table.column :last_name end xml = XmlSimple.xml_in(%%
EmailFirst NameLast Name
Email value is andrew.hunter@livingsocial.com First Name value is Andrew Last Name value is Hunter
%) XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml end it "should be able to specify html attributes" do buffer = @view.table_for @users[0,1], :data_column_html => {:class => "data"} do |table| table.column :first_name end xml = XmlSimple.xml_in(%%
First Name
Andrew
%) XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml end it "should be able to dynamically specify column attributes" do buffer = @view.table_for @users[0, 1], :data_column_html => {:class => lambda {@view.cycle("even", "odd")}, :id => lambda {|user, column| "#{column.name.to_s}_data"}} do |table| table.column :email table.column :first_name table.column :last_name end xml = XmlSimple.xml_in(%%
EmailFirst NameLast Name
andrew.hunter@livingsocial.com Andrew Hunter
%) XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml end it "should allow a global data_column_html option for all columns and the ability to override on a column by column basis" do buffer = @view.table_for @users[0,1], :data_column_html => {:class => lambda {|record, column| "#{column.name}_data"}} do |table| table.column :first_name, :data_column_html => {:class => "my_data"} table.column :last_name end xml = XmlSimple.xml_in(%%
First NameLast Name
Andrew Hunter
%) XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml end end # describe "edit block" do # it "should be able to replace the edit block" do # buffer = @view.table_for @users[0,1] do |table| # table.define :edit do # "Edit Link" # end # table.column :edit # end # # xml = XmlSimple.xml_in(%% # # # #
Edit Link
%) # XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml # end # # it "should be able to create an edit column" do # @view.expects(:edit_user_path).with(User.first).returns("/users/1/edit") # # buffer = @view.table_for @users[0,1] do |table| # table.column :edit # end # # xml = XmlSimple.xml_in(%% # # # #
Edit
%) # XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml # end # # it "should be able to update the label for an edit column" do # @view.expects(:edit_user_path).with(User.first).returns("/users/1/edit") # # buffer = @view.table_for @users[0,1] do |table| # table.column :edit, :data => "Modify" # end # # xml = XmlSimple.xml_in(%% # # # #
Modify
%) # XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml # end # # it "should be able to specify the action for an edit column" do # @view.expects(:modify_user_path).with(User.first).returns("/users/1/modify") # # buffer = @view.table_for @users[0,1] do |table| # table.column :edit, :action => :modify # end # # xml = XmlSimple.xml_in(%% # # # #
Edit
%) # XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml # end # # it "should be able to specify the scope for an edit column" do # @view.expects(:edit_user_test_user_path).with(User.last, User.first).returns("/users/3/test/users/1/edit") # # buffer = @view.table_for @users[0,1] do |table| # table.column :edit, :scope => [User.last, :test] # end # # xml = XmlSimple.xml_in(%% # # # #
Edit
%) # XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml # end # # it "should be able to specify the html for an edit column" do # @view.expects(:edit_user_path).with(User.first).returns("/users/1/edit") # # buffer = @view.table_for @users[0,1] do |table| # table.column :edit, :link_html => {:style => "color:red"} # end # # xml = XmlSimple.xml_in(%% # # # #
Edit
%) # XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml # end # end # # describe "show block" do # it "should be able to replace the show block" do # buffer = @view.table_for @users[0,1] do |table| # table.define :show do # "Show Link" # end # table.column :show # end # # xml = XmlSimple.xml_in(%% # # # #
Show Link
%) # XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml # end # # it "should be able to create a show column" do # @view.expects(:user_path).with(User.first).returns("/users/1") # # buffer = @view.table_for @users[0,1] do |table| # table.column :show # end # # xml = XmlSimple.xml_in(%% # # # #
Show
%) # XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml # end # # it "should be able to update the label for an show column" do # @view.expects(:user_path).with(User.first).returns("/users/1") # # buffer = @view.table_for @users[0,1] do |table| # table.column :show, :data => "Display" # end # # xml = XmlSimple.xml_in(%% # # # #
Display
%) # XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml # end # # it "should be able to specify the action for an show column" do # @view.expects(:display_user_path).with(User.first).returns("/users/1/display") # # buffer = @view.table_for @users[0,1] do |table| # table.column :show, :action => :display # end # # xml = XmlSimple.xml_in(%% # # # #
Show
%) # XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml # end # # it "should be able to specify the scope for an show column" do # @view.expects(:user_test_user_path).with(User.last, User.first).returns("/users/3/test/users/1") # # buffer = @view.table_for @users[0,1] do |table| # table.column :show, :scope => [User.last, :test] # end # # xml = XmlSimple.xml_in(%% # # # #
Show
%) # XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml # end # # it "should be able to specify the html for an show column" do # @view.expects(:user_path).with(User.first).returns("/users/1") # # buffer = @view.table_for @users[0,1] do |table| # table.column :show, :link_html => {:style => "color:red"} # end # # xml = XmlSimple.xml_in(%% # # # #
Show
%) # XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml # end # end # # describe "delete block" do # it "should be able to replace the delete block" do # buffer = @view.table_for @users[0,1] do |table| # table.define :delete do # "Delete Link" # end # table.column :delete # end # # xml = XmlSimple.xml_in(%% # # # #
Delete Link
%) # XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml # end # # it "should be able to create a delete column" do # @view.expects(:user_path).with(User.first).returns("/users/1") # # buffer = @view.table_for @users[0,1] do |table| # table.column :delete # end # # xml = XmlSimple.xml_in(%% # # # # # # # #
# Delete #
%) # XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml # end # # it "should be able to update the label for an delete column" do # @view.expects(:user_path).with(User.first).returns("/users/1") # # buffer = @view.table_for @users[0,1] do |table| # table.column :delete, :data => "Destroy" # end # # xml = XmlSimple.xml_in(%% # # # # # # # #
# Destroy #
%) # XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml # end # # it "should be able to specify the action for an delete column" do # @view.expects(:dispose_user_path).with(User.first).returns("/users/1/dispose") # # buffer = @view.table_for @users[0,1] do |table| # table.column :delete, :action => :dispose # end # # xml = XmlSimple.xml_in(%% # # # # # # # #
# Delete #
%) # XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml # end # # it "should be able to specify the scope for a delete column" do # @view.expects(:user_test_user_path).with(User.last, User.first).returns("/users/3/test/users/1") # # buffer = @view.table_for @users[0,1] do |table| # table.column :delete, :scope => [User.last, :test] # end # # xml = XmlSimple.xml_in(%% # # # # # # # #
# Delete #
%) # XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml # end # # it "should be able to specify the html for a delete column" do # @view.expects(:user_path).with(User.first).returns("/users/1") # # buffer = @view.table_for @users[0,1] do |table| # table.column :delete, :link_html => {:style => "color:red"} # end # # xml = XmlSimple.xml_in(%% # # # # # # # #
# Delete #
%) # XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml # end # # it "should be able to override the delete confirmation message for a delete link" do # @view.expects(:user_path).with(User.first).returns("/users/1") # # buffer = @view.table_for @users[0,1] do |table| # table.column :delete, :confirm => "Are you sure?" # end # # xml = XmlSimple.xml_in(%% # # # # # # # #
# Delete #
%) # XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml # end # # it "should be able to override the method for a delete link" do # @view.expects(:user_path).with(User.first).returns("/users/1") # # buffer = @view.table_for @users[0,1] do |table| # table.column :delete, :method => :get # end # # xml = XmlSimple.xml_in(%% # # # # # # # #
# Delete #
%) # XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml # end # end describe "column data contents block" do it "should be able to replace an individual column data contents block" do buffer = @view.table_for @users[0,1] do |table| table.column :email, :header => "Email Address" table.column :header => "Full Name" do |user| "#{user.first_name} #{user.last_name}" end end xml = XmlSimple.xml_in(%%
Email AddressFull Name
andrew.hunter@livingsocial.comAndrew Hunter
%) XmlSimple.xml_in(buffer, 'NormaliseSpace' => 2).should eql xml end end end