cirandas.net

ref: master

app/mailers/organization_mailing.rb


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
class OrganizationMailing < Mailing

  attr_accessible :recipient_ids

  settings_items :recipient_ids, type: Array

  def generate_from
    "#{person.name} <#{source.environment.noreply_email}>"
  end

  def recipients(offset=0, limit=100)
    result = source.members.order(:id).offset(offset).limit(limit)

    if data.present? and data.is_a?(Hash) and data[:members_filtered]
      result = result.where('profiles.id IN (?)', data[:members_filtered])
    end

    if result.blank?
      result = result.joins("LEFT OUTER JOIN mailing_sents m ON (m.mailing_id = #{id} AND m.person_id = profiles.id)")
      .where("m.person_id" => nil)
    end
    result
  end

  def each_recipient
    offset = 0
    limit = 50
    while (people = recipients.offset(offset).limit(limit)).present?
      people.each do |person|
        yield person
      end
      offset = offset + limit
    end
  end

  def signature_message
    _('Sent by community %s.').html_safe % source.name
  end

  include Rails.application.routes.url_helpers
  def url
    url_for(source.url)
  end
end