Commit db63c60aebb18a046c9cb625e0215b27f301ced0

Authored by Braulio Bhavamitra
1 parent be1a989b

Associate EnterpriseActivation with Task's target

This allow the task to been seen on the control panel

Besides, requestor is not required. In CIRANDAS, for example,
all EnterpriseActivation don't have requestor filled

Also finish the task if Enterprise#enable is called manually
app/models/enterprise.rb
... ... @@ -96,14 +96,22 @@ class Enterprise < Organization
96 96 save
97 97 end
98 98  
  99 + def activation_task
  100 + self.tasks.where(:type => 'EnterpriseActivation').first
  101 + end
  102 +
99 103 def enable(owner)
100 104 return if enabled
101   - affiliate(owner, Profile::Roles.all_roles(environment.id))
102   - update_attribute(:enabled,true)
103   - if environment.replace_enterprise_template_when_enable
104   - apply_template(template)
105   - end
106   - save_without_validation!
  105 + # must be set first for the following to work
  106 + self.enabled = true
  107 +
  108 + self.affiliate owner, Profile::Roles.all_roles(self.environment.id) if owner
  109 +
  110 + self.apply_template template if self.environment.replace_enterprise_template_when_enable
  111 +
  112 + self.activation_task.update_attribute :status, Task::Status::FINISHED rescue nil
  113 +
  114 + self.save_without_validation!
107 115 end
108 116  
109 117 def question
... ...
app/models/enterprise_activation.rb
1 1 class EnterpriseActivation < Task
2 2  
3   - class RequestorRequired < Exception; end
  3 + alias :person :requestor
  4 + alias :person= :requestor=
4 5  
5   - settings_items :enterprise_id, :integer
  6 + alias :enterprise :target
  7 + alias :enterprise= :target=
6 8  
7   - validates_presence_of :enterprise_id
8   -
9   - def enterprise
10   - Enterprise.find(enterprise_id)
11   - end
12   -
13   - def enterprise=(ent)
14   - self.enterprise_id = ent.id
15   - end
  9 + validates_presence_of :enterprise
16 10  
17 11 def perform
18   - raise EnterpriseActivation::RequestorRequired if requestor.nil?
19   - self.enterprise.enable(requestor)
  12 + self.enterprise.enable self.requestor
20 13 end
21 14  
22 15 def title
... ... @@ -28,15 +21,27 @@ class EnterpriseActivation &lt; Task
28 21 end
29 22  
30 23 def information
31   - {:message => _('%{requestor} wants to activate enterprise %{linked_subject}.')}
  24 + if self.requestor
  25 + {:message => _('%{requestor} wants to activate enterprise %{linked_subject}.')}
  26 + else
  27 + {:message => _('Pending activation of enterprise %{linked_subject}.')}
  28 + end
32 29 end
33 30  
34 31 def icon
35   - {:type => :profile_image, :profile => requestor, :url => requestor.url}
  32 + if self.requestor
  33 + {:type => :profile_image, :profile => self.requestor, :url => self.requestor.url}
  34 + else
  35 + {:type => :profile_image, :profile => self.enterprise, :url => self.enterprise.url}
  36 + end
36 37 end
37 38  
38 39 def target_notification_description
39   - _('%{requestor} wants to activate enterprise %{enterprise}.') % {:requestor => requestor.name, :enterprise => enterprise.name}
  40 + if self.requestor
  41 + _('%{requestor} wants to activate enterprise %{enterprise}.') % {:requestor => self.requestor.name, :enterprise => self.enterprise.name}
  42 + else
  43 + _('Pending activation of enterprise %{enterprise}.') % {:enterprise => self.enterprise.name}
  44 + end
40 45 end
41 46  
42 47 end
... ...
db/migrate/20140318225328_set_target_for_enterprise_activation.rb 0 → 100644
... ... @@ -0,0 +1,13 @@
  1 +class SetTargetForEnterpriseActivation < ActiveRecord::Migration
  2 + def self.up
  3 + EnterpriseActivation.find_each do |enterprise_activation|
  4 + enterprise_activation.target = enterprise_activation.enterprise
  5 + enterprise_activation.data.delete :enterprise_id
  6 + enterprise_activation.save
  7 + end
  8 + end
  9 +
  10 + def self.down
  11 + say "this migration can't be reverted"
  12 + end
  13 +end
... ...
po/pt/noosfero.po
... ... @@ -2815,6 +2815,14 @@ msgstr &quot;%{requestor} quer ativar o empreendimento %{linked_subject}.&quot;
2815 2815 msgid "%{requestor} wants to activate enterprise %{enterprise}."
2816 2816 msgstr "%{requestor} quer ativar o empreendimento %{enterprise}."
2817 2817  
  2818 +#: app/models/enterprise_activation.rb:31
  2819 +msgid "Pending activation of enterprise %{linked_subject}."
  2820 +msgstr "Ativação pendente para o empreendimento %{linked_subject}."
  2821 +
  2822 +#: app/models/enterprise_activation.rb:39
  2823 +msgid "Pending activation of enterprise %{enterprise}."
  2824 +msgstr "Ativação pendente para o empreendimento %{enterprise}."
  2825 +
2818 2826 #: app/models/contact.rb:- app/views/friends/index.rhtml:33
2819 2827 #: app/views/friends/index.rhtml:36
2820 2828 msgid "contact"
... ...
test/unit/enterprise_activation_test.rb
... ... @@ -9,24 +9,24 @@ class EnterpriseActivationTest &lt; ActiveSupport::TestCase
9 9 end
10 10  
11 11 should 'keep enterprise_id' do
12   - assert_nil EnterpriseActivation.new.enterprise_id
  12 + assert_nil EnterpriseActivation.new.target_id
13 13 end
14 14  
15 15 should 'have an enteprise through enterprise_id' do
16 16 ent = Enterprise.create!(:name => 'my enterprise', :identifier => 'myent')
17 17  
18   - assert_equal ent, EnterpriseActivation.new(:enterprise_id => ent.id).enterprise
  18 + assert_equal ent, EnterpriseActivation.new(:target => ent).enterprise
19 19 end
20 20  
21 21 should 'require an enterprise' do
22 22 t = EnterpriseActivation.new
23 23 t.valid?
24   - assert t.errors.invalid?(:enterprise_id), "enterprise must be required"
  24 + assert t.errors.invalid?(:enterprise), "enterprise must be required"
25 25  
26 26 ent = Enterprise.create!(:name => 'my enterprise', :identifier => 'myent')
27 27 t.enterprise = ent
28 28 t.valid?
29   - assert !t.errors.invalid?(:enterprise_id), "must validate after enterprise is set"
  29 + assert !t.errors.invalid?(:target_id), "must validate after enterprise is set"
30 30 end
31 31  
32 32 should 'activate enterprise when finished' do
... ... @@ -40,15 +40,6 @@ class EnterpriseActivationTest &lt; ActiveSupport::TestCase
40 40 assert ent.enabled, "finishing task should left enterprise enabled"
41 41 end
42 42  
43   - should 'require requestor to finish' do
44   - ent = Enterprise.create!(:name => 'my enterprise', :identifier => 'myent', :enabled => false)
45   - t = EnterpriseActivation.create!(:enterprise => ent)
46   -
47   - assert_raise EnterpriseActivation::RequestorRequired do
48   - t.finish
49   - end
50   - end
51   -
52 43 should 'put requestor as enterprise owner when finishing' do
53 44 ent = Enterprise.create!(:name => 'my enterprise', :identifier => 'myent', :enabled => false)
54 45 t = EnterpriseActivation.create!(:enterprise => ent)
... ...