app/models/message_request.rb in enju_message-0.2.4 vs app/models/message_request.rb in enju_message-0.3.0.beta.1

- old
+ new

@@ -1,18 +1,17 @@ -require 'erubis' class MessageRequest < ActiveRecord::Base include Statesman::Adapters::ActiveRecordQueries scope :not_sent, -> {in_state(:pending).where('sent_at IS NULL')} scope :sent, -> {in_state(:sent)} belongs_to :message_template, validate: true belongs_to :sender, class_name: "User", foreign_key: "sender_id", validate: true belongs_to :receiver, class_name: "User", foreign_key: "receiver_id", validate: true has_many :messages validates_associated :sender, :receiver, :message_template - validates_presence_of :sender, :receiver, :message_template - validates_presence_of :body, on: :update + validates :sender, :receiver, :message_template, presence: true + validates :body, presence: { on: :update } paginates_per 10 has_many :message_request_transitions, autosave: false @@ -24,19 +23,18 @@ to: :state_machine def send_message message = nil MessageRequest.transaction do - message = Message.new - message.sender = sender - message.recipient = receiver.username - message.subject = subject - message.body = body - message.body = 'test' - message.save! - self.sent_at = Time.zone.now - save(validate: false) + message = Message.create!( + sender: sender, + recipient: receiver.username, + subject: subject, + body: body, + message_request: self + ) + update!(sent_at: Time.zone.now) if ['reservation_expired_for_patron', 'reservation_expired_for_patron'].include?(message_template.status) self.receiver.reserves.each do |reserve| reserve.expiration_notice_to_patron = true reserve.save(validate: false) end @@ -52,11 +50,11 @@ def save_message_body(options = {}) options = { receiver: receiver, locale: receiver.profile.locale }.merge(options) - update_attributes!({body: message_template.embed_body(options)}) + update!({body: message_template.embed_body(options)}) end def self.send_messages count = MessageRequest.not_sent.size MessageRequest.not_sent.each do |request| @@ -64,9 +62,10 @@ end logger.info "#{Time.zone.now} sent #{count} messages!" end private + def self.transition_class MessageRequestTransition end def self.initial_state