Sha256: f86825c66bde191800e11c1000ab73904a2212091050e728657fad517205c49d

Contents?: true

Size: 1.68 KB

Versions: 11

Compression:

Stored size: 1.68 KB

Contents

module NoNotifierNeeded
  module Send
    def send_now(which_email, *args)
      email_hash = translate_to_hash(which_email, args)
      Resque.enqueue(EmailProcessor, email_hash)
    end

    def send_in(time_from_now, which_email, *args)
      email_hash = translate_to_hash(which_email, args)
      Resque.enqueue_at(time_from_now, EmailProcessor, email_hash)
    end

    def send_at(what_time, which_email, *args)
      email_hash = translate_to_hash(which_email, args)
      time_from_now = what_time.is_a?(Time) ? what_time : Chronic.parse(what_time)
      Resque.enqueue_at(time_from_now, EmailProcessor, email_hash)
    end

    private
    def known_models
      @known_models if @known_models
      @known_models = ActiveRecord::Base.send( :descendants ).flatten.uniq
      @known_models += @known_models.collect{|k| k.send(:descendants) }.flatten.uniq
      @known_models.uniq!
      @known_models
    end

    def translate_to_hash(which_email, args)
      th = {}
      th[:which_email] = which_email

      args = args.flatten if args.respond_to?(:flatten)
      args.each do |a|
        next if a.blank?
        if a.is_a?(Hash)
          a.each do|k,v|
            if v.respond_to?(:gsub)
              th[k.to_sym] = CGI.escapeHTML(v)
            else
              th[k.to_sym] = v
            end
          end
        elsif known_models.include?(a.class.name) || a.class.respond_to?(:column_names)
          th[a.class.name.downcase.to_sym] = a.id
        else
          raise ArgumentError.new("Unknown #{a.class} passed to email procesor. \n Object #{a.inspect} \n\n WhichEmail: #{which_email} \n Args #{args} \n\n Known Models #{known_models}")
        end
      end
      th
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
no_notifier_needed-2.2.4 lib/no_notifier_needed/send.rb
no_notifier_needed-2.2.3 lib/no_notifier_needed/send.rb
no_notifier_needed-2.2.2 lib/no_notifier_needed/send.rb
no_notifier_needed-2.2.1 lib/no_notifier_needed/send.rb
no_notifier_needed-2.2.0 lib/no_notifier_needed/send.rb
no_notifier_needed-2.1.0 lib/no_notifier_needed/send.rb
no_notifier_needed-2.0.17 lib/no_notifier_needed/send.rb
no_notifier_needed-2.0.16 lib/no_notifier_needed/send.rb
no_notifier_needed-2.0.15 lib/no_notifier_needed/send.rb
no_notifier_needed-2.0.14 lib/no_notifier_needed/send.rb
no_notifier_needed-2.0.13 lib/no_notifier_needed/send.rb