diff --git a/app/controllers/my_profile/favorite_enterprises_controller.rb b/app/controllers/my_profile/favorite_enterprises_controller.rb
new file mode 100644
index 0000000..a866233
--- /dev/null
+++ b/app/controllers/my_profile/favorite_enterprises_controller.rb
@@ -0,0 +1,27 @@
+class FavoriteEnterprisesController < MyProfileController
+
+# protect 'manage_favorite_enteprises', :profile
+
+ requires_profile_class Person
+
+ def index
+ @favorite_enterprises = profile.favorite_enterprises
+ end
+
+ def add
+ @favorite_enterprise = Enterprise.find(params[:id])
+ if request.post? && params[:confirmation]
+ profile.favorite_enterprises << @favorite_enterprise
+ redirect_to :action => 'index'
+ end
+ end
+
+ def remove
+ @favorite_enterprise = profile.favorite_enterprises.find(params[:id])
+ if request.post? && params[:confirmation]
+ profile.favorite_enterprises.delete(@favorite_enterprise)
+ redirect_to :action => 'index'
+ end
+ end
+
+end
diff --git a/app/controllers/my_profile/profile_design_controller.rb b/app/controllers/my_profile/profile_design_controller.rb
index 2fd6035..15fb0a9 100644
--- a/app/controllers/my_profile/profile_design_controller.rb
+++ b/app/controllers/my_profile/profile_design_controller.rb
@@ -12,8 +12,10 @@ class ProfileDesignController < BoxOrganizerController
blocks << MembersBlock
end
+ # blocks exclusive to person
if profile.person?
blocks << FriendsBlock
+ blocks << FavoriteEnterprisesBlock
end
blocks
diff --git a/app/controllers/public/profile_controller.rb b/app/controllers/public/profile_controller.rb
index 7c41acb..a17a4dc 100644
--- a/app/controllers/public/profile_controller.rb
+++ b/app/controllers/public/profile_controller.rb
@@ -27,4 +27,8 @@ class ProfileController < ApplicationController
def members
@members = profile.members
end
+
+ def favorite_enterprises
+ @favorite_enterprises = profile.favorite_enterprises
+ end
end
diff --git a/app/helpers/favorite_enterprises_helper.rb b/app/helpers/favorite_enterprises_helper.rb
new file mode 100644
index 0000000..003f92d
--- /dev/null
+++ b/app/helpers/favorite_enterprises_helper.rb
@@ -0,0 +1,2 @@
+module FavoriteEnterprisesHelper
+end
diff --git a/app/models/favorite_enterprises_block.rb b/app/models/favorite_enterprises_block.rb
new file mode 100644
index 0000000..ecc2e6c
--- /dev/null
+++ b/app/models/favorite_enterprises_block.rb
@@ -0,0 +1,34 @@
+class FavoriteEnterprisesBlock < ProfileListBlock
+
+ def title
+ _('Favorite Enterprises')
+ end
+
+ def help
+ _('This user\'s favorite enterprises.')
+ end
+
+ def self.description
+ _('A block that displays your favorite enterprises')
+ end
+
+ def footer
+ owner = self.owner
+ return '' unless owner.kind_of?(Person)
+ lambda do
+ link_to _('All favorite enterprises'), :profile => owner.identifier, :controller => 'profile', :action => 'favorite_enterprises'
+ end
+ end
+
+
+ def profile_finder
+ @profile_finder ||= FavoriteEnterprisesBlock::Finder.new(self)
+ end
+
+ class Finder < ProfileListBlock::Finder
+ def ids
+ block.owner.favorite_enterprises.map(&:id)
+ end
+ end
+
+end
diff --git a/app/models/person.rb b/app/models/person.rb
index b484dc4..d51c1cf 100644
--- a/app/models/person.rb
+++ b/app/models/person.rb
@@ -108,4 +108,6 @@ class Person < Profile
person_info.nil? ? self[:name] : (person_info.name || self[:name])
end
+ has_and_belongs_to_many :favorite_enterprises, :class_name => 'Enterprise', :join_table => 'favorite_enteprises_people'
+
end
diff --git a/app/views/blocks/profile_info_actions/enterprise.rhtml b/app/views/blocks/profile_info_actions/enterprise.rhtml
index e69de29..b3fb1c6 100644
--- a/app/views/blocks/profile_info_actions/enterprise.rhtml
+++ b/app/views/blocks/profile_info_actions/enterprise.rhtml
@@ -0,0 +1,5 @@
+
+ <%if logged_in? && (! user.favorite_enterprises.include?(profile)) %>
+ - <%= 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' %>
+ <% end %>
+
diff --git a/app/views/favorite_enterprises/add.rhtml b/app/views/favorite_enterprises/add.rhtml
new file mode 100644
index 0000000..5d8684b
--- /dev/null
+++ b/app/views/favorite_enterprises/add.rhtml
@@ -0,0 +1,12 @@
+<%= _('Adding %s as a favorite enterprise') % @favorite_enterprise.name %>
+
+
+<%= _('Are you sure you want to add %s as your favorite enterprise?') % @favorite_enterprise.name %>
+
+
+<% form_tag do %>
+ <%= hidden_field_tag(:confirmation, 1) %>
+
+ <%= submit_button(:ok, _("Yes, I want to add %s as a favorite enterprise") % @favorite_enterprise.name) %>
+ <%= button(:cancel, _("No, I don't want"), :action => 'index') %>
+<% end %>
diff --git a/app/views/favorite_enterprises/index.rhtml b/app/views/favorite_enterprises/index.rhtml
new file mode 100644
index 0000000..fc2b03a
--- /dev/null
+++ b/app/views/favorite_enterprises/index.rhtml
@@ -0,0 +1,33 @@
+
+
+
<%= _("%s's favorite enteprises") % profile.name %>
+
+
+<% @favorite_enterprises.each do |enterprise| %>
+ -
+ <%= profile_image_link enterprise %>
+
+ <%= link_to content_tag('span',_('remove')),
+ { :action => 'remove', :id => enterprise.id },
+ :class => 'button icon-delete',
+ :title => _('remove'),
+ :help => _('Clicking on this button will remove your friend relation with %s.') % enterprise.name %>
+
+
+<% end %>
+
+
+<% if @favorite_enterprises.empty? %>
+
+
+ <%= _('You have no favorite enteprises yet. Go make some.') %>
+
+
+<% end %>
+
+<% button_bar do %>
+ <%= button(:back, _('Go back'), :controller => 'profile_editor') %>
+<% end %>
+
+
+
diff --git a/app/views/favorite_enterprises/remove.rhtml b/app/views/favorite_enterprises/remove.rhtml
new file mode 100644
index 0000000..3b5fa43
--- /dev/null
+++ b/app/views/favorite_enterprises/remove.rhtml
@@ -0,0 +1,18 @@
+
+
+
<%= _('Removing favorite enterprise: %s') % @favorite_enterprise.name %>
+
+<%= profile_image @favorite_enterprise, :thumb, :class => 'favorite_enterprise_picture' %>
+
+
+<%= _('Are you sure you want to remove %s from your favorite enterprise list?') % @favorite_enterprise.name %>
+
+
+<% form_tag do %>
+ <%= hidden_field_tag(:confirmation, 1) %>
+
+ <%= submit_button(:ok, _("Yes, I want to remove %s from my favorite enterprise list") % @favorite_enterprise.name) %>
+ <%= button(:cancel, _("No, I don't want"), :action => 'index') %>
+<% end %>
+
+
diff --git a/app/views/profile/favorite_enterprises.rhtml b/app/views/profile/favorite_enterprises.rhtml
new file mode 100644
index 0000000..5d67a0e
--- /dev/null
+++ b/app/views/profile/favorite_enterprises.rhtml
@@ -0,0 +1,18 @@
+
+
+
<%= _("%s's favorite enterprises") % profile.name %>
+
+
+<% @favorite_enterprises.each do |enterprise| %>
+ - <%= profile_image_link(enterprise)%>
+<% end %>
+
+
+<% button_bar do %>
+ <%= button :back, _('Go back'), { :controller => 'profile' },
+ :help => _('Back to the page where you come from.') %>
+<% end %>
+
+
+
diff --git a/db/migrate/032_favorite_enterprises_people.rb b/db/migrate/032_favorite_enterprises_people.rb
new file mode 100644
index 0000000..0be66d8
--- /dev/null
+++ b/db/migrate/032_favorite_enterprises_people.rb
@@ -0,0 +1,12 @@
+class FavoriteEnterprisesPeople < ActiveRecord::Migration
+ def self.up
+ create_table :favorite_enteprises_people, :id => false do |t|
+ t.integer :person_id
+ t.integer :enterprise_id
+ end
+ end
+
+ def self.down
+ drop_table :favorite_enteprises_people
+ end
+end
diff --git a/test/functional/favorite_enterprises_controller_test.rb b/test/functional/favorite_enterprises_controller_test.rb
new file mode 100644
index 0000000..076af46
--- /dev/null
+++ b/test/functional/favorite_enterprises_controller_test.rb
@@ -0,0 +1,71 @@
+require File.dirname(__FILE__) + '/../test_helper'
+require 'favorite_enterprises_controller'
+
+class FavoriteEnterprisesController; def rescue_action(e) raise e end; end
+
+class FavoriteEnterprisesControllerTest < Test::Unit::TestCase
+
+ noosfero_test :profile => 'testuser'
+
+ def setup
+ @controller = FavoriteEnterprisesController.new
+ @request = ActionController::TestRequest.new
+ @response = ActionController::TestResponse.new
+
+ self.profile = create_user('testuser').person
+ self.favorite_enterprise = Enterprise.create!(:name => 'the_enterprise', :identifier => 'the_enterprise')
+ login_as ('testuser')
+ end
+ attr_accessor :profile, :favorite_enterprise
+
+ def test_local_files_reference
+ assert_local_files_reference
+ end
+
+ def test_valid_xhtml
+ assert_valid_xhtml
+ end
+
+ should 'list favorite enterprises' do
+ get :index
+ assert_response :success
+ assert_template 'index'
+ assert_kind_of Array, assigns(:favorite_enterprises)
+ end
+
+ should 'confirm addition of new favorite enterprise' do
+ get :add, :id => favorite_enterprise.id
+
+ assert_response :success
+ assert_template 'add'
+
+ ok("must load the favorite enterprise being added to display") { favorite_enterprise == assigns(:favorite_enterprise) }
+
+ end
+
+ should 'actually add favorite_enterprise' do
+ assert_difference profile.favorite_enterprises, :count do
+ post :add, :id => favorite_enterprise.id, :confirmation => '1'
+ assert_response :redirect
+ end
+ end
+
+ should 'confirm removal of favorite enterprise' do
+ profile.favorite_enterprises << favorite_enterprise
+
+ get :remove, :id => favorite_enterprise.id
+ assert_response :success
+ assert_template 'remove'
+ ok("must load the favorite_enterprise being removed") { favorite_enterprise == assigns(:favorite_enterprise) }
+ end
+
+ should 'actually remove favorite_enterprise' do
+ profile.favorite_enterprises << favorite_enterprise
+
+ assert_difference profile.favorite_enterprises, :count, -1 do
+ post :remove, :id => favorite_enterprise.id, :confirmation => '1'
+ assert_redirected_to :action => 'index'
+ end
+ end
+
+end
diff --git a/test/functional/profile_controller_test.rb b/test/functional/profile_controller_test.rb
index 7a58269..32adc3d 100644
--- a/test/functional/profile_controller_test.rb
+++ b/test/functional/profile_controller_test.rb
@@ -54,6 +54,14 @@ class ProfileControllerTest < Test::Unit::TestCase
assert_template 'members'
assert_kind_of Array, assigns(:members)
end
+
+ should 'list favorite enterprises' do
+ get :favorite_enterprises
+
+ assert_response :success
+ assert_template 'favorite_enterprises'
+ assert_kind_of Array, assigns(:favorite_enterprises)
+ end
should 'show Join This Community button for non-member users' do
login_as(@profile.identifier)
diff --git a/test/unit/favorite_enterprises_block_test.rb b/test/unit/favorite_enterprises_block_test.rb
new file mode 100644
index 0000000..2fa3678
--- /dev/null
+++ b/test/unit/favorite_enterprises_block_test.rb
@@ -0,0 +1,63 @@
+require File.dirname(__FILE__) + '/../test_helper'
+
+class FavoriteEnterprisesBlockTest < ActiveSupport::TestCase
+
+ should 'inherit from ProfileListBlock' do
+ assert_kind_of ProfileListBlock, FavoriteEnterprisesBlock.new
+ end
+
+ should 'declare its title' do
+ assert_not_equal ProfileListBlock.new.title, FavoriteEnterprisesBlock.new.title
+ end
+
+ should 'describe itself' do
+ assert_not_equal ProfileListBlock.description, FavoriteEnterprisesBlock.description
+ end
+
+ should 'use its own finder' do
+ assert_not_equal FavoriteEnterprisesBlock::Finder, ProfileListBlock::Finder
+ assert_kind_of FavoriteEnterprisesBlock::Finder, FavoriteEnterprisesBlock.new.profile_finder
+ end
+
+ should 'list owner favorite enterprises' do
+
+ block = FavoriteEnterprisesBlock.new
+ block.limit = 2
+
+ owner = mock
+ block.expects(:owner).returns(owner)
+
+ member1 = mock; member1.stubs(:id).returns(1)
+ member2 = mock; member2.stubs(:id).returns(2)
+ member3 = mock; member3.stubs(:id).returns(3)
+
+ owner.expects(:favorite_enterprises).returns([member1, member2, member3])
+
+ block.profile_finder.expects(:pick_random).with(3).returns(2)
+ block.profile_finder.expects(:pick_random).with(2).returns(0)
+
+ Profile.expects(:find).with(3).returns(member3)
+ Profile.expects(:find).with(1).returns(member1)
+
+ assert_equal [member3, member1], block.profiles
+ end
+
+ should 'link to all enterprises for person' do
+ person = Person.new
+ person.expects(:identifier).returns('theprofile')
+ block = FavoriteEnterprisesBlock.new
+ block.expects(:owner).returns(person)
+
+ expects(:_).with('All favorite enterprises').returns('All enterprises')
+ expects(:link_to).with('All enterprises', :controller => 'profile', :profile => 'theprofile', :action => 'favorite_enterprises')
+
+ instance_eval(&block.footer)
+ end
+
+ should 'give empty footer for unsupported owner type' do
+ block = FavoriteEnterprisesBlock.new
+ block.expects(:owner).returns(1)
+ assert_equal '', block.footer
+ end
+
+end
diff --git a/test/unit/person_test.rb b/test/unit/person_test.rb
index 9a8e836..72cb732 100644
--- a/test/unit/person_test.rb
+++ b/test/unit/person_test.rb
@@ -215,6 +215,15 @@ class PersonTest < Test::Unit::TestCase
assert_equal 'randomhacker', p.name
end
+ should 'have favorite enterprises' do
+ p = create_user('test_person').person
+ e = Enterprise.create!(:name => 'test_ent', :identifier => 'test_ent')
+
+ p.favorite_enterprises << e
+
+ assert_includes Person.find(p.id).favorite_enterprises, e
+ end
+
should 'save info' do
person = create_user('new_person').person
person.info = {:contact_information => 'my contact'}
--
libgit2 0.21.2