Commit 7895b41c43486d5ef685413148ae48ff33d83fd1

Authored by MoisesMachado
1 parent 9db73231

ActionItem32: added a block, management and add button for favorite enterprises


git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@1794 3f533792-8f58-4932-b0fe-aaf55b0a4547
app/controllers/my_profile/favorite_enterprises_controller.rb 0 → 100644
... ... @@ -0,0 +1,27 @@
  1 +class FavoriteEnterprisesController < MyProfileController
  2 +
  3 +# protect 'manage_favorite_enteprises', :profile
  4 +
  5 + requires_profile_class Person
  6 +
  7 + def index
  8 + @favorite_enterprises = profile.favorite_enterprises
  9 + end
  10 +
  11 + def add
  12 + @favorite_enterprise = Enterprise.find(params[:id])
  13 + if request.post? && params[:confirmation]
  14 + profile.favorite_enterprises << @favorite_enterprise
  15 + redirect_to :action => 'index'
  16 + end
  17 + end
  18 +
  19 + def remove
  20 + @favorite_enterprise = profile.favorite_enterprises.find(params[:id])
  21 + if request.post? && params[:confirmation]
  22 + profile.favorite_enterprises.delete(@favorite_enterprise)
  23 + redirect_to :action => 'index'
  24 + end
  25 + end
  26 +
  27 +end
... ...
app/controllers/my_profile/profile_design_controller.rb
... ... @@ -12,8 +12,10 @@ class ProfileDesignController &lt; BoxOrganizerController
12 12 blocks << MembersBlock
13 13 end
14 14  
  15 + # blocks exclusive to person
15 16 if profile.person?
16 17 blocks << FriendsBlock
  18 + blocks << FavoriteEnterprisesBlock
17 19 end
18 20  
19 21 blocks
... ...
app/controllers/public/profile_controller.rb
... ... @@ -27,4 +27,8 @@ class ProfileController &lt; ApplicationController
27 27 def members
28 28 @members = profile.members
29 29 end
  30 +
  31 + def favorite_enterprises
  32 + @favorite_enterprises = profile.favorite_enterprises
  33 + end
30 34 end
... ...
app/helpers/favorite_enterprises_helper.rb 0 → 100644
... ... @@ -0,0 +1,2 @@
  1 +module FavoriteEnterprisesHelper
  2 +end
... ...
app/models/favorite_enterprises_block.rb 0 → 100644
... ... @@ -0,0 +1,34 @@
  1 +class FavoriteEnterprisesBlock < ProfileListBlock
  2 +
  3 + def title
  4 + _('Favorite Enterprises')
  5 + end
  6 +
  7 + def help
  8 + _('This user\'s favorite enterprises.')
  9 + end
  10 +
  11 + def self.description
  12 + _('A block that displays your favorite enterprises')
  13 + end
  14 +
  15 + def footer
  16 + owner = self.owner
  17 + return '' unless owner.kind_of?(Person)
  18 + lambda do
  19 + link_to _('All favorite enterprises'), :profile => owner.identifier, :controller => 'profile', :action => 'favorite_enterprises'
  20 + end
  21 + end
  22 +
  23 +
  24 + def profile_finder
  25 + @profile_finder ||= FavoriteEnterprisesBlock::Finder.new(self)
  26 + end
  27 +
  28 + class Finder < ProfileListBlock::Finder
  29 + def ids
  30 + block.owner.favorite_enterprises.map(&:id)
  31 + end
  32 + end
  33 +
  34 +end
... ...
app/models/person.rb
... ... @@ -108,4 +108,6 @@ class Person &lt; Profile
108 108 person_info.nil? ? self[:name] : (person_info.name || self[:name])
109 109 end
110 110  
  111 + has_and_belongs_to_many :favorite_enterprises, :class_name => 'Enterprise', :join_table => 'favorite_enteprises_people'
  112 +
