Class: Emailbutler::Adapters::ActiveRecord

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/emailbutler/adapters/active_record.rb

Defined Under Namespace

Classes: Model

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ ActiveRecord

Public: Initialize a new ActiveRecord adapter instance.

name - The Symbol name for this adapter. Optional (default :active_record) message_class - The AR class responsible for the messages table.



46
47
48
49
# File 'lib/emailbutler/adapters/active_record.rb', line 46

def initialize(options={})
  @name = options.fetch(:name, :active_record)
  @message_class = options.fetch(:message_class) { Emailbutler::Message }
end

Instance Attribute Details

#message_classObject (readonly)

Public: The name of the adapter.



40
41
42
# File 'lib/emailbutler/adapters/active_record.rb', line 40

def message_class
  @message_class
end

#nameObject (readonly)

Public: The name of the adapter.



40
41
42
# File 'lib/emailbutler/adapters/active_record.rb', line 40

def name
  @name
end

Instance Method Details

#build_message(args = {}) ⇒ Object

Public: Builds a message.



52
53
54
# File 'lib/emailbutler/adapters/active_record.rb', line 52

def build_message(args={})
  @message_class.new(args)
end

#count_messages_by_statusObject

Public: Groups messages by status and count them.



79
80
81
# File 'lib/emailbutler/adapters/active_record.rb', line 79

def count_messages_by_status
  @message_class.group(:status).count
end

#destroy_message(message) ⇒ Object

Public: Destroy the message.



97
98
99
# File 'lib/emailbutler/adapters/active_record.rb', line 97

def destroy_message(message)
  message.destroy
end

#find_message_by(args = {}) ⇒ Object

Public: Finds message by args.



67
68
69
# File 'lib/emailbutler/adapters/active_record.rb', line 67

def find_message_by(args={})
  @message_class.find_by(args)
end

#find_messages_by(args = {}) ⇒ Object

Public: Finds messages by args.



84
85
86
# File 'lib/emailbutler/adapters/active_record.rb', line 84

def find_messages_by(args={})
  @message_class.where(args).order(created_at: :desc)
end

#resend_message(message) ⇒ Object

Public: Resends the message.



89
90
91
92
93
94
# File 'lib/emailbutler/adapters/active_record.rb', line 89

def resend_message(message)
  ::ActiveRecord::Base.transaction do
    message.destroy
    resend_message_with_mailer(message)
  end
end

#save_message(message) ⇒ Object

Public: Saves the message.



62
63
64
# File 'lib/emailbutler/adapters/active_record.rb', line 62

def save_message(message)
  message.save
end

#set_message_attribute(message, attribute, value) ⇒ Object

Public: Sets attribute with value for the message.



57
58
59
# File 'lib/emailbutler/adapters/active_record.rb', line 57

def set_message_attribute(message, attribute, value)
  message[attribute] = value
end

#update_message(message, args = {}) ⇒ Object

Public: Updates the message.



72
73
74
75
76
# File 'lib/emailbutler/adapters/active_record.rb', line 72

def update_message(message, args={})
  message.update(args) if message.timestamp.nil? || args[:timestamp] > message.timestamp
rescue ::ActiveRecord::StaleObjectError
  update_message(message.reload, args)
end