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.



42
43
44
45
# File 'lib/emailbutler/adapters/active_record.rb', line 42

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.



36
37
38
# File 'lib/emailbutler/adapters/active_record.rb', line 36

def message_class
  @message_class
end

#nameObject (readonly)

Public: The name of the adapter.



36
37
38
# File 'lib/emailbutler/adapters/active_record.rb', line 36

def name
  @name
end

Instance Method Details

#build_message(args = {}) ⇒ Object

Public: Builds a message.



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

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

#count_messages_by_statusObject

Public: Groups messages by status and count them.



75
76
77
# File 'lib/emailbutler/adapters/active_record.rb', line 75

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

#destroy_message(message) ⇒ Object

Public: Destroy the message.



93
94
95
# File 'lib/emailbutler/adapters/active_record.rb', line 93

def destroy_message(message)
  message.destroy
end

#find_message_by(args = {}) ⇒ Object

Public: Finds message by args.



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

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

#find_messages_by(args = {}) ⇒ Object

Public: Finds messages by args.



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

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

#resend_message(message) ⇒ Object

Public: Resends the message.



85
86
87
88
89
90
# File 'lib/emailbutler/adapters/active_record.rb', line 85

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.



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

def save_message(message)
  message.save
end

#set_message_attribute(message, attribute, value) ⇒ Object

Public: Sets attribute with value for the message.



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

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

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

Public: Updates the message.



68
69
70
71
72
# File 'lib/emailbutler/adapters/active_record.rb', line 68

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