Skip to content
This repository has been archived by the owner on Oct 8, 2024. It is now read-only.

Commit

Permalink
Makes has_messages compatible with Rails 4
Browse files Browse the repository at this point in the history
  • Loading branch information
briancantrell committed Jun 24, 2014
1 parent 5e8a1a2 commit 5879ad6
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 20 deletions.
21 changes: 8 additions & 13 deletions lib/has_messages.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 4 additions & 6 deletions lib/has_messages/models/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
2 changes: 1 addition & 1 deletion lib/has_messages/models/message_recipient.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 5879ad6

Please sign in to comment.