1
0
Эх сурвалжийг харах

pull some email logic into an EmailConcern

Albert Sun 11 жил өмнө
parent
commit
3ea7b322ff

+ 37 - 0
app/concerns/email_concern.rb

@@ -0,0 +1,37 @@
+module EmailConcern
+  extend ActiveSupport::Concern
+
+  MAIN_KEYS = %w[title message text main value].map(&:to_sym)
+
+  included do
+    self.validate :validate_email_options
+  end
+
+  def validate_email_options
+    errors.add(:base, "subject and expected_receive_period_in_days are required") unless options[:subject].present? && options[:expected_receive_period_in_days].present?
+  end
+
+  def working?
+    last_receive_at && last_receive_at > options[:expected_receive_period_in_days].to_i.days.ago && !recent_error_logs?
+  end
+
+  def present(payload)
+    if payload.is_a?(Hash)
+      payload = ActiveSupport::HashWithIndifferentAccess.new(payload)
+      MAIN_KEYS.each do |key|
+        return { :title => payload[key].to_s, :entries => present_hash(payload, key) } if payload.has_key?(key)
+      end
+
+      { :title => "Event", :entries => present_hash(payload) }
+    else
+      { :title => payload.to_s, :entries => [] }
+    end
+  end
+
+  def present_hash(hash, skip_key = nil)
+    hash.to_a.sort_by {|a| a.first.to_s }.map { |k, v| "#{k}: #{v}" unless k.to_s == skip_key.to_s }.compact
+  end
+
+  module ClassMethods
+  end
+end

+ 2 - 26
app/models/agents/digest_email_agent.rb

@@ -1,6 +1,7 @@
 module Agents
   class DigestEmailAgent < Agent
-    MAIN_KEYS = %w[title message text main value].map(&:to_sym)
+    include EmailConcern
+
     default_schedule "5am"
 
     cannot_create_events!
@@ -22,14 +23,6 @@ module Agents
       }
     end
 
-    def working?
-      last_receive_at && last_receive_at > options[:expected_receive_period_in_days].to_i.days.ago && !recent_error_logs?
-    end
-
-    def validate_options
-      errors.add(:base, "subject and expected_receive_period_in_days are required") unless options[:subject].present? && options[:expected_receive_period_in_days].present?
-    end
-
     def receive(incoming_events)
       incoming_events.each do |event|
         self.memory[:queue] ||= []
@@ -49,22 +42,5 @@ module Agents
         self.memory[:events] = []
       end
     end
-
-    def present(payload)
-      if payload.is_a?(Hash)
-        payload = ActiveSupport::HashWithIndifferentAccess.new(payload)
-        MAIN_KEYS.each do |key|
-          return { :title => payload[key].to_s, :entries => present_hash(payload, key) } if payload.has_key?(key)
-        end
-
-        { :title => "Event", :entries => present_hash(payload) }
-      else
-        { :title => payload.to_s, :entries => [] }
-      end
-    end
-
-    def present_hash(hash, skip_key = nil)
-      hash.to_a.sort_by {|a| a.first.to_s }.map { |k, v| "#{k}: #{v}" unless k.to_s == skip_key.to_s }.compact
-    end
   end
 end

+ 1 - 26
app/models/agents/event_email_agent.rb

@@ -1,6 +1,6 @@
 module Agents
   class EventEmailAgent < Agent
-    MAIN_KEYS = %w[title message text main value].map(&:to_sym)
+    include EmailConcern
 
     cannot_be_scheduled!
     cannot_create_events!
@@ -22,36 +22,11 @@ module Agents
       }
     end
 
-    def working?
-      last_receive_at && last_receive_at > options[:expected_receive_period_in_days].to_i.days.ago && !recent_error_logs?
-    end
-
-    def validate_options
-      errors.add(:base, "subject and expected_receive_period_in_days are required") unless options[:subject].present? && options[:expected_receive_period_in_days].present?
-    end
-
     def receive(incoming_events)
       incoming_events.each do |event|
         log "Sending digest mail to #{user.email} with event #{event.id}"
         SystemMailer.delay.send_message(:to => user.email, :subject => options[:subject], :headline => options[:headline], :groups => [present(event.payload)])
       end
     end
-
-    def present(payload)
-      if payload.is_a?(Hash)
-        payload = ActiveSupport::HashWithIndifferentAccess.new(payload)
-        MAIN_KEYS.each do |key|
-          return { :title => payload[key].to_s, :entries => present_hash(payload, key) } if payload.has_key?(key)
-        end
-
-        { :title => "Event", :entries => present_hash(payload) }
-      else
-        { :title => payload.to_s, :entries => [] }
-      end
-    end
-
-    def present_hash(hash, skip_key = nil)
-      hash.to_a.sort_by {|a| a.first.to_s }.map { |k, v| "#{k}: #{v}" unless k.to_s == skip_key.to_s }.compact
-    end
   end
 end