Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

hashes array support added

  • Loading branch information...
commit 93f942abc98fd7bdbb8e29166a128187fc4c4ed3 1 parent ef944e4
@lunich authored
View
3  lib/table_for/callback_column.rb
@@ -4,9 +4,8 @@ def initialize(template, records, obj, ops)
super
@callback = @options.delete(:callback)
end
-
def content_for(record)
- @attr ? @callback.call(record.send(@attr)) : @callback.call(record)
+ @attr ? @callback.call(record.kind_of?(Hash) ? record[@attr] : record.send(@attr)) : @callback.call(record)
end
end
end
View
2  lib/table_for/simple_column.rb
@@ -1,7 +1,7 @@
module TableHelper
class SimpleColumn < Column # :nodoc:
def content_for(record)
- called_record = record.send(@attr)
+ called_record = record.kind_of?(Hash) ? record[@attr] : record.send(@attr)
if @options[:time_format] and called_record.is_a? Time
called_record = called_record.strftime(@options[:time_format])
end
View
8 lib/table_for/table.rb
@@ -2,7 +2,7 @@
module TableHelper
class Table # :nodoc:
- delegate :content_tag, :content_tag_for, :to => :@template
+ delegate :content_tag, :content_tag_for, :dom_id, :dom_class, :to => :@template
def initialize(template, records, options = {})
@template, @records, @columns = template, records, []
@@ -84,12 +84,14 @@ def tr_options(rec)
res = @tr_html_options.nil? ? {} : @tr_html_options.clone
html_class = @stripes.next
unless html_class.nil?
- res[:class] = [dom_class(rec), res[:class], html_class].compact.join(" ")
+ klasses = [res[:class], html_class]
+ klasses << dom_class(rec) if rec.respond_to?(:model_name)
+ res[:class] = klasses.compact.join(" ")
end
if res.has_key?(:id) && res[:id].respond_to?(:call)
res[:id] = res[:id].call(rec)
end
- res[:id] ||= dom_id(rec)
+ res[:id] ||= dom_id(rec) if rec.respond_to?(:to_key)
res
end
View
32 spec/table_for/callback_column_spec.rb
@@ -10,6 +10,11 @@
# call-proc
let(:given_proc_without_attr) { lambda { |r| "aaa-#{r.id}" }}
let(:given_proc_with_attr) { lambda { |r| "aaa-#{r}" }}
+ # user hash
+ let(:user_hash) { { :id => 12 } }
+ # call-proc
+ let(:given_proc_without_attr_for_hash) { lambda { |r| "aaa-#{r[:id]}" }}
+ let(:given_proc_with_attr_for_hash) { lambda { |r| "aaa-#{r}" }}
# Instance methods
describe "an instance" do
it_should_behave_like "Column class instance"
@@ -28,15 +33,26 @@
col.title.should eq("Id")
end
end
-
# :content_for
- it ":content_for method should success without given attribute" do
- col = build_column(klass, nil, :callback => given_proc_without_attr)
- col.content_for(user).should == given_proc_without_attr.call(user)
+ context "user object" do
+ it ":content_for method should success without given attribute" do
+ col = build_column(klass, nil, :callback => given_proc_without_attr)
+ col.content_for(user).should == "aaa-12"
+ end
+ it ":content_for method should success with given attribute" do
+ col = build_column(klass, :id, :callback => given_proc_with_attr)
+ col.content_for(user).should == "aaa-12"
+ end
end
- it ":content_for method should success with given attribute" do
- col = build_column(klass, :id, :callback => given_proc_with_attr)
- col.content_for(user).should == given_proc_with_attr.call(user.id)
+ context "user hash" do
+ it ":content_for method should success without given attribute" do
+ col = build_column(klass, nil, :callback => given_proc_without_attr_for_hash)
+ col.content_for(user_hash).should == "aaa-12"
+ end
+ it ":content_for method should success with given attribute" do
+ col = build_column(klass, :id, :callback => given_proc_with_attr_for_hash)
+ col.content_for(user_hash).should == "aaa-12"
+ end
end
end
-end
+end
View
6 spec/table_for/simple_column_spec.rb
@@ -7,6 +7,8 @@
let(:klass) { TableHelper::SimpleColumn }
# user (stubbed data)
let(:user) { mock(:id => 12, :created_at => Time.gm(2011, "feb", 24, 14, 23, 1)) }
+ # user (hash)
+ let(:user_hash) { { :id => 12, :created_at => Time.gm(2011, "feb", 24, 14, 23, 1) } }
# Instance methods
describe "an instance" do
it_should_behave_like "Column class instance"
@@ -24,13 +26,15 @@
# :content_for
it ":content_for method should success" do
col = build_column(klass, :id)
- col.content_for(user).should == user.id.to_s
+ col.content_for(user).should == "12"
+ col.content_for(user_hash).should == "12"
end
describe "format" do
it ":time_format should be displayed correctly" do
col = build_column(klass, :created_at, :time_format => '%Y-%m')
col.content_for(user).should == "2011-02"
+ col.content_for(user_hash).should == "2011-02"
end
end
end
View
390 spec/table_for_collection_spec.rb
@@ -5,66 +5,174 @@
let(:template) do
ActionView::Base.new
end
- # users list (stubbed data)
- let(:users) do
- [
- User.new({
- :id => 1209,
- :name => "John Smith",
- :email => "smith@matrix.net",
- :address => "100, Spear Street, NY, USA"
- }),
- User.new({
- :id => 2123,
- :name => "Thomas Anderson",
- :email => "neo@matrix.net",
- :address => "200, Spear Street, NY, USA"
- }),
- User.new({
- :id => 3323,
- :name => "Trinity",
- :email => "trinity@matrix.net",
- :address => "300, Spear Street, NY, USA"
- }),
- User.new({
- :id => 4912,
- :name => "Morpheus",
- :email => "morpheus@matrix.net",
- :address => "400, Spear Street, NY, USA"
- })
- ]
- end
# check if method available
it "should respond to :table_for" do
template.should respond_to(:table_for)
end
-
- # main method
- describe ":table_for method" do
- # <%= table_for @users %>
- it "should raise if no block given" do
- lambda do
- template.table_for(users)
- end.should raise_error(ArgumentError)
+ # <%= table_for @users %>
+ it "should raise if no block given" do
+ lambda do
+ template.table_for([])
+ end.should raise_error(ArgumentError)
+ end
+ # users list (hashes array)
+ context "for hashes array" do
+ let(:users) do
+ [
+ { :hits => 87.0, :id => 1.0 },
+ { :hits => 86.0, :id => 5.0 },
+ { :hits => 78.0, :id => 6.0 },
+ { :hits => 78.0, :id => 3.0 },
+ ]
+ end
+ # <%= table_for @users, :html => { :id => "users", :class => "simple-table" } do %>
+ # <% column :name %>
+ # <% end %>
+ describe "with given :html options" do
+ before(:each) do
+ @html = template.table_for(users, :html => { :id => "users", :class => "simple-table" }) do
+ column :hits
+ end
+ end
+ it "should render specialized table" do
+ @html.should have_selector("table#users.simple-table")
+ end
end
+ # <%= table_for @users, :stripes => ["odd", "even"] do %>
+ # <% column :name %>
+ # <% end %>
+ describe "with cycling stripes" do
+ before(:each) do
+ @html = template.table_for(users, :stripes => %w{s-one s-two s-three}) do
+ column :name
+ end
+ end
+
+ it "should have valid classes" do
+ @html.should have_selector("tr.s-one", :count => 2)
+ @html.should have_selector("tr.s-two", :count => 1)
+ @html.should have_selector("tr.s-three", :count => 1)
+ end
+ end
+
# <%= table_for @users do %>
+ # <% columns :hits, :id %>
+ # <% end %>
+ describe "with columns" do
+ before(:each) do
+ @html = template.table_for(users) do
+ columns :hits, :id
+ end
+ end
+ it "should render valid HTML" do
+ @html.should have_selector("table") do |table|
+ table.should have_selector("thead/tr") do |tr|
+ ["Id", "Hits"].each do |field|
+ tr.should have_selector("th") do |th|
+ th.should contain(field)
+ end
+ end
+ end
+ end
+ end
+ end
+ describe "with named callback column" do
+ before(:each) do
+ @html = template.table_for(users) do
+ column :id do |id|
+ mail_to id
+ end
+ end
+ end
+ it "should render valid HTML" do
+ @html.should have_selector("table") do |table|
+ table.should have_selector("thead/tr/th") do |th|
+ th.should contain("Id")
+ end
+ table.should have_selector("tbody/tr") do |tr|
+ users.each do |user|
+ tr.should have_selector("td") do |td|
+ td.should have_selector("a[@href='mailto:#{user[:id]}']")
+ end
+ end
+ end
+ end
+ end
+ end
+ # <%= table_for @users do %>
+ # <% column :title => "User's name" do |user| %>
+ # <% content_tag :div do %>
+ # <% [user.first_name, user.last_name].join(" ") %>
+ # <% end %>
+ # <% end %>
+ # <% end %>
+ describe "with callback column" do
+ before(:each) do
+ @html = template.table_for(users) do
+ column :id
+ column :title => "HITS" do |user|
+ content_tag :div do
+ user[:hits].to_i.to_s
+ end
+ end
+ end
+ end
+ it "should render valid HTML" do
+ @html.should have_selector("table") do |table|
+ table.should have_selector("thead/tr/th") do |th|
+ th.should contain("HITS")
+ end
+ table.should have_selector("tbody/tr") do |tr|
+ users.each do |user|
+ tr.should have_selector("td") do |td|
+ td.should contain(user[:id].to_s)
+ end
+ tr.should have_selector("td/div") do |td|
+ td.should contain(user[:hits].to_i.to_s)
+ end
+ end
+ end
+ end
+ end
+ end
+ # <%= table_for @users, :stripes => ["odd", "even"], :html => { :tr => { :class => "table-row" } } do %>
# <% column :name %>
# <% end %>
- describe "with simple column" do
+ describe "with tr html" do
+ subject { template.table_for(users, :stripes => ["odd", "even"], :html => { :tr => { :class => "table-row", :id => lambda { |f| "user_#{f[:id].to_i}" } } }) do
+ column :hits
+ end
+ }
+ it "should have valid classes" do
+ should have_selector("tr.odd.table-row", :count => 2)
+ should have_selector("tr.even.table-row", :count => 2)
+ end
+ it "should have valid ids" do
+ users.each do |user|
+ should have_selector("tr.table-row#user_#{user[:id].to_i}") do |tr|
+ tr.should contain(user.hits.to_s)
+ end
+ end
+ end
+ end
+ # <%= table_for @users do %>
+ # <% column :name, :title => "User's name" %>
+ # <% end %>
+ describe "with titled column" do
before(:each) do
@html = template.table_for(users) do
- column :name
+ column :hits, :title => "user's hits"
end
end
it "should render valid HTML" do
@html.should have_selector("table") do |table|
table.should have_selector("thead/tr/th") do |th|
- th.should contain("Name")
+ th.should contain("user's hits")
end
table.should have_selector("tbody/tr") do |tr|
users.each do |user|
tr.should have_selector("td") do |td|
- td.should contain(user.name)
+ td.should contain(user[:hits].to_s)
end
end
end
@@ -72,23 +180,23 @@
end
end
# <%= table_for @users do %>
- # <% column :name, :html => { :width => "50%" } %>
+ # <% column :name %>
# <% end %>
- describe "with column :html options" do
+ describe "with simple column" do
before(:each) do
@html = template.table_for(users) do
- column :id, :html => { :td => { :class => "user-id" }, :th => { :width => "50%" }}
+ column :hits
end
end
it "should render valid HTML" do
@html.should have_selector("table") do |table|
- table.should have_selector("thead/tr") do |tr|
- tr.should have_selector("th[@width='50%']")
+ table.should have_selector("thead/tr/th") do |th|
+ th.should contain("Hits")
end
table.should have_selector("tbody/tr") do |tr|
users.each do |user|
- tr.should have_selector("td.user-id") do |td|
- td.should contain(user.id.to_s)
+ tr.should have_selector("td") do |td|
+ td.should contain(user[:hits].to_s)
end
end
end
@@ -96,27 +204,60 @@
end
end
# <%= table_for @users do %>
- # <% columns :name, :email, :address %>
+ # <% column :name, :html => { :width => "50%" } %>
# <% end %>
- describe "with columns" do
+ describe "with column :html options" do
before(:each) do
@html = template.table_for(users) do
- columns :id, :name, :email, :address
+ column :id, :html => { :td => { :class => "user-id" }, :th => { :width => "50%" }}
end
end
it "should render valid HTML" do
@html.should have_selector("table") do |table|
table.should have_selector("thead/tr") do |tr|
- ["Id", "Name", "Email", "Address"].each do |field|
- tr.should have_selector("th") do |th|
- th.should contain(field)
+ tr.should have_selector("th[@width='50%']")
+ end
+ table.should have_selector("tbody/tr") do |tr|
+ users.each do |user|
+ tr.should have_selector("td.user-id") do |td|
+ td.should contain(user[:id].to_s)
end
end
end
end
end
end
-
+ end
+ # users list (objects array)
+ context "for objects array" do
+ let(:users) do
+ [
+ User.new({
+ :id => 1209,
+ :name => "John Smith",
+ :email => "smith@matrix.net",
+ :address => "100, Spear Street, NY, USA"
+ }),
+ User.new({
+ :id => 2123,
+ :name => "Thomas Anderson",
+ :email => "neo@matrix.net",
+ :address => "200, Spear Street, NY, USA"
+ }),
+ User.new({
+ :id => 3323,
+ :name => "Trinity",
+ :email => "trinity@matrix.net",
+ :address => "300, Spear Street, NY, USA"
+ }),
+ User.new({
+ :id => 4912,
+ :name => "Morpheus",
+ :email => "morpheus@matrix.net",
+ :address => "400, Spear Street, NY, USA"
+ })
+ ]
+ end
# <%= table_for @users, :html => { :id => "users", :class => "simple-table" } do %>
# <% column :name %>
# <% end %>
@@ -130,30 +271,68 @@
@html.should have_selector("table#users.simple-table")
end
end
+ # <%= table_for @users, :stripes => ["odd", "even"] do %>
+ # <% column :name %>
+ # <% end %>
+ describe "with cycling stripes" do
+ before(:each) do
+ @html = template.table_for(users, :stripes => %w{s-one s-two s-three}) do
+ column :name
+ end
+ end
+
+ it "should have valid classes" do
+ @html.should have_selector("tr.s-one", :count => 2)
+ @html.should have_selector("tr.s-two", :count => 1)
+ @html.should have_selector("tr.s-three", :count => 1)
+ end
+ end
+
# <%= table_for @users do %>
- # <% column :name, :title => "User's name" %>
+ # <% columns :name, :email, :address %>
# <% end %>
- describe "with titled column" do
+ describe "with columns" do
before(:each) do
@html = template.table_for(users) do
- column :email, :title => "Email address"
+ columns :id, :name, :email, :address
+ end
+ end
+ it "should render valid HTML" do
+ @html.should have_selector("table") do |table|
+ table.should have_selector("thead/tr") do |tr|
+ ["Id", "Name", "Email", "Address"].each do |field|
+ tr.should have_selector("th") do |th|
+ th.should contain(field)
+ end
+ end
+ end
+ end
+ end
+ end
+ describe "with named callback column" do
+ before(:each) do
+ @html = template.table_for(users) do
+ column :email do |email|
+ mail_to email
+ end
end
end
it "should render valid HTML" do
@html.should have_selector("table") do |table|
table.should have_selector("thead/tr/th") do |th|
- th.should contain("Email address")
+ th.should contain("Email")
end
table.should have_selector("tbody/tr") do |tr|
users.each do |user|
tr.should have_selector("td") do |td|
- td.should contain(user.email)
+ td.should have_selector("a[@href='mailto:#{user.email}']")
end
end
end
end
end
end
+
# <%= table_for @users do %>
# <% column :title => "User's name" do |user| %>
# <% content_tag :div do %>
@@ -190,65 +369,94 @@
end
end
end
-
- describe "with named callback column" do
+ # <%= table_for @users, :stripes => ["odd", "even"], :html => { :tr => { :class => "table-row" } } do %>
+ # <% column :name %>
+ # <% end %>
+ describe "with tr html" do
+ subject { template.table_for(users, :stripes => ["odd", "even"], :html => { :tr => { :class => "table-row", :id => lambda { |f| "user_#{f.id}" } } }) do
+ column :name
+ end
+ }
+ it "should have valid classes" do
+ should have_selector("tr.odd.table-row", :count => 2)
+ should have_selector("tr.even.table-row", :count => 2)
+ end
+ it "should have valid ids" do
+ users.each do |user|
+ should have_selector("tr.table-row#user_#{user.id}") do |tr|
+ tr.should contain(user.name)
+ end
+ end
+ end
+ end
+ # <%= table_for @users do %>
+ # <% column :name, :title => "User's name" %>
+ # <% end %>
+ describe "with titled column" do
before(:each) do
@html = template.table_for(users) do
- column :email do |email|
- mail_to email
- end
+ column :email, :title => "Email address"
end
end
-
it "should render valid HTML" do
@html.should have_selector("table") do |table|
table.should have_selector("thead/tr/th") do |th|
- th.should contain("Email")
+ th.should contain("Email address")
end
table.should have_selector("tbody/tr") do |tr|
users.each do |user|
tr.should have_selector("td") do |td|
- td.should have_selector("a[@href='mailto:#{user.email}']")
+ td.should contain(user.email)
end
end
end
end
end
end
-
- # <%= table_for @users, :stripes => ["odd", "even"] do %>
+ # <%= table_for @users do %>
# <% column :name %>
# <% end %>
- describe "with cycling stripes" do
+ describe "with simple column" do
before(:each) do
- @html = template.table_for(users, :stripes => %w{s-one s-two s-three}) do
+ @html = template.table_for(users) do
column :name
end
end
-
- it "should have valid classes" do
- @html.should have_selector("tr.s-one#new_user", :count => 2)
- @html.should have_selector("tr.s-two#new_user", :count => 1)
- @html.should have_selector("tr.s-three#new_user", :count => 1)
+ it "should render valid HTML" do
+ @html.should have_selector("table") do |table|
+ table.should have_selector("thead/tr/th") do |th|
+ th.should contain("Name")
+ end
+ table.should have_selector("tbody/tr") do |tr|
+ users.each do |user|
+ tr.should have_selector("td") do |td|
+ td.should contain(user.name)
+ end
+ end
+ end
+ end
end
end
-
- # <%= table_for @users, :stripes => ["odd", "even"], :html => { :tr => { :class => "table-row" } } do %>
- # <% column :name %>
+ # <%= table_for @users do %>
+ # <% column :name, :html => { :width => "50%" } %>
# <% end %>
- describe "with tr html" do
- subject { template.table_for(users, :stripes => ["odd", "even"], :html => { :tr => { :class => "table-row", :id => lambda { |f| "user_#{f.id}" } } }) do
- column :name
+ describe "with column :html options" do
+ before(:each) do
+ @html = template.table_for(users) do
+ column :id, :html => { :td => { :class => "user-id" }, :th => { :width => "50%" }}
end
- }
- it "should have valid classes" do
- should have_selector("tr.odd.table-row", :count => 2)
- should have_selector("tr.even.table-row", :count => 2)
end
- it "should have valid ids" do
- users.each do |user|
- should have_selector("tr.table-row#user_#{user.id}") do |tr|
- tr.should contain(user.name)
+ it "should render valid HTML" do
+ @html.should have_selector("table") do |table|
+ table.should have_selector("thead/tr") do |tr|
+ tr.should have_selector("th[@width='50%']")
+ end
+ table.should have_selector("tbody/tr") do |tr|
+ users.each do |user|
+ tr.should have_selector("td.user-id") do |td|
+ td.should contain(user.id.to_s)
+ end
+ end
end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.