diff --git a/app/helpers/profile_helper.rb b/app/helpers/profile_helper.rb new file mode 100644 index 0000000..366f1f9 --- /dev/null +++ b/app/helpers/profile_helper.rb @@ -0,0 +1,28 @@ +module ProfileHelper + + def display_profile_info(profile) + info = profile.info + if info.nil? + content_tag('div', _('This profile does not have any public information')) + else + table_rows = '' + info.each do |item| + name = item[0] + value = item[1] + if value.is_a?(Proc) + value = self.instance_eval(value) + end + + table_rows << content_tag('tr', content_tag('th', name) + content_tag('td', value)) + table_rows << "\n" + end + + content_tag( + 'table', + table_rows, + :class => 'profile_info' + ) + end + end + +end diff --git a/test/unit/profile_helper_test.rb b/test/unit/profile_helper_test.rb new file mode 100644 index 0000000..62f455b --- /dev/null +++ b/test/unit/profile_helper_test.rb @@ -0,0 +1,53 @@ +require File.dirname(__FILE__) + '/../test_helper' + +class ProfileHelperTest < Test::Unit::TestCase + + def setup + @profile = Profile.new + @helper = mock + helper.extend(ProfileHelper) + end + attr_reader :profile, :helper + + def test_should_ignore_nil + profile.stubs(:info).returns(nil) + + helper.expects(:content_tag) + helper.expects(:_) + + helper.display_profile_info(profile) + end + + def test_should_display_info + f1 = 'Field 1' + v1 = 'value 1' + f2 = 'Field 2' + v2 = 'value 2' + array = [ + [ f1, v1 ], + [ f2, v2 ] + ] + profile.stubs(:info).returns(array) + + helper.expects(:content_tag).with('th', f1).returns(f1) + helper.expects(:content_tag).with('td', v1).returns(v1) + helper.expects(:content_tag).with('tr', f1 + v1).returns('r1') + helper.expects(:content_tag).with('th', f2).returns(f2) + helper.expects(:content_tag).with('td', v2).returns(v2) + helper.expects(:content_tag).with('tr', f2 + v2).returns('r2') + helper.expects(:content_tag).with('table', "r1\nr2\n", :class => 'profile_info' ).returns('final') + + assert_equal 'final', helper.display_profile_info(profile) + end + + def test_should_call_blocks + myproc = lambda { content_tag('div', 'lalala') } + profile.stubs(:info).returns([['f1', myproc ]]) + helper.stubs(:content_tag).returns('') + + helper.expects(:instance_eval).with(myproc) + + helper.display_profile_info(profile) + end + +end -- libgit2 0.21.2