111 113 end
... ...
app/views/blocks/profile_info_actions/enterprise.rhtml
... ... @@ -0,0 +1,5 @@
  1 +<ul>
  2 + <%if logged_in? && (! user.favorite_enterprises.include?(profile)) %>
  3 + <li><%= link_to content_tag('span', _('Add favorite enterprise')), { :profile => user.identifier, :controller => 'favorite_enterprises', :action => 'add', :id => profile.id }, :class => 'button with-text icon-add' %></li>
  4 + <% end %>
  5 +</ul>
... ...
app/views/favorite_enterprises/add.rhtml 0 → 100644
... ... @@ -0,0 +1,12 @@
  1 +<h1><%= _('Adding %s as a favorite enterprise') % @favorite_enterprise.name %></h1>
  2 +
  3 +<p>
  4 +<%= _('Are you sure you want to add %s as your favorite enterprise?') % @favorite_enterprise.name %>
  5 +</p>
  6 +
  7 +<% form_tag do %>
  8 + <%= hidden_field_tag(:confirmation, 1) %>
  9 +
  10 + <%= submit_button(:ok, _("Yes, I want to add %s as a favorite enterprise") % @favorite_enterprise.name) %>
  11 + <%= button(:cancel, _("No, I don't want"), :action => 'index') %>
  12 +<% end %>
... ...
app/views/favorite_enterprises/index.rhtml 0 → 100644
... ... @@ -0,0 +1,33 @@
  1 +<div id="manage_favorite_enterprises">
  2 +
  3 +<h1><%= _("%s's favorite enteprises") % profile.name %></h1>
  4 +
  5 +<ul class='profile-list'>
  6 +<% @favorite_enterprises.each do |enterprise| %>
  7 + <li>
  8 + <%= profile_image_link enterprise %>
  9 + <div class="controll">
  10 + <%= link_to content_tag('span',_('remove')),
  11 + { :action => 'remove', :id => enterprise.id },
  12 + :class => 'button icon-delete',
  13 + :title => _('remove'),
  14 + :help => _('Clicking on this button will remove your friend relation with %s.') % enterprise.name %>
  15 + </div><!-- end class="controll" -->
  16 + </li>
  17 +<% end %>
  18 +</ul>
  19 +
  20 +<% if @favorite_enterprises.empty? %>
  21 +<p>
  22 +<em>
  23 + <%= _('You have no favorite enteprises yet. Go make some.') %>
  24 +</em>
  25 +</p>
  26 +<% end %>
  27 +
  28 +<% button_bar do %>
  29 + <%= button(:back, _('Go back'), :controller => 'profile_editor') %>
  30 +<% end %>
  31 +
  32 +</div><!-- end id="manage_friends" -->
  33 +
... ...
app/views/favorite_enterprises/remove.rhtml 0 → 100644
... ... @@ -0,0 +1,18 @@
  1 +<div id="remove_favorite_enterprise">
  2 +
  3 +<h1><%= _('Removing favorite enterprise: %s') % @favorite_enterprise.name %></h1>
  4 +
  5 +<%= profile_image @favorite_enterprise, :thumb, :class => 'favorite_enterprise_picture' %>
  6 +
  7 +<p>
  8 +<%= _('Are you sure you want to remove %s from your favorite enterprise list?') % @favorite_enterprise.name %>
  9 +</p>
  10 +
  11 +<% form_tag do %>
  12 + <%= hidden_field_tag(:confirmation, 1) %>
  13 +
  14 + <%= submit_button(:ok, _("Yes, I want to remove %s from my favorite enterprise list") % @favorite_enterprise.name) %>
  15 + <%= button(:cancel, _("No, I don't want"), :action => 'index') %>
  16 +<% end %>
  17 +
  18 +</div><!-- end id="remove_favorite_enterprise" -->
