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 1 class ProfileEditorController < ApplicationController
2 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 11 end
  12 +
... ...
app/helpers/profile_helper.rb
... ... @@ -6,7 +6,7 @@ module ProfileHelper
6 6 content_tag('div', _('This profile does not have any public information'))
7 7 else
8 8 table_rows = ''
9   - info.each do |item|
  9 + info.summary.each do |item|
10 10 name = item[0]
11 11 value = item[1]
12 12 if value.is_a?(Proc)
... ...
app/models/person.rb
... ... @@ -13,16 +13,9 @@ class Person &lt; Profile
13 13 has_many :person_friendships
14 14 has_many :people, :through => :person_friendships, :foreign_key => 'friend_id'
15 15 has_one :person_info
  16 +
16 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 19 end
27 20  
28 21 validates_presence_of :user_id
... ...
app/models/person_info.rb
... ... @@ -2,4 +2,14 @@ class PersonInfo &lt; ActiveRecord::Base
2 2  
3 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 15 end
... ...
app/models/profile.rb
... ... @@ -73,14 +73,18 @@ class Profile &lt; ActiveRecord::Base
73 73 end
74 74  
75 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 82 # [ 'First Field', first_field_value ],
80 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 89 # In this class, this method returns nil, what is interpreted as "no
86 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 10 assert info.respond_to?(:contact_information)
11 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 27 end
... ...
test/unit/person_test.rb
... ... @@ -66,14 +66,9 @@ class PersonTest &lt; Test::Unit::TestCase
66 66 assert_kind_of PersonInfo, p.person_info
67 67 end
68 68  
69   - should 'provide needed information in info' do
  69 + should 'return person_info as info' do
70 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 72 end
78 73  
79 74 end
... ...
test/unit/profile_helper_test.rb
... ... @@ -27,7 +27,9 @@ class ProfileHelperTest &lt; Test::Unit::TestCase
27 27 [ f1, v1 ],
28 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 34 helper.expects(:content_tag).with('th', f1).returns(f1)
33 35 helper.expects(:content_tag).with('td', v1).returns(v1)
... ... @@ -42,7 +44,9 @@ class ProfileHelperTest &lt; Test::Unit::TestCase
42 44  
43 45 def test_should_call_blocks
44 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 50 helper.stubs(:content_tag).returns('')
47 51  
48 52 helper.expects(:instance_eval).with(myproc)
... ...