diff --git a/lib/has_messages.rb b/lib/has_messages.rb index 56d677e..5772978 100644 --- a/lib/has_messages.rb +++ b/lib/has_messages.rb @@ -38,27 +38,22 @@ module MacroMethods # user = User.find(123) # user.sent_messages def has_messages - has_many :messages, + has_many :messages, -> { where(:hidden_at => nil).order('messages.created_at DESC') }, :as => :sender, - :class_name => 'Message', - :conditions => {:hidden_at => nil}, - :order => 'messages.created_at DESC' - has_many :received_messages, + :class_name => 'Message' + has_many :received_messages, -> { includes('message') + .where('message_recipients.hidden_at IS NULL AND messages.state = ?', 'sent') + .order('messages.created_at DESC') }, :as => :receiver, - :class_name => 'MessageRecipient', - :include => :message, - :conditions => ['message_recipients.hidden_at IS NULL AND messages.state = ?', 'sent'], - :order => 'messages.created_at DESC' + :class_name => 'MessageRecipient' include HasMessages::HasMessagesInstanceMethods end def acts_as_message_topic - has_many :topical_messages, + has_many :topical_messages, -> { order('messages.created_at DESC').where(:hidden_at => nil) }, :as => :topic, - :class_name => 'Message', - :conditions => {:hidden_at => nil}, - :order => 'messages.created_at DESC' + :class_name => 'Message' include HasMessages::ActsAsMessageTopicInstanceMethods end diff --git a/lib/has_messages/models/message.rb b/lib/has_messages/models/message.rb index 9812fc7..d4cfb44 100644 --- a/lib/has_messages/models/message.rb +++ b/lib/has_messages/models/message.rb @@ -31,17 +31,15 @@ class Message < ActiveRecord::Base belongs_to :backup_topic, :polymorphic => true belongs_to :original_message, :class_name => 'Message' - has_many :recipients, :class_name => 'MessageRecipient', :order => 'kind DESC, position ASC', :dependent => :destroy + has_many :recipients, -> { order('kind DESC, position ASC') }, :class_name => 'MessageRecipient', :dependent => :destroy - scope :with_topic, lambda { |topic| where :topic_id => topic.id, :topic_type => topic.class } - scope :with_receiver, lambda { |receiver| joins(:recipients).merge(MessageRecipient.with_receiver(receiver)) } + scope :with_topic, ->(topic) { where :topic_id => topic.id, :topic_type => topic.class } + scope :with_receiver, ->(receiver) { joins(:recipients).merge(MessageRecipient.with_receiver(receiver)) } - scope :sent, where(:state => :sent) + scope :sent, -> { where(:state => :sent) } validates_presence_of :state, :sender_id, :sender_type - attr_accessible :subject, :body, :to, :cc, :bcc - after_save :update_recipients scope :visible, :conditions => {:hidden_at => nil} diff --git a/lib/has_messages/models/message_recipient.rb b/lib/has_messages/models/message_recipient.rb index 2470082..1061834 100644 --- a/lib/has_messages/models/message_recipient.rb +++ b/lib/has_messages/models/message_recipient.rb @@ -43,7 +43,7 @@ class MessageRecipient < ActiveRecord::Base delegate :sender, :subject, :body, :recipients, :to, :cc, :bcc, :created_at, :thread, :topic, :to => :message - scope :visible, :conditions => {:hidden_at => nil} + scope :visible, -> { where(:hidden_at => nil) } # Defines actions for the recipient state_machine :state, :initial => :unread do