... ...
app/views/profile/favorite_enterprises.rhtml 0 → 100644
... ... @@ -0,0 +1,18 @@
  1 +<div class="common-profile-list-block"
  2 + help="<%= _('Here are all <b>%s</b>\'s favorite enterprises.') % profile.name %>">
  3 +
  4 +<h1><%= _("%s's favorite enterprises") % profile.name %></h1>
  5 +
  6 +<ul class='profile-list'>
  7 +<% @favorite_enterprises.each do |enterprise| %>
  8 + <li><%= profile_image_link(enterprise)%></li>
  9 +<% end %>
  10 +</ul>
  11 +
  12 +<% button_bar do %>
  13 + <%= button :back, _('Go back'), { :controller => 'profile' },
  14 + :help => _('Back to the page where you come from.') %>
  15 +<% end %>
  16 +
  17 +</div><!-- fim class="common-profile-list-block" -->
  18 +
... ...
db/migrate/032_favorite_enterprises_people.rb 0 → 100644
... ... @@ -0,0 +1,12 @@
  1 +class FavoriteEnterprisesPeople < ActiveRecord::Migration
  2 + def self.up
  3 + create_table :favorite_enteprises_people, :id => false do |t|
  4 + t.integer :person_id
  5 + t.integer :enterprise_id
  6 + end
  7 + end
  8 +
  9 + def self.down
  10 + drop_table :favorite_enteprises_people
  11 + end
  12 +end
... ...
test/functional/favorite_enterprises_controller_test.rb 0 → 100644
... ... @@ -0,0 +1,71 @@
  1 +require File.dirname(__FILE__) + '/../test_helper'
  2 +require 'favorite_enterprises_controller'
  3 +
  4 +class FavoriteEnterprisesController; def rescue_action(e) raise e end; end
  5 +
  6 +class FavoriteEnterprisesControllerTest < Test::Unit::TestCase
  7 +
  8 + noosfero_test :profile => 'testuser'
  9 +
  10 + def setup
  11 + @controller = FavoriteEnterprisesController.new
  12 + @request = ActionController::TestRequest.new
  13 + @response = ActionController::TestResponse.new
  14 +
  15 + self.profile = create_user('testuser').person
  16 + self.favorite_enterprise = Enterprise.create!(:name => 'the_enterprise', :identifier => 'the_enterprise')
  17 + login_as ('testuser')
  18 + end
  19 + attr_accessor :profile, :favorite_enterprise
  20 +
  21 + def test_local_files_reference
  22 + assert_local_files_reference
  23 + end
  24 +
  25 + def test_valid_xhtml
  26 + assert_valid_xhtml
  27 + end
  28 +
  29 + should 'list favorite enterprises' do
  30 + get :index
  31 + assert_response :success
  32 + assert_template 'index'
  33 + assert_kind_of Array, assigns(:favorite_enterprises)
  34 + end
  35 +
  36 + should 'confirm addition of new favorite enterprise' do
  37 + get :add, :id => favorite_enterprise.id
  38 +
  39 + assert_response :success
  40 + assert_template 'add'
  41 +
  42 + ok("must load the favorite enterprise being added to display") { favorite_enterprise == assigns(:favorite_enterprise) }
  43 +
  44 + end
  45 +
  46 + should 'actually add favorite_enterprise' do
  47 + assert_difference profile.favorite_enterprises, :count do
  48 + post :add, :id => favorite_enterprise.id, :confirmation => '1'
  49 + assert_response :redirect
  50 + end
  51 + end
  52 +
  53 + should 'confirm removal of favorite enterprise' do
  54 + profile.favorite_enterprises << favorite_enterprise
  55 +
  56 + get :remove, :id => favorite_enterprise.id
  57 + assert_response :success
  58 + assert_template 'remove'
  59 + ok("must load the favorite_enterprise being removed") { favorite_enterprise == assigns(:favorite_enterprise) }
  60 + end
  61 +
  62 + should 'actually remove favorite_enterprise' do
  63 + profile.favorite_enterprises << favorite_enterprise
  64 +
  65 + assert_difference profile.favorite_enterprises, :count, -1 do
  66 + post :remove, :id => favorite_enterprise.id, :confirmation => '1'
  67 + assert_redirected_to :action => 'index'
  68 + end
  69 + end
  70 +
  71 +end
