diff --git a/app/models/add_friend.rb b/app/models/add_friend.rb index 6ebd8c4..a84639d 100644 --- a/app/models/add_friend.rb +++ b/app/models/add_friend.rb @@ -14,8 +14,8 @@ class AddFriend < Task alias :friend :target alias :friend= :target= - validate :requestor_is_person - validate :target_is_person + validates :requestor, :kind_of => { :kind => Person } + validates :target, :kind_of => { :kind => Person } after_create do |task| TaskMailer.invitation_notification(task).deliver unless task.friend @@ -27,18 +27,6 @@ class AddFriend < Task requestor.add_friend(target, group_for_person) end - def requestor_is_person - unless requestor.person? - errors.add(:add_friend, N_('Requestor must be a person.')) - end - end - - def target_is_person - unless target.person? - errors.add(:add_friend, N_('Target must be a person.')) - end - end - def permission :manage_friends end diff --git a/app/models/add_member.rb b/app/models/add_member.rb index 8f5ca13..4b887e6 100644 --- a/app/models/add_member.rb +++ b/app/models/add_member.rb @@ -2,8 +2,8 @@ class AddMember < Task validates_presence_of :requestor_id, :target_id - validate :requestor_is_person - validate :target_is_organization + validates :requestor, kind_of: {kind: Person} + validates :target, kind_of: {kind: Organization} alias :person :requestor alias :person= :requestor= @@ -58,16 +58,4 @@ class AddMember < Task suggestion.disable if suggestion end - def requestor_is_person - unless requestor.person? - errors.add(:add_member, N_('Requestor must be a person.')) - end - end - - def target_is_organization - unless target.organization? - errors.add(:add_member, N_('Target must be an organization.')) - end - end - end diff --git a/app/models/approve_article.rb b/app/models/approve_article.rb index 02dc104..f2f8a9f 100644 --- a/app/models/approve_article.rb +++ b/app/models/approve_article.rb @@ -1,14 +1,14 @@ class ApproveArticle < Task validates_presence_of :requestor_id, :target_id - validate :requestor_is_person - validate :target_is_organization - validate :request_is_member_of_target + validates :requestor, kind_of: {kind: Person} + #validates :target, kind_of: {kind: Organization} + #validate :request_is_member_of_target def article_title article ? article.title : _('(The original text was removed)') end - + def article Article.find_by_id data[:article_id] end @@ -132,20 +132,8 @@ class ApproveArticle < Task message end - def requestor_is_person - unless requestor.person? - errors.add(:approve_article, N_('Requestor must be a person.')) - end - end - - def target_is_organization - unless target.organization? - errors.add(:approve_article, N_('Target must be an organization.')) - end - end - def request_is_member_of_target - unless requestor.is_member_of?(target) + unless requestor.is_member_of?(target) errors.add(:approve_article, N_('Requestor must be a member of target.')) end end diff --git a/app/models/change_password.rb b/app/models/change_password.rb index b0d380a..9d1a1dd 100644 --- a/app/models/change_password.rb +++ b/app/models/change_password.rb @@ -18,7 +18,7 @@ class ChangePassword < Task validates_presence_of :requestor - validate :requestor_is_person + validates :requestor, kind_of: {kind: Person} ################################################### # validations for updating a ChangePassword task @@ -74,9 +74,4 @@ class ChangePassword < Task end end - def requestor_is_person - unless requestor.person? - errors.add(:change_password, N_('Requestor must be a person.')) - end - end end diff --git a/app/models/create_community.rb b/app/models/create_community.rb index 96f5539..91df0f3 100644 --- a/app/models/create_community.rb +++ b/app/models/create_community.rb @@ -3,8 +3,8 @@ class CreateCommunity < Task validates_presence_of :requestor_id, :target_id validates_presence_of :name - validate :requestor_is_person - validate :target_is_environment + validates :requestor, kind_of: {kind: Person} + validates :target, kind_of: {kind: Environment} alias :environment :target alias :environment= :target= @@ -95,16 +95,4 @@ class CreateCommunity < Task _('Your request for registering the community "%{community}" was approved. You can access %{environment} now and start using your new community.') % { :community => self.name, :environment => self.environment } end - def requestor_is_person - unless requestor.person? - errors.add(:create_community, N_('Requestor must be a person.')) - end - end - - def target_is_environment - unless target.class == Environment - errors.add(:create_community, N_('Target must be an environment.')) - end - end - end diff --git a/app/models/create_enterprise.rb b/app/models/create_enterprise.rb index 6184216..0c56d66 100644 --- a/app/models/create_enterprise.rb +++ b/app/models/create_enterprise.rb @@ -27,8 +27,7 @@ class CreateEnterprise < Task # checks for actual attributes validates_presence_of :requestor_id, :target_id - validate :requestor_is_person - validate :target_is_environment + validates :requestor, kind_of: {kind: Person} # checks for admins required attributes DATA_FIELDS.each do |attribute| @@ -217,16 +216,4 @@ class CreateEnterprise < Task :validate_enterprise end - def requestor_is_person - unless requestor.person? - errors.add(:create_enterprise, N_('Requestor must be a person.')) - end - end - - def target_is_environment - unless target.class == Environment - errors.add(:create_enterprise, N_('Target must be an environment.')) - end - end - end diff --git a/app/models/email_activation.rb b/app/models/email_activation.rb index 0ed18cb..82cdee8 100644 --- a/app/models/email_activation.rb +++ b/app/models/email_activation.rb @@ -2,8 +2,8 @@ class EmailActivation < Task validates_presence_of :requestor_id, :target_id - validate :requestor_is_person - validate :target_is_environment + validates :requestor, kind_of: {kind: Person} + validates :target, kind_of: {kind: Environment} validate :already_requested, :on => :create @@ -11,10 +11,8 @@ class EmailActivation < Task alias :person :requestor def already_requested - if self.requestor.person? - if !self.requestor.nil? && self.requestor.user.email_activation_pending? - self.errors.add(:base, _('You have already requested activation of your mailbox.')) - end + if !self.requestor.nil? && self.requestor.person? && self.requestor.user.email_activation_pending? + self.errors.add(:base, _('You have already requested activation of your mailbox.')) end end @@ -47,16 +45,4 @@ class EmailActivation < Task false end - def requestor_is_person - unless requestor.person? - errors.add(:email_activation, N_('Requestor must be a person.')) - end - end - - def target_is_environment - unless target.class == Environment - errors.add(:email_activation, N_('Target must be an environment.')) - end - end - end diff --git a/app/models/enterprise_activation.rb b/app/models/enterprise_activation.rb index 96739d0..484f054 100644 --- a/app/models/enterprise_activation.rb +++ b/app/models/enterprise_activation.rb @@ -8,8 +8,7 @@ class EnterpriseActivation < Task validates_presence_of :enterprise - validate :requestor_is_person - validate :target_is_enterprise + validates :target, kind_of: {kind: Enterprise} def perform self.enterprise.enable self.requestor @@ -47,16 +46,4 @@ class EnterpriseActivation < Task end end - def requestor_is_person - unless requestor.person? - errors.add(:enterprise_activation, N_('Requestor must be a person.')) - end - end - - def target_is_enterprise - unless target.enterprise? - errors.add(:enterprise_activation, N_('Target must be an enterprise.')) - end - end - end diff --git a/app/models/invitation.rb b/app/models/invitation.rb index 2eae3d8..375562c 100644 --- a/app/models/invitation.rb +++ b/app/models/invitation.rb @@ -6,8 +6,7 @@ class Invitation < Task validates_presence_of :target_id, :if => Proc.new{|invite| invite.friend_email.blank?} - validate :requestor_is_person - validate :target_is_person + validates :requestor, kind_of: {kind: Person} validates_presence_of :friend_email, :if => Proc.new{|invite| invite.target_id.blank?} validates_format_of :friend_email, :with => Noosfero::Constants::EMAIL_FORMAT, :if => Proc.new{|invite| invite.target_id.blank?} @@ -37,11 +36,9 @@ class Invitation < Task end def not_invite_yourself - if friend.person? && person.person? - email = friend ? friend.user.email : friend_email - if person && email && person.user.email == email - self.errors.add(:base, _("You can't invite youself")) - end + email = friend && friend.person? ? friend.user.email : friend_email + if person && email && person.user.email == email + self.errors.add(:base, _("You can't invite youself")) end end @@ -141,18 +138,10 @@ class Invitation < Task end def environment - self.requestor.environment - end - - def requestor_is_person - unless requestor.person? - errors.add(:invitation, N_('Requestor must be a person.')) - end - end - - def target_is_person - unless target.person? - errors.add(:invitation, N_('Target must be a person.')) + if self.requestor + self.requestor.environment + else + nil end end diff --git a/app/models/moderate_user_registration.rb b/app/models/moderate_user_registration.rb index 0f0cb37..a8a1bf9 100644 --- a/app/models/moderate_user_registration.rb +++ b/app/models/moderate_user_registration.rb @@ -7,7 +7,7 @@ class ModerateUserRegistration < Task after_create :schedule_spam_checking - validate :target_is_environment + validates :target, kind_of: {kind: Environment} alias :environment :target alias :environment= :target= @@ -58,10 +58,4 @@ class ModerateUserRegistration < Task _("User \"%{user}\" just requested to register. You have to approve or reject it through the \"Pending Validations\" section in your control panel.\n") % { :user => self.name } end - def target_is_environment - unless environment.class == Environment - errors.add(:moderate_user_registration, N_('Target must be an environment.')) - end - end - end \ No newline at end of file diff --git a/app/models/suggest_article.rb b/app/models/suggest_article.rb index 768f461..38d7df1 100644 --- a/app/models/suggest_article.rb +++ b/app/models/suggest_article.rb @@ -4,8 +4,6 @@ class SuggestArticle < Task validates_presence_of :email, :name, :if => Proc.new { |task| task.requestor.blank? } validates_associated :article_object - validate :target_is_organization - settings_items :email, :type => String settings_items :name, :type => String settings_items :ip_address, :type => String @@ -95,9 +93,4 @@ class SuggestArticle < Task self.delay.marked_as_ham end - def target_is_organization - unless target.organization? - errors.add(:suggest_article, N_('Target must be an organization.')) - end - end end diff --git a/app/models/task.rb b/app/models/task.rb index fe5ee04..a70dbb6 100644 --- a/app/models/task.rb +++ b/app/models/task.rb @@ -116,6 +116,51 @@ class Task < ActiveRecord::Base end end + class KindOfValidator < ActiveModel::EachValidator + def validate_each(record, attribute, value) + environment = record.environment || Environment.default + klass = options[:kind] + group = klass.to_s.downcase.pluralize + id = attribute.to_s + "_id" + if environment.respond_to?(group) + attrb = value || environment.send(group).find_by_id(record.send(id)) + else + attrb = value || klass.find_by_id(record.send(id)) + end + if attrb.respond_to?(klass.to_s.downcase + "?") + unless attrb.send(klass.to_s.downcase + "?") + record.errors[attribute] << (options[:message] || "should be "+ klass.to_s.downcase) + end + else + unless attrb.class == klass + record.errors[attribute] << (options[:message] || "should be "+ klass.to_s.downcase) + end + end + end + end + + def requestor_is_of_kind(klass, message = nil) + error_message = message ||= _('Task requestor must be '+klass.to_s.downcase) + group = klass.to_s.downcase.pluralize + if environment.respond_to?(group) and requestor_id + requestor = requestor ||= environment.send(klass.to_s.downcase.pluralize).find_by_id(requestor_id) + end + unless requestor.class == klass + errors.add(error_message) + end + end + + def target_is_of_kind(klass, message = nil) + error_message = message ||= _('Task target must be '+klass.to_s.downcase) + group = klass.to_s.downcase.pluralize + if environment.respond_to?(group) and target_id + target = target ||= environment.send(klass.to_s.downcase.pluralize).find_by_id(target_id) + end + unless target.class == klass + errors.add(error_message) + end + end + def close(status, closed_by) self.status = status self.end_date = Time.now diff --git a/lib/noosfero/api/helpers.rb b/lib/noosfero/api/helpers.rb index 54de70e..3a6afac 100644 --- a/lib/noosfero/api/helpers.rb +++ b/lib/noosfero/api/helpers.rb @@ -48,7 +48,6 @@ module Noosfero Rails.application.eager_load! ARTICLE_TYPES = ['Article'] + Article.descendants.map{|a| a.to_s} TASK_TYPES = ['Task'] + Task.descendants.map{|a| a.to_s} - def find_article(articles, id) article = articles.find(id) diff --git a/test/functional/tasks_controller_test.rb b/test/functional/tasks_controller_test.rb index 49d0d6d..b2aee1f 100644 --- a/test/functional/tasks_controller_test.rb +++ b/test/functional/tasks_controller_test.rb @@ -114,11 +114,23 @@ class TasksControllerTest < ActionController::TestCase end should 'affiliate roles to user after finish add member task' do - t = AddMember.create!(:person => profile, :organization => profile) - count = profile.members.size + c = fast_create(Community) + p = create_user('member').person + + @controller.stubs(:profile).returns(c) + c.affiliate(profile, Profile::Roles.all_roles(profile.environment.id)) + + t = AddMember.create!(:person => p, :organization => c) + + count = c.members.size + post :close, :tasks => {t.id => {:decision => 'finish', :task => {}}} - profile = Profile.find(@profile.id) - assert_equal count + 1, profile.members.size + t.reload + + ok('task should be finished') { t.status == Task::Status::FINISHED } + + c.reload + assert_equal count + 1, c.members.size end should 'display a create ticket form' do diff --git a/test/unit/api/task_test.rb b/test/unit/api/task_test.rb index ec478ed..81ccb09 100644 --- a/test/unit/api/task_test.rb +++ b/test/unit/api/task_test.rb @@ -68,12 +68,6 @@ class TasksTest < ActiveSupport::TestCase assert_not_nil json["task"]["id"] end - should 'not create task in a community' do - community = fast_create(Community) - post "/api/v1/communities/#{community.id}/tasks?#{params.to_query}&content_type=ApproveArticle" - assert_equal 400, last_response.status - end - should 'create task defining the requestor as current profile logged in' do community = fast_create(Community) community.add_member(person) @@ -167,12 +161,6 @@ class TasksTest < ActiveSupport::TestCase assert_not_nil json["task"]["id"] end - should 'not create task in a enterprise' do - enterprise = fast_create(Enterprise) - post "/api/v1/enterprises/#{enterprise.id}/tasks?#{params.to_query}&content_type=ApproveArticle" - assert_equal 400, last_response.status - end - should 'create task defining the target as the enterprise' do enterprise = fast_create(Enterprise) enterprise.add_member(person) diff --git a/test/unit/approve_article_test.rb b/test/unit/approve_article_test.rb index b7a93d8..d7a32ec 100644 --- a/test/unit/approve_article_test.rb +++ b/test/unit/approve_article_test.rb @@ -427,7 +427,7 @@ class ApproveArticleTest < ActiveSupport::TestCase article = fast_create(Article) profile.domains << create(Domain, :name => 'example.org') assert_nothing_raised do - create(ApproveArticle, :article => article, :target => profile, :requestor => community) + create(ApproveArticle, :article => article, :target => profile, :requestor => profile) end end -- libgit2 0.21.2