Commit 5bff0b1ddbd744b86cfe31e616878b4190a6fea6

Authored by AntonioTerceiro
1 parent e0f4d9ee

ActionItem7: making everything work together



git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@456 3f533792-8f58-4932-b0fe-aaf55b0a4547
app/controllers/application.rb
... ... @@ -30,10 +30,14 @@ class ApplicationController < ActionController::Base
30 30 before_filter :load_profile_from_params
31 31 def load_profile_from_params
32 32 if params[:profile]
33   - @profile = Profile.find_by_identifier(params[:profile])
  33 + @profile ||= Profile.find_by_identifier(params[:profile])
34 34 end
35 35 end
36 36  
  37 + def profile
  38 + @profile
  39 + end
  40 +
37 41 def self.acts_as_virtual_community_admin_controller
38 42 before_filter :load_admin_controller
39 43 end
... ...
app/controllers/profile_editor_controller.rb
... ... @@ -4,8 +4,11 @@ class ProfileEditorController < ApplicationController
4 4 # edits the profile info (posts back)
5 5 def edit
6 6 if request.post?
  7 + profile.info.update_attributes(params[:info])
  8 + redirect_to :action => 'index'
7 9 else
8   - render :action => profile.info.class.tableize
  10 + @info = profile.info
  11 + render :action => @info.class.name.underscore
9 12 end
10 13 end
11 14 end
... ...
app/helpers/application_helper.rb
... ... @@ -158,4 +158,12 @@ module ApplicationHelper
158 158 @profile || raise("There is no current profile")
159 159 end
160 160  
  161 + # displays an
  162 + #
  163 + # Current implementation generates a <label> tag for +label+ and wrap the
  164 + # label and the control with a <div> tag with class 'formfield'
  165 + def display_form_field(label, html_for_field)
  166 + content_tag('div', content_tag('div', content_tag('label', label)) + html_for_field, :class => 'formfield')
  167 + end
  168 +
161 169 end
... ...
app/models/person.rb
... ... @@ -23,5 +23,6 @@ class Person &lt; Profile
23 23 def initialize(*args)
24 24 super(*args)
25 25 self.person_info ||= PersonInfo.new
  26 + self.person_info.person = self
26 27 end
27 28 end
... ...
app/models/person_info.rb
... ... @@ -6,7 +6,7 @@ class PersonInfo &lt; ActiveRecord::Base
6 6  
7 7 def summary
8 8 [
9   - [ _('Name'), self.person.name ],
  9 + [ _('Name'), self.name ],
10 10 [ _('Address'), self.address ],
11 11 [ _('Contact Information'), self.contact_information ],
12 12 ]
... ...
app/views/profile_editor/index.rhtml 0 → 100644
... ... @@ -0,0 +1,5 @@
  1 +<h2><%= _('My profile') %></h2>
  2 +
  3 +<%= display_profile_info(profile) %>
  4 +
  5 +<%= link_to _('Edit'), :action => 'edit' %>
... ...
app/views/profile_editor/person_info.rhtml 0 → 100644
... ... @@ -0,0 +1,13 @@
  1 +<h2><%= _('Edit person info') %></h2>
  2 +
  3 +<% form_for :info, @info do |f| %>
  4 +
  5 + <%= display_form_field(_('Name'), f.text_field(:name)) %>
  6 +
  7 + <%= display_form_field(_('Address'), f.text_area(:address, :rows => 5)) %>
  8 +
  9 + <%= display_form_field(_('Contact Information'), f.text_area(:contact_information, :rows => 5)) %>
  10 +
  11 + <%= submit_tag _('Save') %>
  12 +
  13 +<% end %>
... ...
db/migrate/013_create_person_infos.rb
1 1 class CreatePersonInfos < ActiveRecord::Migration
2 2 def self.up
3 3 create_table :person_infos do |t|
  4 + t.column :name, :string
4 5 t.column :photo, :text
5 6 t.column :address, :text
6 7 t.column :contact_information, :text
... ...
test/fixtures/person_infos.yml 0 → 100644
... ... @@ -0,0 +1,7 @@
  1 +person_info_for_ze:
  2 + id: 1
  3 + person_id: 4
  4 + name: "Zé, José, Zezinho"
  5 + address: "house of the hat"
  6 + contact_information: "Pavilhão 9, Quadrante 13, Esquina com Avenida das Alamedas, 467, fundos, falar com Dona Ivete após as 16"
  7 +
