Permalink
Browse files

#140 fixed hash-backed enumerations so the correct value is displayed…

… in the show/list views
  • Loading branch information...
matthew
matthew committed Mar 17, 2008
1 parent 4865d87 commit 4311503142969d0a69774ffe7f45682e077def52
Showing with 54 additions and 3 deletions.
  1. +10 −0 Rakefile
  2. +9 −0 lib/streamlined/column/active_record.rb
  3. +35 −3 test/unit/streamlined/column/active_record_test.rb
View
@@ -1,6 +1,16 @@
require 'rake'
require 'rake/testtask'
require 'rake/rdoctask'
+
+# Suppress file lists when running tests
+Rake::TestTask.class_eval do
+ alias_method :original_define, :define
+ def define
+ @verbose = false
+ original_define
+ end
+end
+
require 'test/lib/ar_helper'
load 'test/lib/multi_rails/tasks/multi_rails.rake'
load 'tasks/rcov.rake'
@@ -37,13 +37,22 @@ def edit_view
def render_td_show(view, item)
if enumeration
content = item.send(self.name)
+ if enumeration.first.is_a?(Array)
+ key_value_pair = enumeration.detect { |e| e.last == content }
+ content = key_value_pair.first if key_value_pair
+ end
content = content && !content.blank? ? content : self.unassigned_value
content = wrap_with_link(content, view, item)
else
render_content(view, item)
end
end
+ def enumeration
+ # Convert the enumeration to a 2d array if it's a hash
+ @enumeration.is_a?(Hash) ? @enumeration.to_a : @enumeration
+ end
+
def render_td_list(view, item)
id = relationship_div_id(name, item)
div = render_td_show(view, item)
@@ -136,14 +136,46 @@ def setup
assert_equal "Unassigned", @ar.render_td_show(@view, item)
end
- it "render td with enumeration" do
+ it "render td with array-backed enumeration" do
setup_mocks
@ar.enumeration = %w{ A B C }
@view.should_receive(:crud_context).and_return(:list).once
expected = "<div id=\"EnumerableSelect::column::123::\">render</div>link"
assert_equal expected, @ar.render_td(@view, @item)
end
+ it "render td with hash-backed enumeration" do
+ setup_mocks(:column => 1)
+ @ar.enumeration = { "A" => 1, "B" => 2, "C" => 3 }
+ @view.should_receive(:crud_context).and_return(:list).once
+ expected = "<div id=\"EnumerableSelect::column::123::\">A</div>link"
+ assert_equal expected, @ar.render_td(@view, @item)
+ end
+
+ it "render td with hash-backed enumeration and nil column value" do
+ setup_mocks(:column => nil)
+ @ar.enumeration = { "A" => 1, "B" => 2, "C" => 3 }
+ @view.should_receive(:crud_context).and_return(:list).once
+ expected = "<div id=\"EnumerableSelect::column::123::\">Unassigned</div>link"
+ assert_equal expected, @ar.render_td(@view, @item)
+ end
+
+ it "render td with 2d array-backed enumeration" do
+ setup_mocks(:column => 1)
+ @ar.enumeration = [["A", 1], ["B", 2], ["C", 3]]
+ @view.should_receive(:crud_context).and_return(:list).once
+ expected = "<div id=\"EnumerableSelect::column::123::\">A</div>link"
+ assert_equal expected, @ar.render_td(@view, @item)
+ end
+
+ it "render td with 2d array-backed enumeration and nil column value" do
+ setup_mocks(:column => nil)
+ @ar.enumeration = [["A", 1], ["B", 2], ["C", 3]]
+ @view.should_receive(:crud_context).and_return(:list).once
+ expected = "<div id=\"EnumerableSelect::column::123::\">Unassigned</div>link"
+ assert_equal expected, @ar.render_td(@view, @item)
+ end
+
it "render td list with enumeration and link" do
setup_mocks
@ar.enumeration = %w{ A B C }
@@ -223,8 +255,8 @@ def ar_column(name, human_name)
flexmock(:name => name, :human_name => human_name)
end
- def setup_mocks
+ def setup_mocks(item_attrs={})
@view = flexmock(:controller_name => 'controller_name', :link_to_function => 'link')
- @item = flexmock(:id => 123, :column => 'render')
+ @item = flexmock(item_attrs.reverse_merge(:id => 123, :column => 'render'))
end
end

0 comments on commit 4311503

Please sign in to comment.