Commit e0f4d9eeac9cc52902c188c27aa4c6ea634155db

Authored by AntonioTerceiro
1 parent 1225579e

ActionItem7: redesigning relation between profile and info

git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@455 3f533792-8f58-4932-b0fe-aaf55b0a4547
app/controllers/profile_editor_controller.rb
1 class ProfileEditorController < ApplicationController 1 class ProfileEditorController < ApplicationController
2 helper :profile 2 helper :profile
  3 +
  4 + # edits the profile info (posts back)
  5 + def edit
  6 + if request.post?
  7 + else
  8 + render :action => profile.info.class.tableize
  9 + end
  10 + end
3 end 11 end
  12 +
app/helpers/profile_helper.rb
@@ -6,7 +6,7 @@ module ProfileHelper @@ -6,7 +6,7 @@ module ProfileHelper
6 content_tag('div', _('This profile does not have any public information')) 6 content_tag('div', _('This profile does not have any public information'))
7 else 7 else
8 table_rows = '' 8 table_rows = ''
9 - info.each do |item| 9 + info.summary.each do |item|
10 name = item[0] 10 name = item[0]
11 value = item[1] 11 value = item[1]
12 if value.is_a?(Proc) 12 if value.is_a?(Proc)
app/models/person.rb
@@ -13,16 +13,9 @@ class Person &lt; Profile @@ -13,16 +13,9 @@ class Person &lt; Profile
13 has_many :person_friendships 13 has_many :person_friendships
14 has_many :people, :through => :person_friendships, :foreign_key => 'friend_id' 14 has_many :people, :through => :person_friendships, :foreign_key => 'friend_id'
15 has_one :person_info 15 has_one :person_info
  16 +
16 def info 17 def info
17 - if person_info.nil?  
18 - nil  
19 - else  
20 - [  
21 - [ _('Name'), self.name ],  
22 - [ _('Address'), self.person_info.address ],  
23 - [ _('Contact Information'), self.person_info.contact_information ],  
24 - ]  
25 - end 18 + person_info
26 end 19 end
27 20
28 validates_presence_of :user_id 21 validates_presence_of :user_id
app/models/person_info.rb
@@ -2,4 +2,14 @@ class PersonInfo &lt; ActiveRecord::Base @@ -2,4 +2,14 @@ class PersonInfo &lt; ActiveRecord::Base
2 2
3 # FIXME: add file_column :photo 3 # FIXME: add file_column :photo
4 4
  5 + belongs_to :person
  6 +
  7 + def summary
  8 + [
  9 + [ _('Name'), self.person.name ],
  10 + [ _('Address'), self.address ],
  11 + [ _('Contact Information'), self.contact_information ],
  12 + ]
  13 + end
  14 +
5 end 15 end
app/models/profile.rb
@@ -73,14 +73,18 @@ class Profile &lt; ActiveRecord::Base @@ -73,14 +73,18 @@ class Profile &lt; ActiveRecord::Base
73 end 73 end
74 74
75 # Returns information about the profile's owner that was made public by 75 # Returns information about the profile's owner that was made public by
76 - # him/her. The returned value must be an array in the followinf format: 76 + # him/her.
  77 + #
  78 + # The returned value must be an object that responds to a method "summary",
  79 + # which must return an array in the following format:
77 # 80 #
78 # [ 81 # [
79 # [ 'First Field', first_field_value ], 82 # [ 'First Field', first_field_value ],
80 # [ 'Second Field', second_field_value ], 83 # [ 'Second Field', second_field_value ],
81 # ] 84 # ]
82 # 85 #
83 - # This information shall be used by user interface to present the information 86 + # This information shall be used by user interface to present the
  87 + # information.
84 # 88 #
85 # In this class, this method returns nil, what is interpreted as "no 89 # In this class, this method returns nil, what is interpreted as "no
86 # information at all". Subclasses must override this method to provide their 90 # information at all". Subclasses must override this method to provide their
test/unit/person_info_test.rb
@@ -10,4 +10,18 @@ class PersonInfoTest &lt; Test::Unit::TestCase @@ -10,4 +10,18 @@ class PersonInfoTest &lt; Test::Unit::TestCase
10 assert info.respond_to?(:contact_information) 10 assert info.respond_to?(:contact_information)
11 end 11 end
12 12
  13 + should 'provide needed information in summary' do
  14 + person_info = PersonInfo.new
  15 + person_info.person = Person.new
  16 + person_info.person.name = 'person name'
  17 +
  18 + person_info.address = 'my address'
  19 + person_info.contact_information = 'my contact information'
  20 +
  21 + summary = person_info.summary
  22 + assert(summary.any? { |line| line[1] == 'person name' })
  23 + assert(summary.any? { |line| line[1] == 'my address' })
  24 + assert(summary.any? { |line| line[1] == 'my contact information' })
  25 + end
  26 +
13 end 27 end
test/unit/person_test.rb
@@ -66,14 +66,9 @@ class PersonTest &lt; Test::Unit::TestCase @@ -66,14 +66,9 @@ class PersonTest &lt; Test::Unit::TestCase
66 assert_kind_of PersonInfo, p.person_info 66 assert_kind_of PersonInfo, p.person_info
67 end 67 end
68 68
69 - should 'provide needed information in info' do 69 + should 'return person_info as info' do
70 p = Person.new 70 p = Person.new
71 - p.person_info.address = 'my address'  
72 - p.person_info.contact_information = 'my contact information'  
73 -  
74 - info = p.info  
75 - assert(info.any? { |line| line[1] == 'my address' })  
76 - assert(info.any? { |line| line[1] == 'my contact information' }) 71 + assert_equal p.person_info, p.info
77 end 72 end
78 73
79 end 74 end
test/unit/profile_helper_test.rb
@@ -27,7 +27,9 @@ class ProfileHelperTest &lt; Test::Unit::TestCase @@ -27,7 +27,9 @@ class ProfileHelperTest &lt; Test::Unit::TestCase
27 [ f1, v1 ], 27 [ f1, v1 ],
28 [ f2, v2 ] 28 [ f2, v2 ]
29 ] 29 ]
30 - profile.stubs(:info).returns(array) 30 + info = mock
  31 + info.expects(:summary).returns(array)
  32 + profile.stubs(:info).returns(info)
31 33
32 helper.expects(:content_tag).with('th', f1).returns(f1) 34 helper.expects(:content_tag).with('th', f1).returns(f1)
33 helper.expects(:content_tag).with('td', v1).returns(v1) 35 helper.expects(:content_tag).with('td', v1).returns(v1)
@@ -42,7 +44,9 @@ class ProfileHelperTest &lt; Test::Unit::TestCase @@ -42,7 +44,9 @@ class ProfileHelperTest &lt; Test::Unit::TestCase
42 44
43 def test_should_call_blocks 45 def test_should_call_blocks
44 myproc = lambda { content_tag('div', 'lalala') } 46 myproc = lambda { content_tag('div', 'lalala') }
45 - profile.stubs(:info).returns([['f1', myproc ]]) 47 + info = mock
  48 + info.expects(:summary).returns([['f1', myproc ]])
  49 + profile.stubs(:info).returns(info)
46 helper.stubs(:content_tag).returns('') 50 helper.stubs(:content_tag).returns('')
47 51
48 helper.expects(:instance_eval).with(myproc) 52 helper.expects(:instance_eval).with(myproc)