module JamRuby
  class EmailBatchPeriodic < EmailBatch
    self.abstract_class = true

    def time_since_last_batch_query
      self.class
          .where(['created_at < ?', self.created_at])
          .where(:aasm_state => 'delivered')
          .order('created_at DESC')
          .limit(1)
    end    

    def time_since_last_batch(default_days=2)
      if previous = self.time_since_last_batch_query.first
        return previous.created_at
      end
      Time.now - default_days.days
    end

    def fetch_recipients(since=nil)
      yield([]) if block_given?
    end

    def self.subject(subtype=nil)
      ''
    end

    def self.body
      ''
    end

    def self.new(*args)
      oo = super
      oo.body = ''
      oo.subject = self.subject
      oo
    end

    def deliver_batch_sets!
    end

    def clear_batch_sets!
      self.email_batch_sets.map(&:destroy)
    end

  end
end
