Commit a36f5919a60355800a301dd74e4bd04c7d64ca5c

Authored by Larissa Reis
1 parent ea767f36

Adds form required for community admission

plugins/custom_forms/db/migrate/20131107125327_add_admission_to_form.rb 0 → 100644
... ... @@ -0,0 +1,18 @@
  1 +class AddAdmissionToForm < ActiveRecord::Migration
  2 + def self.up
  3 + change_table :custom_forms_plugin_forms do |t|
  4 + t.boolean :for_admission, :default => false
  5 + end
  6 +
  7 + CustomFormsPlugin::Form.find_each do |f|
  8 + f.for_admission = false
  9 + f.save!
  10 + end
  11 + end
  12 +
  13 + def self.down
  14 + change_table :custom_forms_plugin_forms do |t|
  15 + t.remove :for_admission
  16 + end
  17 + end
  18 +end
... ...
plugins/custom_forms/lib/custom_forms_plugin/admission_survey.rb 0 → 100644
... ... @@ -0,0 +1,24 @@
  1 +class CustomFormsPlugin::AdmissionSurvey < CustomFormsPlugin::MembershipSurvey
  2 +
  3 + def perform
  4 + super
  5 + requestor.add_member(target)
  6 + end
  7 +
  8 + def title
  9 + _("Admission survey")
  10 + end
  11 +
  12 + def information
  13 + {:message => _('%{requestor} wants you to fill in some information before joining.')}
  14 + end
  15 +
  16 + def target_notification_message
  17 + _('Before joining %{requestor}, the administrators of this organization
  18 + wants you to fill in some further information.') % {:requestor => requestor.name}
  19 + end
  20 +
  21 + def target_notification_description
  22 + _('%{requestor} wants you to fill in some further information.') % {:requestor => requestor.name}
  23 + end
  24 +end
... ...
plugins/custom_forms/lib/custom_forms_plugin/form.rb
... ... @@ -18,9 +18,10 @@ class CustomFormsPlugin::Form &lt; Noosfero::Plugin::ActiveRecord
18 18 end
19 19  
20 20 named_scope :from, lambda {|profile| {:conditions => {:profile_id => profile.id}}}
21   - named_scope :on_memberships, {:conditions => {:on_membership => true}}
  21 + named_scope :on_memberships, {:conditions => {:on_membership => true, :for_admission => false}}
  22 + named_scope :for_admissions, {:conditions => {:for_admission => true}}
22 23 =begin
23   - named_scope :accessible_to lambda do |profile|
  24 + named_scope :accessible_to lambda do |profile|
24 25 #TODO should verify is profile is associated with the form owner
25 26 profile_associated = ???
26 27 {:conditions => ["
... ... @@ -60,7 +61,7 @@ class CustomFormsPlugin::Form &lt; Noosfero::Plugin::ActiveRecord
60 61 elsif access.kind_of?(Array)
61 62 access.each do |value|
62 63 if !value.kind_of?(Integer) || !Profile.exists?(value)
63   - errors.add(:access, _('There is no profile with the provided id.'))
  64 + errors.add(:access, _('There is no profile with the provided id.'))
64 65 break
65 66 end
66 67 end
... ...
plugins/custom_forms/lib/ext/role_assignment_trigger.rb
... ... @@ -13,7 +13,26 @@ module RoleAssignmentTrigger
13 13 end
14 14 end
15 15 end
16   -
  16 +
  17 + before_validation_on_create do |ra|
  18 + proceed_creation = true
  19 + if ra.resource.kind_of?(Profile)
  20 + profile = ra.resource
  21 + person = ra.accessor
  22 + ok = !profile.nil? && !person.nil? && profile.environment.present?
  23 + if ok && profile.environment.plugin_enabled?(CustomFormsPlugin) && !person.is_member_of?(profile)
  24 + CustomFormsPlugin::Form.from(profile).for_admissions.each do |form|
  25 + admission_task_pending = person.tasks.pending.select {|task| task.kind_of?(CustomFormsPlugin::AdmissionSurvey) && task.form_id == form.id }.present?
  26 + admission_task_finished = person.tasks.finished.select {|task| task.kind_of?(CustomFormsPlugin::AdmissionSurvey) && task.form_id == form.id }.present?
  27 +
  28 + CustomFormsPlugin::AdmissionSurvey.create!(:requestor => profile, :target => person, :form_id => form.id) unless admission_task_finished || admission_task_pending
  29 + proceed_creation = false unless admission_task_finished
  30 + end
  31 + end
  32 + end
  33 + proceed_creation
  34 + end
  35 +
17 36 after_destroy do |ra|
18 37 if ra.resource.kind_of?(Profile)
19 38 profile = ra.resource
... ... @@ -24,6 +43,10 @@ module RoleAssignmentTrigger
24 43 task = person.tasks.pending.select {|task| task.kind_of?(CustomFormsPlugin::MembershipSurvey) && task.form_id == form.id}.first
25 44 task.cancel if task
26 45 end
  46 + CustomFormsPlugin::Form.from(profile).for_admissions.each do |form|
  47 + task = person.tasks.pending.select {|task| task.kind_of?(CustomFormsPlugin::MembershipSurvey) && task.form_id == form.id}.first
  48 + task.cancel if task
  49 + end
27 50 end
28 51 end
29 52 end
... ...
plugins/custom_forms/test/unit/custom_forms_plugin/admission_survey_test.rb 0 → 100644
... ... @@ -0,0 +1,14 @@
  1 +require File.dirname(__FILE__) + '/../../../../../test/test_helper'
  2 +
  3 +class CustomFormsPlugin::MembershipSurveyTest < ActiveSupport::TestCase
  4 + should 'add member to community on perform' do
  5 + profile = fast_create(Community)
  6 + person = fast_create(Person)
  7 + form = CustomFormsPlugin::Form.create!(:name => 'Simple Form', :profile => profile)
  8 + task = CustomFormsPlugin::AdmissionSurvey.create!(:form_id => form.id, :target => person, :requestor => profile)
  9 +
  10 + assert_difference person.memberships, :count, 1 do
  11 + task.finish
  12 + end
  13 + end
  14 +end
... ...
plugins/custom_forms/test/unit/custom_forms_plugin/form_test.rb
... ... @@ -188,4 +188,27 @@ class CustomFormsPlugin::FormTest &lt; ActiveSupport::TestCase
188 188 assert_equal form.fields, [url_field, license_field]
189 189 end
190 190  
  191 + should 'have a named_scope that retrieves all forms required for membership' do
  192 + profile = fast_create(Profile)
  193 + f1 = CustomFormsPlugin::Form.create!(:name => 'For admission 1', :profile => profile, :for_admission => true)
  194 + f2 = CustomFormsPlugin::Form.create!(:name => 'For admission 2', :profile => profile, :for_admission => true)
  195 + f3 = CustomFormsPlugin::Form.create!(:name => 'Not for admission', :profile => profile, :for_admission => false)
  196 + scope = CustomFormsPlugin::Form.from(profile).for_admissions
  197 +
  198 + assert_equal ActiveRecord::NamedScope::Scope, scope.class
  199 + assert_includes scope, f1
  200 + assert_includes scope, f2
  201 + assert_not_includes scope, f3
  202 + end
  203 +
  204 + should 'not include admission membership in on membership named scope' do
  205 + profile = fast_create(Profile)
  206 + f1 = CustomFormsPlugin::Form.create!(:name => 'On membership', :profile => profile, :on_membership => true)
  207 + f2 = CustomFormsPlugin::Form.create!(:name => 'For admission', :profile => profile, :on_membership => true, :for_admission => true)
  208 + scope = CustomFormsPlugin::Form.from(profile).on_memberships
  209 +
  210 + assert_equal ActiveRecord::NamedScope::Scope, scope.class
  211 + assert_includes scope, f1
  212 + assert_not_includes scope, f2
  213 + end
191 214 end
... ...
plugins/custom_forms/test/unit/ext/role_assingment_test.rb
... ... @@ -27,25 +27,46 @@ class RoleAssignmentsTest &lt; ActiveSupport::TestCase
27 27 end
28 28 end
29 29  
30   - should 'cancel membership_surveys if membership is undone and task is active' do
  30 + should 'cancel surveys if membership is undone and task is active' do
31 31 environment = Environment.default
32 32 environment.enable_plugin(CustomFormsPlugin)
33 33 organization = fast_create(Organization)
34 34 person = fast_create(Person)
35   - form = CustomFormsPlugin::Form.create!(:profile => organization, :name => 'Form', :on_membership => true)
  35 + form1 = CustomFormsPlugin::Form.create!(:profile => organization, :name => 'Form 1', :on_membership => true)
36 36 organization.add_member(person)
37 37  
38 38 assert_difference CustomFormsPlugin::MembershipSurvey.pending, :count, -1 do
39 39 organization.remove_member(person)
40 40 end
41 41  
  42 + form2 = CustomFormsPlugin::Form.create!(:profile => organization, :name => 'Form 2', :for_admission => true)
42 43 organization.add_member(person)
43   - task = CustomFormsPlugin::MembershipSurvey.last
44   - task.status = Task::Status::FINISHED
45   - task.save!
  44 +
  45 + assert_difference CustomFormsPlugin::AdmissionSurvey.pending, :count, -1 do
  46 + organization.remove_member(person)
  47 + end
  48 +
  49 + organization.add_member(person)
  50 + tasks = CustomFormsPlugin::MembershipSurvey.all.last(2)
  51 + tasks.each {|t| t.status = Task::Status::FINISHED }
  52 + tasks.each {|t| t.save! }
46 53 assert_no_difference CustomFormsPlugin::MembershipSurvey.finished, :count do
47 54 organization.remove_member(person)
48 55 end
49 56 end
50   -end
51 57  
  58 + should 'create admission survey when atempted membership' do
  59 + environment = Environment.default
  60 + environment.enable_plugin(CustomFormsPlugin)
  61 + organization = fast_create(Organization)
  62 + person = fast_create(Person)
  63 + f1 = CustomFormsPlugin::Form.create!(:profile => organization, :name => 'Form 1', :for_admission => true)
  64 + f2 = CustomFormsPlugin::Form.create!(:profile => organization, :name => 'Form 2', :for_admission => true)
  65 + f3 = CustomFormsPlugin::Form.create!(:profile => organization, :name => 'Form 3', :for_admission => false)
  66 +
  67 + assert_difference CustomFormsPlugin::AdmissionSurvey, :count, 2 do
  68 + organization.add_member(person)
  69 + end
  70 + assert !organization.members.include?(person)
  71 + end
  72 +end
... ...
plugins/custom_forms/views/custom_forms_plugin_myprofile/_form.html.erb
... ... @@ -11,6 +11,7 @@
11 11 )) %>
12 12 <%= labelled_form_field _('Access'), f.select(:access, access_options(profile))%>
13 13 <% if profile.organization? %>
  14 + <%= labelled_form_field _('Required for membership approval'), f.check_box(:for_admission) %>
14 15 <%= labelled_form_field _('Triggered on membership'), f.check_box(:on_membership) %>
15 16 <% end %>
16 17 <%= labelled_form_field _('Description'), f.text_area(:description, :style => 'width: 100%') %>
... ...