diff --git a/app/models/organization.rb b/app/models/organization.rb index d718866..c540154 100644 --- a/app/models/organization.rb +++ b/app/models/organization.rb @@ -28,7 +28,7 @@ class Organization < Profile end def find_pending_validation(code) - validations.pending.find { |pending| pending.code == code } + validations.pending.find(:first, :conditions => {:code => code}) end def processed_validations @@ -36,7 +36,7 @@ class Organization < Profile end def find_processed_validation(code) - validations.finished.find { |pending| pending.code == code } + validations.finished.find(:first, :conditions => {:code => code}) end def is_validation_entity? @@ -93,4 +93,7 @@ class Organization < Profile [contact_email.blank? ? nil : contact_email].compact + admins.map(&:email) end + def already_request_membership?(person) + self.tasks.pending.find_by_requestor_id(person.id, :conditions => { :type => 'AddMember' }) + end end diff --git a/app/models/profile.rb b/app/models/profile.rb index e9498d2..883b735 100644 --- a/app/models/profile.rb +++ b/app/models/profile.rb @@ -475,7 +475,7 @@ private :generate_url, :url_options def add_member(person) if self.has_members? if self.closed? - AddMember.create!(:person => person, :organization => self) + AddMember.create!(:person => person, :organization => self) unless self.already_request_membership?(person) else self.affiliate(person, Profile::Roles.member(environment.id)) end diff --git a/app/models/task.rb b/app/models/task.rb index 5f5f82e..d7b8f04 100644 --- a/app/models/task.rb +++ b/app/models/task.rb @@ -176,15 +176,10 @@ class Task < ActiveRecord::Base end end - class << self + named_scope :pending, :conditions => { :status => Task::Status::ACTIVE } + named_scope :finished, :conditions => { :status => [Task::Status::CANCELLED, Task::Status::FINISHED] } - def pending - self.find(:all, :conditions => { :status => Task::Status::ACTIVE }) - end - - def finished - self.find(:all, :conditions => { :status => [Task::Status::CANCELLED, Task::Status::FINISHED]}) - end + class << self # generates a random code string consisting of length characters (or 36 by # default) in the ranges a-z and 0-9 diff --git a/test/unit/community_test.rb b/test/unit/community_test.rb index c9bfd52..450829f 100644 --- a/test/unit/community_test.rb +++ b/test/unit/community_test.rb @@ -187,4 +187,17 @@ class CommunityTest < Test::Unit::TestCase Community.create_after_moderation(person, {:environment => env, :name => 'Example'}) end end + + should 'not create new request membership if it already exists' do + community = fast_create(Community) + community.closed = true + community.save + assert_difference AddMember, :count do + community.add_member(person) + end + + assert_no_difference AddMember, :count do + community.add_member(person) + end + end end -- libgit2 0.21.2