tabletastic 0.2.0.pre3 → 0.2.0.pre4

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.rdoc CHANGED
@@ -3,6 +3,7 @@ Git (for upcoming v0.2.0 release)
3
3
  * Updating to Rails3 API and idioms
4
4
  * Adding default table html options
5
5
  * Adding default lambda (table block) so that table_for can be called without passing a block
6
+ * Adding has_one associations
6
7
 
7
8
  v0.1.3 (Dec 28, 2009)
8
9
 
@@ -14,8 +14,8 @@ module Tabletastic
14
14
  private
15
15
  # Finds the class representing the objects within the collection
16
16
  def default_class_for(collection)
17
- if collection.respond_to?(:proxy_reflection)
18
- collection.proxy_reflection.klass
17
+ if collection.respond_to?(:klass) # ActiveRecord::Relation
18
+ collection.klass
19
19
  elsif !collection.empty?
20
20
  collection.first.class
21
21
  end
@@ -36,4 +36,3 @@ end
36
36
  ActiveSupport.on_load(:action_view) do
37
37
  include Tabletastic::Helper
38
38
  end
39
-
@@ -31,7 +31,7 @@ module Tabletastic
31
31
  @table_fields = args.empty? ? active_record_fields : args.collect {|f| TableField.new(f.to_sym)}
32
32
  end
33
33
  action_cells(options[:actions], options[:action_prefix])
34
- [head, body].join("").html_safe
34
+ ["\n", head, "\n", body, "\n"].join("").html_safe
35
35
  end
36
36
 
37
37
  # individually specify a column, which will build up the header,
@@ -77,11 +77,11 @@ module Tabletastic
77
77
 
78
78
  def body
79
79
  content_tag(:tbody) do
80
- @collection.inject("") do |rows, record|
80
+ @collection.inject("\n") do |rows, record|
81
81
  rowclass = @template.cycle("odd","even")
82
82
  rows += @template.content_tag_for(:tr, record, :class => rowclass) do
83
83
  cells_for_row(record)
84
- end
84
+ end + "\n"
85
85
  end
86
86
  end
87
87
  end
@@ -133,7 +133,10 @@ module Tabletastic
133
133
 
134
134
  def active_record_association_reflections
135
135
  return [] unless klass.respond_to?(:reflect_on_all_associations)
136
- associations = klass.reflect_on_all_associations(:belongs_to).map(&:name)
136
+ associations = []
137
+ associations += klass.reflect_on_all_associations(:belongs_to).map(&:name)
138
+ associations += klass.reflect_on_all_associations(:has_one).map(&:name)
139
+ associations
137
140
  end
138
141
 
139
142
  def content_tag(name, content = nil, options = nil, escape = true, &block)
@@ -1,3 +1,3 @@
1
1
  module Tabletastic
2
- VERSION = "0.2.0.pre3"
2
+ VERSION = "0.2.0.pre4"
3
3
  end
data/spec/spec_helper.rb CHANGED
@@ -55,6 +55,8 @@ Spork.prefork do
55
55
  end
56
56
  class ::Author
57
57
  end
58
+ class ::Profile
59
+ end
58
60
 
59
61
  def mock_everything
60
62
  def post_path(post); "/posts/#{post.id}"; end
@@ -71,16 +73,22 @@ Spork.prefork do
71
73
  ::Post.stub!(:human_attribute_name).and_return { |column_name| column_name.humanize }
72
74
  ::Post.stub!(:model_name).and_return(ActiveModel::Name.new(::Post))
73
75
 
74
- @fred = mock('user')
76
+ @fred = mock('author', :to_key => nil)
75
77
  @fred.stub!(:class).and_return(::Author)
76
78
  @fred.stub!(:name).and_return('Fred Smith')
77
79
  @fred.stub!(:id).and_return(37)
78
80
 
81
+ @profile = mock('profile')
82
+ @profile.stub!(:author).and_return(@fred)
83
+ @profile.stub!(:bio).and_return("This is my bio")
84
+ @fred.stub!(:profile).and_return(@profile)
85
+
86
+ ::Author.stub!(:content_columns).and_return([mock('column', :name => "name")])
87
+
79
88
  ::Author.stub!(:find).and_return([@fred])
80
89
  ::Author.stub!(:human_attribute_name).and_return { |column_name| column_name.humanize }
81
90
  ::Author.stub!(:human_name).and_return('Author')
82
91
  ::Author.stub!(:model_name).and_return(ActiveModel::Name.new(::Author))
83
- ::Author.stub!(:reflect_on_association).and_return { |column_name| mock('reflection', :options => {}, :klass => Post, :macro => :has_many) if column_name == :posts }
84
92
 
85
93
  @freds_post = mock('post')
86
94
  @freds_post.stub!(:class).and_return(::Post)
@@ -93,13 +101,22 @@ Spork.prefork do
93
101
  @fred.stub!(:posts).and_return([@freds_post])
94
102
  @fred.stub!(:post_ids).and_return([@freds_post.id])
95
103
 
96
- @mock_reflection_belongs_to_author = mock('reflection', :options => {}, :name => :author, :klass => ::Author, :macro => :belongs_to)
104
+ @mock_reflection_belongs_to_author = mock('reflection1', :options => {}, :name => :author, :macro => :belongs_to, :collection => false)
105
+
106
+ @mock_reflection_has_one_profile = mock('reflection2', :options => {}, :name => :profile, :macro => :has_one, :collection => false)
97
107
 
98
108
  ::Post.stub!(:reflect_on_association).and_return do |column_name|
