app/models/membership_comp.rb in artfully_ose-1.2.0 vs app/models/membership_comp.rb in artfully_ose-1.3.0.pre1

- old
+ new

@@ -13,10 +13,11 @@ attr_accessor :people, :person_id, :segment_id, :search_id, + :advanced_search_id, :organization, :membership_type, :number_of_memberships, :ends_at, :welcome_message, @@ -31,18 +32,13 @@ def persisted? false end - def people_ids - people.nil? ? [] : people.collect(&:id) - end - def people_belong_to_this_org people.each do |person| - Rails.logger.info "FLARG #{person.organization.id} #{organization}" - if person.organization != organization + if person.organization_id != organization.id errors.add(:base, "Person does not belong to this organization. Please contact Artful.ly support if this problem persists.") end end end @@ -55,10 +51,34 @@ # def clear_errors @errors = nil end + # + # TODO: This is a colossal memory/performance hit if @ids.length > a few hundred + # refactor this controller/model to not deal with the actual + # people objects unless absolutely necessary + # + def find_people + + return self.people unless self.people.blank? + + if !self.person_id.blank? + self.people = Array.wrap(Person.find(self.person_id)) + elsif !self.segment_id.blank? + self.people = self.organization.segments.where(:id => self.segment_id).first.people + elsif !self.search_id.blank? + self.people = self.organization.searches.where(:id => self.search_id).first.people + elsif !self.advanced_search_id.blank? + self.people = self.organization.advanced_searches.where(:id => self.advanced_search_id).first.people + else + self.people = [] + end + + self.people + end + def we_have_some_people if people.blank? errors.add(:base, "You haven't selected any people to receive memberships") end end @@ -80,26 +100,31 @@ return false unless valid? MembershipCompJob.enqueue self end def perform - self.people.each do |person| - next if person.email.blank? - next if person.company? - memberships = [] - self.number_of_memberships.to_i.times do - membership = Membership.for(self.membership_type) - membership.ends_at = self.ends_at - membership.sold_price = 0 - membership.total_paid = 0 - membership.welcome_message = self.welcome_message - membership.send_email = self.send_email - membership.save - memberships << membership - end + find_people - comp = Comp.new(nil, [], memberships, person, self.benefactor) - comp.notes = self.notes - comp.submit + ActiveRecord::Base.transaction do + self.people.each do |person| + next if person.email.blank? + next if person.company? + next if person.organization_id != self.organization.id + memberships = [] + self.number_of_memberships.to_i.times do + membership = Membership.for(self.membership_type) + membership.ends_at = self.ends_at + membership.sold_price = 0 + membership.total_paid = 0 + membership.welcome_message = self.welcome_message + membership.send_email = self.send_email + membership.save + memberships << membership + end + + comp = Comp.new([], memberships, [], person, self.benefactor) + comp.notes = self.notes + comp.submit + end end end end \ No newline at end of file