diff --git a/plugins/custom_forms/db/migrate/20131107125327_add_admission_to_form.rb b/plugins/custom_forms/db/migrate/20131107125327_add_admission_to_form.rb new file mode 100644 index 0000000..ac18e06 --- /dev/null +++ b/plugins/custom_forms/db/migrate/20131107125327_add_admission_to_form.rb @@ -0,0 +1,18 @@ +class AddAdmissionToForm < ActiveRecord::Migration + def self.up + change_table :custom_forms_plugin_forms do |t| + t.boolean :for_admission, :default => false + end + + CustomFormsPlugin::Form.find_each do |f| + f.for_admission = false + f.save! + end + end + + def self.down + change_table :custom_forms_plugin_forms do |t| + t.remove :for_admission + end + end +end diff --git a/plugins/custom_forms/lib/custom_forms_plugin/admission_survey.rb b/plugins/custom_forms/lib/custom_forms_plugin/admission_survey.rb new file mode 100644 index 0000000..66ec867 --- /dev/null +++ b/plugins/custom_forms/lib/custom_forms_plugin/admission_survey.rb @@ -0,0 +1,24 @@ +class CustomFormsPlugin::AdmissionSurvey < CustomFormsPlugin::MembershipSurvey + + def perform + super + requestor.add_member(target) + end + + def title + _("Admission survey") + end + + def information + {:message => _('%{requestor} wants you to fill in some information before joining.')} + end + + def target_notification_message + _('Before joining %{requestor}, the administrators of this organization + wants you to fill in some further information.') % {:requestor => requestor.name} + end + + def target_notification_description + _('%{requestor} wants you to fill in some further information.') % {:requestor => requestor.name} + end +end diff --git a/plugins/custom_forms/lib/custom_forms_plugin/form.rb b/plugins/custom_forms/lib/custom_forms_plugin/form.rb index 6acbf27..a3f1154 100644 --- a/plugins/custom_forms/lib/custom_forms_plugin/form.rb +++ b/plugins/custom_forms/lib/custom_forms_plugin/form.rb @@ -18,9 +18,10 @@ class CustomFormsPlugin::Form < Noosfero::Plugin::ActiveRecord end named_scope :from, lambda {|profile| {:conditions => {:profile_id => profile.id}}} - named_scope :on_memberships, {:conditions => {:on_membership => true}} + named_scope :on_memberships, {:conditions => {:on_membership => true, :for_admission => false}} + named_scope :for_admissions, {:conditions => {:for_admission => true}} =begin - named_scope :accessible_to lambda do |profile| + named_scope :accessible_to lambda do |profile| #TODO should verify is profile is associated with the form owner profile_associated = ??? {:conditions => [" @@ -60,7 +61,7 @@ class CustomFormsPlugin::Form < Noosfero::Plugin::ActiveRecord elsif access.kind_of?(Array) access.each do |value| if !value.kind_of?(Integer) || !Profile.exists?(value) - errors.add(:access, _('There is no profile with the provided id.')) + errors.add(:access, _('There is no profile with the provided id.')) break end end diff --git a/plugins/custom_forms/lib/ext/role_assignment_trigger.rb b/plugins/custom_forms/lib/ext/role_assignment_trigger.rb index 013d7b0..d3145f7 100644 --- a/plugins/custom_forms/lib/ext/role_assignment_trigger.rb +++ b/plugins/custom_forms/lib/ext/role_assignment_trigger.rb @@ -13,7 +13,26 @@ module RoleAssignmentTrigger end end end - + + before_validation_on_create do |ra| + proceed_creation = true + if ra.resource.kind_of?(Profile) + profile = ra.resource + person = ra.accessor + ok = !profile.nil? && !person.nil? && profile.environment.present? + if ok && profile.environment.plugin_enabled?(CustomFormsPlugin) && !person.is_member_of?(profile) + CustomFormsPlugin::Form.from(profile).for_admissions.each do |form| + admission_task_pending = person.tasks.pending.select {|task| task.kind_of?(CustomFormsPlugin::AdmissionSurvey) && task.form_id == form.id }.present? + admission_task_finished = person.tasks.finished.select {|task| task.kind_of?(CustomFormsPlugin::AdmissionSurvey) && task.form_id == form.id }.present? + + CustomFormsPlugin::AdmissionSurvey.create!(:requestor => profile, :target => person, :form_id => form.id) unless admission_task_finished || admission_task_pending + proceed_creation = false unless admission_task_finished + end + end + end + proceed_creation + end + after_destroy do |ra| if ra.resource.kind_of?(Profile) profile = ra.resource @@ -24,6 +43,10 @@ module RoleAssignmentTrigger task = person.tasks.pending.select {|task| task.kind_of?(CustomFormsPlugin::MembershipSurvey) && task.form_id == form.id}.first task.cancel if task end + CustomFormsPlugin::Form.from(profile).for_admissions.each do |form| + task = person.tasks.pending.select {|task| task.kind_of?(CustomFormsPlugin::MembershipSurvey) && task.form_id == form.id}.first + task.cancel if task + end end end end diff --git a/plugins/custom_forms/test/unit/custom_forms_plugin/admission_survey_test.rb b/plugins/custom_forms/test/unit/custom_forms_plugin/admission_survey_test.rb new file mode 100644 index 0000000..df9e3b8 --- /dev/null +++ b/plugins/custom_forms/test/unit/custom_forms_plugin/admission_survey_test.rb @@ -0,0 +1,14 @@ +require File.dirname(__FILE__) + '/../../../../../test/test_helper' + +class CustomFormsPlugin::MembershipSurveyTest < ActiveSupport::TestCase + should 'add member to community on perform' do + profile = fast_create(Community) + person = fast_create(Person) + form = CustomFormsPlugin::Form.create!(:name => 'Simple Form', :profile => profile) + task = CustomFormsPlugin::AdmissionSurvey.create!(:form_id => form.id, :target => person, :requestor => profile) + + assert_difference person.memberships, :count, 1 do + task.finish + end + end +end diff --git a/plugins/custom_forms/test/unit/custom_forms_plugin/form_test.rb b/plugins/custom_forms/test/unit/custom_forms_plugin/form_test.rb index bafb6d4..6acecd4 100644 --- a/plugins/custom_forms/test/unit/custom_forms_plugin/form_test.rb +++ b/plugins/custom_forms/test/unit/custom_forms_plugin/form_test.rb @@ -188,4 +188,27 @@ class CustomFormsPlugin::FormTest < ActiveSupport::TestCase assert_equal form.fields, [url_field, license_field] end + should 'have a named_scope that retrieves all forms required for membership' do + profile = fast_create(Profile) + f1 = CustomFormsPlugin::Form.create!(:name => 'For admission 1', :profile => profile, :for_admission => true) + f2 = CustomFormsPlugin::Form.create!(:name => 'For admission 2', :profile => profile, :for_admission => true) + f3 = CustomFormsPlugin::Form.create!(:name => 'Not for admission', :profile => profile, :for_admission => false) + scope = CustomFormsPlugin::Form.from(profile).for_admissions + + assert_equal ActiveRecord::NamedScope::Scope, scope.class + assert_includes scope, f1 + assert_includes scope, f2 + assert_not_includes scope, f3 + end + + should 'not include admission membership in on membership named scope' do + profile = fast_create(Profile) + f1 = CustomFormsPlugin::Form.create!(:name => 'On membership', :profile => profile, :on_membership => true) + f2 = CustomFormsPlugin::Form.create!(:name => 'For admission', :profile => profile, :on_membership => true, :for_admission => true) + scope = CustomFormsPlugin::Form.from(profile).on_memberships + + assert_equal ActiveRecord::NamedScope::Scope, scope.class + assert_includes scope, f1 + assert_not_includes scope, f2 + end end diff --git a/plugins/custom_forms/test/unit/ext/role_assingment_test.rb b/plugins/custom_forms/test/unit/ext/role_assingment_test.rb index 2d14999..2c6bc5d 100644 --- a/plugins/custom_forms/test/unit/ext/role_assingment_test.rb +++ b/plugins/custom_forms/test/unit/ext/role_assingment_test.rb @@ -27,25 +27,46 @@ class RoleAssignmentsTest < ActiveSupport::TestCase end end - should 'cancel membership_surveys if membership is undone and task is active' do + should 'cancel surveys if membership is undone and task is active' do environment = Environment.default environment.enable_plugin(CustomFormsPlugin) organization = fast_create(Organization) person = fast_create(Person) - form = CustomFormsPlugin::Form.create!(:profile => organization, :name => 'Form', :on_membership => true) + form1 = CustomFormsPlugin::Form.create!(:profile => organization, :name => 'Form 1', :on_membership => true) organization.add_member(person) assert_difference CustomFormsPlugin::MembershipSurvey.pending, :count, -1 do organization.remove_member(person) end + form2 = CustomFormsPlugin::Form.create!(:profile => organization, :name => 'Form 2', :for_admission => true) organization.add_member(person) - task = CustomFormsPlugin::MembershipSurvey.last - task.status = Task::Status::FINISHED - task.save! + + assert_difference CustomFormsPlugin::AdmissionSurvey.pending, :count, -1 do + organization.remove_member(person) + end + + organization.add_member(person) + tasks = CustomFormsPlugin::MembershipSurvey.all.last(2) + tasks.each {|t| t.status = Task::Status::FINISHED } + tasks.each {|t| t.save! } assert_no_difference CustomFormsPlugin::MembershipSurvey.finished, :count do organization.remove_member(person) end end -end + should 'create admission survey when atempted membership' do + environment = Environment.default + environment.enable_plugin(CustomFormsPlugin) + organization = fast_create(Organization) + person = fast_create(Person) + f1 = CustomFormsPlugin::Form.create!(:profile => organization, :name => 'Form 1', :for_admission => true) + f2 = CustomFormsPlugin::Form.create!(:profile => organization, :name => 'Form 2', :for_admission => true) + f3 = CustomFormsPlugin::Form.create!(:profile => organization, :name => 'Form 3', :for_admission => false) + + assert_difference CustomFormsPlugin::AdmissionSurvey, :count, 2 do + organization.add_member(person) + end + assert !organization.members.include?(person) + end +end diff --git a/plugins/custom_forms/views/custom_forms_plugin_myprofile/_form.html.erb b/plugins/custom_forms/views/custom_forms_plugin_myprofile/_form.html.erb index 63a8e21..aa5db09 100644 --- a/plugins/custom_forms/views/custom_forms_plugin_myprofile/_form.html.erb +++ b/plugins/custom_forms/views/custom_forms_plugin_myprofile/_form.html.erb @@ -11,6 +11,7 @@ )) %> <%= labelled_form_field _('Access'), f.select(:access, access_options(profile))%> <% if profile.organization? %> + <%= labelled_form_field _('Required for membership approval'), f.check_box(:for_admission) %> <%= labelled_form_field _('Triggered on membership'), f.check_box(:on_membership) %> <% end %> <%= labelled_form_field _('Description'), f.text_area(:description, :style => 'width: 100%') %> -- libgit2 0.21.2