99
109
  @mock_reflection_belongs_to_author if column_name == :author
100
110
  end
101
111
 
102
- ::Post.stub!(:reflect_on_all_associations).with(:belongs_to).and_return([])
112
+ ::Author.stub!(:reflect_on_association).and_return do |column_name|
113
+ mock('reflection', :options => {}, :klass => Post, :macro => :has_many) if column_name == :posts
114
+ @mock_reflection_has_one_profile if column_name == :profile
115
+ end
116
+
117
+
118
+ ::Post.stub!(:reflect_on_all_associations).and_return([])
119
+ ::Author.stub!(:reflect_on_all_associations).and_return([])
103
120
  end
104
121
  end
105
122
 
@@ -71,14 +71,22 @@ describe Tabletastic::TableBuilder do
71
71
  end
72
72
 
73
73
  context "when collection has associations" do
74
+ before do
75
+ @output_buffer = ActiveSupport::SafeBuffer.new
76
+ end
74
77
  it "should handle belongs_to associations" do
75
78
  ::Post.stub!(:reflect_on_all_associations).with(:belongs_to).and_return([@mock_reflection_belongs_to_author])
76
79
  @posts = [@freds_post]
77
- @output_buffer = ""
78
80
  concat table_for(@posts) { |t| t.data }
79
81
  output_buffer.should have_table_with_tag("th", "Author")
80
82
  output_buffer.should have_table_with_tag("td", "Fred Smith")
81
83
  end
84
+
85
+ it "should handle has_one associations" do
86
+ ::Author.stub!(:reflect_on_all_associations).with(:has_one).and_return([@mock_reflection_has_one_profile])
87
+ concat table_for([@fred]) { |t| t.data }
88
+ output_buffer.should have_table_with_tag("th", "Profile")
89
+ end
82
90
  end
83
91
  end
84
92
 
@@ -290,7 +298,7 @@ describe Tabletastic::TableBuilder do
290
298
  end
291
299
  end
292
300
  end
293
-
301
+
294
302
  context "using human_attribute_names" do
295
303
  it "should work" do
296
304
  ::Post.stub!(:human_attribute_name).with('body').and_return("Blah blue")
@@ -305,7 +313,7 @@ describe Tabletastic::TableBuilder do
305
313
  output_buffer.should have_table_with_tag("th", "Blah blue")
306
314
  end
307
315
  end
308
-
316
+
309
317
  context "when table_for is not passed a block" do
310
318
  it "the data should use the default option" do
311
319
  Tabletastic.default_table_block = lambda {|table| table.data}
@@ -29,4 +29,10 @@ describe Tabletastic::TableField do
29
29
  end
30
30
  tf.cell_data("hello").should == "HELLO"
31
31
  end
32
+
33
+ it "should return normal, non html-safe strings" do
34
+ post = mock(:booya => 'crazy')
35
+ tf = TableField.new(:booya)
36
+ tf.cell_data(post).should_not be_html_safe
37
+ end
32
38
  end
@@ -24,6 +24,26 @@ describe "Tabletastic#table_for" do
24
24
  end
25
25
  end
26
26
 
27
+ describe "guessing its class collection" do
28
+ before do
29
+ mock_everything
30
+ end
31
+
32
+ it "should find the class of the collection (ActiveRecord::Relation)" do
33
+ table = mock(Arel::Table)
34
+ collection = ActiveRecord::Relation.new(@post.class, table)
35
+ collection.stub!(:build_arel => table)
36
+ klass = Tabletastic::TableBuilder.send(:default_class_for, collection)
37
+ klass.should == Post
38
+ end
39
+
40
+ it "should find the class of the collection (Array of AR objects)" do
41
+ collection = [@post]
42
+ klass = Tabletastic::TableBuilder.send(:default_class_for, collection)
43
+ klass.should == Post
44
+ end
45
+ end
46
+
27
47
  describe "default options" do
28
48
  before do
29
49
  Tabletastic.default_table_html = {:class => 'default', :cellspacing => 0}
@@ -40,7 +60,7 @@ describe "Tabletastic#table_for" do
40
60
  output_buffer.should_not have_tag("table.default")
41
61
  end
42
62
  end
43
-
63
+
44
64
  describe "without a block" do
45
65
  it "should use default block" do
46
66
  concat table_for([])
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tabletastic
3
3
  version: !ruby/object:Gem::Version
4
- hash: -1876988178
4
+ hash: -1876988177
5
5
  prerelease: true
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
9
  - 0
10
- - pre3
11
- version: 0.2.0.pre3
10
+ - pre4
11
+ version: 0.2.0.pre4
12
12
  platform: ruby
13
13
  authors:
14
14
  - Joshua Davey
@@ -16,7 +16,7 @@ autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2010-06-28 00:00:00 -05:00
19
+ date: 2010-07-18 00:00:00 -05:00
20
20
  default_executable:
21
21
  dependencies:
22
22
  - !ruby/object:Gem::Dependency
@@ -27,13 +27,13 @@ dependencies:
27
27
  requirements:
28
28
  - - ">="
29
29
  - !ruby/object:Gem::Version
30
- hash: -1848230022
30
+ hash: -1848230024
31
31
  segments:
32
32
  - 3
33
33
  - 0
34
34
  - 0
35
- - beta2
36
- version: 3.0.0.beta2
35
+ - beta4
36
+ version: 3.0.0.beta4
37
37
  type: :runtime
38
38
  version_requirements: *id001
39
39
  - !ruby/object:Gem::Dependency