... ...
test/functional/profile_controller_test.rb
... ... @@ -54,6 +54,14 @@ class ProfileControllerTest &lt; Test::Unit::TestCase
54 54 assert_template 'members'
55 55 assert_kind_of Array, assigns(:members)
56 56 end
  57 +
  58 + should 'list favorite enterprises' do
  59 + get :favorite_enterprises
  60 +
  61 + assert_response :success
  62 + assert_template 'favorite_enterprises'
  63 + assert_kind_of Array, assigns(:favorite_enterprises)
  64 + end
57 65  
58 66 should 'show Join This Community button for non-member users' do
59 67 login_as(@profile.identifier)
... ...
test/unit/favorite_enterprises_block_test.rb 0 → 100644
... ... @@ -0,0 +1,63 @@
  1 +require File.dirname(__FILE__) + '/../test_helper'
  2 +
  3 +class FavoriteEnterprisesBlockTest < ActiveSupport::TestCase
  4 +
  5 + should 'inherit from ProfileListBlock' do
  6 + assert_kind_of ProfileListBlock, FavoriteEnterprisesBlock.new
  7 + end
  8 +
  9 + should 'declare its title' do
  10 + assert_not_equal ProfileListBlock.new.title, FavoriteEnterprisesBlock.new.title
  11 + end
  12 +
  13 + should 'describe itself' do
  14 + assert_not_equal ProfileListBlock.description, FavoriteEnterprisesBlock.description
  15 + end
  16 +
  17 + should 'use its own finder' do
  18 + assert_not_equal FavoriteEnterprisesBlock::Finder, ProfileListBlock::Finder
  19 + assert_kind_of FavoriteEnterprisesBlock::Finder, FavoriteEnterprisesBlock.new.profile_finder
  20 + end
  21 +
  22 + should 'list owner favorite enterprises' do
  23 +
  24 + block = FavoriteEnterprisesBlock.new
  25 + block.limit = 2
  26 +
  27 + owner = mock
  28 + block.expects(:owner).returns(owner)
  29 +
  30 + member1 = mock; member1.stubs(:id).returns(1)
  31 + member2 = mock; member2.stubs(:id).returns(2)
  32 + member3 = mock; member3.stubs(:id).returns(3)
  33 +
  34 + owner.expects(:favorite_enterprises).returns([member1, member2, member3])
  35 +
  36 + block.profile_finder.expects(:pick_random).with(3).returns(2)
  37 + block.profile_finder.expects(:pick_random).with(2).returns(0)
  38 +
  39 + Profile.expects(:find).with(3).returns(member3)
  40 + Profile.expects(:find).with(1).returns(member1)
  41 +
  42 + assert_equal [member3, member1], block.profiles
  43 + end
  44 +
  45 + should 'link to all enterprises for person' do
  46 + person = Person.new
  47 + person.expects(:identifier).returns('theprofile')
  48 + block = FavoriteEnterprisesBlock.new
  49 + block.expects(:owner).returns(person)
  50 +
  51 + expects(:_).with('All favorite enterprises').returns('All enterprises')
  52 + expects(:link_to).with('All enterprises', :controller => 'profile', :profile => 'theprofile', :action => 'favorite_enterprises')
  53 +
  54 + instance_eval(&block.footer)
  55 + end
  56 +
  57 + should 'give empty footer for unsupported owner type' do
  58 + block = FavoriteEnterprisesBlock.new
  59 + block.expects(:owner).returns(1)
  60 + assert_equal '', block.footer
  61 + end
  62 +
  63 +end
... ...
test/unit/person_test.rb
... ... @@ -215,6 +215,15 @@ class PersonTest &lt; Test::Unit::TestCase
215 215 assert_equal 'randomhacker', p.name
216 216 end
217 217  
  218 + should 'have favorite enterprises' do
  219 + p = create_user('test_person').person
  220 + e = Enterprise.create!(:name => 'test_ent', :identifier => 'test_ent')
  221 +
  222 + p.favorite_enterprises << e
  223 +
  224 + assert_includes Person.find(p.id).favorite_enterprises, e
  225 + end
  226 +
218 227 should 'save info' do
219 228 person = create_user('new_person').person
220 229 person.info = {:contact_information => 'my contact'}
... ...