... ...
test/functional/profile_editor_controller_test.rb
... ... @@ -11,8 +11,46 @@ class ProfileEditorControllerTest &lt; Test::Unit::TestCase
11 11 @response = ActionController::TestResponse.new
12 12 end
13 13  
14   - # Replace this with your real tests.
15   - def test_truth
16   - assert true
  14 + def test_index
  15 + profile = Person.new
  16 + profile.person_info.name = 'a test profile'
  17 + profile.person_info.address = 'my address'
  18 + profile.person_info.contact_information = 'my contact information'
  19 + @controller.instance_variable_set('@profile', profile)
  20 +
  21 + get :index, :profile => 'test_profile'
  22 + assert_response :success
  23 + assert_not_nil assigns(:profile)
  24 +
  25 + assert_tag :tag => 'td', :content => 'a test profile'
  26 + assert_tag :tag => 'td', :content => 'my address'
  27 + assert_tag :tag => 'td', :content => 'my contact information'
  28 + end
  29 +
  30 + def test_edit_person_info
  31 + profile = Person.new
  32 + profile.name = 'a test profile'
  33 + profile.person_info.address = 'my address'
  34 + profile.person_info.contact_information = 'my contact information'
  35 + @controller.instance_variable_set('@profile', profile)
  36 +
  37 + get :edit, :profile => 'test_profile'
  38 + assert_response :success
  39 + assert_template 'person_info'
  40 +
17 41 end
  42 +
  43 + def test_saving_profile_info
  44 + profile = Person.new
  45 + profile.name = 'a test profile'
  46 + profile.person_info.address = 'my address'
  47 + profile.person_info.contact_information = 'my contact information'
  48 + @controller.instance_variable_set('@profile', profile)
  49 +
  50 + profile.person_info.expects(:update_attributes).with({ 'contact_information' => 'new contact information', 'address' => 'new address' }).returns(true)
  51 + post :edit, :profile => 'test_profile', :info => { 'contact_information' => 'new contact information', 'address' => 'new address' }
  52 +
  53 + assert_redirected_to :action => 'index'
  54 + end
  55 +
18 56 end
... ...
test/integration/edit_enterprise_test.rb
... ... @@ -13,7 +13,7 @@ class EditEnterpriseTest &lt; ActionController::IntegrationTest
13 13  
14 14 follow_redirect!
15 15 assert_response :success
16   - assert_tag :tag => 'a', :attributes => {:href => '/myprofile/ze/enterprise/edit/5'}
  16 + assert_tag :tag => 'a', :attributes => { :href => '/myprofile/ze/enterprise/edit/5'}
17 17  
18 18 get '/myprofile/ze/enterprise/edit/5'
19 19 assert_response :success
... ...
test/integration/editing_person_info_test.rb 0 → 100644
... ... @@ -0,0 +1,27 @@
  1 +require "#{File.dirname(__FILE__)}/../test_helper"
  2 +
  3 +class EditingPersonInfoTest < ActionController::IntegrationTest
  4 +
  5 + fixtures :users, :profiles, :comatose_pages, :domains, :virtual_communities, :person_infos
  6 +
  7 + should 'allow to edit person info' do
  8 +
  9 + profile = Profile.find_by_identifier('ze')
  10 +
  11 + login('ze', 'test')
  12 +
  13 + get '/myprofile/ze'
  14 + assert_response :success
  15 +
  16 + assert_tag :tag => 'td', :content => profile.person_info.name
  17 + assert_tag :tag => 'td', :content => profile.person_info.address
  18 + assert_tag :tag => 'td', :content => profile.person_info.contact_information
  19 +
  20 + get '/myprofile/ze/profile_editor/edit'
  21 + assert_response :success
  22 +
  23 + post '/myprofile/ze/profile_editor/edit', :info => { :address => 'a new address', :contact_information => 'a new contact information' }
  24 + assert_response :redirect
  25 +
  26 + end
  27 +end
... ...
test/unit/person_info_test.rb
... ... @@ -12,9 +12,8 @@ class PersonInfoTest &lt; Test::Unit::TestCase
12 12  
13 13 should 'provide needed information in summary' do
14 14 person_info = PersonInfo.new
15   - person_info.person = Person.new
16   - person_info.person.name = 'person name'
17 15  
  16 + person_info.name = 'person name'
18 17 person_info.address = 'my address'
19 18 person_info.contact_information = 'my contact information'
20 19  
... ...